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