~ubuntu-branches/ubuntu/edgy/pouetchess/edgy-updates

« back to all changes in this revision

Viewing changes to src/sxmlgui/tinyxml.h

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc (mr_pouit)
  • Date: 2006-07-15 15:45:57 UTC
  • Revision ID: james.westby@ubuntu.com-20060715154557-3paxq02hx4od0wm4
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
www.sourceforge.net/projects/tinyxml
 
3
Original code (2.0 and earlier )copyright (c) 2000-2002 Lee Thomason (www.grinninglizard.com)
 
4
 
 
5
This software is provided 'as-is', without any express or implied
 
6
warranty. In no event will the authors be held liable for any
 
7
damages arising from the use of this software.
 
8
 
 
9
Permission is granted to anyone to use this software for any
 
10
purpose, including commercial applications, and to alter it and
 
11
redistribute it freely, subject to the following restrictions:
 
12
 
 
13
1. The origin of this software must not be misrepresented; you must
 
14
not claim that you wrote the original software. If you use this
 
15
software in a product, an acknowledgment in the product documentation
 
16
would be appreciated but is not required.
 
17
 
 
18
2. Altered source versions must be plainly marked as such, and
 
19
must not be misrepresented as being the original software.
 
20
 
 
21
3. This notice may not be removed or altered from any source
 
22
distribution.
 
23
*/
 
24
 
 
25
#define TIXML_USE_STL 1
 
26
 
 
27
#ifndef TINYXML_INCLUDED
 
28
#define TINYXML_INCLUDED
 
29
 
 
30
#ifdef _MSC_VER
 
31
#pragma warning( disable : 4530 )
 
32
#pragma warning( disable : 4786 )
 
33
#endif
 
34
 
 
35
#include <ctype.h>
 
36
#include <stdio.h>
 
37
#include <stdlib.h>
 
38
#include <string.h>
 
39
#include <assert.h>
 
40
 
 
41
// Help out windows:
 
42
#if defined( _DEBUG ) && !defined( DEBUG )
 
43
#define DEBUG
 
44
#endif
 
45
 
 
46
#if defined( DEBUG ) && defined( _MSC_VER )
 
47
#include <windows.h>
 
48
#define TIXML_LOG OutputDebugString
 
49
#else
 
50
#define TIXML_LOG printf
 
51
#endif
 
52
 
 
53
#include <string>
 
54
#include <iostream>
 
55
#define TIXML_STRING    std::string
 
56
#define TIXML_ISTREAM   std::istream
 
57
#define TIXML_OSTREAM   std::ostream
 
58
 
 
59
class TiXmlDocument;
 
60
class TiXmlElement;
 
61
class TiXmlComment;
 
62
class TiXmlUnknown;
 
63
class TiXmlAttribute;
 
64
class TiXmlText;
 
65
class TiXmlDeclaration;
 
66
class TiXmlParsingData;
 
67
 
 
68
const int TIXML_MAJOR_VERSION = 2;
 
69
const int TIXML_MINOR_VERSION = 3;
 
70
const int TIXML_PATCH_VERSION = 4;
 
71
 
 
72
/*      Internal structure for tracking location of items
 
73
        in the XML file.
 
74
*/
 
75
struct TiXmlCursor
 
76
{
 
77
        TiXmlCursor()           { Clear(); }
 
78
        void Clear()            { row = col = -1; }
 
79
 
 
80
        int row;        // 0 based.
 
81
        int col;        // 0 based.
 
82
};
 
83
 
 
84
 
 
85
// Only used by Attribute::Query functions
 
86
enum
 
87
{
 
88
        TIXML_SUCCESS,
 
89
        TIXML_NO_ATTRIBUTE,
 
90
        TIXML_WRONG_TYPE
 
91
};
 
92
 
 
93
 
 
94
// Used by the parsing routines.
 
95
enum TiXmlEncoding
 
96
{
 
97
        TIXML_ENCODING_UNKNOWN,
 
98
        TIXML_ENCODING_UTF8,
 
99
        TIXML_ENCODING_LEGACY
 
100
};
 
101
 
 
102
const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
 
103
 
 
104
/** TiXmlBase is a base class for every class in TinyXml.
 
105
        It does little except to establish that TinyXml classes
 
106
        can be printed and provide some utility functions.
 
107
 
 
108
        In XML, the document and elements can contain
 
109
        other elements and other types of nodes.
 
110
 
 
111
        @verbatim
 
112
        A Document can contain: Element (container or leaf)
 
113
                                                        Comment (leaf)
 
114
                                                        Unknown (leaf)
 
115
                                                        Declaration( leaf )
 
116
 
 
117
        An Element can contain: Element (container or leaf)
 
118
                                                        Text    (leaf)
 
119
                                                        Attributes (not on tree)
 
120
                                                        Comment (leaf)
 
121
                                                        Unknown (leaf)
 
122
 
 
123
        A Decleration contains: Attributes (not on tree)
 
124
        @endverbatim
 
125
*/
 
126
class TiXmlBase
 
127
{
 
128
        friend class TiXmlNode;
 
129
        friend class TiXmlElement;
 
130
        friend class TiXmlDocument;
 
131
 
 
132
public:
 
133
        TiXmlBase()     :       userData(0) {}
 
134
        virtual ~TiXmlBase()                                    {}
 
135
 
 
136
        /**     All TinyXml classes can print themselves to a filestream.
 
137
                This is a formatted print, and will insert tabs and newlines.
 
138
 
 
139
                (For an unformatted stream, use the << operator.)
 
140
        */
 
141
        virtual void Print( FILE* cfile, int depth ) const = 0;
 
142
 
 
143
        /**     The world does not agree on whether white space should be kept or
 
144
                not. In order to make everyone happy, these global, static functions
 
145
                are provided to set whether or not TinyXml will condense all white space
 
146
                into a single space or not. The default is to condense. Note changing this
 
147
                values is not thread safe.
 
148
        */
 
149
        static void SetCondenseWhiteSpace( bool condense )              { condenseWhiteSpace = condense; }
 
150
 
 
151
        /// Return the current white space setting.
 
152
        static bool IsWhiteSpaceCondensed()                                             { return condenseWhiteSpace; }
 
153
 
 
154
        /** Return the position, in the original source file, of this node or attribute.
 
155
                The row and column are 1-based. (That is the first row and first column is
 
156
                1,1). If the returns values are 0 or less, then the parser does not have
 
157
                a row and column value.
 
158
 
 
159
                Generally, the row and column value will be set when the TiXmlDocument::Load(),
 
160
                TiXmlDocument::LoadFile(), or any TiXmlNode::Parse() is called. It will NOT be set
 
161
                when the DOM was created from operator>>.
 
162
 
 
163
                The values reflect the initial load. Once the DOM is modified programmatically
 
164
                (by adding or changing nodes and attributes) the new values will NOT update to
 
165
                reflect changes in the document.
 
166
 
 
167
                There is a minor performance cost to computing the row and column. Computation
 
168
                can be disabled if TiXmlDocument::SetTabSize() is called with 0 as the value.
 
169
 
 
170
                @sa TiXmlDocument::SetTabSize()
 
171
        */
 
172
        int Row() const                 { return location.row + 1; }
 
173
        int Column() const              { return location.col + 1; }    ///< See Row()
 
174
 
 
175
        void  SetUserData( void* user )                 { userData = user; }
 
176
        void* GetUserData()                                             { return userData; }
 
177
 
 
178
        // Table that returs, for a given lead byte, the total number of bytes
 
179
        // in the UTF-8 sequence.
 
180
        static const int utf8ByteTable[256];
 
181
 
 
182
        virtual const char* Parse(      const char* p,
 
183
                                                                TiXmlParsingData* data,
 
184
                                                                TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
 
185
 
 
186
        enum
 
187
        {
 
188
                TIXML_NO_ERROR = 0,
 
189
                TIXML_ERROR,
 
190
                TIXML_ERROR_OPENING_FILE,
 
191
                TIXML_ERROR_OUT_OF_MEMORY,
 
192
                TIXML_ERROR_PARSING_ELEMENT,
 
193
                TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
 
194
                TIXML_ERROR_READING_ELEMENT_VALUE,
 
195
                TIXML_ERROR_READING_ATTRIBUTES,
 
196
                TIXML_ERROR_PARSING_EMPTY,
 
197
                TIXML_ERROR_READING_END_TAG,
 
198
                TIXML_ERROR_PARSING_UNKNOWN,
 
199
                TIXML_ERROR_PARSING_COMMENT,
 
200
                TIXML_ERROR_PARSING_DECLARATION,
 
201
                TIXML_ERROR_DOCUMENT_EMPTY,
 
202
                TIXML_ERROR_EMBEDDED_NULL,
 
203
 
 
204
                TIXML_ERROR_STRING_COUNT
 
205
        };
 
206
 
 
207
protected:
 
208
 
 
209
        // See STL_STRING_BUG
 
210
        // Utility class to overcome a bug.
 
211
        class StringToBuffer
 
212
        {
 
213
          public:
 
214
                StringToBuffer( const TIXML_STRING& str );
 
215
                ~StringToBuffer();
 
216
                char* buffer;
 
217
        };
 
218
 
 
219
        static const char*      SkipWhiteSpace( const char*, TiXmlEncoding encoding );
 
220
        inline static bool      IsWhiteSpace( char c )
 
221
        {
 
222
                return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
 
223
        }
 
224
 
 
225
        virtual void StreamOut (TIXML_OSTREAM *) const = 0;
 
226
 
 
227
        #ifdef TIXML_USE_STL
 
228
            static bool StreamWhiteSpace( TIXML_ISTREAM * in, TIXML_STRING * tag );
 
229
            static bool StreamTo( TIXML_ISTREAM * in, int character, TIXML_STRING * tag );
 
230
        #endif
 
231
 
 
232
        /*      Reads an XML name into the string provided. Returns
 
233
                a pointer just past the last character of the name,
 
234
                or 0 if the function has an error.
 
235
        */
 
236
        static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
 
237
 
 
238
        /*      Reads text. Returns a pointer past the given end tag.
 
239
                Wickedly complex options, but it keeps the (sensitive) code in one place.
 
240
        */
 
241
        static const char* ReadText(    const char* in,                         // where to start
 
242
                                                                        TIXML_STRING* text,                     // the string read
 
243
                                                                        bool ignoreWhiteSpace,          // whether to keep the white space
 
244
                                                                        const char* endTag,                     // what ends this text
 
245
                                                                        bool ignoreCase,                        // whether to ignore case in the end tag
 
246
                                                                        TiXmlEncoding encoding );       // the current encoding
 
247
 
 
248
        // If an entity has been found, transform it into a character.
 
249
        static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
 
250
 
 
251
        // Get a character, while interpreting entities.
 
252
        // The length can be from 0 to 4 bytes.
 
253
        inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
 
254
        {
 
255
                assert( p );
 
256
                if ( encoding == TIXML_ENCODING_UTF8 )
 
257
                {
 
258
                        *length = utf8ByteTable[ *((unsigned char*)p) ];
 
259
                        assert( *length >= 0 && *length < 5 );
 
260
                }
 
261
                else
 
262
                {
 
263
                        *length = 1;
 
264
                }
 
265
 
 
266
                if ( *length == 1 )
 
267
                {
 
268
                        if ( *p == '&' )
 
269
                                return GetEntity( p, _value, length, encoding );
 
270
                        *_value = *p;
 
271
                        return p+1;
 
272
                }
 
273
                else if ( *length )
 
274
                {
 
275
                        strncpy( _value, p, *length );
 
276
                        return p + (*length);
 
277
                }
 
278
                else
 
279
                {
 
280
                        // Not valid text.
 
281
                        return 0;
 
282
                }
 
283
        }
 
284
 
 
285
        // Puts a string to a stream, expanding entities as it goes.
 
286
        // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
 
287
        static void PutString( const TIXML_STRING& str, TIXML_OSTREAM* out );
 
288
 
 
289
        static void PutString( const TIXML_STRING& str, TIXML_STRING* out );
 
290
 
 
291
        // Return true if the next characters in the stream are any of the endTag sequences.
 
292
        // Ignore case only works for english, and should only be relied on when comparing
 
293
        // to Engilish words: StringEqual( p, "version", true ) is fine.
 
294
        static bool StringEqual(        const char* p,
 
295
                                                                const char* endTag,
 
296
                                                                bool ignoreCase,
 
297
                                                                TiXmlEncoding encoding );
 
298
 
 
299
        static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
 
300
 
 
301
        TiXmlCursor location;
 
302
 
 
303
    /// Field containing a generic user pointer
 
304
        void*                   userData;
 
305
 
 
306
        // None of these methods are reliable for any language except English.
 
307
        // Good for approximation, not great for accuracy.
 
308
        static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
 
309
        static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
 
310
        inline static int ToLower( int v, TiXmlEncoding encoding )
 
311
        {
 
312
                if ( encoding == TIXML_ENCODING_UTF8 )
 
313
                {
 
314
                        if ( v < 128 ) return tolower( v );
 
315
                        return v;
 
316
                }
 
317
                else
 
318
                {
 
319
                        return tolower( v );
 
320
                }
 
321
        }
 
322
        static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
 
323
 
 
324
private:
 
325
        TiXmlBase( const TiXmlBase& );                          // not implemented.
 
326
        void operator=( const TiXmlBase& base );        // not allowed.
 
327
 
 
328
        struct Entity
 
329
        {
 
330
                const char*     str;
 
331
                unsigned int    strLength;
 
332
                char                chr;
 
333
        };
 
334
        enum
 
335
        {
 
336
                NUM_ENTITY = 5,
 
337
                MAX_ENTITY_LENGTH = 6
 
338
 
 
339
        };
 
340
        static Entity entity[ NUM_ENTITY ];
 
341
        static bool condenseWhiteSpace;
 
342
};
 
343
 
 
344
 
 
345
/** The parent class for everything in the Document Object Model.
 
346
        (Except for attributes).
 
347
        Nodes have siblings, a parent, and children. A node can be
 
348
        in a document, or stand on its own. The type of a TiXmlNode
 
349
        can be queried, and it can be cast to its more defined type.
 
350
*/
 
351
class TiXmlNode : public TiXmlBase
 
352
{
 
353
        friend class TiXmlDocument;
 
354
        friend class TiXmlElement;
 
355
 
 
356
public:
 
357
        #ifdef TIXML_USE_STL
 
358
 
 
359
            /** An input stream operator, for every class. Tolerant of newlines and
 
360
                    formatting, but doesn't expect them.
 
361
            */
 
362
            friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
 
363
 
 
364
            /** An output stream operator, for every class. Note that this outputs
 
365
                    without any newlines or formatting, as opposed to Print(), which
 
366
                    includes tabs and new lines.
 
367
 
 
368
                    The operator<< and operator>> are not completely symmetric. Writing
 
369
                    a node to a stream is very well defined. You'll get a nice stream
 
370
                    of output, without any extra whitespace or newlines.
 
371
 
 
372
                    But reading is not as well defined. (As it always is.) If you create
 
373
                    a TiXmlElement (for example) and read that from an input stream,
 
374
                    the text needs to define an element or junk will result. This is
 
375
                    true of all input streams, but it's worth keeping in mind.
 
376
 
 
377
                    A TiXmlDocument will read nodes until it reads a root element, and
 
378
                        all the children of that root element.
 
379
            */
 
380
            friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
 
381
 
 
382
                /// Appends the XML node or attribute to a std::string.
 
383
                friend std::string& operator<< (std::string& out, const TiXmlNode& base );
 
384
 
 
385
        #else
 
386
            // Used internally, not part of the public API.
 
387
            friend TIXML_OSTREAM& operator<< (TIXML_OSTREAM& out, const TiXmlNode& base);
 
388
        #endif
 
389
 
 
390
        /** The types of XML nodes supported by TinyXml. (All the
 
391
                        unsupported types are picked up by UNKNOWN.)
 
392
        */
 
393
        enum NodeType
 
394
        {
 
395
                DOCUMENT,
 
396
                ELEMENT,
 
397
                COMMENT,
 
398
                UNKNOWN,
 
399
                TEXT,
 
400
                DECLARATION,
 
401
                TYPECOUNT
 
402
        };
 
403
 
 
404
        virtual ~TiXmlNode();
 
405
 
 
406
        /** The meaning of 'value' changes for the specific type of
 
407
                TiXmlNode.
 
408
                @verbatim
 
409
                Document:       filename of the xml file
 
410
                Element:        name of the element
 
411
                Comment:        the comment text
 
412
                Unknown:        the tag contents
 
413
                Text:           the text string
 
414
                @endverbatim
 
415
 
 
416
                The subclasses will wrap this function.
 
417
        */
 
418
        const char * Value() const { return value.c_str (); }
 
419
 
 
420
        /** Changes the value of the node. Defined as:
 
421
                @verbatim
 
422
                Document:       filename of the xml file
 
423
                Element:        name of the element
 
424
                Comment:        the comment text
 
425
                Unknown:        the tag contents
 
426
                Text:           the text string
 
427
                @endverbatim
 
428
        */
 
429
        void SetValue(const char * _value) { value = _value;}
 
430
 
 
431
    #ifdef TIXML_USE_STL
 
432
        /// STL std::string form.
 
433
        void SetValue( const std::string& _value )
 
434
        {
 
435
                StringToBuffer buf( _value );
 
436
                SetValue( buf.buffer ? buf.buffer : "" );
 
437
        }
 
438
        #endif
 
439
 
 
440
        /// Delete all the children of this node. Does not affect 'this'.
 
441
        void Clear();
 
442
 
 
443
        /// One step up the DOM.
 
444
        TiXmlNode* Parent()                                                     { return parent; }
 
445
        const TiXmlNode* Parent() const                         { return parent; }
 
446
 
 
447
        const TiXmlNode* FirstChild()   const   { return firstChild; }          ///< The first child of this node. Will be null if there are no children.
 
448
        TiXmlNode* FirstChild()                                 { return firstChild; }
 
449
        const TiXmlNode* FirstChild( const char * value ) const;                        ///< The first child of this node with the matching 'value'. Will be null if none found.
 
450
        TiXmlNode* FirstChild( const char * value );                                            ///< The first child of this node with the matching 'value'. Will be null if none found.
 
451
 
 
452
        const TiXmlNode* LastChild() const      { return lastChild; }           /// The last child of this node. Will be null if there are no children.
 
453
        TiXmlNode* LastChild()  { return lastChild; }
 
454
        const TiXmlNode* LastChild( const char * value ) const;                 /// The last child of this node matching 'value'. Will be null if there are no children.
 
455
        TiXmlNode* LastChild( const char * value );
 
456
 
 
457
    #ifdef TIXML_USE_STL
 
458
        const TiXmlNode* FirstChild( const std::string& _value ) const  {       return FirstChild (_value.c_str ());    }       ///< STL std::string form.
 
459
        TiXmlNode* FirstChild( const std::string& _value )                              {       return FirstChild (_value.c_str ());    }       ///< STL std::string form.
 
460
        const TiXmlNode* LastChild( const std::string& _value ) const   {       return LastChild (_value.c_str ());     }       ///< STL std::string form.
 
461
        TiXmlNode* LastChild( const std::string& _value )                               {       return LastChild (_value.c_str ());     }       ///< STL std::string form.
 
462
        #endif
 
463
 
 
464
        /** An alternate way to walk the children of a node.
 
465
                One way to iterate over nodes is:
 
466
                @verbatim
 
467
                        for( child = parent->FirstChild(); child; child = child->NextSibling() )
 
468
                @endverbatim
 
469
 
 
470
                IterateChildren does the same thing with the syntax:
 
471
                @verbatim
 
472
                        child = 0;
 
473
                        while( child = parent->IterateChildren( child ) )
 
474
                @endverbatim
 
475
 
 
476
                IterateChildren takes the previous child as input and finds
 
477
                the next one. If the previous child is null, it returns the
 
478
                first. IterateChildren will return null when done.
 
479
        */
 
480
        const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
 
481
        TiXmlNode* IterateChildren( TiXmlNode* previous );
 
482
 
 
483
        /// This flavor of IterateChildren searches for children with a particular 'value'
 
484
        const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
 
485
        TiXmlNode* IterateChildren( const char * value, TiXmlNode* previous );
 
486
 
 
487
    #ifdef TIXML_USE_STL
 
488
        const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {       return IterateChildren (_value.c_str (), previous);     }       ///< STL std::string form.
 
489
        TiXmlNode* IterateChildren( const std::string& _value, TiXmlNode* previous ) {  return IterateChildren (_value.c_str (), previous);     }       ///< STL std::string form.
 
490
        #endif
 
491
 
 
492
        /** Add a new node related to this. Adds a child past the LastChild.
 
493
                Returns a pointer to the new object or NULL if an error occured.
 
494
        */
 
495
        TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
 
496
 
 
497
 
 
498
        /** Add a new node related to this. Adds a child past the LastChild.
 
499
 
 
500
                NOTE: the node to be added is passed by pointer, and will be
 
501
                henceforth owned (and deleted) by tinyXml. This method is efficient
 
502
                and avoids an extra copy, but should be used with care as it
 
503
                uses a different memory model than the other insert functions.
 
504
 
 
505
                @sa InsertEndChild
 
506
        */
 
507
        TiXmlNode* LinkEndChild( TiXmlNode* addThis );
 
508
 
 
509
        /** Add a new node related to this. Adds a child before the specified child.
 
510
                Returns a pointer to the new object or NULL if an error occured.
 
511
        */
 
512
        TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
 
513
 
 
514
        /** Add a new node related to this. Adds a child after the specified child.
 
515
                Returns a pointer to the new object or NULL if an error occured.
 
516
        */
 
517
        TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
 
518
 
 
519
        /** Replace a child of this node.
 
520
                Returns a pointer to the new object or NULL if an error occured.
 
521
        */
 
522
        TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
 
523
 
 
524
        /// Delete a child of this node.
 
525
        bool RemoveChild( TiXmlNode* removeThis );
 
526
 
 
527
        /// Navigate to a sibling node.
 
528
        const TiXmlNode* PreviousSibling() const                        { return prev; }
 
529
        TiXmlNode* PreviousSibling()                                            { return prev; }
 
530
 
 
531
        /// Navigate to a sibling node.
 
532
        const TiXmlNode* PreviousSibling( const char * ) const;
 
533
        TiXmlNode* PreviousSibling( const char * );
 
534
 
 
535
    #ifdef TIXML_USE_STL
 
536
        const TiXmlNode* PreviousSibling( const std::string& _value ) const     {       return PreviousSibling (_value.c_str ());       }       ///< STL std::string form.
 
537
        TiXmlNode* PreviousSibling( const std::string& _value )                         {       return PreviousSibling (_value.c_str ());       }       ///< STL std::string form.
 
538
        const TiXmlNode* NextSibling( const std::string& _value) const          {       return NextSibling (_value.c_str ());   }       ///< STL std::string form.
 
539
        TiXmlNode* NextSibling( const std::string& _value)                                      {       return NextSibling (_value.c_str ());   }       ///< STL std::string form.
 
540
        #endif
 
541
 
 
542
        /// Navigate to a sibling node.
 
543
        const TiXmlNode* NextSibling() const                            { return next; }
 
544
        TiXmlNode* NextSibling()                                                        { return next; }
 
545
 
 
546
        /// Navigate to a sibling node with the given 'value'.
 
547
        const TiXmlNode* NextSibling( const char * ) const;
 
548
        TiXmlNode* NextSibling( const char * );
 
549
 
 
550
        /** Convenience function to get through elements.
 
551
                Calls NextSibling and ToElement. Will skip all non-Element
 
552
                nodes. Returns 0 if there is not another element.
 
553
        */
 
554
        const TiXmlElement* NextSiblingElement() const;
 
555
        TiXmlElement* NextSiblingElement();
 
556
 
 
557
        /** Convenience function to get through elements.
 
558
                Calls NextSibling and ToElement. Will skip all non-Element
 
559
                nodes. Returns 0 if there is not another element.
 
560
        */
 
561
        const TiXmlElement* NextSiblingElement( const char * ) const;
 
562
        TiXmlElement* NextSiblingElement( const char * );
 
563
 
 
564
    #ifdef TIXML_USE_STL
 
565
        const TiXmlElement* NextSiblingElement( const std::string& _value) const        {       return NextSiblingElement (_value.c_str ());    }       ///< STL std::string form.
 
566
        TiXmlElement* NextSiblingElement( const std::string& _value)                            {       return NextSiblingElement (_value.c_str ());    }       ///< STL std::string form.
 
567
        #endif
 
568
 
 
569
        /// Convenience function to get through elements.
 
570
        const TiXmlElement* FirstChildElement() const;
 
571
        TiXmlElement* FirstChildElement();
 
572
 
 
573
        /// Convenience function to get through elements.
 
574
        const TiXmlElement* FirstChildElement( const char * value ) const;
 
575
        TiXmlElement* FirstChildElement( const char * value );
 
576
 
 
577
    #ifdef TIXML_USE_STL
 
578
        const TiXmlElement* FirstChildElement( const std::string& _value ) const        {       return FirstChildElement (_value.c_str ());     }       ///< STL std::string form.
 
579
        TiXmlElement* FirstChildElement( const std::string& _value )                            {       return FirstChildElement (_value.c_str ());     }       ///< STL std::string form.
 
580
        #endif
 
581
 
 
582
        /** Query the type (as an enumerated value, above) of this node.
 
583
                The possible types are: DOCUMENT, ELEMENT, COMMENT,
 
584
                                                                UNKNOWN, TEXT, and DECLARATION.
 
585
        */
 
586
        virtual int Type() const        { return type; }
 
587
 
 
588
        /** Return a pointer to the Document this node lives in.
 
589
                Returns null if not in a document.
 
590
        */
 
591
        const TiXmlDocument* GetDocument() const;
 
592
        TiXmlDocument* GetDocument();
 
593
 
 
594
        /// Returns true if this node has no children.
 
595
        bool NoChildren() const                                         { return !firstChild; }
 
596
 
 
597
        const TiXmlDocument* ToDocument()       const           { return ( this && type == DOCUMENT ) ? (const TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
598
        const TiXmlElement*  ToElement() const                  { return ( this && type == ELEMENT  ) ? (const TiXmlElement*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
599
        const TiXmlComment*  ToComment() const                  { return ( this && type == COMMENT  ) ? (const TiXmlComment*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
600
        const TiXmlUnknown*  ToUnknown() const                  { return ( this && type == UNKNOWN  ) ? (const TiXmlUnknown*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
601
        const TiXmlText*           ToText()    const            { return ( this && type == TEXT     ) ? (const TiXmlText*)     this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
602
        const TiXmlDeclaration* ToDeclaration() const   { return ( this && type == DECLARATION ) ? (const TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
603
 
 
604
        TiXmlDocument* ToDocument()                     { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
605
        TiXmlElement*  ToElement()                      { return ( this && type == ELEMENT  ) ? (TiXmlElement*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
606
        TiXmlComment*  ToComment()                      { return ( this && type == COMMENT  ) ? (TiXmlComment*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
607
        TiXmlUnknown*  ToUnknown()                      { return ( this && type == UNKNOWN  ) ? (TiXmlUnknown*)  this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
608
        TiXmlText*         ToText()                     { return ( this && type == TEXT     ) ? (TiXmlText*)     this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
609
        TiXmlDeclaration* ToDeclaration()       { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
 
610
 
 
611
        /** Create an exact duplicate of this node and return it. The memory must be deleted
 
612
                by the caller.
 
613
        */
 
614
        virtual TiXmlNode* Clone() const = 0;
 
615
 
 
616
protected:
 
617
        TiXmlNode( NodeType _type );
 
618
 
 
619
        // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
 
620
        // and the assignment operator.
 
621
        void CopyTo( TiXmlNode* target ) const;
 
622
 
 
623
        #ifdef TIXML_USE_STL
 
624
            // The real work of the input operator.
 
625
            virtual void StreamIn( TIXML_ISTREAM* in, TIXML_STRING* tag ) = 0;
 
626
        #endif
 
627
 
 
628
        // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
 
629
        TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
 
630
 
 
631
        // Internal Value function returning a TIXML_STRING
 
632
        const TIXML_STRING& SValue() const      { return value ; }
 
633
 
 
634
        TiXmlNode*              parent;
 
635
        NodeType                type;
 
636
 
 
637
        TiXmlNode*              firstChild;
 
638
        TiXmlNode*              lastChild;
 
639
 
 
640
        TIXML_STRING    value;
 
641
 
 
642
        TiXmlNode*              prev;
 
643
        TiXmlNode*              next;
 
644
 
 
645
private:
 
646
        TiXmlNode( const TiXmlNode& );                          // not implemented.
 
647
        void operator=( const TiXmlNode& base );        // not allowed.
 
648
};
 
649
 
 
650
 
 
651
/** An attribute is a name-value pair. Elements have an arbitrary
 
652
        number of attributes, each with a unique name.
 
653
 
 
654
        @note The attributes are not TiXmlNodes, since they are not
 
655
                  part of the tinyXML document object model. There are other
 
656
                  suggested ways to look at this problem.
 
657
*/
 
658
class TiXmlAttribute : public TiXmlBase
 
659
{
 
660
        friend class TiXmlAttributeSet;
 
661
 
 
662
public:
 
663
        /// Construct an empty attribute.
 
664
        TiXmlAttribute() : TiXmlBase()
 
665
        {
 
666
                document = 0;
 
667
                prev = next = 0;
 
668
        }
 
669
 
 
670
        #ifdef TIXML_USE_STL
 
671
        /// std::string constructor.
 
672
        TiXmlAttribute( const std::string& _name, const std::string& _value )
 
673
        {
 
674
                name = _name;
 
675
                value = _value;
 
676
                document = 0;
 
677
                prev = next = 0;
 
678
        }
 
679
        #endif
 
680
 
 
681
        /// Construct an attribute with a name and value.
 
682
        TiXmlAttribute( const char * _name, const char * _value )
 
683
        {
 
684
                name = _name;
 
685
                value = _value;
 
686
                document = 0;
 
687
                prev = next = 0;
 
688
        }
 
689
 
 
690
        const char*             Name()  const           { return name.c_str (); }               ///< Return the name of this attribute.
 
691
        const char*             Value() const           { return value.c_str (); }              ///< Return the value of this attribute.
 
692
        const int       IntValue() const;                                                                       ///< Return the value of this attribute, converted to an integer.
 
693
        const double    DoubleValue() const;                                                            ///< Return the value of this attribute, converted to a double.
 
694
 
 
695
        /** QueryIntValue examines the value string. It is an alternative to the
 
696
                IntValue() method with richer error checking.
 
697
                If the value is an integer, it is stored in 'value' and
 
698
                the call returns TIXML_SUCCESS. If it is not
 
699
                an integer, it returns TIXML_WRONG_TYPE.
 
700
 
 
701
                A specialized but useful call. Note that for success it returns 0,
 
702
                which is the opposite of almost all other TinyXml calls.
 
703
        */
 
704
        int QueryIntValue( int* value ) const;
 
705
        /// QueryDoubleValue examines the value string. See QueryIntValue().
 
706
        int QueryDoubleValue( double* value ) const;
 
707
 
 
708
        void SetName( const char* _name )       { name = _name; }                               ///< Set the name of this attribute.
 
709
        void SetValue( const char* _value )     { value = _value; }                             ///< Set the value.
 
710
 
 
711
        void SetIntValue( int value );                                                                          ///< Set the value from an integer.
 
712
        void SetDoubleValue( double value );                                                            ///< Set the value from a double.
 
713
 
 
714
    #ifdef TIXML_USE_STL
 
715
        /// STL std::string form.
 
716
        void SetName( const std::string& _name )
 
717
        {
 
718
                StringToBuffer buf( _name );
 
719
                SetName ( buf.buffer ? buf.buffer : "error" );
 
720
        }
 
721
        /// STL std::string form.
 
722
        void SetValue( const std::string& _value )
 
723
        {
 
724
                StringToBuffer buf( _value );
 
725
                SetValue( buf.buffer ? buf.buffer : "error" );
 
726
        }
 
727
        #endif
 
728
 
 
729
        /// Get the next sibling attribute in the DOM. Returns null at end.
 
730
        const TiXmlAttribute* Next() const;
 
731
        TiXmlAttribute* Next();
 
732
        /// Get the previous sibling attribute in the DOM. Returns null at beginning.
 
733
        const TiXmlAttribute* Previous() const;
 
734
        TiXmlAttribute* Previous();
 
735
 
 
736
        bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
 
737
        bool operator<( const TiXmlAttribute& rhs )      const { return name < rhs.name; }
 
738
        bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
 
739
 
 
740
        /*      Attribute parsing starts: first letter of the name
 
741
                                                 returns: the next char after the value end quote
 
742
        */
 
743
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
744
 
 
745
        // Prints this Attribute to a FILE stream.
 
746
        virtual void Print( FILE* cfile, int depth ) const;
 
747
 
 
748
        virtual void StreamOut( TIXML_OSTREAM * out ) const;
 
749
        // [internal use]
 
750
        // Set the document pointer so the attribute can report errors.
 
751
        void SetDocument( TiXmlDocument* doc )  { document = doc; }
 
752
 
 
753
private:
 
754
        TiXmlAttribute( const TiXmlAttribute& );                                // not implemented.
 
755
        void operator=( const TiXmlAttribute& base );   // not allowed.
 
756
 
 
757
        TiXmlDocument*  document;       // A pointer back to a document, for error reporting.
 
758
        TIXML_STRING name;
 
759
        TIXML_STRING value;
 
760
        TiXmlAttribute* prev;
 
761
        TiXmlAttribute* next;
 
762
};
 
763
 
 
764
 
 
765
/*      A class used to manage a group of attributes.
 
766
        It is only used internally, both by the ELEMENT and the DECLARATION.
 
767
 
 
768
        The set can be changed transparent to the Element and Declaration
 
769
        classes that use it, but NOT transparent to the Attribute
 
770
        which has to implement a next() and previous() method. Which makes
 
771
        it a bit problematic and prevents the use of STL.
 
772
 
 
773
        This version is implemented with circular lists because:
 
774
                - I like circular lists
 
775
                - it demonstrates some independence from the (typical) doubly linked list.
 
776
*/
 
777
class TiXmlAttributeSet
 
778
{
 
779
public:
 
780
        TiXmlAttributeSet();
 
781
        ~TiXmlAttributeSet();
 
782
 
 
783
        void Add( TiXmlAttribute* attribute );
 
784
        void Remove( TiXmlAttribute* attribute );
 
785
 
 
786
        const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
 
787
        TiXmlAttribute* First()                                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
 
788
        const TiXmlAttribute* Last() const              { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
 
789
        TiXmlAttribute* Last()                                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
 
790
 
 
791
        const TiXmlAttribute*   Find( const char * name ) const;
 
792
        TiXmlAttribute* Find( const char * name );
 
793
 
 
794
private:
 
795
        //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
 
796
        //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
 
797
        TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
 
798
        void operator=( const TiXmlAttributeSet& );     // not allowed (as TiXmlAttribute)
 
799
 
 
800
        TiXmlAttribute sentinel;
 
801
};
 
802
 
 
803
 
 
804
/** The element is a container class. It has a value, the element name,
 
805
        and can contain other elements, text, comments, and unknowns.
 
806
        Elements also contain an arbitrary number of attributes.
 
807
*/
 
808
class TiXmlElement : public TiXmlNode
 
809
{
 
810
public:
 
811
        /// Construct an element.
 
812
        TiXmlElement (const char * in_value);
 
813
 
 
814
        #ifdef TIXML_USE_STL
 
815
        /// std::string constructor.
 
816
        TiXmlElement( const std::string& _value );
 
817
        #endif
 
818
 
 
819
        TiXmlElement( const TiXmlElement& );
 
820
 
 
821
        void operator=( const TiXmlElement& base );
 
822
 
 
823
        virtual ~TiXmlElement();
 
824
 
 
825
        /** Given an attribute name, Attribute() returns the value
 
826
                for the attribute of that name, or null if none exists.
 
827
        */
 
828
        const char* Attribute( const char* name ) const;
 
829
 
 
830
        /** Given an attribute name, Attribute() returns the value
 
831
                for the attribute of that name, or null if none exists.
 
832
                If the attribute exists and can be converted to an integer,
 
833
                the integer value will be put in the return 'i', if 'i'
 
834
                is non-null.
 
835
        */
 
836
        const char* Attribute( const char* name, int* i ) const;
 
837
 
 
838
        /** Given an attribute name, Attribute() returns the value
 
839
                for the attribute of that name, or null if none exists.
 
840
                If the attribute exists and can be converted to an double,
 
841
                the double value will be put in the return 'd', if 'd'
 
842
                is non-null.
 
843
        */
 
844
        const char* Attribute( const char* name, double* d ) const;
 
845
 
 
846
        /** QueryIntAttribute examines the attribute - it is an alternative to the
 
847
                Attribute() method with richer error checking.
 
848
                If the attribute is an integer, it is stored in 'value' and
 
849
                the call returns TIXML_SUCCESS. If it is not
 
850
                an integer, it returns TIXML_WRONG_TYPE. If the attribute
 
851
                does not exist, then TIXML_NO_ATTRIBUTE is returned.
 
852
        */
 
853
        int QueryIntAttribute( const char* name, int* value ) const;
 
854
        /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
 
855
        int QueryDoubleAttribute( const char* name, double* value ) const;
 
856
        /// QueryFloatAttribute examines the attribute - see QueryIntAttribute().
 
857
        int QueryDoubleAttribute( const char* name, float* value ) const {
 
858
                double d;
 
859
                int result = QueryDoubleAttribute( name, &d );
 
860
                *value = (float)d;
 
861
                return result;
 
862
        }
 
863
 
 
864
        /** Sets an attribute of name to a given value. The attribute
 
865
                will be created if it does not exist, or changed if it does.
 
866
        */
 
867
        void SetAttribute( const char* name, const char * value );
 
868
 
 
869
    #ifdef TIXML_USE_STL
 
870
        const char* Attribute( const std::string& name ) const                          { return Attribute( name.c_str() ); }
 
871
        const char* Attribute( const std::string& name, int* i ) const          { return Attribute( name.c_str(), i ); }
 
872
        const char* Attribute( const std::string& name, double* d ) const       { return Attribute( name.c_str(), d ); }
 
873
        int QueryIntAttribute( const std::string& name, int* value ) const      { return QueryIntAttribute( name.c_str(), value ); }
 
874
        int QueryDoubleAttribute( const std::string& name, double* value ) const { return QueryDoubleAttribute( name.c_str(), value ); }
 
875
 
 
876
        /// STL std::string form.
 
877
        void SetAttribute( const std::string& name, const std::string& _value )
 
878
        {
 
879
                StringToBuffer n( name );
 
880
                StringToBuffer v( _value );
 
881
                if ( n.buffer && v.buffer )
 
882
                        SetAttribute (n.buffer, v.buffer );
 
883
        }
 
884
        ///< STL std::string form.
 
885
        void SetAttribute( const std::string& name, int _value )
 
886
        {
 
887
                StringToBuffer n( name );
 
888
                if ( n.buffer )
 
889
                        SetAttribute (n.buffer, _value);
 
890
        }
 
891
        #endif
 
892
 
 
893
        /** Sets an attribute of name to a given value. The attribute
 
894
                will be created if it does not exist, or changed if it does.
 
895
        */
 
896
        void SetAttribute( const char * name, int value );
 
897
 
 
898
        /** Sets an attribute of name to a given value. The attribute
 
899
                will be created if it does not exist, or changed if it does.
 
900
        */
 
901
        void SetDoubleAttribute( const char * name, double value );
 
902
 
 
903
        /** Deletes an attribute with the given name.
 
904
        */
 
905
        void RemoveAttribute( const char * name );
 
906
    #ifdef TIXML_USE_STL
 
907
        void RemoveAttribute( const std::string& name ) {       RemoveAttribute (name.c_str ());        }       ///< STL std::string form.
 
908
        #endif
 
909
 
 
910
        const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }                ///< Access the first attribute in this element.
 
911
        TiXmlAttribute* FirstAttribute()                                { return attributeSet.First(); }
 
912
        const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }         ///< Access the last attribute in this element.
 
913
        TiXmlAttribute* LastAttribute()                                 { return attributeSet.Last(); }
 
914
 
 
915
        /// Creates a new Element and returns it - the returned element is a copy.
 
916
        virtual TiXmlNode* Clone() const;
 
917
        // Print the Element to a FILE stream.
 
918
        virtual void Print( FILE* cfile, int depth ) const;
 
919
 
 
920
        /*      Attribtue parsing starts: next char past '<'
 
921
                                                 returns: next char past '>'
 
922
        */
 
923
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
924
 
 
925
protected:
 
926
 
 
927
        void CopyTo( TiXmlElement* target ) const;
 
928
        void ClearThis();       // like clear, but initializes 'this' object as well
 
929
 
 
930
        // Used to be public [internal use]
 
931
        #ifdef TIXML_USE_STL
 
932
            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
 
933
        #endif
 
934
        virtual void StreamOut( TIXML_OSTREAM * out ) const;
 
935
 
 
936
        /*      [internal use]
 
937
                Reads the "value" of the element -- another element, or text.
 
938
                This should terminate with the current end tag.
 
939
        */
 
940
        const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
 
941
 
 
942
private:
 
943
 
 
944
        TiXmlAttributeSet attributeSet;
 
945
};
 
946
 
 
947
 
 
948
/**     An XML comment.
 
949
*/
 
950
class TiXmlComment : public TiXmlNode
 
951
{
 
952
public:
 
953
        /// Constructs an empty comment.
 
954
        TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
 
955
        TiXmlComment( const TiXmlComment& );
 
956
        void operator=( const TiXmlComment& base );
 
957
 
 
958
        virtual ~TiXmlComment() {}
 
959
 
 
960
        /// Returns a copy of this Comment.
 
961
        virtual TiXmlNode* Clone() const;
 
962
        /// Write this Comment to a FILE stream.
 
963
        virtual void Print( FILE* cfile, int depth ) const;
 
964
 
 
965
        /*      Attribtue parsing starts: at the ! of the !--
 
966
                                                 returns: next char past '>'
 
967
        */
 
968
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
969
 
 
970
protected:
 
971
        void CopyTo( TiXmlComment* target ) const;
 
972
 
 
973
        // used to be public
 
974
        #ifdef TIXML_USE_STL
 
975
            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
 
976
        #endif
 
977
        virtual void StreamOut( TIXML_OSTREAM * out ) const;
 
978
 
 
979
private:
 
980
 
 
981
};
 
982
 
 
983
 
 
984
/** XML text. Contained in an element.
 
985
*/
 
986
class TiXmlText : public TiXmlNode
 
987
{
 
988
        friend class TiXmlElement;
 
989
public:
 
990
        /// Constructor.
 
991
        TiXmlText (const char * initValue) : TiXmlNode (TiXmlNode::TEXT)
 
992
        {
 
993
                SetValue( initValue );
 
994
        }
 
995
        virtual ~TiXmlText() {}
 
996
 
 
997
        #ifdef TIXML_USE_STL
 
998
        /// Constructor.
 
999
        TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
 
1000
        {
 
1001
                SetValue( initValue );
 
1002
        }
 
1003
        #endif
 
1004
 
 
1005
        TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )       { copy.CopyTo( this ); }
 
1006
        void operator=( const TiXmlText& base )                                                         { base.CopyTo( this ); }
 
1007
 
 
1008
        /// Write this text object to a FILE stream.
 
1009
        virtual void Print( FILE* cfile, int depth ) const;
 
1010
 
 
1011
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1012
 
 
1013
protected :
 
1014
        ///  [internal use] Creates a new Element and returns it.
 
1015
        virtual TiXmlNode* Clone() const;
 
1016
        void CopyTo( TiXmlText* target ) const;
 
1017
 
 
1018
        virtual void StreamOut ( TIXML_OSTREAM * out ) const;
 
1019
        bool Blank() const;     // returns true if all white space and new lines
 
1020
        // [internal use]
 
1021
        #ifdef TIXML_USE_STL
 
1022
            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
 
1023
        #endif
 
1024
 
 
1025
private:
 
1026
};
 
1027
 
 
1028
 
 
1029
/** In correct XML the declaration is the first entry in the file.
 
1030
        @verbatim
 
1031
                <?xml version="1.0" standalone="yes"?>
 
1032
        @endverbatim
 
1033
 
 
1034
        TinyXml will happily read or write files without a declaration,
 
1035
        however. There are 3 possible attributes to the declaration:
 
1036
        version, encoding, and standalone.
 
1037
 
 
1038
        Note: In this version of the code, the attributes are
 
1039
        handled as special cases, not generic attributes, simply
 
1040
        because there can only be at most 3 and they are always the same.
 
1041
*/
 
1042
class TiXmlDeclaration : public TiXmlNode
 
1043
{
 
1044
public:
 
1045
        /// Construct an empty declaration.
 
1046
        TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
 
1047
 
 
1048
#ifdef TIXML_USE_STL
 
1049
        /// Constructor.
 
1050
        TiXmlDeclaration(       const std::string& _version,
 
1051
                                                const std::string& _encoding,
 
1052
                                                const std::string& _standalone );
 
1053
#endif
 
1054
 
 
1055
        /// Construct.
 
1056
        TiXmlDeclaration(       const char* _version,
 
1057
                                                const char* _encoding,
 
1058
                                                const char* _standalone );
 
1059
 
 
1060
        TiXmlDeclaration( const TiXmlDeclaration& copy );
 
1061
        void operator=( const TiXmlDeclaration& copy );
 
1062
 
 
1063
        virtual ~TiXmlDeclaration()     {}
 
1064
 
 
1065
        /// Version. Will return an empty string if none was found.
 
1066
        const char *Version() const                     { return version.c_str (); }
 
1067
        /// Encoding. Will return an empty string if none was found.
 
1068
        const char *Encoding() const            { return encoding.c_str (); }
 
1069
        /// Is this a standalone document?
 
1070
        const char *Standalone() const          { return standalone.c_str (); }
 
1071
 
 
1072
        /// Creates a copy of this Declaration and returns it.
 
1073
        virtual TiXmlNode* Clone() const;
 
1074
        /// Print this declaration to a FILE stream.
 
1075
        virtual void Print( FILE* cfile, int depth ) const;
 
1076
 
 
1077
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1078
 
 
1079
protected:
 
1080
        void CopyTo( TiXmlDeclaration* target ) const;
 
1081
        // used to be public
 
1082
        #ifdef TIXML_USE_STL
 
1083
            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
 
1084
        #endif
 
1085
        virtual void StreamOut ( TIXML_OSTREAM * out) const;
 
1086
 
 
1087
private:
 
1088
 
 
1089
        TIXML_STRING version;
 
1090
        TIXML_STRING encoding;
 
1091
        TIXML_STRING standalone;
 
1092
};
 
1093
 
 
1094
 
 
1095
/** Any tag that tinyXml doesn't recognize is saved as an
 
1096
        unknown. It is a tag of text, but should not be modified.
 
1097
        It will be written back to the XML, unchanged, when the file
 
1098
        is saved.
 
1099
 
 
1100
        DTD tags get thrown into TiXmlUnknowns.
 
1101
*/
 
1102
class TiXmlUnknown : public TiXmlNode
 
1103
{
 
1104
public:
 
1105
        TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )        {}
 
1106
        virtual ~TiXmlUnknown() {}
 
1107
 
 
1108
        TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )              { copy.CopyTo( this ); }
 
1109
        void operator=( const TiXmlUnknown& copy )                                                                              { copy.CopyTo( this ); }
 
1110
 
 
1111
        /// Creates a copy of this Unknown and returns it.
 
1112
        virtual TiXmlNode* Clone() const;
 
1113
        /// Print this Unknown to a FILE stream.
 
1114
        virtual void Print( FILE* cfile, int depth ) const;
 
1115
 
 
1116
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1117
 
 
1118
protected:
 
1119
        void CopyTo( TiXmlUnknown* target ) const;
 
1120
 
 
1121
        #ifdef TIXML_USE_STL
 
1122
            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
 
1123
        #endif
 
1124
        virtual void StreamOut ( TIXML_OSTREAM * out ) const;
 
1125
 
 
1126
private:
 
1127
 
 
1128
};
 
1129
 
 
1130
 
 
1131
/** Always the top level node. A document binds together all the
 
1132
        XML pieces. It can be saved, loaded, and printed to the screen.
 
1133
        The 'value' of a document node is the xml file name.
 
1134
*/
 
1135
class TiXmlDocument : public TiXmlNode
 
1136
{
 
1137
public:
 
1138
        /// Create an empty document, that has no name.
 
1139
        TiXmlDocument();
 
1140
        /// Create a document with a name. The name of the document is also the filename of the xml.
 
1141
        TiXmlDocument( const char * documentName );
 
1142
 
 
1143
        #ifdef TIXML_USE_STL
 
1144
        /// Constructor.
 
1145
        TiXmlDocument( const std::string& documentName );
 
1146
        #endif
 
1147
 
 
1148
        TiXmlDocument( const TiXmlDocument& copy );
 
1149
        void operator=( const TiXmlDocument& copy );
 
1150
 
 
1151
        virtual ~TiXmlDocument() {}
 
1152
 
 
1153
        /** Load a file using the current document value.
 
1154
                Returns true if successful. Will delete any existing
 
1155
                document data before loading.
 
1156
        */
 
1157
        bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 
1158
        /// Save a file using the current document value. Returns true if successful.
 
1159
        bool SaveFile() const;
 
1160
        /// Load a file using the given filename. Returns true if successful.
 
1161
        bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 
1162
        /// Save a file using the given filename. Returns true if successful.
 
1163
        bool SaveFile( const char * filename ) const;
 
1164
 
 
1165
        #ifdef TIXML_USE_STL
 
1166
        bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )                   ///< STL std::string version.
 
1167
        {
 
1168
                StringToBuffer f( filename );
 
1169
                return ( f.buffer && LoadFile( f.buffer, encoding ));
 
1170
        }
 
1171
        bool SaveFile( const std::string& filename ) const              ///< STL std::string version.
 
1172
        {
 
1173
                StringToBuffer f( filename );
 
1174
                return ( f.buffer && SaveFile( f.buffer ));
 
1175
        }
 
1176
        #endif
 
1177
 
 
1178
        /** Parse the given null terminated block of xml data. Passing in an encoding to this
 
1179
                method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml
 
1180
                to use that encoding, regardless of what TinyXml might otherwise try to detect.
 
1181
        */
 
1182
        virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 
1183
 
 
1184
        /** Get the root element -- the only top level element -- of the document.
 
1185
                In well formed XML, there should only be one. TinyXml is tolerant of
 
1186
                multiple elements at the document level.
 
1187
        */
 
1188
        const TiXmlElement* RootElement() const         { return FirstChildElement(); }
 
1189
        TiXmlElement* RootElement()                                     { return FirstChildElement(); }
 
1190
 
 
1191
        /** If an error occurs, Error will be set to true. Also,
 
1192
                - The ErrorId() will contain the integer identifier of the error (not generally useful)
 
1193
                - The ErrorDesc() method will return the name of the error. (very useful)
 
1194
                - The ErrorRow() and ErrorCol() will return the location of the error (if known)
 
1195
        */
 
1196
        bool Error() const                                              { return error; }
 
1197
 
 
1198
        /// Contains a textual (english) description of the error if one occurs.
 
1199
        const char * ErrorDesc() const  { return errorDesc.c_str (); }
 
1200
 
 
1201
        /** Generally, you probably want the error string ( ErrorDesc() ). But if you
 
1202
                prefer the ErrorId, this function will fetch it.
 
1203
        */
 
1204
        const int ErrorId()     const                           { return errorId; }
 
1205
 
 
1206
        /** Returns the location (if known) of the error. The first column is column 1,
 
1207
                and the first row is row 1. A value of 0 means the row and column wasn't applicable
 
1208
                (memory errors, for example, have no row/column) or the parser lost the error. (An
 
1209
                error in the error reporting, in that case.)
 
1210
 
 
1211
                @sa SetTabSize, Row, Column
 
1212
        */
 
1213
        int ErrorRow()  { return errorLocation.row+1; }
 
1214
        int ErrorCol()  { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow()
 
1215
 
 
1216
        /** By calling this method, with a tab size
 
1217
                greater than 0, the row and column of each node and attribute is stored
 
1218
                when the file is loaded. Very useful for tracking the DOM back in to
 
1219
                the source file.
 
1220
 
 
1221
                The tab size is required for calculating the location of nodes. If not
 
1222
                set, the default of 4 is used. The tabsize is set per document. Setting
 
1223
                the tabsize to 0 disables row/column tracking.
 
1224
 
 
1225
                Note that row and column tracking is not supported when using operator>>.
 
1226
 
 
1227
                The tab size needs to be enabled before the parse or load. Correct usage:
 
1228
                @verbatim
 
1229
                TiXmlDocument doc;
 
1230
                doc.SetTabSize( 8 );
 
1231
                doc.Load( "myfile.xml" );
 
1232
                @endverbatim
 
1233
 
 
1234
                @sa Row, Column
 
1235
        */
 
1236
        void SetTabSize( int _tabsize )         { tabsize = _tabsize; }
 
1237
 
 
1238
        int TabSize() const     { return tabsize; }
 
1239
 
 
1240
        /** If you have handled the error, it can be reset with this call. The error
 
1241
                state is automatically cleared if you Parse a new XML block.
 
1242
        */
 
1243
        void ClearError()                                               {       error = false;
 
1244
                                                                                                errorId = 0;
 
1245
                                                                                                errorDesc = "";
 
1246
                                                                                                errorLocation.row = errorLocation.col = 0;
 
1247
                                                                                                //errorLocation.last = 0;
 
1248
                                                                                        }
 
1249
 
 
1250
        /** Dump the document to standard out. */
 
1251
        void Print() const                                              { Print( stdout, 0 ); }
 
1252
 
 
1253
        /// Print this Document to a FILE stream.
 
1254
        virtual void Print( FILE* cfile, int depth = 0 ) const;
 
1255
        // [internal use]
 
1256
        void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
 
1257
 
 
1258
protected :
 
1259
        virtual void StreamOut ( TIXML_OSTREAM * out) const;
 
1260
        // [internal use]
 
1261
        virtual TiXmlNode* Clone() const;
 
1262
        #ifdef TIXML_USE_STL
 
1263
            virtual void StreamIn( TIXML_ISTREAM * in, TIXML_STRING * tag );
 
1264
        #endif
 
1265
 
 
1266
private:
 
1267
        void CopyTo( TiXmlDocument* target ) const;
 
1268
 
 
1269
        bool error;
 
1270
        int  errorId;
 
1271
        TIXML_STRING errorDesc;
 
1272
        int tabsize;
 
1273
        TiXmlCursor errorLocation;
 
1274
};
 
1275
 
 
1276
 
 
1277
/**
 
1278
        A TiXmlHandle is a class that wraps a node pointer with null checks; this is
 
1279
        an incredibly useful thing. Note that TiXmlHandle is not part of the TinyXml
 
1280
        DOM structure. It is a separate utility class.
 
1281
 
 
1282
        Take an example:
 
1283
        @verbatim
 
1284
        <Document>
 
1285
                <Element attributeA = "valueA">
 
1286
                        <Child attributeB = "value1" />
 
1287
                        <Child attributeB = "value2" />
 
1288
                </Element>
 
1289
        <Document>
 
1290
        @endverbatim
 
1291
 
 
1292
        Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
 
1293
        easy to write a *lot* of code that looks like:
 
1294
 
 
1295
        @verbatim
 
1296
        TiXmlElement* root = document.FirstChildElement( "Document" );
 
1297
        if ( root )
 
1298
        {
 
1299
                TiXmlElement* element = root->FirstChildElement( "Element" );
 
1300
                if ( element )
 
1301
                {
 
1302
                        TiXmlElement* child = element->FirstChildElement( "Child" );
 
1303
                        if ( child )
 
1304
                        {
 
1305
                                TiXmlElement* child2 = child->NextSiblingElement( "Child" );
 
1306
                                if ( child2 )
 
1307
                                {
 
1308
                                        // Finally do something useful.
 
1309
        @endverbatim
 
1310
 
 
1311
        And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity
 
1312
        of such code. A TiXmlHandle checks for null     pointers so it is perfectly safe
 
1313
        and correct to use:
 
1314
 
 
1315
        @verbatim
 
1316
        TiXmlHandle docHandle( &document );
 
1317
        TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).Element();
 
1318
        if ( child2 )
 
1319
        {
 
1320
                // do something useful
 
1321
        @endverbatim
 
1322
 
 
1323
        Which is MUCH more concise and useful.
 
1324
 
 
1325
        It is also safe to copy handles - internally they are nothing more than node pointers.
 
1326
        @verbatim
 
1327
        TiXmlHandle handleCopy = handle;
 
1328
        @endverbatim
 
1329
 
 
1330
        What they should not be used for is iteration:
 
1331
 
 
1332
        @verbatim
 
1333
        int i=0;
 
1334
        while ( true )
 
1335
        {
 
1336
                TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).Element();
 
1337
                if ( !child )
 
1338
                        break;
 
1339
                // do something
 
1340
                ++i;
 
1341
        }
 
1342
        @endverbatim
 
1343
 
 
1344
        It seems reasonable, but it is in fact two embedded while loops. The Child method is
 
1345
        a linear walk to find the element, so this code would iterate much more than it needs
 
1346
        to. Instead, prefer:
 
1347
 
 
1348
        @verbatim
 
1349
        TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).Element();
 
1350
 
 
1351
        for( child; child; child=child->NextSiblingElement() )
 
1352
        {
 
1353
                // do something
 
1354
        }
 
1355
        @endverbatim
 
1356
*/
 
1357
class TiXmlHandle
 
1358
{
 
1359
public:
 
1360
        /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
 
1361
        TiXmlHandle( TiXmlNode* node )                                  { this->node = node; }
 
1362
        /// Copy constructor
 
1363
        TiXmlHandle( const TiXmlHandle& ref )                   { this->node = ref.node; }
 
1364
        TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
 
1365
 
 
1366
        /// Return a handle to the first child node.
 
1367
        TiXmlHandle FirstChild() const;
 
1368
        /// Return a handle to the first child node with the given name.
 
1369
        TiXmlHandle FirstChild( const char * value ) const;
 
1370
        /// Return a handle to the first child element.
 
1371
        TiXmlHandle FirstChildElement() const;
 
1372
        /// Return a handle to the first child element with the given name.
 
1373
        TiXmlHandle FirstChildElement( const char * value ) const;
 
1374
 
 
1375
        /** Return a handle to the "index" child with the given name.
 
1376
                The first child is 0, the second 1, etc.
 
1377
        */
 
1378
        TiXmlHandle Child( const char* value, int index ) const;
 
1379
        /** Return a handle to the "index" child.
 
1380
                The first child is 0, the second 1, etc.
 
1381
        */
 
1382
        TiXmlHandle Child( int index ) const;
 
1383
        /** Return a handle to the "index" child element with the given name.
 
1384
                The first child element is 0, the second 1, etc. Note that only TiXmlElements
 
1385
                are indexed: other types are not counted.
 
1386
        */
 
1387
        TiXmlHandle ChildElement( const char* value, int index ) const;
 
1388
        /** Return a handle to the "index" child element.
 
1389
                The first child element is 0, the second 1, etc. Note that only TiXmlElements
 
1390
                are indexed: other types are not counted.
 
1391
        */
 
1392
        TiXmlHandle ChildElement( int index ) const;
 
1393
 
 
1394
        #ifdef TIXML_USE_STL
 
1395
        TiXmlHandle FirstChild( const std::string& _value ) const                               { return FirstChild( _value.c_str() ); }
 
1396
        TiXmlHandle FirstChildElement( const std::string& _value ) const                { return FirstChildElement( _value.c_str() ); }
 
1397
 
 
1398
        TiXmlHandle Child( const std::string& _value, int index ) const                 { return Child( _value.c_str(), index ); }
 
1399
        TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
 
1400
        #endif
 
1401
 
 
1402
        /// Return the handle as a TiXmlNode. This may return null.
 
1403
        TiXmlNode* Node() const                 { return node; }
 
1404
        /// Return the handle as a TiXmlElement. This may return null.
 
1405
        TiXmlElement* Element() const   { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
 
1406
        /// Return the handle as a TiXmlText. This may return null.
 
1407
        TiXmlText* Text() const                 { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
 
1408
        /// Return the handle as a TiXmlUnknown. This may return null;
 
1409
        TiXmlUnknown* Unknown() const                   { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
 
1410
 
 
1411
private:
 
1412
        TiXmlNode* node;
 
1413
};
 
1414
 
 
1415
#ifdef _MSC_VER
 
1416
#pragma warning( default : 4530 )
 
1417
#pragma warning( default : 4786 )
 
1418
#endif
 
1419
 
 
1420
#endif
 
1421