Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tinyxml2.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file tinyxml2.h
1 /*
2 Original code by Lee Thomason (www.grinninglizard.com)
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any
6 damages arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any
9 purpose, including commercial applications, and to alter it and
10 redistribute it freely, subject to the following restrictions:
11 
12 1. The origin of this software must not be misrepresented; you must
13 not claim that you wrote the original software. If you use this
14 software in a product, an acknowledgment in the product documentation
15 would be appreciated but is not required.
16 
17 2. Altered source versions must be plainly marked as such, and
18 must not be misrepresented as being the original software.
19 
20 3. This notice may not be removed or altered from any source
21 distribution.
22 */
23 
24 #ifndef TINYXML2_INCLUDED
25 #define TINYXML2_INCLUDED
26 
27 #if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
28 # include <ctype.h>
29 # include <limits.h>
30 # include <stdio.h>
31 # include <stdlib.h>
32 # include <string.h>
33 # if defined(__PS3__)
34 # include <stddef.h>
35 # endif
36 #else
37 # include <cctype>
38 # include <climits>
39 # include <cstdio>
40 # include <cstdlib>
41 # include <cstring>
42 #endif
43 #include <stdint.h>
44 
45 /*
46  TODO: intern strings instead of allocation.
47 */
48 /*
49  gcc:
50  g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
51 
52  Formatting, Artistic Style:
53  AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
54 */
55 
56 #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
57 # ifndef DEBUG
58 # define DEBUG
59 # endif
60 #endif
61 
62 #ifdef _MSC_VER
63 # pragma warning(push)
64 # pragma warning(disable: 4251)
65 #endif
66 
67 #ifdef _WIN32
68 # ifdef TINYXML2_EXPORT
69 # define TINYXML2_LIB __declspec(dllexport)
70 # elif defined(TINYXML2_IMPORT)
71 # define TINYXML2_LIB __declspec(dllimport)
72 # else
73 # define TINYXML2_LIB
74 # endif
75 #elif __GNUC__ >= 4
76 # define TINYXML2_LIB __attribute__((visibility("default")))
77 #else
78 # define TINYXML2_LIB
79 #endif
80 
81 
82 #if defined(DEBUG)
83 # if defined(_MSC_VER)
84 # // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
85 # define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
86 # elif defined (ANDROID_NDK)
87 # include <android/log.h>
88 # define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
89 # else
90 # include <assert.h>
91 # define TIXMLASSERT assert
92 # endif
93 #else
94 # define TIXMLASSERT( x ) {}
95 #endif
96 
97 
98 /* Versioning, past 1.0.14:
99  http://semver.org/
100 */
101 static const int TIXML2_MAJOR_VERSION = 4;
102 static const int TIXML2_MINOR_VERSION = 0;
103 static const int TIXML2_PATCH_VERSION = 1;
104 
105 namespace tinyxml2
106 {
107 class XMLDocument;
108 class XMLElement;
109 class XMLAttribute;
110 class XMLComment;
111 class XMLText;
112 class XMLDeclaration;
113 class XMLUnknown;
114 class XMLPrinter;
115 
116 /*
117  A class that wraps strings. Normally stores the start and end
118  pointers into the XML file itself, and will apply normalization
119  and entity translation if actually read. Can also store (and memory
120  manage) a traditional char[]
121 */
122 class StrPair
123 {
124 public:
125  enum {
129 
136  };
137 
138  StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
139  ~StrPair();
140 
141  void Set( char* start, char* end, int flags ) {
142  TIXMLASSERT( start );
143  TIXMLASSERT( end );
144  Reset();
145  _start = start;
146  _end = end;
147  _flags = flags | NEEDS_FLUSH;
148  }
149 
150  const char* GetStr();
151 
152  bool Empty() const {
153  return _start == _end;
154  }
155 
156  void SetInternedStr( const char* str ) {
157  Reset();
158  _start = const_cast<char*>(str);
159  }
160 
161  void SetStr( const char* str, int flags=0 );
162 
163  char* ParseText( char* in, const char* endTag, int strFlags );
164  char* ParseName( char* in );
165 
166  void TransferTo( StrPair* other );
167  void Reset();
168 
169 private:
170  void CollapseWhitespace();
171 
172  enum {
173  NEEDS_FLUSH = 0x100,
174  NEEDS_DELETE = 0x200
175  };
176 
177  int _flags;
178  char* _start;
179  char* _end;
180 
181  StrPair( const StrPair& other ); // not supported
182  void operator=( StrPair& other ); // not supported, use TransferTo()
183 };
184 
185 
186 /*
187  A dynamic array of Plain Old Data. Doesn't support constructors, etc.
188  Has a small initial memory pool, so that low or no usage will not
189  cause a call to new/delete
190 */
191 template <class T, int INITIAL_SIZE_XML>
192 class DynArray
193 {
194 public:
196  _mem = _pool;
197  _allocated = INITIAL_SIZE_XML;
198  _size = 0;
199  }
200 
202  if ( _mem != _pool ) {
203  delete [] _mem;
204  }
205  }
206 
207  void Clear() {
208  _size = 0;
209  }
210 
211  void Push( T t ) {
212  TIXMLASSERT( _size < INT_MAX );
213  EnsureCapacity( _size+1 );
214  _mem[_size] = t;
215  ++_size;
216  }
217 
218  T* PushArr( int count ) {
219  TIXMLASSERT( count >= 0 );
220  TIXMLASSERT( _size <= INT_MAX - count );
221  EnsureCapacity( _size+count );
222  T* ret = &_mem[_size];
223  _size += count;
224  return ret;
225  }
226 
227  T Pop() {
228  TIXMLASSERT( _size > 0 );
229  --_size;
230  return _mem[_size];
231  }
232 
233  void PopArr( int count ) {
234  TIXMLASSERT( _size >= count );
235  _size -= count;
236  }
237 
238  bool Empty() const {
239  return _size == 0;
240  }
241 
242  T& operator[](int i) {
243  TIXMLASSERT( i>= 0 && i < _size );
244  return _mem[i];
245  }
246 
247  const T& operator[](int i) const {
248  TIXMLASSERT( i>= 0 && i < _size );
249  return _mem[i];
250  }
251 
252  const T& PeekTop() const {
253  TIXMLASSERT( _size > 0 );
254  return _mem[ _size - 1];
255  }
256 
257  int Size() const {
258  TIXMLASSERT( _size >= 0 );
259  return _size;
260  }
261 
262  int Capacity() const {
263  TIXMLASSERT( _allocated >= INITIAL_SIZE_XML );
264  return _allocated;
265  }
266 
267  const T* Mem() const {
268  TIXMLASSERT( _mem );
269  return _mem;
270  }
271 
272  T* Mem() {
273  TIXMLASSERT( _mem );
274  return _mem;
275  }
276 
277 private:
278  DynArray( const DynArray& ); // not supported
279  void operator=( const DynArray& ); // not supported
280 
281  void EnsureCapacity( int cap ) {
282  TIXMLASSERT( cap > 0 );
283  if ( cap > _allocated ) {
284  TIXMLASSERT( cap <= INT_MAX / 2 );
285  int newAllocated = cap * 2;
286  T* newMem = new T[newAllocated];
287  memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
288  if ( _mem != _pool ) {
289  delete [] _mem;
290  }
291  _mem = newMem;
292  _allocated = newAllocated;
293  }
294  }
295 
296  T* _mem;
297  T _pool[INITIAL_SIZE_XML];
298  int _allocated; // objects allocated
299  int _size; // number objects in use
300 };
301 
302 
303 /*
304  Parent virtual class of a pool for fast allocation
305  and deallocation of objects.
306 */
307 class MemPool
308 {
309 public:
310  MemPool() {}
311  virtual ~MemPool() {}
312 
313  virtual int ItemSize() const = 0;
314  virtual void* Alloc() = 0;
315  virtual void Free( void* ) = 0;
316  virtual void SetTracked() = 0;
317  virtual void Clear() = 0;
318 };
319 
320 
321 /*
322  Template child class to create pools of the correct type.
323 */
324 template< int ITEM_SIZE >
325 class MemPoolT : public MemPool
326 {
327 public:
330  Clear();
331  }
332 
333  void Clear() {
334  // Delete the blocks.
335  while( !_blockPtrs.Empty()) {
336  Block* b = _blockPtrs.Pop();
337  delete b;
338  }
339  _root = 0;
340  _currentAllocs = 0;
341  _nAllocs = 0;
342  _maxAllocs = 0;
343  _nUntracked = 0;
344  }
345 
346  virtual int ItemSize() const {
347  return ITEM_SIZE;
348  }
349  int CurrentAllocs() const {
350  return _currentAllocs;
351  }
352 
353  virtual void* Alloc() {
354  if ( !_root ) {
355  // Need a new block.
356  Block* block = new Block();
357  _blockPtrs.Push( block );
358 
359  Item* blockItems = block->items;
360  for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
361  blockItems[i].next = &(blockItems[i + 1]);
362  }
363  blockItems[ITEMS_PER_BLOCK - 1].next = 0;
364  _root = blockItems;
365  }
366  Item* const result = _root;
367  TIXMLASSERT( result != 0 );
368  _root = _root->next;
369 
370  ++_currentAllocs;
371  if ( _currentAllocs > _maxAllocs ) {
373  }
374  ++_nAllocs;
375  ++_nUntracked;
376  return result;
377  }
378 
379  virtual void Free( void* mem ) {
380  if ( !mem ) {
381  return;
382  }
383  --_currentAllocs;
384  Item* item = static_cast<Item*>( mem );
385 #ifdef DEBUG
386  memset( item, 0xfe, sizeof( *item ) );
387 #endif
388  item->next = _root;
389  _root = item;
390  }
391  void Trace( const char* name ) {
392  printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
393  name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
394  ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
395  }
396 
397  void SetTracked() {
398  --_nUntracked;
399  }
400 
401  int Untracked() const {
402  return _nUntracked;
403  }
404 
405  // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
406  // The test file is large, 170k.
407  // Release: VS2010 gcc(no opt)
408  // 1k: 4000
409  // 2k: 4000
410  // 4k: 3900 21000
411  // 16k: 5200
412  // 32k: 4300
413  // 64k: 4000 21000
414  // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
415  // in private part if ITEMS_PER_BLOCK is private
416  enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
417 
418 private:
419  MemPoolT( const MemPoolT& ); // not supported
420  void operator=( const MemPoolT& ); // not supported
421 
422  union Item {
424  char itemData[ITEM_SIZE];
425  };
426  struct Block {
428  };
431 
433  int _nAllocs;
436 };
437 
438 
439 
460 {
461 public:
462  virtual ~XMLVisitor() {}
463 
465  virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
466  return true;
467  }
469  virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
470  return true;
471  }
472 
474  virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
475  return true;
476  }
478  virtual bool VisitExit( const XMLElement& /*element*/ ) {
479  return true;
480  }
481 
483  virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
484  return true;
485  }
487  virtual bool Visit( const XMLText& /*text*/ ) {
488  return true;
489  }
491  virtual bool Visit( const XMLComment& /*comment*/ ) {
492  return true;
493  }
495  virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
496  return true;
497  }
498 };
499 
500 // WARNING: must match XMLDocument::_errorNames[]
501 enum XMLError {
522 
524 };
525 
526 
527 /*
528  Utility functionality.
529 */
530 class XMLUtil
531 {
532 public:
533  static const char* SkipWhiteSpace( const char* p ) {
534  TIXMLASSERT( p );
535  while( IsWhiteSpace(*p) ) {
536  ++p;
537  }
538  TIXMLASSERT( p );
539  return p;
540  }
541  static char* SkipWhiteSpace( char* p ) {
542  return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p) ) );
543  }
544 
545  // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
546  // correct, but simple, and usually works.
547  static bool IsWhiteSpace( char p ) {
548  return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
549  }
550 
551  inline static bool IsNameStartChar( unsigned char ch ) {
552  if ( ch >= 128 ) {
553  // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
554  return true;
555  }
556  if ( isalpha( ch ) ) {
557  return true;
558  }
559  return ch == ':' || ch == '_';
560  }
561 
562  inline static bool IsNameChar( unsigned char ch ) {
563  return IsNameStartChar( ch )
564  || isdigit( ch )
565  || ch == '.'
566  || ch == '-';
567  }
568 
569  inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
570  if ( p == q ) {
571  return true;
572  }
573  TIXMLASSERT( p );
574  TIXMLASSERT( q );
575  TIXMLASSERT( nChar >= 0 );
576  return strncmp( p, q, nChar ) == 0;
577  }
578 
579  inline static bool IsUTF8Continuation( char p ) {
580  return ( p & 0x80 ) != 0;
581  }
582 
583  static const char* ReadBOM( const char* p, bool* hasBOM );
584  // p is the starting location,
585  // the UTF-8 value of the entity will be placed in value, and length filled in.
586  static const char* GetCharacterRef( const char* p, char* value, int* length );
587  static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
588 
589  // converts primitive types to strings
590  static void ToStr( int v, char* buffer, int bufferSize );
591  static void ToStr( unsigned v, char* buffer, int bufferSize );
592  static void ToStr( bool v, char* buffer, int bufferSize );
593  static void ToStr( float v, char* buffer, int bufferSize );
594  static void ToStr( double v, char* buffer, int bufferSize );
595  static void ToStr(int64_t v, char* buffer, int bufferSize);
596 
597  // converts strings to primitive types
598  static bool ToInt( const char* str, int* value );
599  static bool ToUnsigned( const char* str, unsigned* value );
600  static bool ToBool( const char* str, bool* value );
601  static bool ToFloat( const char* str, float* value );
602  static bool ToDouble( const char* str, double* value );
603  static bool ToInt64(const char* str, int64_t* value);
604 };
605 
606 
633 {
634  friend class XMLDocument;
635  friend class XMLElement;
636 public:
637 
639  const XMLDocument* GetDocument() const {
640  TIXMLASSERT( _document );
641  return _document;
642  }
645  TIXMLASSERT( _document );
646  return _document;
647  }
648 
650  virtual XMLElement* ToElement() {
651  return 0;
652  }
654  virtual XMLText* ToText() {
655  return 0;
656  }
658  virtual XMLComment* ToComment() {
659  return 0;
660  }
662  virtual XMLDocument* ToDocument() {
663  return 0;
664  }
667  return 0;
668  }
670  virtual XMLUnknown* ToUnknown() {
671  return 0;
672  }
673 
674  virtual const XMLElement* ToElement() const {
675  return 0;
676  }
677  virtual const XMLText* ToText() const {
678  return 0;
679  }
680  virtual const XMLComment* ToComment() const {
681  return 0;
682  }
683  virtual const XMLDocument* ToDocument() const {
684  return 0;
685  }
686  virtual const XMLDeclaration* ToDeclaration() const {
687  return 0;
688  }
689  virtual const XMLUnknown* ToUnknown() const {
690  return 0;
691  }
692 
702  const char* Value() const;
703 
707  void SetValue( const char* val, bool staticMem=false );
708 
710  const XMLNode* Parent() const {
711  return _parent;
712  }
713 
715  return _parent;
716  }
717 
719  bool NoChildren() const {
720  return !_firstChild;
721  }
722 
724  const XMLNode* FirstChild() const {
725  return _firstChild;
726  }
727 
729  return _firstChild;
730  }
731 
735  const XMLElement* FirstChildElement( const char* name = 0 ) const;
736 
737  XMLElement* FirstChildElement( const char* name = 0 ) {
738  return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
739  }
740 
742  const XMLNode* LastChild() const {
743  return _lastChild;
744  }
745 
747  return _lastChild;
748  }
749 
753  const XMLElement* LastChildElement( const char* name = 0 ) const;
754 
755  XMLElement* LastChildElement( const char* name = 0 ) {
756  return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
757  }
758 
760  const XMLNode* PreviousSibling() const {
761  return _prev;
762  }
763 
765  return _prev;
766  }
767 
769  const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
770 
771  XMLElement* PreviousSiblingElement( const char* name = 0 ) {
772  return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
773  }
774 
776  const XMLNode* NextSibling() const {
777  return _next;
778  }
779 
781  return _next;
782  }
783 
785  const XMLElement* NextSiblingElement( const char* name = 0 ) const;
786 
787  XMLElement* NextSiblingElement( const char* name = 0 ) {
788  return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
789  }
790 
798  XMLNode* InsertEndChild( XMLNode* addThis );
799 
800  XMLNode* LinkEndChild( XMLNode* addThis ) {
801  return InsertEndChild( addThis );
802  }
810  XMLNode* InsertFirstChild( XMLNode* addThis );
819  XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
820 
824  void DeleteChildren();
825 
829  void DeleteChild( XMLNode* node );
830 
840  virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
841 
848  virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
849 
872  virtual bool Accept( XMLVisitor* visitor ) const = 0;
873 
879  void SetUserData(void* userData) { _userData = userData; }
880 
886  void* GetUserData() const { return _userData; }
887 
888 protected:
889  XMLNode( XMLDocument* );
890  virtual ~XMLNode();
891 
892  virtual char* ParseDeep( char*, StrPair* );
893 
896  mutable StrPair _value;
897 
900 
903 
904  void* _userData;
905 
906 private:
908  void Unlink( XMLNode* child );
909  static void DeleteNode( XMLNode* node );
910  void InsertChildPreamble( XMLNode* insertThis ) const;
911  const XMLElement* ToElementWithName( const char* name ) const;
912 
913  XMLNode( const XMLNode& ); // not supported
914  XMLNode& operator=( const XMLNode& ); // not supported
915 };
916 
917 
931 {
932  friend class XMLDocument;
933 public:
934  virtual bool Accept( XMLVisitor* visitor ) const;
935 
936  virtual XMLText* ToText() {
937  return this;
938  }
939  virtual const XMLText* ToText() const {
940  return this;
941  }
942 
944  void SetCData( bool isCData ) {
945  _isCData = isCData;
946  }
948  bool CData() const {
949  return _isCData;
950  }
951 
952  virtual XMLNode* ShallowClone( XMLDocument* document ) const;
953  virtual bool ShallowEqual( const XMLNode* compare ) const;
954 
955 protected:
956  XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
957  virtual ~XMLText() {}
958 
959  char* ParseDeep( char*, StrPair* endTag );
960 
961 private:
962  bool _isCData;
963 
964  XMLText( const XMLText& ); // not supported
965  XMLText& operator=( const XMLText& ); // not supported
966 };
967 
968 
971 {
972  friend class XMLDocument;
973 public:
974  virtual XMLComment* ToComment() {
975  return this;
976  }
977  virtual const XMLComment* ToComment() const {
978  return this;
979  }
980 
981  virtual bool Accept( XMLVisitor* visitor ) const;
982 
983  virtual XMLNode* ShallowClone( XMLDocument* document ) const;
984  virtual bool ShallowEqual( const XMLNode* compare ) const;
985 
986 protected:
988  virtual ~XMLComment();
989 
990  char* ParseDeep( char*, StrPair* endTag );
991 
992 private:
993  XMLComment( const XMLComment& ); // not supported
994  XMLComment& operator=( const XMLComment& ); // not supported
995 };
996 
997 
1010 {
1011  friend class XMLDocument;
1012 public:
1014  return this;
1015  }
1016  virtual const XMLDeclaration* ToDeclaration() const {
1017  return this;
1018  }
1019 
1020  virtual bool Accept( XMLVisitor* visitor ) const;
1021 
1022  virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1023  virtual bool ShallowEqual( const XMLNode* compare ) const;
1024 
1025 protected:
1027  virtual ~XMLDeclaration();
1028 
1029  char* ParseDeep( char*, StrPair* endTag );
1030 
1031 private:
1032  XMLDeclaration( const XMLDeclaration& ); // not supported
1033  XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
1034 };
1035 
1036 
1045 {
1046  friend class XMLDocument;
1047 public:
1048  virtual XMLUnknown* ToUnknown() {
1049  return this;
1050  }
1051  virtual const XMLUnknown* ToUnknown() const {
1052  return this;
1053  }
1054 
1055  virtual bool Accept( XMLVisitor* visitor ) const;
1056 
1057  virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1058  virtual bool ShallowEqual( const XMLNode* compare ) const;
1059 
1060 protected:
1062  virtual ~XMLUnknown();
1063 
1064  char* ParseDeep( char*, StrPair* endTag );
1065 
1066 private:
1067  XMLUnknown( const XMLUnknown& ); // not supported
1068  XMLUnknown& operator=( const XMLUnknown& ); // not supported
1069 };
1070 
1071 
1072 
1080 {
1081  friend class XMLElement;
1082 public:
1084  const char* Name() const;
1085 
1087  const char* Value() const;
1088 
1090  const XMLAttribute* Next() const {
1091  return _next;
1092  }
1093 
1098  int IntValue() const {
1099  int i = 0;
1100  QueryIntValue(&i);
1101  return i;
1102  }
1103 
1104  int64_t Int64Value() const {
1105  int64_t i = 0;
1106  QueryInt64Value(&i);
1107  return i;
1108  }
1109 
1111  unsigned UnsignedValue() const {
1112  unsigned i=0;
1113  QueryUnsignedValue( &i );
1114  return i;
1115  }
1117  bool BoolValue() const {
1118  bool b=false;
1119  QueryBoolValue( &b );
1120  return b;
1121  }
1123  double DoubleValue() const {
1124  double d=0;
1125  QueryDoubleValue( &d );
1126  return d;
1127  }
1129  float FloatValue() const {
1130  float f=0;
1131  QueryFloatValue( &f );
1132  return f;
1133  }
1134 
1139  XMLError QueryIntValue( int* value ) const;
1141  XMLError QueryUnsignedValue( unsigned int* value ) const;
1143  XMLError QueryInt64Value(int64_t* value) const;
1145  XMLError QueryBoolValue( bool* value ) const;
1147  XMLError QueryDoubleValue( double* value ) const;
1149  XMLError QueryFloatValue( float* value ) const;
1150 
1152  void SetAttribute( const char* value );
1154  void SetAttribute( int value );
1156  void SetAttribute( unsigned value );
1158  void SetAttribute(int64_t value);
1160  void SetAttribute( bool value );
1162  void SetAttribute( double value );
1164  void SetAttribute( float value );
1165 
1166 private:
1167  enum { BUF_SIZE = 200 };
1168 
1169  XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
1170  virtual ~XMLAttribute() {}
1171 
1172  XMLAttribute( const XMLAttribute& ); // not supported
1173  void operator=( const XMLAttribute& ); // not supported
1174  void SetName( const char* name );
1175 
1176  char* ParseDeep( char* p, bool processEntities );
1177 
1178  mutable StrPair _name;
1179  mutable StrPair _value;
1182 };
1183 
1184 
1190 {
1191  friend class XMLDocument;
1192 public:
1194  const char* Name() const {
1195  return Value();
1196  }
1198  void SetName( const char* str, bool staticMem=false ) {
1199  SetValue( str, staticMem );
1200  }
1201 
1202  virtual XMLElement* ToElement() {
1203  return this;
1204  }
1205  virtual const XMLElement* ToElement() const {
1206  return this;
1207  }
1208  virtual bool Accept( XMLVisitor* visitor ) const;
1209 
1233  const char* Attribute( const char* name, const char* value=0 ) const;
1234 
1241  int IntAttribute(const char* name, int defaultValue = 0) const;
1243  unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
1245  int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
1247  bool BoolAttribute(const char* name, bool defaultValue = false) const;
1249  double DoubleAttribute(const char* name, double defaultValue = 0) const;
1251  float FloatAttribute(const char* name, float defaultValue = 0) const;
1252 
1266  XMLError QueryIntAttribute( const char* name, int* value ) const {
1267  const XMLAttribute* a = FindAttribute( name );
1268  if ( !a ) {
1269  return XML_NO_ATTRIBUTE;
1270  }
1271  return a->QueryIntValue( value );
1272  }
1273 
1275  XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
1276  const XMLAttribute* a = FindAttribute( name );
1277  if ( !a ) {
1278  return XML_NO_ATTRIBUTE;
1279  }
1280  return a->QueryUnsignedValue( value );
1281  }
1282 
1284  XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1285  const XMLAttribute* a = FindAttribute(name);
1286  if (!a) {
1287  return XML_NO_ATTRIBUTE;
1288  }
1289  return a->QueryInt64Value(value);
1290  }
1291 
1293  XMLError QueryBoolAttribute( const char* name, bool* value ) const {
1294  const XMLAttribute* a = FindAttribute( name );
1295  if ( !a ) {
1296  return XML_NO_ATTRIBUTE;
1297  }
1298  return a->QueryBoolValue( value );
1299  }
1301  XMLError QueryDoubleAttribute( const char* name, double* value ) const {
1302  const XMLAttribute* a = FindAttribute( name );
1303  if ( !a ) {
1304  return XML_NO_ATTRIBUTE;
1305  }
1306  return a->QueryDoubleValue( value );
1307  }
1309  XMLError QueryFloatAttribute( const char* name, float* value ) const {
1310  const XMLAttribute* a = FindAttribute( name );
1311  if ( !a ) {
1312  return XML_NO_ATTRIBUTE;
1313  }
1314  return a->QueryFloatValue( value );
1315  }
1316 
1317 
1335  int QueryAttribute( const char* name, int* value ) const {
1336  return QueryIntAttribute( name, value );
1337  }
1338 
1339  int QueryAttribute( const char* name, unsigned int* value ) const {
1340  return QueryUnsignedAttribute( name, value );
1341  }
1342 
1343  int QueryAttribute(const char* name, int64_t* value) const {
1344  return QueryInt64Attribute(name, value);
1345  }
1346 
1347  int QueryAttribute( const char* name, bool* value ) const {
1348  return QueryBoolAttribute( name, value );
1349  }
1350 
1351  int QueryAttribute( const char* name, double* value ) const {
1352  return QueryDoubleAttribute( name, value );
1353  }
1354 
1355  int QueryAttribute( const char* name, float* value ) const {
1356  return QueryFloatAttribute( name, value );
1357  }
1358 
1360  void SetAttribute( const char* name, const char* value ) {
1361  XMLAttribute* a = FindOrCreateAttribute( name );
1362  a->SetAttribute( value );
1363  }
1365  void SetAttribute( const char* name, int value ) {
1366  XMLAttribute* a = FindOrCreateAttribute( name );
1367  a->SetAttribute( value );
1368  }
1370  void SetAttribute( const char* name, unsigned value ) {
1371  XMLAttribute* a = FindOrCreateAttribute( name );
1372  a->SetAttribute( value );
1373  }
1374 
1376  void SetAttribute(const char* name, int64_t value) {
1377  XMLAttribute* a = FindOrCreateAttribute(name);
1378  a->SetAttribute(value);
1379  }
1380 
1382  void SetAttribute( const char* name, bool value ) {
1383  XMLAttribute* a = FindOrCreateAttribute( name );
1384  a->SetAttribute( value );
1385  }
1387  void SetAttribute( const char* name, double value ) {
1388  XMLAttribute* a = FindOrCreateAttribute( name );
1389  a->SetAttribute( value );
1390  }
1392  void SetAttribute( const char* name, float value ) {
1393  XMLAttribute* a = FindOrCreateAttribute( name );
1394  a->SetAttribute( value );
1395  }
1396 
1400  void DeleteAttribute( const char* name );
1401 
1403  const XMLAttribute* FirstAttribute() const {
1404  return _rootAttribute;
1405  }
1407  const XMLAttribute* FindAttribute( const char* name ) const;
1408 
1437  const char* GetText() const;
1438 
1473  void SetText( const char* inText );
1475  void SetText( int value );
1477  void SetText( unsigned value );
1479  void SetText(int64_t value);
1481  void SetText( bool value );
1483  void SetText( double value );
1485  void SetText( float value );
1486 
1513  XMLError QueryIntText( int* ival ) const;
1515  XMLError QueryUnsignedText( unsigned* uval ) const;
1517  XMLError QueryInt64Text(int64_t* uval) const;
1519  XMLError QueryBoolText( bool* bval ) const;
1521  XMLError QueryDoubleText( double* dval ) const;
1523  XMLError QueryFloatText( float* fval ) const;
1524 
1525  int IntText(int defaultValue = 0) const;
1526 
1528  unsigned UnsignedText(unsigned defaultValue = 0) const;
1530  int64_t Int64Text(int64_t defaultValue = 0) const;
1532  bool BoolText(bool defaultValue = false) const;
1534  double DoubleText(double defaultValue = 0) const;
1536  float FloatText(float defaultValue = 0) const;
1537 
1538  // internal:
1539  enum {
1540  OPEN, // <foo>
1541  CLOSED, // <foo/>
1542  CLOSING // </foo>
1543  };
1544  int ClosingType() const {
1545  return _closingType;
1546  }
1547  virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1548  virtual bool ShallowEqual( const XMLNode* compare ) const;
1549 
1550 protected:
1551  char* ParseDeep( char* p, StrPair* endTag );
1552 
1553 private:
1555  virtual ~XMLElement();
1556  XMLElement( const XMLElement& ); // not supported
1557  void operator=( const XMLElement& ); // not supported
1558 
1559  XMLAttribute* FindAttribute( const char* name ) {
1560  return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
1561  }
1562  XMLAttribute* FindOrCreateAttribute( const char* name );
1563  //void LinkAttribute( XMLAttribute* attrib );
1564  char* ParseAttributes( char* p );
1565  static void DeleteAttribute( XMLAttribute* attribute );
1566  XMLAttribute* CreateAttribute();
1567 
1568  enum { BUF_SIZE = 200 };
1570  // The attribute list is ordered; there is no 'lastAttribute'
1571  // because the list needs to be scanned for dupes before adding
1572  // a new attribute.
1574 };
1575 
1576 
1580 };
1581 
1582 
1589 {
1590  friend class XMLElement;
1591 public:
1593  XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
1594  ~XMLDocument();
1595 
1597  TIXMLASSERT( this == _document );
1598  return this;
1599  }
1600  virtual const XMLDocument* ToDocument() const {
1601  TIXMLASSERT( this == _document );
1602  return this;
1603  }
1604 
1615  XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
1616 
1622  XMLError LoadFile( const char* filename );
1623 
1635  XMLError LoadFile( FILE* );
1636 
1642  XMLError SaveFile( const char* filename, bool compact = false );
1643 
1651  XMLError SaveFile( FILE* fp, bool compact = false );
1652 
1653  bool ProcessEntities() const {
1654  return _processEntities;
1655  }
1657  return _whitespace;
1658  }
1659 
1663  bool HasBOM() const {
1664  return _writeBOM;
1665  }
1668  void SetBOM( bool useBOM ) {
1669  _writeBOM = useBOM;
1670  }
1671 
1676  return FirstChildElement();
1677  }
1678  const XMLElement* RootElement() const {
1679  return FirstChildElement();
1680  }
1681 
1696  void Print( XMLPrinter* streamer=0 ) const;
1697  virtual bool Accept( XMLVisitor* visitor ) const;
1698 
1704  XMLElement* NewElement( const char* name );
1710  XMLComment* NewComment( const char* comment );
1716  XMLText* NewText( const char* text );
1728  XMLDeclaration* NewDeclaration( const char* text=0 );
1734  XMLUnknown* NewUnknown( const char* text );
1735 
1740  void DeleteNode( XMLNode* node );
1741 
1742  void SetError( XMLError error, const char* str1, const char* str2 );
1743 
1744  void ClearError() {
1745  SetError(XML_SUCCESS, 0, 0);
1746  }
1747 
1749  bool Error() const {
1750  return _errorID != XML_SUCCESS;
1751  }
1753  XMLError ErrorID() const {
1754  return _errorID;
1755  }
1756  const char* ErrorName() const;
1757 
1759  const char* GetErrorStr1() const {
1760  return _errorStr1.GetStr();
1761  }
1763  const char* GetErrorStr2() const {
1764  return _errorStr2.GetStr();
1765  }
1767  void PrintError() const;
1768 
1770  void Clear();
1771 
1772  // internal
1773  char* Identify( char* p, XMLNode** node );
1774 
1775  virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1776  return 0;
1777  }
1778  virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1779  return false;
1780  }
1781 
1782 private:
1783  XMLDocument( const XMLDocument& ); // not supported
1784  void operator=( const XMLDocument& ); // not supported
1785 
1793 
1798 
1799  static const char* _errorNames[XML_ERROR_COUNT];
1800 
1801  void Parse();
1802 };
1803 
1804 
1861 {
1862 public:
1865  _node = node;
1866  }
1869  _node = &node;
1870  }
1873  _node = ref._node;
1874  }
1877  _node = ref._node;
1878  return *this;
1879  }
1880 
1883  return XMLHandle( _node ? _node->FirstChild() : 0 );
1884  }
1886  XMLHandle FirstChildElement( const char* name = 0 ) {
1887  return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
1888  }
1891  return XMLHandle( _node ? _node->LastChild() : 0 );
1892  }
1894  XMLHandle LastChildElement( const char* name = 0 ) {
1895  return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
1896  }
1899  return XMLHandle( _node ? _node->PreviousSibling() : 0 );
1900  }
1903  return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
1904  }
1907  return XMLHandle( _node ? _node->NextSibling() : 0 );
1908  }
1910  XMLHandle NextSiblingElement( const char* name = 0 ) {
1911  return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
1912  }
1913 
1916  return _node;
1917  }
1920  return ( _node ? _node->ToElement() : 0 );
1921  }
1924  return ( _node ? _node->ToText() : 0 );
1925  }
1928  return ( _node ? _node->ToUnknown() : 0 );
1929  }
1932  return ( _node ? _node->ToDeclaration() : 0 );
1933  }
1934 
1935 private:
1937 };
1938 
1939 
1945 {
1946 public:
1948  _node = node;
1949  }
1951  _node = &node;
1952  }
1954  _node = ref._node;
1955  }
1956 
1958  _node = ref._node;
1959  return *this;
1960  }
1961 
1962  const XMLConstHandle FirstChild() const {
1963  return XMLConstHandle( _node ? _node->FirstChild() : 0 );
1964  }
1965  const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
1966  return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
1967  }
1968  const XMLConstHandle LastChild() const {
1969  return XMLConstHandle( _node ? _node->LastChild() : 0 );
1970  }
1971  const XMLConstHandle LastChildElement( const char* name = 0 ) const {
1972  return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
1973  }
1975  return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
1976  }
1977  const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
1978  return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
1979  }
1980  const XMLConstHandle NextSibling() const {
1981  return XMLConstHandle( _node ? _node->NextSibling() : 0 );
1982  }
1983  const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
1984  return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
1985  }
1986 
1987 
1988  const XMLNode* ToNode() const {
1989  return _node;
1990  }
1991  const XMLElement* ToElement() const {
1992  return ( _node ? _node->ToElement() : 0 );
1993  }
1994  const XMLText* ToText() const {
1995  return ( _node ? _node->ToText() : 0 );
1996  }
1997  const XMLUnknown* ToUnknown() const {
1998  return ( _node ? _node->ToUnknown() : 0 );
1999  }
2001  return ( _node ? _node->ToDeclaration() : 0 );
2002  }
2003 
2004 private:
2005  const XMLNode* _node;
2006 };
2007 
2008 
2052 {
2053 public:
2060  XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
2061  virtual ~XMLPrinter() {}
2062 
2064  void PushHeader( bool writeBOM, bool writeDeclaration );
2068  void OpenElement( const char* name, bool compactMode=false );
2070  void PushAttribute( const char* name, const char* value );
2071  void PushAttribute( const char* name, int value );
2072  void PushAttribute( const char* name, unsigned value );
2073  void PushAttribute(const char* name, int64_t value);
2074  void PushAttribute( const char* name, bool value );
2075  void PushAttribute( const char* name, double value );
2077  virtual void CloseElement( bool compactMode=false );
2078 
2080  void PushText( const char* text, bool cdata=false );
2082  void PushText( int value );
2084  void PushText( unsigned value );
2086  void PushText(int64_t value);
2088  void PushText( bool value );
2090  void PushText( float value );
2092  void PushText( double value );
2093 
2095  void PushComment( const char* comment );
2096 
2097  void PushDeclaration( const char* value );
2098  void PushUnknown( const char* value );
2099 
2100  virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2101  virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2102  return true;
2103  }
2104 
2105  virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2106  virtual bool VisitExit( const XMLElement& element );
2107 
2108  virtual bool Visit( const XMLText& text );
2109  virtual bool Visit( const XMLComment& comment );
2110  virtual bool Visit( const XMLDeclaration& declaration );
2111  virtual bool Visit( const XMLUnknown& unknown );
2112 
2117  const char* CStr() const {
2118  return _buffer.Mem();
2119  }
2125  int CStrSize() const {
2126  return _buffer.Size();
2127  }
2132  void ClearBuffer() {
2133  _buffer.Clear();
2134  _buffer.Push(0);
2135  }
2136 
2137 protected:
2138  virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
2139 
2143  virtual void PrintSpace( int depth );
2144  void Print( const char* format, ... );
2145 
2146  void SealElementIfJustOpened();
2149 
2150 private:
2151  void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
2152 
2154  FILE* _fp;
2155  int _depth;
2159 
2160  enum {
2161  ENTITY_RANGE = 64,
2162  BUF_SIZE = 200
2163  };
2164  bool _entityFlag[ENTITY_RANGE];
2165  bool _restrictedEntityFlag[ENTITY_RANGE];
2166 
2168 };
2169 
2170 
2171 } // tinyxml2
2172 
2173 #if defined(_MSC_VER)
2174 # pragma warning(pop)
2175 #endif
2176 
2177 #endif // TINYXML2_INCLUDED