tinyxml.h

Go to the documentation of this file.
00001 /*
00002 www.sourceforge.net/projects/tinyxml
00003 Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
00004 
00005 This software is provided 'as-is', without any express or implied
00006 warranty. In no event will the authors be held liable for any
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any
00010 purpose, including commercial applications, and to alter it and
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must
00014 not claim that you wrote the original software. If you use this
00015 software in a product, an acknowledgment in the product documentation
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source
00022 distribution.
00023 */
00024 
00025 
00026 #ifndef TINYXML_INCLUDED
00027 #define TINYXML_INCLUDED
00028 
00037 #ifdef _MSC_VER
00038 #pragma warning( push )
00039 #pragma warning( disable : 4530 )
00040 #pragma warning( disable : 4786 )
00041 #endif
00042 
00043 #include <ctype.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <assert.h>
00048 
00049 // Help out windows:
00050 #if defined( _DEBUG ) && !defined( DEBUG )
00051 #define DEBUG
00052 #endif
00053 
00054 #ifdef TIXML_USE_STL
00055     #include <string>
00056     #include <iostream>
00057     #define TIXML_STRING    std::string
00058     #define TIXML_ISTREAM   std::istream
00059     #define TIXML_OSTREAM   std::ostream
00060 #else
00061     #include "tinystr.h"
00062     #define TIXML_STRING    TiXmlString
00063     #define TIXML_OSTREAM   TiXmlOutStream
00064 #endif
00065 
00066 // Deprecated library function hell. Compilers want to use the
00067 // new safe versions. This probably doesn't fully address the problem,
00068 // but it gets closer. There are too many compilers for me to fully
00069 // test. If you get compilation troubles, undefine TIXML_SAFE
00070 
00071 #define TIXML_SAFE      // TinyXml isn't fully buffer overrun protected, safe code. This is work in progress.
00072 #ifdef TIXML_SAFE
00073     #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
00074         // Microsoft visual studio, version 2005 and higher.
00075         #define TIXML_SNPRINTF _snprintf_s
00076         #define TIXML_SNSCANF  _snscanf_s
00077     #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
00078         // Microsoft visual studio, version 6 and higher.
00079         //#pragma message( "Using _sn* functions." )
00080         #define TIXML_SNPRINTF _snprintf
00081         #define TIXML_SNSCANF  _snscanf
00082     #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00083         // GCC version 3 and higher.s
00084         //#warning( "Using sn* functions." )
00085         #define TIXML_SNPRINTF snprintf
00086         #define TIXML_SNSCANF  snscanf
00087     #endif
00088 #endif  
00089 
00090 class TiXmlDocument;
00091 class TiXmlElement;
00092 class TiXmlComment;
00093 class TiXmlUnknown;
00094 class TiXmlAttribute;
00095 class TiXmlText;
00096 class TiXmlDeclaration;
00097 class TiXmlParsingData;
00098 
00099 const int TIXML_MAJOR_VERSION = 2;
00100 const int TIXML_MINOR_VERSION = 4;
00101 const int TIXML_PATCH_VERSION = 3;
00102 
00103 /*  Internal structure for tracking location of items 
00104     in the XML file.
00105 */
00106 struct TiXmlCursor
00107 {
00108     TiXmlCursor()       { Clear(); }
00109     void Clear()        { row = col = -1; }
00110 
00111     int row;    // 0 based.
00112     int col;    // 0 based.
00113 };
00114 
00115 
00116 // Only used by Attribute::Query functions
00117 enum 
00118 { 
00119     TIXML_SUCCESS,
00120     TIXML_NO_ATTRIBUTE,
00121     TIXML_WRONG_TYPE
00122 };
00123 
00124 
00125 // Used by the parsing routines.
00126 enum TiXmlEncoding
00127 {
00128     TIXML_ENCODING_UNKNOWN,
00129     TIXML_ENCODING_UTF8,
00130     TIXML_ENCODING_LEGACY
00131 };
00132 
00133 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00134 
00157 class TiXmlBase
00158 {
00159     friend class TiXmlNode;
00160     friend class TiXmlElement;
00161     friend class TiXmlDocument;
00162 
00163 public:
00164     TiXmlBase() :   userData(0) {}
00165     virtual ~TiXmlBase()                    {}
00166 
00172     virtual void Print( FILE* cfile, int depth ) const = 0;
00173 
00180     static void SetCondenseWhiteSpace( bool condense )      { condenseWhiteSpace = condense; }
00181 
00183     static bool IsWhiteSpaceCondensed()                     { return condenseWhiteSpace; }
00184 
00203     int Row() const         { return location.row + 1; }
00204     int Column() const      { return location.col + 1; }    
00205 
00206     void  SetUserData( void* user )         { userData = user; }
00207     void* GetUserData()                     { return userData; }
00208 
00209     // Table that returs, for a given lead byte, the total number of bytes
00210     // in the UTF-8 sequence.
00211     static const int utf8ByteTable[256];
00212 
00213     virtual const char* Parse(  const char* p, 
00214                                 TiXmlParsingData* data, 
00215                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00216 
00217     enum
00218     {
00219         TIXML_NO_ERROR = 0,
00220         TIXML_ERROR,
00221         TIXML_ERROR_OPENING_FILE,
00222         TIXML_ERROR_OUT_OF_MEMORY,
00223         TIXML_ERROR_PARSING_ELEMENT,
00224         TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00225         TIXML_ERROR_READING_ELEMENT_VALUE,
00226         TIXML_ERROR_READING_ATTRIBUTES,
00227         TIXML_ERROR_PARSING_EMPTY,
00228         TIXML_ERROR_READING_END_TAG,
00229         TIXML_ERROR_PARSING_UNKNOWN,
00230         TIXML_ERROR_PARSING_COMMENT,
00231         TIXML_ERROR_PARSING_DECLARATION,
00232         TIXML_ERROR_DOCUMENT_EMPTY,
00233         TIXML_ERROR_EMBEDDED_NULL,
00234         TIXML_ERROR_PARSING_CDATA,
00235 
00236         TIXML_ERROR_STRING_COUNT
00237     };
00238 
00239 protected:
00240 
00241     // See STL_STRING_BUG
00242     // Utility class to overcome a bug.
00243     class StringToBuffer
00244     {
00245       public:
00246         StringToBuffer( const TIXML_STRING& str );
00247         ~StringToBuffer();
00248         char* buffer;
00249     };
00250 
00251     static const char*  SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00252     inline static bool  IsWhiteSpace( char c )      
00253     { 
00254         return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
00255     }
00256     inline static bool  IsWhiteSpace( int c )
00257     {
00258         if ( c < 256 )
00259             return IsWhiteSpace( (char) c );
00260         return false;   // Again, only truly correct for English/Latin...but usually works.
00261     }
00262 
00263     virtual void StreamOut (TIXML_OSTREAM *) const = 0;
00264 
00265     #ifdef TIXML_USE_STL
00266         static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00267         static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00268     #endif
00269 
00270     /*  Reads an XML name into the string provided. Returns
00271         a pointer just past the last character of the name,
00272         or 0 if the function has an error.
00273     */
00274     static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00275 
00276     /*  Reads text. Returns a pointer past the given end tag.
00277         Wickedly complex options, but it keeps the (sensitive) code in one place.
00278     */
00279     static const char* ReadText(    const char* in,             // where to start
00280                                     TIXML_STRING* text,         // the string read
00281                                     bool ignoreWhiteSpace,      // whether to keep the white space
00282                                     const char* endTag,         // what ends this text
00283                                     bool ignoreCase,            // whether to ignore case in the end tag
00284                                     TiXmlEncoding encoding );   // the current encoding
00285 
00286     // If an entity has been found, transform it into a character.
00287     static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00288 
00289     // Get a character, while interpreting entities.
00290     // The length can be from 0 to 4 bytes.
00291     inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00292     {
00293         assert( p );
00294         if ( encoding == TIXML_ENCODING_UTF8 )
00295         {
00296             *length = utf8ByteTable[ *((unsigned char*)p) ];
00297             assert( *length >= 0 && *length < 5 );
00298         }
00299         else
00300         {
00301             *length = 1;
00302         }
00303 
00304         if ( *length == 1 )
00305         {
00306             if ( *p == '&' )
00307                 return GetEntity( p, _value, length, encoding );
00308             *_value = *p;
00309             return p+1;
00310         }
00311         else if ( *length )
00312         {
00313             //strncpy( _value, p, *length );    // lots of compilers don't like this function (unsafe),
00314                                                 // and the null terminator isn't needed
00315             for( int i=0; p[i] && i<*length; ++i ) {
00316                 _value[i] = p[i];
00317             }
00318             return p + (*length);
00319         }
00320         else
00321         {
00322             // Not valid text.
00323             return 0;
00324         }
00325     }
00326 
00327     // Puts a string to a stream, expanding entities as it goes.
00328     // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00329     static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00330 
00331     static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00332 
00333     // Return true if the next characters in the stream are any of the endTag sequences.
00334     // Ignore case only works for english, and should only be relied on when comparing
00335     // to English words: StringEqual( p, "version", true ) is fine.
00336     static bool StringEqual(    const char* p,
00337                                 const char* endTag,
00338                                 bool ignoreCase,
00339                                 TiXmlEncoding encoding );
00340 
00341     static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00342 
00343     TiXmlCursor location;
00344 
00346     void*           userData;
00347     
00348     // None of these methods are reliable for any language except English.
00349     // Good for approximation, not great for accuracy.
00350     static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00351     static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00352     inline static int ToLower( int v, TiXmlEncoding encoding )
00353     {
00354         if ( encoding == TIXML_ENCODING_UTF8 )
00355         {
00356             if ( v < 128 ) return tolower( v );
00357             return v;
00358         }
00359         else
00360         {
00361             return tolower( v );
00362         }
00363     }
00364     static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00365 
00366 private:
00367     TiXmlBase( const TiXmlBase& );              // not implemented.
00368     void operator=( const TiXmlBase& base );    // not allowed.
00369 
00370     struct Entity
00371     {
00372         const char*     str;
00373         unsigned int    strLength;
00374         char   chr;
00375     };
00376     enum
00377     {
00378 #ifdef TIXML_XHTML // by Tobi
00379         NUM_ENTITY = 14,
00380         MAX_ENTITY_LENGTH = 8
00381 #else
00382         NUM_ENTITY = 5,
00383         MAX_ENTITY_LENGTH = 6
00384 #endif
00385     };
00386     static Entity entity[ NUM_ENTITY ];
00387     static bool condenseWhiteSpace;
00388 };
00389 
00390 
00397 class TiXmlNode : public TiXmlBase
00398 {
00399     friend class TiXmlDocument;
00400     friend class TiXmlElement;
00401 
00402 public:
00403     #ifdef TIXML_USE_STL    
00404 
00408         friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00409 
00426         friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00427 
00429         friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00430 
00431     #else
00432         // Used internally, not part of the public API.
00433         friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00434     #endif
00435 
00439     enum NodeType
00440     {
00441         DOCUMENT,
00442         ELEMENT,
00443         COMMENT,
00444         UNKNOWN,
00445         TEXT,
00446         DECLARATION,
00447         TYPECOUNT
00448     };
00449 
00450     virtual ~TiXmlNode();
00451 
00464     const char *Value() const { return value.c_str (); }
00465 
00466     #ifdef TIXML_USE_STL
00467 
00471     const std::string& ValueStr() const { return value; }
00472     #endif
00473 
00483     void SetValue(const char * _value) { value = _value;}
00484 
00485     #ifdef TIXML_USE_STL
00487     void SetValue( const std::string& _value )  { value = _value; }
00488     #endif
00489 
00491     void Clear();
00492 
00494     TiXmlNode* Parent()                         { return parent; }
00495     const TiXmlNode* Parent() const             { return parent; }
00496 
00497     const TiXmlNode* FirstChild()   const   { return firstChild; }      
00498     TiXmlNode* FirstChild()                 { return firstChild; }
00499     const TiXmlNode* FirstChild( const char * value ) const;            
00500     TiXmlNode* FirstChild( const char * value );                        
00501 
00502     const TiXmlNode* LastChild() const  { return lastChild; }       
00503     TiXmlNode* LastChild()  { return lastChild; }
00504     const TiXmlNode* LastChild( const char * value ) const;         
00505     TiXmlNode* LastChild( const char * value ); 
00506 
00507     #ifdef TIXML_USE_STL
00508     const TiXmlNode* FirstChild( const std::string& _value ) const  {   return FirstChild (_value.c_str ());    }   
00509     TiXmlNode* FirstChild( const std::string& _value )              {   return FirstChild (_value.c_str ());    }   
00510     const TiXmlNode* LastChild( const std::string& _value ) const   {   return LastChild (_value.c_str ()); }   
00511     TiXmlNode* LastChild( const std::string& _value )               {   return LastChild (_value.c_str ()); }   
00512     #endif
00513 
00530     const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00531     TiXmlNode* IterateChildren( TiXmlNode* previous );
00532 
00534     const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00535     TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00536 
00537     #ifdef TIXML_USE_STL
00538     const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {   return IterateChildren (_value.c_str (), previous); }   
00539     TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous); }   
00540     #endif
00541 
00545     TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00546 
00547 
00557     TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00558 
00562     TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00563 
00567     TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00568 
00572     TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00573 
00575     bool RemoveChild( TiXmlNode* removeThis );
00576 
00578     const TiXmlNode* PreviousSibling() const            { return prev; }
00579     TiXmlNode* PreviousSibling()                        { return prev; }
00580 
00582     const TiXmlNode* PreviousSibling( const char * ) const;
00583     TiXmlNode* PreviousSibling( const char * );
00584 
00585     #ifdef TIXML_USE_STL
00586     const TiXmlNode* PreviousSibling( const std::string& _value ) const {   return PreviousSibling (_value.c_str ());   }   
00587     TiXmlNode* PreviousSibling( const std::string& _value )             {   return PreviousSibling (_value.c_str ());   }   
00588     const TiXmlNode* NextSibling( const std::string& _value) const      {   return NextSibling (_value.c_str ());   }   
00589     TiXmlNode* NextSibling( const std::string& _value)                  {   return NextSibling (_value.c_str ());   }   
00590     #endif
00591 
00593     const TiXmlNode* NextSibling() const                { return next; }
00594     TiXmlNode* NextSibling()                            { return next; }
00595 
00597     const TiXmlNode* NextSibling( const char * ) const;
00598     TiXmlNode* NextSibling( const char * );
00599 
00604     const TiXmlElement* NextSiblingElement() const;
00605     TiXmlElement* NextSiblingElement();
00606 
00611     const TiXmlElement* NextSiblingElement( const char * ) const;
00612     TiXmlElement* NextSiblingElement( const char * );
00613 
00614     #ifdef TIXML_USE_STL
00615     const TiXmlElement* NextSiblingElement( const std::string& _value) const    {   return NextSiblingElement (_value.c_str ());    }   
00616     TiXmlElement* NextSiblingElement( const std::string& _value)                {   return NextSiblingElement (_value.c_str ());    }   
00617     #endif
00618 
00620     const TiXmlElement* FirstChildElement() const;
00621     TiXmlElement* FirstChildElement();
00622 
00624     const TiXmlElement* FirstChildElement( const char * value ) const;
00625     TiXmlElement* FirstChildElement( const char * value );
00626 
00627     #ifdef TIXML_USE_STL
00628     const TiXmlElement* FirstChildElement( const std::string& _value ) const    {   return FirstChildElement (_value.c_str ()); }   
00629     TiXmlElement* FirstChildElement( const std::string& _value )                {   return FirstChildElement (_value.c_str ()); }   
00630     #endif
00631 
00636     int Type() const    { return type; }
00637 
00641     const TiXmlDocument* GetDocument() const;
00642     TiXmlDocument* GetDocument();
00643 
00645     bool NoChildren() const                     { return !firstChild; }
00646 
00647     virtual const TiXmlDocument*    ToDocument()    const { return 0; } 
00648     virtual const TiXmlElement*     ToElement()     const { return 0; } 
00649     virtual const TiXmlComment*     ToComment()     const { return 0; } 
00650     virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } 
00651     virtual const TiXmlText*        ToText()        const { return 0; } 
00652     virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } 
00653 
00654     virtual TiXmlDocument*          ToDocument()    { return 0; } 
00655     virtual TiXmlElement*           ToElement()     { return 0; } 
00656     virtual TiXmlComment*           ToComment()     { return 0; } 
00657     virtual TiXmlUnknown*           ToUnknown()     { return 0; } 
00658     virtual TiXmlText*              ToText()        { return 0; } 
00659     virtual TiXmlDeclaration*       ToDeclaration() { return 0; } 
00660 
00664     virtual TiXmlNode* Clone() const = 0;
00665 
00666 protected:
00667     TiXmlNode( NodeType _type );
00668 
00669     // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00670     // and the assignment operator.
00671     void CopyTo( TiXmlNode* target ) const;
00672 
00673     #ifdef TIXML_USE_STL
00674         // The real work of the input operator.
00675         virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00676     #endif
00677 
00678     // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00679     TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00680 
00681     TiXmlNode*      parent;
00682     NodeType        type;
00683 
00684     TiXmlNode*      firstChild;
00685     TiXmlNode*      lastChild;
00686 
00687     TIXML_STRING    value;
00688 
00689     TiXmlNode*      prev;
00690     TiXmlNode*      next;
00691 
00692 private:
00693     TiXmlNode( const TiXmlNode& );              // not implemented.
00694     void operator=( const TiXmlNode& base );    // not allowed.
00695 };
00696 
00697 
00705 class TiXmlAttribute : public TiXmlBase
00706 {
00707     friend class TiXmlAttributeSet;
00708 
00709 public:
00711     TiXmlAttribute() : TiXmlBase()
00712     {
00713         document = 0;
00714         prev = next = 0;
00715     }
00716 
00717     #ifdef TIXML_USE_STL
00719     TiXmlAttribute( const std::string& _name, const std::string& _value )
00720     {
00721         name = _name;
00722         value = _value;
00723         document = 0;
00724         prev = next = 0;
00725     }
00726     #endif
00727 
00729     TiXmlAttribute( const char * _name, const char * _value )
00730     {
00731         name = _name;
00732         value = _value;
00733         document = 0;
00734         prev = next = 0;
00735     }
00736 
00737     const char*     Name()  const       { return name.c_str (); }       
00738     const char*     Value() const       { return value.c_str (); }      
00739     int             IntValue() const;                                   
00740     double          DoubleValue() const;                                
00741 
00742     // Get the tinyxml string representation
00743     const TIXML_STRING& NameTStr() const { return name; }
00744 
00754     int QueryIntValue( int* _value ) const;
00759     int QueryULongValue( unsigned long* _value ) const;
00761     int QueryDoubleValue( double* _value ) const;
00762 
00763     void SetName( const char* _name )   { name = _name; }               
00764     void SetValue( const char* _value ) { value = _value; }             
00765 
00766     void SetIntValue( int _value );                                     
00767     void SetDoubleValue( double _value );                               
00768 
00769     #ifdef TIXML_USE_STL
00771     void SetName( const std::string& _name )    { name = _name; }   
00773     void SetValue( const std::string& _value )  { value = _value; }
00774     #endif
00775 
00777     const TiXmlAttribute* Next() const;
00778     TiXmlAttribute* Next();
00780     const TiXmlAttribute* Previous() const;
00781     TiXmlAttribute* Previous();
00782 
00783     bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00784     bool operator<( const TiXmlAttribute& rhs )  const { return name < rhs.name; }
00785     bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00786 
00787     /*  Attribute parsing starts: first letter of the name
00788                          returns: the next char after the value end quote
00789     */
00790     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00791 
00792     // Prints this Attribute to a FILE stream.
00793     virtual void Print( FILE* cfile, int depth ) const;
00794 
00795     virtual void StreamOut( TIXML_OSTREAM * out ) const;
00796     // [internal use]
00797     // Set the document pointer so the attribute can report errors.
00798     void SetDocument( TiXmlDocument* doc )  { document = doc; }
00799 
00800 private:
00801     TiXmlAttribute( const TiXmlAttribute& );                // not implemented.
00802     void operator=( const TiXmlAttribute& base );   // not allowed.
00803 
00804     TiXmlDocument*  document;   // A pointer back to a document, for error reporting.
00805     TIXML_STRING name;
00806     TIXML_STRING value;
00807     TiXmlAttribute* prev;
00808     TiXmlAttribute* next;
00809 };
00810 
00811 
00812 /*  A class used to manage a group of attributes.
00813     It is only used internally, both by the ELEMENT and the DECLARATION.
00814     
00815     The set can be changed transparent to the Element and Declaration
00816     classes that use it, but NOT transparent to the Attribute
00817     which has to implement a next() and previous() method. Which makes
00818     it a bit problematic and prevents the use of STL.
00819 
00820     This version is implemented with circular lists because:
00821         - I like circular lists
00822         - it demonstrates some independence from the (typical) doubly linked list.
00823 */
00824 class TiXmlAttributeSet
00825 {
00826 public:
00827     TiXmlAttributeSet();
00828     ~TiXmlAttributeSet();
00829 
00830     void Add( TiXmlAttribute* attribute );
00831     void Remove( TiXmlAttribute* attribute );
00832 
00833     const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00834     TiXmlAttribute* First()                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00835     const TiXmlAttribute* Last() const      { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00836     TiXmlAttribute* Last()                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00837 
00838     const TiXmlAttribute*   Find( const TIXML_STRING& name ) const;
00839     TiXmlAttribute* Find( const TIXML_STRING& name );
00840 
00841 private:
00842     //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00843     //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00844     TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00845     void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
00846 
00847     TiXmlAttribute sentinel;
00848 };
00849 
00850 
00855 class TiXmlElement : public TiXmlNode
00856 {
00857 public:
00859     TiXmlElement (const char * in_value);
00860 
00861     #ifdef TIXML_USE_STL
00863     TiXmlElement( const std::string& _value );
00864     #endif
00865 
00866     TiXmlElement( const TiXmlElement& );
00867 
00868     void operator=( const TiXmlElement& base );
00869 
00870     virtual ~TiXmlElement();
00871 
00875     const char* Attribute( const char* name ) const;
00876 
00883     const char* Attribute( const char* name, int* i ) const;
00884 
00891     const char* Attribute( const char* name, double* d ) const;
00892 
00900     int QueryIntAttribute( const char* name, int* _value ) const;
00905     int QueryULongAttribute( const char* name, unsigned long* ulval ) const;
00907     int QueryDoubleAttribute( const char* name, double* _value ) const;
00909     int QueryFloatAttribute( const char* name, float* _value ) const {
00910         double d;
00911         int result = QueryDoubleAttribute( name, &d );
00912         if ( result == TIXML_SUCCESS ) {
00913             *_value = (float)d;
00914         }
00915         return result;
00916     }
00917 
00921     void SetAttribute( const char* name, const char * _value );
00922 
00923     #ifdef TIXML_USE_STL
00924     const char* Attribute( const std::string& name ) const              { return Attribute( name.c_str() ); }
00925     const char* Attribute( const std::string& name, int* i ) const      { return Attribute( name.c_str(), i ); }
00926     const char* Attribute( const std::string& name, double* d ) const   { return Attribute( name.c_str(), d ); }
00927     int QueryIntAttribute( const std::string& name, int* _value ) const { return QueryIntAttribute( name.c_str(), _value ); }
00928     int QueryDoubleAttribute( const std::string& name, double* _value ) const { return QueryDoubleAttribute( name.c_str(), _value ); }
00929 
00931     void SetAttribute( const std::string& name, const std::string& _value );
00933     void SetAttribute( const std::string& name, int _value );
00934     #endif
00935 
00939     void SetAttribute( const char * name, int value );
00940 
00944     void SetDoubleAttribute( const char * name, double value );
00945 
00948     void RemoveAttribute( const char * name );
00949     #ifdef TIXML_USE_STL
00950     void RemoveAttribute( const std::string& name ) {   RemoveAttribute (name.c_str ());    }   
00951     #endif
00952 
00953     const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }        
00954     TiXmlAttribute* FirstAttribute()                { return attributeSet.First(); }
00955     const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }     
00956     TiXmlAttribute* LastAttribute()                 { return attributeSet.Last(); }
00957 
00990     const char* GetText() const;
00991 
00993     virtual TiXmlNode* Clone() const;
00994     // Print the Element to a FILE stream.
00995     virtual void Print( FILE* cfile, int depth ) const;
00996 
00997     /*  Attribtue parsing starts: next char past '<'
00998                          returns: next char past '>'
00999     */
01000     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01001 
01002     virtual const TiXmlElement*     ToElement()     const { return this; } 
01003     virtual TiXmlElement*           ToElement()           { return this; } 
01004     
01005 protected:
01006 
01007     void CopyTo( TiXmlElement* target ) const;
01008     void ClearThis();   // like clear, but initializes 'this' object as well
01009 
01010     // Used to be public [internal use]
01011     #ifdef TIXML_USE_STL
01012         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01013     #endif
01014     virtual void StreamOut( TIXML_OSTREAM * out ) const;
01015 
01016     /*  [internal use]
01017         Reads the "value" of the element -- another element, or text.
01018         This should terminate with the current end tag.
01019     */
01020     const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01021 
01022 private:
01023 
01024     TiXmlAttributeSet attributeSet;
01025 };
01026 
01027 
01030 class TiXmlComment : public TiXmlNode
01031 {
01032 public:
01034     TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01035     TiXmlComment( const TiXmlComment& );
01036     void operator=( const TiXmlComment& base );
01037 
01038     virtual ~TiXmlComment() {}
01039 
01041     virtual TiXmlNode* Clone() const;
01043     virtual void Print( FILE* cfile, int depth ) const;
01044 
01045     /*  Attribtue parsing starts: at the ! of the !--
01046                          returns: next char past '>'
01047     */
01048     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01049 
01050     virtual const TiXmlComment*  ToComment() const { return this; } 
01051     virtual TiXmlComment*  ToComment() { return this; } 
01052 
01053 protected:
01054     void CopyTo( TiXmlComment* target ) const;
01055 
01056     // used to be public
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 private:
01063 
01064 };
01065 
01066 
01072 class TiXmlText : public TiXmlNode
01073 {
01074     friend class TiXmlElement;
01075 public:
01080     TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01081     {
01082         SetValue( initValue );
01083         cdata = false;
01084     }
01085     virtual ~TiXmlText() {}
01086 
01087     #ifdef TIXML_USE_STL
01089     TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01090     {
01091         SetValue( initValue );
01092         cdata = false;
01093     }
01094     #endif
01095 
01096     TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )   { copy.CopyTo( this ); }
01097     void operator=( const TiXmlText& base )                             { base.CopyTo( this ); }
01098 
01100     virtual void Print( FILE* cfile, int depth ) const;
01101 
01103     bool CDATA()                    { return cdata; }
01105     void SetCDATA( bool _cdata )    { cdata = _cdata; }
01106 
01107     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01108 
01109     virtual const TiXmlText* ToText() const { return this; } 
01110     virtual TiXmlText*       ToText()       { return this; } 
01111 
01112 protected :
01114     virtual TiXmlNode* Clone() const;
01115     void CopyTo( TiXmlText* target ) const;
01116 
01117     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01118     bool Blank() const; // returns true if all white space and new lines
01119     // [internal use]
01120     #ifdef TIXML_USE_STL
01121         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01122     #endif
01123 
01124 private:
01125     bool cdata;         // true if this should be input and output as a CDATA style text element
01126 };
01127 
01128 
01142 class TiXmlDeclaration : public TiXmlNode
01143 {
01144 public:
01146     TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01147 
01148 #ifdef TIXML_USE_STL
01150     TiXmlDeclaration(   const std::string& _version,
01151                         const std::string& _encoding,
01152                         const std::string& _standalone );
01153 #endif
01154 
01156     TiXmlDeclaration(   const char* _version,
01157                         const char* _encoding,
01158                         const char* _standalone );
01159 
01160     TiXmlDeclaration( const TiXmlDeclaration& copy );
01161     void operator=( const TiXmlDeclaration& copy );
01162 
01163     virtual ~TiXmlDeclaration() {}
01164 
01166     const char *Version() const         { return version.c_str (); }
01168     const char *Encoding() const        { return encoding.c_str (); }
01170     const char *Standalone() const      { return standalone.c_str (); }
01171 
01173     virtual TiXmlNode* Clone() const;
01175     virtual void Print( FILE* cfile, int depth ) const;
01176 
01177     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01178 
01179     virtual const TiXmlDeclaration* ToDeclaration() const { return this; } 
01180     virtual TiXmlDeclaration*       ToDeclaration()       { return this; } 
01181 
01182 protected:
01183     void CopyTo( TiXmlDeclaration* target ) const;
01184     // used to be public
01185     #ifdef TIXML_USE_STL
01186         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01187     #endif
01188     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01189 
01190 private:
01191 
01192     TIXML_STRING version;
01193     TIXML_STRING encoding;
01194     TIXML_STRING standalone;
01195 };
01196 
01197 
01205 class TiXmlUnknown : public TiXmlNode
01206 {
01207 public:
01208     TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )    {}
01209     virtual ~TiXmlUnknown() {}
01210 
01211     TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )      { copy.CopyTo( this ); }
01212     void operator=( const TiXmlUnknown& copy )                                      { copy.CopyTo( this ); }
01213 
01215     virtual TiXmlNode* Clone() const;
01217     virtual void Print( FILE* cfile, int depth ) const;
01218 
01219     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01220 
01221     virtual const TiXmlUnknown*     ToUnknown()     const { return this; } 
01222     virtual TiXmlUnknown*           ToUnknown()     { return this; } 
01223 
01224 protected:
01225     void CopyTo( TiXmlUnknown* target ) const;
01226 
01227     #ifdef TIXML_USE_STL
01228         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01229     #endif
01230     virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01231 
01232 private:
01233 
01234 };
01235 
01236 
01241 class TiXmlDocument : public TiXmlNode
01242 {
01243 public:
01245     TiXmlDocument();
01247     TiXmlDocument( const char * documentName );
01248 
01249     #ifdef TIXML_USE_STL
01251     TiXmlDocument( const std::string& documentName );
01252     #endif
01253 
01254     TiXmlDocument( const TiXmlDocument& copy );
01255     void operator=( const TiXmlDocument& copy );
01256 
01257     virtual ~TiXmlDocument() {}
01258 
01263     bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01265     bool SaveFile() const;
01267     bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01269     bool SaveFile( const char * filename ) const;
01275     bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01277     bool SaveFile( FILE* ) const;
01278 
01279     #ifdef TIXML_USE_STL
01280     bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )           
01281     {
01282         StringToBuffer f( filename );
01283         return ( f.buffer && LoadFile( f.buffer, encoding ));
01284     }
01285     bool SaveFile( const std::string& filename ) const      
01286     {
01287         StringToBuffer f( filename );
01288         return ( f.buffer && SaveFile( f.buffer ));
01289     }
01290     #endif
01291 
01296     virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01297 
01302     const TiXmlElement* RootElement() const     { return FirstChildElement(); }
01303     TiXmlElement* RootElement()                 { return FirstChildElement(); }
01304 
01310     bool Error() const                      { return error; }
01311 
01313     const char * ErrorDesc() const  { return errorDesc.c_str (); }
01314 
01318     int ErrorId()   const               { return errorId; }
01319 
01327     int ErrorRow()  { return errorLocation.row+1; }
01328     int ErrorCol()  { return errorLocation.col+1; } 
01329 
01354     void SetTabSize( int _tabsize )     { tabsize = _tabsize; }
01355 
01356     int TabSize() const { return tabsize; }
01357 
01361     void ClearError()                       {   error = false; 
01362                                                 errorId = 0; 
01363                                                 errorDesc = ""; 
01364                                                 errorLocation.row = errorLocation.col = 0; 
01365                                                 //errorLocation.last = 0; 
01366                                             }
01367 
01369     void Print() const                      { Print( stdout, 0 ); }
01370 
01372     virtual void Print( FILE* cfile, int depth = 0 ) const;
01373     // [internal use]
01374     void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01375 
01376     virtual const TiXmlDocument*    ToDocument()    const { return this; } 
01377     virtual TiXmlDocument*          ToDocument()          { return this; } 
01378 
01379 protected :
01380     virtual void StreamOut ( TIXML_OSTREAM * out) const;
01381     // [internal use]
01382     virtual TiXmlNode* Clone() const;
01383     #ifdef TIXML_USE_STL
01384         virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01385     #endif
01386 
01387 private:
01388     void CopyTo( TiXmlDocument* target ) const;
01389 
01390     bool error;
01391     int  errorId;
01392     TIXML_STRING errorDesc;
01393     int tabsize;
01394     TiXmlCursor errorLocation;
01395     bool useMicrosoftBOM;       // the UTF-8 BOM were found when read. Note this, and try to write.
01396 };
01397 
01398 
01479 class TiXmlHandle
01480 {
01481 public:
01483     TiXmlHandle( TiXmlNode* _node )                 { this->node = _node; }
01485     TiXmlHandle( const TiXmlHandle& ref )           { this->node = ref.node; }
01486     TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01487 
01489     TiXmlHandle FirstChild() const;
01491     TiXmlHandle FirstChild( const char * value ) const;
01493     TiXmlHandle FirstChildElement() const;
01495     TiXmlHandle FirstChildElement( const char * value ) const;
01496 
01500     TiXmlHandle Child( const char* value, int index ) const;
01504     TiXmlHandle Child( int index ) const;
01509     TiXmlHandle ChildElement( const char* value, int index ) const;
01514     TiXmlHandle ChildElement( int index ) const;
01515 
01516     #ifdef TIXML_USE_STL
01517     TiXmlHandle FirstChild( const std::string& _value ) const               { return FirstChild( _value.c_str() ); }
01518     TiXmlHandle FirstChildElement( const std::string& _value ) const        { return FirstChildElement( _value.c_str() ); }
01519 
01520     TiXmlHandle Child( const std::string& _value, int index ) const         { return Child( _value.c_str(), index ); }
01521     TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01522     #endif
01523 
01525     TiXmlNode* Node() const         { return node; } 
01527     TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01529     TiXmlText* Text() const         { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01531     TiXmlUnknown* Unknown() const           { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01532 
01533 private:
01534     TiXmlNode* node;
01535 };
01536 
01537 #ifdef _MSC_VER
01538 #pragma warning( pop )
01539 #endif
01540 
01541 #endif
01542 

Generated on Wed May 9 17:35:55 2007 for netrinjo by  doxygen 1.5.1