Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

tinyxml.h

Go to the documentation of this file.
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( push )
00031 #pragma warning( disable : 4530 )
00032 #pragma warning( disable : 4786 )
00033 #endif
00034 
00035 #ifndef USE_MMGR
00036 #include <ctype.h>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include <assert.h>
00041 #endif
00042 
00043 // Help out windows:
00044 #if defined( _DEBUG ) && !defined( DEBUG )
00045 #define DEBUG
00046 #endif
00047 
00048 #ifdef TIXML_USE_STL
00049     #include <string>
00050     #include <iostream>
00051     #define TIXML_STRING    std::string
00052     #define TIXML_ISTREAM   std::istream
00053     #define TIXML_OSTREAM   std::ostream
00054 #else
00055     #include "tinystr.h"
00056     #define TIXML_STRING    TiXmlString
00057     #define TIXML_OSTREAM   TiXmlOutStream
00058 #endif
00059 
00060 // Deprecated library function hell. Compilers want to use the
00061 // new safe versions. This probably doesn't fully address the problem,
00062 // but it gets closer. There are too many compilers for me to fully
00063 // test. If you get compilation troubles, undefine TIXML_SAFE
00064 
00065 #define TIXML_SAFE      // TinyXml isn't fully buffer overrun protected, safe code. This is work in progress.
00066 #ifdef TIXML_SAFE
00067     #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
00068         // Microsoft visual studio, version 2005 and higher.
00069         #define TIXML_SNPRINTF _snprintf_s
00070         #define TIXML_SNSCANF  _snscanf_s
00071     #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
00072         // Microsoft visual studio, version 6 and higher.
00073         //#pragma message( "Using _sn* functions." )
00074         #define TIXML_SNPRINTF _snprintf
00075         #define TIXML_SNSCANF  _snscanf
00076     #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00077         // GCC version 3 and higher.s
00078         //#warning( "Using sn* functions." )
00079         #define TIXML_SNPRINTF snprintf
00080         #define TIXML_SNSCANF  snscanf
00081     #endif
00082 #endif  
00083 
00084 class TiXmlDocument;
00085 class TiXmlElement;
00086 class TiXmlComment;
00087 class TiXmlUnknown;
00088 class TiXmlAttribute;
00089 class TiXmlText;
00090 class TiXmlDeclaration;
00091 class TiXmlParsingData;
00092 
00093 const int TIXML_MAJOR_VERSION = 2;
00094 const int TIXML_MINOR_VERSION = 4;
00095 const int TIXML_PATCH_VERSION = 3;
00096 
00097 /*  Internal structure for tracking location of items 
00098     in the XML file.
00099 */
00100 struct TiXmlCursor
00101 {
00102     TiXmlCursor()       { Clear(); }
00103     void Clear()        { row = col = -1; }
00104 
00105     int row;    // 0 based.
00106     int col;    // 0 based.
00107 };
00108 
00109 
00110 // Only used by Attribute::Query functions
00111 enum 
00112 { 
00113     TIXML_SUCCESS,
00114     TIXML_NO_ATTRIBUTE,
00115     TIXML_WRONG_TYPE
00116 };
00117 
00118 
00119 // Used by the parsing routines.
00120 enum TiXmlEncoding
00121 {
00122     TIXML_ENCODING_UNKNOWN,
00123     TIXML_ENCODING_UTF8,
00124     TIXML_ENCODING_LEGACY
00125 };
00126 
00127 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00128 
00151 class TiXmlBase
00152 {
00153     friend class TiXmlNode;
00154     friend class TiXmlElement;
00155     friend class TiXmlDocument;
00156 
00157 public:
00158     TiXmlBase() :   userData(0) {}
00159     virtual ~TiXmlBase()                    {}
00160 
00166     virtual void Print( FILE* cfile, int depth ) const = 0;
00167 
00174     static void SetCondenseWhiteSpace( bool condense )      { condenseWhiteSpace = condense; }
00175 
00177     static bool IsWhiteSpaceCondensed()                     { return condenseWhiteSpace; }
00178 
00197     int Row() const         { return location.row + 1; }
00198     int Column() const      { return location.col + 1; }    
00199 
00200     void  SetUserData( void* user )         { userData = user; }
00201     void* GetUserData()                     { return userData; }
00202 
00203     // Table that returs, for a given lead byte, the total number of bytes
00204     // in the UTF-8 sequence.
00205     static const int utf8ByteTable[256];
00206 
00207     virtual const char* Parse(  const char* p, 
00208                                 TiXmlParsingData* data, 
00209                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00210 
00211     enum
00212     {
00213         TIXML_NO_ERROR = 0,
00214         TIXML_ERROR,
00215         TIXML_ERROR_OPENING_FILE,
00216         TIXML_ERROR_OUT_OF_MEMORY,
00217         TIXML_ERROR_PARSING_ELEMENT,
00218         TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00219         TIXML_ERROR_READING_ELEMENT_VALUE,
00220         TIXML_ERROR_READING_ATTRIBUTES,
00221         TIXML_ERROR_PARSING_EMPTY,
00222         TIXML_ERROR_READING_END_TAG,
00223         TIXML_ERROR_PARSING_UNKNOWN,
00224         TIXML_ERROR_PARSING_COMMENT,
00225         TIXML_ERROR_PARSING_DECLARATION,
00226         TIXML_ERROR_DOCUMENT_EMPTY,
00227         TIXML_ERROR_EMBEDDED_NULL,
00228         TIXML_ERROR_PARSING_CDATA,
00229         TIXML_ERROR_DOCUMENT_TOP_ONLY,
00230 
00231         TIXML_ERROR_STRING_COUNT
00232     };
00233 
00234 protected:
00235 
00236     // See STL_STRING_BUG
00237     // Utility class to overcome a bug.
00238     class StringToBuffer
00239     {
00240       public:
00241         StringToBuffer( const TIXML_STRING& str );
00242         ~StringToBuffer();
00243         char* buffer;
00244     };
00245 
00246     static const char*  SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00247     inline static bool  IsWhiteSpace( char c )      
00248     { 
00249         return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
00250     }
00251     inline static bool  IsWhiteSpace( int c )
00252     {
00253         if ( c < 256 )
00254             return IsWhiteSpace( (char) c );
00255         return false;   // Again, only truly correct for English/Latin...but usually works.
00256     }
00257 
00258     virtual void StreamOut (TIXML_OSTREAM *) const = 0;
00259 
00260     #ifdef TIXML_USE_STL
00261         static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00262         static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00263     #endif
00264 
00265     /*  Reads an XML name into the string provided. Returns
00266         a pointer just past the last character of the name,
00267         or 0 if the function has an error.
00268     */
00269     static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00270 
00271     /*  Reads text. Returns a pointer past the given end tag.
00272         Wickedly complex options, but it keeps the (sensitive) code in one place.
00273     */
00274     static const char* ReadText(    const char* in,             // where to start
00275                                     TIXML_STRING* text,         // the string read
00276                                     bool ignoreWhiteSpace,      // whether to keep the white space
00277                                     const char* endTag,         // what ends this text
00278                                     bool ignoreCase,            // whether to ignore case in the end tag
00279                                     TiXmlEncoding encoding );   // the current encoding
00280 
00281     // If an entity has been found, transform it into a character.
00282     static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00283 
00284     // Get a character, while interpreting entities.
00285     // The length can be from 0 to 4 bytes.
00286     inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00287     {
00288         assert( p );
00289         if ( encoding == TIXML_ENCODING_UTF8 )
00290         {
00291             *length = utf8ByteTable[ *((unsigned char*)p) ];
00292             assert( *length >= 0 && *length < 5 );
00293         }
00294         else
00295         {
00296             *length = 1;
00297         }
00298 
00299         if ( *length == 1 )
00300         {
00301             if ( *p == '&' )
00302                 return GetEntity( p, _value, length, encoding );
00303             *_value = *p;
00304             return p+1;
00305         }
00306         else if ( *length )
00307         {
00308             //strncpy( _value, p, *length );    // lots of compilers don't like this function (unsafe),
00309                                                 // and the null terminator isn't needed
00310             for( int i=0; p[i] && i<*length; ++i ) {
00311                 _value[i] = p[i];
00312             }
00313             return p + (*length);
00314         }
00315         else
00316         {
00317             // Not valid text.
00318             return 0;
00319         }
00320     }
00321 
00322     // Puts a string to a stream, expanding entities as it goes.
00323     // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00324     static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00325 
00326     static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00327 
00328     // Return true if the next characters in the stream are any of the endTag sequences.
00329     // Ignore case only works for english, and should only be relied on when comparing
00330     // to English words: StringEqual( p, "version", true ) is fine.
00331     static bool StringEqual(    const char* p,
00332                                 const char* endTag,
00333                                 bool ignoreCase,
00334                                 TiXmlEncoding encoding );
00335 
00336     static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00337 
00338     TiXmlCursor location;
00339 
00341     void*           userData;
00342     
00343     // None of these methods are reliable for any language except English.
00344     // Good for approximation, not great for accuracy.
00345     static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00346     static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00347     inline static int ToLower( int v, TiXmlEncoding encoding )
00348     {
00349         if ( encoding == TIXML_ENCODING_UTF8 )
00350         {
00351             if ( v < 128 ) return tolower( v );
00352             return v;
00353         }
00354         else
00355         {
00356             return tolower( v );
00357         }
00358     }
00359     static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00360 
00361 private:
00362     TiXmlBase( const TiXmlBase& );              // not implemented.
00363     void operator=( const TiXmlBase& base );    // not allowed.
00364 
00365     struct Entity
00366     {
00367         const char*     str;
00368         unsigned int    strLength;
00369         char            chr;
00370     };
00371     enum
00372     {
00373         NUM_ENTITY = 5,
00374         MAX_ENTITY_LENGTH = 6
00375 
00376     };
00377     static Entity entity[ NUM_ENTITY ];
00378     static bool condenseWhiteSpace;
00379 };
00380 
00381 
00388 class TiXmlNode : public TiXmlBase
00389 {
00390     friend class TiXmlDocument;
00391     friend class TiXmlElement;
00392 
00393 public:
00394     #ifdef TIXML_USE_STL    
00395 
00399         friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00400 
00417         friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00418 
00420         friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00421 
00422     #else
00423         // Used internally, not part of the public API.
00424         friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00425     #endif
00426 
00430     enum NodeType
00431     {
00432         DOCUMENT,
00433         ELEMENT,
00434         COMMENT,
00435         UNKNOWN,
00436         TEXT,
00437         DECLARATION,
00438         TYPECOUNT
00439     };
00440 
00441     virtual ~TiXmlNode();
00442 
00455     const char *Value() const { return value.c_str (); }
00456 
00457     #ifdef TIXML_USE_STL
00458 
00462     const std::string& ValueStr() const { return value; }
00463     #endif
00464 
00474     void SetValue(const char * _value) { value = _value;}
00475 
00476     #ifdef TIXML_USE_STL
00477 
00478     void SetValue( const std::string& _value )  { value = _value; }
00479     #endif
00480 
00482     void Clear();
00483 
00485     TiXmlNode* Parent()                         { return parent; }
00486     const TiXmlNode* Parent() const             { return parent; }
00487 
00488     const TiXmlNode* FirstChild()   const   { return firstChild; }      
00489     TiXmlNode* FirstChild()                 { return firstChild; }
00490     const TiXmlNode* FirstChild( const char * value ) const;            
00491     TiXmlNode* FirstChild( const char * value );                        
00492 
00493     const TiXmlNode* LastChild() const  { return lastChild; }       
00494     TiXmlNode* LastChild()  { return lastChild; }
00495     const TiXmlNode* LastChild( const char * value ) const;         
00496     TiXmlNode* LastChild( const char * value ); 
00497 
00498     #ifdef TIXML_USE_STL
00499     const TiXmlNode* FirstChild( const std::string& _value ) const  {   return FirstChild (_value.c_str ());    }   
00500     TiXmlNode* FirstChild( const std::string& _value )              {   return FirstChild (_value.c_str ());    }   
00501     const TiXmlNode* LastChild( const std::string& _value ) const   {   return LastChild (_value.c_str ()); }   
00502     TiXmlNode* LastChild( const std::string& _value )               {   return LastChild (_value.c_str ()); }   
00503     #endif
00504 
00521     const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00522     TiXmlNode* IterateChildren( TiXmlNode* previous );
00523 
00525     const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00526     TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00527 
00528     #ifdef TIXML_USE_STL
00529     const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {   return IterateChildren (_value.c_str (), previous); }   
00530     TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous); }   
00531     #endif
00532 
00536     TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00537 
00538 
00548     TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00549 
00553     TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00554 
00558     TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00559 
00563     TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00564 
00566     bool RemoveChild( TiXmlNode* removeThis );
00567 
00569     const TiXmlNode* PreviousSibling() const            { return prev; }
00570     TiXmlNode* PreviousSibling()                        { return prev; }
00571 
00573     const TiXmlNode* PreviousSibling( const char * ) const;
00574     TiXmlNode* PreviousSibling( const char * );
00575 
00576     #ifdef TIXML_USE_STL
00577     const TiXmlNode* PreviousSibling( const std::string& _value ) const {   return PreviousSibling (_value.c_str ());   }   
00578     TiXmlNode* PreviousSibling( const std::string& _value )             {   return PreviousSibling (_value.c_str ());   }   
00579     const TiXmlNode* NextSibling( const std::string& _value) const      {   return NextSibling (_value.c_str ());   }   
00580     TiXmlNode* NextSibling( const std::string& _value)                  {   return NextSibling (_value.c_str ());   }   
00581     #endif
00582 
00584     const TiXmlNode* NextSibling() const                { return next; }
00585     TiXmlNode* NextSibling()                            { return next; }
00586 
00588     const TiXmlNode* NextSibling( const char * ) const;
00589     TiXmlNode* NextSibling( const char * );
00590 
00595     const TiXmlElement* NextSiblingElement() const;
00596     TiXmlElement* NextSiblingElement();
00597 
00602     const TiXmlElement* NextSiblingElement( const char * ) const;
00603     TiXmlElement* NextSiblingElement( const char * );
00604 
00605     #ifdef TIXML_USE_STL
00606     const TiXmlElement* NextSiblingElement( const std::string& _value) const    {   return NextSiblingElement (_value.c_str ());    }   
00607     TiXmlElement* NextSiblingElement( const std::string& _value)                {   return NextSiblingElement (_value.c_str ());    }   
00608     #endif
00609 
00611     const TiXmlElement* FirstChildElement() const;
00612     TiXmlElement* FirstChildElement();
00613 
00615     const TiXmlElement* FirstChildElement( const char * value ) const;
00616     TiXmlElement* FirstChildElement( const char * value );
00617 
00618     #ifdef TIXML_USE_STL
00619     const TiXmlElement* FirstChildElement( const std::string& _value ) const    {   return FirstChildElement (_value.c_str ()); }   
00620     TiXmlElement* FirstChildElement( const std::string& _value )                {   return FirstChildElement (_value.c_str ()); }   
00621     #endif
00622 
00627     int Type() const    { return type; }
00628 
00632     const TiXmlDocument* GetDocument() const;
00633     TiXmlDocument* GetDocument();
00634 
00636     bool NoChildren() const                     { return !firstChild; }
00637 
00638     virtual const TiXmlDocument*    ToDocument()    const { return 0; } 
00639     virtual const TiXmlElement*     ToElement()     const { return 0; } 
00640     virtual const TiXmlComment*     ToComment()     const { return 0; } 
00641     virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } 
00642     virtual const TiXmlText*        ToText()        const { return 0; } 
00643     virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } 
00644 
00645     virtual TiXmlDocument*          ToDocument()    { return 0; } 
00646     virtual TiXmlElement*           ToElement()     { return 0; } 
00647     virtual TiXmlComment*           ToComment()     { return 0; } 
00648     virtual TiXmlUnknown*           ToUnknown()     { return 0; } 
00649     virtual TiXmlText*              ToText()        { return 0; } 
00650     virtual TiXmlDeclaration*       ToDeclaration() { return 0; } 
00651 
00655     virtual TiXmlNode* Clone() const = 0;
00656 
00657 protected:
00658     TiXmlNode( NodeType _type );
00659 
00660     // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00661     // and the assignment operator.
00662     void CopyTo( TiXmlNode* target ) const;
00663 
00664     #ifdef TIXML_USE_STL
00665         // The real work of the input operator.
00666         virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00667     #endif
00668 
00669     // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00670     TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00671 
00672     TiXmlNode*      parent;
00673     NodeType        type;
00674 
00675     TiXmlNode*      firstChild;
00676     TiXmlNode*      lastChild;
00677 
00678     TIXML_STRING    value;
00679 
00680     TiXmlNode*      prev;
00681     TiXmlNode*      next;
00682 
00683 private:
00684     TiXmlNode( const TiXmlNode& );              // not implemented.
00685     void operator=( const TiXmlNode& base );    // not allowed.
00686 };
00687 
00688 
00696 class TiXmlAttribute : public TiXmlBase
00697 {
00698     friend class TiXmlAttributeSet;
00699 
00700 public:
00702     TiXmlAttribute() : TiXmlBase()
00703     {
00704         document = 0;
00705         prev = next = 0;
00706     }
00707 
00708     #ifdef TIXML_USE_STL
00709 
00710     TiXmlAttribute( const std::string& _name, const std::string& _value )
00711     {
00712         name = _name;
00713         value = _value;
00714         document = 0;
00715         prev = next = 0;
00716     }
00717     #endif
00718 
00720     TiXmlAttribute( const char * _name, const char * _value )
00721     {
00722         name = _name;
00723         value = _value;
00724         document = 0;
00725         prev = next = 0;
00726     }
00727 
00728     const char*     Name()  const       { return name.c_str(); }        
00729     const char*     Value() const       { return value.c_str(); }       
00730     #ifdef TIXML_USE_STL
00731     const std::string& ValueStr() const { return value; }               
00732     #endif
00733     int             IntValue() const;                                   
00734     double          DoubleValue() const;                                
00735 
00736     // Get the tinyxml string representation
00737     const TIXML_STRING& NameTStr() const { return name; }
00738 
00748     int QueryIntValue( int* _value ) const;
00750     int QueryDoubleValue( double* _value ) const;
00751 
00752     void SetName( const char* _name )   { name = _name; }               
00753     void SetValue( const char* _value ) { value = _value; }             
00754 
00755     void SetIntValue( int _value );                                     
00756     void SetDoubleValue( double _value );                               
00757 
00758     #ifdef TIXML_USE_STL
00759 
00760     void SetName( const std::string& _name )    { name = _name; }   
00762     void SetValue( const std::string& _value )  { value = _value; }
00763     #endif
00764 
00766     const TiXmlAttribute* Next() const;
00767     TiXmlAttribute* Next();
00769     const TiXmlAttribute* Previous() const;
00770     TiXmlAttribute* Previous();
00771 
00772     bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00773     bool operator<( const TiXmlAttribute& rhs )  const { return name < rhs.name; }
00774     bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00775 
00776     /*  Attribute parsing starts: first letter of the name
00777                          returns: the next char after the value end quote
00778     */
00779     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00780 
00781     // Prints this Attribute to a FILE stream.
00782     virtual void Print( FILE* cfile, int depth ) const;
00783 
00784     virtual void StreamOut( TIXML_OSTREAM * out ) const;
00785     // [internal use]
00786     // Set the document pointer so the attribute can report errors.
00787     void SetDocument( TiXmlDocument* doc )  { document = doc; }
00788 
00789 private:
00790     TiXmlAttribute( const TiXmlAttribute& );                // not implemented.
00791     void operator=( const TiXmlAttribute& base );   // not allowed.
00792 
00793     TiXmlDocument*  document;   // A pointer back to a document, for error reporting.
00794     TIXML_STRING name;
00795     TIXML_STRING value;
00796     TiXmlAttribute* prev;
00797     TiXmlAttribute* next;
00798 };
00799 
00800 
00801 /*  A class used to manage a group of attributes.
00802     It is only used internally, both by the ELEMENT and the DECLARATION.
00803     
00804     The set can be changed transparent to the Element and Declaration
00805     classes that use it, but NOT transparent to the Attribute
00806     which has to implement a next() and previous() method. Which makes
00807     it a bit problematic and prevents the use of STL.
00808 
00809     This version is implemented with circular lists because:
00810         - I like circular lists
00811         - it demonstrates some independence from the (typical) doubly linked list.
00812 */
00813 class TiXmlAttributeSet
00814 {
00815 public:
00816     TiXmlAttributeSet();
00817     ~TiXmlAttributeSet();
00818 
00819     void Add( TiXmlAttribute* attribute );
00820     void Remove( TiXmlAttribute* attribute );
00821 
00822     const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00823     TiXmlAttribute* First()                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00824     const TiXmlAttribute* Last() const      { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00825     TiXmlAttribute* Last()                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00826 
00827     const TiXmlAttribute*   Find( const TIXML_STRING& name ) const;
00828     TiXmlAttribute* Find( const TIXML_STRING& name );
00829 
00830 private:
00831     //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00832     //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00833     TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00834     void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
00835 
00836     TiXmlAttribute sentinel;
00837 };
00838 
00839 
00844 class TiXmlElement : public TiXmlNode
00845 {
00846 public:
00848     TiXmlElement (const char * in_value);
00849 
00850     #ifdef TIXML_USE_STL
00851 
00852     TiXmlElement( const std::string& _value );
00853     #endif
00854 
00855     TiXmlElement( const TiXmlElement& );
00856 
00857     void operator=( const TiXmlElement& base );
00858 
00859     virtual ~TiXmlElement();
00860 
00864     const char* Attribute( const char* name ) const;
00865 
00872     const char* Attribute( const char* name, int* i ) const;
00873 
00880     const char* Attribute( const char* name, double* d ) const;
00881 
00889     int QueryIntAttribute( const char* name, int* _value ) const;
00891     int QueryDoubleAttribute( const char* name, double* _value ) const;
00893     int QueryFloatAttribute( const char* name, float* _value ) const {
00894         double d;
00895         int result = QueryDoubleAttribute( name, &d );
00896         if ( result == TIXML_SUCCESS ) {
00897             *_value = (float)d;
00898         }
00899         return result;
00900     }
00901     #ifdef TIXML_USE_STL
00902 
00908     template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
00909     {
00910         const TiXmlAttribute* node = attributeSet.Find( name );
00911         if ( !node )
00912             return TIXML_NO_ATTRIBUTE;
00913 
00914         std::stringstream sstream( node->ValueStr() );
00915         sstream >> *outValue;
00916         if ( !sstream.fail() )
00917             return TIXML_SUCCESS;
00918         return TIXML_WRONG_TYPE;
00919     }
00920     #endif
00921 
00925     void SetAttribute( const char* name, const char * _value );
00926 
00927     #ifdef TIXML_USE_STL
00928     const char* Attribute( const std::string& name ) const              { return Attribute( name.c_str() ); }
00929     const char* Attribute( const std::string& name, int* i ) const      { return Attribute( name.c_str(), i ); }
00930     const char* Attribute( const std::string& name, double* d ) const   { return Attribute( name.c_str(), d ); }
00931     int QueryIntAttribute( const std::string& name, int* _value ) const { return QueryIntAttribute( name.c_str(), _value ); }
00932     int QueryDoubleAttribute( const std::string& name, double* _value ) const { return QueryDoubleAttribute( name.c_str(), _value ); }
00933 
00935     void SetAttribute( const std::string& name, const std::string& _value );
00937     void SetAttribute( const std::string& name, int _value );
00938     #endif
00939 
00943     void SetAttribute( const char * name, int value );
00944 
00948     void SetDoubleAttribute( const char * name, double value );
00949 
00952     void RemoveAttribute( const char * name );
00953     #ifdef TIXML_USE_STL
00954     void RemoveAttribute( const std::string& name ) {   RemoveAttribute (name.c_str ());    }   
00955     #endif
00956 
00957     const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }        
00958     TiXmlAttribute* FirstAttribute()                { return attributeSet.First(); }
00959     const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }     
00960     TiXmlAttribute* LastAttribute()                 { return attributeSet.Last(); }
00961 
00994     const char* GetText() const;
00995 
00997     virtual TiXmlNode* Clone() const;
00998     // Print the Element to a FILE stream.
00999     virtual void Print( FILE* cfile, int depth ) const;
01000 
01001     /*  Attribtue parsing starts: next char past '<'
01002                          returns: next char past '>'
01003     */
01004     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01005 
01006     virtual const TiXmlElement*     ToElement()     const { return this; } 
01007     virtual TiXmlElement*           ToElement()           { return this; } 
01008     
01009 protected:
01010 
01011     void CopyTo( TiXmlElement* target ) const;
01012     void ClearThis();   // like clear, but initializes 'this' object as well
01013 
01014     // Used to be public [internal use]
01015     #ifdef TIXML_USE_STL
01016         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01017     #endif
01018     virtual void StreamOut( TIXML_OSTREAM * out ) const;
01019 
01020     /*  [internal use]
01021         Reads the "value" of the element -- another element, or text.
01022         This should terminate with the current end tag.
01023     */
01024     const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01025 
01026 private:
01027 
01028     TiXmlAttributeSet attributeSet;
01029 };
01030 
01031 
01034 class TiXmlComment : public TiXmlNode
01035 {
01036 public:
01038     TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01039     TiXmlComment( const TiXmlComment& );
01040     void operator=( const TiXmlComment& base );
01041 
01042     virtual ~TiXmlComment() {}
01043 
01045     virtual TiXmlNode* Clone() const;
01047     virtual void Print( FILE* cfile, int depth ) const;
01048 
01049     /*  Attribtue parsing starts: at the ! of the !--
01050                          returns: next char past '>'
01051     */
01052     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01053 
01054     virtual const TiXmlComment*  ToComment() const { return this; } 
01055     virtual TiXmlComment*  ToComment() { return this; } 
01056 
01057 protected:
01058     void CopyTo( TiXmlComment* target ) const;
01059 
01060     // used to be public
01061     #ifdef TIXML_USE_STL
01062         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01063     #endif
01064     virtual void StreamOut( TIXML_OSTREAM * out ) const;
01065 
01066 private:
01067 
01068 };
01069 
01070 
01076 class TiXmlText : public TiXmlNode
01077 {
01078     friend class TiXmlElement;
01079 public:
01084     TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01085     {
01086         SetValue( initValue );
01087         cdata = false;
01088     }
01089     virtual ~TiXmlText() {}
01090 
01091     #ifdef TIXML_USE_STL
01092 
01093     TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01094     {
01095         SetValue( initValue );
01096         cdata = false;
01097     }
01098     #endif
01099 
01100     TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )   { copy.CopyTo( this ); }
01101     void operator=( const TiXmlText& base )                             { base.CopyTo( this ); }
01102 
01104     virtual void Print( FILE* cfile, int depth ) const;
01105 
01107     bool CDATA()                    { return cdata; }
01109     void SetCDATA( bool _cdata )    { cdata = _cdata; }
01110 
01111     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01112 
01113     virtual const TiXmlText* ToText() const { return this; } 
01114     virtual TiXmlText*       ToText()       { return this; } 
01115 
01116 protected :
01118     virtual TiXmlNode* Clone() const;
01119     void CopyTo( TiXmlText* target ) const;
01120 
01121     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01122     bool Blank() const; // returns true if all white space and new lines
01123     // [internal use]
01124     #ifdef TIXML_USE_STL
01125         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01126     #endif
01127 
01128 private:
01129     bool cdata;         // true if this should be input and output as a CDATA style text element
01130 };
01131 
01132 
01146 class TiXmlDeclaration : public TiXmlNode
01147 {
01148 public:
01150     TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01151 
01152 #ifdef TIXML_USE_STL
01153 
01154     TiXmlDeclaration(   const std::string& _version,
01155                         const std::string& _encoding,
01156                         const std::string& _standalone );
01157 #endif
01158 
01160     TiXmlDeclaration(   const char* _version,
01161                         const char* _encoding,
01162                         const char* _standalone );
01163 
01164     TiXmlDeclaration( const TiXmlDeclaration& copy );
01165     void operator=( const TiXmlDeclaration& copy );
01166 
01167     virtual ~TiXmlDeclaration() {}
01168 
01170     const char *Version() const         { return version.c_str (); }
01172     const char *Encoding() const        { return encoding.c_str (); }
01174     const char *Standalone() const      { return standalone.c_str (); }
01175 
01177     virtual TiXmlNode* Clone() const;
01179     virtual void Print( FILE* cfile, int depth ) const;
01180 
01181     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01182 
01183     virtual const TiXmlDeclaration* ToDeclaration() const { return this; } 
01184     virtual TiXmlDeclaration*       ToDeclaration()       { return this; } 
01185 
01186 protected:
01187     void CopyTo( TiXmlDeclaration* target ) const;
01188     // used to be public
01189     #ifdef TIXML_USE_STL
01190         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01191     #endif
01192     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01193 
01194 private:
01195 
01196     TIXML_STRING version;
01197     TIXML_STRING encoding;
01198     TIXML_STRING standalone;
01199 };
01200 
01201 
01209 class TiXmlUnknown : public TiXmlNode
01210 {
01211 public:
01212     TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )    {}
01213     virtual ~TiXmlUnknown() {}
01214 
01215     TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )      { copy.CopyTo( this ); }
01216     void operator=( const TiXmlUnknown& copy )                                      { copy.CopyTo( this ); }
01217 
01219     virtual TiXmlNode* Clone() const;
01221     virtual void Print( FILE* cfile, int depth ) const;
01222 
01223     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01224 
01225     virtual const TiXmlUnknown*     ToUnknown()     const { return this; } 
01226     virtual TiXmlUnknown*           ToUnknown()     { return this; } 
01227 
01228 protected:
01229     void CopyTo( TiXmlUnknown* target ) const;
01230 
01231     #ifdef TIXML_USE_STL
01232         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01233     #endif
01234     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01235 
01236 private:
01237 
01238 };
01239 
01240 
01245 class TiXmlDocument : public TiXmlNode
01246 {
01247 public:
01249     TiXmlDocument();
01251     TiXmlDocument( const char * documentName );
01252 
01253     #ifdef TIXML_USE_STL
01254 
01255     TiXmlDocument( const std::string& documentName );
01256     #endif
01257 
01258     TiXmlDocument( const TiXmlDocument& copy );
01259     void operator=( const TiXmlDocument& copy );
01260 
01261     virtual ~TiXmlDocument() {}
01262 
01267     bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01269     bool SaveFile() const;
01271     bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01273     bool SaveFile( const char * filename ) const;
01279     bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01281     bool SaveFile( FILE* ) const;
01282 
01283     #ifdef TIXML_USE_STL
01284     bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )           
01285     {
01286         StringToBuffer f( filename );
01287         return ( f.buffer && LoadFile( f.buffer, encoding ));
01288     }
01289     bool SaveFile( const std::string& filename ) const      
01290     {
01291         StringToBuffer f( filename );
01292         return ( f.buffer && SaveFile( f.buffer ));
01293     }
01294     #endif
01295 
01300     virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01301 
01306     const TiXmlElement* RootElement() const     { return FirstChildElement(); }
01307     TiXmlElement* RootElement()                 { return FirstChildElement(); }
01308 
01314     bool Error() const                      { return error; }
01315 
01317     const char * ErrorDesc() const  { return errorDesc.c_str (); }
01318 
01322     int ErrorId()   const               { return errorId; }
01323 
01331     int ErrorRow()  { return errorLocation.row+1; }
01332     int ErrorCol()  { return errorLocation.col+1; } 
01333 
01358     void SetTabSize( int _tabsize )     { tabsize = _tabsize; }
01359 
01360     int TabSize() const { return tabsize; }
01361 
01365     void ClearError()                       {   error = false; 
01366                                                 errorId = 0; 
01367                                                 errorDesc = ""; 
01368                                                 errorLocation.row = errorLocation.col = 0; 
01369                                                 //errorLocation.last = 0; 
01370                                             }
01371 
01373     void Print() const                      { Print( stdout, 0 ); }
01374 
01376     virtual void Print( FILE* cfile, int depth = 0 ) const;
01377     // [internal use]
01378     void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01379 
01380     virtual const TiXmlDocument*    ToDocument()    const { return this; } 
01381     virtual TiXmlDocument*          ToDocument()          { return this; } 
01382 
01383 protected :
01384     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01385     // [internal use]
01386     virtual TiXmlNode* Clone() const;
01387     #ifdef TIXML_USE_STL
01388         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01389     #endif
01390 
01391 private:
01392     void CopyTo( TiXmlDocument* target ) const;
01393 
01394     bool error;
01395     int  errorId;
01396     TIXML_STRING errorDesc;
01397     int tabsize;
01398     TiXmlCursor errorLocation;
01399     bool useMicrosoftBOM;       // the UTF-8 BOM were found when read. Note this, and try to write.
01400 };
01401 
01402 
01483 class TiXmlHandle
01484 {
01485 public:
01487     TiXmlHandle( TiXmlNode* _node )                 { this->node = _node; }
01489     TiXmlHandle( const TiXmlHandle& ref )           { this->node = ref.node; }
01490     TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01491 
01493     TiXmlHandle FirstChild() const;
01495     TiXmlHandle FirstChild( const char * value ) const;
01497     TiXmlHandle FirstChildElement() const;
01499     TiXmlHandle FirstChildElement( const char * value ) const;
01500 
01504     TiXmlHandle Child( const char* value, int index ) const;
01508     TiXmlHandle Child( int index ) const;
01513     TiXmlHandle ChildElement( const char* value, int index ) const;
01518     TiXmlHandle ChildElement( int index ) const;
01519 
01520     #ifdef TIXML_USE_STL
01521     TiXmlHandle FirstChild( const std::string& _value ) const               { return FirstChild( _value.c_str() ); }
01522     TiXmlHandle FirstChildElement( const std::string& _value ) const        { return FirstChildElement( _value.c_str() ); }
01523 
01524     TiXmlHandle Child( const std::string& _value, int index ) const         { return Child( _value.c_str(), index ); }
01525     TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01526     #endif
01527 
01529     TiXmlNode* Node() const         { return node; } 
01531     TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01533     TiXmlText* Text() const         { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01535     TiXmlUnknown* Unknown() const           { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01536 
01537 private:
01538     TiXmlNode* node;
01539 };
01540 
01541 #ifdef _MSC_VER
01542 #pragma warning( pop )
01543 #endif
01544 
01545 #endif
01546 

Generated for TinyXPath by doxygen SourceForge Logo