TinyXml: tinyxml.h Source File

Main Page | Class Hierarchy | Class List | File List | Class Members

# tinyxml.h

    00001 /*
    00002 www.sourceforge.net/projects/tinyxml
    00003 Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
    00004
    00005 This software is provided 'as-is', without any express or implied
    00006 warranty. In no event will the authors be held liable for any
    00007 damages arising from the use of this software.
    00008
    00009 Permission is granted to anyone to use this software for any
    00010 purpose, including commercial applications, and to alter it and
    00011 redistribute it freely, subject to the following restrictions:
    00012
    00013 1. The origin of this software must not be misrepresented; you must
    00014 not claim that you wrote the original software. If you use this
    00015 software in a product, an acknowledgment in the product documentation
    00016 would be appreciated but is not required.
    00017
    00018 2. Altered source versions must be plainly marked as such, and
    00019 must not be misrepresented as being the original software.
    00020
    00021 3. This notice may not be removed or altered from any source
    00022 distribution.
    00023 */
    00024
    00025
    00026 #ifndef TINYXML_INCLUDED
    00027 #define TINYXML_INCLUDED
    00028
    00029 #ifdef _MSC_VER
    00030 #pragma warning( disable : 4530 )
    00031 #pragma warning( disable : 4786 )
    00032 #endif
    00033
    00034 #include <ctype.h>
    00035 #include <stdio.h>
    00036 #include <stdlib.h>
    00037 #include <string.h>
    00038 #include <assert.h>
    00039
    00040 // Help out windows:
    00041 #if defined( _DEBUG ) && !defined( DEBUG )
    00042 #define DEBUG
    00043 #endif
    00044
    00045 #if defined( DEBUG ) && defined( _MSC_VER )
    00046 #include <windows.h>
    00047 #define TIXML_LOG OutputDebugString
    00048 #else
    00049 #define TIXML_LOG printf
    00050 #endif
    00051
    00052 #ifdef TIXML_USE_STL
    00053     #include <string>
    00054     #include <iostream>
    00055     #define TIXML_STRING    std::string
    00056     #define TIXML_ISTREAM   std::istream
    00057     #define TIXML_OSTREAM   std::ostream
    00058 #else
    00059     #include "tinystr.h"
    00060     #define TIXML_STRING    TiXmlString
    00061     #define TIXML_OSTREAM   TiXmlOutStream
    00062 #endif
    00063
    00064 class TiXmlDocument;
    00065 class TiXmlElement;
    00066 class TiXmlComment;
    00067 class TiXmlUnknown;
    00068 class TiXmlAttribute;
    00069 class TiXmlText;
    00070 class TiXmlDeclaration;
    00071 class TiXmlParsingData;
    00072
    00073 const int TIXML_MAJOR_VERSION = 2;
    00074 const int TIXML_MINOR_VERSION = 3;
    00075 const int TIXML_PATCH_VERSION = 4;
    00076
    00077 /*  Internal structure for tracking location of items
    00078     in the XML file.
    00079 */
    00080 struct TiXmlCursor
    00081 {
    00082     TiXmlCursor()       { Clear(); }
    00083     void Clear()        { row = col = -1; }
    00084
    00085     int row;    // 0 based.
    00086     int col;    // 0 based.
    00087 };
    00088
    00089
    00090 // Only used by Attribute::Query functions
    00091 enum
    00092 {
    00093     TIXML_SUCCESS,
    00094     TIXML_NO_ATTRIBUTE,
    00095     TIXML_WRONG_TYPE
    00096 };
    00097
    00098
    00099 // Used by the parsing routines.
    00100 enum TiXmlEncoding
    00101 {
    00102     TIXML_ENCODING_UNKNOWN,
    00103     TIXML_ENCODING_UTF8,
    00104     TIXML_ENCODING_LEGACY
    00105 };
    00106
    00107 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
    00108
    00131 class TiXmlBase
    00132 {
    00133     friend class TiXmlNode;
    00134     friend class TiXmlElement;
    00135     friend class TiXmlDocument;
    00136
    00137 public:
    00138     TiXmlBase() :   userData(0) {}
    00139     virtual ~TiXmlBase()                    {}
    00140
    00146     virtual void Print( FILE* cfile, int depth ) const = 0;
    00147
    00154     static void SetCondenseWhiteSpace( bool condense )      { condenseWhiteSpace = condense; }
    00155
    00157     static bool IsWhiteSpaceCondensed()                     { return condenseWhiteSpace; }
    00158
    00177     int Row() const         { return location.row + 1; }
    00178     int Column() const      { return location.col + 1; }
    00179
    00180     void  SetUserData( void* user )         { userData = user; }
    00181     void* GetUserData()                     { return userData; }
    00182
    00183     // Table that returs, for a given lead byte, the total number of bytes
    00184     // in the UTF-8 sequence.
    00185     static const int utf8ByteTable[256];
    00186
    00187     virtual const char* Parse(  const char* p,
    00188                                 TiXmlParsingData* data,
    00189                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
    00190
    00191     enum
    00192     {
    00193         TIXML_NO_ERROR = 0,
    00194         TIXML_ERROR,
    00195         TIXML_ERROR_OPENING_FILE,
    00196         TIXML_ERROR_OUT_OF_MEMORY,
    00197         TIXML_ERROR_PARSING_ELEMENT,
    00198         TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
    00199         TIXML_ERROR_READING_ELEMENT_VALUE,
    00200         TIXML_ERROR_READING_ATTRIBUTES,
    00201         TIXML_ERROR_PARSING_EMPTY,
    00202         TIXML_ERROR_READING_END_TAG,
    00203         TIXML_ERROR_PARSING_UNKNOWN,
    00204         TIXML_ERROR_PARSING_COMMENT,
    00205         TIXML_ERROR_PARSING_DECLARATION,
    00206         TIXML_ERROR_DOCUMENT_EMPTY,
    00207         TIXML_ERROR_EMBEDDED_NULL,
    00208
    00209         TIXML_ERROR_STRING_COUNT
    00210     };
    00211
    00212 protected:
    00213
    00214     // See STL_STRING_BUG
    00215     // Utility class to overcome a bug.
    00216     class StringToBuffer
    00217     {
    00218       public:
    00219         StringToBuffer( const TIXML_STRING& str );
    00220         ~StringToBuffer();
    00221         char* buffer;
    00222     };
    00223
    00224     static const char*  SkipWhiteSpace( const char*, TiXmlEncoding encoding );
    00225     inline static bool  IsWhiteSpace( char c )
    00226     {
    00227         return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
    00228     }
    00229
    00230     virtual void StreamOut (TIXML_OSTREAM *) const = 0;
    00231
    00232     #ifdef TIXML_USE_STL
    00233         static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
    00234         static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
    00235     #endif
    00236
    00237     /*  Reads an XML name into the string provided. Returns
    00238         a pointer just past the last character of the name,
    00239         or 0 if the function has an error.
    00240     */
    00241     static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
    00242
    00243     /*  Reads text. Returns a pointer past the given end tag.
    00244         Wickedly complex options, but it keeps the (sensitive) code in one place.
    00245     */
    00246     static const char* ReadText(    const char* in,             // where to start
    00247                                     TIXML_STRING* text,         // the string read
    00248                                     bool ignoreWhiteSpace,      // whether to keep the white space
    00249                                     const char* endTag,         // what ends this text
    00250                                     bool ignoreCase,            // whether to ignore case in the end tag
    00251                                     TiXmlEncoding encoding );   // the current encoding
    00252
    00253     // If an entity has been found, transform it into a character.
    00254     static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
    00255
    00256     // Get a character, while interpreting entities.
    00257     // The length can be from 0 to 4 bytes.
    00258     inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
    00259     {
    00260         assert( p );
    00261         if ( encoding == TIXML_ENCODING_UTF8 )
    00262         {
    00263             *length = utf8ByteTable[ *((unsigned char*)p) ];
    00264             assert( *length >= 0 && *length < 5 );
    00265         }
    00266         else
    00267         {
    00268             *length = 1;
    00269         }
    00270
    00271         if ( *length == 1 )
    00272         {
    00273             if ( *p == '&' )
    00274                 return GetEntity( p, _value, length, encoding );
    00275             *_value = *p;
    00276             return p+1;
    00277         }
    00278         else if ( *length )
    00279         {
    00280             strncpy( _value, p, *length );
    00281             return p + (*length);
    00282         }
    00283         else
    00284         {
    00285             // Not valid text.
    00286             return 0;
    00287         }
    00288     }
    00289
    00290     // Puts a string to a stream, expanding entities as it goes.
    00291     // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
    00292     static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
    00293
    00294     static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
    00295
    00296     // Return true if the next characters in the stream are any of the endTag sequences.
    00297     // Ignore case only works for english, and should only be relied on when comparing
    00298     // to Engilish words: StringEqual( p, "version", true ) is fine.
    00299     static bool StringEqual(    const char* p,
    00300                                 const char* endTag,
    00301                                 bool ignoreCase,
    00302                                 TiXmlEncoding encoding );
    00303
    00304     static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
    00305
    00306     TiXmlCursor location;
    00307
    00309     void*           userData;
    00310
    00311     // None of these methods are reliable for any language except English.
    00312     // Good for approximation, not great for accuracy.
    00313     static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
    00314     static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
    00315     inline static int ToLower( int v, TiXmlEncoding encoding )
    00316     {
    00317         if ( encoding == TIXML_ENCODING_UTF8 )
    00318         {
    00319             if ( v < 128 ) return tolower( v );
    00320             return v;
    00321         }
    00322         else
    00323         {
    00324             return tolower( v );
    00325         }
    00326     }
    00327     static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
    00328
    00329 private:
    00330     TiXmlBase( const TiXmlBase& );              // not implemented.
    00331     void operator=( const TiXmlBase& base );    // not allowed.
    00332
    00333     struct Entity
    00334     {
    00335         const char*     str;
    00336         unsigned int    strLength;
    00337         char            chr;
    00338     };
    00339     enum
    00340     {
    00341         NUM_ENTITY = 5,
    00342         MAX_ENTITY_LENGTH = 6
    00343
    00344     };
    00345     static Entity entity[ NUM_ENTITY ];
    00346     static bool condenseWhiteSpace;
    00347 };
    00348
    00349
    00356 class TiXmlNode : public TiXmlBase
    00357 {
    00358     friend class TiXmlDocument;
    00359     friend class TiXmlElement;
    00360
    00361 public:
    00362     #ifdef TIXML_USE_STL
    00363
    00367         friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
    00368
    00385         friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
    00386
    00388         friend std::string& operator<< (std::string& out, const TiXmlNode& base );
    00389
    00390     #else
    00391         // Used internally, not part of the public API.
    00392         friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
    00393     #endif
    00394
    00398     enum NodeType
    00399     {
    00400         DOCUMENT,
    00401         ELEMENT,
    00402         COMMENT,
    00403         UNKNOWN,
    00404         TEXT,
    00405         DECLARATION,
    00406         TYPECOUNT
    00407     };
    00408
    00409     virtual ~TiXmlNode();
    00410
    00423     const char * Value() const { return value.c_str (); }
    00424
    00434     void SetValue(const char * _value) { value = _value;}
    00435
    00436     #ifdef TIXML_USE_STL
    00437
    00438     void SetValue( const std::string& _value )
    00439     {
    00440         StringToBuffer buf( _value );
    00441         SetValue( buf.buffer ? buf.buffer : "" );
    00442     }
    00443     #endif
    00444
    00446     void Clear();
    00447
    00449     TiXmlNode* Parent()                         { return parent; }
    00450     const TiXmlNode* Parent() const             { return parent; }
    00451
    00452     const TiXmlNode* FirstChild()   const   { return firstChild; }
    00453     TiXmlNode* FirstChild()                 { return firstChild; }
    00454     const TiXmlNode* FirstChild( const char * value ) const;
    00455     TiXmlNode* FirstChild( const char * value );
    00456
    00457     const TiXmlNode* LastChild() const  { return lastChild; }
    00458     TiXmlNode* LastChild()  { return lastChild; }
    00459     const TiXmlNode* LastChild( const char * value ) const;
    00460     TiXmlNode* LastChild( const char * value );
    00461
    00462     #ifdef TIXML_USE_STL
    00463     const TiXmlNode* FirstChild( const std::string& _value ) const  {   return FirstChild (_value.c_str ());    }
    00464     TiXmlNode* FirstChild( const std::string& _value )              {   return FirstChild (_value.c_str ());    }
    00465     const TiXmlNode* LastChild( const std::string& _value ) const   {   return LastChild (_value.c_str ()); }
    00466     TiXmlNode* LastChild( const std::string& _value )               {   return LastChild (_value.c_str ()); }
    00467     #endif
    00468
    00485     const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
    00486     TiXmlNode* IterateChildren( TiXmlNode* previous );
    00487
    00489     const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
    00490     TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
    00491
    00492     #ifdef TIXML_USE_STL
    00493     const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {   return IterateChildren (_value.c_str (), previous); }
    00494     TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous); }
    00495     #endif
    00496
    00500     TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
    00501
    00502
    00512     TiXmlNode* LinkEndChild( TiXmlNode* addThis );
    00513
    00517     TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
    00518
    00522     TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
    00523
    00527     TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
    00528
    00530     bool RemoveChild( TiXmlNode* removeThis );
    00531
    00533     const TiXmlNode* PreviousSibling() const            { return prev; }
    00534     TiXmlNode* PreviousSibling()                        { return prev; }
    00535
    00537     const TiXmlNode* PreviousSibling( const char * ) const;
    00538     TiXmlNode* PreviousSibling( const char * );
    00539
    00540     #ifdef TIXML_USE_STL
    00541     const TiXmlNode* PreviousSibling( const std::string& _value ) const {   return PreviousSibling (_value.c_str ());   }
    00542     TiXmlNode* PreviousSibling( const std::string& _value )             {   return PreviousSibling (_value.c_str ());   }
    00543     const TiXmlNode* NextSibling( const std::string& _value) const      {   return NextSibling (_value.c_str ());   }
    00544     TiXmlNode* NextSibling( const std::string& _value)                  {   return NextSibling (_value.c_str ());   }
    00545     #endif
    00546
    00548     const TiXmlNode* NextSibling() const                { return next; }
    00549     TiXmlNode* NextSibling()                            { return next; }
    00550
    00552     const TiXmlNode* NextSibling( const char * ) const;
    00553     TiXmlNode* NextSibling( const char * );
    00554
    00559     const TiXmlElement* NextSiblingElement() const;
    00560     TiXmlElement* NextSiblingElement();
    00561
    00566     const TiXmlElement* NextSiblingElement( const char * ) const;
    00567     TiXmlElement* NextSiblingElement( const char * );
    00568
    00569     #ifdef TIXML_USE_STL
    00570     const TiXmlElement* NextSiblingElement( const std::string& _value) const    {   return NextSiblingElement (_value.c_str ());    }
    00571     TiXmlElement* NextSiblingElement( const std::string& _value)                {   return NextSiblingElement (_value.c_str ());    }
    00572     #endif
    00573
    00575     const TiXmlElement* FirstChildElement() const;
    00576     TiXmlElement* FirstChildElement();
    00577
    00579     const TiXmlElement* FirstChildElement( const char * value ) const;
    00580     TiXmlElement* FirstChildElement( const char * value );
    00581
    00582     #ifdef TIXML_USE_STL
    00583     const TiXmlElement* FirstChildElement( const std::string& _value ) const    {   return FirstChildElement (_value.c_str ()); }
    00584     TiXmlElement* FirstChildElement( const std::string& _value )                {   return FirstChildElement (_value.c_str ()); }
    00585     #endif
    00586
    00591     virtual int Type() const    { return type; }
    00592
    00596     const TiXmlDocument* GetDocument() const;
    00597     TiXmlDocument* GetDocument();
    00598
    00600     bool NoChildren() const                     { return !firstChild; }
    00601
    00602     const TiXmlDocument* ToDocument()   const       { return ( this && type == DOCUMENT ) ? (const TiXmlDocument*) this : 0; }
    00603     const TiXmlElement*  ToElement() const          { return ( this && type == ELEMENT  ) ? (const TiXmlElement*)  this : 0; }
    00604     const TiXmlComment*  ToComment() const          { return ( this && type == COMMENT  ) ? (const TiXmlComment*)  this : 0; }
    00605     const TiXmlUnknown*  ToUnknown() const          { return ( this && type == UNKNOWN  ) ? (const TiXmlUnknown*)  this : 0; }
    00606     const TiXmlText*       ToText()    const        { return ( this && type == TEXT     ) ? (const TiXmlText*)     this : 0; }
    00607     const TiXmlDeclaration* ToDeclaration() const   { return ( this && type == DECLARATION ) ? (const TiXmlDeclaration*) this : 0; }
    00608
    00609     TiXmlDocument* ToDocument()         { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; }
    00610     TiXmlElement*  ToElement()          { return ( this && type == ELEMENT  ) ? (TiXmlElement*)  this : 0; }
    00611     TiXmlComment*  ToComment()          { return ( this && type == COMMENT  ) ? (TiXmlComment*)  this : 0; }
    00612     TiXmlUnknown*  ToUnknown()          { return ( this && type == UNKNOWN  ) ? (TiXmlUnknown*)  this : 0; }
    00613     TiXmlText*     ToText()             { return ( this && type == TEXT     ) ? (TiXmlText*)     this : 0; }
    00614     TiXmlDeclaration* ToDeclaration()   { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; }
    00615
    00619     virtual TiXmlNode* Clone() const = 0;
    00620
    00621 protected:
    00622     TiXmlNode( NodeType _type );
    00623
    00624     // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
    00625     // and the assignment operator.
    00626     void CopyTo( TiXmlNode* target ) const;
    00627
    00628     #ifdef TIXML_USE_STL
    00629         // The real work of the input operator.
    00630         virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
    00631     #endif
    00632
    00633     // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
    00634     TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
    00635
    00636     // Internal Value function returning a TIXML_STRING
    00637     const TIXML_STRING& SValue() const  { return value ; }
    00638
    00639     TiXmlNode*      parent;
    00640     NodeType        type;
    00641
    00642     TiXmlNode*      firstChild;
    00643     TiXmlNode*      lastChild;
    00644
    00645     TIXML_STRING    value;
    00646
    00647     TiXmlNode*      prev;
    00648     TiXmlNode*      next;
    00649
    00650 private:
    00651     TiXmlNode( const TiXmlNode& );              // not implemented.
    00652     void operator=( const TiXmlNode& base );    // not allowed.
    00653 };
    00654
    00655
    00663 class TiXmlAttribute : public TiXmlBase
    00664 {
    00665     friend class TiXmlAttributeSet;
    00666
    00667 public:
    00669     TiXmlAttribute() : TiXmlBase()
    00670     {
    00671         document = 0;
    00672         prev = next = 0;
    00673     }
    00674
    00675     #ifdef TIXML_USE_STL
    00676
    00677     TiXmlAttribute( const std::string& _name, const std::string& _value )
    00678     {
    00679         name = _name;
    00680         value = _value;
    00681         document = 0;
    00682         prev = next = 0;
    00683     }
    00684     #endif
    00685
    00687     TiXmlAttribute( const char * _name, const char * _value )
    00688     {
    00689         name = _name;
    00690         value = _value;
    00691         document = 0;
    00692         prev = next = 0;
    00693     }
    00694
    00695     const char*     Name()  const       { return name.c_str (); }
    00696     const char*     Value() const       { return value.c_str (); }
    00697     const int       IntValue() const;
    00698     const double    DoubleValue() const;
    00699
    00709     int QueryIntValue( int* value ) const;
    00711     int QueryDoubleValue( double* value ) const;
    00712
    00713     void SetName( const char* _name )   { name = _name; }
    00714     void SetValue( const char* _value ) { value = _value; }
    00715
    00716     void SetIntValue( int value );
    00717     void SetDoubleValue( double value );
    00718
    00719     #ifdef TIXML_USE_STL
    00720
    00721     void SetName( const std::string& _name )
    00722     {
    00723         StringToBuffer buf( _name );
    00724         SetName ( buf.buffer ? buf.buffer : "error" );
    00725     }
    00727     void SetValue( const std::string& _value )
    00728     {
    00729         StringToBuffer buf( _value );
    00730         SetValue( buf.buffer ? buf.buffer : "error" );
    00731     }
    00732     #endif
    00733
    00735     const TiXmlAttribute* Next() const;
    00736     TiXmlAttribute* Next();
    00738     const TiXmlAttribute* Previous() const;
    00739     TiXmlAttribute* Previous();
    00740
    00741     bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
    00742     bool operator<( const TiXmlAttribute& rhs )  const { return name < rhs.name; }
    00743     bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
    00744
    00745     /*  Attribute parsing starts: first letter of the name
    00746                          returns: the next char after the value end quote
    00747     */
    00748     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
    00749
    00750     // Prints this Attribute to a FILE stream.
    00751     virtual void Print( FILE* cfile, int depth ) const;
    00752
    00753     virtual void StreamOut( TIXML_OSTREAM * out ) const;
    00754     // [internal use]
    00755     // Set the document pointer so the attribute can report errors.
    00756     void SetDocument( TiXmlDocument* doc )  { document = doc; }
    00757
    00758 private:
    00759     TiXmlAttribute( const TiXmlAttribute& );                // not implemented.
    00760     void operator=( const TiXmlAttribute& base );   // not allowed.
    00761
    00762     TiXmlDocument*  document;   // A pointer back to a document, for error reporting.
    00763     TIXML_STRING name;
    00764     TIXML_STRING value;
    00765     TiXmlAttribute* prev;
    00766     TiXmlAttribute* next;
    00767 };
    00768
    00769
    00770 /*  A class used to manage a group of attributes.
    00771     It is only used internally, both by the ELEMENT and the DECLARATION.
    00772
    00773     The set can be changed transparent to the Element and Declaration
    00774     classes that use it, but NOT transparent to the Attribute
    00775     which has to implement a next() and previous() method. Which makes
    00776     it a bit problematic and prevents the use of STL.
    00777
    00778     This version is implemented with circular lists because:
    00779         - I like circular lists
    00780         - it demonstrates some independence from the (typical) doubly linked list.
    00781 */
    00782 class TiXmlAttributeSet
    00783 {
    00784 public:
    00785     TiXmlAttributeSet();
    00786     ~TiXmlAttributeSet();
    00787
    00788     void Add( TiXmlAttribute* attribute );
    00789     void Remove( TiXmlAttribute* attribute );
    00790
    00791     const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
    00792     TiXmlAttribute* First()                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
    00793     const TiXmlAttribute* Last() const      { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
    00794     TiXmlAttribute* Last()                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
    00795
    00796     const TiXmlAttribute*   Find( const char * name ) const;
    00797     TiXmlAttribute* Find( const char * name );
    00798
    00799 private:
    00800     //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
    00801     //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
    00802     TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
    00803     void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
    00804
    00805     TiXmlAttribute sentinel;
    00806 };
    00807
    00808
    00813 class TiXmlElement : public TiXmlNode
    00814 {
    00815 public:
    00817     TiXmlElement (const char * in_value);
    00818
    00819     #ifdef TIXML_USE_STL
    00820
    00821     TiXmlElement( const std::string& _value );
    00822     #endif
    00823
    00824     TiXmlElement( const TiXmlElement& );
    00825
    00826     void operator=( const TiXmlElement& base );
    00827
    00828     virtual ~TiXmlElement();
    00829
    00833     const char* Attribute( const char* name ) const;
    00834
    00841     const char* Attribute( const char* name, int* i ) const;
    00842
    00849     const char* Attribute( const char* name, double* d ) const;
    00850
    00858     int QueryIntAttribute( const char* name, int* value ) const;
    00860     int QueryDoubleAttribute( const char* name, double* value ) const;
    00862     int QueryDoubleAttribute( const char* name, float* value ) const {
    00863         double d;
    00864         int result = QueryDoubleAttribute( name, &d );
    00865         *value = (float)d;
    00866         return result;
    00867     }
    00868
    00872     void SetAttribute( const char* name, const char * value );
    00873
    00874     #ifdef TIXML_USE_STL
    00875     const char* Attribute( const std::string& name ) const              { return Attribute( name.c_str() ); }
    00876     const char* Attribute( const std::string& name, int* i ) const      { return Attribute( name.c_str(), i ); }
    00877     const char* Attribute( const std::string& name, double* d ) const   { return Attribute( name.c_str(), d ); }
    00878     int QueryIntAttribute( const std::string& name, int* value ) const  { return QueryIntAttribute( name.c_str(), value ); }
    00879     int QueryDoubleAttribute( const std::string& name, double* value ) const { return QueryDoubleAttribute( name.c_str(), value ); }
    00880
    00882     void SetAttribute( const std::string& name, const std::string& _value )
    00883     {
    00884         StringToBuffer n( name );
    00885         StringToBuffer v( _value );
    00886         if ( n.buffer && v.buffer )
    00887             SetAttribute (n.buffer, v.buffer );
    00888     }
    00890     void SetAttribute( const std::string& name, int _value )
    00891     {
    00892         StringToBuffer n( name );
    00893         if ( n.buffer )
    00894             SetAttribute (n.buffer, _value);
    00895     }
    00896     #endif
    00897
    00901     void SetAttribute( const char * name, int value );
    00902
    00906     void SetDoubleAttribute( const char * name, double value );
    00907
    00910     void RemoveAttribute( const char * name );
    00911     #ifdef TIXML_USE_STL
    00912     void RemoveAttribute( const std::string& name ) {   RemoveAttribute (name.c_str ());    }
    00913     #endif
    00914
    00915     const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }
    00916     TiXmlAttribute* FirstAttribute()                { return attributeSet.First(); }
    00917     const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }
    00918     TiXmlAttribute* LastAttribute()                 { return attributeSet.Last(); }
    00919
    00921     virtual TiXmlNode* Clone() const;
    00922     // Print the Element to a FILE stream.
    00923     virtual void Print( FILE* cfile, int depth ) const;
    00924
    00925     /*  Attribtue parsing starts: next char past '<'
    00926                          returns: next char past '>'
    00927     */
    00928     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
    00929
    00930 protected:
    00931
    00932     void CopyTo( TiXmlElement* target ) const;
    00933     void ClearThis();   // like clear, but initializes 'this' object as well
    00934
    00935     // Used to be public [internal use]
    00936     #ifdef TIXML_USE_STL
    00937         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
    00938     #endif
    00939     virtual void StreamOut( TIXML_OSTREAM * out ) const;
    00940
    00941     /*  [internal use]
    00942         Reads the "value" of the element -- another element, or text.
    00943         This should terminate with the current end tag.
    00944     */
    00945     const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
    00946
    00947 private:
    00948
    00949     TiXmlAttributeSet attributeSet;
    00950 };
    00951
    00952
    00955 class TiXmlComment : public TiXmlNode
    00956 {
    00957 public:
    00959     TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
    00960     TiXmlComment( const TiXmlComment& );
    00961     void operator=( const TiXmlComment& base );
    00962
    00963     virtual ~TiXmlComment() {}
    00964
    00966     virtual TiXmlNode* Clone() const;
    00968     virtual void Print( FILE* cfile, int depth ) const;
    00969
    00970     /*  Attribtue parsing starts: at the ! of the !--
    00971                          returns: next char past '>'
    00972     */
    00973     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
    00974
    00975 protected:
    00976     void CopyTo( TiXmlComment* target ) const;
    00977
    00978     // used to be public
    00979     #ifdef TIXML_USE_STL
    00980         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
    00981     #endif
    00982     virtual void StreamOut( TIXML_OSTREAM * out ) const;
    00983
    00984 private:
    00985
    00986 };
    00987
    00988
    00991 class TiXmlText : public TiXmlNode
    00992 {
    00993     friend class TiXmlElement;
    00994 public:
    00996     TiXmlText (const char * initValue) : TiXmlNode (TiXmlNode::TEXT)
    00997     {
    00998         SetValue( initValue );
    00999     }
    01000     virtual ~TiXmlText() {}
    01001
    01002     #ifdef TIXML_USE_STL
    01003
    01004     TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
    01005     {
    01006         SetValue( initValue );
    01007     }
    01008     #endif
    01009
    01010     TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )   { copy.CopyTo( this ); }
    01011     void operator=( const TiXmlText& base )                             { base.CopyTo( this ); }
    01012
    01014     virtual void Print( FILE* cfile, int depth ) const;
    01015
    01016     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
    01017
    01018 protected :
    01020     virtual TiXmlNode* Clone() const;
    01021     void CopyTo( TiXmlText* target ) const;
    01022
    01023     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
    01024     bool Blank() const; // returns true if all white space and new lines
    01025     // [internal use]
    01026     #ifdef TIXML_USE_STL
    01027         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
    01028     #endif
    01029
    01030 private:
    01031 };
    01032
    01033
    01047 class TiXmlDeclaration : public TiXmlNode
    01048 {
    01049 public:
    01051     TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
    01052
    01053 #ifdef TIXML_USE_STL
    01054
    01055     TiXmlDeclaration(   const std::string& _version,
    01056                         const std::string& _encoding,
    01057                         const std::string& _standalone );
    01058 #endif
    01059
    01061     TiXmlDeclaration(   const char* _version,
    01062                         const char* _encoding,
    01063                         const char* _standalone );
    01064
    01065     TiXmlDeclaration( const TiXmlDeclaration& copy );
    01066     void operator=( const TiXmlDeclaration& copy );
    01067
    01068     virtual ~TiXmlDeclaration() {}
    01069
    01071     const char *Version() const         { return version.c_str (); }
    01073     const char *Encoding() const        { return encoding.c_str (); }
    01075     const char *Standalone() const      { return standalone.c_str (); }
    01076
    01078     virtual TiXmlNode* Clone() const;
    01080     virtual void Print( FILE* cfile, int depth ) const;
    01081
    01082     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
    01083
    01084 protected:
    01085     void CopyTo( TiXmlDeclaration* target ) const;
    01086     // used to be public
    01087     #ifdef TIXML_USE_STL
    01088         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
    01089     #endif
    01090     virtual void StreamOut ( TIXML_OSTREAM * out) const;
    01091
    01092 private:
    01093
    01094     TIXML_STRING version;
    01095     TIXML_STRING encoding;
    01096     TIXML_STRING standalone;
    01097 };
    01098
    01099
    01107 class TiXmlUnknown : public TiXmlNode
    01108 {
    01109 public:
    01110     TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )    {}
    01111     virtual ~TiXmlUnknown() {}
    01112
    01113     TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )      { copy.CopyTo( this ); }
    01114     void operator=( const TiXmlUnknown& copy )                                      { copy.CopyTo( this ); }
    01115
    01117     virtual TiXmlNode* Clone() const;
    01119     virtual void Print( FILE* cfile, int depth ) const;
    01120
    01121     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
    01122
    01123 protected:
    01124     void CopyTo( TiXmlUnknown* target ) const;
    01125
    01126     #ifdef TIXML_USE_STL
    01127         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
    01128     #endif
    01129     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
    01130
    01131 private:
    01132
    01133 };
    01134
    01135
    01140 class TiXmlDocument : public TiXmlNode
    01141 {
    01142 public:
    01144     TiXmlDocument();
    01146     TiXmlDocument( const char * documentName );
    01147
    01148     #ifdef TIXML_USE_STL
    01149
    01150     TiXmlDocument( const std::string& documentName );
    01151     #endif
    01152
    01153     TiXmlDocument( const TiXmlDocument& copy );
    01154     void operator=( const TiXmlDocument& copy );
    01155
    01156     virtual ~TiXmlDocument() {}
    01157
    01162     bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
    01164     bool SaveFile() const;
    01166     bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
    01168     bool SaveFile( const char * filename ) const;
    01169
    01170     #ifdef TIXML_USE_STL
    01171     bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )
    01172     {
    01173         StringToBuffer f( filename );
    01174         return ( f.buffer && LoadFile( f.buffer, encoding ));
    01175     }
    01176     bool SaveFile( const std::string& filename ) const
    01177     {
    01178         StringToBuffer f( filename );
    01179         return ( f.buffer && SaveFile( f.buffer ));
    01180     }
    01181     #endif
    01182
    01187     virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
    01188
    01193     const TiXmlElement* RootElement() const     { return FirstChildElement(); }
    01194     TiXmlElement* RootElement()                 { return FirstChildElement(); }
    01195
    01201     bool Error() const                      { return error; }
    01202
    01204     const char * ErrorDesc() const  { return errorDesc.c_str (); }
    01205
    01209     const int ErrorId() const               { return errorId; }
    01210
    01218     int ErrorRow()  { return errorLocation.row+1; }
    01219     int ErrorCol()  { return errorLocation.col+1; }
    01220
    01241     void SetTabSize( int _tabsize )     { tabsize = _tabsize; }
    01242
    01243     int TabSize() const { return tabsize; }
    01244
    01248     void ClearError()                       {   error = false;
    01249                                                 errorId = 0;
    01250                                                 errorDesc = "";
    01251                                                 errorLocation.row = errorLocation.col = 0;
    01252                                                 //errorLocation.last = 0;
    01253                                             }
    01254
    01256     void Print() const                      { Print( stdout, 0 ); }
    01257
    01259     virtual void Print( FILE* cfile, int depth = 0 ) const;
    01260     // [internal use]
    01261     void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
    01262
    01263 protected :
    01264     virtual void StreamOut ( TIXML_OSTREAM * out) const;
    01265     // [internal use]
    01266     virtual TiXmlNode* Clone() const;
    01267     #ifdef TIXML_USE_STL
    01268         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
    01269     #endif
    01270
    01271 private:
    01272     void CopyTo( TiXmlDocument* target ) const;
    01273
    01274     bool error;
    01275     int  errorId;
    01276     TIXML_STRING errorDesc;
    01277     int tabsize;
    01278     TiXmlCursor errorLocation;
    01279 };
    01280
    01281
    01362 class TiXmlHandle
    01363 {
    01364 public:
    01366     TiXmlHandle( TiXmlNode* node )                  { this->node = node; }
    01368     TiXmlHandle( const TiXmlHandle& ref )           { this->node = ref.node; }
    01369     TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
    01370
    01372     TiXmlHandle FirstChild() const;
    01374     TiXmlHandle FirstChild( const char * value ) const;
    01376     TiXmlHandle FirstChildElement() const;
    01378     TiXmlHandle FirstChildElement( const char * value ) const;
    01379
    01383     TiXmlHandle Child( const char* value, int index ) const;
    01387     TiXmlHandle Child( int index ) const;
    01392     TiXmlHandle ChildElement( const char* value, int index ) const;
    01397     TiXmlHandle ChildElement( int index ) const;
    01398
    01399     #ifdef TIXML_USE_STL
    01400     TiXmlHandle FirstChild( const std::string& _value ) const               { return FirstChild( _value.c_str() ); }
    01401     TiXmlHandle FirstChildElement( const std::string& _value ) const        { return FirstChildElement( _value.c_str() ); }
    01402
    01403     TiXmlHandle Child( const std::string& _value, int index ) const         { return Child( _value.c_str(), index ); }
    01404     TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
    01405     #endif
    01406
    01408     TiXmlNode* Node() const         { return node; }
    01410     TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
    01412     TiXmlText* Text() const         { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
    01414     TiXmlUnknown* Unknown() const           { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
    01415
    01416 private:
    01417     TiXmlNode* node;
    01418 };
    01419
    01420 #ifdef _MSC_VER
    01421 #pragma warning( default : 4530 )
    01422 #pragma warning( default : 4786 )
    01423 #endif
    01424
    01425 #endif
    01426