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 315 private: 316 It _begin, _end; 317 }; 318 319 // Writer interface for node printing (see xml_node::print) 320 class PUGIXML_CLASS xml_writer 321 { 322 public: ~xml_writer()323 virtual ~xml_writer() {} 324 325 // Write memory chunk into stream/file/whatever 326 virtual void write(const void* data, size_t size) = 0; 327 }; 328 329 // xml_writer implementation for FILE* 330 class PUGIXML_CLASS xml_writer_file: public xml_writer 331 { 332 public: 333 // Construct writer from a FILE* object; void* is used to avoid header dependencies on stdio 334 xml_writer_file(void* file); 335 336 virtual void write(const void* data, size_t size) PUGIXML_OVERRIDE; 337 338 private: 339 void* file; 340 }; 341 342 #ifndef PUGIXML_NO_STL 343 // xml_writer implementation for streams 344 class PUGIXML_CLASS xml_writer_stream: public xml_writer 345 { 346 public: 347 // Construct writer from an output stream object 348 xml_writer_stream(std::basic_ostream<char, std::char_traits<char> >& stream); 349 xml_writer_stream(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream); 350 351 virtual void write(const void* data, size_t size) PUGIXML_OVERRIDE; 352 353 private: 354 std::basic_ostream<char, std::char_traits<char> >* narrow_stream; 355 std::basic_ostream<wchar_t, std::char_traits<wchar_t> >* wide_stream; 356 }; 357 #endif 358 359 // A light-weight handle for manipulating attributes in DOM tree 360 class PUGIXML_CLASS xml_attribute 361 { 362 friend class xml_attribute_iterator; 363 friend class xml_node; 364 365 private: 366 xml_attribute_struct* _attr; 367 368 typedef void (*unspecified_bool_type)(xml_attribute***); 369 370 public: 371 // Default constructor. Constructs an empty attribute. 372 xml_attribute(); 373 374 // Constructs attribute from internal pointer 375 explicit xml_attribute(xml_attribute_struct* attr); 376 377 // Safe bool conversion operator 378 operator unspecified_bool_type() const; 379 380 // Borland C++ workaround 381 bool operator!() const; 382 383 // Comparison operators (compares wrapped attribute pointers) 384 bool operator==(const xml_attribute& r) const; 385 bool operator!=(const xml_attribute& r) const; 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 391 // Check if attribute is empty 392 bool empty() const; 393 394 // Get attribute name/value, or "" if attribute is empty 395 const char_t* name() const; 396 const char_t* value() const; 397 398 // Get attribute value, or the default value if attribute is empty 399 const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; 400 401 // Get attribute value as a number, or the default value if conversion did not succeed or attribute is empty 402 int as_int(int def = 0) const; 403 unsigned int as_uint(unsigned int def = 0) const; 404 double as_double(double def = 0) const; 405 float as_float(float def = 0) const; 406 407 #ifdef PUGIXML_HAS_LONG_LONG 408 long long as_llong(long long def = 0) const; 409 unsigned long long as_ullong(unsigned long long def = 0) const; 410 #endif 411 412 // Get attribute value as bool (returns true if first character is in '1tTyY' set), or the default value if attribute is empty 413 bool as_bool(bool def = false) const; 414 415 // Set attribute name/value (returns false if attribute is empty or there is not enough memory) 416 bool set_name(const char_t* rhs); 417 bool set_value(const char_t* rhs); 418 419 // Set attribute value with type conversion (numbers are converted to strings, boolean is converted to "true"/"false") 420 bool set_value(int rhs); 421 bool set_value(unsigned int rhs); 422 bool set_value(long rhs); 423 bool set_value(unsigned long rhs); 424 bool set_value(double rhs); 425 bool set_value(double rhs, int precision); 426 bool set_value(float rhs); 427 bool set_value(float rhs, int precision); 428 bool set_value(bool rhs); 429 430 #ifdef PUGIXML_HAS_LONG_LONG 431 bool set_value(long long rhs); 432 bool set_value(unsigned long long rhs); 433 #endif 434 435 // Set attribute value (equivalent to set_value without error checking) 436 xml_attribute& operator=(const char_t* rhs); 437 xml_attribute& operator=(int rhs); 438 xml_attribute& operator=(unsigned int rhs); 439 xml_attribute& operator=(long rhs); 440 xml_attribute& operator=(unsigned long rhs); 441 xml_attribute& operator=(double rhs); 442 xml_attribute& operator=(float rhs); 443 xml_attribute& operator=(bool rhs); 444 445 #ifdef PUGIXML_HAS_LONG_LONG 446 xml_attribute& operator=(long long rhs); 447 xml_attribute& operator=(unsigned long long rhs); 448 #endif 449 450 // Get next/previous attribute in the attribute list of the parent node 451 xml_attribute next_attribute() const; 452 xml_attribute previous_attribute() const; 453 454 // Get hash value (unique for handles to the same object) 455 size_t hash_value() const; 456 457 // Get internal pointer 458 xml_attribute_struct* internal_object() const; 459 }; 460 461 #ifdef __BORLANDC__ 462 // Borland C++ workaround 463 bool PUGIXML_FUNCTION operator&&(const xml_attribute& lhs, bool rhs); 464 bool PUGIXML_FUNCTION operator||(const xml_attribute& lhs, bool rhs); 465 #endif 466 467 // A light-weight handle for manipulating nodes in DOM tree 468 class PUGIXML_CLASS xml_node 469 { 470 friend class xml_attribute_iterator; 471 friend class xml_node_iterator; 472 friend class xml_named_node_iterator; 473 474 protected: 475 xml_node_struct* _root; 476 477 typedef void (*unspecified_bool_type)(xml_node***); 478 479 public: 480 // Default constructor. Constructs an empty node. 481 xml_node(); 482 483 // Constructs node from internal pointer 484 explicit xml_node(xml_node_struct* p); 485 486 // Safe bool conversion operator 487 operator unspecified_bool_type() const; 488 489 // Borland C++ workaround 490 bool operator!() const; 491 492 // Comparison operators (compares wrapped node pointers) 493 bool operator==(const xml_node& r) const; 494 bool operator!=(const xml_node& r) const; 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 500 // Check if node is empty. 501 bool empty() const; 502 503 // Get node type 504 xml_node_type type() const; 505 506 // Get node name, or "" if node is empty or it has no name 507 const char_t* name() const; 508 509 // Get node value, or "" if node is empty or it has no value 510 // Note: For <node>text</node> node.value() does not return "text"! Use child_value() or text() methods to access text inside nodes. 511 const char_t* value() const; 512 513 // Get attribute list 514 xml_attribute first_attribute() const; 515 xml_attribute last_attribute() const; 516 517 // Get children list 518 xml_node first_child() const; 519 xml_node last_child() const; 520 521 // Get next/previous sibling in the children list of the parent node 522 xml_node next_sibling() const; 523 xml_node previous_sibling() const; 524 525 // Get parent node 526 xml_node parent() const; 527 528 // Get root of DOM tree this node belongs to 529 xml_node root() const; 530 531 // Get text object for the current node 532 xml_text text() const; 533 534 // Get child, attribute or next/previous sibling with the specified name 535 xml_node child(const char_t* name) const; 536 xml_attribute attribute(const char_t* name) const; 537 xml_node next_sibling(const char_t* name) const; 538 xml_node previous_sibling(const char_t* name) const; 539 540 // Get attribute, starting the search from a hint (and updating hint so that searching for a sequence of attributes is fast) 541 xml_attribute attribute(const char_t* name, xml_attribute& hint) const; 542 543 // Get child value of current node; that is, value of the first child node of type PCDATA/CDATA 544 const char_t* child_value() const; 545 546 // Get child value of child with specified name. Equivalent to child(name).child_value(). 547 const char_t* child_value(const char_t* name) const; 548 549 // Set node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value) 550 bool set_name(const char_t* rhs); 551 bool set_value(const char_t* rhs); 552 553 // Add attribute with specified name. Returns added attribute, or empty attribute on errors. 554 xml_attribute append_attribute(const char_t* name); 555 xml_attribute prepend_attribute(const char_t* name); 556 xml_attribute insert_attribute_after(const char_t* name, const xml_attribute& attr); 557 xml_attribute insert_attribute_before(const char_t* name, const xml_attribute& attr); 558 559 // Add a copy of the specified attribute. Returns added attribute, or empty attribute on errors. 560 xml_attribute append_copy(const xml_attribute& proto); 561 xml_attribute prepend_copy(const xml_attribute& proto); 562 xml_attribute insert_copy_after(const xml_attribute& proto, const xml_attribute& attr); 563 xml_attribute insert_copy_before(const xml_attribute& proto, const xml_attribute& attr); 564 565 // Add child node with specified type. Returns added node, or empty node on errors. 566 xml_node append_child(xml_node_type type = node_element); 567 xml_node prepend_child(xml_node_type type = node_element); 568 xml_node insert_child_after(xml_node_type type, const xml_node& node); 569 xml_node insert_child_before(xml_node_type type, const xml_node& node); 570 571 // Add child element with specified name. Returns added node, or empty node on errors. 572 xml_node append_child(const char_t* name); 573 xml_node prepend_child(const char_t* name); 574 xml_node insert_child_after(const char_t* name, const xml_node& node); 575 xml_node insert_child_before(const char_t* name, const xml_node& node); 576 577 // Add a copy of the specified node as a child. Returns added node, or empty node on errors. 578 xml_node append_copy(const xml_node& proto); 579 xml_node prepend_copy(const xml_node& proto); 580 xml_node insert_copy_after(const xml_node& proto, const xml_node& node); 581 xml_node insert_copy_before(const xml_node& proto, const xml_node& node); 582 583 // Move the specified node to become a child of this node. Returns moved node, or empty node on errors. 584 xml_node append_move(const xml_node& moved); 585 xml_node prepend_move(const xml_node& moved); 586 xml_node insert_move_after(const xml_node& moved, const xml_node& node); 587 xml_node insert_move_before(const xml_node& moved, const xml_node& node); 588 589 // Remove specified attribute 590 bool remove_attribute(const xml_attribute& a); 591 bool remove_attribute(const char_t* name); 592 593 // Remove all attributes 594 bool remove_attributes(); 595 596 // Remove specified child 597 bool remove_child(const xml_node& n); 598 bool remove_child(const char_t* name); 599 600 // Remove all children 601 bool remove_children(); 602 603 // Parses buffer as an XML document fragment and appends all nodes as children of the current node. 604 // Copies/converts the buffer, so it may be deleted or changed after the function returns. 605 // Note: append_buffer allocates memory that has the lifetime of the owning document; removing the appended nodes does not immediately reclaim that memory. 606 xml_parse_result append_buffer(const void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); 607 608 // Find attribute using predicate. Returns first attribute for which predicate returned true. find_attribute(Predicate pred) const609 template <typename Predicate> xml_attribute find_attribute(Predicate pred) const 610 { 611 if (!_root) return xml_attribute(); 612 613 for (xml_attribute attrib = first_attribute(); attrib; attrib = attrib.next_attribute()) 614 if (pred(attrib)) 615 return attrib; 616 617 return xml_attribute(); 618 } 619 620 // Find child node using predicate. Returns first child for which predicate returned true. find_child(Predicate pred) const621 template <typename Predicate> xml_node find_child(Predicate pred) const 622 { 623 if (!_root) return xml_node(); 624 625 for (xml_node node = first_child(); node; node = node.next_sibling()) 626 if (pred(node)) 627 return node; 628 629 return xml_node(); 630 } 631 632 // Find node from subtree using predicate. Returns first node from subtree (depth-first), for which predicate returned true. find_node(Predicate pred) const633 template <typename Predicate> xml_node find_node(Predicate pred) const 634 { 635 if (!_root) return xml_node(); 636 637 xml_node cur = first_child(); 638 639 while (cur._root && cur._root != _root) 640 { 641 if (pred(cur)) return cur; 642 643 if (cur.first_child()) cur = cur.first_child(); 644 else if (cur.next_sibling()) cur = cur.next_sibling(); 645 else 646 { 647 while (!cur.next_sibling() && cur._root != _root) cur = cur.parent(); 648 649 if (cur._root != _root) cur = cur.next_sibling(); 650 } 651 } 652 653 return xml_node(); 654 } 655 656 // Find child node by attribute name/value 657 xml_node find_child_by_attribute(const char_t* name, const char_t* attr_name, const char_t* attr_value) const; 658 xml_node find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const; 659 660 #ifndef PUGIXML_NO_STL 661 // Get the absolute node path from root as a text string. 662 string_t path(char_t delimiter = '/') const; 663 #endif 664 665 // Search for a node by path consisting of node names and . or .. elements. 666 xml_node first_element_by_path(const char_t* path, char_t delimiter = '/') const; 667 668 // Recursively traverse subtree with xml_tree_walker 669 bool traverse(xml_tree_walker& walker); 670 671 #ifndef PUGIXML_NO_XPATH 672 // Select single node by evaluating XPath query. Returns first node from the resulting node set. 673 xpath_node select_node(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL) const; 674 xpath_node select_node(const xpath_query& query) const; 675 676 // Select node set by evaluating XPath query 677 xpath_node_set select_nodes(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL) const; 678 xpath_node_set select_nodes(const xpath_query& query) const; 679 680 // (deprecated: use select_node instead) Select single node by evaluating XPath query. 681 PUGIXML_DEPRECATED xpath_node select_single_node(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL) const; 682 PUGIXML_DEPRECATED xpath_node select_single_node(const xpath_query& query) const; 683 684 #endif 685 686 // Print subtree using a writer object 687 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; 688 689 #ifndef PUGIXML_NO_STL 690 // Print subtree to stream 691 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; 692 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; 693 #endif 694 695 // Child nodes iterators 696 typedef xml_node_iterator iterator; 697 698 iterator begin() const; 699 iterator end() const; 700 701 // Attribute iterators 702 typedef xml_attribute_iterator attribute_iterator; 703 704 attribute_iterator attributes_begin() const; 705 attribute_iterator attributes_end() const; 706 707 // Range-based for support 708 xml_object_range<xml_node_iterator> children() const; 709 xml_object_range<xml_named_node_iterator> children(const char_t* name) const; 710 xml_object_range<xml_attribute_iterator> attributes() const; 711 712 // Get node offset in parsed file/string (in char_t units) for debugging purposes 713 ptrdiff_t offset_debug() const; 714 715 // Get hash value (unique for handles to the same object) 716 size_t hash_value() const; 717 718 // Get internal pointer 719 xml_node_struct* internal_object() const; 720 }; 721 722 #ifdef __BORLANDC__ 723 // Borland C++ workaround 724 bool PUGIXML_FUNCTION operator&&(const xml_node& lhs, bool rhs); 725 bool PUGIXML_FUNCTION operator||(const xml_node& lhs, bool rhs); 726 #endif 727 728 // A helper for working with text inside PCDATA nodes 729 class PUGIXML_CLASS xml_text 730 { 731 friend class xml_node; 732 733 xml_node_struct* _root; 734 735 typedef void (*unspecified_bool_type)(xml_text***); 736 737 explicit xml_text(xml_node_struct* root); 738 739 xml_node_struct* _data_new(); 740 xml_node_struct* _data() const; 741 742 public: 743 // Default constructor. Constructs an empty object. 744 xml_text(); 745 746 // Safe bool conversion operator 747 operator unspecified_bool_type() const; 748 749 // Borland C++ workaround 750 bool operator!() const; 751 752 // Check if text object is empty 753 bool empty() const; 754 755 // Get text, or "" if object is empty 756 const char_t* get() const; 757 758 // Get text, or the default value if object is empty 759 const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; 760 761 // Get text as a number, or the default value if conversion did not succeed or object is empty 762 int as_int(int def = 0) const; 763 unsigned int as_uint(unsigned int def = 0) const; 764 double as_double(double def = 0) const; 765 float as_float(float def = 0) const; 766 767 #ifdef PUGIXML_HAS_LONG_LONG 768 long long as_llong(long long def = 0) const; 769 unsigned long long as_ullong(unsigned long long def = 0) const; 770 #endif 771 772 // Get text as bool (returns true if first character is in '1tTyY' set), or the default value if object is empty 773 bool as_bool(bool def = false) const; 774 775 // Set text (returns false if object is empty or there is not enough memory) 776 bool set(const char_t* rhs); 777 778 // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false") 779 bool set(int rhs); 780 bool set(unsigned int rhs); 781 bool set(long rhs); 782 bool set(unsigned long rhs); 783 bool set(double rhs); 784 bool set(double rhs, int precision); 785 bool set(float rhs); 786 bool set(float rhs, int precision); 787 bool set(bool rhs); 788 789 #ifdef PUGIXML_HAS_LONG_LONG 790 bool set(long long rhs); 791 bool set(unsigned long long rhs); 792 #endif 793 794 // Set text (equivalent to set without error checking) 795 xml_text& operator=(const char_t* rhs); 796 xml_text& operator=(int rhs); 797 xml_text& operator=(unsigned int rhs); 798 xml_text& operator=(long rhs); 799 xml_text& operator=(unsigned long rhs); 800 xml_text& operator=(double rhs); 801 xml_text& operator=(float rhs); 802 xml_text& operator=(bool rhs); 803 804 #ifdef PUGIXML_HAS_LONG_LONG 805 xml_text& operator=(long long rhs); 806 xml_text& operator=(unsigned long long rhs); 807 #endif 808 809 // Get the data node (node_pcdata or node_cdata) for this object 810 xml_node data() const; 811 }; 812 813 #ifdef __BORLANDC__ 814 // Borland C++ workaround 815 bool PUGIXML_FUNCTION operator&&(const xml_text& lhs, bool rhs); 816 bool PUGIXML_FUNCTION operator||(const xml_text& lhs, bool rhs); 817 #endif 818 819 // Child node iterator (a bidirectional iterator over a collection of xml_node) 820 class PUGIXML_CLASS xml_node_iterator 821 { 822 friend class xml_node; 823 824 private: 825 mutable xml_node _wrap; 826 xml_node _parent; 827 828 xml_node_iterator(xml_node_struct* ref, xml_node_struct* parent); 829 830 public: 831 // Iterator traits 832 typedef ptrdiff_t difference_type; 833 typedef xml_node value_type; 834 typedef xml_node* pointer; 835 typedef xml_node& reference; 836 837 #ifndef PUGIXML_NO_STL 838 typedef std::bidirectional_iterator_tag iterator_category; 839 #endif 840 841 // Default constructor 842 xml_node_iterator(); 843 844 // Construct an iterator which points to the specified node 845 xml_node_iterator(const xml_node& node); 846 847 // Iterator operators 848 bool operator==(const xml_node_iterator& rhs) const; 849 bool operator!=(const xml_node_iterator& rhs) const; 850 851 xml_node& operator*() const; 852 xml_node* operator->() const; 853 854 const xml_node_iterator& operator++(); 855 xml_node_iterator operator++(int); 856 857 const xml_node_iterator& operator--(); 858 xml_node_iterator operator--(int); 859 }; 860 861 // Attribute iterator (a bidirectional iterator over a collection of xml_attribute) 862 class PUGIXML_CLASS xml_attribute_iterator 863 { 864 friend class xml_node; 865 866 private: 867 mutable xml_attribute _wrap; 868 xml_node _parent; 869 870 xml_attribute_iterator(xml_attribute_struct* ref, xml_node_struct* parent); 871 872 public: 873 // Iterator traits 874 typedef ptrdiff_t difference_type; 875 typedef xml_attribute value_type; 876 typedef xml_attribute* pointer; 877 typedef xml_attribute& reference; 878 879 #ifndef PUGIXML_NO_STL 880 typedef std::bidirectional_iterator_tag iterator_category; 881 #endif 882 883 // Default constructor 884 xml_attribute_iterator(); 885 886 // Construct an iterator which points to the specified attribute 887 xml_attribute_iterator(const xml_attribute& attr, const xml_node& parent); 888 889 // Iterator operators 890 bool operator==(const xml_attribute_iterator& rhs) const; 891 bool operator!=(const xml_attribute_iterator& rhs) const; 892 893 xml_attribute& operator*() const; 894 xml_attribute* operator->() const; 895 896 const xml_attribute_iterator& operator++(); 897 xml_attribute_iterator operator++(int); 898 899 const xml_attribute_iterator& operator--(); 900 xml_attribute_iterator operator--(int); 901 }; 902 903 // Named node range helper 904 class PUGIXML_CLASS xml_named_node_iterator 905 { 906 friend class xml_node; 907 908 public: 909 // Iterator traits 910 typedef ptrdiff_t difference_type; 911 typedef xml_node value_type; 912 typedef xml_node* pointer; 913 typedef xml_node& reference; 914 915 #ifndef PUGIXML_NO_STL 916 typedef std::bidirectional_iterator_tag iterator_category; 917 #endif 918 919 // Default constructor 920 xml_named_node_iterator(); 921 922 // Construct an iterator which points to the specified node 923 xml_named_node_iterator(const xml_node& node, const char_t* name); 924 925 // Iterator operators 926 bool operator==(const xml_named_node_iterator& rhs) const; 927 bool operator!=(const xml_named_node_iterator& rhs) const; 928 929 xml_node& operator*() const; 930 xml_node* operator->() const; 931 932 const xml_named_node_iterator& operator++(); 933 xml_named_node_iterator operator++(int); 934 935 const xml_named_node_iterator& operator--(); 936 xml_named_node_iterator operator--(int); 937 938 private: 939 mutable xml_node _wrap; 940 xml_node _parent; 941 const char_t* _name; 942 943 xml_named_node_iterator(xml_node_struct* ref, xml_node_struct* parent, const char_t* name); 944 }; 945 946 // Abstract tree walker class (see xml_node::traverse) 947 class PUGIXML_CLASS xml_tree_walker 948 { 949 friend class xml_node; 950 951 private: 952 int _depth; 953 954 protected: 955 // Get current traversal depth 956 int depth() const; 957 958 public: 959 xml_tree_walker(); 960 virtual ~xml_tree_walker(); 961 962 // Callback that is called when traversal begins 963 virtual bool begin(xml_node& node); 964 965 // Callback that is called for each node traversed 966 virtual bool for_each(xml_node& node) = 0; 967 968 // Callback that is called when traversal ends 969 virtual bool end(xml_node& node); 970 }; 971 972 // Parsing status, returned as part of xml_parse_result object 973 enum xml_parse_status 974 { 975 status_ok = 0, // No error 976 977 status_file_not_found, // File was not found during load_file() 978 status_io_error, // Error reading from file/stream 979 status_out_of_memory, // Could not allocate memory 980 status_internal_error, // Internal error occurred 981 982 status_unrecognized_tag, // Parser could not determine tag type 983 984 status_bad_pi, // Parsing error occurred while parsing document declaration/processing instruction 985 status_bad_comment, // Parsing error occurred while parsing comment 986 status_bad_cdata, // Parsing error occurred while parsing CDATA section 987 status_bad_doctype, // Parsing error occurred while parsing document type declaration 988 status_bad_pcdata, // Parsing error occurred while parsing PCDATA section 989 status_bad_start_element, // Parsing error occurred while parsing start element tag 990 status_bad_attribute, // Parsing error occurred while parsing element attribute 991 status_bad_end_element, // Parsing error occurred while parsing end element tag 992 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) 993 994 status_append_invalid_root, // Unable to append nodes since root type is not node_element or node_document (exclusive to xml_node::append_buffer) 995 996 status_no_document_element // Parsing resulted in a document without element nodes 997 }; 998 999 // Parsing result 1000 struct PUGIXML_CLASS xml_parse_result 1001 { 1002 // Parsing status (see xml_parse_status) 1003 xml_parse_status status; 1004 1005 // Last parsed offset (in char_t units from start of input data) 1006 ptrdiff_t offset; 1007 1008 // Source document encoding 1009 xml_encoding encoding; 1010 1011 // Default constructor, initializes object to failed state 1012 xml_parse_result(); 1013 1014 // Cast to bool operator 1015 operator bool() const; 1016 1017 // Get error description 1018 const char* description() const; 1019 }; 1020 1021 // Document class (DOM tree root) 1022 class PUGIXML_CLASS xml_document: public xml_node 1023 { 1024 private: 1025 char_t* _buffer; 1026 1027 char _memory[192]; 1028 1029 // Non-copyable semantics 1030 xml_document(const xml_document&); 1031 xml_document& operator=(const xml_document&); 1032 1033 void _create(); 1034 void _destroy(); 1035 void _move(xml_document& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT; 1036 1037 public: 1038 // Default constructor, makes empty document 1039 xml_document(); 1040 1041 // Destructor, invalidates all node/attribute handles to this document 1042 ~xml_document(); 1043 1044 #ifdef PUGIXML_HAS_MOVE 1045 // Move semantics support 1046 xml_document(xml_document&& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT; 1047 xml_document& operator=(xml_document&& rhs) PUGIXML_NOEXCEPT_IF_NOT_COMPACT; 1048 #endif 1049 1050 // Removes all nodes, leaving the empty document 1051 void reset(); 1052 1053 // Removes all nodes, then copies the entire contents of the specified document 1054 void reset(const xml_document& proto); 1055 1056 #ifndef PUGIXML_NO_STL 1057 // Load document from stream. 1058 xml_parse_result load(std::basic_istream<char, std::char_traits<char> >& stream, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); 1059 xml_parse_result load(std::basic_istream<wchar_t, std::char_traits<wchar_t> >& stream, unsigned int options = parse_default); 1060 #endif 1061 1062 // (deprecated: use load_string instead) Load document from zero-terminated string. No encoding conversions are applied. 1063 PUGIXML_DEPRECATED xml_parse_result load(const char_t* contents, unsigned int options = parse_default); 1064 1065 // Load document from zero-terminated string. No encoding conversions are applied. 1066 xml_parse_result load_string(const char_t* contents, unsigned int options = parse_default); 1067 1068 // Load document from file 1069 xml_parse_result load_file(const char* path, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); 1070 xml_parse_result load_file(const wchar_t* path, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); 1071 1072 // Load document from buffer. Copies/converts the buffer, so it may be deleted or changed after the function returns. 1073 xml_parse_result load_buffer(const void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); 1074 1075 // Load document from buffer, using the buffer for in-place parsing (the buffer is modified and used for storage of document data). 1076 // You should ensure that buffer data will persist throughout the document's lifetime, and free the buffer memory manually once document is destroyed. 1077 xml_parse_result load_buffer_inplace(void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); 1078 1079 // Load document from buffer, using the buffer for in-place parsing (the buffer is modified and used for storage of document data). 1080 // 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). 1081 xml_parse_result load_buffer_inplace_own(void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); 1082 1083 // Save XML document to writer (semantics is slightly different from xml_node::print, see documentation for details). 1084 void save(xml_writer& writer, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const; 1085 1086 #ifndef PUGIXML_NO_STL 1087 // Save XML document to stream (semantics is slightly different from xml_node::print, see documentation for details). 1088 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; 1089 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; 1090 #endif 1091 1092 // Save XML to file 1093 bool save_file(const char* path, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const; 1094 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; 1095 1096 // Get document element 1097 xml_node document_element() const; 1098 }; 1099 1100 #ifndef PUGIXML_NO_XPATH 1101 // XPath query return type 1102 enum xpath_value_type 1103 { 1104 xpath_type_none, // Unknown type (query failed to compile) 1105 xpath_type_node_set, // Node set (xpath_node_set) 1106 xpath_type_number, // Number 1107 xpath_type_string, // String 1108 xpath_type_boolean // Boolean 1109 }; 1110 1111 // XPath parsing result 1112 struct PUGIXML_CLASS xpath_parse_result 1113 { 1114 // Error message (0 if no error) 1115 const char* error; 1116 1117 // Last parsed offset (in char_t units from string start) 1118 ptrdiff_t offset; 1119 1120 // Default constructor, initializes object to failed state 1121 xpath_parse_result(); 1122 1123 // Cast to bool operator 1124 operator bool() const; 1125 1126 // Get error description 1127 const char* description() const; 1128 }; 1129 1130 // A single XPath variable 1131 class PUGIXML_CLASS xpath_variable 1132 { 1133 friend class xpath_variable_set; 1134 1135 protected: 1136 xpath_value_type _type; 1137 xpath_variable* _next; 1138 1139 xpath_variable(xpath_value_type type); 1140 1141 // Non-copyable semantics 1142 xpath_variable(const xpath_variable&); 1143 xpath_variable& operator=(const xpath_variable&); 1144 1145 public: 1146 // Get variable name 1147 const char_t* name() const; 1148 1149 // Get variable type 1150 xpath_value_type type() const; 1151 1152 // Get variable value; no type conversion is performed, default value (false, NaN, empty string, empty node set) is returned on type mismatch error 1153 bool get_boolean() const; 1154 double get_number() const; 1155 const char_t* get_string() const; 1156 const xpath_node_set& get_node_set() const; 1157 1158 // Set variable value; no type conversion is performed, false is returned on type mismatch error 1159 bool set(bool value); 1160 bool set(double value); 1161 bool set(const char_t* value); 1162 bool set(const xpath_node_set& value); 1163 }; 1164 1165 // A set of XPath variables 1166 class PUGIXML_CLASS xpath_variable_set 1167 { 1168 private: 1169 xpath_variable* _data[64]; 1170 1171 void _assign(const xpath_variable_set& rhs); 1172 void _swap(xpath_variable_set& rhs); 1173 1174 xpath_variable* _find(const char_t* name) const; 1175 1176 static bool _clone(xpath_variable* var, xpath_variable** out_result); 1177 static void _destroy(xpath_variable* var); 1178 1179 public: 1180 // Default constructor/destructor 1181 xpath_variable_set(); 1182 ~xpath_variable_set(); 1183 1184 // Copy constructor/assignment operator 1185 xpath_variable_set(const xpath_variable_set& rhs); 1186 xpath_variable_set& operator=(const xpath_variable_set& rhs); 1187 1188 #ifdef PUGIXML_HAS_MOVE 1189 // Move semantics support 1190 xpath_variable_set(xpath_variable_set&& rhs) PUGIXML_NOEXCEPT; 1191 xpath_variable_set& operator=(xpath_variable_set&& rhs) PUGIXML_NOEXCEPT; 1192 #endif 1193 1194 // Add a new variable or get the existing one, if the types match 1195 xpath_variable* add(const char_t* name, xpath_value_type type); 1196 1197 // Set value of an existing variable; no type conversion is performed, false is returned if there is no such variable or if types mismatch 1198 bool set(const char_t* name, bool value); 1199 bool set(const char_t* name, double value); 1200 bool set(const char_t* name, const char_t* value); 1201 bool set(const char_t* name, const xpath_node_set& value); 1202 1203 // Get existing variable by name 1204 xpath_variable* get(const char_t* name); 1205 const xpath_variable* get(const char_t* name) const; 1206 }; 1207 1208 // A compiled XPath query object 1209 class PUGIXML_CLASS xpath_query 1210 { 1211 private: 1212 void* _impl; 1213 xpath_parse_result _result; 1214 1215 typedef void (*unspecified_bool_type)(xpath_query***); 1216 1217 // Non-copyable semantics 1218 xpath_query(const xpath_query&); 1219 xpath_query& operator=(const xpath_query&); 1220 1221 public: 1222 // Construct a compiled object from XPath expression. 1223 // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on compilation errors. 1224 explicit xpath_query(const char_t* query, xpath_variable_set* variables = PUGIXML_NULL); 1225 1226 // Constructor 1227 xpath_query(); 1228 1229 // Destructor 1230 ~xpath_query(); 1231 1232 #ifdef PUGIXML_HAS_MOVE 1233 // Move semantics support 1234 xpath_query(xpath_query&& rhs) PUGIXML_NOEXCEPT; 1235 xpath_query& operator=(xpath_query&& rhs) PUGIXML_NOEXCEPT; 1236 #endif 1237 1238 // Get query expression return type 1239 xpath_value_type return_type() const; 1240 1241 // Evaluate expression as boolean value in the specified context; performs type conversion if necessary. 1242 // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. 1243 bool evaluate_boolean(const xpath_node& n) const; 1244 1245 // Evaluate expression as double value in the specified context; performs type conversion if necessary. 1246 // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. 1247 double evaluate_number(const xpath_node& n) const; 1248 1249 #ifndef PUGIXML_NO_STL 1250 // Evaluate expression as string value in the specified context; performs type conversion if necessary. 1251 // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. 1252 string_t evaluate_string(const xpath_node& n) const; 1253 #endif 1254 1255 // Evaluate expression as string value in the specified context; performs type conversion if necessary. 1256 // At most capacity characters are written to the destination buffer, full result size is returned (includes terminating zero). 1257 // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. 1258 // If PUGIXML_NO_EXCEPTIONS is defined, returns empty set instead. 1259 size_t evaluate_string(char_t* buffer, size_t capacity, const xpath_node& n) const; 1260 1261 // Evaluate expression as node set in the specified context. 1262 // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on type mismatch and std::bad_alloc on out of memory errors. 1263 // If PUGIXML_NO_EXCEPTIONS is defined, returns empty node set instead. 1264 xpath_node_set evaluate_node_set(const xpath_node& n) const; 1265 1266 // Evaluate expression as node set in the specified context. 1267 // Return first node in document order, or empty node if node set is empty. 1268 // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on type mismatch and std::bad_alloc on out of memory errors. 1269 // If PUGIXML_NO_EXCEPTIONS is defined, returns empty node instead. 1270 xpath_node evaluate_node(const xpath_node& n) const; 1271 1272 // Get parsing result (used to get compilation errors in PUGIXML_NO_EXCEPTIONS mode) 1273 const xpath_parse_result& result() const; 1274 1275 // Safe bool conversion operator 1276 operator unspecified_bool_type() const; 1277 1278 // Borland C++ workaround 1279 bool operator!() const; 1280 }; 1281 1282 #ifndef PUGIXML_NO_EXCEPTIONS 1283 #if defined(_MSC_VER) 1284 // C4275 can be ignored in Visual C++ if you are deriving 1285 // from a type in the Standard C++ Library 1286 #pragma warning(push) 1287 #pragma warning(disable: 4275) 1288 #endif 1289 // XPath exception class 1290 class PUGIXML_CLASS xpath_exception: public std::exception 1291 { 1292 private: 1293 xpath_parse_result _result; 1294 1295 public: 1296 // Construct exception from parse result 1297 explicit xpath_exception(const xpath_parse_result& result); 1298 1299 // Get error message 1300 virtual const char* what() const throw() PUGIXML_OVERRIDE; 1301 1302 // Get parse result 1303 const xpath_parse_result& result() const; 1304 }; 1305 #if defined(_MSC_VER) 1306 #pragma warning(pop) 1307 #endif 1308 #endif 1309 1310 // XPath node class (either xml_node or xml_attribute) 1311 class PUGIXML_CLASS xpath_node 1312 { 1313 private: 1314 xml_node _node; 1315 xml_attribute _attribute; 1316 1317 typedef void (*unspecified_bool_type)(xpath_node***); 1318 1319 public: 1320 // Default constructor; constructs empty XPath node 1321 xpath_node(); 1322 1323 // Construct XPath node from XML node/attribute 1324 xpath_node(const xml_node& node); 1325 xpath_node(const xml_attribute& attribute, const xml_node& parent); 1326 1327 // Get node/attribute, if any 1328 xml_node node() const; 1329 xml_attribute attribute() const; 1330 1331 // Get parent of contained node/attribute 1332 xml_node parent() const; 1333 1334 // Safe bool conversion operator 1335 operator unspecified_bool_type() const; 1336 1337 // Borland C++ workaround 1338 bool operator!() const; 1339 1340 // Comparison operators 1341 bool operator==(const xpath_node& n) const; 1342 bool operator!=(const xpath_node& n) const; 1343 }; 1344 1345 #ifdef __BORLANDC__ 1346 // Borland C++ workaround 1347 bool PUGIXML_FUNCTION operator&&(const xpath_node& lhs, bool rhs); 1348 bool PUGIXML_FUNCTION operator||(const xpath_node& lhs, bool rhs); 1349 #endif 1350 1351 // A fixed-size collection of XPath nodes 1352 class PUGIXML_CLASS xpath_node_set 1353 { 1354 public: 1355 // Collection type 1356 enum type_t 1357 { 1358 type_unsorted, // Not ordered 1359 type_sorted, // Sorted by document order (ascending) 1360 type_sorted_reverse // Sorted by document order (descending) 1361 }; 1362 1363 // Constant iterator type 1364 typedef const xpath_node* const_iterator; 1365 1366 // We define non-constant iterator to be the same as constant iterator so that various generic algorithms (i.e. boost foreach) work 1367 typedef const xpath_node* iterator; 1368 1369 // Default constructor. Constructs empty set. 1370 xpath_node_set(); 1371 1372 // Constructs a set from iterator range; data is not checked for duplicates and is not sorted according to provided type, so be careful 1373 xpath_node_set(const_iterator begin, const_iterator end, type_t type = type_unsorted); 1374 1375 // Destructor 1376 ~xpath_node_set(); 1377 1378 // Copy constructor/assignment operator 1379 xpath_node_set(const xpath_node_set& ns); 1380 xpath_node_set& operator=(const xpath_node_set& ns); 1381 1382 #ifdef PUGIXML_HAS_MOVE 1383 // Move semantics support 1384 xpath_node_set(xpath_node_set&& rhs) PUGIXML_NOEXCEPT; 1385 xpath_node_set& operator=(xpath_node_set&& rhs) PUGIXML_NOEXCEPT; 1386 #endif 1387 1388 // Get collection type 1389 type_t type() const; 1390 1391 // Get collection size 1392 size_t size() const; 1393 1394 // Indexing operator 1395 const xpath_node& operator[](size_t index) const; 1396 1397 // Collection iterators 1398 const_iterator begin() const; 1399 const_iterator end() const; 1400 1401 // Sort the collection in ascending/descending order by document order 1402 void sort(bool reverse = false); 1403 1404 // Get first node in the collection by document order 1405 xpath_node first() const; 1406 1407 // Check if collection is empty 1408 bool empty() const; 1409 1410 private: 1411 type_t _type; 1412 1413 xpath_node _storage[1]; 1414 1415 xpath_node* _begin; 1416 xpath_node* _end; 1417 1418 void _assign(const_iterator begin, const_iterator end, type_t type); 1419 void _move(xpath_node_set& rhs) PUGIXML_NOEXCEPT; 1420 }; 1421 #endif 1422 1423 #ifndef PUGIXML_NO_STL 1424 // Convert wide string to UTF8 1425 std::basic_string<char, std::char_traits<char>, std::allocator<char> > PUGIXML_FUNCTION as_utf8(const wchar_t* str); 1426 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); 1427 1428 // Convert UTF8 to wide string 1429 std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > PUGIXML_FUNCTION as_wide(const char* str); 1430 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); 1431 #endif 1432 1433 // Memory allocation function interface; returns pointer to allocated memory or NULL on failure 1434 typedef void* (*allocation_function)(size_t size); 1435 1436 // Memory deallocation function interface 1437 typedef void (*deallocation_function)(void* ptr); 1438 1439 // Override default memory management functions. All subsequent allocations/deallocations will be performed via supplied functions. 1440 void PUGIXML_FUNCTION set_memory_management_functions(allocation_function allocate, deallocation_function deallocate); 1441 1442 // Get current memory management functions 1443 allocation_function PUGIXML_FUNCTION get_memory_allocation_function(); 1444 deallocation_function PUGIXML_FUNCTION get_memory_deallocation_function(); 1445 } 1446 1447 #if !defined(PUGIXML_NO_STL) && (defined(_MSC_VER) || defined(__ICC)) 1448 namespace std 1449 { 1450 // Workarounds for (non-standard) iterator category detection for older versions (MSVC7/IC8 and earlier) 1451 std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_node_iterator&); 1452 std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_attribute_iterator&); 1453 std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_named_node_iterator&); 1454 } 1455 #endif 1456 1457 #if !defined(PUGIXML_NO_STL) && defined(__SUNPRO_CC) 1458 namespace std 1459 { 1460 // Workarounds for (non-standard) iterator category detection 1461 std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_node_iterator&); 1462 std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_attribute_iterator&); 1463 std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_named_node_iterator&); 1464 } 1465 #endif 1466 1467 #endif 1468 1469 // Make sure implementation is included in header-only mode 1470 // Use macro expansion in #include to work around QMake (QTBUG-11923) 1471 #if defined(PUGIXML_HEADER_ONLY) && !defined(PUGIXML_SOURCE) 1472 # define PUGIXML_SOURCE "pugixml.cpp" 1473 # include PUGIXML_SOURCE 1474 #endif 1475 1476 /** 1477 * Copyright (c) 2006-2020 Arseny Kapoulkine 1478 * 1479 * Permission is hereby granted, free of charge, to any person 1480 * obtaining a copy of this software and associated documentation 1481 * files (the "Software"), to deal in the Software without 1482 * restriction, including without limitation the rights to use, 1483 * copy, modify, merge, publish, distribute, sublicense, and/or sell 1484 * copies of the Software, and to permit persons to whom the 1485 * Software is furnished to do so, subject to the following 1486 * conditions: 1487 * 1488 * The above copyright notice and this permission notice shall be 1489 * included in all copies or substantial portions of the Software. 1490 * 1491 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1492 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 1493 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1494 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 1495 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 1496 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 1497 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 1498 * OTHER DEALINGS IN THE SOFTWARE. 1499 */ 1500