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

tinyxml.h

00001 /*
00002 www.sourceforge.net/projects/tinyxml
00003 Original code (2.0 and earlier )copyright (c) 2000-2006 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 F.Gaede, DESY : added #define TIXML_USE_STL  for use with marlin
00026 */
00027 
00028 #ifndef TIXML_USE_STL
00029 #define TIXML_USE_STL
00030 #endif
00031 
00032 
00033 #ifndef TINYXML_INCLUDED
00034 #define TINYXML_INCLUDED
00035 
00036 #ifdef _MSC_VER
00037 #pragma warning( push )
00038 #pragma warning( disable : 4530 )
00039 #pragma warning( disable : 4786 )
00040 #endif
00041 
00042 #include <ctype.h>
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 #include <assert.h>
00047 
00048 // Help out windows:
00049 #if defined( _DEBUG ) && !defined( DEBUG )
00050 #define DEBUG
00051 #endif
00052 
00053 #ifdef TIXML_USE_STL
00054         #include <string>
00055         #include <iostream>
00056         #include <sstream>
00057         #define TIXML_STRING            std::string
00058 #else
00059         #include "tinystr.h"
00060         #define TIXML_STRING            TiXmlString
00061 #endif
00062 
00063 // Deprecated library function hell. Compilers want to use the
00064 // new safe versions. This probably doesn't fully address the problem,
00065 // but it gets closer. There are too many compilers for me to fully
00066 // test. If you get compilation troubles, undefine TIXML_SAFE
00067 #define TIXML_SAFE
00068 
00069 #ifdef TIXML_SAFE
00070         #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
00071                 // Microsoft visual studio, version 2005 and higher.
00072                 #define TIXML_SNPRINTF _snprintf_s
00073                 #define TIXML_SNSCANF  _snscanf_s
00074         #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
00075                 // Microsoft visual studio, version 6 and higher.
00076                 //#pragma message( "Using _sn* functions." )
00077                 #define TIXML_SNPRINTF _snprintf
00078                 #define TIXML_SNSCANF  _snscanf
00079         #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00080                 // GCC version 3 and higher.s
00081                 //#warning( "Using sn* functions." )
00082                 #define TIXML_SNPRINTF snprintf
00083                 #define TIXML_SNSCANF  snscanf
00084         #endif
00085 #endif  
00086 
00087 class TiXmlDocument;
00088 class TiXmlElement;
00089 class TiXmlComment;
00090 class TiXmlUnknown;
00091 class TiXmlAttribute;
00092 class TiXmlText;
00093 class TiXmlDeclaration;
00094 class TiXmlParsingData;
00095 
00096 const int TIXML_MAJOR_VERSION = 2;
00097 const int TIXML_MINOR_VERSION = 5;
00098 const int TIXML_PATCH_VERSION = 2;
00099 
00100 /*      Internal structure for tracking location of items 
00101         in the XML file.
00102 */
00103 struct TiXmlCursor
00104 {
00105         TiXmlCursor()           { Clear(); }
00106         void Clear()            { row = col = -1; }
00107 
00108         int row;        // 0 based.
00109         int col;        // 0 based.
00110 };
00111 
00112 
00131 class TiXmlVisitor
00132 {
00133 public:
00134         virtual ~TiXmlVisitor() {}
00135 
00137         virtual bool VisitEnter( const TiXmlDocument& doc )     { return true; }
00139         virtual bool VisitExit( const TiXmlDocument& doc )      { return true; }
00140 
00142         virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )    { return true; }
00144         virtual bool VisitExit( const TiXmlElement& element )                                                                                   { return true; }
00145 
00147         virtual bool Visit( const TiXmlDeclaration& declaration )               { return true; }
00149         virtual bool Visit( const TiXmlText& text )                                             { return true; }
00151         virtual bool Visit( const TiXmlComment& comment )                               { return true; }
00153         virtual bool Visit( const TiXmlUnknown& unknown )                               { return true; }
00154 };
00155 
00156 // Only used by Attribute::Query functions
00157 enum 
00158 { 
00159         TIXML_SUCCESS,
00160         TIXML_NO_ATTRIBUTE,
00161         TIXML_WRONG_TYPE
00162 };
00163 
00164 
00165 // Used by the parsing routines.
00166 enum TiXmlEncoding
00167 {
00168         TIXML_ENCODING_UNKNOWN,
00169         TIXML_ENCODING_UTF8,
00170         TIXML_ENCODING_LEGACY
00171 };
00172 
00173 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00174 
00197 class TiXmlBase
00198 {
00199         friend class TiXmlNode;
00200         friend class TiXmlElement;
00201         friend class TiXmlDocument;
00202 
00203 public:
00204         TiXmlBase()     :       userData(0)             {}
00205         virtual ~TiXmlBase()                    {}
00206 
00216         virtual void Print( FILE* cfile, int depth ) const = 0;
00217 
00224         static void SetCondenseWhiteSpace( bool condense )              { condenseWhiteSpace = condense; }
00225 
00227         static bool IsWhiteSpaceCondensed()                                             { return condenseWhiteSpace; }
00228 
00247         int Row() const                 { return location.row + 1; }
00248         int Column() const              { return location.col + 1; }    
00249 
00250         void  SetUserData( void* user )                 { userData = user; }    
00251         void* GetUserData()                                             { return userData; }    
00252         const void* GetUserData() const                 { return userData; }    
00253 
00254         // Table that returs, for a given lead byte, the total number of bytes
00255         // in the UTF-8 sequence.
00256         static const int utf8ByteTable[256];
00257 
00258         virtual const char* Parse(      const char* p, 
00259                                                                 TiXmlParsingData* data, 
00260                                                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00261 
00262         enum
00263         {
00264                 TIXML_NO_ERROR = 0,
00265                 TIXML_ERROR,
00266                 TIXML_ERROR_OPENING_FILE,
00267                 TIXML_ERROR_OUT_OF_MEMORY,
00268                 TIXML_ERROR_PARSING_ELEMENT,
00269                 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00270                 TIXML_ERROR_READING_ELEMENT_VALUE,
00271                 TIXML_ERROR_READING_ATTRIBUTES,
00272                 TIXML_ERROR_PARSING_EMPTY,
00273                 TIXML_ERROR_READING_END_TAG,
00274                 TIXML_ERROR_PARSING_UNKNOWN,
00275                 TIXML_ERROR_PARSING_COMMENT,
00276                 TIXML_ERROR_PARSING_DECLARATION,
00277                 TIXML_ERROR_DOCUMENT_EMPTY,
00278                 TIXML_ERROR_EMBEDDED_NULL,
00279                 TIXML_ERROR_PARSING_CDATA,
00280                 TIXML_ERROR_DOCUMENT_TOP_ONLY,
00281 
00282                 TIXML_ERROR_STRING_COUNT
00283         };
00284 
00285 protected:
00286 
00287         static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00288         inline static bool IsWhiteSpace( char c )               
00289         { 
00290                 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
00291         }
00292         inline static bool IsWhiteSpace( int c )
00293         {
00294                 if ( c < 256 )
00295                         return IsWhiteSpace( (char) c );
00296                 return false;   // Again, only truly correct for English/Latin...but usually works.
00297         }
00298 
00299         #ifdef TIXML_USE_STL
00300         static bool     StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
00301         static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
00302         #endif
00303 
00304         /*      Reads an XML name into the string provided. Returns
00305                 a pointer just past the last character of the name,
00306                 or 0 if the function has an error.
00307         */
00308         static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00309 
00310         /*      Reads text. Returns a pointer past the given end tag.
00311                 Wickedly complex options, but it keeps the (sensitive) code in one place.
00312         */
00313         static const char* ReadText(    const char* in,                         // where to start
00314                                                                         TIXML_STRING* text,                     // the string read
00315                                                                         bool ignoreWhiteSpace,          // whether to keep the white space
00316                                                                         const char* endTag,                     // what ends this text
00317                                                                         bool ignoreCase,                        // whether to ignore case in the end tag
00318                                                                         TiXmlEncoding encoding );       // the current encoding
00319 
00320         // If an entity has been found, transform it into a character.
00321         static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00322 
00323         // Get a character, while interpreting entities.
00324         // The length can be from 0 to 4 bytes.
00325         inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00326         {
00327                 assert( p );
00328                 if ( encoding == TIXML_ENCODING_UTF8 )
00329                 {
00330                         *length = utf8ByteTable[ *((const unsigned char*)p) ];
00331                         assert( *length >= 0 && *length < 5 );
00332                 }
00333                 else
00334                 {
00335                         *length = 1;
00336                 }
00337 
00338                 if ( *length == 1 )
00339                 {
00340                         if ( *p == '&' )
00341                                 return GetEntity( p, _value, length, encoding );
00342                         *_value = *p;
00343                         return p+1;
00344                 }
00345                 else if ( *length )
00346                 {
00347                         //strncpy( _value, p, *length );        // lots of compilers don't like this function (unsafe),
00348                                                                                                 // and the null terminator isn't needed
00349                         for( int i=0; p[i] && i<*length; ++i ) {
00350                                 _value[i] = p[i];
00351                         }
00352                         return p + (*length);
00353                 }
00354                 else
00355                 {
00356                         // Not valid text.
00357                         return 0;
00358                 }
00359         }
00360 
00361         // Puts a string to a stream, expanding entities as it goes.
00362         // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00363         static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00364 
00365         // Return true if the next characters in the stream are any of the endTag sequences.
00366         // Ignore case only works for english, and should only be relied on when comparing
00367         // to English words: StringEqual( p, "version", true ) is fine.
00368         static bool StringEqual(        const char* p,
00369                                                                 const char* endTag,
00370                                                                 bool ignoreCase,
00371                                                                 TiXmlEncoding encoding );
00372 
00373         static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00374 
00375         TiXmlCursor location;
00376 
00378         void*                   userData;
00379         
00380         // None of these methods are reliable for any language except English.
00381         // Good for approximation, not great for accuracy.
00382         static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00383         static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00384         inline static int ToLower( int v, TiXmlEncoding encoding )
00385         {
00386                 if ( encoding == TIXML_ENCODING_UTF8 )
00387                 {
00388                         if ( v < 128 ) return tolower( v );
00389                         return v;
00390                 }
00391                 else
00392                 {
00393                         return tolower( v );
00394                 }
00395         }
00396         static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00397 
00398 private:
00399         TiXmlBase( const TiXmlBase& );                          // not implemented.
00400         void operator=( const TiXmlBase& base );        // not allowed.
00401 
00402         struct Entity
00403         {
00404                 const char*     str;
00405                 unsigned int    strLength;
00406                 char                chr;
00407         };
00408         enum
00409         {
00410                 NUM_ENTITY = 5,
00411                 MAX_ENTITY_LENGTH = 6
00412 
00413         };
00414         static Entity entity[ NUM_ENTITY ];
00415         static bool condenseWhiteSpace;
00416 };
00417 
00418 
00425 class TiXmlNode : public TiXmlBase
00426 {
00427         friend class TiXmlDocument;
00428         friend class TiXmlElement;
00429 
00430 public:
00431         #ifdef TIXML_USE_STL    
00432 
00436             friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00437 
00454             friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00455 
00457                 friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00458 
00459         #endif
00460 
00464         enum NodeType
00465         {
00466                 DOCUMENT,
00467                 ELEMENT,
00468                 COMMENT,
00469                 UNKNOWN,
00470                 TEXT,
00471                 DECLARATION,
00472                 TYPECOUNT
00473         };
00474 
00475         virtual ~TiXmlNode();
00476 
00489         const char *Value() const { return value.c_str (); }
00490 
00491     #ifdef TIXML_USE_STL
00492 
00496         const std::string& ValueStr() const { return value; }
00497         #endif
00498 
00508         void SetValue(const char * _value) { value = _value;}
00509 
00510     #ifdef TIXML_USE_STL
00511 
00512         void SetValue( const std::string& _value )      { value = _value; }
00513         #endif
00514 
00516         void Clear();
00517 
00519         TiXmlNode* Parent()                                                     { return parent; }
00520         const TiXmlNode* Parent() const                         { return parent; }
00521 
00522         const TiXmlNode* FirstChild()   const   { return firstChild; }          
00523         TiXmlNode* FirstChild()                                 { return firstChild; }
00524         const TiXmlNode* FirstChild( const char * value ) const;                        
00525 
00526         TiXmlNode* FirstChild( const char * _value ) {
00527                 // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
00528                 // call the method, cast the return back to non-const.
00529                 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
00530         }
00531         const TiXmlNode* LastChild() const      { return lastChild; }           
00532         TiXmlNode* LastChild()  { return lastChild; }
00533         
00534         const TiXmlNode* LastChild( const char * value ) const;                 
00535         TiXmlNode* LastChild( const char * _value ) {
00536                 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
00537         }
00538 
00539     #ifdef TIXML_USE_STL
00540         const TiXmlNode* FirstChild( const std::string& _value ) const  {       return FirstChild (_value.c_str ());    }       
00541         TiXmlNode* FirstChild( const std::string& _value )                              {       return FirstChild (_value.c_str ());    }       
00542         const TiXmlNode* LastChild( const std::string& _value ) const   {       return LastChild (_value.c_str ());     }       
00543         TiXmlNode* LastChild( const std::string& _value )                               {       return LastChild (_value.c_str ());     }       
00544         #endif
00545 
00562         const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00563         TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
00564                 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
00565         }
00566 
00568         const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00569         TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
00570                 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
00571         }
00572 
00573     #ifdef TIXML_USE_STL
00574         const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {       return IterateChildren (_value.c_str (), previous);     }       
00575         TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) {    return IterateChildren (_value.c_str (), previous);     }       
00576         #endif
00577 
00581         TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00582 
00583 
00593         TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00594 
00598         TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00599 
00603         TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00604 
00608         TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00609 
00611         bool RemoveChild( TiXmlNode* removeThis );
00612 
00614         const TiXmlNode* PreviousSibling() const                        { return prev; }
00615         TiXmlNode* PreviousSibling()                                            { return prev; }
00616 
00618         const TiXmlNode* PreviousSibling( const char * ) const;
00619         TiXmlNode* PreviousSibling( const char *_prev ) {
00620                 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
00621         }
00622 
00623     #ifdef TIXML_USE_STL
00624         const TiXmlNode* PreviousSibling( const std::string& _value ) const     {       return PreviousSibling (_value.c_str ());       }       
00625         TiXmlNode* PreviousSibling( const std::string& _value )                         {       return PreviousSibling (_value.c_str ());       }       
00626         const TiXmlNode* NextSibling( const std::string& _value) const          {       return NextSibling (_value.c_str ());   }       
00627         TiXmlNode* NextSibling( const std::string& _value)                                      {       return NextSibling (_value.c_str ());   }       
00628         #endif
00629 
00631         const TiXmlNode* NextSibling() const                            { return next; }
00632         TiXmlNode* NextSibling()                                                        { return next; }
00633 
00635         const TiXmlNode* NextSibling( const char * ) const;
00636         TiXmlNode* NextSibling( const char* _next ) {
00637                 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
00638         }
00639 
00644         const TiXmlElement* NextSiblingElement() const;
00645         TiXmlElement* NextSiblingElement() {
00646                 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
00647         }
00648 
00653         const TiXmlElement* NextSiblingElement( const char * ) const;
00654         TiXmlElement* NextSiblingElement( const char *_next ) {
00655                 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
00656         }
00657 
00658     #ifdef TIXML_USE_STL
00659         const TiXmlElement* NextSiblingElement( const std::string& _value) const        {       return NextSiblingElement (_value.c_str ());    }       
00660         TiXmlElement* NextSiblingElement( const std::string& _value)                            {       return NextSiblingElement (_value.c_str ());    }       
00661         #endif
00662 
00664         const TiXmlElement* FirstChildElement() const;
00665         TiXmlElement* FirstChildElement() {
00666                 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
00667         }
00668 
00670         const TiXmlElement* FirstChildElement( const char * _value ) const;
00671         TiXmlElement* FirstChildElement( const char * _value ) {
00672                 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
00673         }
00674 
00675     #ifdef TIXML_USE_STL
00676         const TiXmlElement* FirstChildElement( const std::string& _value ) const        {       return FirstChildElement (_value.c_str ());     }       
00677         TiXmlElement* FirstChildElement( const std::string& _value )                            {       return FirstChildElement (_value.c_str ());     }       
00678         #endif
00679 
00684         int Type() const        { return type; }
00685 
00689         const TiXmlDocument* GetDocument() const;
00690         TiXmlDocument* GetDocument() {
00691                 return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
00692         }
00693 
00695         bool NoChildren() const                                         { return !firstChild; }
00696 
00697         virtual const TiXmlDocument*    ToDocument()    const { return 0; } 
00698         virtual const TiXmlElement*     ToElement()     const { return 0; } 
00699         virtual const TiXmlComment*     ToComment()     const { return 0; } 
00700         virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } 
00701         virtual const TiXmlText*        ToText()        const { return 0; } 
00702         virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } 
00703 
00704         virtual TiXmlDocument*          ToDocument()    { return 0; } 
00705         virtual TiXmlElement*           ToElement()         { return 0; } 
00706         virtual TiXmlComment*           ToComment()     { return 0; } 
00707         virtual TiXmlUnknown*           ToUnknown()         { return 0; } 
00708         virtual TiXmlText*                  ToText()        { return 0; } 
00709         virtual TiXmlDeclaration*       ToDeclaration() { return 0; } 
00710 
00714         virtual TiXmlNode* Clone() const = 0;
00715 
00738         virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
00739 
00740 protected:
00741         TiXmlNode( NodeType _type );
00742 
00743         // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00744         // and the assignment operator.
00745         void CopyTo( TiXmlNode* target ) const;
00746 
00747         #ifdef TIXML_USE_STL
00748             // The real work of the input operator.
00749         virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
00750         #endif
00751 
00752         // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00753         TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00754 
00755         TiXmlNode*              parent;
00756         NodeType                type;
00757 
00758         TiXmlNode*              firstChild;
00759         TiXmlNode*              lastChild;
00760 
00761         TIXML_STRING    value;
00762 
00763         TiXmlNode*              prev;
00764         TiXmlNode*              next;
00765 
00766 private:
00767         TiXmlNode( const TiXmlNode& );                          // not implemented.
00768         void operator=( const TiXmlNode& base );        // not allowed.
00769 };
00770 
00771 
00779 class TiXmlAttribute : public TiXmlBase
00780 {
00781         friend class TiXmlAttributeSet;
00782 
00783 public:
00785         TiXmlAttribute() : TiXmlBase()
00786         {
00787                 document = 0;
00788                 prev = next = 0;
00789         }
00790 
00791         #ifdef TIXML_USE_STL
00792 
00793         TiXmlAttribute( const std::string& _name, const std::string& _value )
00794         {
00795                 name = _name;
00796                 value = _value;
00797                 document = 0;
00798                 prev = next = 0;
00799         }
00800         #endif
00801 
00803         TiXmlAttribute( const char * _name, const char * _value )
00804         {
00805                 name = _name;
00806                 value = _value;
00807                 document = 0;
00808                 prev = next = 0;
00809         }
00810 
00811         const char*             Name()  const           { return name.c_str(); }                
00812         const char*             Value() const           { return value.c_str(); }               
00813         #ifdef TIXML_USE_STL
00814         const std::string& ValueStr() const     { return value; }                               
00815         #endif
00816         int                             IntValue() const;                                                                       
00817         double                  DoubleValue() const;                                                            
00818 
00819         // Get the tinyxml string representation
00820         const TIXML_STRING& NameTStr() const { return name; }
00821 
00831         int QueryIntValue( int* _value ) const;
00833         int QueryDoubleValue( double* _value ) const;
00834 
00835         void SetName( const char* _name )       { name = _name; }                               
00836         void SetValue( const char* _value )     { value = _value; }                             
00837 
00838         void SetIntValue( int _value );                                                                         
00839         void SetDoubleValue( double _value );                                                           
00840 
00841     #ifdef TIXML_USE_STL
00842 
00843         void SetName( const std::string& _name )        { name = _name; }       
00845         void SetValue( const std::string& _value )      { value = _value; }
00846         #endif
00847 
00849         const TiXmlAttribute* Next() const;
00850         TiXmlAttribute* Next() {
00851                 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() ); 
00852         }
00853 
00855         const TiXmlAttribute* Previous() const;
00856         TiXmlAttribute* Previous() {
00857                 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() ); 
00858         }
00859 
00860         bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00861         bool operator<( const TiXmlAttribute& rhs )      const { return name < rhs.name; }
00862         bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00863 
00864         /*      Attribute parsing starts: first letter of the name
00865                                                  returns: the next char after the value end quote
00866         */
00867         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00868 
00869         // Prints this Attribute to a FILE stream.
00870         virtual void Print( FILE* cfile, int depth ) const {
00871                 Print( cfile, depth, 0 );
00872         }
00873         void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
00874 
00875         // [internal use]
00876         // Set the document pointer so the attribute can report errors.
00877         void SetDocument( TiXmlDocument* doc )  { document = doc; }
00878 
00879 private:
00880         TiXmlAttribute( const TiXmlAttribute& );                                // not implemented.
00881         void operator=( const TiXmlAttribute& base );   // not allowed.
00882 
00883         TiXmlDocument*  document;       // A pointer back to a document, for error reporting.
00884         TIXML_STRING name;
00885         TIXML_STRING value;
00886         TiXmlAttribute* prev;
00887         TiXmlAttribute* next;
00888 };
00889 
00890 
00891 /*      A class used to manage a group of attributes.
00892         It is only used internally, both by the ELEMENT and the DECLARATION.
00893         
00894         The set can be changed transparent to the Element and Declaration
00895         classes that use it, but NOT transparent to the Attribute
00896         which has to implement a next() and previous() method. Which makes
00897         it a bit problematic and prevents the use of STL.
00898 
00899         This version is implemented with circular lists because:
00900                 - I like circular lists
00901                 - it demonstrates some independence from the (typical) doubly linked list.
00902 */
00903 class TiXmlAttributeSet
00904 {
00905 public:
00906         TiXmlAttributeSet();
00907         ~TiXmlAttributeSet();
00908 
00909         void Add( TiXmlAttribute* attribute );
00910         void Remove( TiXmlAttribute* attribute );
00911 
00912         const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00913         TiXmlAttribute* First()                                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00914         const TiXmlAttribute* Last() const              { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00915         TiXmlAttribute* Last()                                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00916 
00917         const TiXmlAttribute*   Find( const char* _name ) const;
00918         TiXmlAttribute* Find( const char* _name ) {
00919                 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
00920         }
00921         #ifdef TIXML_USE_STL
00922         const TiXmlAttribute*   Find( const std::string& _name ) const;
00923         TiXmlAttribute* Find( const std::string& _name ) {
00924                 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
00925         }
00926 
00927         #endif
00928 
00929 private:
00930         //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00931         //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00932         TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00933         void operator=( const TiXmlAttributeSet& );     // not allowed (as TiXmlAttribute)
00934 
00935         TiXmlAttribute sentinel;
00936 };
00937 
00938 
00943 class TiXmlElement : public TiXmlNode
00944 {
00945 public:
00947         TiXmlElement (const char * in_value);
00948 
00949         #ifdef TIXML_USE_STL
00950 
00951         TiXmlElement( const std::string& _value );
00952         #endif
00953 
00954         TiXmlElement( const TiXmlElement& );
00955 
00956         void operator=( const TiXmlElement& base );
00957 
00958         virtual ~TiXmlElement();
00959 
00963         const char* Attribute( const char* name ) const;
00964 
00971         const char* Attribute( const char* name, int* i ) const;
00972 
00979         const char* Attribute( const char* name, double* d ) const;
00980 
00988         int QueryIntAttribute( const char* name, int* _value ) const;
00990         int QueryDoubleAttribute( const char* name, double* _value ) const;
00992         int QueryFloatAttribute( const char* name, float* _value ) const {
00993                 double d;
00994                 int result = QueryDoubleAttribute( name, &d );
00995                 if ( result == TIXML_SUCCESS ) {
00996                         *_value = (float)d;
00997                 }
00998                 return result;
00999         }
01000     #ifdef TIXML_USE_STL
01001 
01007         template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
01008         {
01009                 const TiXmlAttribute* node = attributeSet.Find( name );
01010                 if ( !node )
01011                         return TIXML_NO_ATTRIBUTE;
01012 
01013                 std::stringstream sstream( node->ValueStr() );
01014                 sstream >> *outValue;
01015                 if ( !sstream.fail() )
01016                         return TIXML_SUCCESS;
01017                 return TIXML_WRONG_TYPE;
01018         }
01019         #endif
01020 
01024         void SetAttribute( const char* name, const char * _value );
01025 
01026     #ifdef TIXML_USE_STL
01027         const std::string* Attribute( const std::string& name ) const;
01028         const std::string* Attribute( const std::string& name, int* i ) const;
01029         const std::string* Attribute( const std::string& name, double* d ) const;
01030         int QueryIntAttribute( const std::string& name, int* _value ) const;
01031         int QueryDoubleAttribute( const std::string& name, double* _value ) const;
01032 
01034         void SetAttribute( const std::string& name, const std::string& _value );
01036         void SetAttribute( const std::string& name, int _value );
01037         #endif
01038 
01042         void SetAttribute( const char * name, int value );
01043 
01047         void SetDoubleAttribute( const char * name, double value );
01048 
01051         void RemoveAttribute( const char * name );
01052     #ifdef TIXML_USE_STL
01053         void RemoveAttribute( const std::string& name ) {       RemoveAttribute (name.c_str ());        }       
01054         #endif
01055 
01056         const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }                
01057         TiXmlAttribute* FirstAttribute()                                { return attributeSet.First(); }
01058         const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }         
01059         TiXmlAttribute* LastAttribute()                                 { return attributeSet.Last(); }
01060 
01093         const char* GetText() const;
01094 
01096         virtual TiXmlNode* Clone() const;
01097         // Print the Element to a FILE stream.
01098         virtual void Print( FILE* cfile, int depth ) const;
01099 
01100         /*      Attribtue parsing starts: next char past '<'
01101                                                  returns: next char past '>'
01102         */
01103         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01104 
01105         virtual const TiXmlElement*     ToElement()     const { return this; } 
01106         virtual TiXmlElement*           ToElement()               { return this; } 
01107 
01110         virtual bool Accept( TiXmlVisitor* visitor ) const;
01111 
01112 protected:
01113 
01114         void CopyTo( TiXmlElement* target ) const;
01115         void ClearThis();       // like clear, but initializes 'this' object as well
01116 
01117         // Used to be public [internal use]
01118         #ifdef TIXML_USE_STL
01119         virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01120         #endif
01121         /*      [internal use]
01122                 Reads the "value" of the element -- another element, or text.
01123                 This should terminate with the current end tag.
01124         */
01125         const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01126 
01127 private:
01128 
01129         TiXmlAttributeSet attributeSet;
01130 };
01131 
01132 
01135 class TiXmlComment : public TiXmlNode
01136 {
01137 public:
01139         TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01141         TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::COMMENT ) {
01142                 SetValue( _value );
01143         }
01144         TiXmlComment( const TiXmlComment& );
01145         void operator=( const TiXmlComment& base );
01146 
01147         virtual ~TiXmlComment() {}
01148 
01150         virtual TiXmlNode* Clone() const;
01151         // Write this Comment to a FILE stream.
01152         virtual void Print( FILE* cfile, int depth ) const;
01153 
01154         /*      Attribtue parsing starts: at the ! of the !--
01155                                                  returns: next char past '>'
01156         */
01157         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01158 
01159         virtual const TiXmlComment*  ToComment() const { return this; } 
01160         virtual TiXmlComment*  ToComment() { return this; } 
01161 
01164         virtual bool Accept( TiXmlVisitor* visitor ) const;
01165 
01166 protected:
01167         void CopyTo( TiXmlComment* target ) const;
01168 
01169         // used to be public
01170         #ifdef TIXML_USE_STL
01171         virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01172         #endif
01173 //      virtual void StreamOut( TIXML_OSTREAM * out ) const;
01174 
01175 private:
01176 
01177 };
01178 
01179 
01185 class TiXmlText : public TiXmlNode
01186 {
01187         friend class TiXmlElement;
01188 public:
01193         TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01194         {
01195                 SetValue( initValue );
01196                 cdata = false;
01197         }
01198         virtual ~TiXmlText() {}
01199 
01200         #ifdef TIXML_USE_STL
01201 
01202         TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01203         {
01204                 SetValue( initValue );
01205                 cdata = false;
01206         }
01207         #endif
01208 
01209         TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )       { copy.CopyTo( this ); }
01210         void operator=( const TiXmlText& base )                                                         { base.CopyTo( this ); }
01211 
01212         // Write this text object to a FILE stream.
01213         virtual void Print( FILE* cfile, int depth ) const;
01214 
01216         bool CDATA() const                              { return cdata; }
01218         void SetCDATA( bool _cdata )    { cdata = _cdata; }
01219 
01220         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01221 
01222         virtual const TiXmlText* ToText() const { return this; } 
01223         virtual TiXmlText*       ToText()       { return this; } 
01224 
01227         virtual bool Accept( TiXmlVisitor* content ) const;
01228 
01229 protected :
01231         virtual TiXmlNode* Clone() const;
01232         void CopyTo( TiXmlText* target ) const;
01233 
01234         bool Blank() const;     // returns true if all white space and new lines
01235         // [internal use]
01236         #ifdef TIXML_USE_STL
01237         virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01238         #endif
01239 
01240 private:
01241         bool cdata;                     // true if this should be input and output as a CDATA style text element
01242 };
01243 
01244 
01258 class TiXmlDeclaration : public TiXmlNode
01259 {
01260 public:
01262         TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01263 
01264 #ifdef TIXML_USE_STL
01265 
01266         TiXmlDeclaration(       const std::string& _version,
01267                                                 const std::string& _encoding,
01268                                                 const std::string& _standalone );
01269 #endif
01270 
01272         TiXmlDeclaration(       const char* _version,
01273                                                 const char* _encoding,
01274                                                 const char* _standalone );
01275 
01276         TiXmlDeclaration( const TiXmlDeclaration& copy );
01277         void operator=( const TiXmlDeclaration& copy );
01278 
01279         virtual ~TiXmlDeclaration()     {}
01280 
01282         const char *Version() const                     { return version.c_str (); }
01284         const char *Encoding() const            { return encoding.c_str (); }
01286         const char *Standalone() const          { return standalone.c_str (); }
01287 
01289         virtual TiXmlNode* Clone() const;
01290         // Print this declaration to a FILE stream.
01291         virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
01292         virtual void Print( FILE* cfile, int depth ) const {
01293                 Print( cfile, depth, 0 );
01294         }
01295 
01296         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01297 
01298         virtual const TiXmlDeclaration* ToDeclaration() const { return this; } 
01299         virtual TiXmlDeclaration*       ToDeclaration()       { return this; } 
01300 
01303         virtual bool Accept( TiXmlVisitor* visitor ) const;
01304 
01305 protected:
01306         void CopyTo( TiXmlDeclaration* target ) const;
01307         // used to be public
01308         #ifdef TIXML_USE_STL
01309         virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01310         #endif
01311 
01312 private:
01313 
01314         TIXML_STRING version;
01315         TIXML_STRING encoding;
01316         TIXML_STRING standalone;
01317 };
01318 
01319 
01327 class TiXmlUnknown : public TiXmlNode
01328 {
01329 public:
01330         TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )        {}
01331         virtual ~TiXmlUnknown() {}
01332 
01333         TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )              { copy.CopyTo( this ); }
01334         void operator=( const TiXmlUnknown& copy )                                                                              { copy.CopyTo( this ); }
01335 
01337         virtual TiXmlNode* Clone() const;
01338         // Print this Unknown to a FILE stream.
01339         virtual void Print( FILE* cfile, int depth ) const;
01340 
01341         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01342 
01343         virtual const TiXmlUnknown*     ToUnknown()     const { return this; } 
01344         virtual TiXmlUnknown*           ToUnknown()         { return this; } 
01345 
01348         virtual bool Accept( TiXmlVisitor* content ) const;
01349 
01350 protected:
01351         void CopyTo( TiXmlUnknown* target ) const;
01352 
01353         #ifdef TIXML_USE_STL
01354         virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01355         #endif
01356 
01357 private:
01358 
01359 };
01360 
01361 
01366 class TiXmlDocument : public TiXmlNode
01367 {
01368 public:
01370         TiXmlDocument();
01372         TiXmlDocument( const char * documentName );
01373 
01374         #ifdef TIXML_USE_STL
01375 
01376         TiXmlDocument( const std::string& documentName );
01377         #endif
01378 
01379         TiXmlDocument( const TiXmlDocument& copy );
01380         void operator=( const TiXmlDocument& copy );
01381 
01382         virtual ~TiXmlDocument() {}
01383 
01388         bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01390         bool SaveFile() const;
01392         bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01394         bool SaveFile( const char * filename ) const;
01400         bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01402         bool SaveFile( FILE* ) const;
01403 
01404         #ifdef TIXML_USE_STL
01405         bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )                   
01406         {
01407 //              StringToBuffer f( filename );
01408 //              return ( f.buffer && LoadFile( f.buffer, encoding ));
01409                 return LoadFile( filename.c_str(), encoding );
01410         }
01411         bool SaveFile( const std::string& filename ) const              
01412         {
01413 //              StringToBuffer f( filename );
01414 //              return ( f.buffer && SaveFile( f.buffer ));
01415                 return SaveFile( filename.c_str() );
01416         }
01417         #endif
01418 
01423         virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01424 
01429         const TiXmlElement* RootElement() const         { return FirstChildElement(); }
01430         TiXmlElement* RootElement()                                     { return FirstChildElement(); }
01431 
01437         bool Error() const                                              { return error; }
01438 
01440         const char * ErrorDesc() const  { return errorDesc.c_str (); }
01441 
01445         int ErrorId()   const                           { return errorId; }
01446 
01454         int ErrorRow() const    { return errorLocation.row+1; }
01455         int ErrorCol() const    { return errorLocation.col+1; } 
01456 
01481         void SetTabSize( int _tabsize )         { tabsize = _tabsize; }
01482 
01483         int TabSize() const     { return tabsize; }
01484 
01488         void ClearError()                                               {       error = false; 
01489                                                                                                 errorId = 0; 
01490                                                                                                 errorDesc = ""; 
01491                                                                                                 errorLocation.row = errorLocation.col = 0; 
01492                                                                                                 //errorLocation.last = 0; 
01493                                                                                         }
01494 
01496         void Print() const                                              { Print( stdout, 0 ); }
01497 
01498         /* Write the document to a string using formatted printing ("pretty print"). This
01499                 will allocate a character array (new char[]) and return it as a pointer. The
01500                 calling code pust call delete[] on the return char* to avoid a memory leak.
01501         */
01502         //char* PrintToMemory() const; 
01503 
01505         virtual void Print( FILE* cfile, int depth = 0 ) const;
01506         // [internal use]
01507         void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01508 
01509         virtual const TiXmlDocument*    ToDocument()    const { return this; } 
01510         virtual TiXmlDocument*          ToDocument()          { return this; } 
01511 
01514         virtual bool Accept( TiXmlVisitor* content ) const;
01515 
01516 protected :
01517         // [internal use]
01518         virtual TiXmlNode* Clone() const;
01519         #ifdef TIXML_USE_STL
01520         virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01521         #endif
01522 
01523 private:
01524         void CopyTo( TiXmlDocument* target ) const;
01525 
01526         bool error;
01527         int  errorId;
01528         TIXML_STRING errorDesc;
01529         int tabsize;
01530         TiXmlCursor errorLocation;
01531         bool useMicrosoftBOM;           // the UTF-8 BOM were found when read. Note this, and try to write.
01532 };
01533 
01534 
01615 class TiXmlHandle
01616 {
01617 public:
01619         TiXmlHandle( TiXmlNode* _node )                                 { this->node = _node; }
01621         TiXmlHandle( const TiXmlHandle& ref )                   { this->node = ref.node; }
01622         TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01623 
01625         TiXmlHandle FirstChild() const;
01627         TiXmlHandle FirstChild( const char * value ) const;
01629         TiXmlHandle FirstChildElement() const;
01631         TiXmlHandle FirstChildElement( const char * value ) const;
01632 
01636         TiXmlHandle Child( const char* value, int index ) const;
01640         TiXmlHandle Child( int index ) const;
01645         TiXmlHandle ChildElement( const char* value, int index ) const;
01650         TiXmlHandle ChildElement( int index ) const;
01651 
01652         #ifdef TIXML_USE_STL
01653         TiXmlHandle FirstChild( const std::string& _value ) const                               { return FirstChild( _value.c_str() ); }
01654         TiXmlHandle FirstChildElement( const std::string& _value ) const                { return FirstChildElement( _value.c_str() ); }
01655 
01656         TiXmlHandle Child( const std::string& _value, int index ) const                 { return Child( _value.c_str(), index ); }
01657         TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01658         #endif
01659 
01662         TiXmlNode* ToNode() const                       { return node; } 
01665         TiXmlElement* ToElement() const         { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01668         TiXmlText* ToText() const                       { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01671         TiXmlUnknown* ToUnknown() const         { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01672 
01676         TiXmlNode* Node() const                 { return ToNode(); } 
01680         TiXmlElement* Element() const   { return ToElement(); }
01684         TiXmlText* Text() const                 { return ToText(); }
01688         TiXmlUnknown* Unknown() const   { return ToUnknown(); }
01689 
01690 private:
01691         TiXmlNode* node;
01692 };
01693 
01694 
01714 class TiXmlPrinter : public TiXmlVisitor
01715 {
01716 public:
01717         TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
01718                                          buffer(), indent( "    " ), lineBreak( "\n" ) {}
01719 
01720         virtual bool VisitEnter( const TiXmlDocument& doc );
01721         virtual bool VisitExit( const TiXmlDocument& doc );
01722 
01723         virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
01724         virtual bool VisitExit( const TiXmlElement& element );
01725 
01726         virtual bool Visit( const TiXmlDeclaration& declaration );
01727         virtual bool Visit( const TiXmlText& text );
01728         virtual bool Visit( const TiXmlComment& comment );
01729         virtual bool Visit( const TiXmlUnknown& unknown );
01730 
01734         void SetIndent( const char* _indent )                   { indent = _indent ? _indent : "" ; }
01736         const char* Indent()                                                    { return indent.c_str(); }
01741         void SetLineBreak( const char* _lineBreak )             { lineBreak = _lineBreak ? _lineBreak : ""; }
01743         const char* LineBreak()                                                 { return lineBreak.c_str(); }
01744 
01748         void SetStreamPrinting()                                                { indent = "";
01749                                                                                                           lineBreak = "";
01750                                                                                                         }       
01752         const char* CStr()                                                              { return buffer.c_str(); }
01754         size_t Size()                                                                   { return buffer.size(); }
01755 
01756         #ifdef TIXML_USE_STL
01757 
01758         const std::string& Str()                                                { return buffer; }
01759         #endif
01760 
01761 private:
01762         void DoIndent() {
01763                 for( int i=0; i<depth; ++i )
01764                         buffer += indent;
01765         }
01766         void DoLineBreak() {
01767                 buffer += lineBreak;
01768         }
01769 
01770         int depth;
01771         bool simpleTextPrint;
01772         TIXML_STRING buffer;
01773         TIXML_STRING indent;
01774         TIXML_STRING lineBreak;
01775 };
01776 
01777 
01778 #ifdef _MSC_VER
01779 #pragma warning( pop )
01780 #endif
01781 
01782 #endif
01783 

Generated on Mon Jan 12 09:48:52 2009 for Marlin by doxygen 1.3.5