]> git.sesse.net Git - xml-template/commitdiff
Split out the C++0x library into a separate file from the simple unit test.
authorsgunderson@bigfoot.com <>
Wed, 21 Sep 2011 19:34:06 +0000 (21:34 +0200)
committersgunderson@bigfoot.com <>
Wed, 21 Sep 2011 19:34:06 +0000 (21:34 +0200)
c++0x/Makefile
c++0x/simple.cpp
c++0x/xml-template.cpp [new file with mode: 0644]
c++0x/xml-template.h [new file with mode: 0644]

index 979a60f9ad59cf7a3a605e5fff55e14ba41777be..2d8850d9f5b591030738262457a4cc09c2e441c5 100644 (file)
@@ -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)
 
index 69480e9c4abf21bb3ea619a554d7c2e77c6fc40c..05c7ea64f6ad4a49338412173c8814c2d65ec290 100644 (file)
@@ -2,68 +2,10 @@
 #include <string.h>
 #include <libxml/parser.h>
 
-#include <string>
-#include <utility>
-#include <unordered_map>
+#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<const xmlChar *>(str.data()), str.size());
-       }
-
- private:
-       const string str;
-};
-
-class Substitute : public Directive {
- public:
-       Substitute(const unordered_map<string, Directive*> &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<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
-                                           strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
-                                               id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
-                                       }
-                               }
-
-                               // Check all substitutions to see if we found anything appropriate.
-                               for (auto it : substitution_map) {
-                                       if (it.first == reinterpret_cast<const char *>(child->name) ||
-                                           (!id.empty() && it.first == ("#" + id))) {
-                                               it.second->process(child, clean);
-                                               processed = true;
-                                               break;
-                                       }
-                               }
-                       }
-                       
-                       if (!processed) {
-                               process(child, clean);
-                       }
-               }
-       }
-
- private:
-       const unordered_map<string, Directive*> &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 (file)
index 0000000..a158f31
--- /dev/null
@@ -0,0 +1,46 @@
+#include "xml-template.h"
+
+#include <string.h>
+
+using namespace std;
+
+Replace::Replace(const string &str)
+       : str(str) {}
+
+void Replace::process(xmlNode *node, bool clean) {
+       node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
+}
+
+Substitute::Substitute(const unordered_map<string, Directive*> &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<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
+                                   strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
+                                       id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
+                               }
+                       }
+
+                       // Check all substitutions to see if we found anything appropriate.
+                       for (auto it : substitution_map) {
+                               if (it.first == reinterpret_cast<const char *>(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 (file)
index 0000000..cd417e2
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef _XML_TEMPLATE_H
+#define _XML_TEMPLATE_H 1
+
+#include <libxml/tree.h>
+
+#include <string>
+#include <utility>
+#include <unordered_map>
+
+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<std::string, Directive*> &substitution_map);
+
+       virtual void process(xmlNode *node, bool clean);
+
+ private:
+       const std::unordered_map<std::string, Directive*> &substitution_map;
+};
+
+#endif  // !defined(_XML_TEMPLATE_H)