source: NonGTP/OSceneLoader/TinyXML/tinyxml.h @ 965

Revision 965, 30.6 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*
2www.sourceforge.net/projects/tinyxml
3Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any
7damages arising from the use of this software.
8
9Permission is granted to anyone to use this software for any
10purpose, including commercial applications, and to alter it and
11redistribute it freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must
14not claim that you wrote the original software. If you use this
15software in a product, an acknowledgment in the product documentation
16would be appreciated but is not required.
17
182. Altered source versions must be plainly marked as such, and
19must not be misrepresented as being the original software.
20
213. This notice may not be removed or altered from any source
22distribution.
23*/
24
25
26#ifndef TINYXML_INCLUDED
27#define TINYXML_INCLUDED
28
29#define TIXML_USE_STL
30
31#ifdef _MSC_VER
32#pragma warning( disable : 4530 )
33#pragma warning( disable : 4786 )
34#endif
35
36#include <ctype.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <assert.h>
41
42// Help out windows:
43#if defined( _DEBUG ) && !defined( DEBUG )
44#define DEBUG
45#endif
46
47#if defined( DEBUG ) && defined( _MSC_VER )
48#define NOMINMAX
49#include <windows.h>
50#define TIXML_LOG OutputDebugString
51#else
52#define TIXML_LOG printf
53#endif
54
55
56#ifdef TIXML_USE_STL
57        #include <string>
58    #include <iostream>
59        #define TIXML_STRING    std::string
60        #define TIXML_ISTREAM   std::istream
61        #define TIXML_OSTREAM   std::ostream
62#else
63        #include "tinystr.h"
64        #define TIXML_STRING    TiXmlString
65        #define TIXML_OSTREAM   TiXmlOutStream
66#endif
67
68class TiXmlDocument;
69class TiXmlElement;
70class TiXmlComment;
71class TiXmlUnknown;
72class TiXmlAttribute;
73class TiXmlText;
74class TiXmlDeclaration;
75
76
77/** TiXmlBase is a base class for every class in TinyXml.
78        It does little except to establish that TinyXml classes
79        can be printed and provide some utility functions.
80
81        In XML, the document and elements can contain
82        other elements and other types of nodes.
83
84        @verbatim
85        A Document can contain: Element (container or leaf)
86                                                        Comment (leaf)
87                                                        Unknown (leaf)
88                                                        Declaration( leaf )
89
90        An Element can contain: Element (container or leaf)
91                                                        Text    (leaf)
92                                                        Attributes (not on tree)
93                                                        Comment (leaf)
94                                                        Unknown (leaf)
95
96        A Decleration contains: Attributes (not on tree)
97        @endverbatim
98*/
99class TiXmlBase
100{
101        friend class TiXmlNode;
102        friend class TiXmlElement;
103        friend class TiXmlDocument;
104
105public:
106        TiXmlBase()                                                             {}
107        virtual ~TiXmlBase()                                    {}
108
109        /**     All TinyXml classes can print themselves to a filestream.
110                This is a formatted print, and will insert tabs and newlines.
111               
112                (For an unformatted stream, use the << operator.)
113        */
114        virtual void Print( FILE* cfile, int depth ) const = 0;
115
116        /**     The world does not agree on whether white space should be kept or
117                not. In order to make everyone happy, these global, static functions
118                are provided to set whether or not TinyXml will condense all white space
119                into a single space or not. The default is to condense. Note changing these
120                values is not thread safe.
121        */
122        static void SetCondenseWhiteSpace( bool condense )              { condenseWhiteSpace = condense; }
123
124        /// Return the current white space setting.
125        static bool IsWhiteSpaceCondensed()                                             { return condenseWhiteSpace; }
126
127protected:
128
129        static const char*      SkipWhiteSpace( const char* );
130        inline static bool      IsWhiteSpace( int c )           { return ( isspace( c ) || c == '\n' || c == '\r' ); }
131
132        virtual void StreamOut (TIXML_OSTREAM *) const = 0;
133
134        #ifdef TIXML_USE_STL
135            static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
136            static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
137        #endif
138
139        /*      Reads an XML name into the string provided. Returns
140                a pointer just past the last character of the name,
141                or 0 if the function has an error.
142        */
143        static const char* ReadName( const char* p, TIXML_STRING* name );
144
145        /*      Reads text. Returns a pointer past the given end tag.
146                Wickedly complex options, but it keeps the (sensitive) code in one place.
147        */
148        static const char* ReadText(    const char* in,                         // where to start
149                                                                        TIXML_STRING* text,                     // the string read
150                                                                        bool ignoreWhiteSpace,          // whether to keep the white space
151                                                                        const char* endTag,                     // what ends this text
152                                                                        bool ignoreCase );                      // whether to ignore case in the end tag
153        virtual const char* Parse( const char* p ) = 0;
154
155        // If an entity has been found, transform it into a character.
156        static const char* GetEntity( const char* in, char* value );
157
158        // Get a character, while interpreting entities.
159        inline static const char* GetChar( const char* p, char* value )
160        {
161                assert( p );
162                if ( *p == '&' )
163                {
164                        return GetEntity( p, value );
165                }
166                else
167                {
168                        *value = *p;
169                        return p+1;
170                }
171        }
172
173        // Puts a string to a stream, expanding entities as it goes.
174        // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
175        static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
176
177        static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
178
179        // Return true if the next characters in the stream are any of the endTag sequences.
180        bool static StringEqual(        const char* p,
181                                                                const char* endTag,
182                                                                bool ignoreCase );
183
184
185        enum
186        {
187                TIXML_NO_ERROR = 0,
188                TIXML_ERROR,
189                TIXML_ERROR_OPENING_FILE,
190                TIXML_ERROR_OUT_OF_MEMORY,
191                TIXML_ERROR_PARSING_ELEMENT,
192                TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
193                TIXML_ERROR_READING_ELEMENT_VALUE,
194                TIXML_ERROR_READING_ATTRIBUTES,
195                TIXML_ERROR_PARSING_EMPTY,
196                TIXML_ERROR_READING_END_TAG,
197                TIXML_ERROR_PARSING_UNKNOWN,
198                TIXML_ERROR_PARSING_COMMENT,
199                TIXML_ERROR_PARSING_DECLARATION,
200                TIXML_ERROR_DOCUMENT_EMPTY,
201
202                TIXML_ERROR_STRING_COUNT
203        };
204        static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
205
206private:
207        struct Entity
208        {
209                const char*     str;
210                unsigned int    strLength;
211                char                chr;
212        };
213        enum
214        {
215                NUM_ENTITY = 5,
216                MAX_ENTITY_LENGTH = 6
217
218        };
219        static Entity entity[ NUM_ENTITY ];
220        static bool condenseWhiteSpace;
221};
222
223
224/** The parent class for everything in the Document Object Model.
225        (Except for attributes, which are contained in elements.)
226        Nodes have siblings, a parent, and children. A node can be
227        in a document, or stand on its own. The type of a TiXmlNode
228        can be queried, and it can be cast to its more defined type.
229*/
230class TiXmlNode : public TiXmlBase
231{
232        friend class TiXmlDocument;
233        friend class TiXmlElement;
234
235public:
236        #ifdef TIXML_USE_STL   
237
238            /** An input stream operator, for every class. Tolerant of newlines and
239                    formatting, but doesn't expect them.
240            */
241            friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
242
243            /** An output stream operator, for every class. Note that this outputs
244                    without any newlines or formatting, as opposed to Print(), which
245                    includes tabs and new lines.
246
247                    The operator<< and operator>> are not completely symmetric. Writing
248                    a node to a stream is very well defined. You'll get a nice stream
249                    of output, without any extra whitespace or newlines.
250                   
251                    But reading is not as well defined. (As it always is.) If you create
252                    a TiXmlElement (for example) and read that from an input stream,
253                    the text needs to define an element or junk will result. This is
254                    true of all input streams, but it's worth keeping in mind.
255
256                    A TiXmlDocument will read nodes until it reads a root element.
257            */ 
258            friend std::ostream & operator<< (std::ostream& out, const TiXmlNode& base);
259
260        #else
261            // Used internally, not part of the public API.
262            friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
263        #endif
264
265        /** The types of XML nodes supported by TinyXml. (All the
266                        unsupported types are picked up by UNKNOWN.)
267        */
268        enum NodeType
269        {
270                DOCUMENT,
271                ELEMENT,
272                COMMENT,
273                UNKNOWN,
274                TEXT,
275                DECLARATION,
276                TYPECOUNT
277        };
278
279        virtual ~TiXmlNode();
280
281        /** The meaning of 'value' changes for the specific type of
282                TiXmlNode.
283                @verbatim
284                Document:       filename of the xml file
285                Element:        name of the element
286                Comment:        the comment text
287                Unknown:        the tag contents
288                Text:           the text string
289                @endverbatim
290
291                The subclasses will wrap this function.
292        */
293        const char * Value () const { return value.c_str (); }
294
295        /** Changes the value of the node. Defined as:
296                @verbatim
297                Document:       filename of the xml file
298                Element:        name of the element
299                Comment:        the comment text
300                Unknown:        the tag contents
301                Text:           the text string
302                @endverbatim
303        */
304        void SetValue (const char * _value) { value = _value;}
305
306    #ifdef TIXML_USE_STL
307        /// STL std::string form.
308        void SetValue( const std::string& value )   
309        {         
310                SetValue( value.c_str() );     
311        }       
312        #endif
313
314        /// Delete all the children of this node. Does not affect 'this'.
315        void Clear();
316
317        /// One step up the DOM.
318        TiXmlNode* Parent() const                                       { return parent; }
319
320        TiXmlNode* FirstChild() const   { return firstChild; }          ///< The first child of this node. Will be null if there are no children.
321        TiXmlNode* FirstChild( const char * value ) const;                      ///< The first child of this node with the matching 'value'. Will be null if none found.
322
323        TiXmlNode* LastChild() const    { return lastChild; }           /// The last child of this node. Will be null if there are no children.
324        TiXmlNode* LastChild( const char * value ) const;                       /// The last child of this node matching 'value'. Will be null if there are no children.
325
326    #ifdef TIXML_USE_STL
327        TiXmlNode* FirstChild( const std::string& value ) const {       return FirstChild (value.c_str ());     }       ///< STL std::string form.
328        TiXmlNode* LastChild( const std::string& value ) const  {       return LastChild (value.c_str ());      }       ///< STL std::string form.
329        #endif
330
331        /** An alternate way to walk the children of a node.
332                One way to iterate over nodes is:
333                @verbatim
334                        for( child = parent->FirstChild(); child; child = child->NextSibling() )
335                @endverbatim
336
337                IterateChildren does the same thing with the syntax:
338                @verbatim
339                        child = 0;
340                        while( child = parent->IterateChildren( child ) )
341                @endverbatim
342
343                IterateChildren takes the previous child as input and finds
344                the next one. If the previous child is null, it returns the
345                first. IterateChildren will return null when done.
346        */
347        TiXmlNode* IterateChildren( TiXmlNode* previous ) const;
348
349        /// This flavor of IterateChildren searches for children with a particular 'value'
350        TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous ) const;
351
352    #ifdef TIXML_USE_STL
353        TiXmlNode* IterateChildren( const std::string& value, TiXmlNode* previous ) const       {       return IterateChildren (value.c_str (), previous);      }       ///< STL std::string form.
354        #endif
355
356        /** Add a new node related to this. Adds a child past the LastChild.
357                Returns a pointer to the new object or NULL if an error occured.
358        */
359        TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
360
361        /** Add a new node related to this. Adds a child before the specified child.
362                Returns a pointer to the new object or NULL if an error occured.
363        */
364        TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
365
366        /** Add a new node related to this. Adds a child after the specified child.
367                Returns a pointer to the new object or NULL if an error occured.
368        */
369        TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
370
371        /** Replace a child of this node.
372                Returns a pointer to the new object or NULL if an error occured.
373        */
374        TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
375
376        /// Delete a child of this node.
377        bool RemoveChild( TiXmlNode* removeThis );
378
379        /// Navigate to a sibling node.
380        TiXmlNode* PreviousSibling() const                      { return prev; }
381
382        /// Navigate to a sibling node.
383        TiXmlNode* PreviousSibling( const char * ) const;
384
385    #ifdef TIXML_USE_STL
386        TiXmlNode* PreviousSibling( const std::string& value ) const    {       return PreviousSibling (value.c_str ());        }       ///< STL std::string form.
387        TiXmlNode* NextSibling( const std::string& value) const {       return NextSibling (value.c_str ());    }       ///< STL std::string form.
388        #endif
389
390        /// Navigate to a sibling node.
391        TiXmlNode* NextSibling() const                          { return next; }
392
393        /// Navigate to a sibling node with the given 'value'.
394        TiXmlNode* NextSibling( const char * ) const;
395
396        /** Convenience function to get through elements.
397                Calls NextSibling and ToElement. Will skip all non-Element
398                nodes. Returns 0 if there is not another element.
399        */
400        TiXmlElement* NextSiblingElement() const;
401
402        /** Convenience function to get through elements.
403                Calls NextSibling and ToElement. Will skip all non-Element
404                nodes. Returns 0 if there is not another element.
405        */
406        TiXmlElement* NextSiblingElement( const char * ) const;
407
408    #ifdef TIXML_USE_STL
409        TiXmlElement* NextSiblingElement( const std::string& value) const       {       return NextSiblingElement (value.c_str ());     }       ///< STL std::string form.
410        #endif
411
412        /// Convenience function to get through elements.
413        TiXmlElement* FirstChildElement()       const;
414
415        /// Convenience function to get through elements.
416        TiXmlElement* FirstChildElement( const char * value ) const;
417
418    #ifdef TIXML_USE_STL
419        TiXmlElement* FirstChildElement( const std::string& value ) const       {       return FirstChildElement (value.c_str ());      }       ///< STL std::string form.
420        #endif
421
422        /// Query the type (as an enumerated value, above) of this node.
423        virtual int Type() const        { return type; }
424
425        /** Return a pointer to the Document this node lives in.
426                Returns null if not in a document.
427        */
428        TiXmlDocument* GetDocument() const;
429
430        /// Returns true if this node has no children.
431        bool NoChildren() const                                         { return !firstChild; }
432
433        TiXmlDocument* ToDocument()     const           { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
434        TiXmlElement*  ToElement() const                { return ( this && type == ELEMENT  ) ? (TiXmlElement*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
435        TiXmlComment*  ToComment() const                { return ( this && type == COMMENT  ) ? (TiXmlComment*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
436        TiXmlUnknown*  ToUnknown() const                { return ( this && type == UNKNOWN  ) ? (TiXmlUnknown*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
437        TiXmlText*         ToText()    const            { return ( this && type == TEXT     ) ? (TiXmlText*)     this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
438        TiXmlDeclaration* ToDeclaration() const { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
439
440        virtual TiXmlNode* Clone() const = 0;
441
442        void  SetUserData( void* user )                 { userData = user; }
443        void* GetUserData()                                             { return userData; }
444
445protected:
446        TiXmlNode( NodeType type );
447
448        #ifdef TIXML_USE_STL
449            // The real work of the input operator.
450            virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
451        #endif
452
453        // The node is passed in by ownership. This object will delete it.
454        TiXmlNode* LinkEndChild( TiXmlNode* addThis );
455
456        // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
457        TiXmlNode* Identify( const char* start );
458        void CopyToClone( TiXmlNode* target ) const     { target->SetValue (value.c_str() );
459                                                                                                  target->userData = userData; }
460
461        // Internal Value function returning a TIXML_STRING
462        TIXML_STRING SValue() const     { return value ; }
463
464        TiXmlNode*              parent;
465        NodeType                type;
466
467        TiXmlNode*              firstChild;
468        TiXmlNode*              lastChild;
469
470        TIXML_STRING    value;
471
472        TiXmlNode*              prev;
473        TiXmlNode*              next;
474        void*                   userData;
475};
476
477
478/** An attribute is a name-value pair. Elements have an arbitrary
479        number of attributes, each with a unique name.
480
481        @note The attributes are not TiXmlNodes, since they are not
482                  part of the tinyXML document object model. There are other
483                  suggested ways to look at this problem.
484
485        @note Attributes have a parent
486*/
487class TiXmlAttribute : public TiXmlBase
488{
489        friend class TiXmlAttributeSet;
490
491public:
492        /// Construct an empty attribute.
493        TiXmlAttribute() : prev( 0 ), next( 0 ) {}
494
495        #ifdef TIXML_USE_STL
496        /// std::string constructor.
497        TiXmlAttribute( const std::string& _name, const std::string& _value )
498        {
499                name = _name;
500                value = _value;
501        }
502        #endif
503
504        /// Construct an attribute with a name and value.
505        TiXmlAttribute( const char * _name, const char * _value ): name( _name ), value( _value ), prev( 0 ), next( 0 ) {}
506        const char*             Name()  const           { return name.c_str (); }               ///< Return the name of this attribute.
507        const char*             Value() const           { return value.c_str (); }              ///< Return the value of this attribute.
508        const int       IntValue() const;                                                                       ///< Return the value of this attribute, converted to an integer.
509        const double    DoubleValue() const;                                                            ///< Return the value of this attribute, converted to a double.
510
511        void SetName( const char* _name )       { name = _name; }                               ///< Set the name of this attribute.
512        void SetValue( const char* _value )     { value = _value; }                             ///< Set the value.
513
514        void SetIntValue( int value );                                                                          ///< Set the value from an integer.
515        void SetDoubleValue( double value );                                                            ///< Set the value from a double.
516
517    #ifdef TIXML_USE_STL
518        /// STL std::string form.
519        void SetName( const std::string& _name )       
520        {       
521                SetName(_name.c_str() );       
522        }
523        /// STL std::string form.       
524        void SetValue( const std::string& _value )     
525        {       
526                SetValue( value.c_str() );     
527        }
528        #endif
529
530        /// Get the next sibling attribute in the DOM. Returns null at end.
531        TiXmlAttribute* Next() const;
532        /// Get the previous sibling attribute in the DOM. Returns null at beginning.
533        TiXmlAttribute* Previous() const;
534
535        bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
536        bool operator<( const TiXmlAttribute& rhs )      const { return name < rhs.name; }
537        bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
538
539        /*      [internal use]
540                Attribtue parsing starts: first letter of the name
541                                                 returns: the next char after the value end quote
542        */
543        virtual const char* Parse( const char* p );
544
545        // [internal use]
546        virtual void Print( FILE* cfile, int depth ) const;
547
548        virtual void StreamOut( TIXML_OSTREAM * out ) const;
549        // [internal use]
550        // Set the document pointer so the attribute can report errors.
551        void SetDocument( TiXmlDocument* doc )  { document = doc; }
552
553private:
554        TiXmlDocument*  document;       // A pointer back to a document, for error reporting.
555        TIXML_STRING name;
556        TIXML_STRING value;
557        TiXmlAttribute* prev;
558        TiXmlAttribute* next;
559};
560
561
562/*      A class used to manage a group of attributes.
563        It is only used internally, both by the ELEMENT and the DECLARATION.
564       
565        The set can be changed transparent to the Element and Declaration
566        classes that use it, but NOT transparent to the Attribute
567        which has to implement a next() and previous() method. Which makes
568        it a bit problematic and prevents the use of STL.
569
570        This version is implemented with circular lists because:
571                - I like circular lists
572                - it demonstrates some independence from the (typical) doubly linked list.
573*/
574class TiXmlAttributeSet
575{
576public:
577        TiXmlAttributeSet();
578        ~TiXmlAttributeSet();
579
580        void Add( TiXmlAttribute* attribute );
581        void Remove( TiXmlAttribute* attribute );
582
583        TiXmlAttribute* First() const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
584        TiXmlAttribute* Last()  const   { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
585        TiXmlAttribute* Find( const char * name ) const;
586
587private:
588        TiXmlAttribute sentinel;
589};
590
591
592/** The element is a container class. It has a value, the element name,
593        and can contain other elements, text, comments, and unknowns.
594        Elements also contain an arbitrary number of attributes.
595*/
596class TiXmlElement : public TiXmlNode
597{
598public:
599        /// Construct an element.
600        TiXmlElement (const char * in_value);
601
602        #ifdef TIXML_USE_STL
603        /// std::string constructor.
604        TiXmlElement( const std::string& _value ) :     TiXmlNode( TiXmlNode::ELEMENT )
605        {
606                firstChild = lastChild = 0;
607                value = _value;
608        }
609        #endif
610
611        virtual ~TiXmlElement();
612
613// OGRE CHANGE
614// Resolve ambiguity of std::string / const char* conversion of Ogre::String
615#ifndef TIXML_USE_STL
616// OGRE CHANGE
617        /** Given an attribute name, attribute returns the value
618                for the attribute of that name, or null if none exists.
619        */
620        const char* Attribute( const char* name ) const;
621
622        /** Given an attribute name, attribute returns the value
623                for the attribute of that name, or null if none exists.
624                If the attribute exists and can be converted to an integer,
625                the integer value will be put in the return 'i', if 'i'
626                is non-null.
627        */
628        const char* Attribute( const char* name, int* i ) const;
629
630        /** Sets an attribute of name to a given value. The attribute
631                will be created if it does not exist, or changed if it does.
632        */
633        void SetAttribute( const char* name, const char * value );
634// OGRE CHANGE
635#endif
636// OGRE CHANGE
637    #ifdef TIXML_USE_STL
638        const char* Attribute( const std::string& name ) const;
639        const char* Attribute( const std::string& name, int* i ) const;
640
641        /// STL std::string form.
642        void SetAttribute( const std::string& name, const std::string& value );
643        ///< STL std::string form.
644        void SetAttribute( const std::string& name, int value );
645        #endif
646
647        /** Sets an attribute of name to a given value. The attribute
648                will be created if it does not exist, or changed if it does.
649        */
650        void SetAttribute( const char * name, int value );
651
652        /** Deletes an attribute with the given name.
653        */
654        void RemoveAttribute( const char * name );
655    #ifdef TIXML_USE_STL
656        void RemoveAttribute( const std::string& name ) {       RemoveAttribute (name.c_str ());        }       ///< STL std::string form.
657        #endif
658
659        TiXmlAttribute* FirstAttribute() const  { return attributeSet.First(); }                ///< Access the first attribute in this element.
660        TiXmlAttribute* LastAttribute() const   { return attributeSet.Last(); }         ///< Access the last attribute in this element.
661
662        // [internal use] Creates a new Element and returs it.
663        virtual TiXmlNode* Clone() const;
664        // [internal use]
665
666        virtual void Print( FILE* cfile, int depth ) const;
667
668protected:
669
670        // Used to be public [internal use]
671        #ifdef TIXML_USE_STL
672            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
673        #endif
674        virtual void StreamOut( TIXML_OSTREAM * out ) const;
675
676        /*      [internal use]
677                Attribtue parsing starts: next char past '<'
678                                                 returns: next char past '>'
679        */
680        virtual const char* Parse( const char* p );
681
682        /*      [internal use]
683                Reads the "value" of the element -- another element, or text.
684                This should terminate with the current end tag.
685        */
686        const char* ReadValue( const char* in );
687
688private:
689        TiXmlAttributeSet attributeSet;
690};
691
692
693/**     An XML comment.
694*/
695class TiXmlComment : public TiXmlNode
696{
697public:
698        /// Constructs an empty comment.
699        TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
700        virtual ~TiXmlComment() {}
701
702        // [internal use] Creates a new Element and returs it.
703        virtual TiXmlNode* Clone() const;
704        // [internal use]
705        virtual void Print( FILE* cfile, int depth ) const;
706protected:
707        // used to be public
708        #ifdef TIXML_USE_STL
709            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
710        #endif
711        virtual void StreamOut( TIXML_OSTREAM * out ) const;
712        /*      [internal use]
713                Attribtue parsing starts: at the ! of the !--
714                                                 returns: next char past '>'
715        */
716        virtual const char* Parse( const char* p );
717};
718
719
720/** XML text. Contained in an element.
721*/
722class TiXmlText : public TiXmlNode
723{
724        friend class TiXmlElement;
725public:
726        /// Constructor.
727        TiXmlText (const char * initValue) : TiXmlNode (TiXmlNode::TEXT)
728        {
729                SetValue( initValue );
730        }
731        virtual ~TiXmlText() {}
732
733        #ifdef TIXML_USE_STL
734        /// Constructor.
735        TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
736        {
737                SetValue( initValue );
738        }
739        #endif
740
741protected :
742        // [internal use] Creates a new Element and returns it.
743        virtual TiXmlNode* Clone() const;
744        // [internal use]
745        virtual void Print( FILE* cfile, int depth ) const;
746        virtual void StreamOut ( TIXML_OSTREAM * out ) const;
747        // [internal use]
748        bool Blank() const;     // returns true if all white space and new lines
749        /*      [internal use]
750                        Attribtue parsing starts: First char of the text
751                                                         returns: next char past '>'
752                */
753        virtual const char* Parse( const char* p );
754        // [internal use]
755        #ifdef TIXML_USE_STL
756            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
757        #endif
758};
759
760
761/** In correct XML the declaration is the first entry in the file.
762        @verbatim
763                <?xml version="1.0" standalone="yes"?>
764        @endverbatim
765
766        TinyXml will happily read or write files without a declaration,
767        however. There are 3 possible attributes to the declaration:
768        version, encoding, and standalone.
769
770        Note: In this version of the code, the attributes are
771        handled as special cases, not generic attributes, simply
772        because there can only be at most 3 and they are always the same.
773*/
774class TiXmlDeclaration : public TiXmlNode
775{
776public:
777        /// Construct an empty declaration.
778        TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
779
780#ifdef TIXML_USE_STL
781        /// Constructor.
782        TiXmlDeclaration(
783                                                const std::string& _version,
784                                                const std::string& _encoding,
785                                                const std::string& _standalone )
786                                        : TiXmlNode( TiXmlNode::DECLARATION )
787        {
788                version = _version;
789                encoding = _encoding;
790                standalone = _standalone;
791        }
792#endif
793
794        /// Construct.
795        TiXmlDeclaration::TiXmlDeclaration( const char * _version,
796                                                                                const char * _encoding,
797                                                                                const char * _standalone );
798
799        virtual ~TiXmlDeclaration()     {}
800
801        /// Version. Will return empty if none was found.
802        const char * Version() const            { return version.c_str (); }
803        /// Encoding. Will return empty if none was found.
804        const char * Encoding() const           { return encoding.c_str (); }
805        /// Is this a standalone document?
806        const char * Standalone() const         { return standalone.c_str (); }
807
808        // [internal use] Creates a new Element and returs it.
809        virtual TiXmlNode* Clone() const;
810        // [internal use]
811        virtual void Print( FILE* cfile, int depth ) const;
812
813protected:
814        // used to be public
815        #ifdef TIXML_USE_STL
816            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
817        #endif
818        virtual void StreamOut ( TIXML_OSTREAM * out) const;
819        //      [internal use]
820        //      Attribtue parsing starts: next char past '<'
821        //                                       returns: next char past '>'
822
823        virtual const char* Parse( const char* p );
824
825private:
826        TIXML_STRING version;
827        TIXML_STRING encoding;
828        TIXML_STRING standalone;
829};
830
831
832/** Any tag that tinyXml doesn't recognize is save as an
833        unknown. It is a tag of text, but should not be modified.
834        It will be written back to the XML, unchanged, when the file
835        is saved.
836*/
837class TiXmlUnknown : public TiXmlNode
838{
839public:
840        TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
841        virtual ~TiXmlUnknown() {}
842
843        // [internal use]
844        virtual TiXmlNode* Clone() const;
845        // [internal use]
846        virtual void Print( FILE* cfile, int depth ) const;
847protected:
848        // used to be public
849        #ifdef TIXML_USE_STL
850            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
851        #endif
852        virtual void StreamOut ( TIXML_OSTREAM * out ) const;
853        /*      [internal use]
854                Attribute parsing starts: First char of the text
855                                                 returns: next char past '>'
856        */
857        virtual const char* Parse( const char* p );
858};
859
860
861/** Always the top level node. A document binds together all the
862        XML pieces. It can be saved, loaded, and printed to the screen.
863        The 'value' of a document node is the xml file name.
864*/
865class TiXmlDocument : public TiXmlNode
866{
867public:
868        /// Create an empty document, that has no name.
869        TiXmlDocument();
870        /// Create a document with a name. The name of the document is also the filename of the xml.
871        TiXmlDocument( const char * documentName );
872
873        #ifdef TIXML_USE_STL
874        /// Constructor.
875        TiXmlDocument( const std::string& documentName ) :
876            TiXmlNode( TiXmlNode::DOCUMENT )
877        {
878        value = documentName;
879                error = false;
880        }
881        #endif
882
883        virtual ~TiXmlDocument() {}
884
885        /** Load a file using the current document value.
886                Returns true if successful. Will delete any existing
887                document data before loading.
888        */
889        bool LoadFile();
890        /// Save a file using the current document value. Returns true if successful.
891        bool SaveFile() const;
892        /// Load a file using the given filename. Returns true if successful.
893        bool LoadFile( const char * filename );
894        /// Save a file using the given filename. Returns true if successful.
895        bool SaveFile( const char * filename ) const;
896
897        #ifdef TIXML_USE_STL
898        bool LoadFile( const std::string& filename )                    ///< STL std::string version.
899        {
900                return ( LoadFile( filename.c_str() ));
901        }
902        bool SaveFile( const std::string& filename ) const              ///< STL std::string version.
903        {
904                //StringToBuffer f( filename );
905                return ( SaveFile( filename.c_str() ));
906        }
907        #endif
908
909        /// Parse the given null terminated block of xml data.
910        virtual const char* Parse( const char* p );
911
912        /** Get the root element -- the only top level element -- of the document.
913                In well formed XML, there should only be one. TinyXml is tolerant of
914                multiple elements at the document level.
915        */
916        TiXmlElement* RootElement() const               { return FirstChildElement(); }
917
918        /// If, during parsing, a error occurs, Error will be set to true.
919        bool Error() const                                              { return error; }
920
921        /// Contains a textual (english) description of the error if one occurs.
922        const char * ErrorDesc() const  { return errorDesc.c_str (); }
923
924        /** Generally, you probably want the error string ( ErrorDesc() ). But if you
925                        prefer the ErrorId, this function will fetch it.
926                */
927        const int ErrorId()     const                           { return errorId; }
928
929        /// If you have handled the error, it can be reset with this call.
930        void ClearError()                                               { error = false; errorId = 0; errorDesc = ""; }
931
932        /** Dump the document to standard out. */
933        void Print() const                                              { Print( stdout, 0 ); }
934
935        // [internal use]
936        virtual void Print( FILE* cfile, int depth = 0 ) const;
937        // [internal use]
938        void SetError( int err ) {              assert( err > 0 && err < TIXML_ERROR_STRING_COUNT );
939                error   = true;
940                errorId = err;
941        errorDesc = errorString[ errorId ]; }
942
943protected :
944        virtual void StreamOut ( TIXML_OSTREAM * out) const;
945        // [internal use]
946        virtual TiXmlNode* Clone() const;
947        #ifdef TIXML_USE_STL
948            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
949        #endif
950
951private:
952        bool error;
953        int  errorId;
954        TIXML_STRING errorDesc;
955};
956
957#endif
958
Note: See TracBrowser for help on using the repository browser.