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