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( push )
00031 #pragma warning( disable : 4530 )
00032 #pragma warning( disable : 4786 )
00033 #endif
00034 
00035 #include <ctype.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <assert.h>
00040 
00041 // Help out windows:
00042 #if defined( _DEBUG ) && !defined( DEBUG )
00043 #define DEBUG
00044 #endif
00045 
00046 #if defined( DEBUG ) && defined( _MSC_VER )
00047 #include <windows.h>
00048 #define TIXML_LOG OutputDebugString
00049 #else
00050 #define TIXML_LOG printf
00051 #endif
00052 
00053 #ifdef TIXML_USE_STL
00054     #include <string>
00055     #include <iostream>
00056     #define TIXML_STRING    std::string
00057     #define TIXML_ISTREAM   std::istream
00058     #define TIXML_OSTREAM   std::ostream
00059 #else
00060     #include "tinystr.h"
00061     #define TIXML_STRING    TiXmlString
00062     #define TIXML_OSTREAM   TiXmlOutStream
00063 #endif
00064 
00065 // Deprecated library function hell. Compilers want to use the
00066 // new safe versions. This probably doesn't fully address the problem,
00067 // but it gets closer. There are too many compilers for me to fully
00068 // test. If you get compilation troubles, undefine TIXML_SAFE
00069 
00070 #define TIXML_SAFE      // TinyXml isn't fully buffer overrun protected, safe code. This is work in progress.
00071 #ifdef TIXML_SAFE
00072     #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
00073         // Microsoft visual studio, version 6 and higher.
00074         //#pragma message( "Using _sn* functions." )
00075         #define TIXML_SNPRINTF _snprintf
00076         #define TIXML_SNSCANF  _snscanf
00077     #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00078         // GCC version 3 and higher.s
00079         //#warning( "Using sn* functions." )
00080         #define TIXML_SNPRINTF snprintf
00081         #define TIXML_SNSCANF  snscanf
00082     #endif
00083 #endif  
00084 
00085 class TiXmlDocument;
00086 class TiXmlElement;
00087 class TiXmlComment;
00088 class TiXmlUnknown;
00089 class TiXmlAttribute;
00090 class TiXmlText;
00091 class TiXmlDeclaration;
00092 class TiXmlParsingData;
00093 
00094 const int TIXML_MAJOR_VERSION = 2;
00095 const int TIXML_MINOR_VERSION = 4;
00096 const int TIXML_PATCH_VERSION = 0;
00097 
00098 /*  Internal structure for tracking location of items 
00099     in the XML file.
00100 */
00101 struct TiXmlCursor
00102 {
00103     TiXmlCursor()       { Clear(); }
00104     void Clear()        { row = col = -1; }
00105 
00106     int row;    // 0 based.
00107     int col;    // 0 based.
00108 };
00109 
00110 
00111 // Only used by Attribute::Query functions
00112 enum 
00113 { 
00114     TIXML_SUCCESS,
00115     TIXML_NO_ATTRIBUTE,
00116     TIXML_WRONG_TYPE
00117 };
00118 
00119 
00120 // Used by the parsing routines.
00121 enum TiXmlEncoding
00122 {
00123     TIXML_ENCODING_UNKNOWN,
00124     TIXML_ENCODING_UTF8,
00125     TIXML_ENCODING_LEGACY
00126 };
00127 
00128 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00129 
00152 class TiXmlBase
00153 {
00154     friend class TiXmlNode;
00155     friend class TiXmlElement;
00156     friend class TiXmlDocument;
00157 
00158 public:
00159     TiXmlBase() :   userData(0) {}
00160     virtual ~TiXmlBase()                    {}
00161 
00167     virtual void Print( FILE* cfile, int depth ) const = 0;
00168 
00175     static void SetCondenseWhiteSpace( bool condense )      { condenseWhiteSpace = condense; }
00176 
00178     static bool IsWhiteSpaceCondensed()                     { return condenseWhiteSpace; }
00179 
00198     int Row() const         { return location.row + 1; }
00199     int Column() const      { return location.col + 1; }    
00200 
00201     void  SetUserData( void* user )         { userData = user; }
00202     void* GetUserData()                     { return userData; }
00203 
00204     // Table that returs, for a given lead byte, the total number of bytes
00205     // in the UTF-8 sequence.
00206     static const int utf8ByteTable[256];
00207 
00208     virtual const char* Parse(  const char* p, 
00209                                 TiXmlParsingData* data, 
00210                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00211 
00212     enum
00213     {
00214         TIXML_NO_ERROR = 0,
00215         TIXML_ERROR,
00216         TIXML_ERROR_OPENING_FILE,
00217         TIXML_ERROR_OUT_OF_MEMORY,
00218         TIXML_ERROR_PARSING_ELEMENT,
00219         TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00220         TIXML_ERROR_READING_ELEMENT_VALUE,
00221         TIXML_ERROR_READING_ATTRIBUTES,
00222         TIXML_ERROR_PARSING_EMPTY,
00223         TIXML_ERROR_READING_END_TAG,
00224         TIXML_ERROR_PARSING_UNKNOWN,
00225         TIXML_ERROR_PARSING_COMMENT,
00226         TIXML_ERROR_PARSING_DECLARATION,
00227         TIXML_ERROR_DOCUMENT_EMPTY,
00228         TIXML_ERROR_EMBEDDED_NULL,
00229         TIXML_ERROR_PARSING_CDATA,
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 
00252     virtual void StreamOut (TIXML_OSTREAM *) const = 0;
00253 
00254     #ifdef TIXML_USE_STL
00255         static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00256         static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00257     #endif
00258 
00259     /*  Reads an XML name into the string provided. Returns
00260         a pointer just past the last character of the name,
00261         or 0 if the function has an error.
00262     */
00263     static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00264 
00265     /*  Reads text. Returns a pointer past the given end tag.
00266         Wickedly complex options, but it keeps the (sensitive) code in one place.
00267     */
00268     static const char* ReadText(    const char* in,             // where to start
00269                                     TIXML_STRING* text,         // the string read
00270                                     bool ignoreWhiteSpace,      // whether to keep the white space
00271                                     const char* endTag,         // what ends this text
00272                                     bool ignoreCase,            // whether to ignore case in the end tag
00273                                     TiXmlEncoding encoding );   // the current encoding
00274 
00275     // If an entity has been found, transform it into a character.
00276     static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00277 
00278     // Get a character, while interpreting entities.
00279     // The length can be from 0 to 4 bytes.
00280     inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00281     {
00282         assert( p );
00283         if ( encoding == TIXML_ENCODING_UTF8 )
00284         {
00285             *length = utf8ByteTable[ *((unsigned char*)p) ];
00286             assert( *length >= 0 && *length < 5 );
00287         }
00288         else
00289         {
00290             *length = 1;
00291         }
00292 
00293         if ( *length == 1 )
00294         {
00295             if ( *p == '&' )
00296                 return GetEntity( p, _value, length, encoding );
00297             *_value = *p;
00298             return p+1;
00299         }
00300         else if ( *length )
00301         {
00302             //strncpy( _value, p, *length );    // lots of compilers don't like this function (unsafe),
00303                                                 // and the null terminator isn't needed
00304             for( int i=0; p[i] && i<*length; ++i ) {
00305                 _value[i] = p[i];
00306             }
00307             return p + (*length);
00308         }
00309         else
00310         {
00311             // Not valid text.
00312             return 0;
00313         }
00314     }
00315 
00316     // Puts a string to a stream, expanding entities as it goes.
00317     // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00318     static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00319 
00320     static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00321 
00322     // Return true if the next characters in the stream are any of the endTag sequences.
00323     // Ignore case only works for english, and should only be relied on when comparing
00324     // to English words: StringEqual( p, "version", true ) is fine.
00325     static bool StringEqual(    const char* p,
00326                                 const char* endTag,
00327                                 bool ignoreCase,
00328                                 TiXmlEncoding encoding );
00329 
00330     static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00331 
00332     TiXmlCursor location;
00333 
00335     void*           userData;
00336     
00337     // None of these methods are reliable for any language except English.
00338     // Good for approximation, not great for accuracy.
00339     static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00340     static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00341     inline static int ToLower( int v, TiXmlEncoding encoding )
00342     {
00343         if ( encoding == TIXML_ENCODING_UTF8 )
00344         {
00345             if ( v < 128 ) return tolower( v );
00346             return v;
00347         }
00348         else
00349         {
00350             return tolower( v );
00351         }
00352     }
00353     static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00354 
00355 private:
00356     TiXmlBase( const TiXmlBase& );              // not implemented.
00357     void operator=( const TiXmlBase& base );    // not allowed.
00358 
00359     struct Entity
00360     {
00361         const char*     str;
00362         unsigned int    strLength;
00363         char            chr;
00364     };
00365     enum
00366     {
00367         NUM_ENTITY = 5,
00368         MAX_ENTITY_LENGTH = 6
00369 
00370     };
00371     static Entity entity[ NUM_ENTITY ];
00372     static bool condenseWhiteSpace;
00373 };
00374 
00375 
00382 class TiXmlNode : public TiXmlBase
00383 {
00384     friend class TiXmlDocument;
00385     friend class TiXmlElement;
00386 
00387 public:
00388     #ifdef TIXML_USE_STL    
00389 
00393         friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00394 
00411         friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00412 
00414         friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00415 
00416     #else
00417         // Used internally, not part of the public API.
00418         friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00419     #endif
00420 
00424     enum NodeType
00425     {
00426         DOCUMENT,
00427         ELEMENT,
00428         COMMENT,
00429         UNKNOWN,
00430         TEXT,
00431         DECLARATION,
00432         TYPECOUNT
00433     };
00434 
00435     virtual ~TiXmlNode();
00436 
00449     const char *Value() const { return value.c_str (); }
00450 
00451     #ifdef TIXML_USE_STL
00452 
00456     const std::string& ValueStr() const { return value; }
00457     #endif
00458 
00468     void SetValue(const char * _value) { value = _value;}
00469 
00470     #ifdef TIXML_USE_STL
00471 
00472     void SetValue( const std::string& _value )    
00473     {     
00474         StringToBuffer buf( _value );
00475         SetValue( buf.buffer ? buf.buffer : "" );       
00476     }   
00477     #endif
00478 
00480     void Clear();
00481 
00483     TiXmlNode* Parent()                         { return parent; }
00484     const TiXmlNode* Parent() const             { return parent; }
00485 
00486     const TiXmlNode* FirstChild()   const   { return firstChild; }      
00487     TiXmlNode* FirstChild()                 { return firstChild; }
00488     const TiXmlNode* FirstChild( const char * value ) const;            
00489     TiXmlNode* FirstChild( const char * value );                        
00490 
00491     const TiXmlNode* LastChild() const  { return lastChild; }       
00492     TiXmlNode* LastChild()  { return lastChild; }
00493     const TiXmlNode* LastChild( const char * value ) const;         
00494     TiXmlNode* LastChild( const char * value ); 
00495 
00496     #ifdef TIXML_USE_STL
00497     const TiXmlNode* FirstChild( const std::string& _value ) const  {   return FirstChild (_value.c_str ());    }   
00498     TiXmlNode* FirstChild( const std::string& _value )              {   return FirstChild (_value.c_str ());    }   
00499     const TiXmlNode* LastChild( const std::string& _value ) const   {   return LastChild (_value.c_str ()); }   
00500     TiXmlNode* LastChild( const std::string& _value )               {   return LastChild (_value.c_str ()); }   
00501     #endif
00502 
00519     const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00520     TiXmlNode* IterateChildren( TiXmlNode* previous );
00521 
00523     const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00524     TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00525 
00526     #ifdef TIXML_USE_STL
00527     const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {   return IterateChildren (_value.c_str (), previous); }   
00528     TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous); }   
00529     #endif
00530 
00534     TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00535 
00536 
00546     TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00547 
00551     TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00552 
00556     TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00557 
00561     TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00562 
00564     bool RemoveChild( TiXmlNode* removeThis );
00565 
00567     const TiXmlNode* PreviousSibling() const            { return prev; }
00568     TiXmlNode* PreviousSibling()                        { return prev; }
00569 
00571     const TiXmlNode* PreviousSibling( const char * ) const;
00572     TiXmlNode* PreviousSibling( const char * );
00573 
00574     #ifdef TIXML_USE_STL
00575     const TiXmlNode* PreviousSibling( const std::string& _value ) const {   return PreviousSibling (_value.c_str ());   }   
00576     TiXmlNode* PreviousSibling( const std::string& _value )             {   return PreviousSibling (_value.c_str ());   }   
00577     const TiXmlNode* NextSibling( const std::string& _value) const      {   return NextSibling (_value.c_str ());   }   
00578     TiXmlNode* NextSibling( const std::string& _value)                  {   return NextSibling (_value.c_str ());   }   
00579     #endif
00580 
00582     const TiXmlNode* NextSibling() const                { return next; }
00583     TiXmlNode* NextSibling()                            { return next; }
00584 
00586     const TiXmlNode* NextSibling( const char * ) const;
00587     TiXmlNode* NextSibling( const char * );
00588 
00593     const TiXmlElement* NextSiblingElement() const;
00594     TiXmlElement* NextSiblingElement();
00595 
00600     const TiXmlElement* NextSiblingElement( const char * ) const;
00601     TiXmlElement* NextSiblingElement( const char * );
00602 
00603     #ifdef TIXML_USE_STL
00604     const TiXmlElement* NextSiblingElement( const std::string& _value) const    {   return NextSiblingElement (_value.c_str ());    }   
00605     TiXmlElement* NextSiblingElement( const std::string& _value)                {   return NextSiblingElement (_value.c_str ());    }   
00606     #endif
00607 
00609     const TiXmlElement* FirstChildElement() const;
00610     TiXmlElement* FirstChildElement();
00611 
00613     const TiXmlElement* FirstChildElement( const char * value ) const;
00614     TiXmlElement* FirstChildElement( const char * value );
00615 
00616     #ifdef TIXML_USE_STL
00617     const TiXmlElement* FirstChildElement( const std::string& _value ) const    {   return FirstChildElement (_value.c_str ()); }   
00618     TiXmlElement* FirstChildElement( const std::string& _value )                {   return FirstChildElement (_value.c_str ()); }   
00619     #endif
00620 
00625     virtual int Type() const    { return type; }
00626 
00630     const TiXmlDocument* GetDocument() const;
00631     TiXmlDocument* GetDocument();
00632 
00634     bool NoChildren() const                     { return !firstChild; }
00635 
00636     const TiXmlDocument* ToDocument()   const       { return ( this && type == DOCUMENT ) ? (const TiXmlDocument*) this : 0; } 
00637     const TiXmlElement*  ToElement() const          { return ( this && type == ELEMENT  ) ? (const TiXmlElement*)  this : 0; } 
00638     const TiXmlComment*  ToComment() const          { return ( this && type == COMMENT  ) ? (const TiXmlComment*)  this : 0; } 
00639     const TiXmlUnknown*  ToUnknown() const          { return ( this && type == UNKNOWN  ) ? (const TiXmlUnknown*)  this : 0; } 
00640     const TiXmlText*       ToText()    const        { return ( this && type == TEXT     ) ? (const TiXmlText*)     this : 0; } 
00641     const TiXmlDeclaration* ToDeclaration() const   { return ( this && type == DECLARATION ) ? (const TiXmlDeclaration*) this : 0; } 
00642 
00643     TiXmlDocument* ToDocument()         { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } 
00644     TiXmlElement*  ToElement()          { return ( this && type == ELEMENT  ) ? (TiXmlElement*)  this : 0; } 
00645     TiXmlComment*  ToComment()          { return ( this && type == COMMENT  ) ? (TiXmlComment*)  this : 0; } 
00646     TiXmlUnknown*  ToUnknown()          { return ( this && type == UNKNOWN  ) ? (TiXmlUnknown*)  this : 0; } 
00647     TiXmlText*     ToText()             { return ( this && type == TEXT     ) ? (TiXmlText*)     this : 0; } 
00648     TiXmlDeclaration* ToDeclaration()   { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } 
00649 
00653     virtual TiXmlNode* Clone() const = 0;
00654 
00655 protected:
00656     TiXmlNode( NodeType _type );
00657 
00658     // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00659     // and the assignment operator.
00660     void CopyTo( TiXmlNode* target ) const;
00661 
00662     #ifdef TIXML_USE_STL
00663         // The real work of the input operator.
00664         virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00665     #endif
00666 
00667     // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00668     TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00669 
00670     TiXmlNode*      parent;
00671     NodeType        type;
00672 
00673     TiXmlNode*      firstChild;
00674     TiXmlNode*      lastChild;
00675 
00676     TIXML_STRING    value;
00677 
00678     TiXmlNode*      prev;
00679     TiXmlNode*      next;
00680 
00681 private:
00682     TiXmlNode( const TiXmlNode& );              // not implemented.
00683     void operator=( const TiXmlNode& base );    // not allowed.
00684 };
00685 
00686 
00694 class TiXmlAttribute : public TiXmlBase
00695 {
00696     friend class TiXmlAttributeSet;
00697 
00698 public:
00700     TiXmlAttribute() : TiXmlBase()
00701     {
00702         document = 0;
00703         prev = next = 0;
00704     }
00705 
00706     #ifdef TIXML_USE_STL
00707 
00708     TiXmlAttribute( const std::string& _name, const std::string& _value )
00709     {
00710         name = _name;
00711         value = _value;
00712         document = 0;
00713         prev = next = 0;
00714     }
00715     #endif
00716 
00718     TiXmlAttribute( const char * _name, const char * _value )
00719     {
00720         name = _name;
00721         value = _value;
00722         document = 0;
00723         prev = next = 0;
00724     }
00725 
00726     const char*     Name()  const       { return name.c_str (); }       
00727     const char*     Value() const       { return value.c_str (); }      
00728     int             IntValue() const;                                   
00729     double          DoubleValue() const;                                
00730 
00740     int QueryIntValue( int* _value ) const;
00742     int QueryDoubleValue( double* _value ) const;
00743 
00744     void SetName( const char* _name )   { name = _name; }               
00745     void SetValue( const char* _value ) { value = _value; }             
00746 
00747     void SetIntValue( int _value );                                     
00748     void SetDoubleValue( double _value );                               
00749 
00750     #ifdef TIXML_USE_STL
00751 
00752     void SetName( const std::string& _name )    
00753     {   
00754         StringToBuffer buf( _name );
00755         SetName ( buf.buffer ? buf.buffer : "error" );  
00756     }
00758     void SetValue( const std::string& _value )  
00759     {   
00760         StringToBuffer buf( _value );
00761         SetValue( buf.buffer ? buf.buffer : "error" );  
00762     }
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 char * name ) const;
00828     TiXmlAttribute* Find( const char * 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 
00905     void SetAttribute( const char* name, const char * _value );
00906 
00907     #ifdef TIXML_USE_STL
00908     const char* Attribute( const std::string& name ) const              { return Attribute( name.c_str() ); }
00909     const char* Attribute( const std::string& name, int* i ) const      { return Attribute( name.c_str(), i ); }
00910     const char* Attribute( const std::string& name, double* d ) const   { return Attribute( name.c_str(), d ); }
00911     int QueryIntAttribute( const std::string& name, int* _value ) const { return QueryIntAttribute( name.c_str(), _value ); }
00912     int QueryDoubleAttribute( const std::string& name, double* _value ) const { return QueryDoubleAttribute( name.c_str(), _value ); }
00913 
00915     void SetAttribute( const std::string& name, const std::string& _value ) 
00916     {   
00917         StringToBuffer n( name );
00918         StringToBuffer v( _value );
00919         if ( n.buffer && v.buffer )
00920             SetAttribute (n.buffer, v.buffer ); 
00921     }   
00923     void SetAttribute( const std::string& name, int _value )    
00924     {   
00925         StringToBuffer n( name );
00926         if ( n.buffer )
00927             SetAttribute (n.buffer, _value);    
00928     }   
00929     #endif
00930 
00934     void SetAttribute( const char * name, int value );
00935 
00939     void SetDoubleAttribute( const char * name, double value );
00940 
00943     void RemoveAttribute( const char * name );
00944     #ifdef TIXML_USE_STL
00945     void RemoveAttribute( const std::string& name ) {   RemoveAttribute (name.c_str ());    }   
00946     #endif
00947 
00948     const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }        
00949     TiXmlAttribute* FirstAttribute()                { return attributeSet.First(); }
00950     const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }     
00951     TiXmlAttribute* LastAttribute()                 { return attributeSet.Last(); }
00952 
00985     const char* GetText() const;
00986 
00988     virtual TiXmlNode* Clone() const;
00989     // Print the Element to a FILE stream.
00990     virtual void Print( FILE* cfile, int depth ) const;
00991 
00992     /*  Attribtue parsing starts: next char past '<'
00993                          returns: next char past '>'
00994     */
00995     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00996 
00997 protected:
00998 
00999     void CopyTo( TiXmlElement* target ) const;
01000     void ClearThis();   // like clear, but initializes 'this' object as well
01001 
01002     // Used to be public [internal use]
01003     #ifdef TIXML_USE_STL
01004         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01005     #endif
01006     virtual void StreamOut( TIXML_OSTREAM * out ) const;
01007 
01008     /*  [internal use]
01009         Reads the "value" of the element -- another element, or text.
01010         This should terminate with the current end tag.
01011     */
01012     const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01013 
01014 private:
01015 
01016     TiXmlAttributeSet attributeSet;
01017 };
01018 
01019 
01022 class TiXmlComment : public TiXmlNode
01023 {
01024 public:
01026     TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01027     TiXmlComment( const TiXmlComment& );
01028     void operator=( const TiXmlComment& base );
01029 
01030     virtual ~TiXmlComment() {}
01031 
01033     virtual TiXmlNode* Clone() const;
01035     virtual void Print( FILE* cfile, int depth ) const;
01036 
01037     /*  Attribtue parsing starts: at the ! of the !--
01038                          returns: next char past '>'
01039     */
01040     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01041 
01042 protected:
01043     void CopyTo( TiXmlComment* target ) const;
01044 
01045     // used to be public
01046     #ifdef TIXML_USE_STL
01047         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01048     #endif
01049     virtual void StreamOut( TIXML_OSTREAM * out ) const;
01050 
01051 private:
01052 
01053 };
01054 
01055 
01061 class TiXmlText : public TiXmlNode
01062 {
01063     friend class TiXmlElement;
01064 public:
01069     TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01070     {
01071         SetValue( initValue );
01072         cdata = false;
01073     }
01074     virtual ~TiXmlText() {}
01075 
01076     #ifdef TIXML_USE_STL
01077 
01078     TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01079     {
01080         SetValue( initValue );
01081         cdata = false;
01082     }
01083     #endif
01084 
01085     TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )   { copy.CopyTo( this ); }
01086     void operator=( const TiXmlText& base )                             { base.CopyTo( this ); }
01087 
01089     virtual void Print( FILE* cfile, int depth ) const;
01090 
01092     bool CDATA()                    { return cdata; }
01094     void SetCDATA( bool _cdata )    { cdata = _cdata; }
01095 
01096     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01097 
01098 protected :
01100     virtual TiXmlNode* Clone() const;
01101     void CopyTo( TiXmlText* target ) const;
01102 
01103     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01104     bool Blank() const; // returns true if all white space and new lines
01105     // [internal use]
01106     #ifdef TIXML_USE_STL
01107         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01108     #endif
01109 
01110 private:
01111     bool cdata;         // true if this should be input and output as a CDATA style text element
01112 };
01113 
01114 
01128 class TiXmlDeclaration : public TiXmlNode
01129 {
01130 public:
01132     TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01133 
01134 #ifdef TIXML_USE_STL
01135 
01136     TiXmlDeclaration(   const std::string& _version,
01137                         const std::string& _encoding,
01138                         const std::string& _standalone );
01139 #endif
01140 
01142     TiXmlDeclaration(   const char* _version,
01143                         const char* _encoding,
01144                         const char* _standalone );
01145 
01146     TiXmlDeclaration( const TiXmlDeclaration& copy );
01147     void operator=( const TiXmlDeclaration& copy );
01148 
01149     virtual ~TiXmlDeclaration() {}
01150 
01152     const char *Version() const         { return version.c_str (); }
01154     const char *Encoding() const        { return encoding.c_str (); }
01156     const char *Standalone() const      { return standalone.c_str (); }
01157 
01159     virtual TiXmlNode* Clone() const;
01161     virtual void Print( FILE* cfile, int depth ) const;
01162 
01163     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01164 
01165 protected:
01166     void CopyTo( TiXmlDeclaration* target ) const;
01167     // used to be public
01168     #ifdef TIXML_USE_STL
01169         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01170     #endif
01171     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01172 
01173 private:
01174 
01175     TIXML_STRING version;
01176     TIXML_STRING encoding;
01177     TIXML_STRING standalone;
01178 };
01179 
01180 
01188 class TiXmlUnknown : public TiXmlNode
01189 {
01190 public:
01191     TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )    {}
01192     virtual ~TiXmlUnknown() {}
01193 
01194     TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )      { copy.CopyTo( this ); }
01195     void operator=( const TiXmlUnknown& copy )                                      { copy.CopyTo( this ); }
01196 
01198     virtual TiXmlNode* Clone() const;
01200     virtual void Print( FILE* cfile, int depth ) const;
01201 
01202     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01203 
01204 protected:
01205     void CopyTo( TiXmlUnknown* target ) const;
01206 
01207     #ifdef TIXML_USE_STL
01208         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01209     #endif
01210     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01211 
01212 private:
01213 
01214 };
01215 
01216 
01221 class TiXmlDocument : public TiXmlNode
01222 {
01223 public:
01225     TiXmlDocument();
01227     TiXmlDocument( const char * documentName );
01228 
01229     #ifdef TIXML_USE_STL
01230 
01231     TiXmlDocument( const std::string& documentName );
01232     #endif
01233 
01234     TiXmlDocument( const TiXmlDocument& copy );
01235     void operator=( const TiXmlDocument& copy );
01236 
01237     virtual ~TiXmlDocument() {}
01238 
01243     bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01245     bool SaveFile() const;
01247     bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01249     bool SaveFile( const char * filename ) const;
01250 
01251     #ifdef TIXML_USE_STL
01252     bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )           
01253     {
01254         StringToBuffer f( filename );
01255         return ( f.buffer && LoadFile( f.buffer, encoding ));
01256     }
01257     bool SaveFile( const std::string& filename ) const      
01258     {
01259         StringToBuffer f( filename );
01260         return ( f.buffer && SaveFile( f.buffer ));
01261     }
01262     #endif
01263 
01268     virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01269 
01274     const TiXmlElement* RootElement() const     { return FirstChildElement(); }
01275     TiXmlElement* RootElement()                 { return FirstChildElement(); }
01276 
01282     bool Error() const                      { return error; }
01283 
01285     const char * ErrorDesc() const  { return errorDesc.c_str (); }
01286 
01290     int ErrorId()   const               { return errorId; }
01291 
01299     int ErrorRow()  { return errorLocation.row+1; }
01300     int ErrorCol()  { return errorLocation.col+1; } 
01301 
01326     void SetTabSize( int _tabsize )     { tabsize = _tabsize; }
01327 
01328     int TabSize() const { return tabsize; }
01329 
01333     void ClearError()                       {   error = false; 
01334                                                 errorId = 0; 
01335                                                 errorDesc = ""; 
01336                                                 errorLocation.row = errorLocation.col = 0; 
01337                                                 //errorLocation.last = 0; 
01338                                             }
01339 
01341     void Print() const                      { Print( stdout, 0 ); }
01342 
01344     virtual void Print( FILE* cfile, int depth = 0 ) const;
01345     // [internal use]
01346     void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01347 
01348 protected :
01349     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01350     // [internal use]
01351     virtual TiXmlNode* Clone() const;
01352     #ifdef TIXML_USE_STL
01353         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01354     #endif
01355 
01356 private:
01357     void CopyTo( TiXmlDocument* target ) const;
01358 
01359     bool error;
01360     int  errorId;
01361     TIXML_STRING errorDesc;
01362     int tabsize;
01363     TiXmlCursor errorLocation;
01364     bool useMicrosoftBOM;       // the UTF-8 BOM were found when read. Note this, and try to write.
01365 };
01366 
01367 
01448 class TiXmlHandle
01449 {
01450 public:
01452     TiXmlHandle( TiXmlNode* _node )                 { this->node = _node; }
01454     TiXmlHandle( const TiXmlHandle& ref )           { this->node = ref.node; }
01455     TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01456 
01458     TiXmlHandle FirstChild() const;
01460     TiXmlHandle FirstChild( const char * value ) const;
01462     TiXmlHandle FirstChildElement() const;
01464     TiXmlHandle FirstChildElement( const char * value ) const;
01465 
01469     TiXmlHandle Child( const char* value, int index ) const;
01473     TiXmlHandle Child( int index ) const;
01478     TiXmlHandle ChildElement( const char* value, int index ) const;
01483     TiXmlHandle ChildElement( int index ) const;
01484 
01485     #ifdef TIXML_USE_STL
01486     TiXmlHandle FirstChild( const std::string& _value ) const               { return FirstChild( _value.c_str() ); }
01487     TiXmlHandle FirstChildElement( const std::string& _value ) const        { return FirstChildElement( _value.c_str() ); }
01488 
01489     TiXmlHandle Child( const std::string& _value, int index ) const         { return Child( _value.c_str(), index ); }
01490     TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01491     #endif
01492 
01494     TiXmlNode* Node() const         { return node; } 
01496     TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01498     TiXmlText* Text() const         { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01500     TiXmlUnknown* Unknown() const           { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01501 
01502 private:
01503     TiXmlNode* node;
01504 };
01505 
01506 #ifdef _MSC_VER
01507 #pragma warning( pop )
01508 #endif
01509 
01510 #endif
01511 

Generated on Sat Oct 8 14:15:30 2005 for TinyXml by  doxygen 1.4.4