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