DataDirector API
tinyxml.h
1 /*
2 www.sourceforge.net/projects/tinyxml
3 Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
4 
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
12 
13 1. The origin of this software must not be misrepresented; you must
14 not claim that you wrote the original software. If you use this
15 software in a product, an acknowledgment in the product documentation
16 would be appreciated but is not required.
17 
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20 
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24 
25 
26 #ifndef TINYXML_INCLUDED
27 #define TINYXML_INCLUDED
28 
29 #ifdef _MSC_VER
30 #pragma warning( disable : 4530 )
31 #pragma warning( disable : 4786 )
32 #endif
33 
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
39 #include "DDD_Include.h"
40 
41 // Help out windows:
42 #if defined( _DEBUG ) && !defined( DEBUG )
43 #define DEBUG
44 #endif
45 
46 #if defined( DEBUG ) && defined( _MSC_VER )
47 #include <windows.h>
48 #define TIXML_LOG OutputDebugString
49 #else
50 #define TIXML_LOG printf
51 #endif
52 
53 #ifdef TIXML_USE_STL
54  #include <string>
55  #include <iostream>
56  #define TIXML_STRING std::string
57  #define TIXML_ISTREAM std::istream
58  #define TIXML_OSTREAM std::ostream
59 #else
60  #include "tinystr.h"
61  #define TIXML_STRING TiXmlString
62  #define TIXML_OSTREAM TiXmlOutStream
63 #endif
64 
65 class TiXmlDocument;
66 class TiXmlElement;
67 class TiXmlComment;
68 class TiXmlUnknown;
69 class TiXmlAttribute;
70 class TiXmlText;
71 class TiXmlDeclaration;
72 class TiXmlParsingData;
73 
74 const int TIXML_MAJOR_VERSION = 2;
75 const int TIXML_MINOR_VERSION = 3;
76 const int TIXML_PATCH_VERSION = 4;
77 
78 /* Internal structure for tracking location of items
79  in the XML file.
80 */
82 {
83  TiXmlCursor() { Clear(); }
84  void Clear() { row = col = -1; }
85 
86  int row; // 0 based.
87  int col; // 0 based.
88 };
89 
90 
91 // Only used by Attribute::Query functions
92 enum
93 {
94  TIXML_SUCCESS,
95  TIXML_NO_ATTRIBUTE,
96  TIXML_WRONG_TYPE
97 };
98 
99 
100 // Used by the parsing routines.
101 enum TiXmlEncoding
102 {
103  TIXML_ENCODING_UNKNOWN,
104  TIXML_ENCODING_UTF8,
105  TIXML_ENCODING_LEGACY
106 };
107 
108 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
109 
110 /** TiXmlBase is a base class for every class in TinyXml.
111  It does little except to establish that TinyXml classes
112  can be printed and provide some utility functions.
113 
114  In XML, the document and elements can contain
115  other elements and other types of nodes.
116 
117  @verbatim
118  A Document can contain: Element (container or leaf)
119  Comment (leaf)
120  Unknown (leaf)
121  Declaration( leaf )
122 
123  An Element can contain: Element (container or leaf)
124  Text (leaf)
125  Attributes (not on tree)
126  Comment (leaf)
127  Unknown (leaf)
128 
129  A Decleration contains: Attributes (not on tree)
130  @endverbatim
131 */
132 class DDD_EXPORT TiXmlBase
133 {
134  friend class TiXmlNode;
135  friend class TiXmlElement;
136  friend class TiXmlDocument;
137 
138 public:
139  TiXmlBase() : userData(0) {}
140  virtual ~TiXmlBase() {}
141 
142  /** All TinyXml classes can print themselves to a filestream.
143  This is a formatted print, and will insert tabs and newlines.
144 
145  (For an unformatted stream, use the << operator.)
146  */
147  virtual void Print( FILE* cfile, int depth ) const = 0;
148 
149  /** The world does not agree on whether white space should be kept or
150  not. In order to make everyone happy, these global, static functions
151  are provided to set whether or not TinyXml will condense all white space
152  into a single space or not. The default is to condense. Note changing this
153  values is not thread safe.
154  */
155  static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
156 
157  /// Return the current white space setting.
158  static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
159 
160  /** Return the position, in the original source file, of this node or attribute.
161  The row and column are 1-based. (That is the first row and first column is
162  1,1). If the returns values are 0 or less, then the parser does not have
163  a row and column value.
164 
165  Generally, the row and column value will be set when the TiXmlDocument::Load(),
166  TiXmlDocument::LoadFile(), or any TiXmlNode::Parse() is called. It will NOT be set
167  when the DOM was created from operator>>.
168 
169  The values reflect the initial load. Once the DOM is modified programmatically
170  (by adding or changing nodes and attributes) the new values will NOT update to
171  reflect changes in the document.
172 
173  There is a minor performance cost to computing the row and column. Computation
174  can be disabled if TiXmlDocument::SetTabSize() is called with 0 as the value.
175 
176  @sa TiXmlDocument::SetTabSize()
177  */
178  int Row() const { return location.row + 1; }
179  int Column() const { return location.col + 1; } ///< See Row()
180 
181  void SetUserData( void* user ) { userData = user; }
182  void* GetUserData() { return userData; }
183 
184  // Table that returs, for a given lead byte, the total number of bytes
185  // in the UTF-8 sequence.
186  static const int utf8ByteTable[256];
187 
188  virtual const char* Parse( const char* p,
189  TiXmlParsingData* data,
190  TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
191 
192  enum
193  {
194  TIXML_NO_ERROR = 0,
195  TIXML_ERROR,
196  TIXML_ERROR_OPENING_FILE,
197  TIXML_ERROR_OUT_OF_MEMORY,
198  TIXML_ERROR_PARSING_ELEMENT,
199  TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
200  TIXML_ERROR_READING_ELEMENT_VALUE,
201  TIXML_ERROR_READING_ATTRIBUTES,
202  TIXML_ERROR_PARSING_EMPTY,
203  TIXML_ERROR_READING_END_TAG,
204  TIXML_ERROR_PARSING_UNKNOWN,
205  TIXML_ERROR_PARSING_COMMENT,
206  TIXML_ERROR_PARSING_DECLARATION,
207  TIXML_ERROR_DOCUMENT_EMPTY,
208  TIXML_ERROR_EMBEDDED_NULL,
209 
210  TIXML_ERROR_STRING_COUNT
211  };
212 
213 protected:
214 
215  // See STL_STRING_BUG
216  // Utility class to overcome a bug.
218  {
219  public:
220  StringToBuffer( const TIXML_STRING& str );
221  ~StringToBuffer();
222  char* buffer;
223  };
224 
225  static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
226  inline static bool IsWhiteSpace( char c )
227  {
228  return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
229  }
230 
231  virtual void StreamOut (TIXML_OSTREAM *) const = 0;
232 
233  #ifdef TIXML_USE_STL
234  static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
235  static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
236  #endif
237 
238  /* Reads an XML name into the string provided. Returns
239  a pointer just past the last character of the name,
240  or 0 if the function has an error.
241  */
242  static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
243 
244  /* Reads text. Returns a pointer past the given end tag.
245  Wickedly complex options, but it keeps the (sensitive) code in one place.
246  */
247  static const char* ReadText( const char* in, // where to start
248  TIXML_STRING* text, // the string read
249  bool ignoreWhiteSpace, // whether to keep the white space
250  const char* endTag, // what ends this text
251  bool ignoreCase, // whether to ignore case in the end tag
252  TiXmlEncoding encoding ); // the current encoding
253 
254  // If an entity has been found, transform it into a character.
255  static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
256 
257  // Get a character, while interpreting entities.
258  // The length can be from 0 to 4 bytes.
259  inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
260  {
261  assert( p );
262  if ( encoding == TIXML_ENCODING_UTF8 )
263  {
264  *length = utf8ByteTable[ *((unsigned char*)p) ];
265  assert( *length >= 0 && *length < 5 );
266  }
267  else
268  {
269  *length = 1;
270  }
271 
272  if ( *length == 1 )
273  {
274  if ( *p == '&' )
275  return GetEntity( p, _value, length, encoding );
276  *_value = *p;
277  return p+1;
278  }
279  else if ( *length )
280  {
281  strncpy( _value, p, *length );
282  return p + (*length);
283  }
284  else
285  {
286  // Not valid text.
287  return 0;
288  }
289  }
290 
291  // Puts a string to a stream, expanding entities as it goes.
292  // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
293  static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
294 
295  static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
296 
297  // Return true if the next characters in the stream are any of the endTag sequences.
298  // Ignore case only works for english, and should only be relied on when comparing
299  // to Engilish words: StringEqual( p, "version", true ) is fine.
300  static bool StringEqual( const char* p,
301  const char* endTag,
302  bool ignoreCase,
303  TiXmlEncoding encoding );
304 
305  static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
306 
307  TiXmlCursor location;
308 
309  /// Field containing a generic user pointer
310  void* userData;
311 
312  // None of these methods are reliable for any language except English.
313  // Good for approximation, not great for accuracy.
314  static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
315  static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
316  inline static int ToLower( int v, TiXmlEncoding encoding )
317  {
318  if ( encoding == TIXML_ENCODING_UTF8 )
319  {
320  if ( v < 128 ) return tolower( v );
321  return v;
322  }
323  else
324  {
325  return tolower( v );
326  }
327  }
328  static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
329 
330 private:
331  TiXmlBase( const TiXmlBase& ); // not implemented.
332  void operator=( const TiXmlBase& base ); // not allowed.
333 
334  struct Entity
335  {
336  const char* str;
337  unsigned int strLength;
338  char chr;
339  };
340  enum
341  {
342  NUM_ENTITY = 5,
343  MAX_ENTITY_LENGTH = 6
344 
345  };
346  static Entity entity[ NUM_ENTITY ];
347  static bool condenseWhiteSpace;
348 };
349 
350 
351 /** The parent class for everything in the Document Object Model.
352  (Except for attributes).
353  Nodes have siblings, a parent, and children. A node can be
354  in a document, or stand on its own. The type of a TiXmlNode
355  can be queried, and it can be cast to its more defined type.
356 */
357 class DDD_EXPORT TiXmlNode : public TiXmlBase
358 {
359  friend class TiXmlDocument;
360  friend class TiXmlElement;
361 
362 public:
363  #ifdef TIXML_USE_STL
364 
365  /** An input stream operator, for every class. Tolerant of newlines and
366  formatting, but doesn't expect them.
367  */
368  friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
369 
370  /** An output stream operator, for every class. Note that this outputs
371  without any newlines or formatting, as opposed to Print(), which
372  includes tabs and new lines.
373 
374  The operator<< and operator>> are not completely symmetric. Writing
375  a node to a stream is very well defined. You'll get a nice stream
376  of output, without any extra whitespace or newlines.
377 
378  But reading is not as well defined. (As it always is.) If you create
379  a TiXmlElement (for example) and read that from an input stream,
380  the text needs to define an element or junk will result. This is
381  true of all input streams, but it's worth keeping in mind.
382 
383  A TiXmlDocument will read nodes until it reads a root element, and
384  all the children of that root element.
385  */
386  friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
387 
388  /// Appends the XML node or attribute to a std::string.
389  friend std::string& operator<< (std::string& out, const TiXmlNode& base );
390 
391  #else
392  // Used internally, not part of the public API.
393  friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
394  #endif
395 
396  /** The types of XML nodes supported by TinyXml. (All the
397  unsupported types are picked up by UNKNOWN.)
398  */
399  enum NodeType
400  {
401  DOCUMENT,
402  ELEMENT,
403  COMMENT,
404  UNKNOWN,
405  TEXT,
406  DECLARATION,
407  TYPECOUNT
408  };
409 
410  virtual ~TiXmlNode();
411 
412  /** The meaning of 'value' changes for the specific type of
413  TiXmlNode.
414  @verbatim
415  Document: filename of the xml file
416  Element: name of the element
417  Comment: the comment text
418  Unknown: the tag contents
419  Text: the text string
420  @endverbatim
421 
422  The subclasses will wrap this function.
423  */
424  const char * Value() const { return value.c_str (); }
425 
426  /** Changes the value of the node. Defined as:
427  @verbatim
428  Document: filename of the xml file
429  Element: name of the element
430  Comment: the comment text
431  Unknown: the tag contents
432  Text: the text string
433  @endverbatim
434  */
435  void SetValue(const char * _value) { value = _value;}
436 
437  #ifdef TIXML_USE_STL
438  /// STL std::string form.
439  void SetValue( const std::string& _value )
440  {
441  StringToBuffer buf( _value );
442  SetValue( buf.buffer ? buf.buffer : "" );
443  }
444  #endif
445 
446  /// Delete all the children of this node. Does not affect 'this'.
447  void Clear();
448 
449  /// One step up the DOM.
450  TiXmlNode* Parent() { return parent; }
451  const TiXmlNode* Parent() const { return parent; }
452 
453  const TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children.
454  TiXmlNode* FirstChild() { return firstChild; }
455  const TiXmlNode* FirstChild( const char * value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found.
456  TiXmlNode* FirstChild( const char * value ); ///< The first child of this node with the matching 'value'. Will be null if none found.
457 
458  const TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children.
459  TiXmlNode* LastChild() { return lastChild; }
460  const TiXmlNode* LastChild( const char * value ) const; /// The last child of this node matching 'value'. Will be null if there are no children.
461  TiXmlNode* LastChild( const char * value );
462 
463  #ifdef TIXML_USE_STL
464  const TiXmlNode* FirstChild( const std::string& _value ) const { return FirstChild (_value.c_str ()); } ///< STL std::string form.
465  TiXmlNode* FirstChild( const std::string& _value ) { return FirstChild (_value.c_str ()); } ///< STL std::string form.
466  const TiXmlNode* LastChild( const std::string& _value ) const { return LastChild (_value.c_str ()); } ///< STL std::string form.
467  TiXmlNode* LastChild( const std::string& _value ) { return LastChild (_value.c_str ()); } ///< STL std::string form.
468  #endif
469 
470  /** An alternate way to walk the children of a node.
471  One way to iterate over nodes is:
472  @verbatim
473  for( child = parent->FirstChild(); child; child = child->NextSibling() )
474  @endverbatim
475 
476  IterateChildren does the same thing with the syntax:
477  @verbatim
478  child = 0;
479  while( child = parent->IterateChildren( child ) )
480  @endverbatim
481 
482  IterateChildren takes the previous child as input and finds
483  the next one. If the previous child is null, it returns the
484  first. IterateChildren will return null when done.
485  */
486  const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
487  TiXmlNode* IterateChildren( TiXmlNode* previous );
488 
489  /// This flavor of IterateChildren searches for children with a particular 'value'
490  const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
491  TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
492 
493  #ifdef TIXML_USE_STL
494  const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
495  TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
496  #endif
497 
498  /** Add a new node related to this. Adds a child past the LastChild.
499  Returns a pointer to the new object or NULL if an error occured.
500  */
501  TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
502 
503 
504  /** Add a new node related to this. Adds a child past the LastChild.
505 
506  NOTE: the node to be added is passed by pointer, and will be
507  henceforth owned (and deleted) by tinyXml. This method is efficient
508  and avoids an extra copy, but should be used with care as it
509  uses a different memory model than the other insert functions.
510 
511  @sa InsertEndChild
512  */
513  TiXmlNode* LinkEndChild( TiXmlNode* addThis );
514 
515  /** Add a new node related to this. Adds a child before the specified child.
516  Returns a pointer to the new object or NULL if an error occured.
517  */
518  TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
519 
520  /** Add a new node related to this. Adds a child after the specified child.
521  Returns a pointer to the new object or NULL if an error occured.
522  */
523  TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
524 
525  /** Replace a child of this node.
526  Returns a pointer to the new object or NULL if an error occured.
527  */
528  TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
529 
530  /// Delete a child of this node.
531  bool RemoveChild( TiXmlNode* removeThis );
532 
533  /// Navigate to a sibling node.
534  const TiXmlNode* PreviousSibling() const { return prev; }
535  TiXmlNode* PreviousSibling() { return prev; }
536 
537  /// Navigate to a sibling node.
538  const TiXmlNode* PreviousSibling( const char * ) const;
539  TiXmlNode* PreviousSibling( const char * );
540 
541  #ifdef TIXML_USE_STL
542  const TiXmlNode* PreviousSibling( const std::string& _value ) const { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
543  TiXmlNode* PreviousSibling( const std::string& _value ) { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
544  const TiXmlNode* NextSibling( const std::string& _value) const { return NextSibling (_value.c_str ()); } ///< STL std::string form.
545  TiXmlNode* NextSibling( const std::string& _value) { return NextSibling (_value.c_str ()); } ///< STL std::string form.
546  #endif
547 
548  /// Navigate to a sibling node.
549  const TiXmlNode* NextSibling() const { return next; }
550  TiXmlNode* NextSibling() { return next; }
551 
552  /// Navigate to a sibling node with the given 'value'.
553  const TiXmlNode* NextSibling( const char * ) const;
554  TiXmlNode* NextSibling( const char * );
555 
556  /** Convenience function to get through elements.
557  Calls NextSibling and ToElement. Will skip all non-Element
558  nodes. Returns 0 if there is not another element.
559  */
560  const TiXmlElement* NextSiblingElement() const;
562 
563  /** Convenience function to get through elements.
564  Calls NextSibling and ToElement. Will skip all non-Element
565  nodes. Returns 0 if there is not another element.
566  */
567  const TiXmlElement* NextSiblingElement( const char * ) const;
568  TiXmlElement* NextSiblingElement( const char * );
569 
570  #ifdef TIXML_USE_STL
571  const TiXmlElement* NextSiblingElement( const std::string& _value) const { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
572  TiXmlElement* NextSiblingElement( const std::string& _value) { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
573  #endif
574 
575  /// Convenience function to get through elements.
576  const TiXmlElement* FirstChildElement() const;
578 
579  /// Convenience function to get through elements.
580  const TiXmlElement* FirstChildElement( const char * value ) const;
581  TiXmlElement* FirstChildElement( const char * value );
582 
583  #ifdef TIXML_USE_STL
584  const TiXmlElement* FirstChildElement( const std::string& _value ) const { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
585  TiXmlElement* FirstChildElement( const std::string& _value ) { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
586  #endif
587 
588  /** Query the type (as an enumerated value, above) of this node.
589  The possible types are: DOCUMENT, ELEMENT, COMMENT,
590  UNKNOWN, TEXT, and DECLARATION.
591  */
592  virtual int Type() const { return type; }
593 
594  /** Return a pointer to the Document this node lives in.
595  Returns null if not in a document.
596  */
597  const TiXmlDocument* GetDocument() const;
599 
600  /// Returns true if this node has no children.
601  bool NoChildren() const { return !firstChild; }
602 
603  const TiXmlDocument* ToDocument() const { return ( this && type == DOCUMENT ) ? (const TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
604  const TiXmlElement* ToElement() const { return ( this && type == ELEMENT ) ? (const TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
605  const TiXmlComment* ToComment() const { return ( this && type == COMMENT ) ? (const TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
606  const TiXmlUnknown* ToUnknown() const { return ( this && type == UNKNOWN ) ? (const TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
607  const TiXmlText* ToText() const { return ( this && type == TEXT ) ? (const TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
608  const TiXmlDeclaration* ToDeclaration() const { return ( this && type == DECLARATION ) ? (const TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
609 
610  TiXmlDocument* ToDocument() { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
611  TiXmlElement* ToElement() { return ( this && type == ELEMENT ) ? (TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
612  TiXmlComment* ToComment() { return ( this && type == COMMENT ) ? (TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
613  TiXmlUnknown* ToUnknown() { return ( this && type == UNKNOWN ) ? (TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
614  TiXmlText* ToText() { return ( this && type == TEXT ) ? (TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
615  TiXmlDeclaration* ToDeclaration() { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
616 
617  /** Create an exact duplicate of this node and return it. The memory must be deleted
618  by the caller.
619  */
620  virtual TiXmlNode* Clone() const = 0;
621 
622 protected:
623  TiXmlNode( NodeType _type );
624 
625  // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
626  // and the assignment operator.
627  void CopyTo( TiXmlNode* target ) const;
628 
629  #ifdef TIXML_USE_STL
630  // The real work of the input operator.
631  virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
632  #endif
633 
634  // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
635  TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
636 
637  // Internal Value function returning a TIXML_STRING
638  const TIXML_STRING& SValue() const { return value ; }
639 
640  TiXmlNode* parent;
641  NodeType type;
642 
643  TiXmlNode* firstChild;
644  TiXmlNode* lastChild;
645 
646  TIXML_STRING value;
647 
648  TiXmlNode* prev;
649  TiXmlNode* next;
650 
651 private:
652  TiXmlNode( const TiXmlNode& ); // not implemented.
653  void operator=( const TiXmlNode& base ); // not allowed.
654 };
655 
656 
657 /** An attribute is a name-value pair. Elements have an arbitrary
658  number of attributes, each with a unique name.
659 
660  @note The attributes are not TiXmlNodes, since they are not
661  part of the tinyXML document object model. There are other
662  suggested ways to look at this problem.
663 */
664 class DDD_EXPORT TiXmlAttribute : public TiXmlBase
665 {
666  friend class TiXmlAttributeSet;
667 
668 public:
669  /// Construct an empty attribute.
671  {
672  document = 0;
673  prev = next = 0;
674  }
675 
676  #ifdef TIXML_USE_STL
677  /// std::string constructor.
678  TiXmlAttribute( const std::string& _name, const std::string& _value )
679  {
680  name = _name;
681  value = _value;
682  document = 0;
683  prev = next = 0;
684  }
685  #endif
686 
687  /// Construct an attribute with a name and value.
688  TiXmlAttribute( const char * _name, const char * _value )
689  {
690  name = _name;
691  value = _value;
692  document = 0;
693  prev = next = 0;
694  }
695 
696  const char* Name() const { return name.c_str (); } ///< Return the name of this attribute.
697  const char* Value() const { return value.c_str (); } ///< Return the value of this attribute.
698  const int IntValue() const; ///< Return the value of this attribute, converted to an integer.
699  const double DoubleValue() const; ///< Return the value of this attribute, converted to a double.
700 
701  /** QueryIntValue examines the value string. It is an alternative to the
702  IntValue() method with richer error checking.
703  If the value is an integer, it is stored in 'value' and
704  the call returns TIXML_SUCCESS. If it is not
705  an integer, it returns TIXML_WRONG_TYPE.
706 
707  A specialized but useful call. Note that for success it returns 0,
708  which is the opposite of almost all other TinyXml calls.
709  */
710  int QueryIntValue( int* value ) const;
711  /// QueryDoubleValue examines the value string. See QueryIntValue().
712  int QueryDoubleValue( double* value ) const;
713 
714  void SetName( const char* _name ) { name = _name; } ///< Set the name of this attribute.
715  void SetValue( const char* _value ) { value = _value; } ///< Set the value.
716 
717  void SetIntValue( int value ); ///< Set the value from an integer.
718  void SetDoubleValue( double value ); ///< Set the value from a double.
719 
720  #ifdef TIXML_USE_STL
721  /// STL std::string form.
722  void SetName( const std::string& _name )
723  {
724  StringToBuffer buf( _name );
725  SetName ( buf.buffer ? buf.buffer : "error" );
726  }
727  /// STL std::string form.
728  void SetValue( const std::string& _value )
729  {
730  StringToBuffer buf( _value );
731  SetValue( buf.buffer ? buf.buffer : "error" );
732  }
733  #endif
734 
735  /// Get the next sibling attribute in the DOM. Returns null at end.
736  const TiXmlAttribute* Next() const;
737  TiXmlAttribute* Next();
738  /// Get the previous sibling attribute in the DOM. Returns null at beginning.
739  const TiXmlAttribute* Previous() const;
740  TiXmlAttribute* Previous();
741 
742  bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
743  bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
744  bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
745 
746  /* Attribute parsing starts: first letter of the name
747  returns: the next char after the value end quote
748  */
749  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
750 
751  // Prints this Attribute to a FILE stream.
752  virtual void Print( FILE* cfile, int depth ) const;
753 
754  virtual void StreamOut( TIXML_OSTREAM * out ) const;
755  // [internal use]
756  // Set the document pointer so the attribute can report errors.
757  void SetDocument( TiXmlDocument* doc ) { document = doc; }
758 
759 private:
760  TiXmlAttribute( const TiXmlAttribute& ); // not implemented.
761  void operator=( const TiXmlAttribute& base ); // not allowed.
762 
763  TiXmlDocument* document; // A pointer back to a document, for error reporting.
764  TIXML_STRING name;
765  TIXML_STRING value;
766  TiXmlAttribute* prev;
767  TiXmlAttribute* next;
768 };
769 
770 
771 /* A class used to manage a group of attributes.
772  It is only used internally, both by the ELEMENT and the DECLARATION.
773 
774  The set can be changed transparent to the Element and Declaration
775  classes that use it, but NOT transparent to the Attribute
776  which has to implement a next() and previous() method. Which makes
777  it a bit problematic and prevents the use of STL.
778 
779  This version is implemented with circular lists because:
780  - I like circular lists
781  - it demonstrates some independence from the (typical) doubly linked list.
782 */
784 {
785 public:
788 
789  void Add( TiXmlAttribute* attribute );
790  void Remove( TiXmlAttribute* attribute );
791 
792  const TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
793  TiXmlAttribute* First() { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
794  const TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
795  TiXmlAttribute* Last() { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
796 
797  const TiXmlAttribute* Find( const char * name ) const;
798  TiXmlAttribute* Find( const char * name );
799 
800 private:
801  //*ME: Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
802  //*ME: this class must be also use a hidden/disabled copy-constructor !!!
803  TiXmlAttributeSet( const TiXmlAttributeSet& ); // not allowed
804  void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
805 
806  TiXmlAttribute sentinel;
807 };
808 
809 
810 /** The element is a container class. It has a value, the element name,
811  and can contain other elements, text, comments, and unknowns.
812  Elements also contain an arbitrary number of attributes.
813 */
814 class DDD_EXPORT TiXmlElement : public TiXmlNode
815 {
816 public:
817  /// Construct an element.
818  TiXmlElement (const char * in_value);
819 
820  #ifdef TIXML_USE_STL
821  /// std::string constructor.
822  TiXmlElement( const std::string& _value );
823  #endif
824 
825  TiXmlElement( const TiXmlElement& );
826 
827  void operator=( const TiXmlElement& base );
828 
829  virtual ~TiXmlElement();
830 
831  /** Given an attribute name, Attribute() returns the value
832  for the attribute of that name, or null if none exists.
833  */
834  const char* Attribute( const char* name ) const;
835 
836  /** Given an attribute name, Attribute() returns the value
837  for the attribute of that name, or null if none exists.
838  If the attribute exists and can be converted to an integer,
839  the integer value will be put in the return 'i', if 'i'
840  is non-null.
841  */
842  const char* Attribute( const char* name, int* i ) const;
843 
844  /** Given an attribute name, Attribute() returns the value
845  for the attribute of that name, or null if none exists.
846  If the attribute exists and can be converted to an double,
847  the double value will be put in the return 'd', if 'd'
848  is non-null.
849  */
850  const char* Attribute( const char* name, double* d ) const;
851 
852  /** QueryIntAttribute examines the attribute - it is an alternative to the
853  Attribute() method with richer error checking.
854  If the attribute is an integer, it is stored in 'value' and
855  the call returns TIXML_SUCCESS. If it is not
856  an integer, it returns TIXML_WRONG_TYPE. If the attribute
857  does not exist, then TIXML_NO_ATTRIBUTE is returned.
858  */
859  int QueryIntAttribute( const char* name, int* value ) const;
860  /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
861  int QueryDoubleAttribute( const char* name, double* value ) const;
862  /// QueryFloatAttribute examines the attribute - see QueryIntAttribute().
863  int QueryDoubleAttribute( const char* name, float* value ) const {
864  double d;
865  int result = QueryDoubleAttribute( name, &d );
866  *value = (float)d;
867  return result;
868  }
869 
870  /** Sets an attribute of name to a given value. The attribute
871  will be created if it does not exist, or changed if it does.
872  */
873  void SetAttribute( const char* name, const char * value );
874 
875  #ifdef TIXML_USE_STL
876  const char* Attribute( const std::string& name ) const { return Attribute( name.c_str() ); }
877  const char* Attribute( const std::string& name, int* i ) const { return Attribute( name.c_str(), i ); }
878  const char* Attribute( const std::string& name, double* d ) const { return Attribute( name.c_str(), d ); }
879  int QueryIntAttribute( const std::string& name, int* value ) const { return QueryIntAttribute( name.c_str(), value ); }
880  int QueryDoubleAttribute( const std::string& name, double* value ) const { return QueryDoubleAttribute( name.c_str(), value ); }
881 
882  /// STL std::string form.
883  void SetAttribute( const std::string& name, const std::string& _value )
884  {
885  StringToBuffer n( name );
886  StringToBuffer v( _value );
887  if ( n.buffer && v.buffer )
888  SetAttribute (n.buffer, v.buffer );
889  }
890  ///< STL std::string form.
891  void SetAttribute( const std::string& name, int _value )
892  {
893  StringToBuffer n( name );
894  if ( n.buffer )
895  SetAttribute (n.buffer, _value);
896  }
897  #endif
898 
899  /** Sets an attribute of name to a given value. The attribute
900  will be created if it does not exist, or changed if it does.
901  */
902  void SetAttribute( const char * name, int value );
903 
904  /** Sets an attribute of name to a given value. The attribute
905  will be created if it does not exist, or changed if it does.
906  */
907  void SetDoubleAttribute( const char * name, double value );
908 
909  /** Deletes an attribute with the given name.
910  */
911  void RemoveAttribute( const char * name );
912  #ifdef TIXML_USE_STL
913  void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); } ///< STL std::string form.
914  #endif
915 
916  const TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
917  TiXmlAttribute* FirstAttribute() { return attributeSet.First(); }
918  const TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); } ///< Access the last attribute in this element.
919  TiXmlAttribute* LastAttribute() { return attributeSet.Last(); }
920 
921  /// Creates a new Element and returns it - the returned element is a copy.
922  virtual TiXmlNode* Clone() const;
923  // Print the Element to a FILE stream.
924  virtual void Print( FILE* cfile, int depth ) const;
925 
926  /* Attribtue parsing starts: next char past '<'
927  returns: next char past '>'
928  */
929  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
930 
931 protected:
932 
933  void CopyTo( TiXmlElement* target ) const;
934  void ClearThis(); // like clear, but initializes 'this' object as well
935 
936  // Used to be public [internal use]
937  #ifdef TIXML_USE_STL
938  virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
939  #endif
940  virtual void StreamOut( TIXML_OSTREAM * out ) const;
941 
942  /* [internal use]
943  Reads the "value" of the element -- another element, or text.
944  This should terminate with the current end tag.
945  */
946  const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
947 
948 private:
949 
950  TiXmlAttributeSet attributeSet;
951 };
952 
953 
954 /** An XML comment.
955 */
956 class DDD_EXPORT TiXmlComment : public TiXmlNode
957 {
958 public:
959  /// Constructs an empty comment.
960  TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
961  TiXmlComment( const TiXmlComment& );
962  void operator=( const TiXmlComment& base );
963 
964  virtual ~TiXmlComment() {}
965 
966  /// Returns a copy of this Comment.
967  virtual TiXmlNode* Clone() const;
968  /// Write this Comment to a FILE stream.
969  virtual void Print( FILE* cfile, int depth ) const;
970 
971  /* Attribtue parsing starts: at the ! of the !--
972  returns: next char past '>'
973  */
974  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
975 
976 protected:
977  void CopyTo( TiXmlComment* target ) const;
978 
979  // used to be public
980  #ifdef TIXML_USE_STL
981  virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
982  #endif
983  virtual void StreamOut( TIXML_OSTREAM * out ) const;
984 
985 private:
986 
987 };
988 
989 
990 /** XML text. Contained in an element.
991 */
992 class DDD_EXPORT TiXmlText : public TiXmlNode
993 {
994  friend class TiXmlElement;
995 public:
996  /// Constructor.
997  TiXmlText (const char * initValue) : TiXmlNode (TiXmlNode::TEXT)
998  {
999  SetValue( initValue );
1000  }
1001  virtual ~TiXmlText() {}
1002 
1003  #ifdef TIXML_USE_STL
1004  /// Constructor.
1005  TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
1006  {
1007  SetValue( initValue );
1008  }
1009  #endif
1010 
1011  TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT ) { copy.CopyTo( this ); }
1012  void operator=( const TiXmlText& base ) { base.CopyTo( this ); }
1013 
1014  /// Write this text object to a FILE stream.
1015  virtual void Print( FILE* cfile, int depth ) const;
1016 
1017  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1018 
1019 protected :
1020  /// [internal use] Creates a new Element and returns it.
1021  virtual TiXmlNode* Clone() const;
1022  void CopyTo( TiXmlText* target ) const;
1023 
1024  virtual void StreamOut ( TIXML_OSTREAM * out ) const;
1025  bool Blank() const; // returns true if all white space and new lines
1026  // [internal use]
1027  #ifdef TIXML_USE_STL
1028  virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1029  #endif
1030 
1031 private:
1032 };
1033 
1034 
1035 /** In correct XML the declaration is the first entry in the file.
1036  @verbatim
1037  <?xml version="1.0" standalone="yes"?>
1038  @endverbatim
1039 
1040  TinyXml will happily read or write files without a declaration,
1041  however. There are 3 possible attributes to the declaration:
1042  version, encoding, and standalone.
1043 
1044  Note: In this version of the code, the attributes are
1045  handled as special cases, not generic attributes, simply
1046  because there can only be at most 3 and they are always the same.
1047 */
1048 class DDD_EXPORT TiXmlDeclaration : public TiXmlNode
1049 {
1050 public:
1051  /// Construct an empty declaration.
1052  TiXmlDeclaration() : TiXmlNode( TiXmlNode::DECLARATION ) {}
1053 
1054 #ifdef TIXML_USE_STL
1055  /// Constructor.
1056  TiXmlDeclaration( const std::string& _version,
1057  const std::string& _encoding,
1058  const std::string& _standalone );
1059 #endif
1060 
1061  /// Construct.
1062  TiXmlDeclaration( const char* _version,
1063  const char* _encoding,
1064  const char* _standalone );
1065 
1066  TiXmlDeclaration( const TiXmlDeclaration& copy );
1067  void operator=( const TiXmlDeclaration& copy );
1068 
1069  virtual ~TiXmlDeclaration() {}
1070 
1071  /// Version. Will return an empty string if none was found.
1072  const char *Version() const { return version.c_str (); }
1073  /// Encoding. Will return an empty string if none was found.
1074  const char *Encoding() const { return encoding.c_str (); }
1075  /// Is this a standalone document?
1076  const char *Standalone() const { return standalone.c_str (); }
1077 
1078  /// Creates a copy of this Declaration and returns it.
1079  virtual TiXmlNode* Clone() const;
1080  /// Print this declaration to a FILE stream.
1081  virtual void Print( FILE* cfile, int depth ) const;
1082 
1083  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1084 
1085 protected:
1086  void CopyTo( TiXmlDeclaration* target ) const;
1087  // used to be public
1088  #ifdef TIXML_USE_STL
1089  virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1090  #endif
1091  virtual void StreamOut ( TIXML_OSTREAM * out) const;
1092 
1093 private:
1094 
1095  TIXML_STRING version;
1096  TIXML_STRING encoding;
1097  TIXML_STRING standalone;
1098 };
1099 
1100 
1101 /** Any tag that tinyXml doesn't recognize is saved as an
1102  unknown. It is a tag of text, but should not be modified.
1103  It will be written back to the XML, unchanged, when the file
1104  is saved.
1105 
1106  DTD tags get thrown into TiXmlUnknowns.
1107 */
1108 class DDD_EXPORT TiXmlUnknown : public TiXmlNode
1109 {
1110 public:
1111  TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
1112  virtual ~TiXmlUnknown() {}
1113 
1114  TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN ) { copy.CopyTo( this ); }
1115  void operator=( const TiXmlUnknown& copy ) { copy.CopyTo( this ); }
1116 
1117  /// Creates a copy of this Unknown and returns it.
1118  virtual TiXmlNode* Clone() const;
1119  /// Print this Unknown to a FILE stream.
1120  virtual void Print( FILE* cfile, int depth ) const;
1121 
1122  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1123 
1124 protected:
1125  void CopyTo( TiXmlUnknown* target ) const;
1126 
1127  #ifdef TIXML_USE_STL
1128  virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1129  #endif
1130  virtual void StreamOut ( TIXML_OSTREAM * out ) const;
1131 
1132 private:
1133 
1134 };
1135 
1136 
1137 /** Always the top level node. A document binds together all the
1138  XML pieces. It can be saved, loaded, and printed to the screen.
1139  The 'value' of a document node is the xml file name.
1140 */
1141 class DDD_EXPORT TiXmlDocument : public TiXmlNode
1142 {
1143 public:
1144  /// Create an empty document, that has no name.
1145  TiXmlDocument();
1146  /// Create a document with a name. The name of the document is also the filename of the xml.
1147  TiXmlDocument( const char * documentName );
1148 
1149  #ifdef TIXML_USE_STL
1150  /// Constructor.
1151  TiXmlDocument( const std::string& documentName );
1152  #endif
1153 
1154  TiXmlDocument( const TiXmlDocument& copy );
1155  void operator=( const TiXmlDocument& copy );
1156 
1157  virtual ~TiXmlDocument() {}
1158 
1159  /** Load a file using the current document value.
1160  Returns true if successful. Will delete any existing
1161  document data before loading.
1162  */
1163  bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1164  /// Save a file using the current document value. Returns true if successful.
1165  bool SaveFile() const;
1166  /// Load a file using the given filename. Returns true if successful.
1167  bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1168  /// Save a file using the given filename. Returns true if successful.
1169  bool SaveFile( const char * filename ) const;
1170 
1171  #ifdef TIXML_USE_STL
1172  bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ) ///< STL std::string version.
1173  {
1174  StringToBuffer f( filename );
1175  return ( f.buffer && LoadFile( f.buffer, encoding ));
1176  }
1177  bool SaveFile( const std::string& filename ) const ///< STL std::string version.
1178  {
1179  StringToBuffer f( filename );
1180  return ( f.buffer && SaveFile( f.buffer ));
1181  }
1182  #endif
1183 
1184  /** Parse the given null terminated block of xml data. Passing in an encoding to this
1185  method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml
1186  to use that encoding, regardless of what TinyXml might otherwise try to detect.
1187  */
1188  virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1189 
1190  /** Get the root element -- the only top level element -- of the document.
1191  In well formed XML, there should only be one. TinyXml is tolerant of
1192  multiple elements at the document level.
1193  */
1194  const TiXmlElement* RootElement() const { return FirstChildElement(); }
1195  TiXmlElement* RootElement() { return FirstChildElement(); }
1196 
1197  /** If an error occurs, Error will be set to true. Also,
1198  - The ErrorId() will contain the integer identifier of the error (not generally useful)
1199  - The ErrorDesc() method will return the name of the error. (very useful)
1200  - The ErrorRow() and ErrorCol() will return the location of the error (if known)
1201  */
1202  bool Error() const { return error; }
1203 
1204  /// Contains a textual (english) description of the error if one occurs.
1205  const char * ErrorDesc() const { return errorDesc.c_str (); }
1206 
1207  /** Generally, you probably want the error string ( ErrorDesc() ). But if you
1208  prefer the ErrorId, this function will fetch it.
1209  */
1210  const int ErrorId() const { return errorId; }
1211 
1212  /** Returns the location (if known) of the error. The first column is column 1,
1213  and the first row is row 1. A value of 0 means the row and column wasn't applicable
1214  (memory errors, for example, have no row/column) or the parser lost the error. (An
1215  error in the error reporting, in that case.)
1216 
1217  @sa SetTabSize, Row, Column
1218  */
1219  int ErrorRow() { return errorLocation.row+1; }
1220  int ErrorCol() { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow()
1221 
1222  /** By calling this method, with a tab size
1223  greater than 0, the row and column of each node and attribute is stored
1224  when the file is loaded. Very useful for tracking the DOM back in to
1225  the source file.
1226 
1227  The tab size is required for calculating the location of nodes. If not
1228  set, the default of 4 is used. The tabsize is set per document. Setting
1229  the tabsize to 0 disables row/column tracking.
1230 
1231  Note that row and column tracking is not supported when using operator>>.
1232 
1233  The tab size needs to be enabled before the parse or load. Correct usage:
1234  @verbatim
1235  TiXmlDocument doc;
1236  doc.SetTabSize( 8 );
1237  doc.Load( "myfile.xml" );
1238  @endverbatim
1239 
1240  @sa Row, Column
1241  */
1242  void SetTabSize( int _tabsize ) { tabsize = _tabsize; }
1243 
1244  int TabSize() const { return tabsize; }
1245 
1246  /** If you have handled the error, it can be reset with this call. The error
1247  state is automatically cleared if you Parse a new XML block.
1248  */
1249  void ClearError() { error = false;
1250  errorId = 0;
1251  errorDesc = "";
1252  errorLocation.row = errorLocation.col = 0;
1253  //errorLocation.last = 0;
1254  }
1255 
1256  /** Dump the document to standard out. */
1257  void Print() const { Print( stdout, 0 ); }
1258 
1259  /// Print this Document to a FILE stream.
1260  virtual void Print( FILE* cfile, int depth = 0 ) const;
1261  // [internal use]
1262  void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
1263 
1264 protected :
1265  virtual void StreamOut ( TIXML_OSTREAM * out) const;
1266  // [internal use]
1267  virtual TiXmlNode* Clone() const;
1268  #ifdef TIXML_USE_STL
1269  virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
1270  #endif
1271 
1272 private:
1273  void CopyTo( TiXmlDocument* target ) const;
1274 
1275  bool error;
1276  int errorId;
1277  TIXML_STRING errorDesc;
1278  int tabsize;
1279  TiXmlCursor errorLocation;
1280 };
1281 
1282 
1283 /**
1284  A TiXmlHandle is a class that wraps a node pointer with null checks; this is
1285  an incredibly useful thing. Note that TiXmlHandle is not part of the TinyXml
1286  DOM structure. It is a separate utility class.
1287 
1288  Take an example:
1289  @verbatim
1290  <Document>
1291  <Element attributeA = "valueA">
1292  <Child attributeB = "value1" />
1293  <Child attributeB = "value2" />
1294  </Element>
1295  <Document>
1296  @endverbatim
1297 
1298  Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
1299  easy to write a *lot* of code that looks like:
1300 
1301  @verbatim
1302  TiXmlElement* root = document.FirstChildElement( "Document" );
1303  if ( root )
1304  {
1305  TiXmlElement* element = root->FirstChildElement( "Element" );
1306  if ( element )
1307  {
1308  TiXmlElement* child = element->FirstChildElement( "Child" );
1309  if ( child )
1310  {
1311  TiXmlElement* child2 = child->NextSiblingElement( "Child" );
1312  if ( child2 )
1313  {
1314  // Finally do something useful.
1315  @endverbatim
1316 
1317  And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity
1318  of such code. A TiXmlHandle checks for null pointers so it is perfectly safe
1319  and correct to use:
1320 
1321  @verbatim
1322  TiXmlHandle docHandle( &document );
1323  TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).Element();
1324  if ( child2 )
1325  {
1326  // do something useful
1327  @endverbatim
1328 
1329  Which is MUCH more concise and useful.
1330 
1331  It is also safe to copy handles - internally they are nothing more than node pointers.
1332  @verbatim
1333  TiXmlHandle handleCopy = handle;
1334  @endverbatim
1335 
1336  What they should not be used for is iteration:
1337 
1338  @verbatim
1339  int i=0;
1340  while ( true )
1341  {
1342  TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).Element();
1343  if ( !child )
1344  break;
1345  // do something
1346  ++i;
1347  }
1348  @endverbatim
1349 
1350  It seems reasonable, but it is in fact two embedded while loops. The Child method is
1351  a linear walk to find the element, so this code would iterate much more than it needs
1352  to. Instead, prefer:
1353 
1354  @verbatim
1355  TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).Element();
1356 
1357  for( child; child; child=child->NextSiblingElement() )
1358  {
1359  // do something
1360  }
1361  @endverbatim
1362 */
1363 class DDD_EXPORT TiXmlHandle
1364 {
1365 public:
1366  /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
1367  TiXmlHandle( TiXmlNode* node ) { this->node = node; }
1368  /// Copy constructor
1369  TiXmlHandle( const TiXmlHandle& ref ) { this->node = ref.node; }
1370  TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
1371 
1372  /// Return a handle to the first child node.
1373  TiXmlHandle FirstChild() const;
1374  /// Return a handle to the first child node with the given name.
1375  TiXmlHandle FirstChild( const char * value ) const;
1376  /// Return a handle to the first child element.
1377  TiXmlHandle FirstChildElement() const;
1378  /// Return a handle to the first child element with the given name.
1379  TiXmlHandle FirstChildElement( const char * value ) const;
1380 
1381  /** Return a handle to the "index" child with the given name.
1382  The first child is 0, the second 1, etc.
1383  */
1384  TiXmlHandle Child( const char* value, int index ) const;
1385  /** Return a handle to the "index" child.
1386  The first child is 0, the second 1, etc.
1387  */
1388  TiXmlHandle Child( int index ) const;
1389  /** Return a handle to the "index" child element with the given name.
1390  The first child element is 0, the second 1, etc. Note that only TiXmlElements
1391  are indexed: other types are not counted.
1392  */
1393  TiXmlHandle ChildElement( const char* value, int index ) const;
1394  /** Return a handle to the "index" child element.
1395  The first child element is 0, the second 1, etc. Note that only TiXmlElements
1396  are indexed: other types are not counted.
1397  */
1398  TiXmlHandle ChildElement( int index ) const;
1399 
1400  #ifdef TIXML_USE_STL
1401  TiXmlHandle FirstChild( const std::string& _value ) const { return FirstChild( _value.c_str() ); }
1402  TiXmlHandle FirstChildElement( const std::string& _value ) const { return FirstChildElement( _value.c_str() ); }
1403 
1404  TiXmlHandle Child( const std::string& _value, int index ) const { return Child( _value.c_str(), index ); }
1405  TiXmlHandle ChildElement( const std::string& _value, int index ) const { return ChildElement( _value.c_str(), index ); }
1406  #endif
1407 
1408  /// Return the handle as a TiXmlNode. This may return null.
1409  TiXmlNode* Node() const { return node; }
1410  /// Return the handle as a TiXmlElement. This may return null.
1411  TiXmlElement* Element() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
1412  /// Return the handle as a TiXmlText. This may return null.
1413  TiXmlText* Text() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
1414  /// Return the handle as a TiXmlUnknown. This may return null;
1415  TiXmlUnknown* Unknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
1416 
1417 private:
1418  TiXmlNode* node;
1419 };
1420 
1421 #ifdef _MSC_VER
1422 #pragma warning( default : 4530 )
1423 #pragma warning( default : 4786 )
1424 #endif
1425 
1426 #endif
1427 
TiXmlElement * Element() const
Return the handle as a TiXmlElement. This may return null.
Definition: tinyxml.h:1411
const TiXmlDocument * GetDocument() const
Definition: tinyxml.h:664
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
Definition: tinyxml.h:783
void Clear()
Delete all the children of this node. Does not affect 'this'.
TiXmlNode * InsertBeforeChild(TiXmlNode *beforeThis, const TiXmlNode &addThis)
TiXmlUnknown * Unknown() const
Return the handle as a TiXmlUnknown. This may return null;.
Definition: tinyxml.h:1415
bool Error() const
Definition: tinyxml.h:1202
const char * Encoding() const
Encoding. Will return an empty string if none was found.
Definition: tinyxml.h:1074
void SetValue(const char *_value)
Definition: tinyxml.h:435
TiXmlText(const char *initValue)
Constructor.
Definition: tinyxml.h:997
TiXmlAttribute()
Construct an empty attribute.
Definition: tinyxml.h:670
const char * ErrorDesc() const
Contains a textual (english) description of the error if one occurs.
Definition: tinyxml.h:1205
void ClearError()
Definition: tinyxml.h:1249
const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:604
TiXmlNode * ReplaceChild(TiXmlNode *replaceThis, const TiXmlNode &withThis)
Definition: tinyxml.h:81
TiXmlNode * InsertAfterChild(TiXmlNode *afterThis, const TiXmlNode &addThis)
int ErrorCol()
The column where the error occured. See ErrorRow()
Definition: tinyxml.h:1220
int ErrorRow()
Definition: tinyxml.h:1219
void SetName(const char *_name)
Set the name of this attribute.
Definition: tinyxml.h:714
virtual int Type() const
Definition: tinyxml.h:592
const TiXmlText * ToText() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:607
const char * Name() const
Return the name of this attribute.
Definition: tinyxml.h:696
virtual TiXmlNode * Clone() const =0
TiXmlElement * ToElement()
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:611
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
int Row() const
Definition: tinyxml.h:178
TiXmlUnknown * ToUnknown()
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:613
TiXmlText * Text() const
Return the handle as a TiXmlText. This may return null.
Definition: tinyxml.h:1413
void SetValue(const char *_value)
Set the value.
Definition: tinyxml.h:715
const TiXmlNode * NextSibling() const
Navigate to a sibling node.
Definition: tinyxml.h:549
void SetTabSize(int _tabsize)
Definition: tinyxml.h:1242
const TiXmlElement * NextSiblingElement() const
const TiXmlAttribute * FirstAttribute() const
Access the first attribute in this element.
Definition: tinyxml.h:916
TiXmlText * ToText()
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:614
NodeType
Definition: tinyxml.h:399
int Column() const
See Row()
Definition: tinyxml.h:179
int QueryDoubleAttribute(const char *name, float *value) const
QueryFloatAttribute examines the attribute - see QueryIntAttribute().
Definition: tinyxml.h:863
const TiXmlNode * IterateChildren(const TiXmlNode *previous) const
Definition: tinyxml.h:1048
const int ErrorId() const
Definition: tinyxml.h:1210
Definition: tinyxml.h:1108
const TiXmlAttribute * LastAttribute() const
Access the last attribute in this element.
Definition: tinyxml.h:918
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
TiXmlComment * ToComment()
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:612
const TiXmlUnknown * ToUnknown() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:606
TiXmlComment()
Constructs an empty comment.
Definition: tinyxml.h:960
TiXmlNode * Parent()
One step up the DOM.
Definition: tinyxml.h:450
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
const TiXmlDocument * ToDocument() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:603
Definition: tinyxml.h:1141
TiXmlDocument * ToDocument()
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:610
TiXmlHandle(const TiXmlHandle &ref)
Copy constructor.
Definition: tinyxml.h:1369
void Print() const
Definition: tinyxml.h:1257
const char * Value() const
Return the value of this attribute.
Definition: tinyxml.h:697
TiXmlAttribute(const char *_name, const char *_value)
Construct an attribute with a name and value.
Definition: tinyxml.h:688
Definition: tinyxml.h:1363
TiXmlNode * LastChild()
The last child of this node. Will be null if there are no children.
Definition: tinyxml.h:459
TiXmlHandle(TiXmlNode *node)
Create a handle from any node (at any depth of the tree.) This can be a null pointer.
Definition: tinyxml.h:1367
static bool IsWhiteSpaceCondensed()
Return the current white space setting.
Definition: tinyxml.h:158
const char * Version() const
Version. Will return an empty string if none was found.
Definition: tinyxml.h:1072
const char * Value() const
Definition: tinyxml.h:424
void * userData
Field containing a generic user pointer.
Definition: tinyxml.h:310
const TiXmlNode * PreviousSibling() const
Navigate to a sibling node.
Definition: tinyxml.h:534
Definition: tinyxml.h:132
const TiXmlElement * RootElement() const
Definition: tinyxml.h:1194
TiXmlDeclaration * ToDeclaration()
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:615
Definition: tinyxml.h:357
Definition: tinyxml.h:217
TiXmlNode * Node() const
Return the handle as a TiXmlNode. This may return null.
Definition: tinyxml.h:1409
Definition: tinyxml.h:992
virtual TiXmlNode * Clone() const
Creates a new Element and returns it - the returned element is a copy.
virtual void Print(FILE *cfile, int depth) const =0
TiXmlDeclaration()
Construct an empty declaration.
Definition: tinyxml.h:1052
const char * Standalone() const
Is this a standalone document?
Definition: tinyxml.h:1076
Definition: tinyxml.h:956
static void SetCondenseWhiteSpace(bool condense)
Definition: tinyxml.h:155
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:453
const TiXmlComment * ToComment() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:605
bool NoChildren() const
Returns true if this node has no children.
Definition: tinyxml.h:601
const TiXmlDeclaration * ToDeclaration() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:608
Definition: tinyxml.h:814
bool RemoveChild(TiXmlNode *removeThis)
Delete a child of this node.