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( 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_TICPP
00049     #ifndef TIXML_USE_STL
00050         #define TIXML_USE_STL
00051     #endif
00052 #endif
00053 
00054 #ifdef TIXML_USE_STL
00055     #include <string>
00056     #include <iostream>
00057     #include <sstream>
00058     #define TIXML_STRING    std::string
00059     #define TIXML_ISTREAM   std::istream
00060     #define TIXML_OSTREAM   std::ostream
00061     #define TIXML_ENDL      std::endl
00062 #else
00063     #include "tinystr.h"
00064     #define TIXML_STRING    TiXmlString
00065     #define TIXML_OSTREAM   TiXmlOutStream
00066     #define TIXML_ENDL      "\n"
00067 #endif
00068 
00069 // Deprecated library function hell. Compilers want to use the
00070 // new safe versions. This probably doesn't fully address the problem,
00071 // but it gets closer. There are too many compilers for me to fully
00072 // test. If you get compilation troubles, undefine TIXML_SAFE
00073 
00074 #define TIXML_SAFE      // TinyXml isn't fully buffer overrun protected, safe code. This is work in progress.
00075 #ifdef TIXML_SAFE
00076     #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
00077         // Microsoft visual studio, version 2005 and higher.
00078         #define TIXML_SNPRINTF _snprintf_s
00079         #define TIXML_SNSCANF  _snscanf_s
00080     #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
00081         // Microsoft visual studio, version 6 and higher.
00082         //#pragma message( "Using _sn* functions." )
00083         #define TIXML_SNPRINTF _snprintf
00084         #define TIXML_SNSCANF  _snscanf
00085     #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00086         // GCC version 3 and higher.s
00087         //#warning( "Using sn* functions." )
00088         #define TIXML_SNPRINTF snprintf
00089         #define TIXML_SNSCANF  snscanf
00090     #endif
00091 #endif
00092 
00093 class TiXmlDocument;
00094 class TiXmlElement;
00095 class TiXmlComment;
00096 class TiXmlUnknown;
00097 class TiXmlAttribute;
00098 class TiXmlText;
00099 class TiXmlDeclaration;
00100 class TiXmlParsingData;
00101 
00102 const int TIXML_MAJOR_VERSION = 2;
00103 const int TIXML_MINOR_VERSION = 4;
00104 const int TIXML_PATCH_VERSION = 3;
00105 
00106 /*  Internal structure for tracking location of items
00107     in the XML file.
00108 */
00109 struct TiXmlCursor
00110 {
00111     TiXmlCursor()       { Clear(); }
00112     void Clear()        { row = col = -1; }
00113 
00114     int row;    // 0 based.
00115     int col;    // 0 based.
00116 };
00117 
00118 
00119 // Only used by Attribute::Query functions
00120 enum
00121 {
00122     TIXML_SUCCESS,
00123     TIXML_NO_ATTRIBUTE,
00124     TIXML_WRONG_TYPE
00125 };
00126 
00127 
00128 // Used by the parsing routines.
00129 enum TiXmlEncoding
00130 {
00131     TIXML_ENCODING_UNKNOWN,
00132     TIXML_ENCODING_UTF8,
00133     TIXML_ENCODING_LEGACY
00134 };
00135 
00136 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00137 
00160 #ifdef TIXML_USE_TICPP
00161 #include "ticpprc.h"
00162 class TiXmlBase : public TiCppRC
00163 #else
00164 class TiXmlBase
00165 #endif
00166 {
00167     friend class TiXmlNode;
00168     friend class TiXmlElement;
00169     friend class TiXmlDocument;
00170 
00171 public:
00172     TiXmlBase() :   userData(0) {}
00173     virtual ~TiXmlBase()                    {}
00174 
00180     virtual void Print( FILE* cfile, int depth ) const = 0;
00181 
00188     static void SetCondenseWhiteSpace( bool condense )      { condenseWhiteSpace = condense; }
00189 
00191     static bool IsWhiteSpaceCondensed()                     { return condenseWhiteSpace; }
00192 
00211     int Row() const         { return location.row + 1; }
00212     int Column() const      { return location.col + 1; }    
00213 
00214     void  SetUserData( void* user )         { userData = user; }
00215     void* GetUserData()                     { return userData; }
00216 
00217     // Table that returs, for a given lead byte, the total number of bytes
00218     // in the UTF-8 sequence.
00219     static const int utf8ByteTable[256];
00220 
00221     virtual const char* Parse(  const char* p,
00222                                 TiXmlParsingData* data,
00223                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00224 
00225     enum
00226     {
00227         TIXML_NO_ERROR = 0,
00228         TIXML_ERROR,
00229         TIXML_ERROR_OPENING_FILE,
00230         TIXML_ERROR_OUT_OF_MEMORY,
00231         TIXML_ERROR_PARSING_ELEMENT,
00232         TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00233         TIXML_ERROR_READING_ELEMENT_VALUE,
00234         TIXML_ERROR_READING_ATTRIBUTES,
00235         TIXML_ERROR_PARSING_EMPTY,
00236         TIXML_ERROR_READING_END_TAG,
00237         TIXML_ERROR_PARSING_UNKNOWN,
00238         TIXML_ERROR_PARSING_COMMENT,
00239         TIXML_ERROR_PARSING_DECLARATION,
00240         TIXML_ERROR_DOCUMENT_EMPTY,
00241         TIXML_ERROR_EMBEDDED_NULL,
00242         TIXML_ERROR_PARSING_CDATA,
00243         TIXML_ERROR_DOCUMENT_TOP_ONLY,
00244 
00245         TIXML_ERROR_STRING_COUNT
00246     };
00247 
00248 protected:
00249 
00250     void StreamDepth( TIXML_OSTREAM* stream, int depth ) const;
00251 
00252     // See STL_STRING_BUG
00253     // Utility class to overcome a bug.
00254     class StringToBuffer
00255     {
00256       public:
00257         StringToBuffer( const TIXML_STRING& str );
00258         ~StringToBuffer();
00259         char* buffer;
00260     };
00261 
00262     static const char*  SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00263     inline static bool  IsWhiteSpace( char c )
00264     {
00265         return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
00266     }
00267     inline static bool  IsWhiteSpace( int c )
00268     {
00269         if ( c < 256 )
00270             return IsWhiteSpace( (char) c );
00271         return false;   // Again, only truly correct for English/Latin...but usually works.
00272     }
00273 
00274     virtual void StreamOut (TIXML_OSTREAM *) const = 0;
00275     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const = 0;
00276 
00277     #ifdef TIXML_USE_STL
00278         static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00279         static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00280     #endif
00281 
00282     /*  Reads an XML name into the string provided. Returns
00283         a pointer just past the last character of the name,
00284         or 0 if the function has an error.
00285     */
00286     static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00287 
00288     /*  Reads text. Returns a pointer past the given end tag.
00289         Wickedly complex options, but it keeps the (sensitive) code in one place.
00290     */
00291     static const char* ReadText(    const char* in,             // where to start
00292                                     TIXML_STRING* text,         // the string read
00293                                     bool ignoreWhiteSpace,      // whether to keep the white space
00294                                     const char* endTag,         // what ends this text
00295                                     bool ignoreCase,            // whether to ignore case in the end tag
00296                                     TiXmlEncoding encoding );   // the current encoding
00297 
00298     // If an entity has been found, transform it into a character.
00299     static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00300 
00301     // Get a character, while interpreting entities.
00302     // The length can be from 0 to 4 bytes.
00303     inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00304     {
00305         assert( p );
00306         if ( encoding == TIXML_ENCODING_UTF8 )
00307         {
00308             *length = utf8ByteTable[ *((unsigned char*)p) ];
00309             assert( *length >= 0 && *length < 5 );
00310         }
00311         else
00312         {
00313             *length = 1;
00314         }
00315 
00316         if ( *length == 1 )
00317         {
00318             if ( *p == '&' )
00319                 return GetEntity( p, _value, length, encoding );
00320             *_value = *p;
00321             return p+1;
00322         }
00323         else if ( *length )
00324         {
00325             //strncpy( _value, p, *length );    // lots of compilers don't like this function (unsafe),
00326                                                 // and the null terminator isn't needed
00327             for( int i=0; p[i] && i<*length; ++i ) {
00328                 _value[i] = p[i];
00329             }
00330             return p + (*length);
00331         }
00332         else
00333         {
00334             // Not valid text.
00335             return 0;
00336         }
00337     }
00338 
00339     // Puts a string to a stream, expanding entities as it goes.
00340     // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00341     static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00342 
00343     static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00344 
00345     // Return true if the next characters in the stream are any of the endTag sequences.
00346     // Ignore case only works for english, and should only be relied on when comparing
00347     // to English words: StringEqual( p, "version", true ) is fine.
00348     static bool StringEqual(    const char* p,
00349                                 const char* endTag,
00350                                 bool ignoreCase,
00351                                 TiXmlEncoding encoding );
00352 
00353     static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00354 
00355     TiXmlCursor location;
00356 
00358     void*           userData;
00359 
00360     // None of these methods are reliable for any language except English.
00361     // Good for approximation, not great for accuracy.
00362     static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00363     static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00364     inline static int ToLower( int v, TiXmlEncoding encoding )
00365     {
00366         if ( encoding == TIXML_ENCODING_UTF8 )
00367         {
00368             if ( v < 128 ) return tolower( v );
00369             return v;
00370         }
00371         else
00372         {
00373             return tolower( v );
00374         }
00375     }
00376     static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00377 
00378 private:
00379     TiXmlBase( const TiXmlBase& );              // not implemented.
00380     void operator=( const TiXmlBase& base );    // not allowed.
00381 
00382     struct Entity
00383     {
00384         const char*     str;
00385         unsigned int    strLength;
00386         char            chr;
00387     };
00388     enum
00389     {
00390         NUM_ENTITY = 5,
00391         MAX_ENTITY_LENGTH = 6
00392 
00393     };
00394     static Entity entity[ NUM_ENTITY ];
00395     static bool condenseWhiteSpace;
00396 };
00397 
00398 
00405 class TiXmlNode : public TiXmlBase
00406 {
00407     friend class TiXmlDocument;
00408     friend class TiXmlElement;
00409 
00410 public:
00411     #ifdef TIXML_USE_STL
00412 
00416         friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00417 
00434         friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00435 
00437         friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00438 
00439     #else
00440         // Used internally, not part of the public API.
00441         friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00442     #endif
00443 
00447     enum NodeType
00448     {
00449         DOCUMENT,
00450         ELEMENT,
00451         COMMENT,
00452         UNKNOWN,
00453         TEXT,
00454         DECLARATION,
00455         TYPECOUNT
00456     };
00457 
00458     virtual ~TiXmlNode();
00459 
00472     const char *Value() const { return value.c_str (); }
00473 
00474     #ifdef TIXML_USE_STL
00475 
00479     const std::string& ValueStr() const { return value; }
00480     #endif
00481 
00491     void SetValue(const char * _value) { value = _value;}
00492 
00493     #ifdef TIXML_USE_STL
00495     void SetValue( const std::string& _value )  { value = _value; }
00496     #endif
00497 
00499     void Clear();
00500 
00502     TiXmlNode* Parent()                         { return parent; }
00503     const TiXmlNode* Parent() const             { return parent; }
00504 
00505     const TiXmlNode* FirstChild()   const   { return firstChild; }      
00506     TiXmlNode* FirstChild()                 { return firstChild; }
00507     const TiXmlNode* FirstChild( const char * value ) const;            
00508     TiXmlNode* FirstChild( const char * value );                        
00509 
00510     const TiXmlNode* LastChild() const  { return lastChild; }       
00511     TiXmlNode* LastChild()  { return lastChild; }
00512     const TiXmlNode* LastChild( const char * value ) const;         
00513     TiXmlNode* LastChild( const char * value );
00514 
00515     #ifdef TIXML_USE_STL
00516     const TiXmlNode* FirstChild( const std::string& _value ) const  {   return FirstChild (_value.c_str ());    }   
00517     TiXmlNode* FirstChild( const std::string& _value )              {   return FirstChild (_value.c_str ());    }   
00518     const TiXmlNode* LastChild( const std::string& _value ) const   {   return LastChild (_value.c_str ()); }   
00519     TiXmlNode* LastChild( const std::string& _value )               {   return LastChild (_value.c_str ()); }   
00520     #endif
00521 
00538     const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00539     TiXmlNode* IterateChildren( TiXmlNode* previous );
00540 
00542     const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00543     TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00544 
00545     #ifdef TIXML_USE_STL
00546     const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {   return IterateChildren (_value.c_str (), previous); }   
00547     TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous); }   
00548     #endif
00549 
00553     TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00554 
00555 
00565     TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00566 
00570     TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00571 
00575     TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00576 
00580     TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00581 
00583     bool RemoveChild( TiXmlNode* removeThis );
00584 
00586     const TiXmlNode* PreviousSibling() const            { return prev; }
00587     TiXmlNode* PreviousSibling()                        { return prev; }
00588 
00590     const TiXmlNode* PreviousSibling( const char * ) const;
00591     TiXmlNode* PreviousSibling( const char * );
00592 
00593     #ifdef TIXML_USE_STL
00594     const TiXmlNode* PreviousSibling( const std::string& _value ) const {   return PreviousSibling (_value.c_str ());   }   
00595     TiXmlNode* PreviousSibling( const std::string& _value )             {   return PreviousSibling (_value.c_str ());   }   
00596     const TiXmlNode* NextSibling( const std::string& _value) const      {   return NextSibling (_value.c_str ());   }   
00597     TiXmlNode* NextSibling( const std::string& _value)                  {   return NextSibling (_value.c_str ());   }   
00598     #endif
00599 
00601     const TiXmlNode* NextSibling() const                { return next; }
00602     TiXmlNode* NextSibling()                            { return next; }
00603 
00605     const TiXmlNode* NextSibling( const char * ) const;
00606     TiXmlNode* NextSibling( const char * );
00607 
00612     const TiXmlElement* NextSiblingElement() const;
00613     TiXmlElement* NextSiblingElement();
00614 
00619     const TiXmlElement* NextSiblingElement( const char * ) const;
00620     TiXmlElement* NextSiblingElement( const char * );
00621 
00622     #ifdef TIXML_USE_STL
00623     const TiXmlElement* NextSiblingElement( const std::string& _value) const    {   return NextSiblingElement (_value.c_str ());    }   
00624     TiXmlElement* NextSiblingElement( const std::string& _value)                {   return NextSiblingElement (_value.c_str ());    }   
00625     #endif
00626 
00628     const TiXmlElement* FirstChildElement() const;
00629     TiXmlElement* FirstChildElement();
00630 
00632     const TiXmlElement* FirstChildElement( const char * value ) const;
00633     TiXmlElement* FirstChildElement( const char * value );
00634 
00635     #ifdef TIXML_USE_STL
00636     const TiXmlElement* FirstChildElement( const std::string& _value ) const    {   return FirstChildElement (_value.c_str ()); }   
00637     TiXmlElement* FirstChildElement( const std::string& _value )                {   return FirstChildElement (_value.c_str ()); }   
00638     #endif
00639 
00644     int Type() const    { return type; }
00645 
00649     const TiXmlDocument* GetDocument() const;
00650     TiXmlDocument* GetDocument();
00651 
00653     bool NoChildren() const                     { return !firstChild; }
00654 
00655     virtual const TiXmlDocument*    ToDocument()    const { return 0; } 
00656     virtual const TiXmlElement*     ToElement()     const { return 0; } 
00657     virtual const TiXmlComment*     ToComment()     const { return 0; } 
00658     virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } 
00659     virtual const TiXmlText*        ToText()        const { return 0; } 
00660     virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } 
00661 
00662     virtual TiXmlDocument*          ToDocument()    { return 0; } 
00663     virtual TiXmlElement*           ToElement()     { return 0; } 
00664     virtual TiXmlComment*           ToComment()     { return 0; } 
00665     virtual TiXmlUnknown*           ToUnknown()     { return 0; } 
00666     virtual TiXmlText*              ToText()        { return 0; } 
00667     virtual TiXmlDeclaration*       ToDeclaration() { return 0; } 
00668 
00672     virtual TiXmlNode* Clone() const = 0;
00673 
00674 protected:
00675     TiXmlNode( NodeType _type );
00676 
00677     // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00678     // and the assignment operator.
00679     void CopyTo( TiXmlNode* target ) const;
00680 
00681     #ifdef TIXML_USE_STL
00682         // The real work of the input operator.
00683         virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00684     #endif
00685 
00686     // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00687     TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00688 
00689     TiXmlNode*      parent;
00690     NodeType        type;
00691 
00692     TiXmlNode*      firstChild;
00693     TiXmlNode*      lastChild;
00694 
00695     TIXML_STRING    value;
00696 
00697     TiXmlNode*      prev;
00698     TiXmlNode*      next;
00699 
00700 private:
00701     TiXmlNode( const TiXmlNode& );              // not implemented.
00702     void operator=( const TiXmlNode& base );    // not allowed.
00703 };
00704 
00712 class TiXmlAttribute : public TiXmlBase
00713 {
00714     friend class TiXmlAttributeSet;
00715 
00716 public:
00718     TiXmlAttribute() : TiXmlBase()
00719     {
00720         document = 0;
00721         prev = next = 0;
00722     }
00723 
00724     #ifdef TIXML_USE_STL
00726     TiXmlAttribute( const std::string& _name, const std::string& _value )
00727     {
00728         name = _name;
00729         value = _value;
00730         document = 0;
00731         prev = next = 0;
00732     }
00733     #endif
00734 
00736     TiXmlAttribute( const char * _name, const char * _value )
00737     {
00738         name = _name;
00739         value = _value;
00740         document = 0;
00741         prev = next = 0;
00742     }
00743 
00744     const char*     Name()  const       { return name.c_str(); }        
00745     const char*     Value() const       { return value.c_str(); }       
00746     #ifdef TIXML_USE_STL
00747     const std::string& ValueStr() const { return value; }               
00748     #endif
00749     int             IntValue() const;                                   
00750     double          DoubleValue() const;                                
00751 
00752     // Get the tinyxml string representation
00753     const TIXML_STRING& NameTStr() const { return name; }
00754 
00764     int QueryIntValue( int* _value ) const;
00766     int QueryDoubleValue( double* _value ) const;
00767 
00768     void SetName( const char* _name )   { name = _name; }               
00769     void SetValue( const char* _value ) { value = _value; }             
00770 
00771     void SetIntValue( int _value );                                     
00772     void SetDoubleValue( double _value );                               
00773 
00774     #ifdef TIXML_USE_STL
00776     void SetName( const std::string& _name )    { name = _name; }
00778     void SetValue( const std::string& _value )  { value = _value; }
00779     #endif
00780 
00782     const TiXmlAttribute* Next() const;
00783     TiXmlAttribute* Next();
00785     const TiXmlAttribute* Previous() const;
00786     TiXmlAttribute* Previous();
00787 
00788     bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00789     bool operator<( const TiXmlAttribute& rhs )  const { return name < rhs.name; }
00790     bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00791 
00792     /*  Attribute parsing starts: first letter of the name
00793                          returns: the next char after the value end quote
00794     */
00795     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00796 
00797     // Prints this Attribute to a FILE stream.
00798     virtual void Print( FILE* cfile, int depth ) const;
00799 
00800     virtual void StreamOut( TIXML_OSTREAM * out ) const;
00801     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const {}
00802 
00803     // [internal use]
00804     // Set the document pointer so the attribute can report errors.
00805     void SetDocument( TiXmlDocument* doc )  { document = doc; }
00806 
00807 private:
00808     TiXmlAttribute( const TiXmlAttribute& );                // not implemented.
00809     void operator=( const TiXmlAttribute& base );   // not allowed.
00810 
00811     TiXmlDocument*  document;   // A pointer back to a document, for error reporting.
00812     TIXML_STRING name;
00813     TIXML_STRING value;
00814     TiXmlAttribute* prev;
00815     TiXmlAttribute* next;
00816 };
00817 
00818 
00819 /*  A class used to manage a group of attributes.
00820     It is only used internally, both by the ELEMENT and the DECLARATION.
00821 
00822     The set can be changed transparent to the Element and Declaration
00823     classes that use it, but NOT transparent to the Attribute
00824     which has to implement a next() and previous() method. Which makes
00825     it a bit problematic and prevents the use of STL.
00826 
00827     This version is implemented with circular lists because:
00828         - I like circular lists
00829         - it demonstrates some independence from the (typical) doubly linked list.
00830 */
00831 class TiXmlAttributeSet
00832 {
00833 public:
00834     TiXmlAttributeSet();
00835     ~TiXmlAttributeSet();
00836 
00837     void Add( TiXmlAttribute* attribute );
00838     void Remove( TiXmlAttribute* attribute );
00839 
00840     const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00841     TiXmlAttribute* First()                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00842     const TiXmlAttribute* Last() const      { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00843     TiXmlAttribute* Last()                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00844 
00845     const TiXmlAttribute*   Find( const TIXML_STRING& name ) const;
00846     TiXmlAttribute* Find( const TIXML_STRING& name );
00847 
00848 private:
00849     //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00850     //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00851     TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00852     void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
00853 
00854     TiXmlAttribute sentinel;
00855 };
00856 
00857 
00862 class TiXmlElement : public TiXmlNode
00863 {
00864 public:
00866     TiXmlElement (const char * in_value);
00867 
00868     #ifdef TIXML_USE_STL
00870     TiXmlElement( const std::string& _value );
00871     #endif
00872 
00873     TiXmlElement( const TiXmlElement& );
00874 
00875     void operator=( const TiXmlElement& base );
00876 
00877     virtual ~TiXmlElement();
00878 
00882     const char* Attribute( const char* name ) const;
00883 
00890     const char* Attribute( const char* name, int* i ) const;
00891 
00898     const char* Attribute( const char* name, double* d ) const;
00899 
00907     int QueryIntAttribute( const char* name, int* _value ) const;
00909     int QueryDoubleAttribute( const char* name, double* _value ) const;
00911     int QueryFloatAttribute( const char* name, float* _value ) const {
00912         double d;
00913         int result = QueryDoubleAttribute( name, &d );
00914         if ( result == TIXML_SUCCESS ) {
00915             *_value = (float)d;
00916         }
00917         return result;
00918     }
00919     #ifdef TIXML_USE_STL
00920 
00926     template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
00927     {
00928         const TiXmlAttribute* node = attributeSet.Find( name );
00929         if ( !node )
00930             return TIXML_NO_ATTRIBUTE;
00931 
00932         std::stringstream sstream( node->ValueStr() );
00933         sstream >> *outValue;
00934         if ( !sstream.fail() )
00935             return TIXML_SUCCESS;
00936         return TIXML_WRONG_TYPE;
00937     }
00938     #endif
00939 
00943     void SetAttribute( const char* name, const char * _value );
00944 
00945     #ifdef TIXML_USE_STL
00946     const char* Attribute( const std::string& name ) const              { return Attribute( name.c_str() ); }
00947     const char* Attribute( const std::string& name, int* i ) const      { return Attribute( name.c_str(), i ); }
00948     const char* Attribute( const std::string& name, double* d ) const   { return Attribute( name.c_str(), d ); }
00949     int QueryIntAttribute( const std::string& name, int* _value ) const { return QueryIntAttribute( name.c_str(), _value ); }
00950     int QueryDoubleAttribute( const std::string& name, double* _value ) const { return QueryDoubleAttribute( name.c_str(), _value ); }
00951 
00953     void SetAttribute( const std::string& name, const std::string& _value );
00955     void SetAttribute( const std::string& name, int _value );
00956     #endif
00957 
00961     void SetAttribute( const char * name, int value );
00962 
00966     void SetDoubleAttribute( const char * name, double value );
00967 
00970     void RemoveAttribute( const char * name );
00971     #ifdef TIXML_USE_STL
00972     void RemoveAttribute( const std::string& name ) {   RemoveAttribute (name.c_str ());    }   
00973     #endif
00974 
00975     const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }        
00976     TiXmlAttribute* FirstAttribute()                { return attributeSet.First(); }
00977     const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }     
00978     TiXmlAttribute* LastAttribute()                 { return attributeSet.Last(); }
00979 
01012     const char* GetText() const;
01013 
01015     virtual TiXmlNode* Clone() const;
01016     // Print the Element to a FILE stream.
01017     virtual void Print( FILE* cfile, int depth ) const;
01018 
01019     /*  Attribtue parsing starts: next char past '<'
01020                          returns: next char past '>'
01021     */
01022     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01023 
01024     virtual const TiXmlElement*     ToElement()     const { return this; } 
01025     virtual TiXmlElement*           ToElement()           { return this; } 
01026 
01027 protected:
01028 
01029     void CopyTo( TiXmlElement* target ) const;
01030     void ClearThis();   // like clear, but initializes 'this' object as well
01031 
01032     // Used to be public [internal use]
01033     #ifdef TIXML_USE_STL
01034         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01035     #endif
01036     virtual void StreamOut( TIXML_OSTREAM * out ) const;
01037     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const;
01038 
01039     /*  [internal use]
01040         Reads the "value" of the element -- another element, or text.
01041         This should terminate with the current end tag.
01042     */
01043     const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01044 
01045 private:
01046 
01047     TiXmlAttributeSet attributeSet;
01048 };
01049 
01050 
01053 class TiXmlComment : public TiXmlNode
01054 {
01055 public:
01057     TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01058     TiXmlComment( const TiXmlComment& );
01059     void operator=( const TiXmlComment& base );
01060 
01061     virtual ~TiXmlComment() {}
01062 
01064     virtual TiXmlNode* Clone() const;
01066     virtual void Print( FILE* cfile, int depth ) const;
01067 
01068     /*  Attribtue parsing starts: at the ! of the !--
01069                          returns: next char past '>'
01070     */
01071     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01072 
01073     virtual const TiXmlComment*  ToComment() const { return this; } 
01074     virtual TiXmlComment*  ToComment() { return this; } 
01075 
01076 protected:
01077     void CopyTo( TiXmlComment* target ) const;
01078 
01079     // used to be public
01080     #ifdef TIXML_USE_STL
01081         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01082     #endif
01083     virtual void StreamOut( TIXML_OSTREAM * out ) const;
01084     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const;
01085 
01086 private:
01087 
01088 };
01089 
01090 
01096 class TiXmlText : public TiXmlNode
01097 {
01098     friend class TiXmlElement;
01099 public:
01104     TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01105     {
01106         SetValue( initValue );
01107         cdata = false;
01108     }
01109     virtual ~TiXmlText() {}
01110 
01111     #ifdef TIXML_USE_STL
01113     TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01114     {
01115         SetValue( initValue );
01116         cdata = false;
01117     }
01118     #endif
01119 
01120     TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )   { copy.CopyTo( this ); }
01121     void operator=( const TiXmlText& base )                             { base.CopyTo( this ); }
01122 
01124     virtual void Print( FILE* cfile, int depth ) const;
01125 
01127     bool CDATA()                    { return cdata; }
01129     void SetCDATA( bool _cdata )    { cdata = _cdata; }
01130 
01131     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01132 
01133     virtual const TiXmlText* ToText() const { return this; } 
01134     virtual TiXmlText*       ToText()       { return this; } 
01135 
01136 protected :
01138     virtual TiXmlNode* Clone() const;
01139     void CopyTo( TiXmlText* target ) const;
01140 
01141     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01142     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const;
01143 
01144     bool Blank() const; // returns true if all white space and new lines
01145     // [internal use]
01146     #ifdef TIXML_USE_STL
01147         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01148     #endif
01149 
01150 private:
01151     bool cdata;         // true if this should be input and output as a CDATA style text element
01152 };
01153 
01154 
01168 class TiXmlDeclaration : public TiXmlNode
01169 {
01170 public:
01172     TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01173 
01174 #ifdef TIXML_USE_STL
01176     TiXmlDeclaration(   const std::string& _version,
01177                         const std::string& _encoding,
01178                         const std::string& _standalone );
01179 #endif
01180 
01182     TiXmlDeclaration(   const char* _version,
01183                         const char* _encoding,
01184                         const char* _standalone );
01185 
01186     TiXmlDeclaration( const TiXmlDeclaration& copy );
01187     void operator=( const TiXmlDeclaration& copy );
01188 
01189     virtual ~TiXmlDeclaration() {}
01190 
01192     const char *Version() const         { return version.c_str (); }
01194     const char *Encoding() const        { return encoding.c_str (); }
01196     const char *Standalone() const      { return standalone.c_str (); }
01197 
01199     virtual TiXmlNode* Clone() const;
01201     virtual void Print( FILE* cfile, int depth ) const;
01202 
01203     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01204 
01205     virtual const TiXmlDeclaration* ToDeclaration() const { return this; } 
01206     virtual TiXmlDeclaration*       ToDeclaration()       { return this; } 
01207 
01208 protected:
01209     void CopyTo( TiXmlDeclaration* target ) const;
01210     // used to be public
01211     #ifdef TIXML_USE_STL
01212         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01213     #endif
01214     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01215     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const;
01216 
01217 private:
01218 
01219     TIXML_STRING version;
01220     TIXML_STRING encoding;
01221     TIXML_STRING standalone;
01222 };
01223 
01224 
01232 class TiXmlUnknown : public TiXmlNode
01233 {
01234 public:
01235     TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )    {}
01236     virtual ~TiXmlUnknown() {}
01237 
01238     TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )      { copy.CopyTo( this ); }
01239     void operator=( const TiXmlUnknown& copy )                                      { copy.CopyTo( this ); }
01240 
01242     virtual TiXmlNode* Clone() const;
01244     virtual void Print( FILE* cfile, int depth ) const;
01245 
01246     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01247 
01248     virtual const TiXmlUnknown*     ToUnknown()     const { return this; } 
01249     virtual TiXmlUnknown*           ToUnknown()     { return this; } 
01250 
01251 protected:
01252     void CopyTo( TiXmlUnknown* target ) const;
01253 
01254     #ifdef TIXML_USE_STL
01255         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01256     #endif
01257     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01258     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const;
01259 
01260 private:
01261 
01262 };
01263 
01264 
01269 class TiXmlDocument : public TiXmlNode
01270 {
01271 public:
01273     TiXmlDocument();
01275     TiXmlDocument( const char * documentName );
01276 
01277     #ifdef TIXML_USE_STL
01279     TiXmlDocument( const std::string& documentName );
01280     #endif
01281 
01282     TiXmlDocument( const TiXmlDocument& copy );
01283     void operator=( const TiXmlDocument& copy );
01284 
01285     virtual ~TiXmlDocument() {}
01286 
01291     bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01293     bool SaveFile() const;
01295     bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01297     bool SaveFile( const char * filename ) const;
01303     bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01305     bool SaveFile( FILE* ) const;
01306 
01307     #ifdef TIXML_USE_STL
01308     bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )           
01309     {
01310         StringToBuffer f( filename );
01311         return ( f.buffer && LoadFile( f.buffer, encoding ));
01312     }
01313     bool SaveFile( const std::string& filename ) const      
01314     {
01315         StringToBuffer f( filename );
01316         return ( f.buffer && SaveFile( f.buffer ));
01317     }
01321     std::string GetAsString();
01322     #endif
01323 
01330     bool GetAsCharBuffer( char* buffer, size_t bufferSize );
01331 
01336     virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01337 
01342     const TiXmlElement* RootElement() const     { return FirstChildElement(); }
01343     TiXmlElement* RootElement()                 { return FirstChildElement(); }
01344 
01350     bool Error() const                      { return error; }
01351 
01353     const char * ErrorDesc() const  { return errorDesc.c_str (); }
01354 
01358     int ErrorId()   const               { return errorId; }
01359 
01367     int ErrorRow()  { return errorLocation.row+1; }
01368     int ErrorCol()  { return errorLocation.col+1; } 
01369 
01394     void SetTabSize( int _tabsize )     { tabsize = _tabsize; }
01395 
01396     int TabSize() const { return tabsize; }
01397 
01401     void ClearError()                       {   error = false;
01402                                                 errorId = 0;
01403                                                 errorDesc = "";
01404                                                 errorLocation.row = errorLocation.col = 0;
01405                                                 //errorLocation.last = 0;
01406                                             }
01407 
01409     void Print() const                      { Print( stdout, 0 ); }
01410 
01412     virtual void Print( FILE* cfile, int depth = 0 ) const;
01413     // [internal use]
01414     void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01415 
01416     virtual const TiXmlDocument*    ToDocument()    const { return this; } 
01417     virtual TiXmlDocument*          ToDocument()          { return this; } 
01418 
01419 protected :
01420     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01421     virtual void FormattedStreamOut( TIXML_OSTREAM * stream, int depth ) const;
01422     // [internal use]
01423     virtual TiXmlNode* Clone() const;
01424     #ifdef TIXML_USE_STL
01425         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01426     #endif
01427 
01428 private:
01429     void CopyTo( TiXmlDocument* target ) const;
01430 
01431     bool error;
01432     int  errorId;
01433     TIXML_STRING errorDesc;
01434     int tabsize;
01435     TiXmlCursor errorLocation;
01436     bool useMicrosoftBOM;       // the UTF-8 BOM were found when read. Note this, and try to write.
01437 };
01438 
01439 
01520 class TiXmlHandle
01521 {
01522 public:
01524     TiXmlHandle( TiXmlNode* _node )                 { this->node = _node; }
01526     TiXmlHandle( const TiXmlHandle& ref )           { this->node = ref.node; }
01527     TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01528 
01530     TiXmlHandle FirstChild() const;
01532     TiXmlHandle FirstChild( const char * value ) const;
01534     TiXmlHandle FirstChildElement() const;
01536     TiXmlHandle FirstChildElement( const char * value ) const;
01537 
01541     TiXmlHandle Child( const char* value, int index ) const;
01545     TiXmlHandle Child( int index ) const;
01550     TiXmlHandle ChildElement( const char* value, int index ) const;
01555     TiXmlHandle ChildElement( int index ) const;
01556 
01557     #ifdef TIXML_USE_STL
01558     TiXmlHandle FirstChild( const std::string& _value ) const               { return FirstChild( _value.c_str() ); }
01559     TiXmlHandle FirstChildElement( const std::string& _value ) const        { return FirstChildElement( _value.c_str() ); }
01560 
01561     TiXmlHandle Child( const std::string& _value, int index ) const         { return Child( _value.c_str(), index ); }
01562     TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01563     #endif
01564 
01566     TiXmlNode* Node() const         { return node; }
01568     TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01570     TiXmlText* Text() const         { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01572     TiXmlUnknown* Unknown() const           { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01573 
01574 private:
01575     TiXmlNode* node;
01576 };
01577 
01578 #ifdef _MSC_VER
01579 #pragma warning( pop )
01580 #endif
01581 
01582 #endif
01583 

Generated on Thu Sep 7 14:23:32 2006 for TinyXml by  doxygen 1.4.7