pugixml.hpp 55 KB

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