~fluidity-core/fluidity/embedded_models

« back to all changes in this revision

Viewing changes to libvtkfortran/include/tinyxml.h

  • Committer: Timothy Bond
  • Date: 2011-04-14 15:40:24 UTC
  • Revision ID: timothy.bond@imperial.ac.uk-20110414154024-116ci9gq6mwigmaw
Following the move from svn to bzr we change the nature of inclusion of these
four software libraries. Previously, they were included as svn externals and
pulled at latest version for trunk, pinned to specific versions for release
and stable trunk. Since bzr is less elegant at dealing with externals we have
made the decision to include the packages directly into the trunk instead.

At this import the versions are:

libadaptivity: r163
libvtkfortran: r67
libspud: r545
libmba2d: r28

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-2006 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
 
 
26
#ifndef TINYXML_INCLUDED
 
27
#define TINYXML_INCLUDED
 
28
 
 
29
#define TIXML_USE_STL
 
30
 
 
31
#ifdef _MSC_VER
 
32
#pragma warning( push )
 
33
#pragma warning( disable : 4530 )
 
34
#pragma warning( disable : 4786 )
 
35
#endif
 
36
 
 
37
#include <ctype.h>
 
38
#include <stdio.h>
 
39
#include <stdlib.h>
 
40
#include <string.h>
 
41
#include <assert.h>
 
42
 
 
43
// Help out windows:
 
44
#if defined( _DEBUG ) && !defined( DEBUG )
 
45
#define DEBUG
 
46
#endif
 
47
 
 
48
#ifdef TIXML_USE_STL
 
49
        #include <string>
 
50
        #include <iostream>
 
51
        #include <sstream>
 
52
        #define TIXML_STRING            std::string
 
53
#else
 
54
        #include "tinystr.h"
 
55
        #define TIXML_STRING            TiXmlString
 
56
#endif
 
57
 
 
58
// Deprecated library function hell. Compilers want to use the
 
59
// new safe versions. This probably doesn't fully address the problem,
 
60
// but it gets closer. There are too many compilers for me to fully
 
61
// test. If you get compilation troubles, undefine TIXML_SAFE
 
62
#define TIXML_SAFE
 
63
 
 
64
#ifdef TIXML_SAFE
 
65
        #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
 
66
                // Microsoft visual studio, version 2005 and higher.
 
67
                #define TIXML_SNPRINTF _snprintf_s
 
68
                #define TIXML_SNSCANF  _snscanf_s
 
69
                #define TIXML_SSCANF   sscanf_s
 
70
        #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
 
71
                // Microsoft visual studio, version 6 and higher.
 
72
                //#pragma message( "Using _sn* functions." )
 
73
                #define TIXML_SNPRINTF _snprintf
 
74
                #define TIXML_SNSCANF  _snscanf
 
75
                #define TIXML_SSCANF   sscanf
 
76
        #elif defined(__GNUC__) && (__GNUC__ >= 3 )
 
77
                // GCC version 3 and higher.s
 
78
                //#warning( "Using sn* functions." )
 
79
                #define TIXML_SNPRINTF snprintf
 
80
                #define TIXML_SNSCANF  snscanf
 
81
                #define TIXML_SSCANF   sscanf
 
82
        #else
 
83
                #define TIXML_SSCANF   sscanf
 
84
        #endif
 
85
#endif  
 
86
 
 
87
class TiXmlDocument;
 
88
class TiXmlElement;
 
89
class TiXmlComment;
 
90
class TiXmlUnknown;
 
91
class TiXmlAttribute;
 
92
class TiXmlText;
 
93
class TiXmlDeclaration;
 
94
class TiXmlParsingData;
 
95
 
 
96
const int TIXML_MAJOR_VERSION = 2;
 
97
const int TIXML_MINOR_VERSION = 5;
 
98
const int TIXML_PATCH_VERSION = 3;
 
99
 
 
100
/*      Internal structure for tracking location of items 
 
101
        in the XML file.
 
102
*/
 
103
struct TiXmlCursor
 
104
{
 
105
        TiXmlCursor()           { Clear(); }
 
106
        void Clear()            { row = col = -1; }
 
107
 
 
108
        int row;        // 0 based.
 
109
        int col;        // 0 based.
 
110
};
 
111
 
 
112
 
 
113
/**
 
114
        If you call the Accept() method, it requires being passed a TiXmlVisitor
 
115
        class to handle callbacks. For nodes that contain other nodes (Document, Element)
 
116
        you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
 
117
        are simple called with Visit().
 
118
 
 
119
        If you return 'true' from a Visit method, recursive parsing will continue. If you return
 
120
        false, <b>no children of this node or its sibilings</b> will be Visited.
 
121
 
 
122
        All flavors of Visit methods have a default implementation that returns 'true' (continue 
 
123
        visiting). You need to only override methods that are interesting to you.
 
124
 
 
125
        Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
 
126
 
 
127
        You should never change the document from a callback.
 
128
 
 
129
        @sa TiXmlNode::Accept()
 
130
*/
 
131
class TiXmlVisitor
 
132
{
 
133
public:
 
134
        virtual ~TiXmlVisitor() {}
 
135
 
 
136
        /// Visit a document.
 
137
        virtual bool VisitEnter( const TiXmlDocument& /*doc*/ )                 { return true; }
 
138
        /// Visit a document.
 
139
        virtual bool VisitExit( const TiXmlDocument& /*doc*/ )                  { return true; }
 
140
 
 
141
        /// Visit an element.
 
142
        virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ )    { return true; }
 
143
        /// Visit an element.
 
144
        virtual bool VisitExit( const TiXmlElement& /*element*/ )               { return true; }
 
145
 
 
146
        /// Visit a declaration
 
147
        virtual bool Visit( const TiXmlDeclaration& /*declaration*/ )   { return true; }
 
148
        /// Visit a text node
 
149
        virtual bool Visit( const TiXmlText& /*text*/ )                                 { return true; }
 
150
        /// Visit a comment node
 
151
        virtual bool Visit( const TiXmlComment& /*comment*/ )                   { return true; }
 
152
        /// Visit an unknow node
 
153
        virtual bool Visit( const TiXmlUnknown& /*unknown*/ )                   { return true; }
 
154
};
 
155
 
 
156
// Only used by Attribute::Query functions
 
157
enum 
 
158
 
159
        TIXML_SUCCESS,
 
160
        TIXML_NO_ATTRIBUTE,
 
161
        TIXML_WRONG_TYPE
 
162
};
 
163
 
 
164
 
 
165
// Used by the parsing routines.
 
166
enum TiXmlEncoding
 
167
{
 
168
        TIXML_ENCODING_UNKNOWN,
 
169
        TIXML_ENCODING_UTF8,
 
170
        TIXML_ENCODING_LEGACY
 
171
};
 
172
 
 
173
const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
 
174
 
 
175
/** TiXmlBase is a base class for every class in TinyXml.
 
176
        It does little except to establish that TinyXml classes
 
177
        can be printed and provide some utility functions.
 
178
 
 
179
        In XML, the document and elements can contain
 
180
        other elements and other types of nodes.
 
181
 
 
182
        @verbatim
 
183
        A Document can contain: Element (container or leaf)
 
184
                                                        Comment (leaf)
 
185
                                                        Unknown (leaf)
 
186
                                                        Declaration( leaf )
 
187
 
 
188
        An Element can contain: Element (container or leaf)
 
189
                                                        Text    (leaf)
 
190
                                                        Attributes (not on tree)
 
191
                                                        Comment (leaf)
 
192
                                                        Unknown (leaf)
 
193
 
 
194
        A Decleration contains: Attributes (not on tree)
 
195
        @endverbatim
 
196
*/
 
197
class TiXmlBase
 
198
{
 
199
        friend class TiXmlNode;
 
200
        friend class TiXmlElement;
 
201
        friend class TiXmlDocument;
 
202
 
 
203
public:
 
204
        TiXmlBase()     :       userData(0)             {}
 
205
        virtual ~TiXmlBase()                    {}
 
206
 
 
207
        /**     All TinyXml classes can print themselves to a filestream
 
208
                or the string class (TiXmlString in non-STL mode, std::string
 
209
                in STL mode.) Either or both cfile and str can be null.
 
210
                
 
211
                This is a formatted print, and will insert 
 
212
                tabs and newlines.
 
213
                
 
214
                (For an unformatted stream, use the << operator.)
 
215
        */
 
216
        virtual void Print( FILE* cfile, int depth ) const = 0;
 
217
 
 
218
        /**     The world does not agree on whether white space should be kept or
 
219
                not. In order to make everyone happy, these global, static functions
 
220
                are provided to set whether or not TinyXml will condense all white space
 
221
                into a single space or not. The default is to condense. Note changing this
 
222
                value is not thread safe.
 
223
        */
 
224
        static void SetCondenseWhiteSpace( bool condense )              { condenseWhiteSpace = condense; }
 
225
 
 
226
        /// Return the current white space setting.
 
227
        static bool IsWhiteSpaceCondensed()                                             { return condenseWhiteSpace; }
 
228
 
 
229
        /** Return the position, in the original source file, of this node or attribute.
 
230
                The row and column are 1-based. (That is the first row and first column is
 
231
                1,1). If the returns values are 0 or less, then the parser does not have
 
232
                a row and column value.
 
233
 
 
234
                Generally, the row and column value will be set when the TiXmlDocument::Load(),
 
235
                TiXmlDocument::LoadFile(), or any TiXmlNode::Parse() is called. It will NOT be set
 
236
                when the DOM was created from operator>>.
 
237
 
 
238
                The values reflect the initial load. Once the DOM is modified programmatically
 
239
                (by adding or changing nodes and attributes) the new values will NOT update to
 
240
                reflect changes in the document.
 
241
 
 
242
                There is a minor performance cost to computing the row and column. Computation
 
243
                can be disabled if TiXmlDocument::SetTabSize() is called with 0 as the value.
 
244
 
 
245
                @sa TiXmlDocument::SetTabSize()
 
246
        */
 
247
        int Row() const                 { return location.row + 1; }
 
248
        int Column() const              { return location.col + 1; }    ///< See Row()
 
249
 
 
250
        void  SetUserData( void* user )                 { userData = user; }    ///< Set a pointer to arbitrary user data.
 
251
        void* GetUserData()                                             { return userData; }    ///< Get a pointer to arbitrary user data.
 
252
        const void* GetUserData() const                 { return userData; }    ///< Get a pointer to arbitrary user data.
 
253
 
 
254
        // Table that returs, for a given lead byte, the total number of bytes
 
255
        // in the UTF-8 sequence.
 
256
        static const int utf8ByteTable[256];
 
257
 
 
258
        virtual const char* Parse(      const char* p, 
 
259
                                                                TiXmlParsingData* data, 
 
260
                                                                TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
 
261
 
 
262
        /** Expands entities in a string. Note this should not contian the tag's '<', '>', etc, 
 
263
                or they will be transformed into entities!
 
264
        */
 
265
        static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out );
 
266
 
 
267
        enum
 
268
        {
 
269
                TIXML_NO_ERROR = 0,
 
270
                TIXML_ERROR,
 
271
                TIXML_ERROR_OPENING_FILE,
 
272
                TIXML_ERROR_OUT_OF_MEMORY,
 
273
                TIXML_ERROR_PARSING_ELEMENT,
 
274
                TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
 
275
                TIXML_ERROR_READING_ELEMENT_VALUE,
 
276
                TIXML_ERROR_READING_ATTRIBUTES,
 
277
                TIXML_ERROR_PARSING_EMPTY,
 
278
                TIXML_ERROR_READING_END_TAG,
 
279
                TIXML_ERROR_PARSING_UNKNOWN,
 
280
                TIXML_ERROR_PARSING_COMMENT,
 
281
                TIXML_ERROR_PARSING_DECLARATION,
 
282
                TIXML_ERROR_DOCUMENT_EMPTY,
 
283
                TIXML_ERROR_EMBEDDED_NULL,
 
284
                TIXML_ERROR_PARSING_CDATA,
 
285
                TIXML_ERROR_DOCUMENT_TOP_ONLY,
 
286
 
 
287
                TIXML_ERROR_STRING_COUNT
 
288
        };
 
289
 
 
290
protected:
 
291
 
 
292
        static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
 
293
        inline static bool IsWhiteSpace( char c )               
 
294
        { 
 
295
                return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' ); 
 
296
        }
 
297
        inline static bool IsWhiteSpace( int c )
 
298
        {
 
299
                if ( c < 256 )
 
300
                        return IsWhiteSpace( (char) c );
 
301
                return false;   // Again, only truly correct for English/Latin...but usually works.
 
302
        }
 
303
 
 
304
        #ifdef TIXML_USE_STL
 
305
        static bool     StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
 
306
        static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
 
307
        #endif
 
308
 
 
309
        /*      Reads an XML name into the string provided. Returns
 
310
                a pointer just past the last character of the name,
 
311
                or 0 if the function has an error.
 
312
        */
 
313
        static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
 
314
 
 
315
        /*      Reads text. Returns a pointer past the given end tag.
 
316
                Wickedly complex options, but it keeps the (sensitive) code in one place.
 
317
        */
 
318
        static const char* ReadText(    const char* in,                         // where to start
 
319
                                                                        TIXML_STRING* text,                     // the string read
 
320
                                                                        bool ignoreWhiteSpace,          // whether to keep the white space
 
321
                                                                        const char* endTag,                     // what ends this text
 
322
                                                                        bool ignoreCase,                        // whether to ignore case in the end tag
 
323
                                                                        TiXmlEncoding encoding );       // the current encoding
 
324
 
 
325
        // If an entity has been found, transform it into a character.
 
326
        static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
 
327
 
 
328
        // Get a character, while interpreting entities.
 
329
        // The length can be from 0 to 4 bytes.
 
330
        inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
 
331
        {
 
332
                assert( p );
 
333
                if ( encoding == TIXML_ENCODING_UTF8 )
 
334
                {
 
335
                        *length = utf8ByteTable[ *((const unsigned char*)p) ];
 
336
                        assert( *length >= 0 && *length < 5 );
 
337
                }
 
338
                else
 
339
                {
 
340
                        *length = 1;
 
341
                }
 
342
 
 
343
                if ( *length == 1 )
 
344
                {
 
345
                        if ( *p == '&' )
 
346
                                return GetEntity( p, _value, length, encoding );
 
347
                        *_value = *p;
 
348
                        return p+1;
 
349
                }
 
350
                else if ( *length )
 
351
                {
 
352
                        //strncpy( _value, p, *length );        // lots of compilers don't like this function (unsafe),
 
353
                                                                                                // and the null terminator isn't needed
 
354
                        for( int i=0; p[i] && i<*length; ++i ) {
 
355
                                _value[i] = p[i];
 
356
                        }
 
357
                        return p + (*length);
 
358
                }
 
359
                else
 
360
                {
 
361
                        // Not valid text.
 
362
                        return 0;
 
363
                }
 
364
        }
 
365
 
 
366
        // Return true if the next characters in the stream are any of the endTag sequences.
 
367
        // Ignore case only works for english, and should only be relied on when comparing
 
368
        // to English words: StringEqual( p, "version", true ) is fine.
 
369
        static bool StringEqual(        const char* p,
 
370
                                                                const char* endTag,
 
371
                                                                bool ignoreCase,
 
372
                                                                TiXmlEncoding encoding );
 
373
 
 
374
        static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
 
375
 
 
376
        TiXmlCursor location;
 
377
 
 
378
    /// Field containing a generic user pointer
 
379
        void*                   userData;
 
380
        
 
381
        // None of these methods are reliable for any language except English.
 
382
        // Good for approximation, not great for accuracy.
 
383
        static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
 
384
        static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
 
385
        inline static int ToLower( int v, TiXmlEncoding encoding )
 
386
        {
 
387
                if ( encoding == TIXML_ENCODING_UTF8 )
 
388
                {
 
389
                        if ( v < 128 ) return tolower( v );
 
390
                        return v;
 
391
                }
 
392
                else
 
393
                {
 
394
                        return tolower( v );
 
395
                }
 
396
        }
 
397
        static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
 
398
 
 
399
private:
 
400
        TiXmlBase( const TiXmlBase& );                          // not implemented.
 
401
        void operator=( const TiXmlBase& base );        // not allowed.
 
402
 
 
403
        struct Entity
 
404
        {
 
405
                const char*     str;
 
406
                unsigned int    strLength;
 
407
                char                chr;
 
408
        };
 
409
        enum
 
410
        {
 
411
                NUM_ENTITY = 5,
 
412
                MAX_ENTITY_LENGTH = 6
 
413
 
 
414
        };
 
415
        static Entity entity[ NUM_ENTITY ];
 
416
        static bool condenseWhiteSpace;
 
417
};
 
418
 
 
419
 
 
420
/** The parent class for everything in the Document Object Model.
 
421
        (Except for attributes).
 
422
        Nodes have siblings, a parent, and children. A node can be
 
423
        in a document, or stand on its own. The type of a TiXmlNode
 
424
        can be queried, and it can be cast to its more defined type.
 
425
*/
 
426
class TiXmlNode : public TiXmlBase
 
427
{
 
428
        friend class TiXmlDocument;
 
429
        friend class TiXmlElement;
 
430
 
 
431
public:
 
432
        #ifdef TIXML_USE_STL    
 
433
 
 
434
            /** An input stream operator, for every class. Tolerant of newlines and
 
435
                    formatting, but doesn't expect them.
 
436
            */
 
437
            friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
 
438
 
 
439
            /** An output stream operator, for every class. Note that this outputs
 
440
                    without any newlines or formatting, as opposed to Print(), which
 
441
                    includes tabs and new lines.
 
442
 
 
443
                    The operator<< and operator>> are not completely symmetric. Writing
 
444
                    a node to a stream is very well defined. You'll get a nice stream
 
445
                    of output, without any extra whitespace or newlines.
 
446
                    
 
447
                    But reading is not as well defined. (As it always is.) If you create
 
448
                    a TiXmlElement (for example) and read that from an input stream,
 
449
                    the text needs to define an element or junk will result. This is
 
450
                    true of all input streams, but it's worth keeping in mind.
 
451
 
 
452
                    A TiXmlDocument will read nodes until it reads a root element, and
 
453
                        all the children of that root element.
 
454
            */  
 
455
            friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
 
456
 
 
457
                /// Appends the XML node or attribute to a std::string.
 
458
                friend std::string& operator<< (std::string& out, const TiXmlNode& base );
 
459
 
 
460
        #endif
 
461
 
 
462
        /** The types of XML nodes supported by TinyXml. (All the
 
463
                        unsupported types are picked up by UNKNOWN.)
 
464
        */
 
465
        enum NodeType
 
466
        {
 
467
                DOCUMENT,
 
468
                ELEMENT,
 
469
                COMMENT,
 
470
                UNKNOWN,
 
471
                TEXT,
 
472
                DECLARATION,
 
473
                TYPECOUNT
 
474
        };
 
475
 
 
476
        virtual ~TiXmlNode();
 
477
 
 
478
        /** The meaning of 'value' changes for the specific type of
 
479
                TiXmlNode.
 
480
                @verbatim
 
481
                Document:       filename of the xml file
 
482
                Element:        name of the element
 
483
                Comment:        the comment text
 
484
                Unknown:        the tag contents
 
485
                Text:           the text string
 
486
                @endverbatim
 
487
 
 
488
                The subclasses will wrap this function.
 
489
        */
 
490
        const char *Value() const { return value.c_str (); }
 
491
 
 
492
    #ifdef TIXML_USE_STL
 
493
        /** Return Value() as a std::string. If you only use STL,
 
494
            this is more efficient than calling Value().
 
495
                Only available in STL mode.
 
496
        */
 
497
        const std::string& ValueStr() const { return value; }
 
498
        #endif
 
499
 
 
500
        const TIXML_STRING& ValueTStr() const { return value; }
 
501
 
 
502
        /** Changes the value of the node. Defined as:
 
503
                @verbatim
 
504
                Document:       filename of the xml file
 
505
                Element:        name of the element
 
506
                Comment:        the comment text
 
507
                Unknown:        the tag contents
 
508
                Text:           the text string
 
509
                @endverbatim
 
510
        */
 
511
        void SetValue(const char * _value) { value = _value;}
 
512
 
 
513
    #ifdef TIXML_USE_STL
 
514
        /// STL std::string form.
 
515
        void SetValue( const std::string& _value )      { value = _value; }
 
516
        #endif
 
517
 
 
518
        /// Delete all the children of this node. Does not affect 'this'.
 
519
        void Clear();
 
520
 
 
521
        /// One step up the DOM.
 
522
        TiXmlNode* Parent()                                                     { return parent; }
 
523
        const TiXmlNode* Parent() const                         { return parent; }
 
524
 
 
525
        const TiXmlNode* FirstChild()   const           { return firstChild; }  ///< The first child of this node. Will be null if there are no children.
 
526
        TiXmlNode* FirstChild()                                         { return firstChild; }
 
527
        const TiXmlNode* FirstChild( const char * value ) const;                        ///< The first child of this node with the matching 'value'. Will be null if none found.
 
528
        /// The first child of this node with the matching 'value'. Will be null if none found.
 
529
        TiXmlNode* FirstChild( const char * _value ) {
 
530
                // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
 
531
                // call the method, cast the return back to non-const.
 
532
                return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
 
533
        }
 
534
        const TiXmlNode* LastChild() const      { return lastChild; }           /// The last child of this node. Will be null if there are no children.
 
535
        TiXmlNode* LastChild()  { return lastChild; }
 
536
        
 
537
        const TiXmlNode* LastChild( const char * value ) const;                 /// The last child of this node matching 'value'. Will be null if there are no children.
 
538
        TiXmlNode* LastChild( const char * _value ) {
 
539
                return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
 
540
        }
 
541
 
 
542
    #ifdef TIXML_USE_STL
 
543
        const TiXmlNode* FirstChild( const std::string& _value ) const  {       return FirstChild (_value.c_str ());    }       ///< STL std::string form.
 
544
        TiXmlNode* FirstChild( const std::string& _value )                              {       return FirstChild (_value.c_str ());    }       ///< STL std::string form.
 
545
        const TiXmlNode* LastChild( const std::string& _value ) const   {       return LastChild (_value.c_str ());     }       ///< STL std::string form.
 
546
        TiXmlNode* LastChild( const std::string& _value )                               {       return LastChild (_value.c_str ());     }       ///< STL std::string form.
 
547
        #endif
 
548
 
 
549
        /** An alternate way to walk the children of a node.
 
550
                One way to iterate over nodes is:
 
551
                @verbatim
 
552
                        for( child = parent->FirstChild(); child; child = child->NextSibling() )
 
553
                @endverbatim
 
554
 
 
555
                IterateChildren does the same thing with the syntax:
 
556
                @verbatim
 
557
                        child = 0;
 
558
                        while( child = parent->IterateChildren( child ) )
 
559
                @endverbatim
 
560
 
 
561
                IterateChildren takes the previous child as input and finds
 
562
                the next one. If the previous child is null, it returns the
 
563
                first. IterateChildren will return null when done.
 
564
        */
 
565
        const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
 
566
        TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
 
567
                return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
 
568
        }
 
569
 
 
570
        /// This flavor of IterateChildren searches for children with a particular 'value'
 
571
        const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
 
572
        TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
 
573
                return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
 
574
        }
 
575
 
 
576
    #ifdef TIXML_USE_STL
 
577
        const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const  {       return IterateChildren (_value.c_str (), previous);     }       ///< STL std::string form.
 
578
        TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) {    return IterateChildren (_value.c_str (), previous);     }       ///< STL std::string form.
 
579
        #endif
 
580
 
 
581
        /** Add a new node related to this. Adds a child past the LastChild.
 
582
                Returns a pointer to the new object or NULL if an error occured.
 
583
        */
 
584
        TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
 
585
 
 
586
 
 
587
        /** Add a new node related to this. Adds a child past the LastChild.
 
588
 
 
589
                NOTE: the node to be added is passed by pointer, and will be
 
590
                henceforth owned (and deleted) by tinyXml. This method is efficient
 
591
                and avoids an extra copy, but should be used with care as it
 
592
                uses a different memory model than the other insert functions.
 
593
 
 
594
                @sa InsertEndChild
 
595
        */
 
596
        TiXmlNode* LinkEndChild( TiXmlNode* addThis );
 
597
 
 
598
        /** Add a new node related to this. Adds a child before the specified child.
 
599
                Returns a pointer to the new object or NULL if an error occured.
 
600
        */
 
601
        TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
 
602
 
 
603
        /** Add a new node related to this. Adds a child after the specified child.
 
604
                Returns a pointer to the new object or NULL if an error occured.
 
605
        */
 
606
        TiXmlNode* InsertAfterChild(  TiXmlNode* afterThis, const TiXmlNode& addThis );
 
607
 
 
608
        /** Replace a child of this node.
 
609
                Returns a pointer to the new object or NULL if an error occured.
 
610
        */
 
611
        TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
 
612
 
 
613
        /// Delete a child of this node.
 
614
        bool RemoveChild( TiXmlNode* removeThis );
 
615
 
 
616
        /// Navigate to a sibling node.
 
617
        const TiXmlNode* PreviousSibling() const                        { return prev; }
 
618
        TiXmlNode* PreviousSibling()                                            { return prev; }
 
619
 
 
620
        /// Navigate to a sibling node.
 
621
        const TiXmlNode* PreviousSibling( const char * ) const;
 
622
        TiXmlNode* PreviousSibling( const char *_prev ) {
 
623
                return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
 
624
        }
 
625
 
 
626
    #ifdef TIXML_USE_STL
 
627
        const TiXmlNode* PreviousSibling( const std::string& _value ) const     {       return PreviousSibling (_value.c_str ());       }       ///< STL std::string form.
 
628
        TiXmlNode* PreviousSibling( const std::string& _value )                         {       return PreviousSibling (_value.c_str ());       }       ///< STL std::string form.
 
629
        const TiXmlNode* NextSibling( const std::string& _value) const          {       return NextSibling (_value.c_str ());   }       ///< STL std::string form.
 
630
        TiXmlNode* NextSibling( const std::string& _value)                                      {       return NextSibling (_value.c_str ());   }       ///< STL std::string form.
 
631
        #endif
 
632
 
 
633
        /// Navigate to a sibling node.
 
634
        const TiXmlNode* NextSibling() const                            { return next; }
 
635
        TiXmlNode* NextSibling()                                                        { return next; }
 
636
 
 
637
        /// Navigate to a sibling node with the given 'value'.
 
638
        const TiXmlNode* NextSibling( const char * ) const;
 
639
        TiXmlNode* NextSibling( const char* _next ) {
 
640
                return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
 
641
        }
 
642
 
 
643
        /** Convenience function to get through elements.
 
644
                Calls NextSibling and ToElement. Will skip all non-Element
 
645
                nodes. Returns 0 if there is not another element.
 
646
        */
 
647
        const TiXmlElement* NextSiblingElement() const;
 
648
        TiXmlElement* NextSiblingElement() {
 
649
                return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
 
650
        }
 
651
 
 
652
        /** Convenience function to get through elements.
 
653
                Calls NextSibling and ToElement. Will skip all non-Element
 
654
                nodes. Returns 0 if there is not another element.
 
655
        */
 
656
        const TiXmlElement* NextSiblingElement( const char * ) const;
 
657
        TiXmlElement* NextSiblingElement( const char *_next ) {
 
658
                return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
 
659
        }
 
660
 
 
661
    #ifdef TIXML_USE_STL
 
662
        const TiXmlElement* NextSiblingElement( const std::string& _value) const        {       return NextSiblingElement (_value.c_str ());    }       ///< STL std::string form.
 
663
        TiXmlElement* NextSiblingElement( const std::string& _value)                            {       return NextSiblingElement (_value.c_str ());    }       ///< STL std::string form.
 
664
        #endif
 
665
 
 
666
        /// Convenience function to get through elements.
 
667
        const TiXmlElement* FirstChildElement() const;
 
668
        TiXmlElement* FirstChildElement() {
 
669
                return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
 
670
        }
 
671
 
 
672
        /// Convenience function to get through elements.
 
673
        const TiXmlElement* FirstChildElement( const char * _value ) const;
 
674
        TiXmlElement* FirstChildElement( const char * _value ) {
 
675
                return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
 
676
        }
 
677
 
 
678
    #ifdef TIXML_USE_STL
 
679
        const TiXmlElement* FirstChildElement( const std::string& _value ) const        {       return FirstChildElement (_value.c_str ());     }       ///< STL std::string form.
 
680
        TiXmlElement* FirstChildElement( const std::string& _value )                            {       return FirstChildElement (_value.c_str ());     }       ///< STL std::string form.
 
681
        #endif
 
682
 
 
683
        /** Query the type (as an enumerated value, above) of this node.
 
684
                The possible types are: DOCUMENT, ELEMENT, COMMENT,
 
685
                                                                UNKNOWN, TEXT, and DECLARATION.
 
686
        */
 
687
        int Type() const        { return type; }
 
688
 
 
689
        /** Return a pointer to the Document this node lives in.
 
690
                Returns null if not in a document.
 
691
        */
 
692
        const TiXmlDocument* GetDocument() const;
 
693
        TiXmlDocument* GetDocument() {
 
694
                return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
 
695
        }
 
696
 
 
697
        /// Returns true if this node has no children.
 
698
        bool NoChildren() const                                         { return !firstChild; }
 
699
 
 
700
        virtual const TiXmlDocument*    ToDocument()    const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
701
        virtual const TiXmlElement*     ToElement()     const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
702
        virtual const TiXmlComment*     ToComment()     const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
703
        virtual const TiXmlUnknown*     ToUnknown()     const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
704
        virtual const TiXmlText*        ToText()        const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
705
        virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
706
 
 
707
        virtual TiXmlDocument*          ToDocument()    { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
708
        virtual TiXmlElement*           ToElement()         { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
709
        virtual TiXmlComment*           ToComment()     { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
710
        virtual TiXmlUnknown*           ToUnknown()         { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
711
        virtual TiXmlText*                  ToText()        { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
712
        virtual TiXmlDeclaration*       ToDeclaration() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
 
713
 
 
714
        /** Create an exact duplicate of this node and return it. The memory must be deleted
 
715
                by the caller. 
 
716
        */
 
717
        virtual TiXmlNode* Clone() const = 0;
 
718
 
 
719
        /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the 
 
720
                XML tree will be conditionally visited and the host will be called back
 
721
                via the TiXmlVisitor interface.
 
722
 
 
723
                This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
 
724
                the XML for the callbacks, so the performance of TinyXML is unchanged by using this
 
725
                interface versus any other.)
 
726
 
 
727
                The interface has been based on ideas from:
 
728
 
 
729
                - http://www.saxproject.org/
 
730
                - http://c2.com/cgi/wiki?HierarchicalVisitorPattern 
 
731
 
 
732
                Which are both good references for "visiting".
 
733
 
 
734
                An example of using Accept():
 
735
                @verbatim
 
736
                TiXmlPrinter printer;
 
737
                tinyxmlDoc.Accept( &printer );
 
738
                const char* xmlcstr = printer.CStr();
 
739
                @endverbatim
 
740
        */
 
741
        virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
 
742
 
 
743
protected:
 
744
        TiXmlNode( NodeType _type );
 
745
 
 
746
        // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
 
747
        // and the assignment operator.
 
748
        void CopyTo( TiXmlNode* target ) const;
 
749
 
 
750
        #ifdef TIXML_USE_STL
 
751
            // The real work of the input operator.
 
752
        virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
 
753
        #endif
 
754
 
 
755
        // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
 
756
        TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
 
757
 
 
758
        TiXmlNode*              parent;
 
759
        NodeType                type;
 
760
 
 
761
        TiXmlNode*              firstChild;
 
762
        TiXmlNode*              lastChild;
 
763
 
 
764
        TIXML_STRING    value;
 
765
 
 
766
        TiXmlNode*              prev;
 
767
        TiXmlNode*              next;
 
768
 
 
769
private:
 
770
        TiXmlNode( const TiXmlNode& );                          // not implemented.
 
771
        void operator=( const TiXmlNode& base );        // not allowed.
 
772
};
 
773
 
 
774
 
 
775
/** An attribute is a name-value pair. Elements have an arbitrary
 
776
        number of attributes, each with a unique name.
 
777
 
 
778
        @note The attributes are not TiXmlNodes, since they are not
 
779
                  part of the tinyXML document object model. There are other
 
780
                  suggested ways to look at this problem.
 
781
*/
 
782
class TiXmlAttribute : public TiXmlBase
 
783
{
 
784
        friend class TiXmlAttributeSet;
 
785
 
 
786
public:
 
787
        /// Construct an empty attribute.
 
788
        TiXmlAttribute() : TiXmlBase()
 
789
        {
 
790
                document = 0;
 
791
                prev = next = 0;
 
792
        }
 
793
 
 
794
        #ifdef TIXML_USE_STL
 
795
        /// std::string constructor.
 
796
        TiXmlAttribute( const std::string& _name, const std::string& _value )
 
797
        {
 
798
                name = _name;
 
799
                value = _value;
 
800
                document = 0;
 
801
                prev = next = 0;
 
802
        }
 
803
        #endif
 
804
 
 
805
        /// Construct an attribute with a name and value.
 
806
        TiXmlAttribute( const char * _name, const char * _value )
 
807
        {
 
808
                name = _name;
 
809
                value = _value;
 
810
                document = 0;
 
811
                prev = next = 0;
 
812
        }
 
813
 
 
814
        const char*             Name()  const           { return name.c_str(); }                ///< Return the name of this attribute.
 
815
        const char*             Value() const           { return value.c_str(); }               ///< Return the value of this attribute.
 
816
        #ifdef TIXML_USE_STL
 
817
        const std::string& ValueStr() const     { return value; }                               ///< Return the value of this attribute.
 
818
        #endif
 
819
        int                             IntValue() const;                                                                       ///< Return the value of this attribute, converted to an integer.
 
820
        double                  DoubleValue() const;                                                            ///< Return the value of this attribute, converted to a double.
 
821
 
 
822
        // Get the tinyxml string representation
 
823
        const TIXML_STRING& NameTStr() const { return name; }
 
824
 
 
825
        /** QueryIntValue examines the value string. It is an alternative to the
 
826
                IntValue() method with richer error checking.
 
827
                If the value is an integer, it is stored in 'value' and 
 
828
                the call returns TIXML_SUCCESS. If it is not
 
829
                an integer, it returns TIXML_WRONG_TYPE.
 
830
 
 
831
                A specialized but useful call. Note that for success it returns 0,
 
832
                which is the opposite of almost all other TinyXml calls.
 
833
        */
 
834
        int QueryIntValue( int* _value ) const;
 
835
        /// QueryDoubleValue examines the value string. See QueryIntValue().
 
836
        int QueryDoubleValue( double* _value ) const;
 
837
 
 
838
        void SetName( const char* _name )       { name = _name; }                               ///< Set the name of this attribute.
 
839
        void SetValue( const char* _value )     { value = _value; }                             ///< Set the value.
 
840
 
 
841
        void SetIntValue( int _value );                                                                         ///< Set the value from an integer.
 
842
        void SetDoubleValue( double _value );                                                           ///< Set the value from a double.
 
843
 
 
844
    #ifdef TIXML_USE_STL
 
845
        /// STL std::string form.
 
846
        void SetName( const std::string& _name )        { name = _name; }       
 
847
        /// STL std::string form.       
 
848
        void SetValue( const std::string& _value )      { value = _value; }
 
849
        #endif
 
850
 
 
851
        /// Get the next sibling attribute in the DOM. Returns null at end.
 
852
        const TiXmlAttribute* Next() const;
 
853
        TiXmlAttribute* Next() {
 
854
                return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() ); 
 
855
        }
 
856
 
 
857
        /// Get the previous sibling attribute in the DOM. Returns null at beginning.
 
858
        const TiXmlAttribute* Previous() const;
 
859
        TiXmlAttribute* Previous() {
 
860
                return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() ); 
 
861
        }
 
862
 
 
863
        bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
 
864
        bool operator<( const TiXmlAttribute& rhs )      const { return name < rhs.name; }
 
865
        bool operator>( const TiXmlAttribute& rhs )  const { return name > rhs.name; }
 
866
 
 
867
        /*      Attribute parsing starts: first letter of the name
 
868
                                                 returns: the next char after the value end quote
 
869
        */
 
870
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
871
 
 
872
        // Prints this Attribute to a FILE stream.
 
873
        virtual void Print( FILE* cfile, int depth ) const {
 
874
                Print( cfile, depth, 0 );
 
875
        }
 
876
        void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
 
877
 
 
878
        // [internal use]
 
879
        // Set the document pointer so the attribute can report errors.
 
880
        void SetDocument( TiXmlDocument* doc )  { document = doc; }
 
881
 
 
882
private:
 
883
        TiXmlAttribute( const TiXmlAttribute& );                                // not implemented.
 
884
        void operator=( const TiXmlAttribute& base );   // not allowed.
 
885
 
 
886
        TiXmlDocument*  document;       // A pointer back to a document, for error reporting.
 
887
        TIXML_STRING name;
 
888
        TIXML_STRING value;
 
889
        TiXmlAttribute* prev;
 
890
        TiXmlAttribute* next;
 
891
};
 
892
 
 
893
 
 
894
/*      A class used to manage a group of attributes.
 
895
        It is only used internally, both by the ELEMENT and the DECLARATION.
 
896
        
 
897
        The set can be changed transparent to the Element and Declaration
 
898
        classes that use it, but NOT transparent to the Attribute
 
899
        which has to implement a next() and previous() method. Which makes
 
900
        it a bit problematic and prevents the use of STL.
 
901
 
 
902
        This version is implemented with circular lists because:
 
903
                - I like circular lists
 
904
                - it demonstrates some independence from the (typical) doubly linked list.
 
905
*/
 
906
class TiXmlAttributeSet
 
907
{
 
908
public:
 
909
        TiXmlAttributeSet();
 
910
        ~TiXmlAttributeSet();
 
911
 
 
912
        void Add( TiXmlAttribute* attribute );
 
913
        void Remove( TiXmlAttribute* attribute );
 
914
 
 
915
        const TiXmlAttribute* First()   const   { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
 
916
        TiXmlAttribute* First()                                 { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
 
917
        const TiXmlAttribute* Last() const              { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
 
918
        TiXmlAttribute* Last()                                  { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
 
919
 
 
920
        const TiXmlAttribute*   Find( const char* _name ) const;
 
921
        TiXmlAttribute* Find( const char* _name ) {
 
922
                return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
 
923
        }
 
924
        #ifdef TIXML_USE_STL
 
925
        const TiXmlAttribute*   Find( const std::string& _name ) const;
 
926
        TiXmlAttribute* Find( const std::string& _name ) {
 
927
                return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
 
928
        }
 
929
 
 
930
        #endif
 
931
 
 
932
private:
 
933
        //*ME:  Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
 
934
        //*ME:  this class must be also use a hidden/disabled copy-constructor !!!
 
935
        TiXmlAttributeSet( const TiXmlAttributeSet& );  // not allowed
 
936
        void operator=( const TiXmlAttributeSet& );     // not allowed (as TiXmlAttribute)
 
937
 
 
938
        TiXmlAttribute sentinel;
 
939
};
 
940
 
 
941
 
 
942
/** The element is a container class. It has a value, the element name,
 
943
        and can contain other elements, text, comments, and unknowns.
 
944
        Elements also contain an arbitrary number of attributes.
 
945
*/
 
946
class TiXmlElement : public TiXmlNode
 
947
{
 
948
public:
 
949
        /// Construct an element.
 
950
        TiXmlElement (const char * in_value);
 
951
 
 
952
        #ifdef TIXML_USE_STL
 
953
        /// std::string constructor.
 
954
        TiXmlElement( const std::string& _value );
 
955
        #endif
 
956
 
 
957
        TiXmlElement( const TiXmlElement& );
 
958
 
 
959
        void operator=( const TiXmlElement& base );
 
960
 
 
961
        virtual ~TiXmlElement();
 
962
 
 
963
        /** Given an attribute name, Attribute() returns the value
 
964
                for the attribute of that name, or null if none exists.
 
965
        */
 
966
        const char* Attribute( const char* name ) const;
 
967
 
 
968
        /** Given an attribute name, Attribute() returns the value
 
969
                for the attribute of that name, or null if none exists.
 
970
                If the attribute exists and can be converted to an integer,
 
971
                the integer value will be put in the return 'i', if 'i'
 
972
                is non-null.
 
973
        */
 
974
        const char* Attribute( const char* name, int* i ) const;
 
975
 
 
976
        /** Given an attribute name, Attribute() returns the value
 
977
                for the attribute of that name, or null if none exists.
 
978
                If the attribute exists and can be converted to an double,
 
979
                the double value will be put in the return 'd', if 'd'
 
980
                is non-null.
 
981
        */
 
982
        const char* Attribute( const char* name, double* d ) const;
 
983
 
 
984
        /** QueryIntAttribute examines the attribute - it is an alternative to the
 
985
                Attribute() method with richer error checking.
 
986
                If the attribute is an integer, it is stored in 'value' and 
 
987
                the call returns TIXML_SUCCESS. If it is not
 
988
                an integer, it returns TIXML_WRONG_TYPE. If the attribute
 
989
                does not exist, then TIXML_NO_ATTRIBUTE is returned.
 
990
        */      
 
991
        int QueryIntAttribute( const char* name, int* _value ) const;
 
992
        /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
 
993
        int QueryDoubleAttribute( const char* name, double* _value ) const;
 
994
        /// QueryFloatAttribute examines the attribute - see QueryIntAttribute().
 
995
        int QueryFloatAttribute( const char* name, float* _value ) const {
 
996
                double d;
 
997
                int result = QueryDoubleAttribute( name, &d );
 
998
                if ( result == TIXML_SUCCESS ) {
 
999
                        *_value = (float)d;
 
1000
                }
 
1001
                return result;
 
1002
        }
 
1003
 
 
1004
    #ifdef TIXML_USE_STL
 
1005
        /** Template form of the attribute query which will try to read the
 
1006
                attribute into the specified type. Very easy, very powerful, but
 
1007
                be careful to make sure to call this with the correct type.
 
1008
                
 
1009
                NOTE: This method doesn't work correctly for 'string' types.
 
1010
 
 
1011
                @return TIXML_SUCCESS, TIXML_WRONG_TYPE, or TIXML_NO_ATTRIBUTE
 
1012
        */
 
1013
        template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
 
1014
        {
 
1015
                const TiXmlAttribute* node = attributeSet.Find( name );
 
1016
                if ( !node )
 
1017
                        return TIXML_NO_ATTRIBUTE;
 
1018
 
 
1019
                std::stringstream sstream( node->ValueStr() );
 
1020
                sstream >> *outValue;
 
1021
                if ( !sstream.fail() )
 
1022
                        return TIXML_SUCCESS;
 
1023
                return TIXML_WRONG_TYPE;
 
1024
        }
 
1025
        /*
 
1026
         This is - in theory - a bug fix for "QueryValueAtribute returns truncated std::string"
 
1027
         but template specialization is hard to get working cross-compiler. Leaving the bug for now.
 
1028
         
 
1029
        // The above will fail for std::string because the space character is used as a seperator.
 
1030
        // Specialize for strings. Bug [ 1695429 ] QueryValueAtribute returns truncated std::string
 
1031
        template<> int QueryValueAttribute( const std::string& name, std::string* outValue ) const
 
1032
        {
 
1033
                const TiXmlAttribute* node = attributeSet.Find( name );
 
1034
                if ( !node )
 
1035
                        return TIXML_NO_ATTRIBUTE;
 
1036
                *outValue = node->ValueStr();
 
1037
                return TIXML_SUCCESS;
 
1038
        }
 
1039
        */
 
1040
        #endif
 
1041
 
 
1042
        /** Sets an attribute of name to a given value. The attribute
 
1043
                will be created if it does not exist, or changed if it does.
 
1044
        */
 
1045
        void SetAttribute( const char* name, const char * _value );
 
1046
 
 
1047
    #ifdef TIXML_USE_STL
 
1048
        const std::string* Attribute( const std::string& name ) const;
 
1049
        const std::string* Attribute( const std::string& name, int* i ) const;
 
1050
        const std::string* Attribute( const std::string& name, double* d ) const;
 
1051
        int QueryIntAttribute( const std::string& name, int* _value ) const;
 
1052
        int QueryDoubleAttribute( const std::string& name, double* _value ) const;
 
1053
 
 
1054
        /// STL std::string form.
 
1055
        void SetAttribute( const std::string& name, const std::string& _value );
 
1056
        ///< STL std::string form.
 
1057
        void SetAttribute( const std::string& name, int _value );
 
1058
        #endif
 
1059
 
 
1060
        /** Sets an attribute of name to a given value. The attribute
 
1061
                will be created if it does not exist, or changed if it does.
 
1062
        */
 
1063
        void SetAttribute( const char * name, int value );
 
1064
 
 
1065
        /** Sets an attribute of name to a given value. The attribute
 
1066
                will be created if it does not exist, or changed if it does.
 
1067
        */
 
1068
        void SetDoubleAttribute( const char * name, double value );
 
1069
 
 
1070
        /** Deletes an attribute with the given name.
 
1071
        */
 
1072
        void RemoveAttribute( const char * name );
 
1073
    #ifdef TIXML_USE_STL
 
1074
        void RemoveAttribute( const std::string& name ) {       RemoveAttribute (name.c_str ());        }       ///< STL std::string form.
 
1075
        #endif
 
1076
 
 
1077
        const TiXmlAttribute* FirstAttribute() const    { return attributeSet.First(); }                ///< Access the first attribute in this element.
 
1078
        TiXmlAttribute* FirstAttribute()                                { return attributeSet.First(); }
 
1079
        const TiXmlAttribute* LastAttribute()   const   { return attributeSet.Last(); }         ///< Access the last attribute in this element.
 
1080
        TiXmlAttribute* LastAttribute()                                 { return attributeSet.Last(); }
 
1081
 
 
1082
        /** Convenience function for easy access to the text inside an element. Although easy
 
1083
                and concise, GetText() is limited compared to getting the TiXmlText child
 
1084
                and accessing it directly.
 
1085
        
 
1086
                If the first child of 'this' is a TiXmlText, the GetText()
 
1087
                returns the character string of the Text node, else null is returned.
 
1088
 
 
1089
                This is a convenient method for getting the text of simple contained text:
 
1090
                @verbatim
 
1091
                <foo>This is text</foo>
 
1092
                const char* str = fooElement->GetText();
 
1093
                @endverbatim
 
1094
 
 
1095
                'str' will be a pointer to "This is text". 
 
1096
                
 
1097
                Note that this function can be misleading. If the element foo was created from
 
1098
                this XML:
 
1099
                @verbatim
 
1100
                <foo><b>This is text</b></foo> 
 
1101
                @endverbatim
 
1102
 
 
1103
                then the value of str would be null. The first child node isn't a text node, it is
 
1104
                another element. From this XML:
 
1105
                @verbatim
 
1106
                <foo>This is <b>text</b></foo> 
 
1107
                @endverbatim
 
1108
                GetText() will return "This is ".
 
1109
 
 
1110
                WARNING: GetText() accesses a child node - don't become confused with the 
 
1111
                                 similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are 
 
1112
                                 safe type casts on the referenced node.
 
1113
        */
 
1114
        const char* GetText() const;
 
1115
 
 
1116
        /// Creates a new Element and returns it - the returned element is a copy.
 
1117
        virtual TiXmlNode* Clone() const;
 
1118
        // Print the Element to a FILE stream.
 
1119
        virtual void Print( FILE* cfile, int depth ) const;
 
1120
 
 
1121
        /*      Attribtue parsing starts: next char past '<'
 
1122
                                                 returns: next char past '>'
 
1123
        */
 
1124
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1125
 
 
1126
        virtual const TiXmlElement*     ToElement()     const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1127
        virtual TiXmlElement*           ToElement()               { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1128
 
 
1129
        /** Walk the XML tree visiting this node and all of its children. 
 
1130
        */
 
1131
        virtual bool Accept( TiXmlVisitor* visitor ) const;
 
1132
 
 
1133
protected:
 
1134
 
 
1135
        void CopyTo( TiXmlElement* target ) const;
 
1136
        void ClearThis();       // like clear, but initializes 'this' object as well
 
1137
 
 
1138
        // Used to be public [internal use]
 
1139
        #ifdef TIXML_USE_STL
 
1140
        virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
 
1141
        #endif
 
1142
        /*      [internal use]
 
1143
                Reads the "value" of the element -- another element, or text.
 
1144
                This should terminate with the current end tag.
 
1145
        */
 
1146
        const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
 
1147
 
 
1148
private:
 
1149
 
 
1150
        TiXmlAttributeSet attributeSet;
 
1151
};
 
1152
 
 
1153
 
 
1154
/**     An XML comment.
 
1155
*/
 
1156
class TiXmlComment : public TiXmlNode
 
1157
{
 
1158
public:
 
1159
        /// Constructs an empty comment.
 
1160
        TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
 
1161
        /// Construct a comment from text.
 
1162
        TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::COMMENT ) {
 
1163
                SetValue( _value );
 
1164
        }
 
1165
        TiXmlComment( const TiXmlComment& );
 
1166
        void operator=( const TiXmlComment& base );
 
1167
 
 
1168
        virtual ~TiXmlComment() {}
 
1169
 
 
1170
        /// Returns a copy of this Comment.
 
1171
        virtual TiXmlNode* Clone() const;
 
1172
        // Write this Comment to a FILE stream.
 
1173
        virtual void Print( FILE* cfile, int depth ) const;
 
1174
 
 
1175
        /*      Attribtue parsing starts: at the ! of the !--
 
1176
                                                 returns: next char past '>'
 
1177
        */
 
1178
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1179
 
 
1180
        virtual const TiXmlComment*  ToComment() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1181
        virtual TiXmlComment*  ToComment() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1182
 
 
1183
        /** Walk the XML tree visiting this node and all of its children. 
 
1184
        */
 
1185
        virtual bool Accept( TiXmlVisitor* visitor ) const;
 
1186
 
 
1187
protected:
 
1188
        void CopyTo( TiXmlComment* target ) const;
 
1189
 
 
1190
        // used to be public
 
1191
        #ifdef TIXML_USE_STL
 
1192
        virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
 
1193
        #endif
 
1194
//      virtual void StreamOut( TIXML_OSTREAM * out ) const;
 
1195
 
 
1196
private:
 
1197
 
 
1198
};
 
1199
 
 
1200
 
 
1201
/** XML text. A text node can have 2 ways to output the next. "normal" output 
 
1202
        and CDATA. It will default to the mode it was parsed from the XML file and
 
1203
        you generally want to leave it alone, but you can change the output mode with 
 
1204
        SetCDATA() and query it with CDATA().
 
1205
*/
 
1206
class TiXmlText : public TiXmlNode
 
1207
{
 
1208
        friend class TiXmlElement;
 
1209
public:
 
1210
        /** Constructor for text element. By default, it is treated as 
 
1211
                normal, encoded text. If you want it be output as a CDATA text
 
1212
                element, set the parameter _cdata to 'true'
 
1213
        */
 
1214
        TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
 
1215
        {
 
1216
                SetValue( initValue );
 
1217
                cdata = false;
 
1218
        }
 
1219
        virtual ~TiXmlText() {}
 
1220
 
 
1221
        #ifdef TIXML_USE_STL
 
1222
        /// Constructor.
 
1223
        TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
 
1224
        {
 
1225
                SetValue( initValue );
 
1226
                cdata = false;
 
1227
        }
 
1228
        #endif
 
1229
 
 
1230
        TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT )       { copy.CopyTo( this ); }
 
1231
        void operator=( const TiXmlText& base )                                                         { base.CopyTo( this ); }
 
1232
 
 
1233
        // Write this text object to a FILE stream.
 
1234
        virtual void Print( FILE* cfile, int depth ) const;
 
1235
 
 
1236
        /// Queries whether this represents text using a CDATA section.
 
1237
        bool CDATA() const                              { return cdata; }
 
1238
        /// Turns on or off a CDATA representation of text.
 
1239
        void SetCDATA( bool _cdata )    { cdata = _cdata; }
 
1240
 
 
1241
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1242
 
 
1243
        virtual const TiXmlText* ToText() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1244
        virtual TiXmlText*       ToText()       { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1245
 
 
1246
        /** Walk the XML tree visiting this node and all of its children. 
 
1247
        */
 
1248
        virtual bool Accept( TiXmlVisitor* content ) const;
 
1249
 
 
1250
protected :
 
1251
        ///  [internal use] Creates a new Element and returns it.
 
1252
        virtual TiXmlNode* Clone() const;
 
1253
        void CopyTo( TiXmlText* target ) const;
 
1254
 
 
1255
        bool Blank() const;     // returns true if all white space and new lines
 
1256
        // [internal use]
 
1257
        #ifdef TIXML_USE_STL
 
1258
        virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
 
1259
        #endif
 
1260
 
 
1261
private:
 
1262
        bool cdata;                     // true if this should be input and output as a CDATA style text element
 
1263
};
 
1264
 
 
1265
 
 
1266
/** In correct XML the declaration is the first entry in the file.
 
1267
        @verbatim
 
1268
                <?xml version="1.0" standalone="yes"?>
 
1269
        @endverbatim
 
1270
 
 
1271
        TinyXml will happily read or write files without a declaration,
 
1272
        however. There are 3 possible attributes to the declaration:
 
1273
        version, encoding, and standalone.
 
1274
 
 
1275
        Note: In this version of the code, the attributes are
 
1276
        handled as special cases, not generic attributes, simply
 
1277
        because there can only be at most 3 and they are always the same.
 
1278
*/
 
1279
class TiXmlDeclaration : public TiXmlNode
 
1280
{
 
1281
public:
 
1282
        /// Construct an empty declaration.
 
1283
        TiXmlDeclaration()   : TiXmlNode( TiXmlNode::DECLARATION ) {}
 
1284
 
 
1285
#ifdef TIXML_USE_STL
 
1286
        /// Constructor.
 
1287
        TiXmlDeclaration(       const std::string& _version,
 
1288
                                                const std::string& _encoding,
 
1289
                                                const std::string& _standalone );
 
1290
#endif
 
1291
 
 
1292
        /// Construct.
 
1293
        TiXmlDeclaration(       const char* _version,
 
1294
                                                const char* _encoding,
 
1295
                                                const char* _standalone );
 
1296
 
 
1297
        TiXmlDeclaration( const TiXmlDeclaration& copy );
 
1298
        void operator=( const TiXmlDeclaration& copy );
 
1299
 
 
1300
        virtual ~TiXmlDeclaration()     {}
 
1301
 
 
1302
        /// Version. Will return an empty string if none was found.
 
1303
        const char *Version() const                     { return version.c_str (); }
 
1304
        /// Encoding. Will return an empty string if none was found.
 
1305
        const char *Encoding() const            { return encoding.c_str (); }
 
1306
        /// Is this a standalone document?
 
1307
        const char *Standalone() const          { return standalone.c_str (); }
 
1308
 
 
1309
        /// Creates a copy of this Declaration and returns it.
 
1310
        virtual TiXmlNode* Clone() const;
 
1311
        // Print this declaration to a FILE stream.
 
1312
        virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
 
1313
        virtual void Print( FILE* cfile, int depth ) const {
 
1314
                Print( cfile, depth, 0 );
 
1315
        }
 
1316
 
 
1317
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1318
 
 
1319
        virtual const TiXmlDeclaration* ToDeclaration() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1320
        virtual TiXmlDeclaration*       ToDeclaration()       { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1321
 
 
1322
        /** Walk the XML tree visiting this node and all of its children. 
 
1323
        */
 
1324
        virtual bool Accept( TiXmlVisitor* visitor ) const;
 
1325
 
 
1326
protected:
 
1327
        void CopyTo( TiXmlDeclaration* target ) const;
 
1328
        // used to be public
 
1329
        #ifdef TIXML_USE_STL
 
1330
        virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
 
1331
        #endif
 
1332
 
 
1333
private:
 
1334
 
 
1335
        TIXML_STRING version;
 
1336
        TIXML_STRING encoding;
 
1337
        TIXML_STRING standalone;
 
1338
};
 
1339
 
 
1340
 
 
1341
/** Any tag that tinyXml doesn't recognize is saved as an
 
1342
        unknown. It is a tag of text, but should not be modified.
 
1343
        It will be written back to the XML, unchanged, when the file
 
1344
        is saved.
 
1345
 
 
1346
        DTD tags get thrown into TiXmlUnknowns.
 
1347
*/
 
1348
class TiXmlUnknown : public TiXmlNode
 
1349
{
 
1350
public:
 
1351
        TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN )        {}
 
1352
        virtual ~TiXmlUnknown() {}
 
1353
 
 
1354
        TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN )              { copy.CopyTo( this ); }
 
1355
        void operator=( const TiXmlUnknown& copy )                                                                              { copy.CopyTo( this ); }
 
1356
 
 
1357
        /// Creates a copy of this Unknown and returns it.
 
1358
        virtual TiXmlNode* Clone() const;
 
1359
        // Print this Unknown to a FILE stream.
 
1360
        virtual void Print( FILE* cfile, int depth ) const;
 
1361
 
 
1362
        virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
 
1363
 
 
1364
        virtual const TiXmlUnknown*     ToUnknown()     const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1365
        virtual TiXmlUnknown*           ToUnknown()         { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1366
 
 
1367
        /** Walk the XML tree visiting this node and all of its children. 
 
1368
        */
 
1369
        virtual bool Accept( TiXmlVisitor* content ) const;
 
1370
 
 
1371
protected:
 
1372
        void CopyTo( TiXmlUnknown* target ) const;
 
1373
 
 
1374
        #ifdef TIXML_USE_STL
 
1375
        virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
 
1376
        #endif
 
1377
 
 
1378
private:
 
1379
 
 
1380
};
 
1381
 
 
1382
 
 
1383
/** Always the top level node. A document binds together all the
 
1384
        XML pieces. It can be saved, loaded, and printed to the screen.
 
1385
        The 'value' of a document node is the xml file name.
 
1386
*/
 
1387
class TiXmlDocument : public TiXmlNode
 
1388
{
 
1389
public:
 
1390
        /// Create an empty document, that has no name.
 
1391
        TiXmlDocument();
 
1392
        /// Create a document with a name. The name of the document is also the filename of the xml.
 
1393
        TiXmlDocument( const char * documentName );
 
1394
 
 
1395
        #ifdef TIXML_USE_STL
 
1396
        /// Constructor.
 
1397
        TiXmlDocument( const std::string& documentName );
 
1398
        #endif
 
1399
 
 
1400
        TiXmlDocument( const TiXmlDocument& copy );
 
1401
        void operator=( const TiXmlDocument& copy );
 
1402
 
 
1403
        virtual ~TiXmlDocument() {}
 
1404
 
 
1405
        /** Load a file using the current document value.
 
1406
                Returns true if successful. Will delete any existing
 
1407
                document data before loading.
 
1408
        */
 
1409
        bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 
1410
        /// Save a file using the current document value. Returns true if successful.
 
1411
        bool SaveFile() const;
 
1412
        /// Load a file using the given filename. Returns true if successful.
 
1413
        bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 
1414
        /// Save a file using the given filename. Returns true if successful.
 
1415
        bool SaveFile( const char * filename ) const;
 
1416
        /** Load a file using the given FILE*. Returns true if successful. Note that this method
 
1417
                doesn't stream - the entire object pointed at by the FILE*
 
1418
                will be interpreted as an XML file. TinyXML doesn't stream in XML from the current
 
1419
                file location. Streaming may be added in the future.
 
1420
        */
 
1421
        bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 
1422
        /// Save a file using the given FILE*. Returns true if successful.
 
1423
        bool SaveFile( FILE* ) const;
 
1424
 
 
1425
        #ifdef TIXML_USE_STL
 
1426
        bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )                   ///< STL std::string version.
 
1427
        {
 
1428
//              StringToBuffer f( filename );
 
1429
//              return ( f.buffer && LoadFile( f.buffer, encoding ));
 
1430
                return LoadFile( filename.c_str(), encoding );
 
1431
        }
 
1432
        bool SaveFile( const std::string& filename ) const              ///< STL std::string version.
 
1433
        {
 
1434
//              StringToBuffer f( filename );
 
1435
//              return ( f.buffer && SaveFile( f.buffer ));
 
1436
                return SaveFile( filename.c_str() );
 
1437
        }
 
1438
        #endif
 
1439
 
 
1440
        /** Parse the given null terminated block of xml data. Passing in an encoding to this
 
1441
                method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml
 
1442
                to use that encoding, regardless of what TinyXml might otherwise try to detect.
 
1443
        */
 
1444
        virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 
1445
 
 
1446
        /** Get the root element -- the only top level element -- of the document.
 
1447
                In well formed XML, there should only be one. TinyXml is tolerant of
 
1448
                multiple elements at the document level.
 
1449
        */
 
1450
        const TiXmlElement* RootElement() const         { return FirstChildElement(); }
 
1451
        TiXmlElement* RootElement()                                     { return FirstChildElement(); }
 
1452
 
 
1453
        /** If an error occurs, Error will be set to true. Also,
 
1454
                - The ErrorId() will contain the integer identifier of the error (not generally useful)
 
1455
                - The ErrorDesc() method will return the name of the error. (very useful)
 
1456
                - The ErrorRow() and ErrorCol() will return the location of the error (if known)
 
1457
        */      
 
1458
        bool Error() const                                              { return error; }
 
1459
 
 
1460
        /// Contains a textual (english) description of the error if one occurs.
 
1461
        const char * ErrorDesc() const  { return errorDesc.c_str (); }
 
1462
 
 
1463
        /** Generally, you probably want the error string ( ErrorDesc() ). But if you
 
1464
                prefer the ErrorId, this function will fetch it.
 
1465
        */
 
1466
        int ErrorId()   const                           { return errorId; }
 
1467
 
 
1468
        /** Returns the location (if known) of the error. The first column is column 1, 
 
1469
                and the first row is row 1. A value of 0 means the row and column wasn't applicable
 
1470
                (memory errors, for example, have no row/column) or the parser lost the error. (An
 
1471
                error in the error reporting, in that case.)
 
1472
 
 
1473
                @sa SetTabSize, Row, Column
 
1474
        */
 
1475
        int ErrorRow() const    { return errorLocation.row+1; }
 
1476
        int ErrorCol() const    { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow()
 
1477
 
 
1478
        /** SetTabSize() allows the error reporting functions (ErrorRow() and ErrorCol())
 
1479
                to report the correct values for row and column. It does not change the output
 
1480
                or input in any way.
 
1481
                
 
1482
                By calling this method, with a tab size
 
1483
                greater than 0, the row and column of each node and attribute is stored
 
1484
                when the file is loaded. Very useful for tracking the DOM back in to
 
1485
                the source file.
 
1486
 
 
1487
                The tab size is required for calculating the location of nodes. If not
 
1488
                set, the default of 4 is used. The tabsize is set per document. Setting
 
1489
                the tabsize to 0 disables row/column tracking.
 
1490
 
 
1491
                Note that row and column tracking is not supported when using operator>>.
 
1492
 
 
1493
                The tab size needs to be enabled before the parse or load. Correct usage:
 
1494
                @verbatim
 
1495
                TiXmlDocument doc;
 
1496
                doc.SetTabSize( 8 );
 
1497
                doc.Load( "myfile.xml" );
 
1498
                @endverbatim
 
1499
 
 
1500
                @sa Row, Column
 
1501
        */
 
1502
        void SetTabSize( int _tabsize )         { tabsize = _tabsize; }
 
1503
 
 
1504
        int TabSize() const     { return tabsize; }
 
1505
 
 
1506
        /** If you have handled the error, it can be reset with this call. The error
 
1507
                state is automatically cleared if you Parse a new XML block.
 
1508
        */
 
1509
        void ClearError()                                               {       error = false; 
 
1510
                                                                                                errorId = 0; 
 
1511
                                                                                                errorDesc = ""; 
 
1512
                                                                                                errorLocation.row = errorLocation.col = 0; 
 
1513
                                                                                                //errorLocation.last = 0; 
 
1514
                                                                                        }
 
1515
 
 
1516
        /** Write the document to standard out using formatted printing ("pretty print"). */
 
1517
        void Print() const                                              { Print( stdout, 0 ); }
 
1518
 
 
1519
        /* Write the document to a string using formatted printing ("pretty print"). This
 
1520
                will allocate a character array (new char[]) and return it as a pointer. The
 
1521
                calling code pust call delete[] on the return char* to avoid a memory leak.
 
1522
        */
 
1523
        //char* PrintToMemory() const; 
 
1524
 
 
1525
        /// Print this Document to a FILE stream.
 
1526
        virtual void Print( FILE* cfile, int depth = 0 ) const;
 
1527
        // [internal use]
 
1528
        void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
 
1529
 
 
1530
        virtual const TiXmlDocument*    ToDocument()    const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1531
        virtual TiXmlDocument*          ToDocument()          { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
 
1532
 
 
1533
        /** Walk the XML tree visiting this node and all of its children. 
 
1534
        */
 
1535
        virtual bool Accept( TiXmlVisitor* content ) const;
 
1536
 
 
1537
protected :
 
1538
        // [internal use]
 
1539
        virtual TiXmlNode* Clone() const;
 
1540
        #ifdef TIXML_USE_STL
 
1541
        virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
 
1542
        #endif
 
1543
 
 
1544
private:
 
1545
        void CopyTo( TiXmlDocument* target ) const;
 
1546
 
 
1547
        bool error;
 
1548
        int  errorId;
 
1549
        TIXML_STRING errorDesc;
 
1550
        int tabsize;
 
1551
        TiXmlCursor errorLocation;
 
1552
        bool useMicrosoftBOM;           // the UTF-8 BOM were found when read. Note this, and try to write.
 
1553
};
 
1554
 
 
1555
 
 
1556
/**
 
1557
        A TiXmlHandle is a class that wraps a node pointer with null checks; this is
 
1558
        an incredibly useful thing. Note that TiXmlHandle is not part of the TinyXml
 
1559
        DOM structure. It is a separate utility class.
 
1560
 
 
1561
        Take an example:
 
1562
        @verbatim
 
1563
        <Document>
 
1564
                <Element attributeA = "valueA">
 
1565
                        <Child attributeB = "value1" />
 
1566
                        <Child attributeB = "value2" />
 
1567
                </Element>
 
1568
        <Document>
 
1569
        @endverbatim
 
1570
 
 
1571
        Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very 
 
1572
        easy to write a *lot* of code that looks like:
 
1573
 
 
1574
        @verbatim
 
1575
        TiXmlElement* root = document.FirstChildElement( "Document" );
 
1576
        if ( root )
 
1577
        {
 
1578
                TiXmlElement* element = root->FirstChildElement( "Element" );
 
1579
                if ( element )
 
1580
                {
 
1581
                        TiXmlElement* child = element->FirstChildElement( "Child" );
 
1582
                        if ( child )
 
1583
                        {
 
1584
                                TiXmlElement* child2 = child->NextSiblingElement( "Child" );
 
1585
                                if ( child2 )
 
1586
                                {
 
1587
                                        // Finally do something useful.
 
1588
        @endverbatim
 
1589
 
 
1590
        And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity
 
1591
        of such code. A TiXmlHandle checks for null     pointers so it is perfectly safe 
 
1592
        and correct to use:
 
1593
 
 
1594
        @verbatim
 
1595
        TiXmlHandle docHandle( &document );
 
1596
        TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
 
1597
        if ( child2 )
 
1598
        {
 
1599
                // do something useful
 
1600
        @endverbatim
 
1601
 
 
1602
        Which is MUCH more concise and useful.
 
1603
 
 
1604
        It is also safe to copy handles - internally they are nothing more than node pointers.
 
1605
        @verbatim
 
1606
        TiXmlHandle handleCopy = handle;
 
1607
        @endverbatim
 
1608
 
 
1609
        What they should not be used for is iteration:
 
1610
 
 
1611
        @verbatim
 
1612
        int i=0; 
 
1613
        while ( true )
 
1614
        {
 
1615
                TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).ToElement();
 
1616
                if ( !child )
 
1617
                        break;
 
1618
                // do something
 
1619
                ++i;
 
1620
        }
 
1621
        @endverbatim
 
1622
 
 
1623
        It seems reasonable, but it is in fact two embedded while loops. The Child method is 
 
1624
        a linear walk to find the element, so this code would iterate much more than it needs 
 
1625
        to. Instead, prefer:
 
1626
 
 
1627
        @verbatim
 
1628
        TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).ToElement();
 
1629
 
 
1630
        for( child; child; child=child->NextSiblingElement() )
 
1631
        {
 
1632
                // do something
 
1633
        }
 
1634
        @endverbatim
 
1635
*/
 
1636
class TiXmlHandle
 
1637
{
 
1638
public:
 
1639
        /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
 
1640
        TiXmlHandle( TiXmlNode* _node )                                 { this->node = _node; }
 
1641
        /// Copy constructor
 
1642
        TiXmlHandle( const TiXmlHandle& ref )                   { this->node = ref.node; }
 
1643
        TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
 
1644
 
 
1645
        /// Return a handle to the first child node.
 
1646
        TiXmlHandle FirstChild() const;
 
1647
        /// Return a handle to the first child node with the given name.
 
1648
        TiXmlHandle FirstChild( const char * value ) const;
 
1649
        /// Return a handle to the first child element.
 
1650
        TiXmlHandle FirstChildElement() const;
 
1651
        /// Return a handle to the first child element with the given name.
 
1652
        TiXmlHandle FirstChildElement( const char * value ) const;
 
1653
 
 
1654
        /** Return a handle to the "index" child with the given name. 
 
1655
                The first child is 0, the second 1, etc.
 
1656
        */
 
1657
        TiXmlHandle Child( const char* value, int index ) const;
 
1658
        /** Return a handle to the "index" child. 
 
1659
                The first child is 0, the second 1, etc.
 
1660
        */
 
1661
        TiXmlHandle Child( int index ) const;
 
1662
        /** Return a handle to the "index" child element with the given name. 
 
1663
                The first child element is 0, the second 1, etc. Note that only TiXmlElements
 
1664
                are indexed: other types are not counted.
 
1665
        */
 
1666
        TiXmlHandle ChildElement( const char* value, int index ) const;
 
1667
        /** Return a handle to the "index" child element. 
 
1668
                The first child element is 0, the second 1, etc. Note that only TiXmlElements
 
1669
                are indexed: other types are not counted.
 
1670
        */
 
1671
        TiXmlHandle ChildElement( int index ) const;
 
1672
 
 
1673
        #ifdef TIXML_USE_STL
 
1674
        TiXmlHandle FirstChild( const std::string& _value ) const                               { return FirstChild( _value.c_str() ); }
 
1675
        TiXmlHandle FirstChildElement( const std::string& _value ) const                { return FirstChildElement( _value.c_str() ); }
 
1676
 
 
1677
        TiXmlHandle Child( const std::string& _value, int index ) const                 { return Child( _value.c_str(), index ); }
 
1678
        TiXmlHandle ChildElement( const std::string& _value, int index ) const  { return ChildElement( _value.c_str(), index ); }
 
1679
        #endif
 
1680
 
 
1681
        /** Return the handle as a TiXmlNode. This may return null.
 
1682
        */
 
1683
        TiXmlNode* ToNode() const                       { return node; } 
 
1684
        /** Return the handle as a TiXmlElement. This may return null.
 
1685
        */
 
1686
        TiXmlElement* ToElement() const         { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
 
1687
        /**     Return the handle as a TiXmlText. This may return null.
 
1688
        */
 
1689
        TiXmlText* ToText() const                       { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
 
1690
        /** Return the handle as a TiXmlUnknown. This may return null.
 
1691
        */
 
1692
        TiXmlUnknown* ToUnknown() const         { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
 
1693
 
 
1694
        /** @deprecated use ToNode. 
 
1695
                Return the handle as a TiXmlNode. This may return null.
 
1696
        */
 
1697
        TiXmlNode* Node() const                 { return ToNode(); } 
 
1698
        /** @deprecated use ToElement. 
 
1699
                Return the handle as a TiXmlElement. This may return null.
 
1700
        */
 
1701
        TiXmlElement* Element() const   { return ToElement(); }
 
1702
        /**     @deprecated use ToText()
 
1703
                Return the handle as a TiXmlText. This may return null.
 
1704
        */
 
1705
        TiXmlText* Text() const                 { return ToText(); }
 
1706
        /** @deprecated use ToUnknown()
 
1707
                Return the handle as a TiXmlUnknown. This may return null.
 
1708
        */
 
1709
        TiXmlUnknown* Unknown() const   { return ToUnknown(); }
 
1710
 
 
1711
private:
 
1712
        TiXmlNode* node;
 
1713
};
 
1714
 
 
1715
 
 
1716
/** Print to memory functionality. The TiXmlPrinter is useful when you need to:
 
1717
 
 
1718
        -# Print to memory (especially in non-STL mode)
 
1719
        -# Control formatting (line endings, etc.)
 
1720
 
 
1721
        When constructed, the TiXmlPrinter is in its default "pretty printing" mode.
 
1722
        Before calling Accept() you can call methods to control the printing
 
1723
        of the XML document. After TiXmlNode::Accept() is called, the printed document can
 
1724
        be accessed via the CStr(), Str(), and Size() methods.
 
1725
 
 
1726
        TiXmlPrinter uses the Visitor API.
 
1727
        @verbatim
 
1728
        TiXmlPrinter printer;
 
1729
        printer.SetIndent( "\t" );
 
1730
 
 
1731
        doc.Accept( &printer );
 
1732
        fprintf( stdout, "%s", printer.CStr() );
 
1733
        @endverbatim
 
1734
*/
 
1735
class TiXmlPrinter : public TiXmlVisitor
 
1736
{
 
1737
public:
 
1738
        TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
 
1739
                                         buffer(), indent( "    " ), lineBreak( "\n" ) {}
 
1740
 
 
1741
        virtual bool VisitEnter( const TiXmlDocument& doc );
 
1742
        virtual bool VisitExit( const TiXmlDocument& doc );
 
1743
 
 
1744
        virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
 
1745
        virtual bool VisitExit( const TiXmlElement& element );
 
1746
 
 
1747
        virtual bool Visit( const TiXmlDeclaration& declaration );
 
1748
        virtual bool Visit( const TiXmlText& text );
 
1749
        virtual bool Visit( const TiXmlComment& comment );
 
1750
        virtual bool Visit( const TiXmlUnknown& unknown );
 
1751
 
 
1752
        /** Set the indent characters for printing. By default 4 spaces
 
1753
                but tab (\t) is also useful, or null/empty string for no indentation.
 
1754
        */
 
1755
        void SetIndent( const char* _indent )                   { indent = _indent ? _indent : "" ; }
 
1756
        /// Query the indention string.
 
1757
        const char* Indent()                                                    { return indent.c_str(); }
 
1758
        /** Set the line breaking string. By default set to newline (\n). 
 
1759
                Some operating systems prefer other characters, or can be
 
1760
                set to the null/empty string for no indenation.
 
1761
        */
 
1762
        void SetLineBreak( const char* _lineBreak )             { lineBreak = _lineBreak ? _lineBreak : ""; }
 
1763
        /// Query the current line breaking string.
 
1764
        const char* LineBreak()                                                 { return lineBreak.c_str(); }
 
1765
 
 
1766
        /** Switch over to "stream printing" which is the most dense formatting without 
 
1767
                linebreaks. Common when the XML is needed for network transmission.
 
1768
        */
 
1769
        void SetStreamPrinting()                                                { indent = "";
 
1770
                                                                                                          lineBreak = "";
 
1771
                                                                                                        }       
 
1772
        /// Return the result.
 
1773
        const char* CStr()                                                              { return buffer.c_str(); }
 
1774
        /// Return the length of the result string.
 
1775
        size_t Size()                                                                   { return buffer.size(); }
 
1776
 
 
1777
        #ifdef TIXML_USE_STL
 
1778
        /// Return the result.
 
1779
        const std::string& Str()                                                { return buffer; }
 
1780
        #endif
 
1781
 
 
1782
private:
 
1783
        void DoIndent() {
 
1784
                for( int i=0; i<depth; ++i )
 
1785
                        buffer += indent;
 
1786
        }
 
1787
        void DoLineBreak() {
 
1788
                buffer += lineBreak;
 
1789
        }
 
1790
 
 
1791
        int depth;
 
1792
        bool simpleTextPrint;
 
1793
        TIXML_STRING buffer;
 
1794
        TIXML_STRING indent;
 
1795
        TIXML_STRING lineBreak;
 
1796
};
 
1797
 
 
1798
 
 
1799
#ifdef _MSC_VER
 
1800
#pragma warning( pop )
 
1801
#endif
 
1802
 
 
1803
#endif
 
1804