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 #ifdef TIXML_USE_STL
00047         #include <string>
00048         #include <iostream>
00049         #include <sstream>
00050         #define TIXML_STRING    std::string
00051         #define TIXML_ISTREAM   std::istream
00052         #define TIXML_OSTREAM   std::ostream
00053 #else
00054         #include "tinystr.h"
00055         #define TIXML_STRING    TiXmlString
00056         #define TIXML_OSTREAM   TiXmlOutStream
00057 #endif
00058 
00059 // Deprecated library function hell. Compilers want to use the
00060 // new safe versions. This probably doesn't fully address the problem,
00061 // but it gets closer. There are too many compilers for me to fully
00062 // test. If you get compilation troubles, undefine TIXML_SAFE
00063 #define TIXML_SAFE
00064 
00065 #ifdef TIXML_SAFE
00066         #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
00067                 // Microsoft visual studio, version 2005 and higher.
00068                 #define TIXML_SNPRINTF _snprintf_s
00069                 #define TIXML_SNSCANF  _snscanf_s
00070         #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
00071                 // Microsoft visual studio, version 6 and higher.
00072                 //#pragma message( "Using _sn* functions." )
00073                 #define TIXML_SNPRINTF _snprintf
00074                 #define TIXML_SNSCANF  _snscanf
00075         #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00076                 // GCC version 3 and higher.s
00077                 //#warning( "Using sn* functions." )
00078                 #define TIXML_SNPRINTF snprintf
00079                 #define TIXML_SNSCANF  snscanf
00080         #endif
00081 #endif  
00082 
00083 class TiXmlDocument;
00084 class TiXmlElement;
00085 class TiXmlComment;
00086 class TiXmlUnknown;
00087 class TiXmlAttribute;
00088 class TiXmlText;
00089 class TiXmlDeclaration;
00090 class TiXmlParsingData;
00091 
00092 const int TIXML_MAJOR_VERSION = 2;
00093 const int TIXML_MINOR_VERSION = 4;
00094 const int TIXML_PATCH_VERSION = 3;
00095 
00096 /*      Internal structure for tracking location of items 
00097         in the XML file.
00098 */
00099 struct TiXmlCursor
00100 {
00101         TiXmlCursor()           { Clear(); }
00102         void Clear()            { row = col = -1; }
00103 
00104         int row;        // 0 based.
00105         int col;        // 0 based.
00106 };
00107 
00108 
00119 class TiXmlVisitor
00120 {
00121 public:
00122         virtual ~TiXmlVisitor() {}
00123 
00124         virtual bool EnterDocument( const TiXmlDocument& doc, int depth ) = 0;
00125         virtual bool ExitDocument( const TiXmlDocument& doc, int depth ) = 0;
00126 
00127         virtual bool EnterElement( const TiXmlElement& element, const TiXmlAttribute* firstAttribute, int depth ) = 0;
00128         virtual bool ExitElement( const TiXmlElement& element, int depth ) = 0;
00129 
00130         virtual bool VisitDeclaration( const TiXmlDeclaration& declaration, int depth ) = 0;
00131         virtual bool VisitText( const TiXmlText& text, int depth ) = 0;
00132         virtual bool VisitComment( const TiXmlComment& comment, int depth ) = 0;
00133         virtual bool VisitUnknown( const TiXmlUnknown& unknown, int depth ) = 0;
00134 };
00135 
00136 // Only used by Attribute::Query functions
00137 enum 
00138 { 
00139         TIXML_SUCCESS,
00140         TIXML_NO_ATTRIBUTE,
00141         TIXML_WRONG_TYPE
00142 };
00143 
00144 
00145 // Used by the parsing routines.
00146 enum TiXmlEncoding
00147 {
00148         TIXML_ENCODING_UNKNOWN,
00149         TIXML_ENCODING_UTF8,
00150         TIXML_ENCODING_LEGACY
00151 };
00152 
00153 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00154 
00177 class TiXmlBase
00178 {
00179         friend class TiXmlNode;
00180         friend class TiXmlElement;
00181         friend class TiXmlDocument;
00182 
00183 public:
00184         TiXmlBase()     :       userData(0)             {}
00185         virtual ~TiXmlBase()                    {}
00186 
00196         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const = 0;
00197 
00204         static void SetCondenseWhiteSpace( bool condense )              { condenseWhiteSpace = condense; }
00205 
00207         static bool IsWhiteSpaceCondensed()                                             { return condenseWhiteSpace; }
00208 
00227         int Row() const                 { return location.row + 1; }
00228         int Column() const              { return location.col + 1; }    
00229 
00230         void  SetUserData( void* user )                 { userData = user; }    
00231         void* GetUserData()                                             { return userData; }    
00232         const void* GetUserData() const                 { return userData; }    
00233 
00234         // Table that returs, for a given lead byte, the total number of bytes
00235         // in the UTF-8 sequence.
00236         static const int utf8ByteTable[256];
00237 
00238         virtual const char* Parse(      const char* p, 
00239                                                                 TiXmlParsingData* data, 
00240                                                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00241 
00242         enum
00243         {
00244                 TIXML_NO_ERROR = 0,
00245                 TIXML_ERROR,
00246                 TIXML_ERROR_OPENING_FILE,
00247                 TIXML_ERROR_OUT_OF_MEMORY,
00248                 TIXML_ERROR_PARSING_ELEMENT,
00249                 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00250                 TIXML_ERROR_READING_ELEMENT_VALUE,
00251                 TIXML_ERROR_READING_ATTRIBUTES,
00252                 TIXML_ERROR_PARSING_EMPTY,
00253                 TIXML_ERROR_READING_END_TAG,
00254                 TIXML_ERROR_PARSING_UNKNOWN,
00255                 TIXML_ERROR_PARSING_COMMENT,
00256                 TIXML_ERROR_PARSING_DECLARATION,
00257                 TIXML_ERROR_DOCUMENT_EMPTY,
00258                 TIXML_ERROR_EMBEDDED_NULL,
00259                 TIXML_ERROR_PARSING_CDATA,
00260                 TIXML_ERROR_DOCUMENT_TOP_ONLY,
00261 
00262                 TIXML_ERROR_STRING_COUNT
00263         };
00264 
00265 protected:
00266 
00267         // See STL_STRING_BUG
00268         // Utility class to overcome a bug.
00269         class StringToBuffer
00270         {
00271           public:
00272                 StringToBuffer( const TIXML_STRING& str );
00273                 ~StringToBuffer();
00274                 char* buffer;
00275         };
00276 
00277         static const char*      SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00278         inline static bool      IsWhiteSpace( char c )          
00279         { 
00280                 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
00281         }
00282         inline static bool      IsWhiteSpace( int c )
00283         {
00284                 if ( c < 256 )
00285                         return IsWhiteSpace( (char) c );
00286                 return false;   // Again, only truly correct for English/Latin...but usually works.
00287         }
00288 
00289         virtual void StreamOut (TIXML_OSTREAM *) const = 0;
00290 
00291         inline static void DPRINT( FILE* cfile, TIXML_STRING* str, const char* const v ) {
00292                 if ( cfile ) fprintf( cfile, v );
00293                 if ( str )   (*str) += v;
00294         }
00295 
00296         #ifdef TIXML_USE_STL
00297             static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00298             static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00299         #endif
00300 
00301         /*      Reads an XML name into the string provided. Returns
00302                 a pointer just past the last character of the name,
00303                 or 0 if the function has an error.
00304         */
00305         static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00306 
00307         /*      Reads text. Returns a pointer past the given end tag.
00308                 Wickedly complex options, but it keeps the (sensitive) code in one place.
00309         */
00310         static const char* ReadText(    const char* in,                         // where to start
00311                                                                         TIXML_STRING* text,                     // the string read
00312                                                                         bool ignoreWhiteSpace,          // whether to keep the white space
00313                                                                         const char* endTag,                     // what ends this text
00314                                                                         bool ignoreCase,                        // whether to ignore case in the end tag
00315                                                                         TiXmlEncoding encoding );       // the current encoding
00316 
00317         // If an entity has been found, transform it into a character.
00318         static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00319 
00320         // Get a character, while interpreting entities.
00321         // The length can be from 0 to 4 bytes.
00322         inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00323         {
00324                 assert( p );
00325                 if ( encoding == TIXML_ENCODING_UTF8 )
00326                 {
00327                         *length = utf8ByteTable[ *((const unsigned char*)p) ];
00328                         assert( *length >= 0 && *length < 5 );
00329                 }
00330                 else
00331                 {
00332                         *length = 1;
00333                 }
00334 
00335                 if ( *length == 1 )
00336                 {
00337                         if ( *p == '&' )
00338                                 return GetEntity( p, _value, length, encoding );
00339                         *_value = *p;
00340                         return p+1;
00341                 }
00342                 else if ( *length )
00343                 {
00344                         //strncpy( _value, p, *length );        // lots of compilers don't like this function (unsafe),
00345                                                                                                 // and the null terminator isn't needed
00346                         for( int i=0; p[i] && i<*length; ++i ) {
00347                                 _value[i] = p[i];
00348                         }
00349                         return p + (*length);
00350                 }
00351                 else
00352                 {
00353                         // Not valid text.
00354                         return 0;
00355                 }
00356         }
00357 
00358         // Puts a string to a stream, expanding entities as it goes.
00359         // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00360         static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00361 
00362         static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00363 
00364         // Return true if the next characters in the stream are any of the endTag sequences.
00365         // Ignore case only works for english, and should only be relied on when comparing
00366         // to English words: StringEqual( p, "version", true ) is fine.
00367         static bool StringEqual(        const char* p,
00368                                                                 const char* endTag,
00369                                                                 bool ignoreCase,
00370                                                                 TiXmlEncoding encoding );
00371 
00372         static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00373 
00374         TiXmlCursor location;
00375 
00377         void*                   userData;
00378         
00379         // None of these methods are reliable for any language except English.
00380         // Good for approximation, not great for accuracy.
00381         static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00382         static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00383         inline static int ToLower( int v, TiXmlEncoding encoding )
00384         {
00385                 if ( encoding == TIXML_ENCODING_UTF8 )
00386                 {
00387                         if ( v < 128 ) return tolower( v );
00388                         return v;
00389                 }
00390                 else
00391                 {
00392                         return tolower( v );
00393                 }
00394         }
00395         static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00396 
00397 private:
00398         TiXmlBase( const TiXmlBase& );                          // not implemented.
00399         void operator=( const TiXmlBase& base );        // not allowed.
00400 
00401         struct Entity
00402         {
00403                 const char*     str;
00404                 unsigned int    strLength;
00405                 char                chr;
00406         };
00407         enum
00408         {
00409                 NUM_ENTITY = 5,
00410                 MAX_ENTITY_LENGTH = 6
00411 
00412         };
00413         static Entity entity[ NUM_ENTITY ];
00414         static bool condenseWhiteSpace;
00415 };
00416 
00417 
00424 class TiXmlNode : public TiXmlBase
00425 {
00426         friend class TiXmlDocument;
00427         friend class TiXmlElement;
00428 
00429 public:
00430         #ifdef TIXML_USE_STL    
00431 
00435             friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00436 
00453             friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00454 
00456                 friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00457 
00458         #else
00459             // Used internally, not part of the public API.
00460             friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00461         #endif
00462 
00466         enum NodeType
00467         {
00468                 DOCUMENT,
00469                 ELEMENT,
00470                 COMMENT,
00471                 UNKNOWN,
00472                 TEXT,
00473                 DECLARATION,
00474                 TYPECOUNT
00475         };
00476 
00477         virtual ~TiXmlNode();
00478 
00491         const char *Value() const { return value.c_str (); }
00492 
00493     #ifdef TIXML_USE_STL
00494 
00498         const std::string& ValueStr() const { return value; }
00499         #endif
00500 
00510         void SetValue(const char * _value) { value = _value;}
00511 
00512     #ifdef TIXML_USE_STL
00514         void SetValue( const std::string& _value )      { value = _value; }
00515         #endif
00516 
00518         void Clear();
00519 
00521         TiXmlNode* Parent()                                                     { return parent; }
00522         const TiXmlNode* Parent() const                         { return parent; }
00523 
00524         const TiXmlNode* FirstChild()   const   { return firstChild; }          
00525         TiXmlNode* FirstChild()                                 { return firstChild; }
00526         const TiXmlNode* FirstChild( const char * value ) const;                        
00527         TiXmlNode* FirstChild( const char * value );                                            
00528 
00529         const TiXmlNode* LastChild() const      { return lastChild; }           
00530         TiXmlNode* LastChild()  { return lastChild; }
00531         const TiXmlNode* LastChild( const char * value ) const;                 
00532         TiXmlNode* LastChild( const char * value );     
00533 
00534     #ifdef TIXML_USE_STL
00535         const TiXmlNode* FirstChild( const std::string& _value ) const  {       return FirstChild (_value.c_str ());    }       
00536         TiXmlNode* FirstChild( const std::string& _value )                              {       return FirstChild (_value.c_str ());    }       
00537         const TiXmlNode* LastChild( const std::string& _value ) const   {       return LastChild (_value.c_str ());     }       
00538         TiXmlNode* LastChild( const std::string& _value )                               {       return LastChild (_value.c_str ());     }       
00539         #endif
00540 
00557         const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00558         TiXmlNode* IterateChildren( TiXmlNode* previous );
00559 
00561         const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00562         TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00563 
00564     #ifdef TIXML_USE_STL
00565         const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {       return IterateChildren (_value.c_str (), previous);     }       
00566         TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous);     }       
00567         #endif
00568 
00572         TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00573 
00574 
00584         TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00585 
00589         TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00590 
00594         TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00595 
00599         TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00600 
00602         bool RemoveChild( TiXmlNode* removeThis );
00603 
00605         const TiXmlNode* PreviousSibling() const                        { return prev; }
00606         TiXmlNode* PreviousSibling()                                            { return prev; }
00607 
00609         const TiXmlNode* PreviousSibling( const char * ) const;
00610         TiXmlNode* PreviousSibling( const char * );
00611 
00612     #ifdef TIXML_USE_STL
00613         const TiXmlNode* PreviousSibling( const std::string& _value ) const     {       return PreviousSibling (_value.c_str ());       }       
00614         TiXmlNode* PreviousSibling( const std::string& _value )                         {       return PreviousSibling (_value.c_str ());       }       
00615         const TiXmlNode* NextSibling( const std::string& _value) const          {       return NextSibling (_value.c_str ());   }       
00616         TiXmlNode* NextSibling( const std::string& _value)                                      {       return NextSibling (_value.c_str ());   }       
00617         #endif
00618 
00620         const TiXmlNode* NextSibling() const                            { return next; }
00621         TiXmlNode* NextSibling()                                                        { return next; }
00622 
00624         const TiXmlNode* NextSibling( const char * ) const;
00625         TiXmlNode* NextSibling( const char * );
00626 
00631         const TiXmlElement* NextSiblingElement() const;
00632         TiXmlElement* NextSiblingElement();
00633 
00638         const TiXmlElement* NextSiblingElement( const char * ) const;
00639         TiXmlElement* NextSiblingElement( const char * );
00640 
00641     #ifdef TIXML_USE_STL
00642         const TiXmlElement* NextSiblingElement( const std::string& _value) const        {       return NextSiblingElement (_value.c_str ());    }       
00643         TiXmlElement* NextSiblingElement( const std::string& _value)                            {       return NextSiblingElement (_value.c_str ());    }       
00644         #endif
00645 
00647         const TiXmlElement* FirstChildElement() const;
00648         TiXmlElement* FirstChildElement();
00649 
00651         const TiXmlElement* FirstChildElement( const char * value ) const;
00652         TiXmlElement* FirstChildElement( const char * value );
00653 
00654     #ifdef TIXML_USE_STL
00655         const TiXmlElement* FirstChildElement( const std::string& _value ) const        {       return FirstChildElement (_value.c_str ());     }       
00656         TiXmlElement* FirstChildElement( const std::string& _value )                            {       return FirstChildElement (_value.c_str ());     }       
00657         #endif
00658 
00663         int Type() const        { return type; }
00664 
00668         const TiXmlDocument* GetDocument() const;
00669         TiXmlDocument* GetDocument();
00670 
00672         bool NoChildren() const                                         { return !firstChild; }
00673 
00674         virtual const TiXmlDocument*    ToDocument()    const { return 0; } 
00675         virtual const TiXmlElement*     ToElement()     const { return 0; } 
00676         virtual const TiXmlComment*     ToComment()     const { return 0; } 
00677         virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } 
00678         virtual const TiXmlText*        ToText()        const { return 0; } 
00679         virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } 
00680 
00681         virtual TiXmlDocument*          ToDocument()    { return 0; } 
00682         virtual TiXmlElement*           ToElement()         { return 0; } 
00683         virtual TiXmlComment*           ToComment()     { return 0; } 
00684         virtual TiXmlUnknown*           ToUnknown()         { return 0; } 
00685         virtual TiXmlText*                  ToText()        { return 0; } 
00686         virtual TiXmlDeclaration*       ToDeclaration() { return 0; } 
00687 
00691         virtual TiXmlNode* Clone() const = 0;
00692 
00693         virtual bool Accept( TiXmlVisitor* visitor, int depth=0 ) const = 0;
00694 
00695 protected:
00696         TiXmlNode( NodeType _type );
00697 
00698         // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00699         // and the assignment operator.
00700         void CopyTo( TiXmlNode* target ) const;
00701 
00702         #ifdef TIXML_USE_STL
00703             // The real work of the input operator.
00704             virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00705         #endif
00706 
00707         // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00708         TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00709 
00710         TiXmlNode*              parent;
00711         NodeType                type;
00712 
00713         TiXmlNode*              firstChild;
00714         TiXmlNode*              lastChild;
00715 
00716         TIXML_STRING    value;
00717 
00718         TiXmlNode*              prev;
00719         TiXmlNode*              next;
00720 
00721 private:
00722         TiXmlNode( const TiXmlNode& );                          // not implemented.
00723         void operator=( const TiXmlNode& base );        // not allowed.
00724 };
00725 
00726 
00734 class TiXmlAttribute : public TiXmlBase
00735 {
00736         friend class TiXmlAttributeSet;
00737 
00738 public:
00740         TiXmlAttribute() : TiXmlBase()
00741         {
00742                 document = 0;
00743                 prev = next = 0;
00744         }
00745 
00746         #ifdef TIXML_USE_STL
00748         TiXmlAttribute( const std::string& _name, const std::string& _value )
00749         {
00750                 name = _name;
00751                 value = _value;
00752                 document = 0;
00753                 prev = next = 0;
00754         }
00755         #endif
00756 
00758         TiXmlAttribute( const char * _name, const char * _value )
00759         {
00760                 name = _name;
00761                 value = _value;
00762                 document = 0;
00763                 prev = next = 0;
00764         }
00765 
00766         const char*             Name()  const           { return name.c_str(); }                
00767         const char*             Value() const           { return value.c_str(); }               
00768         #ifdef TIXML_USE_STL
00769         const std::string& ValueStr() const     { return value; }                               
00770         #endif
00771         int                             IntValue() const;                                                                       
00772         double                  DoubleValue() const;                                                            
00773 
00774         // Get the tinyxml string representation
00775         const TIXML_STRING& NameTStr() const { return name; }
00776 
00786         int QueryIntValue( int* _value ) const;
00788         int QueryDoubleValue( double* _value ) const;
00789 
00790         void SetName( const char* _name )       { name = _name; }                               
00791         void SetValue( const char* _value )     { value = _value; }                             
00792 
00793         void SetIntValue( int _value );                                                                         
00794         void SetDoubleValue( double _value );                                                           
00795 
00796     #ifdef TIXML_USE_STL
00798         void SetName( const std::string& _name )        { name = _name; }       
00800         void SetValue( const std::string& _value )      { value = _value; }
00801         #endif
00802 
00804         const TiXmlAttribute* Next() const;
00805         TiXmlAttribute* Next();
00807         const TiXmlAttribute* Previous() const;
00808         TiXmlAttribute* Previous();
00809 
00810         bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00811         bool operator<( const TiXmlAttribute& rhs )      const { return name < rhs.name; }
00812         bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00813 
00814         /*      Attribute parsing starts: first letter of the name
00815                                                  returns: the next char after the value end quote
00816         */
00817         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00818 
00819         // Prints this Attribute to a FILE stream.
00820         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
00821 
00822         virtual void StreamOut( TIXML_OSTREAM * out ) const;
00823         // [internal use]
00824         // Set the document pointer so the attribute can report errors.
00825         void SetDocument( TiXmlDocument* doc )  { document = doc; }
00826 
00827 private:
00828         TiXmlAttribute( const TiXmlAttribute& );                                // not implemented.
00829         void operator=( const TiXmlAttribute& base );   // not allowed.
00830 
00831         TiXmlDocument*  document;       // A pointer back to a document, for error reporting.
00832         TIXML_STRING name;
00833         TIXML_STRING value;
00834         TiXmlAttribute* prev;
00835         TiXmlAttribute* next;
00836 };
00837 
00838 
00839 /*      A class used to manage a group of attributes.
00840         It is only used internally, both by the ELEMENT and the DECLARATION.
00841         
00842         The set can be changed transparent to the Element and Declaration
00843         classes that use it, but NOT transparent to the Attribute
00844         which has to implement a next() and previous() method. Which makes
00845         it a bit problematic and prevents the use of STL.
00846 
00847         This version is implemented with circular lists because:
00848                 - I like circular lists
00849                 - it demonstrates some independence from the (typical) doubly linked list.
00850 */
00851 class TiXmlAttributeSet
00852 {
00853 public:
00854         TiXmlAttributeSet();
00855         ~TiXmlAttributeSet();
00856 
00857         void Add( TiXmlAttribute* attribute );
00858         void Remove( TiXmlAttribute* attribute );
00859 
00860         const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00861         TiXmlAttribute* First()                                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00862         const TiXmlAttribute* Last() const              { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00863         TiXmlAttribute* Last()                                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00864 
00865         const TiXmlAttribute*   Find( const TIXML_STRING& name ) const;
00866         TiXmlAttribute* Find( const TIXML_STRING& name );
00867 
00868 private:
00869         //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00870         //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00871         TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00872         void operator=( const TiXmlAttributeSet& );     // not allowed (as TiXmlAttribute)
00873 
00874         TiXmlAttribute sentinel;
00875 };
00876 
00877 
00882 class TiXmlElement : public TiXmlNode
00883 {
00884 public:
00886         TiXmlElement (const char * in_value);
00887 
00888         #ifdef TIXML_USE_STL
00890         TiXmlElement( const std::string& _value );
00891         #endif
00892 
00893         TiXmlElement( const TiXmlElement& );
00894 
00895         void operator=( const TiXmlElement& base );
00896 
00897         virtual ~TiXmlElement();
00898 
00902         const char* Attribute( const char* name ) const;
00903 
00910         const char* Attribute( const char* name, int* i ) const;
00911 
00918         const char* Attribute( const char* name, double* d ) const;
00919 
00927         int QueryIntAttribute( const char* name, int* _value ) const;
00929         int QueryDoubleAttribute( const char* name, double* _value ) const;
00931         int QueryFloatAttribute( const char* name, float* _value ) const {
00932                 double d;
00933                 int result = QueryDoubleAttribute( name, &d );
00934                 if ( result == TIXML_SUCCESS ) {
00935                         *_value = (float)d;
00936                 }
00937                 return result;
00938         }
00939     #ifdef TIXML_USE_STL
00940 
00946         template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
00947         {
00948                 const TiXmlAttribute* node = attributeSet.Find( name );
00949                 if ( !node )
00950                         return TIXML_NO_ATTRIBUTE;
00951 
00952                 std::stringstream sstream( node->ValueStr() );
00953                 sstream >> *outValue;
00954                 if ( !sstream.fail() )
00955                         return TIXML_SUCCESS;
00956                 return TIXML_WRONG_TYPE;
00957         }
00958         #endif
00959 
00963         void SetAttribute( const char* name, const char * _value );
00964 
00965     #ifdef TIXML_USE_STL
00966         const char* Attribute( const std::string& name ) const                          { return Attribute( name.c_str() ); }
00967         const char* Attribute( const std::string& name, int* i ) const          { return Attribute( name.c_str(), i ); }
00968         const char* Attribute( const std::string& name, double* d ) const       { return Attribute( name.c_str(), d ); }
00969         int QueryIntAttribute( const std::string& name, int* _value ) const     { return QueryIntAttribute( name.c_str(), _value ); }
00970         int QueryDoubleAttribute( const std::string& name, double* _value ) const { return QueryDoubleAttribute( name.c_str(), _value ); }
00971 
00973         void SetAttribute( const std::string& name, const std::string& _value );
00975         void SetAttribute( const std::string& name, int _value );
00976         #endif
00977 
00981         void SetAttribute( const char * name, int value );
00982 
00986         void SetDoubleAttribute( const char * name, double value );
00987 
00990         void RemoveAttribute( const char * name );
00991     #ifdef TIXML_USE_STL
00992         void RemoveAttribute( const std::string& name ) {       RemoveAttribute (name.c_str ());        }       
00993         #endif
00994 
00995         const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }                
00996         TiXmlAttribute* FirstAttribute()                                { return attributeSet.First(); }
00997         const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }         
00998         TiXmlAttribute* LastAttribute()                                 { return attributeSet.Last(); }
00999 
01032         const char* GetText() const;
01033 
01035         virtual TiXmlNode* Clone() const;
01036         // Print the Element to a FILE stream.
01037         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
01038 
01039         /*      Attribtue parsing starts: next char past '<'
01040                                                  returns: next char past '>'
01041         */
01042         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01043 
01044         virtual const TiXmlElement*     ToElement()     const { return this; } 
01045         virtual TiXmlElement*           ToElement()               { return this; } 
01046 
01049         virtual bool Accept( TiXmlVisitor* visitor, int depth = 0 ) const;
01050 
01051 protected:
01052 
01053         void CopyTo( TiXmlElement* target ) const;
01054         void ClearThis();       // like clear, but initializes 'this' object as well
01055 
01056         // Used to be public [internal use]
01057         #ifdef TIXML_USE_STL
01058             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01059         #endif
01060         virtual void StreamOut( TIXML_OSTREAM * out ) const;
01061 
01062         /*      [internal use]
01063                 Reads the "value" of the element -- another element, or text.
01064                 This should terminate with the current end tag.
01065         */
01066         const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01067 
01068 private:
01069 
01070         TiXmlAttributeSet attributeSet;
01071 };
01072 
01073 
01076 class TiXmlComment : public TiXmlNode
01077 {
01078 public:
01080         TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01081         TiXmlComment( const TiXmlComment& );
01082         void operator=( const TiXmlComment& base );
01083 
01084         virtual ~TiXmlComment() {}
01085 
01087         virtual TiXmlNode* Clone() const;
01088         // Write this Comment to a FILE stream.
01089         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
01090 
01091         /*      Attribtue parsing starts: at the ! of the !--
01092                                                  returns: next char past '>'
01093         */
01094         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01095 
01096         virtual const TiXmlComment*  ToComment() const { return this; } 
01097         virtual TiXmlComment*  ToComment() { return this; } 
01098 
01101         virtual bool Accept( TiXmlVisitor* visitor, int depth = 0 ) const;
01102 
01103 protected:
01104         void CopyTo( TiXmlComment* target ) const;
01105 
01106         // used to be public
01107         #ifdef TIXML_USE_STL
01108             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01109         #endif
01110         virtual void StreamOut( TIXML_OSTREAM * out ) const;
01111 
01112 private:
01113 
01114 };
01115 
01116 
01122 class TiXmlText : public TiXmlNode
01123 {
01124         friend class TiXmlElement;
01125 public:
01130         TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01131         {
01132                 SetValue( initValue );
01133                 cdata = false;
01134         }
01135         virtual ~TiXmlText() {}
01136 
01137         #ifdef TIXML_USE_STL
01139         TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01140         {
01141                 SetValue( initValue );
01142                 cdata = false;
01143         }
01144         #endif
01145 
01146         TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )       { copy.CopyTo( this ); }
01147         void operator=( const TiXmlText& base )                                                         { base.CopyTo( this ); }
01148 
01149         // Write this text object to a FILE stream.
01150         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
01151 
01153         bool CDATA()                                    { return cdata; }
01155         void SetCDATA( bool _cdata )    { cdata = _cdata; }
01156 
01157         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01158 
01159         virtual const TiXmlText* ToText() const { return this; } 
01160         virtual TiXmlText*       ToText()       { return this; } 
01161 
01164         virtual bool Accept( TiXmlVisitor* content, int depth = 0 ) const;
01165 
01166 protected :
01168         virtual TiXmlNode* Clone() const;
01169         void CopyTo( TiXmlText* target ) const;
01170 
01171         virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01172         bool Blank() const;     // returns true if all white space and new lines
01173         // [internal use]
01174         #ifdef TIXML_USE_STL
01175             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01176         #endif
01177 
01178 private:
01179         bool cdata;                     // true if this should be input and output as a CDATA style text element
01180 };
01181 
01182 
01196 class TiXmlDeclaration : public TiXmlNode
01197 {
01198 public:
01200         TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01201 
01202 #ifdef TIXML_USE_STL
01204         TiXmlDeclaration(       const std::string& _version,
01205                                                 const std::string& _encoding,
01206                                                 const std::string& _standalone );
01207 #endif
01208 
01210         TiXmlDeclaration(       const char* _version,
01211                                                 const char* _encoding,
01212                                                 const char* _standalone );
01213 
01214         TiXmlDeclaration( const TiXmlDeclaration& copy );
01215         void operator=( const TiXmlDeclaration& copy );
01216 
01217         virtual ~TiXmlDeclaration()     {}
01218 
01220         const char *Version() const                     { return version.c_str (); }
01222         const char *Encoding() const            { return encoding.c_str (); }
01224         const char *Standalone() const          { return standalone.c_str (); }
01225 
01227         virtual TiXmlNode* Clone() const;
01228         // Print this declaration to a FILE stream.
01229         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
01230 
01231         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01232 
01233         virtual const TiXmlDeclaration* ToDeclaration() const { return this; } 
01234         virtual TiXmlDeclaration*       ToDeclaration()       { return this; } 
01235 
01238         virtual bool Accept( TiXmlVisitor* visitor, int depth = 0 ) const;
01239 
01240 protected:
01241         void CopyTo( TiXmlDeclaration* target ) const;
01242         // used to be public
01243         #ifdef TIXML_USE_STL
01244             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01245         #endif
01246         virtual void StreamOut ( TIXML_OSTREAM * out) const;
01247 
01248 private:
01249 
01250         TIXML_STRING version;
01251         TIXML_STRING encoding;
01252         TIXML_STRING standalone;
01253 };
01254 
01255 
01263 class TiXmlUnknown : public TiXmlNode
01264 {
01265 public:
01266         TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )        {}
01267         virtual ~TiXmlUnknown() {}
01268 
01269         TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )              { copy.CopyTo( this ); }
01270         void operator=( const TiXmlUnknown& copy )                                                                              { copy.CopyTo( this ); }
01271 
01273         virtual TiXmlNode* Clone() const;
01274         // Print this Unknown to a FILE stream.
01275         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
01276 
01277         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01278 
01279         virtual const TiXmlUnknown*     ToUnknown()     const { return this; } 
01280         virtual TiXmlUnknown*           ToUnknown()         { return this; } 
01281 
01284         virtual bool Accept( TiXmlVisitor* content, int depth = 0 ) const;
01285 
01286 protected:
01287         void CopyTo( TiXmlUnknown* target ) const;
01288 
01289         #ifdef TIXML_USE_STL
01290             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01291         #endif
01292         virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01293 
01294 private:
01295 
01296 };
01297 
01298 
01303 class TiXmlDocument : public TiXmlNode
01304 {
01305 public:
01307         TiXmlDocument();
01309         TiXmlDocument( const char * documentName );
01310 
01311         #ifdef TIXML_USE_STL
01313         TiXmlDocument( const std::string& documentName );
01314         #endif
01315 
01316         TiXmlDocument( const TiXmlDocument& copy );
01317         void operator=( const TiXmlDocument& copy );
01318 
01319         virtual ~TiXmlDocument() {}
01320 
01325         bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01327         bool SaveFile() const;
01329         bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01331         bool SaveFile( const char * filename ) const;
01337         bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01339         bool SaveFile( FILE* ) const;
01340 
01341         #ifdef TIXML_USE_STL
01342         bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )                   
01343         {
01344                 StringToBuffer f( filename );
01345                 return ( f.buffer && LoadFile( f.buffer, encoding ));
01346         }
01347         bool SaveFile( const std::string& filename ) const              
01348         {
01349                 StringToBuffer f( filename );
01350                 return ( f.buffer && SaveFile( f.buffer ));
01351         }
01352         #endif
01353 
01358         virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01359 
01364         const TiXmlElement* RootElement() const         { return FirstChildElement(); }
01365         TiXmlElement* RootElement()                                     { return FirstChildElement(); }
01366 
01372         bool Error() const                                              { return error; }
01373 
01375         const char * ErrorDesc() const  { return errorDesc.c_str (); }
01376 
01380         int ErrorId()   const                           { return errorId; }
01381 
01389         int ErrorRow()  { return errorLocation.row+1; }
01390         int ErrorCol()  { return errorLocation.col+1; } 
01391 
01416         void SetTabSize( int _tabsize )         { tabsize = _tabsize; }
01417 
01418         int TabSize() const     { return tabsize; }
01419 
01423         void ClearError()                                               {       error = false; 
01424                                                                                                 errorId = 0; 
01425                                                                                                 errorDesc = ""; 
01426                                                                                                 errorLocation.row = errorLocation.col = 0; 
01427                                                                                                 //errorLocation.last = 0; 
01428                                                                                         }
01429 
01431         void Print() const                                              { Print( stdout, 0 ); }
01432 
01437         char* PrintToMemory() const; 
01438 
01440         virtual void Print( FILE* cfile, int depth = 0, TIXML_STRING* str = 0 ) const;
01441         // [internal use]
01442         void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01443 
01444         virtual const TiXmlDocument*    ToDocument()    const { return this; } 
01445         virtual TiXmlDocument*          ToDocument()          { return this; } 
01446 
01449         virtual bool Accept( TiXmlVisitor* content, int depth = 0 ) const;
01450 
01451 protected :
01452         virtual void StreamOut ( TIXML_OSTREAM * out) const;
01453         // [internal use]
01454         virtual TiXmlNode* Clone() const;
01455         #ifdef TIXML_USE_STL
01456             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01457         #endif
01458 
01459 private:
01460         void CopyTo( TiXmlDocument* target ) const;
01461 
01462         bool error;
01463         int  errorId;
01464         TIXML_STRING errorDesc;
01465         int tabsize;
01466         TiXmlCursor errorLocation;
01467         bool useMicrosoftBOM;           // the UTF-8 BOM were found when read. Note this, and try to write.
01468 };
01469 
01470 
01551 class TiXmlHandle
01552 {
01553 public:
01555         TiXmlHandle( TiXmlNode* _node )                                 { this->node = _node; }
01557         TiXmlHandle( const TiXmlHandle& ref )                   { this->node = ref.node; }
01558         TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01559 
01561         TiXmlHandle FirstChild() const;
01563         TiXmlHandle FirstChild( const char * value ) const;
01565         TiXmlHandle FirstChildElement() const;
01567         TiXmlHandle FirstChildElement( const char * value ) const;
01568 
01572         TiXmlHandle Child( const char* value, int index ) const;
01576         TiXmlHandle Child( int index ) const;
01581         TiXmlHandle ChildElement( const char* value, int index ) const;
01586         TiXmlHandle ChildElement( int index ) const;
01587 
01588         #ifdef TIXML_USE_STL
01589         TiXmlHandle FirstChild( const std::string& _value ) const                               { return FirstChild( _value.c_str() ); }
01590         TiXmlHandle FirstChildElement( const std::string& _value ) const                { return FirstChildElement( _value.c_str() ); }
01591 
01592         TiXmlHandle Child( const std::string& _value, int index ) const                 { return Child( _value.c_str(), index ); }
01593         TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01594         #endif
01595 
01598         TiXmlNode* ToNode() const                       { return node; } 
01601         TiXmlElement* ToElement() const         { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01604         TiXmlText* ToText() const                       { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01607         TiXmlUnknown* ToUnknown() const         { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01608 
01612         TiXmlNode* Node() const                 { return ToNode(); } 
01616         TiXmlElement* Element() const   { return ToElement(); }
01620         TiXmlText* Text() const                 { return ToText(); }
01624         TiXmlUnknown* Unknown() const   { return ToUnknown(); }
01625 
01626 private:
01627         TiXmlNode* node;
01628 };
01629 
01630 #ifdef _MSC_VER
01631 #pragma warning( pop )
01632 #endif
01633 
01634 #endif
01635 

Generated on Thu Jul 20 20:45:32 2006 for Kyra by  doxygen 1.4.7