source: icGREP/icgrep-devel/icgrep/combine/pugixml/docs/samples/include.cpp @ 5516

Last change on this file since 5516 was 5516, checked in by faldebey, 2 years ago

added combinatorial testing tool

File size: 1.7 KB
RevLine 
[5516]1#include "pugixml.hpp"
2
3#include <string.h>
4#include <iostream>
5
6// tag::code[]
7bool load_preprocess(pugi::xml_document& doc, const char* path);
8
9bool 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
48bool 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
56int 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
Note: See TracBrowser for help on using the repository browser.