comparison dep/pugixml/src/pugixml.hpp @ 367:8d45d892be88 default tip

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