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