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