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