]> git.sesse.net Git - xml-template/blob - c++0x/xml-template.cpp
Fix the worst (but not all) memory leaks in the C++0x version.
[xml-template] / c++0x / xml-template.cpp
1 #include "xml-template.h"
2
3 #include <string.h>
4 #include <libxml/parser.h>
5
6 using namespace std;
7
8 namespace {
9
10 void clean_node(xmlNode *node)
11 {
12         if (node->type != XML_ELEMENT_NODE) {
13                 return;
14         }
15         if (strcmp(reinterpret_cast<const char *>(node->ns->href), "http://template.sesse.net/") == 0) {
16                 xmlNode *frag = xmlNewDocFragment(node->doc);
17                 xmlReplaceNode(node, frag);
18                 frag->children = node->children;
19         }
20 }
21
22 }  // namespace
23
24 Directive::~Directive() {}
25
26 Replace::Replace(const string &str)
27         : str(str) {}
28
29 void Replace::process(xmlNode *node, bool clean) {
30         node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
31         if (clean) {
32                 clean_node(node);
33         }
34 }
35
36 Clone::Clone(const vector<Directive *> &subdirectives)
37         : subdirectives(subdirectives) {}
38         
39 Clone::Clone(initializer_list<Directive *> subdirectives)
40         : subdirectives(subdirectives) {}
41
42 Clone::~Clone()
43 {
44         for (auto it : subdirectives) {
45                 delete it;
46         }
47 }
48
49 void Clone::process(xmlNode *node, bool clean)
50 {
51         // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
52         // how to clone elements.
53         vector<xmlNode *> new_nodes;
54
55         for (auto it : subdirectives) {
56                 xmlNode *new_node;
57                 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
58                 it->process(new_node, clean);
59                 for (xmlNode *child = new_node->children; child != NULL; child = child->next) {
60                         new_nodes.push_back(child);
61                 }
62         }
63
64         node->children = NULL;
65         for (auto child : new_nodes) {
66                 xmlAddChild(node, child);
67         }       
68         if (clean) {
69                 clean_node(node);
70         }
71 }
72
73 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
74         : substitution_map(substitution_map) {}
75         
76 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
77         : substitution_map(substitution_map) {}
78
79 Substitute::~Substitute()
80 {
81         for (auto it : substitution_map) {
82                 delete it.second;
83         }
84 }
85
86 void Substitute::process(xmlNode *node, bool clean)
87 {
88         for (xmlNode *child = node->children; child != NULL; child = child->next) {
89                 bool processed = false;
90
91                 if (child->type == XML_ELEMENT_NODE) {
92                         // Find the ID, if any.
93                         string id;
94                         for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
95                                 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
96                                     strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
97                                         id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
98
99                                         if (clean) {
100                                                 if (attr->prev == NULL) {
101                                                         child->properties = attr->next;
102                                                 } else {
103                                                         attr->prev->next = attr->next;
104                                                 }
105                                         }
106                                 }
107                         }
108
109                         // Check all substitutions to see if we found anything appropriate.
110                         for (auto it : substitution_map) {
111                                 if (it.first == reinterpret_cast<const char *>(child->name) ||
112                                     (!id.empty() && it.first == ("#" + id))) {
113                                         it.second->process(child, clean);
114                                         processed = true;
115                                         break;
116                                 }
117                         }
118                 }
119                 
120                 if (!processed) {
121                         process(child, clean);
122                 }
123         }
124         if (clean) {
125                 clean_node(node);
126         }
127 }
128         
129 void process_file(const string &input_filename,
130                   const string &output_filename,
131                   Directive *root_directive)
132 {
133         LIBXML_TEST_VERSION
134
135         xmlDocPtr doc = xmlParseFile(input_filename.c_str());
136         root_directive->process(xmlDocGetRootElement(doc), true);
137         xmlSaveFile(output_filename.c_str(), doc);
138         xmlFreeDoc(doc);
139
140         xmlCleanupParser();
141         xmlMemoryDump();
142 }