pugixml.hpp 52 KB

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