pugixml.hpp 53 KB

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