pugixml.hpp 52 KB

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