pugixml.hpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /**
  2. * pugixml parser - version 1.7
  3. * --------------------------------------------------------
  4. * Copyright (C) 2006-2015, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
  5. * Report bugs and download new versions at http://pugixml.org/
  6. *
  7. * This library is distributed under the MIT License. See notice at the end
  8. * of this file.
  9. *
  10. * This work is based on the pugxml parser, which is:
  11. * Copyright (C) 2003, by Kristen Wegner (kristen@tima.net)
  12. */
  13. #ifndef PUGIXML_VERSION
  14. // Define version macro; evaluates to major * 100 + minor so that it's safe to use in less-than comparisons
  15. # define PUGIXML_VERSION 170
  16. #endif
  17. // Include user configuration file (this can define various configuration macros)
  18. #include "pugiconfig.hpp"
  19. #ifndef HEADER_PUGIXML_HPP
  20. #define HEADER_PUGIXML_HPP
  21. // Include stddef.h for size_t and ptrdiff_t
  22. #include <stddef.h>
  23. // Include exception header for XPath
  24. #if !defined(PUGIXML_NO_XPATH) && !defined(PUGIXML_NO_EXCEPTIONS)
  25. # include <exception>
  26. #endif
  27. // Include STL headers
  28. #ifndef PUGIXML_NO_STL
  29. # include <iterator>
  30. # include <iosfwd>
  31. # include <string>
  32. #endif
  33. // Macro for deprecated features
  34. #ifndef PUGIXML_DEPRECATED
  35. # if defined(__GNUC__)
  36. # define PUGIXML_DEPRECATED __attribute__((deprecated))
  37. # elif defined(_MSC_VER) && _MSC_VER >= 1300
  38. # define PUGIXML_DEPRECATED __declspec(deprecated)
  39. # else
  40. # define PUGIXML_DEPRECATED
  41. # endif
  42. #endif
  43. // If no API is defined, assume default
  44. #ifndef PUGIXML_API
  45. # define PUGIXML_API
  46. #endif
  47. // If no API for classes is defined, assume default
  48. #ifndef PUGIXML_CLASS
  49. # define PUGIXML_CLASS PUGIXML_API
  50. #endif
  51. // If no API for functions is defined, assume default
  52. #ifndef PUGIXML_FUNCTION
  53. # define PUGIXML_FUNCTION PUGIXML_API
  54. #endif
  55. // If the platform is known to have long long support, enable long long functions
  56. #ifndef PUGIXML_HAS_LONG_LONG
  57. # if __cplusplus >= 201103
  58. # define PUGIXML_HAS_LONG_LONG
  59. # elif defined(_MSC_VER) && _MSC_VER >= 1400
  60. # define PUGIXML_HAS_LONG_LONG
  61. # endif
  62. #endif
  63. // Character interface macros
  64. #ifdef PUGIXML_WCHAR_MODE
  65. # define PUGIXML_TEXT(t) L ## t
  66. # define PUGIXML_CHAR wchar_t
  67. #else
  68. # define PUGIXML_TEXT(t) t
  69. # define PUGIXML_CHAR char
  70. #endif
  71. namespace pugi
  72. {
  73. // Character type used for all internal storage and operations; depends on PUGIXML_WCHAR_MODE
  74. typedef PUGIXML_CHAR char_t;
  75. #ifndef PUGIXML_NO_STL
  76. // String type used for operations that work with STL string; depends on PUGIXML_WCHAR_MODE
  77. typedef std::basic_string<PUGIXML_CHAR, std::char_traits<PUGIXML_CHAR>, std::allocator<PUGIXML_CHAR> > string_t;
  78. #endif
  79. }
  80. // The PugiXML namespace
  81. namespace pugi
  82. {
  83. // Tree node types
  84. enum xml_node_type
  85. {
  86. node_null, // Empty (null) node handle
  87. node_document, // A document tree's absolute root
  88. node_element, // Element tag, i.e. '<node/>'
  89. node_pcdata, // Plain character data, i.e. 'text'
  90. node_cdata, // Character data, i.e. '<![CDATA[text]]>'
  91. node_comment, // Comment tag, i.e. '<!-- text -->'
  92. node_pi, // Processing instruction, i.e. '<?name?>'
  93. node_declaration, // Document declaration, i.e. '<?xml version="1.0"?>'
  94. node_doctype // Document type declaration, i.e. '<!DOCTYPE doc>'
  95. };
  96. // Parsing options
  97. // Minimal parsing mode (equivalent to turning all other flags off).
  98. // Only elements and PCDATA sections are added to the DOM tree, no text conversions are performed.
  99. const unsigned int parse_minimal = 0x0000;
  100. // This flag determines if processing instructions (node_pi) are added to the DOM tree. This flag is off by default.
  101. const unsigned int parse_pi = 0x0001;
  102. // This flag determines if comments (node_comment) are added to the DOM tree. This flag is off by default.
  103. const unsigned int parse_comments = 0x0002;
  104. // This flag determines if CDATA sections (node_cdata) are added to the DOM tree. This flag is on by default.
  105. const unsigned int parse_cdata = 0x0004;
  106. // This flag determines if plain character data (node_pcdata) that consist only of whitespace are added to the DOM tree.
  107. // This flag is off by default; turning it on usually results in slower parsing and more memory consumption.
  108. const unsigned int parse_ws_pcdata = 0x0008;
  109. // This flag determines if character and entity references are expanded during parsing. This flag is on by default.
  110. const unsigned int parse_escapes = 0x0010;
  111. // This flag determines if EOL characters are normalized (converted to #xA) during parsing. This flag is on by default.
  112. const unsigned int parse_eol = 0x0020;
  113. // This flag determines if attribute values are normalized using CDATA normalization rules during parsing. This flag is on by default.
  114. const unsigned int parse_wconv_attribute = 0x0040;
  115. // This flag determines if attribute values are normalized using NMTOKENS normalization rules during parsing. This flag is off by default.
  116. const unsigned int parse_wnorm_attribute = 0x0080;
  117. // This flag determines if document declaration (node_declaration) is added to the DOM tree. This flag is off by default.
  118. const unsigned int parse_declaration = 0x0100;
  119. // This flag determines if document type declaration (node_doctype) is added to the DOM tree. This flag is off by default.
  120. const unsigned int parse_doctype = 0x0200;
  121. // This flag determines if plain character data (node_pcdata) that is the only child of the parent node and that consists only
  122. // of whitespace is added to the DOM tree.
  123. // This flag is off by default; turning it on may result in slower parsing and more memory consumption.
  124. const unsigned int parse_ws_pcdata_single = 0x0400;
  125. // This flag determines if leading and trailing whitespace is to be removed from plain character data. This flag is off by default.
  126. const unsigned int parse_trim_pcdata = 0x0800;
  127. // This flag determines if plain character data that does not have a parent node is added to the DOM tree, and if an empty document
  128. // is a valid document. This flag is off by default.
  129. const unsigned int parse_fragment = 0x1000;
  130. // The default parsing mode.
  131. // Elements, PCDATA and CDATA sections are added to the DOM tree, character/reference entities are expanded,
  132. // End-of-Line characters are normalized, attribute values are normalized using CDATA normalization rules.
  133. const unsigned int parse_default = parse_cdata | parse_escapes | parse_wconv_attribute | parse_eol;
  134. // The full parsing mode.
  135. // Nodes of all types are added to the DOM tree, character/reference entities are expanded,
  136. // End-of-Line characters are normalized, attribute values are normalized using CDATA normalization rules.
  137. const unsigned int parse_full = parse_default | parse_pi | parse_comments | parse_declaration | parse_doctype;
  138. // These flags determine the encoding of input data for XML document
  139. enum xml_encoding
  140. {
  141. encoding_auto, // Auto-detect input encoding using BOM or < / <? detection; use UTF8 if BOM is not found
  142. encoding_utf8, // UTF8 encoding
  143. encoding_utf16_le, // Little-endian UTF16
  144. encoding_utf16_be, // Big-endian UTF16
  145. encoding_utf16, // UTF16 with native endianness
  146. encoding_utf32_le, // Little-endian UTF32
  147. encoding_utf32_be, // Big-endian UTF32
  148. encoding_utf32, // UTF32 with native endianness
  149. encoding_wchar, // The same encoding wchar_t has (either UTF16 or UTF32)
  150. encoding_latin1
  151. };
  152. // Formatting flags
  153. // Indent the nodes that are written to output stream with as many indentation strings as deep the node is in DOM tree. This flag is on by default.
  154. const unsigned int format_indent = 0x01;
  155. // Write encoding-specific BOM to the output stream. This flag is off by default.
  156. const unsigned int format_write_bom = 0x02;
  157. // Use raw output mode (no indentation and no line breaks are written). This flag is off by default.
  158. const unsigned int format_raw = 0x04;
  159. // Omit default XML declaration even if there is no declaration in the document. This flag is off by default.
  160. const unsigned int format_no_declaration = 0x08;
  161. // Don't escape attribute values and PCDATA contents. This flag is off by default.
  162. const unsigned int format_no_escapes = 0x10;
  163. // Open file using text mode in xml_document::save_file. This enables special character (i.e. new-line) conversions on some systems. This flag is off by default.
  164. const unsigned int format_save_file_text = 0x20;
  165. // Write every attribute on a new line with appropriate indentation. This flag is off by default.
  166. const unsigned int format_indent_attributes = 0x40;
  167. // The default set of formatting flags.
  168. // Nodes are indented depending on their depth in DOM tree, a default declaration is output if document has none.
  169. const unsigned int format_default = format_indent;
  170. // Forward declarations
  171. struct xml_attribute_struct;
  172. struct xml_node_struct;
  173. class xml_node_iterator;
  174. class xml_attribute_iterator;
  175. class xml_named_node_iterator;
  176. class xml_tree_walker;
  177. struct xml_parse_result;
  178. class xml_node;
  179. class xml_text;
  180. #ifndef PUGIXML_NO_XPATH
  181. class xpath_node;
  182. class xpath_node_set;
  183. class xpath_query;
  184. class xpath_variable_set;
  185. #endif
  186. // Range-based for loop support
  187. template <typename It> class xml_object_range
  188. {
  189. public:
  190. typedef It const_iterator;
  191. typedef It iterator;
  192. xml_object_range(It b, It e): _begin(b), _end(e)
  193. {
  194. }
  195. It begin() const { return _begin; }
  196. It end() const { return _end; }
  197. private:
  198. It _begin, _end;
  199. };
  200. // Writer interface for node printing (see xml_node::print)
  201. class PUGIXML_CLASS xml_writer
  202. {
  203. public:
  204. virtual ~xml_writer() {}
  205. // Write memory chunk into stream/file/whatever
  206. virtual void write(const void* data, size_t size) = 0;
  207. };
  208. // xml_writer implementation for FILE*
  209. class PUGIXML_CLASS xml_writer_file: public xml_writer
  210. {
  211. public:
  212. // Construct writer from a FILE* object; void* is used to avoid header dependencies on stdio
  213. xml_writer_file(void* file);
  214. virtual void write(const void* data, size_t size);
  215. private:
  216. void* file;
  217. };
  218. #ifndef PUGIXML_NO_STL
  219. // xml_writer implementation for streams
  220. class PUGIXML_CLASS xml_writer_stream: public xml_writer
  221. {
  222. public:
  223. // Construct writer from an output stream object
  224. xml_writer_stream(std::basic_ostream<char, std::char_traits<char> >& stream);
  225. xml_writer_stream(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream);
  226. virtual void write(const void* data, size_t size);
  227. private:
  228. std::basic_ostream<char, std::char_traits<char> >* narrow_stream;
  229. std::basic_ostream<wchar_t, std::char_traits<wchar_t> >* wide_stream;
  230. };
  231. #endif
  232. // A light-weight handle for manipulating attributes in DOM tree
  233. class PUGIXML_CLASS xml_attribute
  234. {
  235. friend class xml_attribute_iterator;
  236. friend class xml_node;
  237. private:
  238. xml_attribute_struct* _attr;
  239. typedef void (*unspecified_bool_type)(xml_attribute***);
  240. public:
  241. // Default constructor. Constructs an empty attribute.
  242. xml_attribute();
  243. // Constructs attribute from internal pointer
  244. explicit xml_attribute(xml_attribute_struct* attr);
  245. // Safe bool conversion operator
  246. operator unspecified_bool_type() const;
  247. // Borland C++ workaround
  248. bool operator!() const;
  249. // Comparison operators (compares wrapped attribute pointers)
  250. bool operator==(const xml_attribute& r) const;
  251. bool operator!=(const xml_attribute& r) const;
  252. bool operator<(const xml_attribute& r) const;
  253. bool operator>(const xml_attribute& r) const;
  254. bool operator<=(const xml_attribute& r) const;
  255. bool operator>=(const xml_attribute& r) const;
  256. // Check if attribute is empty
  257. bool empty() const;
  258. // Get attribute name/value, or "" if attribute is empty
  259. const char_t* name() const;
  260. const char_t* value() const;
  261. // Get attribute value, or the default value if attribute is empty
  262. const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;
  263. // Get attribute value as a number, or the default value if conversion did not succeed or attribute is empty
  264. int as_int(int def = 0) const;
  265. unsigned int as_uint(unsigned int def = 0) const;
  266. double as_double(double def = 0) const;
  267. float as_float(float def = 0) const;
  268. #ifdef PUGIXML_HAS_LONG_LONG
  269. long long as_llong(long long def = 0) const;
  270. unsigned long long as_ullong(unsigned long long def = 0) const;
  271. #endif
  272. // Get attribute value as bool (returns true if first character is in '1tTyY' set), or the default value if attribute is empty
  273. bool as_bool(bool def = false) const;
  274. // Set attribute name/value (returns false if attribute is empty or there is not enough memory)
  275. bool set_name(const char_t* rhs);
  276. bool set_value(const char_t* rhs);
  277. // Set attribute value with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
  278. bool set_value(int rhs);
  279. bool set_value(unsigned int rhs);
  280. bool set_value(double rhs);
  281. bool set_value(float rhs);
  282. bool set_value(bool rhs);
  283. #ifdef PUGIXML_HAS_LONG_LONG
  284. bool set_value(long long rhs);
  285. bool set_value(unsigned long long rhs);
  286. #endif
  287. // Set attribute value (equivalent to set_value without error checking)
  288. xml_attribute& operator=(const char_t* rhs);
  289. xml_attribute& operator=(int rhs);
  290. xml_attribute& operator=(unsigned int rhs);
  291. xml_attribute& operator=(double rhs);
  292. xml_attribute& operator=(float rhs);
  293. xml_attribute& operator=(bool rhs);
  294. #ifdef PUGIXML_HAS_LONG_LONG
  295. xml_attribute& operator=(long long rhs);
  296. xml_attribute& operator=(unsigned long long rhs);
  297. #endif
  298. // Get next/previous attribute in the attribute list of the parent node
  299. xml_attribute next_attribute() const;
  300. xml_attribute previous_attribute() const;
  301. // Get hash value (unique for handles to the same object)
  302. size_t hash_value() const;
  303. // Get internal pointer
  304. xml_attribute_struct* internal_object() const;
  305. };
  306. #ifdef __BORLANDC__
  307. // Borland C++ workaround
  308. bool PUGIXML_FUNCTION operator&&(const xml_attribute& lhs, bool rhs);
  309. bool PUGIXML_FUNCTION operator||(const xml_attribute& lhs, bool rhs);
  310. #endif
  311. // A light-weight handle for manipulating nodes in DOM tree
  312. class PUGIXML_CLASS xml_node
  313. {
  314. friend class xml_attribute_iterator;
  315. friend class xml_node_iterator;
  316. friend class xml_named_node_iterator;
  317. protected:
  318. xml_node_struct* _root;
  319. typedef void (*unspecified_bool_type)(xml_node***);
  320. public:
  321. // Default constructor. Constructs an empty node.
  322. xml_node();
  323. // Constructs node from internal pointer
  324. explicit xml_node(xml_node_struct* p);
  325. // Safe bool conversion operator
  326. operator unspecified_bool_type() const;
  327. // Borland C++ workaround
  328. bool operator!() const;
  329. // Comparison operators (compares wrapped node pointers)
  330. bool operator==(const xml_node& r) const;
  331. bool operator!=(const xml_node& r) const;
  332. bool operator<(const xml_node& r) const;
  333. bool operator>(const xml_node& r) const;
  334. bool operator<=(const xml_node& r) const;
  335. bool operator>=(const xml_node& r) const;
  336. // Check if node is empty.
  337. bool empty() const;
  338. // Get node type
  339. xml_node_type type() const;
  340. // Get node name, or "" if node is empty or it has no name
  341. const char_t* name() const;
  342. // Get node value, or "" if node is empty or it has no value
  343. // Note: For <node>text</node> node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes.
  344. const char_t* value() const;
  345. // Get attribute list
  346. xml_attribute first_attribute() const;
  347. xml_attribute last_attribute() const;
  348. // Get children list
  349. xml_node first_child() const;
  350. xml_node last_child() const;
  351. // Get next/previous sibling in the children list of the parent node
  352. xml_node next_sibling() const;
  353. xml_node previous_sibling() const;
  354. // Get parent node
  355. xml_node parent() const;
  356. // Get root of DOM tree this node belongs to
  357. xml_node root() const;
  358. // Get text object for the current node
  359. xml_text text() const;
  360. // Get child, attribute or next/previous sibling with the specified name
  361. xml_node child(const char_t* name) const;
  362. xml_attribute attribute(const char_t* name) const;
  363. xml_node next_sibling(const char_t* name) const;
  364. xml_node previous_sibling(const char_t* name) const;
  365. // Get attribute, starting the search from a hint (and updating hint so that searching for a sequence of attributes is fast)
  366. xml_attribute attribute(const char_t* name, xml_attribute& hint) const;
  367. // Get child value of current node; that is, value of the first child node of type PCDATA/CDATA
  368. const char_t* child_value() const;
  369. // Get child value of child with specified name. Equivalent to child(name).child_value().
  370. const char_t* child_value(const char_t* name) const;
  371. // Set node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value)
  372. bool set_name(const char_t* rhs);
  373. bool set_value(const char_t* rhs);
  374. // Add attribute with specified name. Returns added attribute, or empty attribute on errors.
  375. xml_attribute append_attribute(const char_t* name);
  376. xml_attribute prepend_attribute(const char_t* name);
  377. xml_attribute insert_attribute_after(const char_t* name, const xml_attribute& attr);
  378. xml_attribute insert_attribute_before(const char_t* name, const xml_attribute& attr);
  379. // Add a copy of the specified attribute. Returns added attribute, or empty attribute on errors.
  380. xml_attribute append_copy(const xml_attribute& proto);
  381. xml_attribute prepend_copy(const xml_attribute& proto);
  382. xml_attribute insert_copy_after(const xml_attribute& proto, const xml_attribute& attr);
  383. xml_attribute insert_copy_before(const xml_attribute& proto, const xml_attribute& attr);
  384. // Add child node with specified type. Returns added node, or empty node on errors.
  385. xml_node append_child(xml_node_type type = node_element);
  386. xml_node prepend_child(xml_node_type type = node_element);
  387. xml_node insert_child_after(xml_node_type type, const xml_node& node);
  388. xml_node insert_child_before(xml_node_type type, const xml_node& node);
  389. // Add child element with specified name. Returns added node, or empty node on errors.
  390. xml_node append_child(const char_t* name);
  391. xml_node prepend_child(const char_t* name);
  392. xml_node insert_child_after(const char_t* name, const xml_node& node);
  393. xml_node insert_child_before(const char_t* name, const xml_node& node);
  394. // Add a copy of the specified node as a child. Returns added node, or empty node on errors.
  395. xml_node append_copy(const xml_node& proto);
  396. xml_node prepend_copy(const xml_node& proto);
  397. xml_node insert_copy_after(const xml_node& proto, const xml_node& node);
  398. xml_node insert_copy_before(const xml_node& proto, const xml_node& node);
  399. // Move the specified node to become a child of this node. Returns moved node, or empty node on errors.
  400. xml_node append_move(const xml_node& moved);
  401. xml_node prepend_move(const xml_node& moved);
  402. xml_node insert_move_after(const xml_node& moved, const xml_node& node);
  403. xml_node insert_move_before(const xml_node& moved, const xml_node& node);
  404. // Remove specified attribute
  405. bool remove_attribute(const xml_attribute& a);
  406. bool remove_attribute(const char_t* name);
  407. // Remove specified child
  408. bool remove_child(const xml_node& n);
  409. bool remove_child(const char_t* name);
  410. // Parses buffer as an XML document fragment and appends all nodes as children of the current node.
  411. // Copies/converts the buffer, so it may be deleted or changed after the function returns.
  412. // Note: append_buffer allocates memory that has the lifetime of the owning document; removing the appended nodes does not immediately reclaim that memory.
  413. xml_parse_result append_buffer(const void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
  414. // Find attribute using predicate. Returns first attribute for which predicate returned true.
  415. template <typename Predicate> xml_attribute find_attribute(Predicate pred) const
  416. {
  417. if (!_root) return xml_attribute();
  418. for (xml_attribute attrib = first_attribute(); attrib; attrib = attrib.next_attribute())
  419. if (pred(attrib))
  420. return attrib;
  421. return xml_attribute();
  422. }
  423. // Find child node using predicate. Returns first child for which predicate returned true.
  424. template <typename Predicate> xml_node find_child(Predicate pred) const
  425. {
  426. if (!_root) return xml_node();
  427. for (xml_node node = first_child(); node; node = node.next_sibling())
  428. if (pred(node))
  429. return node;
  430. return xml_node();
  431. }
  432. // Find node from subtree using predicate. Returns first node from subtree (depth-first), for which predicate returned true.
  433. template <typename Predicate> xml_node find_node(Predicate pred) const
  434. {
  435. if (!_root) return xml_node();
  436. xml_node cur = first_child();
  437. while (cur._root && cur._root != _root)
  438. {
  439. if (pred(cur)) return cur;
  440. if (cur.first_child()) cur = cur.first_child();
  441. else if (cur.next_sibling()) cur = cur.next_sibling();
  442. else
  443. {
  444. while (!cur.next_sibling() && cur._root != _root) cur = cur.parent();
  445. if (cur._root != _root) cur = cur.next_sibling();
  446. }
  447. }
  448. return xml_node();
  449. }
  450. // Find child node by attribute name/value
  451. xml_node find_child_by_attribute(const char_t* name, const char_t* attr_name, const char_t* attr_value) const;
  452. xml_node find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const;
  453. #ifndef PUGIXML_NO_STL
  454. // Get the absolute node path from root as a text string.
  455. string_t path(char_t delimiter = '/') const;
  456. #endif
  457. // Search for a node by path consisting of node names and . or .. elements.
  458. xml_node first_element_by_path(const char_t* path, char_t delimiter = '/') const;
  459. // Recursively traverse subtree with xml_tree_walker
  460. bool traverse(xml_tree_walker& walker);
  461. #ifndef PUGIXML_NO_XPATH
  462. // Select single node by evaluating XPath query. Returns first node from the resulting node set.
  463. xpath_node select_node(const char_t* query, xpath_variable_set* variables = 0) const;
  464. xpath_node select_node(const xpath_query& query) const;
  465. // Select node set by evaluating XPath query
  466. xpath_node_set select_nodes(const char_t* query, xpath_variable_set* variables = 0) const;
  467. xpath_node_set select_nodes(const xpath_query& query) const;
  468. // (deprecated: use select_node instead) Select single node by evaluating XPath query.
  469. xpath_node select_single_node(const char_t* query, xpath_variable_set* variables = 0) const;
  470. xpath_node select_single_node(const xpath_query& query) const;
  471. #endif
  472. // Print subtree using a writer object
  473. void print(xml_writer& writer, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const;
  474. #ifndef PUGIXML_NO_STL
  475. // Print subtree to stream
  476. void print(std::basic_ostream<char, std::char_traits<char> >& os, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const;
  477. void print(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& os, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, unsigned int depth = 0) const;
  478. #endif
  479. // Child nodes iterators
  480. typedef xml_node_iterator iterator;
  481. iterator begin() const;
  482. iterator end() const;
  483. // Attribute iterators
  484. typedef xml_attribute_iterator attribute_iterator;
  485. attribute_iterator attributes_begin() const;
  486. attribute_iterator attributes_end() const;
  487. // Range-based for support
  488. xml_object_range<xml_node_iterator> children() const;
  489. xml_object_range<xml_named_node_iterator> children(const char_t* name) const;
  490. xml_object_range<xml_attribute_iterator> attributes() const;
  491. // Get node offset in parsed file/string (in char_t units) for debugging purposes
  492. ptrdiff_t offset_debug() const;
  493. // Get hash value (unique for handles to the same object)
  494. size_t hash_value() const;
  495. // Get internal pointer
  496. xml_node_struct* internal_object() const;
  497. };
  498. #ifdef __BORLANDC__
  499. // Borland C++ workaround
  500. bool PUGIXML_FUNCTION operator&&(const xml_node& lhs, bool rhs);
  501. bool PUGIXML_FUNCTION operator||(const xml_node& lhs, bool rhs);
  502. #endif
  503. // A helper for working with text inside PCDATA nodes
  504. class PUGIXML_CLASS xml_text
  505. {
  506. friend class xml_node;
  507. xml_node_struct* _root;
  508. typedef void (*unspecified_bool_type)(xml_text***);
  509. explicit xml_text(xml_node_struct* root);
  510. xml_node_struct* _data_new();
  511. xml_node_struct* _data() const;
  512. public:
  513. // Default constructor. Constructs an empty object.
  514. xml_text();
  515. // Safe bool conversion operator
  516. operator unspecified_bool_type() const;
  517. // Borland C++ workaround
  518. bool operator!() const;
  519. // Check if text object is empty
  520. bool empty() const;
  521. // Get text, or "" if object is empty
  522. const char_t* get() const;
  523. // Get text, or the default value if object is empty
  524. const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;
  525. // Get text as a number, or the default value if conversion did not succeed or object is empty
  526. int as_int(int def = 0) const;
  527. unsigned int as_uint(unsigned int def = 0) const;
  528. double as_double(double def = 0) const;
  529. float as_float(float def = 0) const;
  530. #ifdef PUGIXML_HAS_LONG_LONG
  531. long long as_llong(long long def = 0) const;
  532. unsigned long long as_ullong(unsigned long long def = 0) const;
  533. #endif
  534. // Get text as bool (returns true if first character is in '1tTyY' set), or the default value if object is empty
  535. bool as_bool(bool def = false) const;
  536. // Set text (returns false if object is empty or there is not enough memory)
  537. bool set(const char_t* rhs);
  538. // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
  539. bool set(int rhs);
  540. bool set(unsigned int rhs);
  541. bool set(double rhs);
  542. bool set(float rhs);
  543. bool set(bool rhs);
  544. #ifdef PUGIXML_HAS_LONG_LONG
  545. bool set(long long rhs);
  546. bool set(unsigned long long rhs);
  547. #endif
  548. // Set text (equivalent to set without error checking)
  549. xml_text& operator=(const char_t* rhs);
  550. xml_text& operator=(int rhs);
  551. xml_text& operator=(unsigned int rhs);
  552. xml_text& operator=(double rhs);
  553. xml_text& operator=(float rhs);
  554. xml_text& operator=(bool rhs);
  555. #ifdef PUGIXML_HAS_LONG_LONG
  556. xml_text& operator=(long long rhs);
  557. xml_text& operator=(unsigned long long rhs);
  558. #endif
  559. // Get the data node (node_pcdata or node_cdata) for this object
  560. xml_node data() const;
  561. };
  562. #ifdef __BORLANDC__
  563. // Borland C++ workaround
  564. bool PUGIXML_FUNCTION operator&&(const xml_text& lhs, bool rhs);
  565. bool PUGIXML_FUNCTION operator||(const xml_text& lhs, bool rhs);
  566. #endif
  567. // Child node iterator (a bidirectional iterator over a collection of xml_node)
  568. class PUGIXML_CLASS xml_node_iterator
  569. {
  570. friend class xml_node;
  571. private:
  572. mutable xml_node _wrap;
  573. xml_node _parent;
  574. xml_node_iterator(xml_node_struct* ref, xml_node_struct* parent);
  575. public:
  576. // Iterator traits
  577. typedef ptrdiff_t difference_type;
  578. typedef xml_node value_type;
  579. typedef xml_node* pointer;
  580. typedef xml_node& reference;
  581. #ifndef PUGIXML_NO_STL
  582. typedef std::bidirectional_iterator_tag iterator_category;
  583. #endif
  584. // Default constructor
  585. xml_node_iterator();
  586. // Construct an iterator which points to the specified node
  587. xml_node_iterator(const xml_node& node);
  588. // Iterator operators
  589. bool operator==(const xml_node_iterator& rhs) const;
  590. bool operator!=(const xml_node_iterator& rhs) const;
  591. xml_node& operator*() const;
  592. xml_node* operator->() const;
  593. const xml_node_iterator& operator++();
  594. xml_node_iterator operator++(int);
  595. const xml_node_iterator& operator--();
  596. xml_node_iterator operator--(int);
  597. };
  598. // Attribute iterator (a bidirectional iterator over a collection of xml_attribute)
  599. class PUGIXML_CLASS xml_attribute_iterator
  600. {
  601. friend class xml_node;
  602. private:
  603. mutable xml_attribute _wrap;
  604. xml_node _parent;
  605. xml_attribute_iterator(xml_attribute_struct* ref, xml_node_struct* parent);
  606. public:
  607. // Iterator traits
  608. typedef ptrdiff_t difference_type;
  609. typedef xml_attribute value_type;
  610. typedef xml_attribute* pointer;
  611. typedef xml_attribute& reference;
  612. #ifndef PUGIXML_NO_STL
  613. typedef std::bidirectional_iterator_tag iterator_category;
  614. #endif
  615. // Default constructor
  616. xml_attribute_iterator();
  617. // Construct an iterator which points to the specified attribute
  618. xml_attribute_iterator(const xml_attribute& attr, const xml_node& parent);
  619. // Iterator operators
  620. bool operator==(const xml_attribute_iterator& rhs) const;
  621. bool operator!=(const xml_attribute_iterator& rhs) const;
  622. xml_attribute& operator*() const;
  623. xml_attribute* operator->() const;
  624. const xml_attribute_iterator& operator++();
  625. xml_attribute_iterator operator++(int);
  626. const xml_attribute_iterator& operator--();
  627. xml_attribute_iterator operator--(int);
  628. };
  629. // Named node range helper
  630. class PUGIXML_CLASS xml_named_node_iterator
  631. {
  632. friend class xml_node;
  633. public:
  634. // Iterator traits
  635. typedef ptrdiff_t difference_type;
  636. typedef xml_node value_type;
  637. typedef xml_node* pointer;
  638. typedef xml_node& reference;
  639. #ifndef PUGIXML_NO_STL
  640. typedef std::bidirectional_iterator_tag iterator_category;
  641. #endif
  642. // Default constructor
  643. xml_named_node_iterator();
  644. // Construct an iterator which points to the specified node
  645. xml_named_node_iterator(const xml_node& node, const char_t* name);
  646. // Iterator operators
  647. bool operator==(const xml_named_node_iterator& rhs) const;
  648. bool operator!=(const xml_named_node_iterator& rhs) const;
  649. xml_node& operator*() const;
  650. xml_node* operator->() const;
  651. const xml_named_node_iterator& operator++();
  652. xml_named_node_iterator operator++(int);
  653. const xml_named_node_iterator& operator--();
  654. xml_named_node_iterator operator--(int);
  655. private:
  656. mutable xml_node _wrap;
  657. xml_node _parent;
  658. const char_t* _name;
  659. xml_named_node_iterator(xml_node_struct* ref, xml_node_struct* parent, const char_t* name);
  660. };
  661. // Abstract tree walker class (see xml_node::traverse)
  662. class PUGIXML_CLASS xml_tree_walker
  663. {
  664. friend class xml_node;
  665. private:
  666. int _depth;
  667. protected:
  668. // Get current traversal depth
  669. int depth() const;
  670. public:
  671. xml_tree_walker();
  672. virtual ~xml_tree_walker();
  673. // Callback that is called when traversal begins
  674. virtual bool begin(xml_node& node);
  675. // Callback that is called for each node traversed
  676. virtual bool for_each(xml_node& node) = 0;
  677. // Callback that is called when traversal ends
  678. virtual bool end(xml_node& node);
  679. };
  680. // Parsing status, returned as part of xml_parse_result object
  681. enum xml_parse_status
  682. {
  683. status_ok = 0, // No error
  684. status_file_not_found, // File was not found during load_file()
  685. status_io_error, // Error reading from file/stream
  686. status_out_of_memory, // Could not allocate memory
  687. status_internal_error, // Internal error occurred
  688. status_unrecognized_tag, // Parser could not determine tag type
  689. status_bad_pi, // Parsing error occurred while parsing document declaration/processing instruction
  690. status_bad_comment, // Parsing error occurred while parsing comment
  691. status_bad_cdata, // Parsing error occurred while parsing CDATA section
  692. status_bad_doctype, // Parsing error occurred while parsing document type declaration
  693. status_bad_pcdata, // Parsing error occurred while parsing PCDATA section
  694. status_bad_start_element, // Parsing error occurred while parsing start element tag
  695. status_bad_attribute, // Parsing error occurred while parsing element attribute
  696. status_bad_end_element, // Parsing error occurred while parsing end element tag
  697. status_end_element_mismatch,// There was a mismatch of start-end tags (closing tag had incorrect name, some tag was not closed or there was an excessive closing tag)
  698. status_append_invalid_root, // Unable to append nodes since root type is not node_element or node_document (exclusive to xml_node::append_buffer)
  699. status_no_document_element // Parsing resulted in a document without element nodes
  700. };
  701. // Parsing result
  702. struct PUGIXML_CLASS xml_parse_result
  703. {
  704. // Parsing status (see xml_parse_status)
  705. xml_parse_status status;
  706. // Last parsed offset (in char_t units from start of input data)
  707. ptrdiff_t offset;
  708. // Source document encoding
  709. xml_encoding encoding;
  710. // Default constructor, initializes object to failed state
  711. xml_parse_result();
  712. // Cast to bool operator
  713. operator bool() const;
  714. // Get error description
  715. const char* description() const;
  716. };
  717. // Document class (DOM tree root)
  718. class PUGIXML_CLASS xml_document: public xml_node
  719. {
  720. private:
  721. char_t* _buffer;
  722. char _memory[192];
  723. // Non-copyable semantics
  724. xml_document(const xml_document&);
  725. xml_document& operator=(const xml_document&);
  726. void create();
  727. void destroy();
  728. public:
  729. // Default constructor, makes empty document
  730. xml_document();
  731. // Destructor, invalidates all node/attribute handles to this document
  732. ~xml_document();
  733. // Removes all nodes, leaving the empty document
  734. void reset();
  735. // Removes all nodes, then copies the entire contents of the specified document
  736. void reset(const xml_document& proto);
  737. #ifndef PUGIXML_NO_STL
  738. // Load document from stream.
  739. xml_parse_result load(std::basic_istream<char, std::char_traits<char> >& stream, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
  740. xml_parse_result load(std::basic_istream<wchar_t, std::char_traits<wchar_t> >& stream, unsigned int options = parse_default);
  741. #endif
  742. // (deprecated: use load_string instead) Load document from zero-terminated string. No encoding conversions are applied.
  743. xml_parse_result load(const char_t* contents, unsigned int options = parse_default);
  744. // Load document from zero-terminated string. No encoding conversions are applied.
  745. xml_parse_result load_string(const char_t* contents, unsigned int options = parse_default);
  746. // Load document from file
  747. xml_parse_result load_file(const char* path, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
  748. xml_parse_result load_file(const wchar_t* path, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
  749. // Load document from buffer. Copies/converts the buffer, so it may be deleted or changed after the function returns.
  750. xml_parse_result load_buffer(const void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
  751. // Load document from buffer, using the buffer for in-place parsing (the buffer is modified and used for storage of document data).
  752. // You should ensure that buffer data will persist throughout the document's lifetime, and free the buffer memory manually once document is destroyed.
  753. xml_parse_result load_buffer_inplace(void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
  754. // Load document from buffer, using the buffer for in-place parsing (the buffer is modified and used for storage of document data).
  755. // You should allocate the buffer with pugixml allocation function; document will free the buffer when it is no longer needed (you can't use it anymore).
  756. xml_parse_result load_buffer_inplace_own(void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);
  757. // Save XML document to writer (semantics is slightly different from xml_node::print, see documentation for details).
  758. void save(xml_writer& writer, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const;
  759. #ifndef PUGIXML_NO_STL
  760. // Save XML document to stream (semantics is slightly different from xml_node::print, see documentation for details).
  761. void save(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const;
  762. void save(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default) const;
  763. #endif
  764. // Save XML to file
  765. bool save_file(const char* path, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const;
  766. bool save_file(const wchar_t* path, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const;
  767. // Get document element
  768. xml_node document_element() const;
  769. };
  770. #ifndef PUGIXML_NO_XPATH
  771. // XPath query return type
  772. enum xpath_value_type
  773. {
  774. xpath_type_none, // Unknown type (query failed to compile)
  775. xpath_type_node_set, // Node set (xpath_node_set)
  776. xpath_type_number, // Number
  777. xpath_type_string, // String
  778. xpath_type_boolean // Boolean
  779. };
  780. // XPath parsing result
  781. struct PUGIXML_CLASS xpath_parse_result
  782. {
  783. // Error message (0 if no error)
  784. const char* error;
  785. // Last parsed offset (in char_t units from string start)
  786. ptrdiff_t offset;
  787. // Default constructor, initializes object to failed state
  788. xpath_parse_result();
  789. // Cast to bool operator
  790. operator bool() const;
  791. // Get error description
  792. const char* description() const;
  793. };
  794. // A single XPath variable
  795. class PUGIXML_CLASS xpath_variable
  796. {
  797. friend class xpath_variable_set;
  798. protected:
  799. xpath_value_type _type;
  800. xpath_variable* _next;
  801. xpath_variable(xpath_value_type type);
  802. // Non-copyable semantics
  803. xpath_variable(const xpath_variable&);
  804. xpath_variable& operator=(const xpath_variable&);
  805. public:
  806. // Get variable name
  807. const char_t* name() const;
  808. // Get variable type
  809. xpath_value_type type() const;
  810. // Get variable value; no type conversion is performed, default value (false, NaN, empty string, empty node set) is returned on type mismatch error
  811. bool get_boolean() const;
  812. double get_number() const;
  813. const char_t* get_string() const;
  814. const xpath_node_set& get_node_set() const;
  815. // Set variable value; no type conversion is performed, false is returned on type mismatch error
  816. bool set(bool value);
  817. bool set(double value);
  818. bool set(const char_t* value);
  819. bool set(const xpath_node_set& value);
  820. };
  821. // A set of XPath variables
  822. class PUGIXML_CLASS xpath_variable_set
  823. {
  824. private:
  825. xpath_variable* _data[64];
  826. void _assign(const xpath_variable_set& rhs);
  827. void _swap(xpath_variable_set& rhs);
  828. xpath_variable* _find(const char_t* name) const;
  829. static bool _clone(xpath_variable* var, xpath_variable** out_result);
  830. static void _destroy(xpath_variable* var);
  831. public:
  832. // Default constructor/destructor
  833. xpath_variable_set();
  834. ~xpath_variable_set();
  835. // Copy constructor/assignment operator
  836. xpath_variable_set(const xpath_variable_set& rhs);
  837. xpath_variable_set& operator=(const xpath_variable_set& rhs);
  838. #if __cplusplus >= 201103
  839. // Move semantics support
  840. xpath_variable_set(xpath_variable_set&& rhs);
  841. xpath_variable_set& operator=(xpath_variable_set&& rhs);
  842. #endif
  843. // Add a new variable or get the existing one, if the types match
  844. xpath_variable* add(const char_t* name, xpath_value_type type);
  845. // Set value of an existing variable; no type conversion is performed, false is returned if there is no such variable or if types mismatch
  846. bool set(const char_t* name, bool value);
  847. bool set(const char_t* name, double value);
  848. bool set(const char_t* name, const char_t* value);
  849. bool set(const char_t* name, const xpath_node_set& value);
  850. // Get existing variable by name
  851. xpath_variable* get(const char_t* name);
  852. const xpath_variable* get(const char_t* name) const;
  853. };
  854. // A compiled XPath query object
  855. class PUGIXML_CLASS xpath_query
  856. {
  857. private:
  858. void* _impl;
  859. xpath_parse_result _result;
  860. typedef void (*unspecified_bool_type)(xpath_query***);
  861. // Non-copyable semantics
  862. xpath_query(const xpath_query&);
  863. xpath_query& operator=(const xpath_query&);
  864. public:
  865. // Construct a compiled object from XPath expression.
  866. // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on compilation errors.
  867. explicit xpath_query(const char_t* query, xpath_variable_set* variables = 0);
  868. // Constructor
  869. xpath_query();
  870. // Destructor
  871. ~xpath_query();
  872. #if __cplusplus >= 201103
  873. // Move semantics support
  874. xpath_query(xpath_query&& rhs);
  875. xpath_query& operator=(xpath_query&& rhs);
  876. #endif
  877. // Get query expression return type
  878. xpath_value_type return_type() const;
  879. // Evaluate expression as boolean value in the specified context; performs type conversion if necessary.
  880. // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
  881. bool evaluate_boolean(const xpath_node& n) const;
  882. // Evaluate expression as double value in the specified context; performs type conversion if necessary.
  883. // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
  884. double evaluate_number(const xpath_node& n) const;
  885. #ifndef PUGIXML_NO_STL
  886. // Evaluate expression as string value in the specified context; performs type conversion if necessary.
  887. // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
  888. string_t evaluate_string(const xpath_node& n) const;
  889. #endif
  890. // Evaluate expression as string value in the specified context; performs type conversion if necessary.
  891. // At most capacity characters are written to the destination buffer, full result size is returned (includes terminating zero).
  892. // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors.
  893. // If PUGIXML_NO_EXCEPTIONS is defined, returns empty set instead.
  894. size_t evaluate_string(char_t* buffer, size_t capacity, const xpath_node& n) const;
  895. // Evaluate expression as node set in the specified context.
  896. // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on type mismatch and std::bad_alloc on out of memory errors.
  897. // If PUGIXML_NO_EXCEPTIONS is defined, returns empty node set instead.
  898. xpath_node_set evaluate_node_set(const xpath_node& n) const;
  899. // Evaluate expression as node set in the specified context.
  900. // Return first node in document order, or empty node if node set is empty.
  901. // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on type mismatch and std::bad_alloc on out of memory errors.
  902. // If PUGIXML_NO_EXCEPTIONS is defined, returns empty node instead.
  903. xpath_node evaluate_node(const xpath_node& n) const;
  904. // Get parsing result (used to get compilation errors in PUGIXML_NO_EXCEPTIONS mode)
  905. const xpath_parse_result& result() const;
  906. // Safe bool conversion operator
  907. operator unspecified_bool_type() const;
  908. // Borland C++ workaround
  909. bool operator!() const;
  910. };
  911. #ifndef PUGIXML_NO_EXCEPTIONS
  912. // XPath exception class
  913. class PUGIXML_CLASS xpath_exception: public std::exception
  914. {
  915. private:
  916. xpath_parse_result _result;
  917. public:
  918. // Construct exception from parse result
  919. explicit xpath_exception(const xpath_parse_result& result);
  920. // Get error message
  921. virtual const char* what() const throw();
  922. // Get parse result
  923. const xpath_parse_result& result() const;
  924. };
  925. #endif
  926. // XPath node class (either xml_node or xml_attribute)
  927. class PUGIXML_CLASS xpath_node
  928. {
  929. private:
  930. xml_node _node;
  931. xml_attribute _attribute;
  932. typedef void (*unspecified_bool_type)(xpath_node***);
  933. public:
  934. // Default constructor; constructs empty XPath node
  935. xpath_node();
  936. // Construct XPath node from XML node/attribute
  937. xpath_node(const xml_node& node);
  938. xpath_node(const xml_attribute& attribute, const xml_node& parent);
  939. // Get node/attribute, if any
  940. xml_node node() const;
  941. xml_attribute attribute() const;
  942. // Get parent of contained node/attribute
  943. xml_node parent() const;
  944. // Safe bool conversion operator
  945. operator unspecified_bool_type() const;
  946. // Borland C++ workaround
  947. bool operator!() const;
  948. // Comparison operators
  949. bool operator==(const xpath_node& n) const;
  950. bool operator!=(const xpath_node& n) const;
  951. };
  952. #ifdef __BORLANDC__
  953. // Borland C++ workaround
  954. bool PUGIXML_FUNCTION operator&&(const xpath_node& lhs, bool rhs);
  955. bool PUGIXML_FUNCTION operator||(const xpath_node& lhs, bool rhs);
  956. #endif
  957. // A fixed-size collection of XPath nodes
  958. class PUGIXML_CLASS xpath_node_set
  959. {
  960. public:
  961. // Collection type
  962. enum type_t
  963. {
  964. type_unsorted, // Not ordered
  965. type_sorted, // Sorted by document order (ascending)
  966. type_sorted_reverse // Sorted by document order (descending)
  967. };
  968. // Constant iterator type
  969. typedef const xpath_node* const_iterator;
  970. // We define non-constant iterator to be the same as constant iterator so that various generic algorithms (i.e. boost foreach) work
  971. typedef const xpath_node* iterator;
  972. // Default constructor. Constructs empty set.
  973. xpath_node_set();
  974. // Constructs a set from iterator range; data is not checked for duplicates and is not sorted according to provided type, so be careful
  975. xpath_node_set(const_iterator begin, const_iterator end, type_t type = type_unsorted);
  976. // Destructor
  977. ~xpath_node_set();
  978. // Copy constructor/assignment operator
  979. xpath_node_set(const xpath_node_set& ns);
  980. xpath_node_set& operator=(const xpath_node_set& ns);
  981. #if __cplusplus >= 201103
  982. // Move semantics support
  983. xpath_node_set(xpath_node_set&& rhs);
  984. xpath_node_set& operator=(xpath_node_set&& rhs);
  985. #endif
  986. // Get collection type
  987. type_t type() const;
  988. // Get collection size
  989. size_t size() const;
  990. // Indexing operator
  991. const xpath_node& operator[](size_t index) const;
  992. // Collection iterators
  993. const_iterator begin() const;
  994. const_iterator end() const;
  995. // Sort the collection in ascending/descending order by document order
  996. void sort(bool reverse = false);
  997. // Get first node in the collection by document order
  998. xpath_node first() const;
  999. // Check if collection is empty
  1000. bool empty() const;
  1001. private:
  1002. type_t _type;
  1003. xpath_node _storage;
  1004. xpath_node* _begin;
  1005. xpath_node* _end;
  1006. void _assign(const_iterator begin, const_iterator end, type_t type);
  1007. void _move(xpath_node_set& rhs);
  1008. };
  1009. #endif
  1010. #ifndef PUGIXML_NO_STL
  1011. // Convert wide string to UTF8
  1012. std::basic_string<char, std::char_traits<char>, std::allocator<char> > PUGIXML_FUNCTION as_utf8(const wchar_t* str);
  1013. std::basic_string<char, std::char_traits<char>, std::allocator<char> > PUGIXML_FUNCTION as_utf8(const std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >& str);
  1014. // Convert UTF8 to wide string
  1015. std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > PUGIXML_FUNCTION as_wide(const char* str);
  1016. std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > PUGIXML_FUNCTION as_wide(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >& str);
  1017. #endif
  1018. // Memory allocation function interface; returns pointer to allocated memory or NULL on failure
  1019. typedef void* (*allocation_function)(size_t size);
  1020. // Memory deallocation function interface
  1021. typedef void (*deallocation_function)(void* ptr);
  1022. // Override default memory management functions. All subsequent allocations/deallocations will be performed via supplied functions.
  1023. void PUGIXML_FUNCTION set_memory_management_functions(allocation_function allocate, deallocation_function deallocate);
  1024. // Get current memory management functions
  1025. allocation_function PUGIXML_FUNCTION get_memory_allocation_function();
  1026. deallocation_function PUGIXML_FUNCTION get_memory_deallocation_function();
  1027. }
  1028. #if !defined(PUGIXML_NO_STL) && (defined(_MSC_VER) || defined(__ICC))
  1029. namespace std
  1030. {
  1031. // Workarounds for (non-standard) iterator category detection for older versions (MSVC7/IC8 and earlier)
  1032. std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_node_iterator&);
  1033. std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_attribute_iterator&);
  1034. std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_named_node_iterator&);
  1035. }
  1036. #endif
  1037. #if !defined(PUGIXML_NO_STL) && defined(__SUNPRO_CC)
  1038. namespace std
  1039. {
  1040. // Workarounds for (non-standard) iterator category detection
  1041. std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_node_iterator&);
  1042. std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_attribute_iterator&);
  1043. std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_named_node_iterator&);
  1044. }
  1045. #endif
  1046. #endif
  1047. // Make sure implementation is included in header-only mode
  1048. // Use macro expansion in #include to work around QMake (QTBUG-11923)
  1049. #if defined(PUGIXML_HEADER_ONLY) && !defined(PUGIXML_SOURCE)
  1050. # define PUGIXML_SOURCE "pugixml.cpp"
  1051. # include PUGIXML_SOURCE
  1052. #endif
  1053. /**
  1054. * Copyright (c) 2006-2015 Arseny Kapoulkine
  1055. *
  1056. * Permission is hereby granted, free of charge, to any person
  1057. * obtaining a copy of this software and associated documentation
  1058. * files (the "Software"), to deal in the Software without
  1059. * restriction, including without limitation the rights to use,
  1060. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  1061. * copies of the Software, and to permit persons to whom the
  1062. * Software is furnished to do so, subject to the following
  1063. * conditions:
  1064. *
  1065. * The above copyright notice and this permission notice shall be
  1066. * included in all copies or substantial portions of the Software.
  1067. *
  1068. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1069. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  1070. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  1071. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  1072. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  1073. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1074. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  1075. * OTHER DEALINGS IN THE SOFTWARE.
  1076. */