Yate

tinyxml.h

00001 /*
00002 www.sourceforge.net/projects/tinyxml
00003 Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
00004 
00005 This software is provided 'as-is', without any express or implied
00006 warranty. In no event will the authors be held liable for any
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any
00010 purpose, including commercial applications, and to alter it and
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must
00014 not claim that you wrote the original software. If you use this
00015 software in a product, an acknowledgment in the product documentation
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source
00022 distribution.
00023 */
00024 
00025 #ifndef TINYXML_INCLUDED
00026 #define TINYXML_INCLUDED
00027 
00028 #include <yateclass.h>
00029 
00030 #ifdef _MSC_VER
00031 #pragma warning( push )
00032 #pragma warning( disable : 4530 )
00033 #pragma warning( disable : 4786 )
00034 #endif
00035 
00036 #include <ctype.h>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include <assert.h>
00041 
00042 // Help out windows:
00043 #if defined( _DEBUG ) && !defined( DEBUG )
00044 #define DEBUG
00045 #endif
00046 
00047 #ifdef TIXML_USE_STL
00048         #include <string>
00049         #include <iostream>
00050         #define TIXML_STRING    std::string
00051         #define TIXML_ISTREAM   std::istream
00052         #define TIXML_OSTREAM   std::ostream
00053 #else
00054         #include "tinystr.h"
00055         #define TIXML_STRING    TiXmlString
00056         #define TIXML_OSTREAM   TiXmlOutStream
00057 #endif
00058 
00059 // Deprecated library function hell. Compilers want to use the
00060 // new safe versions. This probably doesn't fully address the problem,
00061 // but it gets closer. There are too many compilers for me to fully
00062 // test. If you get compilation troubles, undefine TIXML_SAFE
00063 
00064 #define TIXML_SAFE              // TinyXml isn't fully buffer overrun protected, safe code. This is work in progress.
00065 #ifdef TIXML_SAFE
00066         #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
00067                 // Microsoft visual studio, version 2005 and higher.
00068                 #define TIXML_SNPRINTF _snprintf_s
00069                 #define TIXML_SNSCANF  _snscanf_s
00070         #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
00071                 // Microsoft visual studio, version 6 and higher.
00072                 //#pragma message( "Using _sn* functions." )
00073                 #define TIXML_SNPRINTF _snprintf
00074                 #define TIXML_SNSCANF  _snscanf
00075         #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00076                 // GCC version 3 and higher.s
00077                 //#warning( "Using sn* functions." )
00078                 #define TIXML_SNPRINTF snprintf
00079                 #define TIXML_SNSCANF  snscanf
00080         #endif
00081 #endif  
00082 
00083 #ifdef _WINDOWS
00084 
00085 #ifdef LIBYXML_EXPORTS
00086 #define YXML_API __declspec(dllexport)
00087 #else
00088 #ifndef LIBYXML_STATIC
00089 #define YXML_API __declspec(dllimport)
00090 #endif
00091 #endif
00092 
00093 #endif /* _WINDOWS */
00094 
00095 #ifndef YXML_API
00096 #define YXML_API
00097 #endif
00098 
00102 namespace TelEngine {
00103 
00104 class TiXmlDocument;
00105 class TiXmlElement;
00106 class TiXmlComment;
00107 class TiXmlUnknown;
00108 class TiXmlAttribute;
00109 class TiXmlText;
00110 class TiXmlDeclaration;
00111 class TiXmlParsingData;
00112 
00113 const int TIXML_MAJOR_VERSION = 2;
00114 const int TIXML_MINOR_VERSION = 4;
00115 const int TIXML_PATCH_VERSION = 3;
00116 
00117 /*      Internal structure for tracking location of items 
00118         in the XML file.
00119 */
00120 struct YXML_API TiXmlCursor
00121 {
00122         TiXmlCursor()           { Clear(); }
00123         void Clear()            { row = col = -1; }
00124 
00125         int row;        // 0 based.
00126         int col;        // 0 based.
00127 };
00128 
00129 
00130 // Only used by Attribute::Query functions
00131 enum 
00132 { 
00133         TIXML_SUCCESS,
00134         TIXML_NO_ATTRIBUTE,
00135         TIXML_WRONG_TYPE
00136 };
00137 
00138 
00139 // Used by the parsing routines.
00140 enum TiXmlEncoding
00141 {
00142         TIXML_ENCODING_UNKNOWN,
00143         TIXML_ENCODING_UTF8,
00144         TIXML_ENCODING_LEGACY
00145 };
00146 
00147 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
00148 
00171 class YXML_API TiXmlBase
00172 {
00173         friend class TiXmlNode;
00174         friend class TiXmlElement;
00175         friend class TiXmlDocument;
00176 
00177 public:
00178         TiXmlBase()     :       userData(0) {}
00179         virtual ~TiXmlBase()                                    {}
00180 
00186         virtual void Print( FILE* cfile, int depth ) const = 0;
00187 
00194         static void SetCondenseWhiteSpace( bool condense )              { condenseWhiteSpace = condense; }
00195 
00197         static bool IsWhiteSpaceCondensed()                                             { return condenseWhiteSpace; }
00198 
00217         int Row() const                 { return location.row + 1; }
00218         int Column() const              { return location.col + 1; }    
00219 
00220         void  SetUserData( void* user )                 { userData = user; }
00221         void* GetUserData()                                             { return userData; }
00222 
00223         // Table that returs, for a given lead byte, the total number of bytes
00224         // in the UTF-8 sequence.
00225         static const int utf8ByteTable[256];
00226 
00227 
00228 
00229         virtual const char* Parse(const char* p, 
00230                                   TiXmlParsingData* data, 
00231                                   TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
00232 
00233         enum
00234         {
00235                 TIXML_NO_ERROR = 0,
00236                 TIXML_ERROR,
00237                 TIXML_ERROR_OPENING_FILE,
00238                 TIXML_ERROR_OUT_OF_MEMORY,
00239                 TIXML_ERROR_PARSING_ELEMENT,
00240                 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
00241                 TIXML_ERROR_READING_ELEMENT_VALUE,
00242                 TIXML_ERROR_READING_ATTRIBUTES,
00243                 TIXML_ERROR_PARSING_EMPTY,
00244                 TIXML_ERROR_READING_END_TAG,
00245                 TIXML_ERROR_PARSING_UNKNOWN,
00246                 TIXML_ERROR_PARSING_COMMENT,
00247                 TIXML_ERROR_PARSING_DECLARATION,
00248                 TIXML_ERROR_DOCUMENT_EMPTY,
00249                 TIXML_ERROR_EMBEDDED_NULL,
00250                 TIXML_ERROR_PARSING_CDATA,
00251                 TIXML_ERROR_INCOMPLETE,
00252                 TIXML_ERROR_BUFFEROVERRUN,
00253 
00254                 TIXML_ERROR_STRING_COUNT
00255         };
00256 
00257 protected:
00258 
00259         // See STL_STRING_BUG
00260         // Utility class to overcome a bug.
00261         class StringToBuffer
00262         {
00263           public:
00264                 StringToBuffer( const TIXML_STRING& str );
00265                 ~StringToBuffer();
00266                 char* buffer;
00267         };
00268 
00269         static const char*      SkipWhiteSpace( const char*, TiXmlEncoding encoding );
00270         inline static bool      IsWhiteSpace( char c )          
00271         { 
00272                 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
00273         }
00274         inline static bool      IsWhiteSpace( int c )
00275         {
00276                 if ( c < 256 )
00277                         return IsWhiteSpace( (char) c );
00278                 return false;   // Again, only truly correct for English/Latin...but usually works.
00279         }
00280 
00281 public:
00282         virtual void StreamOut (TIXML_OSTREAM *, bool = false) const = 0;
00283 
00284 protected:
00285         #ifdef TIXML_USE_STL
00286             static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
00287             static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
00288         #endif
00289 
00290         /*      Reads an XML name into the string provided. Returns
00291                 a pointer just past the last character of the name,
00292                 or 0 if p or *p are 0.
00293         */
00294         static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
00295 
00296         /*      Reads text. Returns a pointer past the given end tag.
00297                 Wickedly complex options, but it keeps the (sensitive) code in one place.
00298                 *** Return 0 if end tag is not found.
00299                 *** Set eof to true if data ended before end tag.
00300         */
00301         static const char* ReadText(const char* in,              // where to start
00302                                     TIXML_STRING* text,          // the string read
00303                                     bool ignoreWhiteSpace,       // whether to keep the white space
00304                                     const char* endTag,          // what ends this text
00305                                     bool ignoreCase,             // whether to ignore case in the end tag
00306                                     TiXmlEncoding encoding,      // the current encoding
00307                                     bool* eof = 0);              // Optional flag set to true if data finishes before endTag
00308 
00309         // If an entity has been found, transform it into a character.
00310         static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
00311 
00312         // Get a character, while interpreting entities.
00313         // The length can be from 0 to 4 bytes.
00314         inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
00315         {
00316                 assert( p );
00317                 if ( encoding == TIXML_ENCODING_UTF8 )
00318                 {
00319                         *length = utf8ByteTable[ *((unsigned char*)p) ];
00320                         assert( *length >= 0 && *length < 5 );
00321                 }
00322                 else
00323                 {
00324                         *length = 1;
00325                 }
00326 
00327                 if ( *length == 1 )
00328                 {
00329                         if ( *p == '&' )
00330                                 return GetEntity( p, _value, length, encoding );
00331                         *_value = *p;
00332                         return p+1;
00333                 }
00334                 else if ( *length )
00335                 {
00336                         //strncpy( _value, p, *length );
00337                         // lots of compilers don't like this function (unsafe),
00338                         // and the null terminator isn't needed
00339                         for( int i=0; p[i] && i<*length; ++i ) {
00340                                 _value[i] = p[i];
00341                         }
00342                         return p + (*length);
00343                 }
00344                 else
00345                 {
00346                         // Not valid text.
00347                         return 0;
00348                 }
00349         }
00350 
00351         // Puts a string to a stream, expanding entities as it goes.
00352         // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
00353         static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
00354 
00355         static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
00356 
00357         // Return true if the next characters in the stream are any of the endTag sequences.
00358         // Ignore case only works for english, and should only be relied on when comparing
00359         // to English words: StringEqual( p, "version", true ) is fine.
00360         // eof is set to true if p ended before endTag
00361         static bool StringEqual(const char* p,
00362                                 const char* endTag,
00363                                 bool ignoreCase,
00364                                 TiXmlEncoding encoding,
00365                                 bool* eof = 0);
00366 
00367         static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
00368 
00369         TiXmlCursor location;
00370 
00372         void*                   userData;
00373         
00374         // None of these methods are reliable for any language except English.
00375         // Good for approximation, not great for accuracy.
00376         static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
00377         static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
00378         inline static int ToLower( int v, TiXmlEncoding encoding )
00379         {
00380                 if ( encoding == TIXML_ENCODING_UTF8 )
00381                 {
00382                         if ( v < 128 ) return tolower( v );
00383                         return v;
00384                 }
00385                 else
00386                 {
00387                         return tolower( v );
00388                 }
00389         }
00390         static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
00391 
00392 private:
00393         TiXmlBase( const TiXmlBase& );                          // not implemented.
00394         void operator=( const TiXmlBase& base );        // not allowed.
00395 
00396         struct Entity
00397         {
00398                 const char*     str;
00399                 unsigned int    strLength;
00400                 char                chr;
00401         };
00402         enum
00403         {
00404                 NUM_ENTITY = 5,
00405                 MAX_ENTITY_LENGTH = 6
00406 
00407         };
00408         static Entity entity[ NUM_ENTITY ];
00409         static bool condenseWhiteSpace;
00410 };
00411 
00412 
00419 class YXML_API TiXmlNode : public TiXmlBase
00420 {
00421         friend class TiXmlDocument;
00422         friend class TiXmlElement;
00423 
00424 public:
00425         #ifdef TIXML_USE_STL    
00426 
00430             friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
00431 
00448             friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
00449 
00451                 friend std::string& operator<< (std::string& out, const TiXmlNode& base );
00452 
00453         #else
00454             // Used internally, not part of the public API.
00455             friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
00456         #endif
00457 
00461         enum NodeType
00462         {
00463                 DOCUMENT,
00464                 ELEMENT,
00465                 COMMENT,
00466                 UNKNOWN,
00467                 TEXT,
00468                 DECLARATION,
00469                 TYPECOUNT
00470         };
00471 
00472         virtual ~TiXmlNode();
00473 
00486         const char *Value() const { return value.c_str (); }
00487 
00488     #ifdef TIXML_USE_STL
00489 
00493         const std::string& ValueStr() const { return value; }
00494         #endif
00495 
00505         void SetValue(const char * _value) { value = _value;}
00506 
00507     #ifdef TIXML_USE_STL
00508 
00509         void SetValue( const std::string& _value )      { value = _value; }
00510         #endif
00511 
00513         void Clear();
00514 
00516         TiXmlNode* Parent()                                                     { return parent; }
00517         const TiXmlNode* Parent() const                         { return parent; }
00518 
00519         const TiXmlNode* FirstChild()   const   { return firstChild; }          
00520         TiXmlNode* FirstChild()                                 { return firstChild; }
00521         const TiXmlNode* FirstChild( const char * value ) const;                        
00522         TiXmlNode* FirstChild( const char * value );                                            
00523 
00524         const TiXmlNode* LastChild() const      { return lastChild; }           
00525         TiXmlNode* LastChild()  { return lastChild; }
00526         const TiXmlNode* LastChild( const char * value ) const;                 
00527         TiXmlNode* LastChild( const char * value );     
00528 
00529     #ifdef TIXML_USE_STL
00530         const TiXmlNode* FirstChild( const std::string& _value ) const  {       return FirstChild (_value.c_str ());    }       
00531         TiXmlNode* FirstChild( const std::string& _value )                              {       return FirstChild (_value.c_str ());    }       
00532         const TiXmlNode* LastChild( const std::string& _value ) const   {       return LastChild (_value.c_str ());     }       
00533         TiXmlNode* LastChild( const std::string& _value )                               {       return LastChild (_value.c_str ());     }       
00534         #endif
00535 
00552         const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
00553         TiXmlNode* IterateChildren( TiXmlNode* previous );
00554 
00556         const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
00557         TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
00558 
00559     #ifdef TIXML_USE_STL
00560         const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {       return IterateChildren (_value.c_str (), previous);     }       
00561         TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous);     }       
00562         #endif
00563 
00567         TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
00568 
00569 
00579         TiXmlNode* LinkEndChild( TiXmlNode* addThis );
00580 
00584         TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
00585 
00589         TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
00590 
00594         TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
00595 
00597         bool RemoveChild( TiXmlNode* removeThis, bool del = true );
00598 
00600         const TiXmlNode* PreviousSibling() const                        { return prev; }
00601         TiXmlNode* PreviousSibling()                                            { return prev; }
00602 
00604         const TiXmlNode* PreviousSibling( const char * ) const;
00605         TiXmlNode* PreviousSibling( const char * );
00606 
00607     #ifdef TIXML_USE_STL
00608         const TiXmlNode* PreviousSibling( const std::string& _value ) const     {       return PreviousSibling (_value.c_str ());       }       
00609         TiXmlNode* PreviousSibling( const std::string& _value )                         {       return PreviousSibling (_value.c_str ());       }       
00610         const TiXmlNode* NextSibling( const std::string& _value) const          {       return NextSibling (_value.c_str ());   }       
00611         TiXmlNode* NextSibling( const std::string& _value)                                      {       return NextSibling (_value.c_str ());   }       
00612         #endif
00613 
00615         const TiXmlNode* NextSibling() const                            { return next; }
00616         TiXmlNode* NextSibling()                                                        { return next; }
00617 
00619         const TiXmlNode* NextSibling( const char * ) const;
00620         TiXmlNode* NextSibling( const char * );
00621 
00626         const TiXmlElement* NextSiblingElement() const;
00627         TiXmlElement* NextSiblingElement();
00628 
00633         const TiXmlElement* NextSiblingElement( const char * ) const;
00634         TiXmlElement* NextSiblingElement( const char * );
00635 
00636     #ifdef TIXML_USE_STL
00637         const TiXmlElement* NextSiblingElement( const std::string& _value) const        {       return NextSiblingElement (_value.c_str ());    }       
00638         TiXmlElement* NextSiblingElement( const std::string& _value)                            {       return NextSiblingElement (_value.c_str ());    }       
00639         #endif
00640 
00642         const TiXmlElement* FirstChildElement() const;
00643         TiXmlElement* FirstChildElement();
00644 
00646         const TiXmlElement* FirstChildElement( const char * value ) const;
00647         TiXmlElement* FirstChildElement( const char * value );
00648 
00649     #ifdef TIXML_USE_STL
00650         const TiXmlElement* FirstChildElement( const std::string& _value ) const        {       return FirstChildElement (_value.c_str ());     }       
00651         TiXmlElement* FirstChildElement( const std::string& _value )                            {       return FirstChildElement (_value.c_str ());     }       
00652         #endif
00653 
00658         int Type() const        { return type; }
00659 
00663         const TiXmlDocument* GetDocument() const;
00664         TiXmlDocument* GetDocument();
00665 
00667         bool NoChildren() const                                         { return !firstChild; }
00668 
00669         virtual const TiXmlDocument*    ToDocument()    const { return 0; } 
00670         virtual const TiXmlElement*     ToElement()     const { return 0; } 
00671         virtual const TiXmlComment*     ToComment()     const { return 0; } 
00672         virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } 
00673         virtual const TiXmlText*        ToText()        const { return 0; } 
00674         virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } 
00675 
00676         virtual TiXmlDocument*          ToDocument()    { return 0; } 
00677         virtual TiXmlElement*           ToElement()         { return 0; } 
00678         virtual TiXmlComment*           ToComment()     { return 0; } 
00679         virtual TiXmlUnknown*           ToUnknown()         { return 0; } 
00680         virtual TiXmlText*                  ToText()        { return 0; } 
00681         virtual TiXmlDeclaration*       ToDeclaration() { return 0; } 
00682 
00686         virtual TiXmlNode* Clone() const = 0;
00687 
00688 protected:
00689         TiXmlNode( NodeType _type );
00690 
00691         // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
00692         // and the assignment operator.
00693         void CopyTo( TiXmlNode* target ) const;
00694 
00695         #ifdef TIXML_USE_STL
00696             // The real work of the input operator.
00697             virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
00698         #endif
00699 
00700         // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
00701         TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
00702 
00703         TiXmlNode*              parent;
00704         NodeType                type;
00705 
00706         TiXmlNode*              firstChild;
00707         TiXmlNode*              lastChild;
00708 
00709         TIXML_STRING    value;
00710 
00711         TiXmlNode*              prev;
00712         TiXmlNode*              next;
00713 
00714 private:
00715         TiXmlNode( const TiXmlNode& );                          // not implemented.
00716         void operator=( const TiXmlNode& base );        // not allowed.
00717 };
00718 
00719 
00727 class YXML_API TiXmlAttribute : public TiXmlBase
00728 {
00729         friend class TiXmlAttributeSet;
00730         friend class TiXmlElement;
00731 
00732 public:
00734         TiXmlAttribute() : TiXmlBase()
00735         {
00736                 document = 0;
00737                 prev = next = 0;
00738         }
00739 
00740         #ifdef TIXML_USE_STL
00741 
00742         TiXmlAttribute( const std::string& _name, const std::string& _value )
00743         {
00744                 name = _name;
00745                 value = _value;
00746                 document = 0;
00747                 prev = next = 0;
00748         }
00749         #endif
00750 
00752         TiXmlAttribute( const char * _name, const char * _value )
00753         {
00754                 name = _name;
00755                 value = _value;
00756                 document = 0;
00757                 prev = next = 0;
00758         }
00759 
00760         const char*             Name()  const           { return name.c_str (); }               
00761         const char*             Value() const           { return value.c_str (); }              
00762         int                             IntValue() const;                                                                       
00763         double                  DoubleValue() const;                                                            
00764 
00765         // Get the tinyxml string representation
00766         const TIXML_STRING& NameTStr() const { return name; }
00767 
00777         int QueryIntValue( int* _value ) const;
00779         int QueryDoubleValue( double* _value ) const;
00780 
00781         void SetName( const char* _name )       { name = _name; }                               
00782         void SetValue( const char* _value )     { value = _value; }                             
00783 
00784         void SetIntValue( int _value );                                                                         
00785         void SetDoubleValue( double _value );                                                           
00786 
00787     #ifdef TIXML_USE_STL
00788 
00789         void SetName( const std::string& _name )        { name = _name; }       
00791         void SetValue( const std::string& _value )      { value = _value; }
00792         #endif
00793 
00795         const TiXmlAttribute* Next() const;
00796         TiXmlAttribute* Next();
00798         const TiXmlAttribute* Previous() const;
00799         TiXmlAttribute* Previous();
00800 
00801         bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
00802         bool operator<( const TiXmlAttribute& rhs )      const { return name < rhs.name; }
00803         bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
00804 
00805         /*      Attribute parsing starts: first letter of the name
00806                                                  returns: the next char after the value end quote
00807         */
00808         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
00809 
00810         // Prints this Attribute to a FILE stream.
00811         virtual void Print( FILE* cfile, int depth ) const;
00812 
00813 public:
00814         virtual void StreamOut( TIXML_OSTREAM * out, bool unclosed = false ) const;
00815 
00816 protected:
00817         // [internal use]
00818         // Set the document pointer so the attribute can report errors.
00819         void SetDocument( TiXmlDocument* doc )  { document = doc; }
00820 
00821 private:
00822         TiXmlAttribute( const TiXmlAttribute& );                                // not implemented.
00823         void operator=( const TiXmlAttribute& base );   // not allowed.
00824 
00825         TiXmlDocument*  document;       // A pointer back to a document, for error reporting.
00826         TIXML_STRING name;
00827         TIXML_STRING value;
00828         TiXmlAttribute* prev;
00829         TiXmlAttribute* next;
00830 };
00831 
00832 
00833 /*      A class used to manage a group of attributes.
00834         It is only used internally, both by the ELEMENT and the DECLARATION.
00835         
00836         The set can be changed transparent to the Element and Declaration
00837         classes that use it, but NOT transparent to the Attribute
00838         which has to implement a next() and previous() method. Which makes
00839         it a bit problematic and prevents the use of STL.
00840 
00841         This version is implemented with circular lists because:
00842                 - I like circular lists
00843                 - it demonstrates some independence from the (typical) doubly linked list.
00844 */
00845 class YXML_API TiXmlAttributeSet
00846 {
00847 public:
00848         TiXmlAttributeSet();
00849         ~TiXmlAttributeSet();
00850 
00851         void Add( TiXmlAttribute* attribute );
00852         void Remove( TiXmlAttribute* attribute );
00853 
00854         const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00855         TiXmlAttribute* First()                                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
00856         const TiXmlAttribute* Last() const              { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00857         TiXmlAttribute* Last()                                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
00858 
00859         const TiXmlAttribute*   Find( const TIXML_STRING& name ) const;
00860         TiXmlAttribute* Find( const TIXML_STRING& name );
00861 
00862 private:
00863         //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
00864         //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
00865         TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
00866         void operator=( const TiXmlAttributeSet& );     // not allowed (as TiXmlAttribute)
00867 
00868         TiXmlAttribute sentinel;
00869 };
00870 
00871 
00876 class YXML_API TiXmlElement : public TiXmlNode
00877 {
00878 public:
00880         TiXmlElement (const char * in_value);
00881 
00882         #ifdef TIXML_USE_STL
00883 
00884         TiXmlElement( const std::string& _value );
00885         #endif
00886 
00887         TiXmlElement( const TiXmlElement& );
00888 
00889         void operator=( const TiXmlElement& base );
00890 
00891         virtual ~TiXmlElement();
00892 
00896         const char* Attribute( const char* name ) const;
00897 
00904         const char* Attribute( const char* name, int* i ) const;
00905 
00912         const char* Attribute( const char* name, double* d ) const;
00913 
00921         int QueryIntAttribute( const char* name, int* _value ) const;
00923         int QueryDoubleAttribute( const char* name, double* _value ) const;
00925         int QueryFloatAttribute( const char* name, float* _value ) const {
00926                 double d;
00927                 int result = QueryDoubleAttribute( name, &d );
00928                 if ( result == TIXML_SUCCESS ) {
00929                         *_value = (float)d;
00930                 }
00931                 return result;
00932         }
00933 
00937         void SetAttribute( const char* name, const char * _value );
00938 
00939     #ifdef TIXML_USE_STL
00940         const char* Attribute( const std::string& name ) const                          { return Attribute( name.c_str() ); }
00941         const char* Attribute( const std::string& name, int* i ) const          { return Attribute( name.c_str(), i ); }
00942         const char* Attribute( const std::string& name, double* d ) const       { return Attribute( name.c_str(), d ); }
00943         int QueryIntAttribute( const std::string& name, int* _value ) const     { return QueryIntAttribute( name.c_str(), _value ); }
00944         int QueryDoubleAttribute( const std::string& name, double* _value ) const { return QueryDoubleAttribute( name.c_str(), _value ); }
00945 
00947         void SetAttribute( const std::string& name, const std::string& _value );
00949         void SetAttribute( const std::string& name, int _value );
00950         #endif
00951 
00955         void SetAttribute( const char * name, int value );
00956 
00960         void SetDoubleAttribute( const char * name, double value );
00961 
00964         void RemoveAttribute( const char * name );
00965     #ifdef TIXML_USE_STL
00966         void RemoveAttribute( const std::string& name ) {       RemoveAttribute (name.c_str ());        }       
00967         #endif
00968 
00969         const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }                
00970         TiXmlAttribute* FirstAttribute()                                { return attributeSet.First(); }
00971         const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }         
00972         TiXmlAttribute* LastAttribute()                                 { return attributeSet.Last(); }
00973 
01006         const char* GetText() const;
01007 
01009         virtual TiXmlNode* Clone() const;
01010         // Print the Element to a FILE stream.
01011         virtual void Print( FILE* cfile, int depth ) const;
01012 
01013         /*      Attribtue parsing starts: next char past '<'
01014                                                  returns: next char past '>'
01015         */
01016         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01017 
01018         virtual const TiXmlElement*     ToElement()     const { return this; } 
01019         virtual TiXmlElement*           ToElement()               { return this; } 
01020         
01021 protected:
01022 
01023         void CopyTo( TiXmlElement* target ) const;
01024         void ClearThis();       // like clear, but initializes 'this' object as well
01025 
01026         // Used to be public [internal use]
01027         #ifdef TIXML_USE_STL
01028             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01029         #endif
01030 public:
01031         virtual void StreamOut( TIXML_OSTREAM * out, bool unclosed = false ) const;
01032 
01033 protected:
01034         /*      [internal use]
01035                 Reads the "value" of the element -- another element, or text.
01036                 This should terminate with the current end tag.
01037         */
01038         const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01039 
01040 private:
01041 
01042         TiXmlAttributeSet attributeSet;
01043 };
01044 
01045 
01048 class YXML_API TiXmlComment : public TiXmlNode
01049 {
01050 public:
01052         TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
01053         TiXmlComment( const TiXmlComment& );
01054         void operator=( const TiXmlComment& base );
01055 
01056         virtual ~TiXmlComment() {}
01057 
01059         virtual TiXmlNode* Clone() const;
01061         virtual void Print( FILE* cfile, int depth ) const;
01062 
01063         /*      Attribtue parsing starts: at the ! of the !--
01064                                                  returns: next char past '>'
01065         */
01066         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01067 
01068         virtual const TiXmlComment*  ToComment() const { return this; } 
01069         virtual TiXmlComment*  ToComment() { return this; } 
01070 
01071 protected:
01072         void CopyTo( TiXmlComment* target ) const;
01073 
01074         // used to be public
01075         #ifdef TIXML_USE_STL
01076             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01077         #endif
01078 
01079 public:
01080         virtual void StreamOut( TIXML_OSTREAM * out, bool unclosed = false ) const;
01081 
01082 private:
01083 
01084 };
01085 
01086 
01092 class YXML_API TiXmlText : public TiXmlNode
01093 {
01094         friend class TiXmlElement;
01095 public:
01100         TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
01101         {
01102                 SetValue( initValue );
01103                 cdata = false;
01104         }
01105         virtual ~TiXmlText() {}
01106 
01107         #ifdef TIXML_USE_STL
01108 
01109         TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
01110         {
01111                 SetValue( initValue );
01112                 cdata = false;
01113         }
01114         #endif
01115 
01116         TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )       { copy.CopyTo( this ); }
01117         void operator=( const TiXmlText& base )                                                         { base.CopyTo( this ); }
01118 
01120         virtual void Print( FILE* cfile, int depth ) const;
01121 
01123         bool CDATA()                                    { return cdata; }
01125         void SetCDATA( bool _cdata )    { cdata = _cdata; }
01126 
01127         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01128 
01129         virtual const TiXmlText* ToText() const { return this; } 
01130         virtual TiXmlText*       ToText()       { return this; } 
01131 
01132 protected :
01134         virtual TiXmlNode* Clone() const;
01135         void CopyTo( TiXmlText* target ) const;
01136 
01137 public:
01138         virtual void StreamOut ( TIXML_OSTREAM * out, bool unclosed = false ) const;
01139 
01140 protected:
01141         bool Blank() const;     // returns true if all white space and new lines
01142         // [internal use]
01143         #ifdef TIXML_USE_STL
01144             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01145         #endif
01146 
01147 private:
01148         bool cdata;                     // true if this should be input and output as a CDATA style text element
01149 };
01150 
01151 
01165 class YXML_API TiXmlDeclaration : public TiXmlNode
01166 {
01167 public:
01169         TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
01170 
01171 #ifdef TIXML_USE_STL
01172 
01173         TiXmlDeclaration(       const std::string& _version,
01174                                                 const std::string& _encoding,
01175                                                 const std::string& _standalone );
01176 #endif
01177 
01179         TiXmlDeclaration(       const char* _version,
01180                                                 const char* _encoding,
01181                                                 const char* _standalone );
01182 
01183         TiXmlDeclaration( const TiXmlDeclaration& copy );
01184         void operator=( const TiXmlDeclaration& copy );
01185 
01186         virtual ~TiXmlDeclaration()     {}
01187 
01189         const char *Version() const                     { return version.c_str (); }
01191         const char *Encoding() const            { return encoding.c_str (); }
01193         const char *Standalone() const          { return standalone.c_str (); }
01194 
01196         virtual TiXmlNode* Clone() const;
01198         virtual void Print( FILE* cfile, int depth ) const;
01199 
01200         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01201 
01202         virtual const TiXmlDeclaration* ToDeclaration() const { return this; } 
01203         virtual TiXmlDeclaration*       ToDeclaration()       { return this; } 
01204 
01205 protected:
01206         void CopyTo( TiXmlDeclaration* target ) const;
01207         // used to be public
01208         #ifdef TIXML_USE_STL
01209             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01210         #endif
01211 
01212 public:
01213         virtual void StreamOut ( TIXML_OSTREAM * out, bool unclosed = false ) const;
01214 
01215 private:
01216 
01217         TIXML_STRING version;
01218         TIXML_STRING encoding;
01219         TIXML_STRING standalone;
01220 };
01221 
01222 
01230 class YXML_API TiXmlUnknown : public TiXmlNode
01231 {
01232 public:
01233         TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )        {}
01234         virtual ~TiXmlUnknown() {}
01235 
01236         TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )              { copy.CopyTo( this ); }
01237         void operator=( const TiXmlUnknown& copy )                                                                              { copy.CopyTo( this ); }
01238 
01240         virtual TiXmlNode* Clone() const;
01242         virtual void Print( FILE* cfile, int depth ) const;
01243 
01244         virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
01245 
01246         virtual const TiXmlUnknown*     ToUnknown()     const { return this; } 
01247         virtual TiXmlUnknown*           ToUnknown()         { return this; } 
01248 
01249 protected:
01250         void CopyTo( TiXmlUnknown* target ) const;
01251 
01252         #ifdef TIXML_USE_STL
01253             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01254         #endif
01255 
01256 public:
01257         virtual void StreamOut ( TIXML_OSTREAM * out, bool unclosed = false ) const;
01258 
01259 private:
01260 
01261 };
01262 
01263 
01268 class YXML_API TiXmlDocument : public TiXmlNode
01269 {
01270 public:
01272         TiXmlDocument();
01274         TiXmlDocument( const char * documentName );
01275 
01276         #ifdef TIXML_USE_STL
01277 
01278         TiXmlDocument( const std::string& documentName );
01279         #endif
01280 
01281         TiXmlDocument( const TiXmlDocument& copy );
01282         void operator=( const TiXmlDocument& copy );
01283 
01284         virtual ~TiXmlDocument() {}
01285 
01290         bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01292         bool SaveFile() const;
01294         bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01296         bool SaveFile( const char * filename ) const;
01302         bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01304         bool SaveFile( FILE* ) const;
01305 
01306         #ifdef TIXML_USE_STL
01307         bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )                   
01308         {
01309                 StringToBuffer f( filename );
01310                 return ( f.buffer && LoadFile( f.buffer, encoding ));
01311         }
01312         bool SaveFile( const std::string& filename ) const              
01313         {
01314                 StringToBuffer f( filename );
01315                 return ( f.buffer && SaveFile( f.buffer ));
01316         }
01317         #endif
01318 
01324         virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
01325 
01330         const TiXmlElement* RootElement() const         { return FirstChildElement(); }
01331         TiXmlElement* RootElement()                                     { return FirstChildElement(); }
01332 
01338         bool Error() const                                              { return error; }
01339 
01341         const char * ErrorDesc() const  { return errorDesc.c_str (); }
01342 
01346         int ErrorId()   const                           { return errorId; }
01347 
01355         int ErrorRow()  { return errorLocation.row+1; }
01356         int ErrorCol()  { return errorLocation.col+1; } 
01357 
01382         void SetTabSize( int _tabsize )         { tabsize = _tabsize; }
01383 
01384         int TabSize() const     { return tabsize; }
01385 
01389         void ClearError()                                               {       error = false; 
01390                                                                                                 errorId = 0; 
01391                                                                                                 errorDesc = ""; 
01392                                                                                                 errorLocation.row = errorLocation.col = 0; 
01393                                                                                                 //errorLocation.last = 0; 
01394                                                                                         }
01395 
01397         void Print() const                                              { Print( stdout, 0 ); }
01398 
01400         virtual void Print( FILE* cfile, int depth = 0 ) const;
01401         // [internal use]
01402         void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
01403         virtual const TiXmlDocument*    ToDocument()    const { return this; } 
01404         virtual TiXmlDocument*          ToDocument()          { return this; } 
01405 
01406         virtual void StreamOut ( TIXML_OSTREAM * out, bool unclosed = false ) const;
01407 
01408 protected :
01409         // [internal use]
01410         virtual TiXmlNode* Clone() const;
01411         #ifdef TIXML_USE_STL
01412             virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
01413         #endif
01414 
01415 private:
01416         void CopyTo( TiXmlDocument* target ) const;
01417 
01418         bool error;
01419         int  errorId;
01420         TIXML_STRING errorDesc;
01421         int tabsize;
01422         TiXmlCursor errorLocation;
01423         bool useMicrosoftBOM;           // the UTF-8 BOM were found when read. Note this, and try to write.
01424 };
01425 
01426 
01507 class YXML_API TiXmlHandle
01508 {
01509 public:
01511         TiXmlHandle( TiXmlNode* _node )                                 { this->node = _node; }
01513         TiXmlHandle( const TiXmlHandle& ref )                   { this->node = ref.node; }
01514         TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
01515 
01517         TiXmlHandle FirstChild() const;
01519         TiXmlHandle FirstChild( const char * value ) const;
01521         TiXmlHandle FirstChildElement() const;
01523         TiXmlHandle FirstChildElement( const char * value ) const;
01524 
01528         TiXmlHandle Child( const char* value, int index ) const;
01532         TiXmlHandle Child( int index ) const;
01537         TiXmlHandle ChildElement( const char* value, int index ) const;
01542         TiXmlHandle ChildElement( int index ) const;
01543 
01544         #ifdef TIXML_USE_STL
01545         TiXmlHandle FirstChild( const std::string& _value ) const                               { return FirstChild( _value.c_str() ); }
01546         TiXmlHandle FirstChildElement( const std::string& _value ) const                { return FirstChildElement( _value.c_str() ); }
01547 
01548         TiXmlHandle Child( const std::string& _value, int index ) const                 { return Child( _value.c_str(), index ); }
01549         TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
01550         #endif
01551 
01553         TiXmlNode* Node() const                 { return node; } 
01555         TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
01557         TiXmlText* Text() const                 { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
01559         TiXmlUnknown* Unknown() const                   { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
01560 
01561 private:
01562         TiXmlNode* node;
01563 };
01564 
01565 class YXML_API TiXmlParsingData
01566 {
01567         friend class TiXmlDocument;
01568   public:
01569         void Stamp( const char* now, TiXmlEncoding encoding );
01570 
01571         const TiXmlCursor& Cursor()     { return cursor; }
01572 
01573   private:
01574         // Only used by the document!
01575         TiXmlParsingData( const char* start, int _tabsize, int row, int col )
01576         {
01577                 assert( start );
01578                 stamp = start;
01579                 tabsize = _tabsize;
01580                 cursor.row = row;
01581                 cursor.col = col;
01582         }
01583 
01584         TiXmlCursor             cursor;
01585         const char*             stamp;
01586         int                             tabsize;
01587 };
01588 
01589 }; // TelEngine namespace
01590 
01591 #ifdef _MSC_VER
01592 #pragma warning( pop )
01593 #endif
01594 
01595 #endif
01596