Main Page | Class Hierarchy | Class List | File List | Class Members

tinyxml.h

00001 /*
00002 www.sourceforge.net/projects/tinyxml
00003 Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
00004 
00005 This software is provided 'as-is', without any express or implied
00006 warranty. In no event will the authors be held liable for any
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any
00010 purpose, including commercial applications, and to alter it and
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must
00014 not claim that you wrote the original software. If you use this
00015 software in a product, an acknowledgment in the product documentation
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source
00022 distribution.
00023 
00024  F.Gaede, DESY : added #define TIXML_USE_STL  for use with gear
00025                : put everything in namespace gear
00026 
00027    $Id: tinyxml.h,v 1.1.1.1 2005/09/13 14:41:03 gaede Exp $
00028 */
00029 
00030 #ifndef TIXML_USE_STL
00031 #define TIXML_USE_STL
00032 #endif
00033 
00034 #ifndef TINYXML_INCLUDED
00035 #define TINYXML_INCLUDED
00036 
00037 #ifdef _MSC_VER
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 #if defined( DEBUG ) && defined( _MSC_VER )
00054 #include <windows.h>
00055 #define TIXML_LOG OutputDebugString
00056 #else
00057 #define TIXML_LOG printf
00058 #endif
00059 
00060 #ifdef TIXML_USE_STL
00061         #include <string>
00062         #include <iostream>
00063         #define TIXML_STRING    std::string
00064         #define TIXML_ISTREAM   std::istream
00065         #define TIXML_OSTREAM   std::ostream
00066 #else
00067         #include "tinystr.h"
00068         #define TIXML_STRING    TiXmlString
00069         #define TIXML_OSTREAM   TiXmlOutStream
00070 #endif
00071 
00072 
00073 //fg: put it namespace gear
00074 namespace gear {
00075 
00076 class TiXmlDocument;
00077 class TiXmlElement;
00078 class TiXmlComment;
00079 class TiXmlUnknown;
00080 class TiXmlAttribute;
00081 class TiXmlText;
00082 class TiXmlDeclaration;
00083 class TiXmlParsingData;
00084 
00085 const int TIXML_MAJOR_VERSION = 2;
00086 const int TIXML_MINOR_VERSION = 3;
00087 const int TIXML_PATCH_VERSION = 4;
00088 
00089 /*      Internal structure for tracking location of items 
00090         in the XML file.
00091 */
00092 struct TiXmlCursor
00093 {
00094         TiXmlCursor()           { Clear(); }
00095         void Clear()            { row = col = -1; }
00096 
00097         int row;        // 0 based.
00098         int col;        // 0 based.
00099 };
00100 
00101 
00102 // Only used by Attribute::Query functions
00103 enum 
00104 { 
00105         TIXML_SUCCESS,
00106         TIXML_NO_ATTRIBUTE,
00107         TIXML_WRONG_TYPE
00108 };
00109 
00110 
00111 // Used by the parsing routines.
00112 enum TiXmlEncoding
00113 {
00114         TIXML_ENCODING_UNKNOWN,
00115         TIXML_ENCODING_UTF8,
00116         TIXML_ENCODING_LEGACY
00117 };
00118 
00119 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00120 
00143 class TiXmlBase
00144 {
00145         friend class TiXmlNode;
00146         friend class TiXmlElement;
00147         friend class TiXmlDocument;
00148 
00149 public:
00150         TiXmlBase()     :       userData(0) {}
00151         virtual ~TiXmlBase()                                    {}
00152 
00158         virtual void Print( FILE* cfile, int depth ) const = 0;
00159 
00166         static void SetCondenseWhiteSpace( bool condense )              { condenseWhiteSpace = condense; }
00167 
00169         static bool IsWhiteSpaceCondensed()                                             { return condenseWhiteSpace; }
00170 
00189         int Row() const                 { return location.row + 1; }
00190         int Column() const              { return location.col + 1; }    
00191 
00192         void  SetUserData( void* user )                 { userData = user; }
00193         void* GetUserData()                                             { return userData; }
00194 
00195         // Table that returs, for a given lead byte, the total number of bytes
00196         // in the UTF-8 sequence.
00197         static const int utf8ByteTable[256];
00198 
00199         virtual const char* Parse(      const char* p, 
00200                                                                 TiXmlParsingData* data, 
00201                                                                 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00202 
00203         enum
00204         {
00205                 TIXML_NO_ERROR = 0,
00206                 TIXML_ERROR,
00207                 TIXML_ERROR_OPENING_FILE,
00208                 TIXML_ERROR_OUT_OF_MEMORY,
00209                 TIXML_ERROR_PARSING_ELEMENT,
00210                 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00211                 TIXML_ERROR_READING_ELEMENT_VALUE,
00212                 TIXML_ERROR_READING_ATTRIBUTES,
00213                 TIXML_ERROR_PARSING_EMPTY,
00214                 TIXML_ERROR_READING_END_TAG,
00215                 TIXML_ERROR_PARSING_UNKNOWN,
00216                 TIXML_ERROR_PARSING_COMMENT,
00217                 TIXML_ERROR_PARSING_DECLARATION,
00218                 TIXML_ERROR_DOCUMENT_EMPTY,
00219                 TIXML_ERROR_EMBEDDED_NULL,
00220 
00221                 TIXML_ERROR_STRING_COUNT
00222         };
00223 
00224 protected:
00225 
00226         // See STL_STRING_BUG
00227         // Utility class to overcome a bug.
00228         class StringToBuffer
00229         {
00230           public:
00231                 StringToBuffer( const TIXML_STRING& str );
00232                 ~StringToBuffer();
00233                 char* buffer;
00234         };
00235 
00236         static const char*      SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00237         inline static bool      IsWhiteSpace( char c )          
00238         { 
00239                 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
00240         }
00241 
00242         virtual void StreamOut (TIXML_OSTREAM *) const = 0;
00243 
00244         #ifdef TIXML_USE_STL
00245             static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00246             static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00247         #endif
00248 
00249         /*      Reads an XML name into the string provided. Returns
00250                 a pointer just past the last character of the name,
00251                 or 0 if the function has an error.
00252         */
00253         static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00254 
00255         /*      Reads text. Returns a pointer past the given end tag.
00256                 Wickedly complex options, but it keeps the (sensitive) code in one place.
00257         */
00258         static const char* ReadText(    const char* in,                         // where to start
00259                                                                         TIXML_STRING* text,                     // the string read
00260                                                                         bool ignoreWhiteSpace,          // whether to keep the white space
00261                                                                         const char* endTag,                     // what ends this text
00262                                                                         bool ignoreCase,                        // whether to ignore case in the end tag
00263                                                                         TiXmlEncoding encoding );       // the current encoding
00264 
00265         // If an entity has been found, transform it into a character.
00266         static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00267 
00268         // Get a character, while interpreting entities.
00269         // The length can be from 0 to 4 bytes.
00270         inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00271         {
00272                 assert( p );
00273                 if ( encoding == TIXML_ENCODING_UTF8 )
00274                 {
00275                         *length = utf8ByteTable[ *((unsigned char*)p) ];
00276                         assert( *length >= 0 && *length < 5 );
00277                 }
00278                 else
00279                 {
00280                         *length = 1;
00281                 }
00282 
00283                 if ( *length == 1 )
00284                 {
00285                         if ( *p == '&' )
00286                                 return GetEntity( p, _value, length, encoding );
00287                         *_value = *p;
00288                         return p+1;
00289                 }
00290                 else if ( *length )
00291                 {
00292                         strncpy( _value, p, *length );
00293                         return p + (*length);
00294                 }
00295                 else
00296                 {
00297                         // Not valid text.
00298                         return 0;
00299                 }
00300         }
00301 
00302         // Puts a string to a stream, expanding entities as it goes.
00303         // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00304         static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00305 
00306         static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00307 
00308         // Return true if the next characters in the stream are any of the endTag sequences.
00309         // Ignore case only works for english, and should only be relied on when comparing
00310         // to Engilish words: StringEqual( p, "version", true ) is fine.
00311         static bool StringEqual(        const char* p,
00312                                                                 const char* endTag,
00313                                                                 bool ignoreCase,
00314                                                                 TiXmlEncoding encoding );
00315 
00316         static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00317 
00318         TiXmlCursor location;
00319 
00321         void*                   userData;
00322         
00323         // None of these methods are reliable for any language except English.
00324         // Good for approximation, not great for accuracy.
00325         static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00326         static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00327         inline static int ToLower( int v, TiXmlEncoding encoding )
00328         {
00329                 if ( encoding == TIXML_ENCODING_UTF8 )
00330                 {
00331                         if ( v < 128 ) return tolower( v );
00332                         return v;
00333                 }
00334                 else
00335                 {
00336                         return tolower( v );
00337                 }
00338         }
00339         static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00340 
00341 private:
00342         TiXmlBase( const TiXmlBase& );                          // not implemented.
00343         void operator=( const TiXmlBase& base );        // not allowed.
00344 
00345         struct Entity
00346         {
00347                 const char*     str;
00348                 unsigned int    strLength;
00349                 char                chr;
00350         };
00351         enum
00352         {
00353                 NUM_ENTITY = 5,
00354                 MAX_ENTITY_LENGTH = 6
00355 
00356         };
00357         static Entity entity[ NUM_ENTITY ];
00358         static bool condenseWhiteSpace;
00359 };
00360 
00361 
00368 class TiXmlNode : public TiXmlBase
00369 {
00370         friend class TiXmlDocument;
00371         friend class TiXmlElement;
00372 
00373 public:
00374         #ifdef TIXML_USE_STL    
00375 
00379             friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00380 
00397             friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00398 
00400                 friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00401 
00402         #else
00403             // Used internally, not part of the public API.
00404             friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00405         #endif
00406 
00410         enum NodeType
00411         {
00412                 DOCUMENT,
00413                 ELEMENT,
00414                 COMMENT,
00415                 UNKNOWN,
00416                 TEXT,
00417                 DECLARATION,
00418                 TYPECOUNT
00419         };
00420 
00421         virtual ~TiXmlNode();
00422 
00435         const char * Value() const { return value.c_str (); }
00436 
00446         void SetValue(const char * _value) { value = _value;}
00447 
00448     #ifdef TIXML_USE_STL
00449 
00450         void SetValue( const std::string& _value )    
00451         {         
00452                 StringToBuffer buf( _value );
00453                 SetValue( buf.buffer ? buf.buffer : "" );       
00454         }       
00455         #endif
00456 
00458         void Clear();
00459 
00461         TiXmlNode* Parent()                                                     { return parent; }
00462         const TiXmlNode* Parent() const                         { return parent; }
00463 
00464         const TiXmlNode* FirstChild()   const   { return firstChild; }          
00465         TiXmlNode* FirstChild()                                 { return firstChild; }
00466         const TiXmlNode* FirstChild( const char * value ) const;                        
00467         TiXmlNode* FirstChild( const char * value );                                            
00468 
00469         const TiXmlNode* LastChild() const      { return lastChild; }           
00470         TiXmlNode* LastChild()  { return lastChild; }
00471         const TiXmlNode* LastChild( const char * value ) const;                 
00472         TiXmlNode* LastChild( const char * value );     
00473 
00474     #ifdef TIXML_USE_STL
00475         const TiXmlNode* FirstChild( const std::string& _value ) const  {       return FirstChild (_value.c_str ());    }       
00476         TiXmlNode* FirstChild( const std::string& _value )                              {       return FirstChild (_value.c_str ());    }       
00477         const TiXmlNode* LastChild( const std::string& _value ) const   {       return LastChild (_value.c_str ());     }       
00478         TiXmlNode* LastChild( const std::string& _value )                               {       return LastChild (_value.c_str ());     }       
00479         #endif
00480 
00497         const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00498         TiXmlNode* IterateChildren( TiXmlNode* previous );
00499 
00501         const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00502         TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00503 
00504     #ifdef TIXML_USE_STL
00505         const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {       return IterateChildren (_value.c_str (), previous);     }       
00506         TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous);     }       
00507         #endif
00508 
00512         TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00513 
00514 
00524         TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00525 
00529         TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00530 
00534         TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00535 
00539         TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00540 
00542         bool RemoveChild( TiXmlNode* removeThis );
00543 
00545         const TiXmlNode* PreviousSibling() const                        { return prev; }
00546         TiXmlNode* PreviousSibling()                                            { return prev; }
00547 
00549         const TiXmlNode* PreviousSibling( const char * ) const;
00550         TiXmlNode* PreviousSibling( const char * );
00551 
00552     #ifdef TIXML_USE_STL
00553         const TiXmlNode* PreviousSibling( const std::string& _value ) const     {       return PreviousSibling (_value.c_str ());       }       
00554         TiXmlNode* PreviousSibling( const std::string& _value )                         {       return PreviousSibling (_value.c_str ());       }       
00555         const TiXmlNode* NextSibling( const std::string& _value) const          {       return NextSibling (_value.c_str ());   }       
00556         TiXmlNode* NextSibling( const std::string& _value)                                      {       return NextSibling (_value.c_str ());   }       
00557         #endif
00558 
00560         const TiXmlNode* NextSibling() const                            { return next; }
00561         TiXmlNode* NextSibling()                                                        { return next; }
00562 
00564         const TiXmlNode* NextSibling( const char * ) const;
00565         TiXmlNode* NextSibling( const char * );
00566 
00571         const TiXmlElement* NextSiblingElement() const;
00572         TiXmlElement* NextSiblingElement();
00573 
00578         const TiXmlElement* NextSiblingElement( const char * ) const;
00579         TiXmlElement* NextSiblingElement( const char * );
00580 
00581     #ifdef TIXML_USE_STL
00582         const TiXmlElement* NextSiblingElement( const std::string& _value) const        {       return NextSiblingElement (_value.c_str ());    }       
00583         TiXmlElement* NextSiblingElement( const std::string& _value)                            {       return NextSiblingElement (_value.c_str ());    }       
00584         #endif
00585 
00587         const TiXmlElement* FirstChildElement() const;
00588         TiXmlElement* FirstChildElement();
00589 
00591         const TiXmlElement* FirstChildElement( const char * value ) const;
00592         TiXmlElement* FirstChildElement( const char * value );
00593 
00594     #ifdef TIXML_USE_STL
00595         const TiXmlElement* FirstChildElement( const std::string& _value ) const        {       return FirstChildElement (_value.c_str ());     }       
00596         TiXmlElement* FirstChildElement( const std::string& _value )                            {       return FirstChildElement (_value.c_str ());     }       
00597         #endif
00598 
00603         virtual int Type() const        { return type; }
00604 
00608         const TiXmlDocument* GetDocument() const;
00609         TiXmlDocument* GetDocument();
00610 
00612         bool NoChildren() const                                         { return !firstChild; }
00613 
00614         const TiXmlDocument* ToDocument()       const           { return ( this && type == DOCUMENT ) ? (const TiXmlDocument*) this : 0; } 
00615         const TiXmlElement*  ToElement() const                  { return ( this && type == ELEMENT  ) ? (const TiXmlElement*)  this : 0; } 
00616         const TiXmlComment*  ToComment() const                  { return ( this && type == COMMENT  ) ? (const TiXmlComment*)  this : 0; } 
00617         const TiXmlUnknown*  ToUnknown() const                  { return ( this && type == UNKNOWN  ) ? (const TiXmlUnknown*)  this : 0; } 
00618         const TiXmlText*           ToText()    const            { return ( this && type == TEXT     ) ? (const TiXmlText*)     this : 0; } 
00619         const TiXmlDeclaration* ToDeclaration() const   { return ( this && type == DECLARATION ) ? (const TiXmlDeclaration*) this : 0; } 
00620 
00621         TiXmlDocument* ToDocument()                     { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } 
00622         TiXmlElement*  ToElement()                      { return ( this && type == ELEMENT  ) ? (TiXmlElement*)  this : 0; } 
00623         TiXmlComment*  ToComment()                      { return ( this && type == COMMENT  ) ? (TiXmlComment*)  this : 0; } 
00624         TiXmlUnknown*  ToUnknown()                      { return ( this && type == UNKNOWN  ) ? (TiXmlUnknown*)  this : 0; } 
00625         TiXmlText*         ToText()                     { return ( this && type == TEXT     ) ? (TiXmlText*)     this : 0; } 
00626         TiXmlDeclaration* ToDeclaration()       { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } 
00627 
00631         virtual TiXmlNode* Clone() const = 0;
00632 
00633 protected:
00634         TiXmlNode( NodeType _type );
00635 
00636         // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00637         // and the assignment operator.
00638         void CopyTo( TiXmlNode* target ) const;
00639 
00640         #ifdef TIXML_USE_STL
00641             // The real work of the input operator.
00642             virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00643         #endif
00644 
00645         // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00646         TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00647 
00648         // Internal Value function returning a TIXML_STRING
00649         const TIXML_STRING& SValue() const      { return value ; }
00650 
00651         TiXmlNode*              parent;
00652         NodeType                type;
00653 
00654         TiXmlNode*              firstChild;
00655         TiXmlNode*              lastChild;
00656 
00657         TIXML_STRING    value;
00658 
00659         TiXmlNode*              prev;
00660         TiXmlNode*              next;
00661 
00662 private:
00663         TiXmlNode( const TiXmlNode& );                          // not implemented.
00664         void operator=( const TiXmlNode& base );        // not allowed.
00665 };
00666 
00667 
00675 class TiXmlAttribute : public TiXmlBase
00676 {
00677         friend class TiXmlAttributeSet;
00678 
00679 public:
00681         TiXmlAttribute() : TiXmlBase()
00682         {
00683                 document = 0;
00684                 prev = next = 0;
00685         }
00686 
00687         #ifdef TIXML_USE_STL
00688 
00689         TiXmlAttribute( const std::string& _name, const std::string& _value )
00690         {
00691                 name = _name;
00692                 value = _value;
00693                 document = 0;
00694                 prev = next = 0;
00695         }
00696         #endif
00697 
00699         TiXmlAttribute( const char * _name, const char * _value )
00700         {
00701                 name = _name;
00702                 value = _value;
00703                 document = 0;
00704                 prev = next = 0;
00705         }
00706 
00707         const char*             Name()  const           { return name.c_str (); }               
00708         const char*             Value() const           { return value.c_str (); }              
00709         const int       IntValue() const;                                                                       
00710         const double    DoubleValue() const;                                                            
00711 
00721         int QueryIntValue( int* value ) const;
00723         int QueryDoubleValue( double* value ) const;
00724 
00725         void SetName( const char* _name )       { name = _name; }                               
00726         void SetValue( const char* _value )     { value = _value; }                             
00727 
00728         void SetIntValue( int value );                                                                          
00729         void SetDoubleValue( double value );                                                            
00730 
00731     #ifdef TIXML_USE_STL
00732 
00733         void SetName( const std::string& _name )        
00734         {       
00735                 StringToBuffer buf( _name );
00736                 SetName ( buf.buffer ? buf.buffer : "error" );  
00737         }
00739         void SetValue( const std::string& _value )      
00740         {       
00741                 StringToBuffer buf( _value );
00742                 SetValue( buf.buffer ? buf.buffer : "error" );  
00743         }
00744         #endif
00745 
00747         const TiXmlAttribute* Next() const;
00748         TiXmlAttribute* Next();
00750         const TiXmlAttribute* Previous() const;
00751         TiXmlAttribute* Previous();
00752 
00753         bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00754         bool operator<( const TiXmlAttribute& rhs )      const { return name < rhs.name; }
00755         bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00756 
00757         /*      Attribute parsing starts: first letter of the name
00758                                                  returns: the next char after the value end quote
00759         */
00760         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00761 
00762         // Prints this Attribute to a FILE stream.
00763         virtual void Print( FILE* cfile, int depth ) const;
00764 
00765         virtual void StreamOut( TIXML_OSTREAM * out ) const;
00766         // [internal use]
00767         // Set the document pointer so the attribute can report errors.
00768         void SetDocument( TiXmlDocument* doc )  { document = doc; }
00769 
00770 private:
00771         TiXmlAttribute( const TiXmlAttribute& );                                // not implemented.
00772         void operator=( const TiXmlAttribute& base );   // not allowed.
00773 
00774         TiXmlDocument*  document;       // A pointer back to a document, for error reporting.
00775         TIXML_STRING name;
00776         TIXML_STRING value;
00777         TiXmlAttribute* prev;
00778         TiXmlAttribute* next;
00779 };
00780 
00781 
00782 /*      A class used to manage a group of attributes.
00783         It is only used internally, both by the ELEMENT and the DECLARATION.
00784         
00785         The set can be changed transparent to the Element and Declaration
00786         classes that use it, but NOT transparent to the Attribute
00787         which has to implement a next() and previous() method. Which makes
00788         it a bit problematic and prevents the use of STL.
00789 
00790         This version is implemented with circular lists because:
00791                 - I like circular lists
00792                 - it demonstrates some independence from the (typical) doubly linked list.
00793 */
00794 class TiXmlAttributeSet
00795 {
00796 public:
00797         TiXmlAttributeSet();
00798         ~TiXmlAttributeSet();
00799 
00800         void Add( TiXmlAttribute* attribute );
00801         void Remove( TiXmlAttribute* attribute );
00802 
00803         const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00804         TiXmlAttribute* First()                                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00805         const TiXmlAttribute* Last() const              { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00806         TiXmlAttribute* Last()                                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00807 
00808         const TiXmlAttribute*   Find( const char * name ) const;
00809         TiXmlAttribute* Find( const char * name );
00810 
00811 private:
00812         //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00813         //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00814         TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00815         void operator=( const TiXmlAttributeSet& );     // not allowed (as TiXmlAttribute)
00816 
00817         TiXmlAttribute sentinel;
00818 };
00819 
00820 
00825 class TiXmlElement : public TiXmlNode
00826 {
00827 public:
00829         TiXmlElement (const char * in_value);
00830 
00831         #ifdef TIXML_USE_STL
00832 
00833         TiXmlElement( const std::string& _value );
00834         #endif
00835 
00836         TiXmlElement( const TiXmlElement& );
00837 
00838         void operator=( const TiXmlElement& base );
00839 
00840         virtual ~TiXmlElement();
00841 
00845         const char* Attribute( const char* name ) const;
00846 
00853         const char* Attribute( const char* name, int* i ) const;
00854 
00861         const char* Attribute( const char* name, double* d ) const;
00862 
00870         int QueryIntAttribute( const char* name, int* value ) const;
00872         int QueryDoubleAttribute( const char* name, double* value ) const;
00874         int QueryDoubleAttribute( const char* name, float* value ) const {
00875                 double d;
00876                 int result = QueryDoubleAttribute( name, &d );
00877                 *value = (float)d;
00878                 return result;
00879         }
00880 
00884         void SetAttribute( const char* name, const char * value );
00885 
00886     #ifdef TIXML_USE_STL
00887         const char* Attribute( const std::string& name ) const                          { return Attribute( name.c_str() ); }
00888         const char* Attribute( const std::string& name, int* i ) const          { return Attribute( name.c_str(), i ); }
00889         const char* Attribute( const std::string& name, double* d ) const       { return Attribute( name.c_str(), d ); }
00890         int QueryIntAttribute( const std::string& name, int* value ) const      { return QueryIntAttribute( name.c_str(), value ); }
00891         int QueryDoubleAttribute( const std::string& name, double* value ) const { return QueryDoubleAttribute( name.c_str(), value ); }
00892 
00894         void SetAttribute( const std::string& name, const std::string& _value ) 
00895         {       
00896                 StringToBuffer n( name );
00897                 StringToBuffer v( _value );
00898                 if ( n.buffer && v.buffer )
00899                         SetAttribute (n.buffer, v.buffer );     
00900         }       
00902         void SetAttribute( const std::string& name, int _value )        
00903         {       
00904                 StringToBuffer n( name );
00905                 if ( n.buffer )
00906                         SetAttribute (n.buffer, _value);        
00907         }       
00908         #endif
00909 
00913         void SetAttribute( const char * name, int value );
00914 
00918         void SetDoubleAttribute( const char * name, double value );
00919 
00922         void RemoveAttribute( const char * name );
00923     #ifdef TIXML_USE_STL
00924         void RemoveAttribute( const std::string& name ) {       RemoveAttribute (name.c_str ());        }       
00925         #endif
00926 
00927         const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }                
00928         TiXmlAttribute* FirstAttribute()                                { return attributeSet.First(); }
00929         const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }         
00930         TiXmlAttribute* LastAttribute()                                 { return attributeSet.Last(); }
00931 
00933         virtual TiXmlNode* Clone() const;
00934         // Print the Element to a FILE stream.
00935         virtual void Print( FILE* cfile, int depth ) const;
00936 
00937         /*      Attribtue parsing starts: next char past '<'
00938                                                  returns: next char past '>'
00939         */
00940         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00941 
00942 protected:
00943 
00944         void CopyTo( TiXmlElement* target ) const;
00945         void ClearThis();       // like clear, but initializes 'this' object as well
00946 
00947         // Used to be public [internal use]
00948         #ifdef TIXML_USE_STL
00949             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
00950         #endif
00951         virtual void StreamOut( TIXML_OSTREAM * out ) const;
00952 
00953         /*      [internal use]
00954                 Reads the "value" of the element -- another element, or text.
00955                 This should terminate with the current end tag.
00956         */
00957         const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
00958 
00959 private:
00960 
00961         TiXmlAttributeSet attributeSet;
00962 };
00963 
00964 
00967 class TiXmlComment : public TiXmlNode
00968 {
00969 public:
00971         TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
00972         TiXmlComment( const TiXmlComment& );
00973         void operator=( const TiXmlComment& base );
00974 
00975         virtual ~TiXmlComment() {}
00976 
00978         virtual TiXmlNode* Clone() const;
00980         virtual void Print( FILE* cfile, int depth ) const;
00981 
00982         /*      Attribtue parsing starts: at the ! of the !--
00983                                                  returns: next char past '>'
00984         */
00985         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00986 
00987 protected:
00988         void CopyTo( TiXmlComment* target ) const;
00989 
00990         // used to be public
00991         #ifdef TIXML_USE_STL
00992             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
00993         #endif
00994         virtual void StreamOut( TIXML_OSTREAM * out ) const;
00995 
00996 private:
00997 
00998 };
00999 
01000 
01003 class TiXmlText : public TiXmlNode
01004 {
01005         friend class TiXmlElement;
01006 public:
01008         TiXmlText (const char * initValue) : TiXmlNode (TiXmlNode::TEXT)
01009         {
01010                 SetValue( initValue );
01011         }
01012         virtual ~TiXmlText() {}
01013 
01014         #ifdef TIXML_USE_STL
01015 
01016         TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01017         {
01018                 SetValue( initValue );
01019         }
01020         #endif
01021 
01022         TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )       { copy.CopyTo( this ); }
01023         void operator=( const TiXmlText& base )                                                         { base.CopyTo( this ); }
01024 
01026         virtual void Print( FILE* cfile, int depth ) const;
01027 
01028         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01029 
01030 protected :
01032         virtual TiXmlNode* Clone() const;
01033         void CopyTo( TiXmlText* target ) const;
01034 
01035         virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01036         bool Blank() const;     // returns true if all white space and new lines
01037         // [internal use]
01038         #ifdef TIXML_USE_STL
01039             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01040         #endif
01041 
01042 private:
01043 };
01044 
01045 
01059 class TiXmlDeclaration : public TiXmlNode
01060 {
01061 public:
01063         TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01064 
01065 #ifdef TIXML_USE_STL
01066 
01067         TiXmlDeclaration(       const std::string& _version,
01068                                                 const std::string& _encoding,
01069                                                 const std::string& _standalone );
01070 #endif
01071 
01073         TiXmlDeclaration(       const char* _version,
01074                                                 const char* _encoding,
01075                                                 const char* _standalone );
01076 
01077         TiXmlDeclaration( const TiXmlDeclaration& copy );
01078         void operator=( const TiXmlDeclaration& copy );
01079 
01080         virtual ~TiXmlDeclaration()     {}
01081 
01083         const char *Version() const                     { return version.c_str (); }
01085         const char *Encoding() const            { return encoding.c_str (); }
01087         const char *Standalone() const          { return standalone.c_str (); }
01088 
01090         virtual TiXmlNode* Clone() const;
01092         virtual void Print( FILE* cfile, int depth ) const;
01093 
01094         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01095 
01096 protected:
01097         void CopyTo( TiXmlDeclaration* target ) const;
01098         // used to be public
01099         #ifdef TIXML_USE_STL
01100             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01101         #endif
01102         virtual void StreamOut ( TIXML_OSTREAM * out) const;
01103 
01104 private:
01105 
01106         TIXML_STRING version;
01107         TIXML_STRING encoding;
01108         TIXML_STRING standalone;
01109 };
01110 
01111 
01119 class TiXmlUnknown : public TiXmlNode
01120 {
01121 public:
01122         TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )        {}
01123         virtual ~TiXmlUnknown() {}
01124 
01125         TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )              { copy.CopyTo( this ); }
01126         void operator=( const TiXmlUnknown& copy )                                                                              { copy.CopyTo( this ); }
01127 
01129         virtual TiXmlNode* Clone() const;
01131         virtual void Print( FILE* cfile, int depth ) const;
01132 
01133         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01134 
01135 protected:
01136         void CopyTo( TiXmlUnknown* target ) const;
01137 
01138         #ifdef TIXML_USE_STL
01139             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01140         #endif
01141         virtual void StreamOut ( TIXML_OSTREAM * out ) const;
01142 
01143 private:
01144 
01145 };
01146 
01147 
01152 class TiXmlDocument : public TiXmlNode
01153 {
01154 public:
01156         TiXmlDocument();
01158         TiXmlDocument( const char * documentName );
01159 
01160         #ifdef TIXML_USE_STL
01161 
01162         TiXmlDocument( const std::string& documentName );
01163         #endif
01164 
01165         TiXmlDocument( const TiXmlDocument& copy );
01166         void operator=( const TiXmlDocument& copy );
01167 
01168         virtual ~TiXmlDocument() {}
01169 
01174         bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01176         bool SaveFile() const;
01178         bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01180         bool SaveFile( const char * filename ) const;
01181 
01182         #ifdef TIXML_USE_STL
01183         bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )                   
01184         {
01185                 StringToBuffer f( filename );
01186                 return ( f.buffer && LoadFile( f.buffer, encoding ));
01187         }
01188         bool SaveFile( const std::string& filename ) const              
01189         {
01190                 StringToBuffer f( filename );
01191                 return ( f.buffer && SaveFile( f.buffer ));
01192         }
01193         #endif
01194 
01199         virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01200 
01205         const TiXmlElement* RootElement() const         { return FirstChildElement(); }
01206         TiXmlElement* RootElement()                                     { return FirstChildElement(); }
01207 
01213         bool Error() const                                              { return error; }
01214 
01216         const char * ErrorDesc() const  { return errorDesc.c_str (); }
01217 
01221         const int ErrorId()     const                           { return errorId; }
01222 
01230         int ErrorRow()  { return errorLocation.row+1; }
01231         int ErrorCol()  { return errorLocation.col+1; } 
01232 
01253         void SetTabSize( int _tabsize )         { tabsize = _tabsize; }
01254 
01255         int TabSize() const     { return tabsize; }
01256 
01260         void ClearError()                                               {       error = false; 
01261                                                                                                 errorId = 0; 
01262                                                                                                 errorDesc = ""; 
01263                                                                                                 errorLocation.row = errorLocation.col = 0; 
01264                                                                                                 //errorLocation.last = 0; 
01265                                                                                         }
01266 
01268         void Print() const                                              { Print( stdout, 0 ); }
01269 
01271         virtual void Print( FILE* cfile, int depth = 0 ) const;
01272         // [internal use]
01273         void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01274 
01275 protected :
01276         virtual void StreamOut ( TIXML_OSTREAM * out) const;
01277         // [internal use]
01278         virtual TiXmlNode* Clone() const;
01279         #ifdef TIXML_USE_STL
01280             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01281         #endif
01282 
01283 private:
01284         void CopyTo( TiXmlDocument* target ) const;
01285 
01286         bool error;
01287         int  errorId;
01288         TIXML_STRING errorDesc;
01289         int tabsize;
01290         TiXmlCursor errorLocation;
01291 };
01292 
01293 
01374 class TiXmlHandle
01375 {
01376 public:
01378         TiXmlHandle( TiXmlNode* node )                                  { this->node = node; }
01380         TiXmlHandle( const TiXmlHandle& ref )                   { this->node = ref.node; }
01381         TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01382 
01384         TiXmlHandle FirstChild() const;
01386         TiXmlHandle FirstChild( const char * value ) const;
01388         TiXmlHandle FirstChildElement() const;
01390         TiXmlHandle FirstChildElement( const char * value ) const;
01391 
01395         TiXmlHandle Child( const char* value, int index ) const;
01399         TiXmlHandle Child( int index ) const;
01404         TiXmlHandle ChildElement( const char* value, int index ) const;
01409         TiXmlHandle ChildElement( int index ) const;
01410 
01411         #ifdef TIXML_USE_STL
01412         TiXmlHandle FirstChild( const std::string& _value ) const                               { return FirstChild( _value.c_str() ); }
01413         TiXmlHandle FirstChildElement( const std::string& _value ) const                { return FirstChildElement( _value.c_str() ); }
01414 
01415         TiXmlHandle Child( const std::string& _value, int index ) const                 { return Child( _value.c_str(), index ); }
01416         TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01417         #endif
01418 
01420         TiXmlNode* Node() const                 { return node; } 
01422         TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01424         TiXmlText* Text() const                 { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01426         TiXmlUnknown* Unknown() const                   { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01427 
01428 private:
01429         TiXmlNode* node;
01430 };
01431 
01432 
01433 
01434 //fg: put it namespace gear
01435 } // end namespace gear {
01436 
01437 
01438 
01439 #ifdef _MSC_VER
01440 #pragma warning( default : 4530 )
01441 #pragma warning( default : 4786 )
01442 #endif
01443 
01444 #endif
01445 

Generated on Tue Sep 5 11:36:30 2006 for Gear by doxygen 1.3.5