Mercurial > minori
comparison dep/pugixml/docs/samples/traverse_predicate.cpp @ 55:d10b6c6b432e
add xml lib, we will need to use it eventually
| author | Paper <mrpapersonic@gmail.com> | 
|---|---|
| date | Tue, 26 Sep 2023 12:37:08 -0400 | 
| parents | |
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 54:466ac9870df9 | 55:d10b6c6b432e | 
|---|---|
| 1 #include "pugixml.hpp" | |
| 2 | |
| 3 #include <string.h> | |
| 4 #include <iostream> | |
| 5 | |
| 6 // tag::decl[] | |
| 7 bool small_timeout(pugi::xml_node node) | |
| 8 { | |
| 9 return node.attribute("Timeout").as_int() < 20; | |
| 10 } | |
| 11 | |
| 12 struct allow_remote_predicate | |
| 13 { | |
| 14 bool operator()(pugi::xml_attribute attr) const | |
| 15 { | |
| 16 return strcmp(attr.name(), "AllowRemote") == 0; | |
| 17 } | |
| 18 | |
| 19 bool operator()(pugi::xml_node node) const | |
| 20 { | |
| 21 return node.attribute("AllowRemote").as_bool(); | |
| 22 } | |
| 23 }; | |
| 24 // end::decl[] | |
| 25 | |
| 26 int main() | |
| 27 { | |
| 28 pugi::xml_document doc; | |
| 29 if (!doc.load_file("xgconsole.xml")) return -1; | |
| 30 | |
| 31 pugi::xml_node tools = doc.child("Profile").child("Tools"); | |
| 32 | |
| 33 // tag::find[] | |
| 34 // Find child via predicate (looks for direct children only) | |
| 35 std::cout << tools.find_child(allow_remote_predicate()).attribute("Filename").value() << std::endl; | |
| 36 | |
| 37 // Find node via predicate (looks for all descendants in depth-first order) | |
| 38 std::cout << doc.find_node(allow_remote_predicate()).attribute("Filename").value() << std::endl; | |
| 39 | |
| 40 // Find attribute via predicate | |
| 41 std::cout << tools.last_child().find_attribute(allow_remote_predicate()).value() << std::endl; | |
| 42 | |
| 43 // We can use simple functions instead of function objects | |
| 44 std::cout << tools.find_child(small_timeout).attribute("Filename").value() << std::endl; | |
| 45 // end::find[] | |
| 46 } | |
| 47 | |
| 48 // vim:et | 
