Mercurial > minori
comparison dep/pugixml/docs/samples/include.cpp @ 367:8d45d892be88 default tip
*: instead of pugixml, use Qt XML features
this means we have one extra Qt dependency though...
author | Paper <paper@tflc.us> |
---|---|
date | Sun, 17 Nov 2024 22:55:47 -0500 |
parents | 886f66775f31 |
children |
comparison
equal
deleted
inserted
replaced
366:886f66775f31 | 367:8d45d892be88 |
---|---|
1 #include "pugixml.hpp" | |
2 | |
3 #include <string.h> | |
4 #include <iostream> | |
5 | |
6 // tag::code[] | |
7 bool load_preprocess(pugi::xml_document& doc, const char* path); | |
8 | |
9 bool preprocess(pugi::xml_node node) | |
10 { | |
11 for (pugi::xml_node child = node.first_child(); child; ) | |
12 { | |
13 if (child.type() == pugi::node_pi && strcmp(child.name(), "include") == 0) | |
14 { | |
15 pugi::xml_node include = child; | |
16 | |
17 // load new preprocessed document (note: ideally this should handle relative paths) | |
18 const char* path = include.value(); | |
19 | |
20 pugi::xml_document doc; | |
21 if (!load_preprocess(doc, path)) return false; | |
22 | |
23 // insert the comment marker above include directive | |
24 node.insert_child_before(pugi::node_comment, include).set_value(path); | |
25 | |
26 // copy the document above the include directive (this retains the original order!) | |
27 for (pugi::xml_node ic = doc.first_child(); ic; ic = ic.next_sibling()) | |
28 { | |
29 node.insert_copy_before(ic, include); | |
30 } | |
31 | |
32 // remove the include node and move to the next child | |
33 child = child.next_sibling(); | |
34 | |
35 node.remove_child(include); | |
36 } | |
37 else | |
38 { | |
39 if (!preprocess(child)) return false; | |
40 | |
41 child = child.next_sibling(); | |
42 } | |
43 } | |
44 | |
45 return true; | |
46 } | |
47 | |
48 bool load_preprocess(pugi::xml_document& doc, const char* path) | |
49 { | |
50 pugi::xml_parse_result result = doc.load_file(path, pugi::parse_default | pugi::parse_pi); // for <?include?> | |
51 | |
52 return result ? preprocess(doc) : false; | |
53 } | |
54 // end::code[] | |
55 | |
56 int main() | |
57 { | |
58 pugi::xml_document doc; | |
59 if (!load_preprocess(doc, "character.xml")) return -1; | |
60 | |
61 doc.print(std::cout); | |
62 } | |
63 | |
64 // vim:et |