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