00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #define TIXML_USE_STL 1
00029
00030 #ifndef TINYXML_INCLUDED
00031 #define TINYXML_INCLUDED
00032
00033 #include <vos/vutil/vutildefs.hh>
00034
00035 #ifdef _MSC_VER
00036 #pragma warning( push )
00037 #pragma warning( disable : 4530 )
00038 #pragma warning( disable : 4786 )
00039 #endif
00040
00041 #include <ctype.h>
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <assert.h>
00046
00047
00048 #if defined( _DEBUG ) && !defined( DEBUG )
00049 #define DEBUG
00050 #endif
00051
00052 #if defined( DEBUG ) && defined( _MSC_VER )
00053 #include <windows.h>
00054 #define TIXML_LOG OutputDebugString
00055 #else
00056 #define TIXML_LOG printf
00057 #endif
00058
00059 #ifdef TIXML_USE_STL
00060 #include <string>
00061 #include <iostream>
00062 #define TIXML_STRING std::string
00063 #define TIXML_ISTREAM std::istream
00064 #define TIXML_OSTREAM std::ostream
00065 #else
00066 #define TIXML_STRING TiXmlString
00067 #define TIXML_OSTREAM TiXmlOutStream
00068 #endif
00069
00070
00071
00072
00073
00074
00075 #define TIXML_SAFE // TinyXml isn't fully buffer overrun protected, safe code. This is work in progress.
00076 #ifdef TIXML_SAFE
00077 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
00078
00079
00080 #define TIXML_SNPRINTF _snprintf
00081 #define TIXML_SNSCANF _snscanf
00082 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00083
00084
00085 #define TIXML_SNPRINTF snprintf
00086 #define TIXML_SNSCANF snscanf
00087 #endif
00088 #endif
00089
00090 class TiXmlDocument;
00091 class TiXmlElement;
00092 class TiXmlComment;
00093 class TiXmlUnknown;
00094 class TiXmlAttribute;
00095 class TiXmlText;
00096 class TiXmlDeclaration;
00097 class TiXmlParsingData;
00098
00099 const int TIXML_MAJOR_VERSION = 2;
00100 const int TIXML_MINOR_VERSION = 4;
00101 const int TIXML_PATCH_VERSION = 2;
00102
00103
00104
00105
00106 struct VUTIL_API TiXmlCursor
00107 {
00108 TiXmlCursor() { Clear(); }
00109 void Clear() { row = col = -1; }
00110
00111 int row;
00112 int col;
00113 };
00114
00115
00116
00117 enum
00118 {
00119 TIXML_SUCCESS,
00120 TIXML_NO_ATTRIBUTE,
00121 TIXML_WRONG_TYPE
00122 };
00123
00124
00125
00126 enum TiXmlEncoding
00127 {
00128 TIXML_ENCODING_UNKNOWN,
00129 TIXML_ENCODING_UTF8,
00130 TIXML_ENCODING_LEGACY
00131 };
00132
00133 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 class VUTIL_API TiXmlBase
00158 {
00159 friend class TiXmlNode;
00160 friend class TiXmlElement;
00161 friend class TiXmlDocument;
00162
00163 public:
00164 TiXmlBase() : userData(0) {}
00165 virtual ~TiXmlBase() {}
00166
00167
00168
00169
00170
00171
00172 virtual void Print( FILE* cfile, int depth ) const = 0;
00173
00174
00175
00176
00177
00178
00179
00180 static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
00181
00182
00183 static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 int Row() const { return location.row + 1; }
00204 int Column() const { return location.col + 1; }
00205
00206 void SetUserData( void* user ) { userData = user; }
00207 void* GetUserData() { return userData; }
00208
00209
00210
00211 static const int utf8ByteTable[256];
00212
00213 virtual const char* Parse( const char* p,
00214 TiXmlParsingData* data,
00215 TiXmlEncoding encoding ) = 0;
00216
00217 enum
00218 {
00219 TIXML_NO_ERROR = 0,
00220 TIXML_ERROR,
00221 TIXML_ERROR_OPENING_FILE,
00222 TIXML_ERROR_OUT_OF_MEMORY,
00223 TIXML_ERROR_PARSING_ELEMENT,
00224 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00225 TIXML_ERROR_READING_ELEMENT_VALUE,
00226 TIXML_ERROR_READING_ATTRIBUTES,
00227 TIXML_ERROR_PARSING_EMPTY,
00228 TIXML_ERROR_READING_END_TAG,
00229 TIXML_ERROR_PARSING_UNKNOWN,
00230 TIXML_ERROR_PARSING_COMMENT,
00231 TIXML_ERROR_PARSING_DECLARATION,
00232 TIXML_ERROR_DOCUMENT_EMPTY,
00233 TIXML_ERROR_EMBEDDED_NULL,
00234 TIXML_ERROR_PARSING_CDATA,
00235
00236 TIXML_ERROR_STRING_COUNT
00237 };
00238
00239 protected:
00240
00241
00242
00243 class VUTIL_API StringToBuffer
00244 {
00245 public:
00246 StringToBuffer( const TIXML_STRING& str );
00247 ~StringToBuffer();
00248 char* buffer;
00249 };
00250
00251 static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00252 inline static bool IsWhiteSpace( char c )
00253 {
00254 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
00255 }
00256
00257 virtual void StreamOut (TIXML_OSTREAM *, int depth) const = 0;
00258
00259 #ifdef TIXML_USE_STL
00260 static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00261 static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00262 #endif
00263
00264
00265
00266
00267
00268 static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00269
00270
00271
00272
00273 static const char* ReadText( const char* in,
00274 TIXML_STRING* text,
00275 bool ignoreWhiteSpace,
00276 const char* endTag,
00277 bool ignoreCase,
00278 TiXmlEncoding encoding );
00279
00280
00281 static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00282
00283
00284
00285 inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00286 {
00287 assert( p );
00288 if ( encoding == TIXML_ENCODING_UTF8 )
00289 {
00290 *length = utf8ByteTable[ *((unsigned char*)p) ];
00291 assert( *length >= 0 && *length < 5 );
00292 }
00293 else
00294 {
00295 *length = 1;
00296 }
00297
00298 if ( *length == 1 )
00299 {
00300 if ( *p == '&' )
00301 return GetEntity( p, _value, length, encoding );
00302 *_value = *p;
00303 return p+1;
00304 }
00305 else if ( *length )
00306 {
00307
00308
00309 for( int i=0; p[i] && i<*length; ++i ) {
00310 _value[i] = p[i];
00311 }
00312 return p + (*length);
00313 }
00314 else
00315 {
00316
00317 return 0;
00318 }
00319 }
00320
00321
00322
00323 static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00324
00325 static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00326
00327
00328
00329
00330 static bool StringEqual( const char* p,
00331 const char* endTag,
00332 bool ignoreCase,
00333 TiXmlEncoding encoding );
00334
00335 static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00336
00337 TiXmlCursor location;
00338
00339
00340 void* userData;
00341
00342
00343
00344 static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00345 static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00346 inline static int ToLower( int v, TiXmlEncoding encoding )
00347 {
00348 if ( encoding == TIXML_ENCODING_UTF8 )
00349 {
00350 if ( v < 128 ) return tolower( v );
00351 return v;
00352 }
00353 else
00354 {
00355 return tolower( v );
00356 }
00357 }
00358 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00359
00360 private:
00361 TiXmlBase( const TiXmlBase& );
00362 void operator=( const TiXmlBase& base );
00363
00364 struct Entity
00365 {
00366 const char* str;
00367 unsigned int strLength;
00368 char chr;
00369 };
00370 enum
00371 {
00372 NUM_ENTITY = 5,
00373 MAX_ENTITY_LENGTH = 6
00374
00375 };
00376 static Entity entity[ NUM_ENTITY ];
00377 static bool condenseWhiteSpace;
00378 };
00379
00380
00381
00382
00383
00384
00385
00386
00387 class VUTIL_API TiXmlNode : public TiXmlBase
00388 {
00389 friend class TiXmlDocument;
00390 friend class TiXmlElement;
00391
00392 public:
00393 #ifdef TIXML_USE_STL
00394
00395
00396
00397
00398 friend VUTIL_API std::istream& operator >> (std::istream& in, TiXmlNode& base);
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 VUTIL_API friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00417
00418
00419 VUTIL_API friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00420
00421 #else
00422
00423 friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00424 #endif
00425
00426
00427
00428
00429 enum NodeType
00430 {
00431 DOCUMENT,
00432 ELEMENT,
00433 COMMENT,
00434 UNKNOWN,
00435 TEXT,
00436 DECLARATION,
00437 TYPECOUNT
00438 };
00439
00440 virtual ~TiXmlNode();
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 const char *Value() const { return value.c_str (); }
00455
00456 #ifdef TIXML_USE_STL
00457
00458
00459
00460
00461 const std::string& ValueStr() const { return value; }
00462 #endif
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 void SetValue(const char * _value) { value = _value;}
00474
00475 #ifdef TIXML_USE_STL
00476
00477 void SetValue( const std::string& _value )
00478 {
00479 StringToBuffer buf( _value );
00480 SetValue( buf.buffer ? buf.buffer : "" );
00481 }
00482 #endif
00483
00484
00485 void Clear();
00486
00487
00488 TiXmlNode* Parent() { return parent; }
00489 const TiXmlNode* Parent() const { return parent; }
00490
00491 const TiXmlNode* FirstChild() const { return firstChild; }
00492 TiXmlNode* FirstChild() { return firstChild; }
00493 const TiXmlNode* FirstChild( const char * value ) const;
00494 TiXmlNode* FirstChild( const char * value );
00495
00496 const TiXmlNode* LastChild() const { return lastChild; }
00497 TiXmlNode* LastChild() { return lastChild; }
00498 const TiXmlNode* LastChild( const char * value ) const;
00499 TiXmlNode* LastChild( const char * value );
00500
00501 #ifdef TIXML_USE_STL
00502 const TiXmlNode* FirstChild( const std::string& _value ) const { return FirstChild (_value.c_str ()); }
00503 TiXmlNode* FirstChild( const std::string& _value ) { return FirstChild (_value.c_str ()); }
00504 const TiXmlNode* LastChild( const std::string& _value ) const { return LastChild (_value.c_str ()); }
00505 TiXmlNode* LastChild( const std::string& _value ) { return LastChild (_value.c_str ()); }
00506 #endif
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524 const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00525 TiXmlNode* IterateChildren( TiXmlNode* previous );
00526
00527
00528 const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00529 TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00530
00531 #ifdef TIXML_USE_STL
00532 const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const { return IterateChildren (_value.c_str (), previous); }
00533 TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); }
00534 #endif
00535
00536
00537
00538
00539 TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551 TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00552
00553
00554
00555
00556 TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00557
00558
00559
00560
00561 TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
00562
00563
00564
00565
00566 TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00567
00568
00569 bool RemoveChild( TiXmlNode* removeThis );
00570
00571
00572 const TiXmlNode* PreviousSibling() const { return prev; }
00573 TiXmlNode* PreviousSibling() { return prev; }
00574
00575
00576 const TiXmlNode* PreviousSibling( const char * ) const;
00577 TiXmlNode* PreviousSibling( const char * );
00578
00579 #ifdef TIXML_USE_STL
00580 const TiXmlNode* PreviousSibling( const std::string& _value ) const { return PreviousSibling (_value.c_str ()); }
00581 TiXmlNode* PreviousSibling( const std::string& _value ) { return PreviousSibling (_value.c_str ()); }
00582 const TiXmlNode* NextSibling( const std::string& _value) const { return NextSibling (_value.c_str ()); }
00583 TiXmlNode* NextSibling( const std::string& _value) { return NextSibling (_value.c_str ()); }
00584 #endif
00585
00586
00587 const TiXmlNode* NextSibling() const { return next; }
00588 TiXmlNode* NextSibling() { return next; }
00589
00590
00591 const TiXmlNode* NextSibling( const char * ) const;
00592 TiXmlNode* NextSibling( const char * );
00593
00594
00595
00596
00597
00598 const TiXmlElement* NextSiblingElement() const;
00599 TiXmlElement* NextSiblingElement();
00600
00601
00602
00603
00604
00605 const TiXmlElement* NextSiblingElement( const char * ) const;
00606 TiXmlElement* NextSiblingElement( const char * );
00607
00608 #ifdef TIXML_USE_STL
00609 const TiXmlElement* NextSiblingElement( const std::string& _value) const { return NextSiblingElement (_value.c_str ()); }
00610 TiXmlElement* NextSiblingElement( const std::string& _value) { return NextSiblingElement (_value.c_str ()); }
00611 #endif
00612
00613
00614 const TiXmlElement* FirstChildElement() const;
00615 TiXmlElement* FirstChildElement();
00616
00617
00618 const TiXmlElement* FirstChildElement( const char * value ) const;
00619 TiXmlElement* FirstChildElement( const char * value );
00620
00621 #ifdef TIXML_USE_STL
00622 const TiXmlElement* FirstChildElement( const std::string& _value ) const { return FirstChildElement (_value.c_str ()); }
00623 TiXmlElement* FirstChildElement( const std::string& _value ) { return FirstChildElement (_value.c_str ()); }
00624 #endif
00625
00626
00627
00628
00629
00630 int Type() const { return type; }
00631
00632
00633
00634
00635 const TiXmlDocument* GetDocument() const;
00636 TiXmlDocument* GetDocument();
00637
00638
00639 bool NoChildren() const { return !firstChild; }
00640
00641 const TiXmlDocument* ToDocument() const { return ( this && type == DOCUMENT ) ? (const TiXmlDocument*) this : 0; }
00642 const TiXmlElement* ToElement() const { return ( this && type == ELEMENT ) ? (const TiXmlElement*) this : 0; }
00643 const TiXmlComment* ToComment() const { return ( this && type == COMMENT ) ? (const TiXmlComment*) this : 0; }
00644 const TiXmlUnknown* ToUnknown() const { return ( this && type == UNKNOWN ) ? (const TiXmlUnknown*) this : 0; }
00645 const TiXmlText* ToText() const { return ( this && type == TEXT ) ? (const TiXmlText*) this : 0; }
00646 const TiXmlDeclaration* ToDeclaration() const { return ( this && type == DECLARATION ) ? (const TiXmlDeclaration*) this : 0; }
00647
00648 TiXmlDocument* ToDocument() { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; }
00649 TiXmlElement* ToElement() { return ( this && type == ELEMENT ) ? (TiXmlElement*) this : 0; }
00650 TiXmlComment* ToComment() { return ( this && type == COMMENT ) ? (TiXmlComment*) this : 0; }
00651 TiXmlUnknown* ToUnknown() { return ( this && type == UNKNOWN ) ? (TiXmlUnknown*) this : 0; }
00652 TiXmlText* ToText() { return ( this && type == TEXT ) ? (TiXmlText*) this : 0; }
00653 TiXmlDeclaration* ToDeclaration() { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; }
00654
00655
00656
00657
00658 virtual TiXmlNode* Clone() const = 0;
00659
00660 protected:
00661 TiXmlNode( NodeType _type );
00662
00663
00664
00665 void CopyTo( TiXmlNode* target ) const;
00666
00667 #ifdef TIXML_USE_STL
00668
00669 virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00670 #endif
00671
00672
00673 TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00674
00675 TiXmlNode* parent;
00676 NodeType type;
00677
00678 TiXmlNode* firstChild;
00679 TiXmlNode* lastChild;
00680
00681 TIXML_STRING value;
00682
00683 TiXmlNode* prev;
00684 TiXmlNode* next;
00685
00686 private:
00687 TiXmlNode( const TiXmlNode& );
00688 void operator=( const TiXmlNode& base );
00689 };
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699 class VUTIL_API TiXmlAttribute : public TiXmlBase
00700 {
00701 friend class TiXmlAttributeSet;
00702
00703 public:
00704
00705 TiXmlAttribute() : TiXmlBase()
00706 {
00707 document = 0;
00708 prev = next = 0;
00709 }
00710
00711 #ifdef TIXML_USE_STL
00712
00713 TiXmlAttribute( const std::string& _name, const std::string& _value )
00714 {
00715 name = _name;
00716 value = _value;
00717 document = 0;
00718 prev = next = 0;
00719 }
00720 #endif
00721
00722
00723 TiXmlAttribute( const char * _name, const char * _value )
00724 {
00725 name = _name;
00726 value = _value;
00727 document = 0;
00728 prev = next = 0;
00729 }
00730
00731 const char* Name() const { return name.c_str (); }
00732 const char* Value() const { return value.c_str (); }
00733 int IntValue() const;
00734 double DoubleValue() const;
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745 int QueryIntValue( int* _value ) const;
00746
00747 int QueryDoubleValue( double* _value ) const;
00748
00749 void SetName( const char* _name ) { name = _name; }
00750 void SetValue( const char* _value ) { value = _value; }
00751
00752 void SetIntValue( int _value );
00753 void SetDoubleValue( double _value );
00754
00755 #ifdef TIXML_USE_STL
00756
00757 void SetName( const std::string& _name )
00758 {
00759 StringToBuffer buf( _name );
00760 SetName ( buf.buffer ? buf.buffer : "error" );
00761 }
00762
00763 void SetValue( const std::string& _value )
00764 {
00765 StringToBuffer buf( _value );
00766 SetValue( buf.buffer ? buf.buffer : "error" );
00767 }
00768 #endif
00769
00770
00771 const TiXmlAttribute* Next() const;
00772 TiXmlAttribute* Next();
00773
00774 const TiXmlAttribute* Previous() const;
00775 TiXmlAttribute* Previous();
00776
00777 bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00778 bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
00779 bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
00780
00781
00782
00783
00784 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00785
00786
00787 virtual void Print( FILE* cfile, int depth ) const;
00788
00789 virtual