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