Skip to content

Commit

Permalink
Fix bug, add xmlite::xmlnode::exists
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Jan 13, 2022
1 parent 6183a7f commit eb2fd1e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
Binary file modified C_bindings/bin/libxmlite.a
Binary file not shown.
1 change: 1 addition & 0 deletions C_bindings/include/xmlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef struct xmlite_xmlnode_IdxVec

} xmlite_xmlnode_IdxVec_t;

bool xmlite_xmlnode_exists(const xmlite_xmlnode_t * obj, const char * str, size_t strLen);
xmlite_xmlnode_IdxVec_t xmlite_xmlnode_atStr(const xmlite_xmlnode_t * obj, const char * str, size_t length);
xmlite_xmlnode_constref_t xmlite_xmlnode_atNum(const xmlite_xmlnode_t * obj, size_t idx);
xmlite_xmlnode_ref_t xmlite_xmlnode_idxNum(xmlite_xmlnode_t * obj, size_t idx);
Expand Down
14 changes: 14 additions & 0 deletions C_bindings/src/xmlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ bool xmlite_xmlnode_attrRemove(xmlite_xmlnode_t * obj, const char * key, size_t
}
}

bool xmlite_xmlnode_exists(const xmlite_xmlnode_t * obj, const char * str, size_t strLen)
{
strLen = xmlite::strlen(str, strLen);

try
{
return static_cast<const xmlite::xmlnode *>(obj->mem)->exists({ str, strLen });
}
catch (std::exception & e)
{
inner::s_lastException = std::move(e);
return false;
}
}
xmlite_xmlnode_IdxVec_t xmlite_xmlnode_atStr(const xmlite_xmlnode_t * obj, const char * str, size_t length)
{
length = xmlite::strlen(str, length);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# xmlite

![Release version](https://img.shields.io/badge/alpha-v0.9.1-green.svg)
![Release version](https://img.shields.io/badge/alpha-v0.9.2-green.svg)
![C++ version](https://img.shields.io/badge/version-C++11-blue.svg)

A light-weight single-include C++ XML library, relying on KISS (Keep It Stupid Simple)
Expand Down
49 changes: 31 additions & 18 deletions include/xmlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <type_traits>
#include <exception>

#include <iostream>

#include <cstring>
#include <cstdint>

Expand Down Expand Up @@ -233,6 +231,10 @@ namespace xmlite
return this->m_attributes;
}

bool exists(const std::string & str) const noexcept
{
return this->m_idxMap.find(str) != this->m_idxMap.end();
}
const IdxVec & at(const std::string & str) const
{
return this->m_idxMap.at(str);
Expand Down Expand Up @@ -1057,7 +1059,7 @@ inline xmlite::xmlnode xmlite::xmlnode::innerParse(const char * xml, std::size_t
{
ended = true;
}
tagEnd = it - ended;
tagEnd = it + 1;
break;
}
}
Expand Down Expand Up @@ -1174,6 +1176,10 @@ inline xmlite::xmlnode xmlite::xmlnode::innerParse(const char * xml, std::size_t
parseTagContents(start, end);
break;
}
else if (ended == true)
{
++start;
}
}

return node;
Expand Down Expand Up @@ -1216,22 +1222,11 @@ inline xmlite::xmlnode::xmlnode(const char * xmlFile, std::size_t length)

inline std::string xmlite::xmlnode::innerDump(std::size_t depth) const
{
if (this->m_role == objtype::EndPoint)
{
std::string str;
for (std::size_t i = 0; i < depth; ++i)
{
str += '\t';
}
return str + this->m_tag;
}
else if (this->m_role == objtype::Object)
if (this->m_role == objtype::Object)
{
std::string str;
for (std::size_t i = 0; i < depth; ++i)
{
str += '\t';
}
str.append(depth, '\t');

str += '<' + this->m_tag;
for (const auto & i : this->m_attributes)
{
Expand All @@ -1240,7 +1235,7 @@ inline std::string xmlite::xmlnode::innerDump(std::size_t depth) const

str += '>';

for (auto i : this->m_values)
for (const auto & i : this->m_values)
{
str += '\n';
str += i.innerDump(depth + 1);
Expand All @@ -1255,6 +1250,24 @@ inline std::string xmlite::xmlnode::innerDump(std::size_t depth) const

return str;
}
else if (this->m_role == objtype::EndPoint)
{
return std::string(depth, '\t') + this->m_tag;
}
else if (!this->m_attributes.empty())
{
std::string str;
str.append(depth, '\t');

str += '<' + this->m_tag;
for (const auto & i : this->m_attributes)
{
str += ' ' + i.first + "=\"" + i.second + '"';
}
str += "/>";

return str;
}
else
{
return {};
Expand Down

0 comments on commit eb2fd1e

Please sign in to comment.