commit 3c17f4c11c9f4183b3ed5a3ed8423858a058de8c Author: Daniel Swanson Date: Mon Jun 14 11:37:09 2021 -0500 move xml iterator to lttoolbox diff --git a/lttoolbox/Makefile.am b/lttoolbox/Makefile.am index 44c38f9..2fd56b0 100644 --- a/lttoolbox/Makefile.am +++ b/lttoolbox/Makefile.am @@ -3,13 +3,13 @@ h_sources = alphabet.h att_compiler.h buffer.h compiler.h compression.h \ deserialiser.h entry_token.h expander.h fst_processor.h input_file.h lt_locale.h \ match_exe.h match_node.h match_state.h my_stdio.h node.h \ pattern_list.h regexp_compiler.h serialiser.h sorted_vector.h state.h string_utils.h \ - transducer.h trans_exe.h xml_parse_util.h exception.h tmx_compiler.h \ + transducer.h trans_exe.h xml_parse_util.h xml_walk_util.h exception.h tmx_compiler.h \ ustring.h cc_sources = alphabet.cc att_compiler.cc compiler.cc compression.cc entry_token.cc \ expander.cc fst_processor.cc input_file.cc lt_locale.cc match_exe.cc \ match_node.cc match_state.cc node.cc pattern_list.cc \ regexp_compiler.cc sorted_vector.cc state.cc string_utils.cc transducer.cc \ - trans_exe.cc xml_parse_util.cc tmx_compiler.cc ustring.cc + trans_exe.cc xml_parse_util.cc xml_walk_util.cc tmx_compiler.cc ustring.cc library_includedir = $(includedir)/$(PACKAGE_NAME)-$(VERSION_API)/$(PACKAGE_NAME) library_include_HEADERS = $(h_sources) diff --git a/lttoolbox/xml_walk_util.cc b/lttoolbox/xml_walk_util.cc new file mode 100644 index 0000000..8611556 --- /dev/null +++ b/lttoolbox/xml_walk_util.cc @@ -0,0 +1,65 @@ +#include + +children::children(xmlNode* node_) + : node(node_), cur(node->children) +{ + while (cur && cur->type != XML_ELEMENT_NODE) { + cur = cur->next; + } +} + +children::children(const children& it) + : node(it.node), cur(it.cur) +{} + +children::~children() +{} // we don't own the pointers, so we don't delete them + +children& +children::operator++() +{ + if (node && cur) { + cur = cur->next; + while (cur && cur->type != XML_ELEMENT_NODE) { + cur = cur->next; + } + } + return *this; +} + +children +children::begin() +{ + return children(node); +} + +children +children::end() +{ + children ret(node); + ret.cur = nullptr; + return ret; +} + +bool +children::operator!=(const children& other) const +{ + return node != other.node || cur != other.cur; +} + +bool +children::operator==(const children& other) const +{ + return node == other.node && cur == other.cur; +} + +UString +getattr(xmlNode* node, const char* attr) +{ + for (xmlAttr* i = node->properties; i != NULL; i = i->next) { + if (!xmlStrcmp(i->name, (const xmlChar*) attr)) { + return to_ustring((const char*) i->children->content); + } + } + return ""_u; +} diff --git a/lttoolbox/xml_walk_util.h b/lttoolbox/xml_walk_util.h new file mode 100644 index 0000000..13ca6a4 --- /dev/null +++ b/lttoolbox/xml_walk_util.h @@ -0,0 +1,29 @@ +#ifndef _XML_WALK_UTIL_ +#define _XML_WALK_UTIL_ + +#include + +#include +#include + +class children +{ +private: + xmlNode* node; + xmlNode* cur; +public: + children(xmlNode* node); + children(const children& it); + ~children(); + + children& operator++(); + children begin(); + children end(); + inline xmlNode* operator*() const { return cur; } + bool operator!=(const children& other) const; + bool operator==(const children& other) const; +}; + +UString getattr(xmlNode* node, const char* attr); + +#endif