eXperiment robotics framework

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-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 
00026 #ifndef TINYXML_INCLUDED
00027 #define TINYXML_INCLUDED
00028 
00029 #ifdef _MSC_VER
00030 #pragma warning( push )
00031 #pragma warning( disable : 4530 )
00032 #pragma warning( disable : 4786 )
00033 #endif
00034 
00035 #include <ctype.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <assert.h>
00040 
00041 // Help out windows:
00042 #if defined( _DEBUG ) && !defined( DEBUG )
00043 #define DEBUG
00044 #endif
00045 
00046 #ifndef TIXML_USE_STL
00047 #define TIXML_USE_STL
00048 #endif
00049 
00050 #ifdef TIXML_USE_STL
00051     #include <string>
00052     #include <iostream>
00053     #include <sstream>
00054     #define TIXML_STRING        std::string
00055 #else
00056     #include "tinystr.h"
00057     #define TIXML_STRING        TiXmlString
00058 #endif
00059 
00060 // Deprecated library function hell. Compilers want to use the
00061 // new safe versions. This probably doesn't fully address the problem,
00062 // but it gets closer. There are too many compilers for me to fully
00063 // test. If you get compilation troubles, undefine TIXML_SAFE
00064 #define TIXML_SAFE
00065 
00066 #ifdef TIXML_SAFE
00067     #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
00068         // Microsoft visual studio, version 2005 and higher.
00069         #define TIXML_SNPRINTF _snprintf_s
00070         #define TIXML_SNSCANF  _snscanf_s
00071         #define TIXML_SSCANF   sscanf_s
00072     #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
00073         // Microsoft visual studio, version 6 and higher.
00074         //#pragma message( "Using _sn* functions." )
00075         #define TIXML_SNPRINTF _snprintf
00076         #define TIXML_SNSCANF  _snscanf
00077         #define TIXML_SSCANF   sscanf
00078     #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00079         // GCC version 3 and higher.s
00080         //#warning( "Using sn* functions." )
00081         #define TIXML_SNPRINTF snprintf
00082         #define TIXML_SNSCANF  snscanf
00083         #define TIXML_SSCANF   sscanf
00084     #else
00085         #define TIXML_SSCANF   sscanf
00086     #endif
00087 #endif  
00088 
00089 class TiXmlDocument;
00090 class TiXmlElement;
00091 class TiXmlComment;
00092 class TiXmlUnknown;
00093 class TiXmlAttribute;
00094 class TiXmlText;
00095 class TiXmlDeclaration;
00096 class TiXmlParsingData;
00097 
00098 const int TIXML_MAJOR_VERSION = 2;
00099 const int TIXML_MINOR_VERSION = 5;
00100 const int TIXML_PATCH_VERSION = 3;
00101 
00102 /*  Internal structure for tracking location of items 
00103     in the XML file.
00104 */
00105 struct TiXmlCursor
00106 {
00107     TiXmlCursor()       { Clear(); }
00108     void Clear()        { row = col = -1; }
00109 
00110     int row;    // 0 based.
00111     int col;    // 0 based.
00112 };
00113 
00114 
00133 class TiXmlVisitor
00134 {
00135 public:
00136     virtual ~TiXmlVisitor() {}
00137 
00139     virtual bool VisitEnter( const TiXmlDocument& /*doc*/ )         { return true; }
00141     virtual bool VisitExit( const TiXmlDocument& /*doc*/ )          { return true; }
00142 
00144     virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ )    { return true; }
00146     virtual bool VisitExit( const TiXmlElement& /*element*/ )       { return true; }
00147 
00149     virtual bool Visit( const TiXmlDeclaration& /*declaration*/ )   { return true; }
00151     virtual bool Visit( const TiXmlText& /*text*/ )                 { return true; }
00153     virtual bool Visit( const TiXmlComment& /*comment*/ )           { return true; }
00155     virtual bool Visit( const TiXmlUnknown& /*unknown*/ )           { return true; }
00156 };
00157 
00158 // Only used by Attribute::Query functions
00159 enum 
00160 { 
00161     TIXML_SUCCESS,
00162     TIXML_NO_ATTRIBUTE,
00163     TIXML_WRONG_TYPE
00164 };
00165 
00166 
00167 // Used by the parsing routines.
00168 enum TiXmlEncoding
00169 {
00170     TIXML_ENCODING_UNKNOWN,
00171     TIXML_ENCODING_UTF8,
00172     TIXML_ENCODING_LEGACY
00173 };
00174 
00175 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00176 
00199 class TiXmlBase
00200 {
00201     friend class TiXmlNode;
00202     friend class TiXmlElement;
00203     friend class TiXmlDocument;
00204 
00205 public:
00206     TiXmlBase() :   userData(0)     {}
00207     virtual ~TiXmlBase()            {}
00208 
00218     virtual void Print( FILE* cfile, int depth ) const = 0;
00219 
00226     static void SetCondenseWhiteSpace( bool condense )      { condenseWhiteSpace = condense; }
00227 
00229     static bool IsWhiteSpaceCondensed()                     { return condenseWhiteSpace; }
00230 
00249     int Row() const         { return location.row + 1; }
00250     int Column() const      { return location.col + 1; }    
00251 
00252     void  SetUserData( void* user )         { userData = user; }    
00253     void* GetUserData()                     { return userData; }    
00254     const void* GetUserData() const         { return userData; }    
00255 
00256     // Table that returs, for a given lead byte, the total number of bytes
00257     // in the UTF-8 sequence.
00258     static const int utf8ByteTable[256];
00259 
00260     virtual const char* Parse(  const char* p, 
00261                                 TiXmlParsingData* data, 
00262                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00263 
00267     static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out );
00268 
00269     enum
00270     {
00271         TIXML_NO_ERROR = 0,
00272         TIXML_ERROR,
00273         TIXML_ERROR_OPENING_FILE,
00274         TIXML_ERROR_OUT_OF_MEMORY,
00275         TIXML_ERROR_PARSING_ELEMENT,
00276         TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00277         TIXML_ERROR_READING_ELEMENT_VALUE,
00278         TIXML_ERROR_READING_ATTRIBUTES,
00279         TIXML_ERROR_PARSING_EMPTY,
00280         TIXML_ERROR_READING_END_TAG,
00281         TIXML_ERROR_PARSING_UNKNOWN,
00282         TIXML_ERROR_PARSING_COMMENT,
00283         TIXML_ERROR_PARSING_DECLARATION,
00284         TIXML_ERROR_DOCUMENT_EMPTY,
00285         TIXML_ERROR_EMBEDDED_NULL,
00286         TIXML_ERROR_PARSING_CDATA,
00287         TIXML_ERROR_DOCUMENT_TOP_ONLY,
00288 
00289         TIXML_ERROR_STRING_COUNT
00290     };
00291 
00292 protected:
00293 
00294     static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00295     inline static bool IsWhiteSpace( char c )       
00296     { 
00297         return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
00298     }
00299     inline static bool IsWhiteSpace( int c )
00300     {
00301         if ( c < 256 )
00302             return IsWhiteSpace( (char) c );
00303         return false;   // Again, only truly correct for English/Latin...but usually works.
00304     }
00305 
00306     #ifdef TIXML_USE_STL
00307     static bool StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
00308     static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
00309     #endif
00310 
00311     /*  Reads an XML name into the string provided. Returns
00312         a pointer just past the last character of the name,
00313         or 0 if the function has an error.
00314     */
00315     static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00316 
00317     /*  Reads text. Returns a pointer past the given end tag.
00318         Wickedly complex options, but it keeps the (sensitive) code in one place.
00319     */
00320     static const char* ReadText(    const char* in,             // where to start
00321                                     TIXML_STRING* text,         // the string read
00322                                     bool ignoreWhiteSpace,      // whether to keep the white space
00323                                     const char* endTag,         // what ends this text
00324                                     bool ignoreCase,            // whether to ignore case in the end tag
00325                                     TiXmlEncoding encoding );   // the current encoding
00326 
00327     // If an entity has been found, transform it into a character.
00328     static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00329 
00330     // Get a character, while interpreting entities.
00331     // The length can be from 0 to 4 bytes.
00332     inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00333     {
00334         assert( p );
00335         if ( encoding == TIXML_ENCODING_UTF8 )
00336         {
00337             *length = utf8ByteTable[ *((const unsigned char*)p) ];
00338             assert( *length >= 0 && *length < 5 );
00339         }
00340         else
00341         {
00342             *length = 1;
00343         }
00344 
00345         if ( *length == 1 )
00346         {
00347             if ( *p == '&' )
00348                 return GetEntity( p, _value, length, encoding );
00349             *_value = *p;
00350             return p+1;
00351         }
00352         else if ( *length )
00353         {
00354             //strncpy( _value, p, *length );    // lots of compilers don't like this function (unsafe),
00355                                                 // and the null terminator isn't needed
00356             for( int i=0; p[i] && i<*length; ++i ) {
00357                 _value[i] = p[i];
00358             }
00359             return p + (*length);
00360         }
00361         else
00362         {
00363             // Not valid text.
00364             return 0;
00365         }
00366     }
00367 
00368     // Return true if the next characters in the stream are any of the endTag sequences.
00369     // Ignore case only works for english, and should only be relied on when comparing
00370     // to English words: StringEqual( p, "version", true ) is fine.
00371     static bool StringEqual(    const char* p,
00372                                 const char* endTag,
00373                                 bool ignoreCase,
00374                                 TiXmlEncoding encoding );
00375 
00376     static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00377 
00378     TiXmlCursor location;
00379 
00381     void*           userData;
00382     
00383     // None of these methods are reliable for any language except English.
00384     // Good for approximation, not great for accuracy.
00385     static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00386     static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00387     inline static int ToLower( int v, TiXmlEncoding encoding )
00388     {
00389         if ( encoding == TIXML_ENCODING_UTF8 )
00390         {
00391             if ( v < 128 ) return tolower( v );
00392             return v;
00393         }
00394         else
00395         {
00396             return tolower( v );
00397         }
00398     }
00399     static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00400 
00401 private:
00402     TiXmlBase( const TiXmlBase& );              // not implemented.
00403     void operator=( const TiXmlBase& base );    // not allowed.
00404 
00405     struct Entity
00406     {
00407         const char*     str;
00408         unsigned int    strLength;
00409         char            chr;
00410     };
00411     enum
00412     {
00413         NUM_ENTITY = 5,
00414         MAX_ENTITY_LENGTH = 6
00415 
00416     };
00417     static Entity entity[ NUM_ENTITY ];
00418     static bool condenseWhiteSpace;
00419 };
00420 
00421 
00428 class TiXmlNode : public TiXmlBase
00429 {
00430     friend class TiXmlDocument;
00431     friend class TiXmlElement;
00432 
00433 public:
00434     #ifdef TIXML_USE_STL    
00435 
00439         friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00440 
00457         friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00458 
00460         friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00461 
00462     #endif
00463 
00467     enum NodeType
00468     {
00469         DOCUMENT,
00470         ELEMENT,
00471         COMMENT,
00472         UNKNOWN,
00473         TEXT,
00474         DECLARATION,
00475         TYPECOUNT
00476     };
00477 
00478     virtual ~TiXmlNode();
00479 
00492     const char *Value() const { return value.c_str (); }
00493 
00494     #ifdef TIXML_USE_STL
00495 
00499     const std::string& ValueStr() const { return value; }
00500     #endif
00501 
00502     const TIXML_STRING& ValueTStr() const { return value; }
00503 
00513     void SetValue(const char * _value) { value = _value;}
00514 
00515     #ifdef TIXML_USE_STL
00517     void SetValue( const std::string& _value )  { value = _value; }
00518     #endif
00519 
00521     void Clear();
00522 
00524     TiXmlNode* Parent()                         { return parent; }
00525     const TiXmlNode* Parent() const             { return parent; }
00526 
00527     const TiXmlNode* FirstChild()   const       { return firstChild; }  
00528     TiXmlNode* FirstChild()                     { return firstChild; }
00529     const TiXmlNode* FirstChild( const char * value ) const;            
00530 
00531     TiXmlNode* FirstChild( const char * _value ) {
00532         // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
00533         // call the method, cast the return back to non-const.
00534         return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
00535     }
00536     const TiXmlNode* LastChild() const  { return lastChild; }       
00537     TiXmlNode* LastChild()  { return lastChild; }
00538     
00539     const TiXmlNode* LastChild( const char * value ) const;         
00540     TiXmlNode* LastChild( const char * _value ) {
00541         return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
00542     }
00543 
00544     #ifdef TIXML_USE_STL
00545     const TiXmlNode* FirstChild( const std::string& _value ) const  {   return FirstChild (_value.c_str ());    }   
00546     TiXmlNode* FirstChild( const std::string& _value )              {   return FirstChild (_value.c_str ());    }   
00547     const TiXmlNode* LastChild( const std::string& _value ) const   {   return LastChild (_value.c_str ()); }   
00548     TiXmlNode* LastChild( const std::string& _value )               {   return LastChild (_value.c_str ()); }   
00549     #endif
00550 
00567     const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00568     TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
00569         return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
00570     }
00571 
00573     const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00574     TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
00575         return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
00576     }
00577 
00578     #ifdef TIXML_USE_STL
00579     const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {   return IterateChildren (_value.c_str (), previous); }   
00580     TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) {    return IterateChildren (_value.c_str (), previous); }   
00581     #endif
00582 
00586     TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00587 
00588 
00598     TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00599 
00603     TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00604 
00608     TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00609 
00613     TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00614 
00616     bool RemoveChild( TiXmlNode* removeThis );
00617 
00619     const TiXmlNode* PreviousSibling() const            { return prev; }
00620     TiXmlNode* PreviousSibling()                        { return prev; }
00621 
00623     const TiXmlNode* PreviousSibling( const char * ) const;
00624     TiXmlNode* PreviousSibling( const char *_prev ) {
00625         return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
00626     }
00627 
00628     #ifdef TIXML_USE_STL
00629     const TiXmlNode* PreviousSibling( const std::string& _value ) const {   return PreviousSibling (_value.c_str ());   }   
00630     TiXmlNode* PreviousSibling( const std::string& _value )             {   return PreviousSibling (_value.c_str ());   }   
00631     const TiXmlNode* NextSibling( const std::string& _value) const      {   return NextSibling (_value.c_str ());   }   
00632     TiXmlNode* NextSibling( const std::string& _value)                  {   return NextSibling (_value.c_str ());   }   
00633     #endif
00634 
00636     const TiXmlNode* NextSibling() const                { return next; }
00637     TiXmlNode* NextSibling()                            { return next; }
00638 
00640     const TiXmlNode* NextSibling( const char * ) const;
00641     TiXmlNode* NextSibling( const char* _next ) {
00642         return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
00643     }
00644 
00649     const TiXmlElement* NextSiblingElement() const;
00650     TiXmlElement* NextSiblingElement() {
00651         return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
00652     }
00653 
00658     const TiXmlElement* NextSiblingElement( const char * ) const;
00659     TiXmlElement* NextSiblingElement( const char *_next ) {
00660         return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
00661     }
00662 
00663     #ifdef TIXML_USE_STL
00664     const TiXmlElement* NextSiblingElement( const std::string& _value) const    {   return NextSiblingElement (_value.c_str ());    }   
00665     TiXmlElement* NextSiblingElement( const std::string& _value)                {   return NextSiblingElement (_value.c_str ());    }   
00666     #endif
00667 
00669     const TiXmlElement* FirstChildElement() const;
00670     TiXmlElement* FirstChildElement() {
00671         return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
00672     }
00673 
00675     const TiXmlElement* FirstChildElement( const char * _value ) const;
00676     TiXmlElement* FirstChildElement( const char * _value ) {
00677         return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
00678     }
00679 
00680     #ifdef TIXML_USE_STL
00681     const TiXmlElement* FirstChildElement( const std::string& _value ) const    {   return FirstChildElement (_value.c_str ()); }   
00682     TiXmlElement* FirstChildElement( const std::string& _value )                {   return FirstChildElement (_value.c_str ()); }   
00683     #endif
00684 
00689     int Type() const    { return type; }
00690 
00694     const TiXmlDocument* GetDocument() const;
00695     TiXmlDocument* GetDocument() {
00696         return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
00697     }
00698 
00700     bool NoChildren() const                     { return !firstChild; }
00701 
00702     virtual const TiXmlDocument*    ToDocument()    const { return 0; } 
00703     virtual const TiXmlElement*     ToElement()     const { return 0; } 
00704     virtual const TiXmlComment*     ToComment()     const { return 0; } 
00705     virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } 
00706     virtual const TiXmlText*        ToText()        const { return 0; } 
00707     virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } 
00708 
00709     virtual TiXmlDocument*          ToDocument()    { return 0; } 
00710     virtual TiXmlElement*           ToElement()     { return 0; } 
00711     virtual TiXmlComment*           ToComment()     { return 0; } 
00712     virtual TiXmlUnknown*           ToUnknown()     { return 0; } 
00713     virtual TiXmlText*              ToText()        { return 0; } 
00714     virtual TiXmlDeclaration*       ToDeclaration() { return 0; } 
00715 
00719     virtual TiXmlNode* Clone() const = 0;
00720 
00743     virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
00744 
00745 protected:
00746     TiXmlNode( NodeType _type );
00747 
00748     // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00749     // and the assignment operator.
00750     void CopyTo( TiXmlNode* target ) const;
00751 
00752     #ifdef TIXML_USE_STL
00753         // The real work of the input operator.
00754     virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
00755     #endif
00756 
00757     // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00758     TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00759 
00760     TiXmlNode*      parent;
00761     NodeType        type;
00762 
00763     TiXmlNode*      firstChild;
00764     TiXmlNode*      lastChild;
00765 
00766     TIXML_STRING    value;
00767 
00768     TiXmlNode*      prev;
00769     TiXmlNode*      next;
00770 
00771 private:
00772     TiXmlNode( const TiXmlNode& );              // not implemented.
00773     void operator=( const TiXmlNode& base );    // not allowed.
00774 };
00775 
00776 
00784 class TiXmlAttribute : public TiXmlBase
00785 {
00786     friend class TiXmlAttributeSet;
00787 
00788 public:
00790     TiXmlAttribute() : TiXmlBase()
00791     {
00792         document = 0;
00793         prev = next = 0;
00794     }
00795 
00796     #ifdef TIXML_USE_STL
00798     TiXmlAttribute( const std::string& _name, const std::string& _value )
00799     {
00800         name = _name;
00801         value = _value;
00802         document = 0;
00803         prev = next = 0;
00804     }
00805     #endif
00806 
00808     TiXmlAttribute( const char * _name, const char * _value )
00809     {
00810         name = _name;
00811         value = _value;
00812         document = 0;
00813         prev = next = 0;
00814     }
00815 
00816     const char*     Name()  const       { return name.c_str(); }        
00817     const char*     Value() const       { return value.c_str(); }       
00818     #ifdef TIXML_USE_STL
00819     const std::string& ValueStr() const { return value; }               
00820     #endif
00821     int             IntValue() const;                                   
00822     double          DoubleValue() const;                                
00823 
00824     // Get the tinyxml string representation
00825     const TIXML_STRING& NameTStr() const { return name; }
00826 
00836     int QueryIntValue( int* _value ) const;
00838     int QueryDoubleValue( double* _value ) const;
00839 
00840     void SetName( const char* _name )   { name = _name; }               
00841     void SetValue( const char* _value ) { value = _value; }             
00842 
00843     void SetIntValue( int _value );                                     
00844     void SetDoubleValue( double _value );                               
00845 
00846     #ifdef TIXML_USE_STL
00848     void SetName( const std::string& _name )    { name = _name; }   
00850     void SetValue( const std::string& _value )  { value = _value; }
00851     #endif
00852 
00854     const TiXmlAttribute* Next() const;
00855     TiXmlAttribute* Next() {
00856         return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() ); 
00857     }
00858 
00860     const TiXmlAttribute* Previous() const;
00861     TiXmlAttribute* Previous() {
00862         return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() ); 
00863     }
00864 
00865     bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00866     bool operator<( const TiXmlAttribute& rhs )  const { return name < rhs.name; }
00867     bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00868 
00869     /*  Attribute parsing starts: first letter of the name
00870                          returns: the next char after the value end quote
00871     */
00872     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00873 
00874     // Prints this Attribute to a FILE stream.
00875     virtual void Print( FILE* cfile, int depth ) const {
00876         Print( cfile, depth, 0 );
00877     }
00878     void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
00879 
00880     // [internal use]
00881     // Set the document pointer so the attribute can report errors.
00882     void SetDocument( TiXmlDocument* doc )  { document = doc; }
00883 
00884 private:
00885     TiXmlAttribute( const TiXmlAttribute& );                // not implemented.
00886     void operator=( const TiXmlAttribute& base );   // not allowed.
00887 
00888     TiXmlDocument*  document;   // A pointer back to a document, for error reporting.
00889     TIXML_STRING name;
00890     TIXML_STRING value;
00891     TiXmlAttribute* prev;
00892     TiXmlAttribute* next;
00893 };
00894 
00895 
00896 /*  A class used to manage a group of attributes.
00897     It is only used internally, both by the ELEMENT and the DECLARATION.
00898     
00899     The set can be changed transparent to the Element and Declaration
00900     classes that use it, but NOT transparent to the Attribute
00901     which has to implement a next() and previous() method. Which makes
00902     it a bit problematic and prevents the use of STL.
00903 
00904     This version is implemented with circular lists because:
00905         - I like circular lists
00906         - it demonstrates some independence from the (typical) doubly linked list.
00907 */
00908 class TiXmlAttributeSet
00909 {
00910 public:
00911     TiXmlAttributeSet();
00912     ~TiXmlAttributeSet();
00913 
00914     void Add( TiXmlAttribute* attribute );
00915     void Remove( TiXmlAttribute* attribute );
00916 
00917     const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00918     TiXmlAttribute* First()                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00919     const TiXmlAttribute* Last() const      { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00920     TiXmlAttribute* Last()                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00921 
00922     const TiXmlAttribute*   Find( const char* _name ) const;
00923     TiXmlAttribute* Find( const char* _name ) {
00924         return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
00925     }
00926     #ifdef TIXML_USE_STL
00927     const TiXmlAttribute*   Find( const std::string& _name ) const;
00928     TiXmlAttribute* Find( const std::string& _name ) {
00929         return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
00930     }
00931 
00932     #endif
00933 
00934 private:
00935     //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00936     //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00937     TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00938     void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
00939 
00940     TiXmlAttribute sentinel;
00941 };
00942 
00943 
00948 class TiXmlElement : public TiXmlNode
00949 {
00950 public:
00952     TiXmlElement (const char * in_value);
00953 
00954     #ifdef TIXML_USE_STL
00956     TiXmlElement( const std::string& _value );
00957     #endif
00958 
00959     TiXmlElement( const TiXmlElement& );
00960 
00961     void operator=( const TiXmlElement& base );
00962 
00963     virtual ~TiXmlElement();
00964 
00968     const char* Attribute( const char* name ) const;
00969 
00976     const char* Attribute( const char* name, int* i ) const;
00977 
00984     const char* Attribute( const char* name, double* d ) const;
00985 
00993     int QueryIntAttribute( const char* name, int* _value ) const;
00995     int QueryDoubleAttribute( const char* name, double* _value ) const;
00997     int QueryFloatAttribute( const char* name, float* _value ) const {
00998         double d;
00999         int result = QueryDoubleAttribute( name, &d );
01000         if ( result == TIXML_SUCCESS ) {
01001             *_value = (float)d;
01002         }
01003         return result;
01004     }
01005 
01006     #ifdef TIXML_USE_STL
01007 
01015     template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
01016     {
01017         const TiXmlAttribute* node = attributeSet.Find( name );
01018         if ( !node )
01019             return TIXML_NO_ATTRIBUTE;
01020 
01021         std::stringstream sstream( node->ValueStr() );
01022         sstream >> *outValue;
01023         if ( !sstream.fail() )
01024             return TIXML_SUCCESS;
01025         return TIXML_WRONG_TYPE;
01026     }
01027     /*
01028      This is - in theory - a bug fix for "QueryValueAtribute returns truncated std::string"
01029      but template specialization is hard to get working cross-compiler. Leaving the bug for now.
01030      
01031     // The above will fail for std::string because the space character is used as a seperator.
01032     // Specialize for strings. Bug [ 1695429 ] QueryValueAtribute returns truncated std::string
01033     template<> int QueryValueAttribute( const std::string& name, std::string* outValue ) const
01034     {
01035         const TiXmlAttribute* node = attributeSet.Find( name );
01036         if ( !node )
01037             return TIXML_NO_ATTRIBUTE;
01038         *outValue = node->ValueStr();
01039         return TIXML_SUCCESS;
01040     }
01041     */
01042     #endif
01043 
01047     void SetAttribute( const char* name, const char * _value );
01048 
01049     #ifdef TIXML_USE_STL
01050     const std::string* Attribute( const std::string& name ) const;
01051     const std::string* Attribute( const std::string& name, int* i ) const;
01052     const std::string* Attribute( const std::string& name, double* d ) const;
01053     int QueryIntAttribute( const std::string& name, int* _value ) const;
01054     int QueryDoubleAttribute( const std::string& name, double* _value ) const;
01055 
01057     void SetAttribute( const std::string& name, const std::string& _value );
01059     void SetAttribute( const std::string& name, int _value );
01060     #endif
01061 
01065     void SetAttribute( const char * name, int value );
01066 
01070     void SetDoubleAttribute( const char * name, double value );
01071 
01074     void RemoveAttribute( const char * name );
01075     #ifdef TIXML_USE_STL
01076     void RemoveAttribute( const std::string& name ) {   RemoveAttribute (name.c_str ());    }   
01077     #endif
01078 
01079     const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }        
01080     TiXmlAttribute* FirstAttribute()                { return attributeSet.First(); }
01081     const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }     
01082     TiXmlAttribute* LastAttribute()                 { return attributeSet.Last(); }
01083 
01116     const char* GetText() const;
01117 
01119     virtual TiXmlNode* Clone() const;
01120     // Print the Element to a FILE stream.
01121     virtual void Print( FILE* cfile, int depth ) const;
01122 
01123     /*  Attribtue parsing starts: next char past '<'
01124                          returns: next char past '>'
01125     */
01126     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01127 
01128     virtual const TiXmlElement*     ToElement()     const { return this; } 
01129     virtual TiXmlElement*           ToElement()           { return this; } 
01130 
01133     virtual bool Accept( TiXmlVisitor* visitor ) const;
01134 
01135 protected:
01136 
01137     void CopyTo( TiXmlElement* target ) const;
01138     void ClearThis();   // like clear, but initializes 'this' object as well
01139 
01140     // Used to be public [internal use]
01141     #ifdef TIXML_USE_STL
01142     virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01143     #endif
01144     /*  [internal use]
01145         Reads the "value" of the element -- another element, or text.
01146         This should terminate with the current end tag.
01147     */
01148     const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01149 
01150 private:
01151 
01152     TiXmlAttributeSet attributeSet;
01153 };
01154 
01155 
01158 class TiXmlComment : public TiXmlNode
01159 {
01160 public:
01162     TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01164     TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::COMMENT ) {
01165         SetValue( _value );
01166     }
01167     TiXmlComment( const TiXmlComment& );
01168     void operator=( const TiXmlComment& base );
01169 
01170     virtual ~TiXmlComment() {}
01171 
01173     virtual TiXmlNode* Clone() const;
01174     // Write this Comment to a FILE stream.
01175     virtual void Print( FILE* cfile, int depth ) const;
01176 
01177     /*  Attribtue parsing starts: at the ! of the !--
01178                          returns: next char past '>'
01179     */
01180     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01181 
01182     virtual const TiXmlComment*  ToComment() const { return this; } 
01183     virtual TiXmlComment*  ToComment() { return this; } 
01184 
01187     virtual bool Accept( TiXmlVisitor* visitor ) const;
01188 
01189 protected:
01190     void CopyTo( TiXmlComment* target ) const;
01191 
01192     // used to be public
01193     #ifdef TIXML_USE_STL
01194     virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01195     #endif
01196 //  virtual void StreamOut( TIXML_OSTREAM * out ) const;
01197 
01198 private:
01199 
01200 };
01201 
01202 
01208 class TiXmlText : public TiXmlNode
01209 {
01210     friend class TiXmlElement;
01211 public:
01216     TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01217     {
01218         SetValue( initValue );
01219         cdata = false;
01220     }
01221     virtual ~TiXmlText() {}
01222 
01223     #ifdef TIXML_USE_STL
01225     TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01226     {
01227         SetValue( initValue );
01228         cdata = false;
01229     }
01230     #endif
01231 
01232     TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )   { copy.CopyTo( this ); }
01233     void operator=( const TiXmlText& base )                             { base.CopyTo( this ); }
01234 
01235     // Write this text object to a FILE stream.
01236     virtual void Print( FILE* cfile, int depth ) const;
01237 
01239     bool CDATA() const              { return cdata; }
01241     void SetCDATA( bool _cdata )    { cdata = _cdata; }
01242 
01243     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01244 
01245     virtual const TiXmlText* ToText() const { return this; } 
01246     virtual TiXmlText*       ToText()       { return this; } 
01247 
01250     virtual bool Accept( TiXmlVisitor* content ) const;
01251 
01252 protected :
01254     virtual TiXmlNode* Clone() const;
01255     void CopyTo( TiXmlText* target ) const;
01256 
01257     bool Blank() const; // returns true if all white space and new lines
01258     // [internal use]
01259     #ifdef TIXML_USE_STL
01260     virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01261     #endif
01262 
01263 private:
01264     bool cdata;         // true if this should be input and output as a CDATA style text element
01265 };
01266 
01267 
01281 class TiXmlDeclaration : public TiXmlNode
01282 {
01283 public:
01285     TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01286 
01287 #ifdef TIXML_USE_STL
01289     TiXmlDeclaration(   const std::string& _version,
01290                         const std::string& _encoding,
01291                         const std::string& _standalone );
01292 #endif
01293 
01295     TiXmlDeclaration(   const char* _version,
01296                         const char* _encoding,
01297                         const char* _standalone );
01298 
01299     TiXmlDeclaration( const TiXmlDeclaration& copy );
01300     void operator=( const TiXmlDeclaration& copy );
01301 
01302     virtual ~TiXmlDeclaration() {}
01303 
01305     const char *Version() const         { return version.c_str (); }
01307     const char *Encoding() const        { return encoding.c_str (); }
01309     const char *Standalone() const      { return standalone.c_str (); }
01310 
01312     virtual TiXmlNode* Clone() const;
01313     // Print this declaration to a FILE stream.
01314     virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
01315     virtual void Print( FILE* cfile, int depth ) const {
01316         Print( cfile, depth, 0 );
01317     }
01318 
01319     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01320 
01321     virtual const TiXmlDeclaration* ToDeclaration() const { return this; } 
01322     virtual TiXmlDeclaration*       ToDeclaration()       { return this; } 
01323 
01326     virtual bool Accept( TiXmlVisitor* visitor ) const;
01327 
01328 protected:
01329     void CopyTo( TiXmlDeclaration* target ) const;
01330     // used to be public
01331     #ifdef TIXML_USE_STL
01332     virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01333     #endif
01334 
01335 private:
01336 
01337     TIXML_STRING version;
01338     TIXML_STRING encoding;
01339     TIXML_STRING standalone;
01340 };
01341 
01342 
01350 class TiXmlUnknown : public TiXmlNode
01351 {
01352 public:
01353     TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )    {}
01354     virtual ~TiXmlUnknown() {}
01355 
01356     TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )      { copy.CopyTo( this ); }
01357     void operator=( const TiXmlUnknown& copy )                                      { copy.CopyTo( this ); }
01358 
01360     virtual TiXmlNode* Clone() const;
01361     // Print this Unknown to a FILE stream.
01362     virtual void Print( FILE* cfile, int depth ) const;
01363 
01364     virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01365 
01366     virtual const TiXmlUnknown*     ToUnknown()     const { return this; } 
01367     virtual TiXmlUnknown*           ToUnknown()     { return this; } 
01368 
01371     virtual bool Accept( TiXmlVisitor* content ) const;
01372 
01373 protected:
01374     void CopyTo( TiXmlUnknown* target ) const;
01375 
01376     #ifdef TIXML_USE_STL
01377     virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01378     #endif
01379 
01380 private:
01381 
01382 };
01383 
01384 
01389 class TiXmlDocument : public TiXmlNode
01390 {
01391 public:
01393     TiXmlDocument();
01395     TiXmlDocument( const char * documentName );
01396 
01397     #ifdef TIXML_USE_STL
01399     TiXmlDocument( const std::string& documentName );
01400     #endif
01401 
01402     TiXmlDocument( const TiXmlDocument& copy );
01403     void operator=( const TiXmlDocument& copy );
01404 
01405     virtual ~TiXmlDocument() {}
01406 
01411     bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01413     bool SaveFile() const;
01415     bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01417     bool SaveFile( const char * filename ) const;
01423     bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01425     bool SaveFile( FILE* ) const;
01426 
01427     #ifdef TIXML_USE_STL
01428     bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )           
01429     {
01430 //      StringToBuffer f( filename );
01431 //      return ( f.buffer && LoadFile( f.buffer, encoding ));
01432         return LoadFile( filename.c_str(), encoding );
01433     }
01434     bool SaveFile( const std::string& filename ) const      
01435     {
01436 //      StringToBuffer f( filename );
01437 //      return ( f.buffer && SaveFile( f.buffer ));
01438         return SaveFile( filename.c_str() );
01439     }
01440     #endif
01441 
01446     virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01447 
01452     const TiXmlElement* RootElement() const     { return FirstChildElement(); }
01453     TiXmlElement* RootElement()                 { return FirstChildElement(); }
01454 
01460     bool Error() const                      { return error; }
01461 
01463     const char * ErrorDesc() const  { return errorDesc.c_str (); }
01464 
01468     int ErrorId()   const               { return errorId; }
01469 
01477     int ErrorRow() const    { return errorLocation.row+1; }
01478     int ErrorCol() const    { return errorLocation.col+1; } 
01479 
01504     void SetTabSize( int _tabsize )     { tabsize = _tabsize; }
01505 
01506     int TabSize() const { return tabsize; }
01507 
01511     void ClearError()                       {   error = false; 
01512                                                 errorId = 0; 
01513                                                 errorDesc = ""; 
01514                                                 errorLocation.row = errorLocation.col = 0; 
01515                                                 //errorLocation.last = 0; 
01516                                             }
01517 
01519     void Print() const                      { Print( stdout, 0 ); }
01520 
01521     /* Write the document to a string using formatted printing ("pretty print"). This
01522         will allocate a character array (new char[]) and return it as a pointer. The
01523         calling code pust call delete[] on the return char* to avoid a memory leak.
01524     */
01525     //char* PrintToMemory() const; 
01526 
01528     virtual void Print( FILE* cfile, int depth = 0 ) const;
01529     // [internal use]
01530     void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01531 
01532     virtual const TiXmlDocument*    ToDocument()    const { return this; } 
01533     virtual TiXmlDocument*          ToDocument()          { return this; } 
01534 
01537     virtual bool Accept( TiXmlVisitor* content ) const;
01538 
01539 protected :
01540     // [internal use]
01541     virtual TiXmlNode* Clone() const;
01542     #ifdef TIXML_USE_STL
01543     virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
01544     #endif
01545 
01546 private:
01547     void CopyTo( TiXmlDocument* target ) const;
01548 
01549     bool error;
01550     int  errorId;
01551     TIXML_STRING errorDesc;
01552     int tabsize;
01553     TiXmlCursor errorLocation;
01554     bool useMicrosoftBOM;       // the UTF-8 BOM were found when read. Note this, and try to write.
01555 };
01556 
01557 
01638 class TiXmlHandle
01639 {
01640 public:
01642     TiXmlHandle( TiXmlNode* _node )                 { this->node = _node; }
01644     TiXmlHandle( const TiXmlHandle& ref )           { this->node = ref.node; }
01645     TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01646 
01648     TiXmlHandle FirstChild() const;
01650     TiXmlHandle FirstChild( const char * value ) const;
01652     TiXmlHandle FirstChildElement() const;
01654     TiXmlHandle FirstChildElement( const char * value ) const;
01655 
01659     TiXmlHandle Child( const char* value, int index ) const;
01663     TiXmlHandle Child( int index ) const;
01668     TiXmlHandle ChildElement( const char* value, int index ) const;
01673     TiXmlHandle ChildElement( int index ) const;
01674 
01675     #ifdef TIXML_USE_STL
01676     TiXmlHandle FirstChild( const std::string& _value ) const               { return FirstChild( _value.c_str() ); }
01677     TiXmlHandle FirstChildElement( const std::string& _value ) const        { return FirstChildElement( _value.c_str() ); }
01678 
01679     TiXmlHandle Child( const std::string& _value, int index ) const         { return Child( _value.c_str(), index ); }
01680     TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01681     #endif
01682 
01685     TiXmlNode* ToNode() const           { return node; } 
01688     TiXmlElement* ToElement() const     { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01691     TiXmlText* ToText() const           { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01694     TiXmlUnknown* ToUnknown() const     { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01695 
01699     TiXmlNode* Node() const         { return ToNode(); } 
01703     TiXmlElement* Element() const   { return ToElement(); }
01707     TiXmlText* Text() const         { return ToText(); }
01711     TiXmlUnknown* Unknown() const   { return ToUnknown(); }
01712 
01713 private:
01714     TiXmlNode* node;
01715 };
01716 
01717 
01737 class TiXmlPrinter : public TiXmlVisitor
01738 {
01739 public:
01740     TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
01741                      buffer(), indent( "    " ), lineBreak( "\n" ) {}
01742 
01743     virtual bool VisitEnter( const TiXmlDocument& doc );
01744     virtual bool VisitExit( const TiXmlDocument& doc );
01745 
01746     virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
01747     virtual bool VisitExit( const TiXmlElement& element );
01748 
01749     virtual bool Visit( const TiXmlDeclaration& declaration );
01750     virtual bool Visit( const TiXmlText& text );
01751     virtual bool Visit( const TiXmlComment& comment );
01752     virtual bool Visit( const TiXmlUnknown& unknown );
01753 
01757     void SetIndent( const char* _indent )           { indent = _indent ? _indent : "" ; }
01759     const char* Indent()                            { return indent.c_str(); }
01764     void SetLineBreak( const char* _lineBreak )     { lineBreak = _lineBreak ? _lineBreak : ""; }
01766     const char* LineBreak()                         { return lineBreak.c_str(); }
01767 
01771     void SetStreamPrinting()                        { indent = "";
01772                                                       lineBreak = "";
01773                                                     }   
01775     const char* CStr()                              { return buffer.c_str(); }
01777     size_t Size()                                   { return buffer.size(); }
01778 
01779     #ifdef TIXML_USE_STL
01781     const std::string& Str()                        { return buffer; }
01782     #endif
01783 
01784 private:
01785     void DoIndent() {
01786         for( int i=0; i<depth; ++i )
01787             buffer += indent;
01788     }
01789     void DoLineBreak() {
01790         buffer += lineBreak;
01791     }
01792 
01793     int depth;
01794     bool simpleTextPrint;
01795     TIXML_STRING buffer;
01796     TIXML_STRING indent;
01797     TIXML_STRING lineBreak;
01798 };
01799 
01800 
01801 #ifdef _MSC_VER
01802 #pragma warning( pop )
01803 #endif
01804 
01805 #endif
01806 

The miarn project - written by Joao Xavier