File indexing completed on 2025-01-18 10:18:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #ifndef TINYXML2_INCLUDED
0025 #define TINYXML2_INCLUDED
0026
0027 #if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
0028 # include <ctype.h>
0029 # include <limits.h>
0030 # include <stdio.h>
0031 # include <stdlib.h>
0032 # include <string.h>
0033 # if defined(__PS3__)
0034 # include <stddef.h>
0035 # endif
0036 #else
0037 # include <cctype>
0038 # include <climits>
0039 # include <cstdio>
0040 # include <cstdlib>
0041 # include <cstring>
0042 #endif
0043 #include <stdint.h>
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 #if defined( _DEBUG ) || defined (__DEBUG__)
0057 # ifndef TINYXML2_DEBUG
0058 # define TINYXML2_DEBUG
0059 # endif
0060 #endif
0061
0062 #ifdef _MSC_VER
0063 # pragma warning(push)
0064 # pragma warning(disable: 4251)
0065 #endif
0066
0067 #ifdef _WIN32
0068 # ifdef TINYXML2_EXPORT
0069 # define TINYXML2_LIB __declspec(dllexport)
0070 # elif defined(TINYXML2_IMPORT)
0071 # define TINYXML2_LIB __declspec(dllimport)
0072 # else
0073 # define TINYXML2_LIB
0074 # endif
0075 #elif __GNUC__ >= 4
0076 # define TINYXML2_LIB __attribute__((visibility("default")))
0077 #else
0078 # define TINYXML2_LIB
0079 #endif
0080
0081
0082 #if !defined(TIXMLASSERT)
0083 #if defined(TINYXML2_DEBUG)
0084 # if defined(_MSC_VER)
0085 #
0086 # define TIXMLASSERT( x ) do { if ( !((void)0,(x))) { __debugbreak(); } } while(false)
0087 # elif defined (ANDROID_NDK)
0088 # include <android/log.h>
0089 # define TIXMLASSERT( x ) do { if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); } } while(false)
0090 # else
0091 # include <assert.h>
0092 # define TIXMLASSERT assert
0093 # endif
0094 #else
0095 # define TIXMLASSERT( x ) do {} while(false)
0096 #endif
0097 #endif
0098
0099
0100
0101
0102 static const int TIXML2_MAJOR_VERSION = 9;
0103 static const int TIXML2_MINOR_VERSION = 0;
0104 static const int TIXML2_PATCH_VERSION = 0;
0105
0106 #define TINYXML2_MAJOR_VERSION 9
0107 #define TINYXML2_MINOR_VERSION 0
0108 #define TINYXML2_PATCH_VERSION 0
0109
0110
0111
0112
0113
0114
0115 static const int TINYXML2_MAX_ELEMENT_DEPTH = 500;
0116
0117 namespace tinyxml2
0118 {
0119 class XMLDocument;
0120 class XMLElement;
0121 class XMLAttribute;
0122 class XMLComment;
0123 class XMLText;
0124 class XMLDeclaration;
0125 class XMLUnknown;
0126 class XMLPrinter;
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136 class TINYXML2_LIB StrPair
0137 {
0138 public:
0139 enum Mode {
0140 NEEDS_ENTITY_PROCESSING = 0x01,
0141 NEEDS_NEWLINE_NORMALIZATION = 0x02,
0142 NEEDS_WHITESPACE_COLLAPSING = 0x04,
0143
0144 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
0145 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
0146 ATTRIBUTE_NAME = 0,
0147 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
0148 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
0149 COMMENT = NEEDS_NEWLINE_NORMALIZATION
0150 };
0151
0152 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
0153 ~StrPair();
0154
0155 void Set( char* start, char* end, int flags ) {
0156 TIXMLASSERT( start );
0157 TIXMLASSERT( end );
0158 Reset();
0159 _start = start;
0160 _end = end;
0161 _flags = flags | NEEDS_FLUSH;
0162 }
0163
0164 const char* GetStr();
0165
0166 bool Empty() const {
0167 return _start == _end;
0168 }
0169
0170 void SetInternedStr( const char* str ) {
0171 Reset();
0172 _start = const_cast<char*>(str);
0173 }
0174
0175 void SetStr( const char* str, int flags=0 );
0176
0177 char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
0178 char* ParseName( char* in );
0179
0180 void TransferTo( StrPair* other );
0181 void Reset();
0182
0183 private:
0184 void CollapseWhitespace();
0185
0186 enum {
0187 NEEDS_FLUSH = 0x100,
0188 NEEDS_DELETE = 0x200
0189 };
0190
0191 int _flags;
0192 char* _start;
0193 char* _end;
0194
0195 StrPair( const StrPair& other );
0196 void operator=( const StrPair& other );
0197 };
0198
0199
0200
0201
0202
0203
0204
0205 template <class T, int INITIAL_SIZE>
0206 class DynArray
0207 {
0208 public:
0209 DynArray() :
0210 _mem( _pool ),
0211 _allocated( INITIAL_SIZE ),
0212 _size( 0 )
0213 {
0214 }
0215
0216 ~DynArray() {
0217 if ( _mem != _pool ) {
0218 delete [] _mem;
0219 }
0220 }
0221
0222 void Clear() {
0223 _size = 0;
0224 }
0225
0226 void Push( T t ) {
0227 TIXMLASSERT( _size < INT_MAX );
0228 EnsureCapacity( _size+1 );
0229 _mem[_size] = t;
0230 ++_size;
0231 }
0232
0233 T* PushArr( int count ) {
0234 TIXMLASSERT( count >= 0 );
0235 TIXMLASSERT( _size <= INT_MAX - count );
0236 EnsureCapacity( _size+count );
0237 T* ret = &_mem[_size];
0238 _size += count;
0239 return ret;
0240 }
0241
0242 T Pop() {
0243 TIXMLASSERT( _size > 0 );
0244 --_size;
0245 return _mem[_size];
0246 }
0247
0248 void PopArr( int count ) {
0249 TIXMLASSERT( _size >= count );
0250 _size -= count;
0251 }
0252
0253 bool Empty() const {
0254 return _size == 0;
0255 }
0256
0257 T& operator[](int i) {
0258 TIXMLASSERT( i>= 0 && i < _size );
0259 return _mem[i];
0260 }
0261
0262 const T& operator[](int i) const {
0263 TIXMLASSERT( i>= 0 && i < _size );
0264 return _mem[i];
0265 }
0266
0267 const T& PeekTop() const {
0268 TIXMLASSERT( _size > 0 );
0269 return _mem[ _size - 1];
0270 }
0271
0272 int Size() const {
0273 TIXMLASSERT( _size >= 0 );
0274 return _size;
0275 }
0276
0277 int Capacity() const {
0278 TIXMLASSERT( _allocated >= INITIAL_SIZE );
0279 return _allocated;
0280 }
0281
0282 void SwapRemove(int i) {
0283 TIXMLASSERT(i >= 0 && i < _size);
0284 TIXMLASSERT(_size > 0);
0285 _mem[i] = _mem[_size - 1];
0286 --_size;
0287 }
0288
0289 const T* Mem() const {
0290 TIXMLASSERT( _mem );
0291 return _mem;
0292 }
0293
0294 T* Mem() {
0295 TIXMLASSERT( _mem );
0296 return _mem;
0297 }
0298
0299 private:
0300 DynArray( const DynArray& );
0301 void operator=( const DynArray& );
0302
0303 void EnsureCapacity( int cap ) {
0304 TIXMLASSERT( cap > 0 );
0305 if ( cap > _allocated ) {
0306 TIXMLASSERT( cap <= INT_MAX / 2 );
0307 const int newAllocated = cap * 2;
0308 T* newMem = new T[newAllocated];
0309 TIXMLASSERT( newAllocated >= _size );
0310 memcpy( newMem, _mem, sizeof(T)*_size );
0311 if ( _mem != _pool ) {
0312 delete [] _mem;
0313 }
0314 _mem = newMem;
0315 _allocated = newAllocated;
0316 }
0317 }
0318
0319 T* _mem;
0320 T _pool[INITIAL_SIZE];
0321 int _allocated;
0322 int _size;
0323 };
0324
0325
0326
0327
0328
0329
0330 class MemPool
0331 {
0332 public:
0333 MemPool() {}
0334 virtual ~MemPool() {}
0335
0336 virtual int ItemSize() const = 0;
0337 virtual void* Alloc() = 0;
0338 virtual void Free( void* ) = 0;
0339 virtual void SetTracked() = 0;
0340 };
0341
0342
0343
0344
0345
0346 template< int ITEM_SIZE >
0347 class MemPoolT : public MemPool
0348 {
0349 public:
0350 MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
0351 ~MemPoolT() {
0352 MemPoolT< ITEM_SIZE >::Clear();
0353 }
0354
0355 void Clear() {
0356
0357 while( !_blockPtrs.Empty()) {
0358 Block* lastBlock = _blockPtrs.Pop();
0359 delete lastBlock;
0360 }
0361 _root = 0;
0362 _currentAllocs = 0;
0363 _nAllocs = 0;
0364 _maxAllocs = 0;
0365 _nUntracked = 0;
0366 }
0367
0368 virtual int ItemSize() const {
0369 return ITEM_SIZE;
0370 }
0371 int CurrentAllocs() const {
0372 return _currentAllocs;
0373 }
0374
0375 virtual void* Alloc() {
0376 if ( !_root ) {
0377
0378 Block* block = new Block;
0379 _blockPtrs.Push( block );
0380
0381 Item* blockItems = block->items;
0382 for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
0383 blockItems[i].next = &(blockItems[i + 1]);
0384 }
0385 blockItems[ITEMS_PER_BLOCK - 1].next = 0;
0386 _root = blockItems;
0387 }
0388 Item* const result = _root;
0389 TIXMLASSERT( result != 0 );
0390 _root = _root->next;
0391
0392 ++_currentAllocs;
0393 if ( _currentAllocs > _maxAllocs ) {
0394 _maxAllocs = _currentAllocs;
0395 }
0396 ++_nAllocs;
0397 ++_nUntracked;
0398 return result;
0399 }
0400
0401 virtual void Free( void* mem ) {
0402 if ( !mem ) {
0403 return;
0404 }
0405 --_currentAllocs;
0406 Item* item = static_cast<Item*>( mem );
0407 #ifdef TINYXML2_DEBUG
0408 memset( item, 0xfe, sizeof( *item ) );
0409 #endif
0410 item->next = _root;
0411 _root = item;
0412 }
0413 void Trace( const char* name ) {
0414 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
0415 name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
0416 ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
0417 }
0418
0419 void SetTracked() {
0420 --_nUntracked;
0421 }
0422
0423 int Untracked() const {
0424 return _nUntracked;
0425 }
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438 enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
0439
0440 private:
0441 MemPoolT( const MemPoolT& );
0442 void operator=( const MemPoolT& );
0443
0444 union Item {
0445 Item* next;
0446 char itemData[ITEM_SIZE];
0447 };
0448 struct Block {
0449 Item items[ITEMS_PER_BLOCK];
0450 };
0451 DynArray< Block*, 10 > _blockPtrs;
0452 Item* _root;
0453
0454 int _currentAllocs;
0455 int _nAllocs;
0456 int _maxAllocs;
0457 int _nUntracked;
0458 };
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481 class TINYXML2_LIB XMLVisitor
0482 {
0483 public:
0484 virtual ~XMLVisitor() {}
0485
0486
0487 virtual bool VisitEnter( const XMLDocument& ) {
0488 return true;
0489 }
0490
0491 virtual bool VisitExit( const XMLDocument& ) {
0492 return true;
0493 }
0494
0495
0496 virtual bool VisitEnter( const XMLElement& , const XMLAttribute* ) {
0497 return true;
0498 }
0499
0500 virtual bool VisitExit( const XMLElement& ) {
0501 return true;
0502 }
0503
0504
0505 virtual bool Visit( const XMLDeclaration& ) {
0506 return true;
0507 }
0508
0509 virtual bool Visit( const XMLText& ) {
0510 return true;
0511 }
0512
0513 virtual bool Visit( const XMLComment& ) {
0514 return true;
0515 }
0516
0517 virtual bool Visit( const XMLUnknown& ) {
0518 return true;
0519 }
0520 };
0521
0522
0523 enum XMLError {
0524 XML_SUCCESS = 0,
0525 XML_NO_ATTRIBUTE,
0526 XML_WRONG_ATTRIBUTE_TYPE,
0527 XML_ERROR_FILE_NOT_FOUND,
0528 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
0529 XML_ERROR_FILE_READ_ERROR,
0530 XML_ERROR_PARSING_ELEMENT,
0531 XML_ERROR_PARSING_ATTRIBUTE,
0532 XML_ERROR_PARSING_TEXT,
0533 XML_ERROR_PARSING_CDATA,
0534 XML_ERROR_PARSING_COMMENT,
0535 XML_ERROR_PARSING_DECLARATION,
0536 XML_ERROR_PARSING_UNKNOWN,
0537 XML_ERROR_EMPTY_DOCUMENT,
0538 XML_ERROR_MISMATCHED_ELEMENT,
0539 XML_ERROR_PARSING,
0540 XML_CAN_NOT_CONVERT_TEXT,
0541 XML_NO_TEXT_NODE,
0542 XML_ELEMENT_DEPTH_EXCEEDED,
0543
0544 XML_ERROR_COUNT
0545 };
0546
0547
0548
0549
0550
0551 class TINYXML2_LIB XMLUtil
0552 {
0553 public:
0554 static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
0555 TIXMLASSERT( p );
0556
0557 while( IsWhiteSpace(*p) ) {
0558 if (curLineNumPtr && *p == '\n') {
0559 ++(*curLineNumPtr);
0560 }
0561 ++p;
0562 }
0563 TIXMLASSERT( p );
0564 return p;
0565 }
0566 static char* SkipWhiteSpace( char* const p, int* curLineNumPtr ) {
0567 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
0568 }
0569
0570
0571
0572 static bool IsWhiteSpace( char p ) {
0573 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
0574 }
0575
0576 inline static bool IsNameStartChar( unsigned char ch ) {
0577 if ( ch >= 128 ) {
0578
0579 return true;
0580 }
0581 if ( isalpha( ch ) ) {
0582 return true;
0583 }
0584 return ch == ':' || ch == '_';
0585 }
0586
0587 inline static bool IsNameChar( unsigned char ch ) {
0588 return IsNameStartChar( ch )
0589 || isdigit( ch )
0590 || ch == '.'
0591 || ch == '-';
0592 }
0593
0594 inline static bool IsPrefixHex( const char* p) {
0595 p = SkipWhiteSpace(p, 0);
0596 return p && *p == '0' && ( *(p + 1) == 'x' || *(p + 1) == 'X');
0597 }
0598
0599 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
0600 if ( p == q ) {
0601 return true;
0602 }
0603 TIXMLASSERT( p );
0604 TIXMLASSERT( q );
0605 TIXMLASSERT( nChar >= 0 );
0606 return strncmp( p, q, nChar ) == 0;
0607 }
0608
0609 inline static bool IsUTF8Continuation( const char p ) {
0610 return ( p & 0x80 ) != 0;
0611 }
0612
0613 static const char* ReadBOM( const char* p, bool* hasBOM );
0614
0615
0616 static const char* GetCharacterRef( const char* p, char* value, int* length );
0617 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
0618
0619
0620 static void ToStr( int v, char* buffer, int bufferSize );
0621 static void ToStr( unsigned v, char* buffer, int bufferSize );
0622 static void ToStr( bool v, char* buffer, int bufferSize );
0623 static void ToStr( float v, char* buffer, int bufferSize );
0624 static void ToStr( double v, char* buffer, int bufferSize );
0625 static void ToStr(int64_t v, char* buffer, int bufferSize);
0626 static void ToStr(uint64_t v, char* buffer, int bufferSize);
0627
0628
0629 static bool ToInt( const char* str, int* value );
0630 static bool ToUnsigned( const char* str, unsigned* value );
0631 static bool ToBool( const char* str, bool* value );
0632 static bool ToFloat( const char* str, float* value );
0633 static bool ToDouble( const char* str, double* value );
0634 static bool ToInt64(const char* str, int64_t* value);
0635 static bool ToUnsigned64(const char* str, uint64_t* value);
0636
0637
0638
0639
0640
0641 static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
0642
0643 private:
0644 static const char* writeBoolTrue;
0645 static const char* writeBoolFalse;
0646 };
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671
0672
0673
0674 class TINYXML2_LIB XMLNode
0675 {
0676 friend class XMLDocument;
0677 friend class XMLElement;
0678 public:
0679
0680
0681 const XMLDocument* GetDocument() const {
0682 TIXMLASSERT( _document );
0683 return _document;
0684 }
0685
0686 XMLDocument* GetDocument() {
0687 TIXMLASSERT( _document );
0688 return _document;
0689 }
0690
0691
0692 virtual XMLElement* ToElement() {
0693 return 0;
0694 }
0695
0696 virtual XMLText* ToText() {
0697 return 0;
0698 }
0699
0700 virtual XMLComment* ToComment() {
0701 return 0;
0702 }
0703
0704 virtual XMLDocument* ToDocument() {
0705 return 0;
0706 }
0707
0708 virtual XMLDeclaration* ToDeclaration() {
0709 return 0;
0710 }
0711
0712 virtual XMLUnknown* ToUnknown() {
0713 return 0;
0714 }
0715
0716 virtual const XMLElement* ToElement() const {
0717 return 0;
0718 }
0719 virtual const XMLText* ToText() const {
0720 return 0;
0721 }
0722 virtual const XMLComment* ToComment() const {
0723 return 0;
0724 }
0725 virtual const XMLDocument* ToDocument() const {
0726 return 0;
0727 }
0728 virtual const XMLDeclaration* ToDeclaration() const {
0729 return 0;
0730 }
0731 virtual const XMLUnknown* ToUnknown() const {
0732 return 0;
0733 }
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744 const char* Value() const;
0745
0746
0747
0748
0749 void SetValue( const char* val, bool staticMem=false );
0750
0751
0752 int GetLineNum() const { return _parseLineNum; }
0753
0754
0755 const XMLNode* Parent() const {
0756 return _parent;
0757 }
0758
0759 XMLNode* Parent() {
0760 return _parent;
0761 }
0762
0763
0764 bool NoChildren() const {
0765 return !_firstChild;
0766 }
0767
0768
0769 const XMLNode* FirstChild() const {
0770 return _firstChild;
0771 }
0772
0773 XMLNode* FirstChild() {
0774 return _firstChild;
0775 }
0776
0777
0778
0779
0780 const XMLElement* FirstChildElement( const char* name = 0 ) const;
0781
0782 XMLElement* FirstChildElement( const char* name = 0 ) {
0783 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
0784 }
0785
0786
0787 const XMLNode* LastChild() const {
0788 return _lastChild;
0789 }
0790
0791 XMLNode* LastChild() {
0792 return _lastChild;
0793 }
0794
0795
0796
0797
0798 const XMLElement* LastChildElement( const char* name = 0 ) const;
0799
0800 XMLElement* LastChildElement( const char* name = 0 ) {
0801 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
0802 }
0803
0804
0805 const XMLNode* PreviousSibling() const {
0806 return _prev;
0807 }
0808
0809 XMLNode* PreviousSibling() {
0810 return _prev;
0811 }
0812
0813
0814 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
0815
0816 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
0817 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
0818 }
0819
0820
0821 const XMLNode* NextSibling() const {
0822 return _next;
0823 }
0824
0825 XMLNode* NextSibling() {
0826 return _next;
0827 }
0828
0829
0830 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
0831
0832 XMLElement* NextSiblingElement( const char* name = 0 ) {
0833 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
0834 }
0835
0836
0837
0838
0839
0840
0841
0842
0843 XMLNode* InsertEndChild( XMLNode* addThis );
0844
0845 XMLNode* LinkEndChild( XMLNode* addThis ) {
0846 return InsertEndChild( addThis );
0847 }
0848
0849
0850
0851
0852
0853
0854
0855 XMLNode* InsertFirstChild( XMLNode* addThis );
0856
0857
0858
0859
0860
0861
0862
0863
0864 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
0865
0866
0867
0868
0869 void DeleteChildren();
0870
0871
0872
0873
0874 void DeleteChild( XMLNode* node );
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900 XMLNode* DeepClone( XMLDocument* target ) const;
0901
0902
0903
0904
0905
0906
0907
0908 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929
0930
0931
0932 virtual bool Accept( XMLVisitor* visitor ) const = 0;
0933
0934
0935
0936
0937
0938
0939 void SetUserData(void* userData) { _userData = userData; }
0940
0941
0942
0943
0944
0945
0946 void* GetUserData() const { return _userData; }
0947
0948 protected:
0949 explicit XMLNode( XMLDocument* );
0950 virtual ~XMLNode();
0951
0952 virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
0953
0954 XMLDocument* _document;
0955 XMLNode* _parent;
0956 mutable StrPair _value;
0957 int _parseLineNum;
0958
0959 XMLNode* _firstChild;
0960 XMLNode* _lastChild;
0961
0962 XMLNode* _prev;
0963 XMLNode* _next;
0964
0965 void* _userData;
0966
0967 private:
0968 MemPool* _memPool;
0969 void Unlink( XMLNode* child );
0970 static void DeleteNode( XMLNode* node );
0971 void InsertChildPreamble( XMLNode* insertThis ) const;
0972 const XMLElement* ToElementWithName( const char* name ) const;
0973
0974 XMLNode( const XMLNode& );
0975 XMLNode& operator=( const XMLNode& );
0976 };
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991 class TINYXML2_LIB XMLText : public XMLNode
0992 {
0993 friend class XMLDocument;
0994 public:
0995 virtual bool Accept( XMLVisitor* visitor ) const;
0996
0997 virtual XMLText* ToText() {
0998 return this;
0999 }
1000 virtual const XMLText* ToText() const {
1001 return this;
1002 }
1003
1004
1005 void SetCData( bool isCData ) {
1006 _isCData = isCData;
1007 }
1008
1009 bool CData() const {
1010 return _isCData;
1011 }
1012
1013 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1014 virtual bool ShallowEqual( const XMLNode* compare ) const;
1015
1016 protected:
1017 explicit XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
1018 virtual ~XMLText() {}
1019
1020 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
1021
1022 private:
1023 bool _isCData;
1024
1025 XMLText( const XMLText& );
1026 XMLText& operator=( const XMLText& );
1027 };
1028
1029
1030
1031 class TINYXML2_LIB XMLComment : public XMLNode
1032 {
1033 friend class XMLDocument;
1034 public:
1035 virtual XMLComment* ToComment() {
1036 return this;
1037 }
1038 virtual const XMLComment* ToComment() const {
1039 return this;
1040 }
1041
1042 virtual bool Accept( XMLVisitor* visitor ) const;
1043
1044 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1045 virtual bool ShallowEqual( const XMLNode* compare ) const;
1046
1047 protected:
1048 explicit XMLComment( XMLDocument* doc );
1049 virtual ~XMLComment();
1050
1051 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
1052
1053 private:
1054 XMLComment( const XMLComment& );
1055 XMLComment& operator=( const XMLComment& );
1056 };
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070 class TINYXML2_LIB XMLDeclaration : public XMLNode
1071 {
1072 friend class XMLDocument;
1073 public:
1074 virtual XMLDeclaration* ToDeclaration() {
1075 return this;
1076 }
1077 virtual const XMLDeclaration* ToDeclaration() const {
1078 return this;
1079 }
1080
1081 virtual bool Accept( XMLVisitor* visitor ) const;
1082
1083 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1084 virtual bool ShallowEqual( const XMLNode* compare ) const;
1085
1086 protected:
1087 explicit XMLDeclaration( XMLDocument* doc );
1088 virtual ~XMLDeclaration();
1089
1090 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
1091
1092 private:
1093 XMLDeclaration( const XMLDeclaration& );
1094 XMLDeclaration& operator=( const XMLDeclaration& );
1095 };
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105 class TINYXML2_LIB XMLUnknown : public XMLNode
1106 {
1107 friend class XMLDocument;
1108 public:
1109 virtual XMLUnknown* ToUnknown() {
1110 return this;
1111 }
1112 virtual const XMLUnknown* ToUnknown() const {
1113 return this;
1114 }
1115
1116 virtual bool Accept( XMLVisitor* visitor ) const;
1117
1118 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1119 virtual bool ShallowEqual( const XMLNode* compare ) const;
1120
1121 protected:
1122 explicit XMLUnknown( XMLDocument* doc );
1123 virtual ~XMLUnknown();
1124
1125 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
1126
1127 private:
1128 XMLUnknown( const XMLUnknown& );
1129 XMLUnknown& operator=( const XMLUnknown& );
1130 };
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140 class TINYXML2_LIB XMLAttribute
1141 {
1142 friend class XMLElement;
1143 public:
1144
1145 const char* Name() const;
1146
1147
1148 const char* Value() const;
1149
1150
1151 int GetLineNum() const { return _parseLineNum; }
1152
1153
1154 const XMLAttribute* Next() const {
1155 return _next;
1156 }
1157
1158
1159
1160
1161
1162 int IntValue() const {
1163 int i = 0;
1164 QueryIntValue(&i);
1165 return i;
1166 }
1167
1168 int64_t Int64Value() const {
1169 int64_t i = 0;
1170 QueryInt64Value(&i);
1171 return i;
1172 }
1173
1174 uint64_t Unsigned64Value() const {
1175 uint64_t i = 0;
1176 QueryUnsigned64Value(&i);
1177 return i;
1178 }
1179
1180
1181 unsigned UnsignedValue() const {
1182 unsigned i=0;
1183 QueryUnsignedValue( &i );
1184 return i;
1185 }
1186
1187 bool BoolValue() const {
1188 bool b=false;
1189 QueryBoolValue( &b );
1190 return b;
1191 }
1192
1193 double DoubleValue() const {
1194 double d=0;
1195 QueryDoubleValue( &d );
1196 return d;
1197 }
1198
1199 float FloatValue() const {
1200 float f=0;
1201 QueryFloatValue( &f );
1202 return f;
1203 }
1204
1205
1206
1207
1208
1209 XMLError QueryIntValue( int* value ) const;
1210
1211 XMLError QueryUnsignedValue( unsigned int* value ) const;
1212
1213 XMLError QueryInt64Value(int64_t* value) const;
1214
1215 XMLError QueryUnsigned64Value(uint64_t* value) const;
1216
1217 XMLError QueryBoolValue( bool* value ) const;
1218
1219 XMLError QueryDoubleValue( double* value ) const;
1220
1221 XMLError QueryFloatValue( float* value ) const;
1222
1223
1224 void SetAttribute( const char* value );
1225
1226 void SetAttribute( int value );
1227
1228 void SetAttribute( unsigned value );
1229
1230 void SetAttribute(int64_t value);
1231
1232 void SetAttribute(uint64_t value);
1233
1234 void SetAttribute( bool value );
1235
1236 void SetAttribute( double value );
1237
1238 void SetAttribute( float value );
1239
1240 private:
1241 enum { BUF_SIZE = 200 };
1242
1243 XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
1244 virtual ~XMLAttribute() {}
1245
1246 XMLAttribute( const XMLAttribute& );
1247 void operator=( const XMLAttribute& );
1248 void SetName( const char* name );
1249
1250 char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
1251
1252 mutable StrPair _name;
1253 mutable StrPair _value;
1254 int _parseLineNum;
1255 XMLAttribute* _next;
1256 MemPool* _memPool;
1257 };
1258
1259
1260
1261
1262
1263
1264 class TINYXML2_LIB XMLElement : public XMLNode
1265 {
1266 friend class XMLDocument;
1267 public:
1268
1269 const char* Name() const {
1270 return Value();
1271 }
1272
1273 void SetName( const char* str, bool staticMem=false ) {
1274 SetValue( str, staticMem );
1275 }
1276
1277 virtual XMLElement* ToElement() {
1278 return this;
1279 }
1280 virtual const XMLElement* ToElement() const {
1281 return this;
1282 }
1283 virtual bool Accept( XMLVisitor* visitor ) const;
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308 const char* Attribute( const char* name, const char* value=0 ) const;
1309
1310
1311
1312
1313
1314
1315
1316 int IntAttribute(const char* name, int defaultValue = 0) const;
1317
1318 unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
1319
1320 int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
1321
1322 uint64_t Unsigned64Attribute(const char* name, uint64_t defaultValue = 0) const;
1323
1324 bool BoolAttribute(const char* name, bool defaultValue = false) const;
1325
1326 double DoubleAttribute(const char* name, double defaultValue = 0) const;
1327
1328 float FloatAttribute(const char* name, float defaultValue = 0) const;
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343 XMLError QueryIntAttribute( const char* name, int* value ) const {
1344 const XMLAttribute* a = FindAttribute( name );
1345 if ( !a ) {
1346 return XML_NO_ATTRIBUTE;
1347 }
1348 return a->QueryIntValue( value );
1349 }
1350
1351
1352 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
1353 const XMLAttribute* a = FindAttribute( name );
1354 if ( !a ) {
1355 return XML_NO_ATTRIBUTE;
1356 }
1357 return a->QueryUnsignedValue( value );
1358 }
1359
1360
1361 XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1362 const XMLAttribute* a = FindAttribute(name);
1363 if (!a) {
1364 return XML_NO_ATTRIBUTE;
1365 }
1366 return a->QueryInt64Value(value);
1367 }
1368
1369
1370 XMLError QueryUnsigned64Attribute(const char* name, uint64_t* value) const {
1371 const XMLAttribute* a = FindAttribute(name);
1372 if(!a) {
1373 return XML_NO_ATTRIBUTE;
1374 }
1375 return a->QueryUnsigned64Value(value);
1376 }
1377
1378
1379 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
1380 const XMLAttribute* a = FindAttribute( name );
1381 if ( !a ) {
1382 return XML_NO_ATTRIBUTE;
1383 }
1384 return a->QueryBoolValue( value );
1385 }
1386
1387 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
1388 const XMLAttribute* a = FindAttribute( name );
1389 if ( !a ) {
1390 return XML_NO_ATTRIBUTE;
1391 }
1392 return a->QueryDoubleValue( value );
1393 }
1394
1395 XMLError QueryFloatAttribute( const char* name, float* value ) const {
1396 const XMLAttribute* a = FindAttribute( name );
1397 if ( !a ) {
1398 return XML_NO_ATTRIBUTE;
1399 }
1400 return a->QueryFloatValue( value );
1401 }
1402
1403
1404 XMLError QueryStringAttribute(const char* name, const char** value) const {
1405 const XMLAttribute* a = FindAttribute(name);
1406 if (!a) {
1407 return XML_NO_ATTRIBUTE;
1408 }
1409 *value = a->Value();
1410 return XML_SUCCESS;
1411 }
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432 XMLError QueryAttribute( const char* name, int* value ) const {
1433 return QueryIntAttribute( name, value );
1434 }
1435
1436 XMLError QueryAttribute( const char* name, unsigned int* value ) const {
1437 return QueryUnsignedAttribute( name, value );
1438 }
1439
1440 XMLError QueryAttribute(const char* name, int64_t* value) const {
1441 return QueryInt64Attribute(name, value);
1442 }
1443
1444 XMLError QueryAttribute(const char* name, uint64_t* value) const {
1445 return QueryUnsigned64Attribute(name, value);
1446 }
1447
1448 XMLError QueryAttribute( const char* name, bool* value ) const {
1449 return QueryBoolAttribute( name, value );
1450 }
1451
1452 XMLError QueryAttribute( const char* name, double* value ) const {
1453 return QueryDoubleAttribute( name, value );
1454 }
1455
1456 XMLError QueryAttribute( const char* name, float* value ) const {
1457 return QueryFloatAttribute( name, value );
1458 }
1459
1460 XMLError QueryAttribute(const char* name, const char** value) const {
1461 return QueryStringAttribute(name, value);
1462 }
1463
1464
1465 void SetAttribute( const char* name, const char* value ) {
1466 XMLAttribute* a = FindOrCreateAttribute( name );
1467 a->SetAttribute( value );
1468 }
1469
1470 void SetAttribute( const char* name, int value ) {
1471 XMLAttribute* a = FindOrCreateAttribute( name );
1472 a->SetAttribute( value );
1473 }
1474
1475 void SetAttribute( const char* name, unsigned value ) {
1476 XMLAttribute* a = FindOrCreateAttribute( name );
1477 a->SetAttribute( value );
1478 }
1479
1480
1481 void SetAttribute(const char* name, int64_t value) {
1482 XMLAttribute* a = FindOrCreateAttribute(name);
1483 a->SetAttribute(value);
1484 }
1485
1486
1487 void SetAttribute(const char* name, uint64_t value) {
1488 XMLAttribute* a = FindOrCreateAttribute(name);
1489 a->SetAttribute(value);
1490 }
1491
1492
1493 void SetAttribute( const char* name, bool value ) {
1494 XMLAttribute* a = FindOrCreateAttribute( name );
1495 a->SetAttribute( value );
1496 }
1497
1498 void SetAttribute( const char* name, double value ) {
1499 XMLAttribute* a = FindOrCreateAttribute( name );
1500 a->SetAttribute( value );
1501 }
1502
1503 void SetAttribute( const char* name, float value ) {
1504 XMLAttribute* a = FindOrCreateAttribute( name );
1505 a->SetAttribute( value );
1506 }
1507
1508
1509
1510
1511 void DeleteAttribute( const char* name );
1512
1513
1514 const XMLAttribute* FirstAttribute() const {
1515 return _rootAttribute;
1516 }
1517
1518 const XMLAttribute* FindAttribute( const char* name ) const;
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548 const char* GetText() const;
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584 void SetText( const char* inText );
1585
1586 void SetText( int value );
1587
1588 void SetText( unsigned value );
1589
1590 void SetText(int64_t value);
1591
1592 void SetText(uint64_t value);
1593
1594 void SetText( bool value );
1595
1596 void SetText( double value );
1597
1598 void SetText( float value );
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626 XMLError QueryIntText( int* ival ) const;
1627
1628 XMLError QueryUnsignedText( unsigned* uval ) const;
1629
1630 XMLError QueryInt64Text(int64_t* uval) const;
1631
1632 XMLError QueryUnsigned64Text(uint64_t* uval) const;
1633
1634 XMLError QueryBoolText( bool* bval ) const;
1635
1636 XMLError QueryDoubleText( double* dval ) const;
1637
1638 XMLError QueryFloatText( float* fval ) const;
1639
1640 int IntText(int defaultValue = 0) const;
1641
1642
1643 unsigned UnsignedText(unsigned defaultValue = 0) const;
1644
1645 int64_t Int64Text(int64_t defaultValue = 0) const;
1646
1647 uint64_t Unsigned64Text(uint64_t defaultValue = 0) const;
1648
1649 bool BoolText(bool defaultValue = false) const;
1650
1651 double DoubleText(double defaultValue = 0) const;
1652
1653 float FloatText(float defaultValue = 0) const;
1654
1655
1656
1657
1658
1659 XMLElement* InsertNewChildElement(const char* name);
1660
1661 XMLComment* InsertNewComment(const char* comment);
1662
1663 XMLText* InsertNewText(const char* text);
1664
1665 XMLDeclaration* InsertNewDeclaration(const char* text);
1666
1667 XMLUnknown* InsertNewUnknown(const char* text);
1668
1669
1670
1671 enum ElementClosingType {
1672 OPEN,
1673 CLOSED,
1674 CLOSING
1675 };
1676 ElementClosingType ClosingType() const {
1677 return _closingType;
1678 }
1679 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1680 virtual bool ShallowEqual( const XMLNode* compare ) const;
1681
1682 protected:
1683 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
1684
1685 private:
1686 XMLElement( XMLDocument* doc );
1687 virtual ~XMLElement();
1688 XMLElement( const XMLElement& );
1689 void operator=( const XMLElement& );
1690
1691 XMLAttribute* FindOrCreateAttribute( const char* name );
1692 char* ParseAttributes( char* p, int* curLineNumPtr );
1693 static void DeleteAttribute( XMLAttribute* attribute );
1694 XMLAttribute* CreateAttribute();
1695
1696 enum { BUF_SIZE = 200 };
1697 ElementClosingType _closingType;
1698
1699
1700
1701 XMLAttribute* _rootAttribute;
1702 };
1703
1704
1705 enum Whitespace {
1706 PRESERVE_WHITESPACE,
1707 COLLAPSE_WHITESPACE
1708 };
1709
1710
1711
1712
1713
1714
1715
1716 class TINYXML2_LIB XMLDocument : public XMLNode
1717 {
1718 friend class XMLElement;
1719
1720
1721 friend class XMLNode;
1722 friend class XMLText;
1723 friend class XMLComment;
1724 friend class XMLDeclaration;
1725 friend class XMLUnknown;
1726 public:
1727
1728 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
1729 ~XMLDocument();
1730
1731 virtual XMLDocument* ToDocument() {
1732 TIXMLASSERT( this == _document );
1733 return this;
1734 }
1735 virtual const XMLDocument* ToDocument() const {
1736 TIXMLASSERT( this == _document );
1737 return this;
1738 }
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750 XMLError Parse( const char* xml, size_t nBytes=static_cast<size_t>(-1) );
1751
1752
1753
1754
1755
1756
1757 XMLError LoadFile( const char* filename );
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770 XMLError LoadFile( FILE* );
1771
1772
1773
1774
1775
1776
1777 XMLError SaveFile( const char* filename, bool compact = false );
1778
1779
1780
1781
1782
1783
1784
1785
1786 XMLError SaveFile( FILE* fp, bool compact = false );
1787
1788 bool ProcessEntities() const {
1789 return _processEntities;
1790 }
1791 Whitespace WhitespaceMode() const {
1792 return _whitespaceMode;
1793 }
1794
1795
1796
1797
1798 bool HasBOM() const {
1799 return _writeBOM;
1800 }
1801
1802
1803 void SetBOM( bool useBOM ) {
1804 _writeBOM = useBOM;
1805 }
1806
1807
1808
1809
1810 XMLElement* RootElement() {
1811 return FirstChildElement();
1812 }
1813 const XMLElement* RootElement() const {
1814 return FirstChildElement();
1815 }
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831 void Print( XMLPrinter* streamer=0 ) const;
1832 virtual bool Accept( XMLVisitor* visitor ) const;
1833
1834
1835
1836
1837
1838
1839 XMLElement* NewElement( const char* name );
1840
1841
1842
1843
1844
1845 XMLComment* NewComment( const char* comment );
1846
1847
1848
1849
1850
1851 XMLText* NewText( const char* text );
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863 XMLDeclaration* NewDeclaration( const char* text=0 );
1864
1865
1866
1867
1868
1869 XMLUnknown* NewUnknown( const char* text );
1870
1871
1872
1873
1874
1875 void DeleteNode( XMLNode* node );
1876
1877
1878 void ClearError();
1879
1880
1881 bool Error() const {
1882 return _errorID != XML_SUCCESS;
1883 }
1884
1885 XMLError ErrorID() const {
1886 return _errorID;
1887 }
1888 const char* ErrorName() const;
1889 static const char* ErrorIDToName(XMLError errorID);
1890
1891
1892
1893
1894 const char* ErrorStr() const;
1895
1896
1897 void PrintError() const;
1898
1899
1900 int ErrorLineNum() const
1901 {
1902 return _errorLineNum;
1903 }
1904
1905
1906 void Clear();
1907
1908
1909
1910
1911
1912
1913
1914
1915 void DeepCopy(XMLDocument* target) const;
1916
1917
1918 char* Identify( char* p, XMLNode** node );
1919
1920
1921 void MarkInUse(const XMLNode* const);
1922
1923 virtual XMLNode* ShallowClone( XMLDocument* ) const {
1924 return 0;
1925 }
1926 virtual bool ShallowEqual( const XMLNode* ) const {
1927 return false;
1928 }
1929
1930 private:
1931 XMLDocument( const XMLDocument& );
1932 void operator=( const XMLDocument& );
1933
1934 bool _writeBOM;
1935 bool _processEntities;
1936 XMLError _errorID;
1937 Whitespace _whitespaceMode;
1938 mutable StrPair _errorStr;
1939 int _errorLineNum;
1940 char* _charBuffer;
1941 int _parseCurLineNum;
1942 int _parsingDepth;
1943
1944
1945
1946
1947
1948
1949 DynArray<XMLNode*, 10> _unlinked;
1950
1951 MemPoolT< sizeof(XMLElement) > _elementPool;
1952 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1953 MemPoolT< sizeof(XMLText) > _textPool;
1954 MemPoolT< sizeof(XMLComment) > _commentPool;
1955
1956 static const char* _errorNames[XML_ERROR_COUNT];
1957
1958 void Parse();
1959
1960 void SetError( XMLError error, int lineNum, const char* format, ... );
1961
1962
1963
1964
1965 class DepthTracker {
1966 public:
1967 explicit DepthTracker(XMLDocument * document) {
1968 this->_document = document;
1969 document->PushDepth();
1970 }
1971 ~DepthTracker() {
1972 _document->PopDepth();
1973 }
1974 private:
1975 XMLDocument * _document;
1976 };
1977 void PushDepth();
1978 void PopDepth();
1979
1980 template<class NodeType, int PoolElementSize>
1981 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
1982 };
1983
1984 template<class NodeType, int PoolElementSize>
1985 inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1986 {
1987 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1988 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1989 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1990 TIXMLASSERT( returnNode );
1991 returnNode->_memPool = &pool;
1992
1993 _unlinked.Push(returnNode);
1994 return returnNode;
1995 }
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052 class TINYXML2_LIB XMLHandle
2053 {
2054 public:
2055
2056 explicit XMLHandle( XMLNode* node ) : _node( node ) {
2057 }
2058
2059 explicit XMLHandle( XMLNode& node ) : _node( &node ) {
2060 }
2061
2062 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
2063 }
2064
2065 XMLHandle& operator=( const XMLHandle& ref ) {
2066 _node = ref._node;
2067 return *this;
2068 }
2069
2070
2071 XMLHandle FirstChild() {
2072 return XMLHandle( _node ? _node->FirstChild() : 0 );
2073 }
2074
2075 XMLHandle FirstChildElement( const char* name = 0 ) {
2076 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
2077 }
2078
2079 XMLHandle LastChild() {
2080 return XMLHandle( _node ? _node->LastChild() : 0 );
2081 }
2082
2083 XMLHandle LastChildElement( const char* name = 0 ) {
2084 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
2085 }
2086
2087 XMLHandle PreviousSibling() {
2088 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
2089 }
2090
2091 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
2092 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
2093 }
2094
2095 XMLHandle NextSibling() {
2096 return XMLHandle( _node ? _node->NextSibling() : 0 );
2097 }
2098
2099 XMLHandle NextSiblingElement( const char* name = 0 ) {
2100 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
2101 }
2102
2103
2104 XMLNode* ToNode() {
2105 return _node;
2106 }
2107
2108 XMLElement* ToElement() {
2109 return ( _node ? _node->ToElement() : 0 );
2110 }
2111
2112 XMLText* ToText() {
2113 return ( _node ? _node->ToText() : 0 );
2114 }
2115
2116 XMLUnknown* ToUnknown() {
2117 return ( _node ? _node->ToUnknown() : 0 );
2118 }
2119
2120 XMLDeclaration* ToDeclaration() {
2121 return ( _node ? _node->ToDeclaration() : 0 );
2122 }
2123
2124 private:
2125 XMLNode* _node;
2126 };
2127
2128
2129
2130
2131
2132
2133 class TINYXML2_LIB XMLConstHandle
2134 {
2135 public:
2136 explicit XMLConstHandle( const XMLNode* node ) : _node( node ) {
2137 }
2138 explicit XMLConstHandle( const XMLNode& node ) : _node( &node ) {
2139 }
2140 XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
2141 }
2142
2143 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
2144 _node = ref._node;
2145 return *this;
2146 }
2147
2148 const XMLConstHandle FirstChild() const {
2149 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
2150 }
2151 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2152 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
2153 }
2154 const XMLConstHandle LastChild() const {
2155 return XMLConstHandle( _node ? _node->LastChild() : 0 );
2156 }
2157 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2158 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
2159 }
2160 const XMLConstHandle PreviousSibling() const {
2161 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
2162 }
2163 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2164 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
2165 }
2166 const XMLConstHandle NextSibling() const {
2167 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
2168 }
2169 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2170 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
2171 }
2172
2173
2174 const XMLNode* ToNode() const {
2175 return _node;
2176 }
2177 const XMLElement* ToElement() const {
2178 return ( _node ? _node->ToElement() : 0 );
2179 }
2180 const XMLText* ToText() const {
2181 return ( _node ? _node->ToText() : 0 );
2182 }
2183 const XMLUnknown* ToUnknown() const {
2184 return ( _node ? _node->ToUnknown() : 0 );
2185 }
2186 const XMLDeclaration* ToDeclaration() const {
2187 return ( _node ? _node->ToDeclaration() : 0 );
2188 }
2189
2190 private:
2191 const XMLNode* _node;
2192 };
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237 class TINYXML2_LIB XMLPrinter : public XMLVisitor
2238 {
2239 public:
2240
2241
2242
2243
2244
2245
2246 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
2247 virtual ~XMLPrinter() {}
2248
2249
2250 void PushHeader( bool writeBOM, bool writeDeclaration );
2251
2252
2253
2254 void OpenElement( const char* name, bool compactMode=false );
2255
2256 void PushAttribute( const char* name, const char* value );
2257 void PushAttribute( const char* name, int value );
2258 void PushAttribute( const char* name, unsigned value );
2259 void PushAttribute( const char* name, int64_t value );
2260 void PushAttribute( const char* name, uint64_t value );
2261 void PushAttribute( const char* name, bool value );
2262 void PushAttribute( const char* name, double value );
2263
2264 virtual void CloseElement( bool compactMode=false );
2265
2266
2267 void PushText( const char* text, bool cdata=false );
2268
2269 void PushText( int value );
2270
2271 void PushText( unsigned value );
2272
2273 void PushText( int64_t value );
2274
2275 void PushText( uint64_t value );
2276
2277 void PushText( bool value );
2278
2279 void PushText( float value );
2280
2281 void PushText( double value );
2282
2283
2284 void PushComment( const char* comment );
2285
2286 void PushDeclaration( const char* value );
2287 void PushUnknown( const char* value );
2288
2289 virtual bool VisitEnter( const XMLDocument& );
2290 virtual bool VisitExit( const XMLDocument& ) {
2291 return true;
2292 }
2293
2294 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2295 virtual bool VisitExit( const XMLElement& element );
2296
2297 virtual bool Visit( const XMLText& text );
2298 virtual bool Visit( const XMLComment& comment );
2299 virtual bool Visit( const XMLDeclaration& declaration );
2300 virtual bool Visit( const XMLUnknown& unknown );
2301
2302
2303
2304
2305
2306 const char* CStr() const {
2307 return _buffer.Mem();
2308 }
2309
2310
2311
2312
2313
2314 int CStrSize() const {
2315 return _buffer.Size();
2316 }
2317
2318
2319
2320
2321 void ClearBuffer( bool resetToFirstElement = true ) {
2322 _buffer.Clear();
2323 _buffer.Push(0);
2324 _firstElement = resetToFirstElement;
2325 }
2326
2327 protected:
2328 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
2329
2330
2331
2332
2333 virtual void PrintSpace( int depth );
2334 virtual void Print( const char* format, ... );
2335 virtual void Write( const char* data, size_t size );
2336 virtual void Putc( char ch );
2337
2338 inline void Write(const char* data) { Write(data, strlen(data)); }
2339
2340 void SealElementIfJustOpened();
2341 bool _elementJustOpened;
2342 DynArray< const char*, 10 > _stack;
2343
2344 private:
2345
2346
2347
2348
2349 void PrepareForNewNode( bool compactMode );
2350 void PrintString( const char*, bool restrictedEntitySet );
2351
2352 bool _firstElement;
2353 FILE* _fp;
2354 int _depth;
2355 int _textDepth;
2356 bool _processEntities;
2357 bool _compactMode;
2358
2359 enum {
2360 ENTITY_RANGE = 64,
2361 BUF_SIZE = 200
2362 };
2363 bool _entityFlag[ENTITY_RANGE];
2364 bool _restrictedEntityFlag[ENTITY_RANGE];
2365
2366 DynArray< char, 20 > _buffer;
2367
2368
2369 XMLPrinter( const XMLPrinter& );
2370 XMLPrinter& operator=( const XMLPrinter& );
2371 };
2372
2373
2374 }
2375
2376 #if defined(_MSC_VER)
2377 # pragma warning(pop)
2378 #endif
2379
2380 #endif