From: sgunderson@bigfoot.com <> Date: Wed, 21 Sep 2011 19:34:06 +0000 (+0200) Subject: Split out the C++0x library into a separate file from the simple unit test. X-Git-Url: https://git.sesse.net/?p=xml-template;a=commitdiff_plain;h=59996859ced59ac9fdb307b2eb2130b48d4d4f63 Split out the C++0x library into a separate file from the simple unit test. --- diff --git a/c++0x/Makefile b/c++0x/Makefile index 979a60f..2d8850d 100644 --- a/c++0x/Makefile +++ b/c++0x/Makefile @@ -1,6 +1,7 @@ CXX=g++ CXXFLAGS=-std=gnu++0x -g -Wall $(shell xml2-config --cflags) +OBJS=simple.o xml-template.o -simple: simple.o - $(CXX) -o $@ $< $(shell xml2-config --libs) +simple: $(OBJS) + $(CXX) -o $@ $(OBJS) $(shell xml2-config --libs) diff --git a/c++0x/simple.cpp b/c++0x/simple.cpp index 69480e9..05c7ea6 100644 --- a/c++0x/simple.cpp +++ b/c++0x/simple.cpp @@ -2,68 +2,10 @@ #include #include -#include -#include -#include +#include "xml-template.h" using namespace std; -class Directive { - public: - virtual void process(xmlNode *node, bool clean) = 0; -}; - -class Replace : public Directive { - public: - Replace(const string &str) : str(str) {} - virtual void process(xmlNode *node, bool clean) { - node->children = xmlNewTextLen(reinterpret_cast(str.data()), str.size()); - } - - private: - const string str; -}; - -class Substitute : public Directive { - public: - Substitute(const unordered_map &substitution_map) - : substitution_map(substitution_map) {} - - virtual void process(xmlNode *node, bool clean) { - for (xmlNode *child = node->children; child != NULL; child = child->next) { - bool processed = false; - - if (child->type == XML_ELEMENT_NODE) { - // Find the ID, if any. - string id; - for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) { - if (strcmp(reinterpret_cast(attr->ns->href), "http://template.sesse.net/") == 0 && - strcmp(reinterpret_cast(attr->name), "id") == 0) { - id = reinterpret_cast(xmlNodeGetContent(attr->children)); - } - } - - // Check all substitutions to see if we found anything appropriate. - for (auto it : substitution_map) { - if (it.first == reinterpret_cast(child->name) || - (!id.empty() && it.first == ("#" + id))) { - it.second->process(child, clean); - processed = true; - break; - } - } - } - - if (!processed) { - process(child, clean); - } - } - } - - private: - const unordered_map &substitution_map; -}; - int main(int argc, char **argv) { LIBXML_TEST_VERSION diff --git a/c++0x/xml-template.cpp b/c++0x/xml-template.cpp new file mode 100644 index 0000000..a158f31 --- /dev/null +++ b/c++0x/xml-template.cpp @@ -0,0 +1,46 @@ +#include "xml-template.h" + +#include + +using namespace std; + +Replace::Replace(const string &str) + : str(str) {} + +void Replace::process(xmlNode *node, bool clean) { + node->children = xmlNewTextLen(reinterpret_cast(str.data()), str.size()); +} + +Substitute::Substitute(const unordered_map &substitution_map) + : substitution_map(substitution_map) {} + +void Substitute::process(xmlNode *node, bool clean) { + for (xmlNode *child = node->children; child != NULL; child = child->next) { + bool processed = false; + + if (child->type == XML_ELEMENT_NODE) { + // Find the ID, if any. + string id; + for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) { + if (strcmp(reinterpret_cast(attr->ns->href), "http://template.sesse.net/") == 0 && + strcmp(reinterpret_cast(attr->name), "id") == 0) { + id = reinterpret_cast(xmlNodeGetContent(attr->children)); + } + } + + // Check all substitutions to see if we found anything appropriate. + for (auto it : substitution_map) { + if (it.first == reinterpret_cast(child->name) || + (!id.empty() && it.first == ("#" + id))) { + it.second->process(child, clean); + processed = true; + break; + } + } + } + + if (!processed) { + process(child, clean); + } + } +} diff --git a/c++0x/xml-template.h b/c++0x/xml-template.h new file mode 100644 index 0000000..cd417e2 --- /dev/null +++ b/c++0x/xml-template.h @@ -0,0 +1,34 @@ +#ifndef _XML_TEMPLATE_H +#define _XML_TEMPLATE_H 1 + +#include + +#include +#include +#include + +class Directive { + public: + virtual void process(xmlNode *node, bool clean) = 0; +}; + +class Replace : public Directive { + public: + Replace(const std::string &str); + virtual void process(xmlNode *node, bool clean); + + private: + const std::string str; +}; + +class Substitute : public Directive { + public: + Substitute(const std::unordered_map &substitution_map); + + virtual void process(xmlNode *node, bool clean); + + private: + const std::unordered_map &substitution_map; +}; + +#endif // !defined(_XML_TEMPLATE_H)