goat3d

diff libs/tinyxml2/tinyxml2.h @ 19:b35427826b60

- added XML format reading support - wrote a rudimentary version of goatview
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Sep 2013 06:58:37 +0300
parents
children 9862541fdcf5
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/tinyxml2/tinyxml2.h	Fri Sep 27 06:58:37 2013 +0300
     1.3 @@ -0,0 +1,1987 @@
     1.4 +/*
     1.5 +Original code by Lee Thomason (www.grinninglizard.com)
     1.6 +
     1.7 +This software is provided 'as-is', without any express or implied
     1.8 +warranty. In no event will the authors be held liable for any
     1.9 +damages arising from the use of this software.
    1.10 +
    1.11 +Permission is granted to anyone to use this software for any
    1.12 +purpose, including commercial applications, and to alter it and
    1.13 +redistribute it freely, subject to the following restrictions:
    1.14 +
    1.15 +1. The origin of this software must not be misrepresented; you must
    1.16 +not claim that you wrote the original software. If you use this
    1.17 +software in a product, an acknowledgment in the product documentation
    1.18 +would be appreciated but is not required.
    1.19 +
    1.20 +
    1.21 +2. Altered source versions must be plainly marked as such, and
    1.22 +must not be misrepresented as being the original software.
    1.23 +
    1.24 +3. This notice may not be removed or altered from any source
    1.25 +distribution.
    1.26 +*/
    1.27 +
    1.28 +#ifndef TINYXML2_INCLUDED
    1.29 +#define TINYXML2_INCLUDED
    1.30 +
    1.31 +#if defined(ANDROID_NDK) || defined(__BORLANDC__)
    1.32 +#   include <ctype.h>
    1.33 +#   include <limits.h>
    1.34 +#   include <stdio.h>
    1.35 +#   include <stdlib.h>
    1.36 +#   include <string.h>
    1.37 +#   include <stdarg.h>
    1.38 +#else
    1.39 +#   include <cctype>
    1.40 +#   include <climits>
    1.41 +#   include <cstdio>
    1.42 +#   include <cstdlib>
    1.43 +#   include <cstring>
    1.44 +#   include <cstdarg>
    1.45 +#endif
    1.46 +
    1.47 +/*
    1.48 +   TODO: intern strings instead of allocation.
    1.49 +*/
    1.50 +/*
    1.51 +	gcc:
    1.52 +        g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
    1.53 +
    1.54 +    Formatting, Artistic Style:
    1.55 +        AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
    1.56 +*/
    1.57 +
    1.58 +#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
    1.59 +#   ifndef DEBUG
    1.60 +#       define DEBUG
    1.61 +#   endif
    1.62 +#endif
    1.63 +
    1.64 +#ifdef _MSC_VER
    1.65 +#   pragma warning(push)
    1.66 +#   pragma warning(disable: 4251)
    1.67 +#endif
    1.68 +
    1.69 +#ifdef _WIN32
    1.70 +#   ifdef TINYXML2_EXPORT
    1.71 +#       define TINYXML2_LIB __declspec(dllexport)
    1.72 +#   elif defined(TINYXML2_IMPORT)
    1.73 +#       define TINYXML2_LIB __declspec(dllimport)
    1.74 +#   else
    1.75 +#       define TINYXML2_LIB
    1.76 +#   endif
    1.77 +#else
    1.78 +#   define TINYXML2_LIB
    1.79 +#endif
    1.80 +
    1.81 +
    1.82 +#if defined(DEBUG)
    1.83 +#   if defined(_MSC_VER)
    1.84 +#       define TIXMLASSERT( x )           if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
    1.85 +#   elif defined (ANDROID_NDK)
    1.86 +#       include <android/log.h>
    1.87 +#       define TIXMLASSERT( x )           if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
    1.88 +#   else
    1.89 +#       include <assert.h>
    1.90 +#       define TIXMLASSERT                assert
    1.91 +#   endif
    1.92 +#   else
    1.93 +#       define TIXMLASSERT( x )           {}
    1.94 +#endif
    1.95 +
    1.96 +
    1.97 +#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
    1.98 +// Microsoft visual studio, version 2005 and higher.
    1.99 +/*int _snprintf_s(
   1.100 +   char *buffer,
   1.101 +   size_t sizeOfBuffer,
   1.102 +   size_t count,
   1.103 +   const char *format [,
   1.104 +	  argument] ...
   1.105 +);*/
   1.106 +inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
   1.107 +{
   1.108 +    va_list va;
   1.109 +    va_start( va, format );
   1.110 +    int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
   1.111 +    va_end( va );
   1.112 +    return result;
   1.113 +}
   1.114 +#define TIXML_SSCANF   sscanf_s
   1.115 +#else
   1.116 +// GCC version 3 and higher
   1.117 +//#warning( "Using sn* functions." )
   1.118 +#define TIXML_SNPRINTF snprintf
   1.119 +#define TIXML_SSCANF   sscanf
   1.120 +#endif
   1.121 +
   1.122 +static const int TIXML2_MAJOR_VERSION = 1;
   1.123 +static const int TIXML2_MINOR_VERSION = 0;
   1.124 +static const int TIXML2_PATCH_VERSION = 11;
   1.125 +
   1.126 +namespace tinyxml2
   1.127 +{
   1.128 +class XMLDocument;
   1.129 +class XMLElement;
   1.130 +class XMLAttribute;
   1.131 +class XMLComment;
   1.132 +class XMLText;
   1.133 +class XMLDeclaration;
   1.134 +class XMLUnknown;
   1.135 +class XMLPrinter;
   1.136 +
   1.137 +/*
   1.138 +	A class that wraps strings. Normally stores the start and end
   1.139 +	pointers into the XML file itself, and will apply normalization
   1.140 +	and entity translation if actually read. Can also store (and memory
   1.141 +	manage) a traditional char[]
   1.142 +*/
   1.143 +class StrPair
   1.144 +{
   1.145 +public:
   1.146 +    enum {
   1.147 +        NEEDS_ENTITY_PROCESSING			= 0x01,
   1.148 +        NEEDS_NEWLINE_NORMALIZATION		= 0x02,
   1.149 +        COLLAPSE_WHITESPACE	                = 0x04,
   1.150 +
   1.151 +        TEXT_ELEMENT		            	= NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
   1.152 +        TEXT_ELEMENT_LEAVE_ENTITIES		= NEEDS_NEWLINE_NORMALIZATION,
   1.153 +        ATTRIBUTE_NAME		            	= 0,
   1.154 +        ATTRIBUTE_VALUE		            	= NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
   1.155 +        ATTRIBUTE_VALUE_LEAVE_ENTITIES  	= NEEDS_NEWLINE_NORMALIZATION,
   1.156 +        COMMENT				        = NEEDS_NEWLINE_NORMALIZATION
   1.157 +    };
   1.158 +
   1.159 +    StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
   1.160 +    ~StrPair();
   1.161 +
   1.162 +    void Set( char* start, char* end, int flags ) {
   1.163 +        Reset();
   1.164 +        _start  = start;
   1.165 +        _end    = end;
   1.166 +        _flags  = flags | NEEDS_FLUSH;
   1.167 +    }
   1.168 +
   1.169 +    const char* GetStr();
   1.170 +
   1.171 +    bool Empty() const {
   1.172 +        return _start == _end;
   1.173 +    }
   1.174 +
   1.175 +    void SetInternedStr( const char* str ) {
   1.176 +        Reset();
   1.177 +        _start = const_cast<char*>(str);
   1.178 +    }
   1.179 +
   1.180 +    void SetStr( const char* str, int flags=0 );
   1.181 +
   1.182 +    char* ParseText( char* in, const char* endTag, int strFlags );
   1.183 +    char* ParseName( char* in );
   1.184 +
   1.185 +private:
   1.186 +    void Reset();
   1.187 +    void CollapseWhitespace();
   1.188 +
   1.189 +    enum {
   1.190 +        NEEDS_FLUSH = 0x100,
   1.191 +        NEEDS_DELETE = 0x200
   1.192 +    };
   1.193 +
   1.194 +    // After parsing, if *_end != 0, it can be set to zero.
   1.195 +    int     _flags;
   1.196 +    char*   _start;
   1.197 +    char*   _end;
   1.198 +};
   1.199 +
   1.200 +
   1.201 +/*
   1.202 +	A dynamic array of Plain Old Data. Doesn't support constructors, etc.
   1.203 +	Has a small initial memory pool, so that low or no usage will not
   1.204 +	cause a call to new/delete
   1.205 +*/
   1.206 +template <class T, int INIT>
   1.207 +class DynArray
   1.208 +{
   1.209 +public:
   1.210 +    DynArray< T, INIT >() {
   1.211 +        _mem = _pool;
   1.212 +        _allocated = INIT;
   1.213 +        _size = 0;
   1.214 +    }
   1.215 +
   1.216 +    ~DynArray() {
   1.217 +        if ( _mem != _pool ) {
   1.218 +            delete [] _mem;
   1.219 +        }
   1.220 +    }
   1.221 +
   1.222 +    void Push( T t ) {
   1.223 +        EnsureCapacity( _size+1 );
   1.224 +        _mem[_size++] = t;
   1.225 +    }
   1.226 +
   1.227 +    T* PushArr( int count ) {
   1.228 +        EnsureCapacity( _size+count );
   1.229 +        T* ret = &_mem[_size];
   1.230 +        _size += count;
   1.231 +        return ret;
   1.232 +    }
   1.233 +
   1.234 +    T Pop() {
   1.235 +        return _mem[--_size];
   1.236 +    }
   1.237 +
   1.238 +    void PopArr( int count ) {
   1.239 +        TIXMLASSERT( _size >= count );
   1.240 +        _size -= count;
   1.241 +    }
   1.242 +
   1.243 +    bool Empty() const					{
   1.244 +        return _size == 0;
   1.245 +    }
   1.246 +
   1.247 +    T& operator[](int i)				{
   1.248 +        TIXMLASSERT( i>= 0 && i < _size );
   1.249 +        return _mem[i];
   1.250 +    }
   1.251 +
   1.252 +    const T& operator[](int i) const	{
   1.253 +        TIXMLASSERT( i>= 0 && i < _size );
   1.254 +        return _mem[i];
   1.255 +    }
   1.256 +
   1.257 +    int Size() const					{
   1.258 +        return _size;
   1.259 +    }
   1.260 +
   1.261 +    int Capacity() const				{
   1.262 +        return _allocated;
   1.263 +    }
   1.264 +
   1.265 +    const T* Mem() const				{
   1.266 +        return _mem;
   1.267 +    }
   1.268 +
   1.269 +    T* Mem()							{
   1.270 +        return _mem;
   1.271 +    }
   1.272 +
   1.273 +private:
   1.274 +    void EnsureCapacity( int cap ) {
   1.275 +        if ( cap > _allocated ) {
   1.276 +            int newAllocated = cap * 2;
   1.277 +            T* newMem = new T[newAllocated];
   1.278 +            memcpy( newMem, _mem, sizeof(T)*_size );	// warning: not using constructors, only works for PODs
   1.279 +            if ( _mem != _pool ) {
   1.280 +                delete [] _mem;
   1.281 +            }
   1.282 +            _mem = newMem;
   1.283 +            _allocated = newAllocated;
   1.284 +        }
   1.285 +    }
   1.286 +
   1.287 +    T*  _mem;
   1.288 +    T   _pool[INIT];
   1.289 +    int _allocated;		// objects allocated
   1.290 +    int _size;			// number objects in use
   1.291 +};
   1.292 +
   1.293 +
   1.294 +/*
   1.295 +	Parent virtual class of a pool for fast allocation
   1.296 +	and deallocation of objects.
   1.297 +*/
   1.298 +class MemPool
   1.299 +{
   1.300 +public:
   1.301 +    MemPool() {}
   1.302 +    virtual ~MemPool() {}
   1.303 +
   1.304 +    virtual int ItemSize() const = 0;
   1.305 +    virtual void* Alloc() = 0;
   1.306 +    virtual void Free( void* ) = 0;
   1.307 +    virtual void SetTracked() = 0;
   1.308 +};
   1.309 +
   1.310 +
   1.311 +/*
   1.312 +	Template child class to create pools of the correct type.
   1.313 +*/
   1.314 +template< int SIZE >
   1.315 +class MemPoolT : public MemPool
   1.316 +{
   1.317 +public:
   1.318 +    MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0)	{}
   1.319 +    ~MemPoolT() {
   1.320 +        // Delete the blocks.
   1.321 +        for( int i=0; i<_blockPtrs.Size(); ++i ) {
   1.322 +            delete _blockPtrs[i];
   1.323 +        }
   1.324 +    }
   1.325 +
   1.326 +    virtual int ItemSize() const	{
   1.327 +        return SIZE;
   1.328 +    }
   1.329 +    int CurrentAllocs() const		{
   1.330 +        return _currentAllocs;
   1.331 +    }
   1.332 +
   1.333 +    virtual void* Alloc() {
   1.334 +        if ( !_root ) {
   1.335 +            // Need a new block.
   1.336 +            Block* block = new Block();
   1.337 +            _blockPtrs.Push( block );
   1.338 +
   1.339 +            for( int i=0; i<COUNT-1; ++i ) {
   1.340 +                block->chunk[i].next = &block->chunk[i+1];
   1.341 +            }
   1.342 +            block->chunk[COUNT-1].next = 0;
   1.343 +            _root = block->chunk;
   1.344 +        }
   1.345 +        void* result = _root;
   1.346 +        _root = _root->next;
   1.347 +
   1.348 +        ++_currentAllocs;
   1.349 +        if ( _currentAllocs > _maxAllocs ) {
   1.350 +            _maxAllocs = _currentAllocs;
   1.351 +        }
   1.352 +        _nAllocs++;
   1.353 +        _nUntracked++;
   1.354 +        return result;
   1.355 +    }
   1.356 +    virtual void Free( void* mem ) {
   1.357 +        if ( !mem ) {
   1.358 +            return;
   1.359 +        }
   1.360 +        --_currentAllocs;
   1.361 +        Chunk* chunk = (Chunk*)mem;
   1.362 +#ifdef DEBUG
   1.363 +        memset( chunk, 0xfe, sizeof(Chunk) );
   1.364 +#endif
   1.365 +        chunk->next = _root;
   1.366 +        _root = chunk;
   1.367 +    }
   1.368 +    void Trace( const char* name ) {
   1.369 +        printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
   1.370 +                name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
   1.371 +    }
   1.372 +
   1.373 +    void SetTracked() {
   1.374 +        _nUntracked--;
   1.375 +    }
   1.376 +
   1.377 +    int Untracked() const {
   1.378 +        return _nUntracked;
   1.379 +    }
   1.380 +
   1.381 +	// This number is perf sensitive. 4k seems like a good tradeoff on my machine.
   1.382 +	// The test file is large, 170k.
   1.383 +	// Release:		VS2010 gcc(no opt)
   1.384 +	//		1k:		4000
   1.385 +	//		2k:		4000
   1.386 +	//		4k:		3900	21000
   1.387 +	//		16k:	5200
   1.388 +	//		32k:	4300
   1.389 +	//		64k:	4000	21000
   1.390 +    enum { COUNT = (4*1024)/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private
   1.391 +
   1.392 +private:
   1.393 +    union Chunk {
   1.394 +        Chunk*  next;
   1.395 +        char    mem[SIZE];
   1.396 +    };
   1.397 +    struct Block {
   1.398 +        Chunk chunk[COUNT];
   1.399 +    };
   1.400 +    DynArray< Block*, 10 > _blockPtrs;
   1.401 +    Chunk* _root;
   1.402 +
   1.403 +    int _currentAllocs;
   1.404 +    int _nAllocs;
   1.405 +    int _maxAllocs;
   1.406 +    int _nUntracked;
   1.407 +};
   1.408 +
   1.409 +
   1.410 +
   1.411 +/**
   1.412 +	Implements the interface to the "Visitor pattern" (see the Accept() method.)
   1.413 +	If you call the Accept() method, it requires being passed a XMLVisitor
   1.414 +	class to handle callbacks. For nodes that contain other nodes (Document, Element)
   1.415 +	you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
   1.416 +	are simply called with Visit().
   1.417 +
   1.418 +	If you return 'true' from a Visit method, recursive parsing will continue. If you return
   1.419 +	false, <b>no children of this node or its siblings</b> will be visited.
   1.420 +
   1.421 +	All flavors of Visit methods have a default implementation that returns 'true' (continue
   1.422 +	visiting). You need to only override methods that are interesting to you.
   1.423 +
   1.424 +	Generally Accept() is called on the XMLDocument, although all nodes support visiting.
   1.425 +
   1.426 +	You should never change the document from a callback.
   1.427 +
   1.428 +	@sa XMLNode::Accept()
   1.429 +*/
   1.430 +class TINYXML2_LIB XMLVisitor
   1.431 +{
   1.432 +public:
   1.433 +    virtual ~XMLVisitor() {}
   1.434 +
   1.435 +    /// Visit a document.
   1.436 +    virtual bool VisitEnter( const XMLDocument& /*doc*/ )			{
   1.437 +        return true;
   1.438 +    }
   1.439 +    /// Visit a document.
   1.440 +    virtual bool VisitExit( const XMLDocument& /*doc*/ )			{
   1.441 +        return true;
   1.442 +    }
   1.443 +
   1.444 +    /// Visit an element.
   1.445 +    virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ )	{
   1.446 +        return true;
   1.447 +    }
   1.448 +    /// Visit an element.
   1.449 +    virtual bool VisitExit( const XMLElement& /*element*/ )			{
   1.450 +        return true;
   1.451 +    }
   1.452 +
   1.453 +    /// Visit a declaration.
   1.454 +    virtual bool Visit( const XMLDeclaration& /*declaration*/ )		{
   1.455 +        return true;
   1.456 +    }
   1.457 +    /// Visit a text node.
   1.458 +    virtual bool Visit( const XMLText& /*text*/ )					{
   1.459 +        return true;
   1.460 +    }
   1.461 +    /// Visit a comment node.
   1.462 +    virtual bool Visit( const XMLComment& /*comment*/ )				{
   1.463 +        return true;
   1.464 +    }
   1.465 +    /// Visit an unknown node.
   1.466 +    virtual bool Visit( const XMLUnknown& /*unknown*/ )				{
   1.467 +        return true;
   1.468 +    }
   1.469 +};
   1.470 +
   1.471 +
   1.472 +/*
   1.473 +	Utility functionality.
   1.474 +*/
   1.475 +class XMLUtil
   1.476 +{
   1.477 +public:
   1.478 +    // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
   1.479 +    // correct, but simple, and usually works.
   1.480 +    static const char* SkipWhiteSpace( const char* p )	{
   1.481 +        while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<const unsigned char*>(p) ) ) {
   1.482 +            ++p;
   1.483 +        }
   1.484 +        return p;
   1.485 +    }
   1.486 +    static char* SkipWhiteSpace( char* p )				{
   1.487 +        while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<unsigned char*>(p) ) )		{
   1.488 +            ++p;
   1.489 +        }
   1.490 +        return p;
   1.491 +    }
   1.492 +    static bool IsWhiteSpace( char p )					{
   1.493 +        return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
   1.494 +    }
   1.495 +    
   1.496 +    inline static bool IsNameStartChar( unsigned char ch ) {
   1.497 +        return ( ( ch < 128 ) ? isalpha( ch ) : 1 )
   1.498 +               || ch == ':'
   1.499 +               || ch == '_';
   1.500 +    }
   1.501 +    
   1.502 +    inline static bool IsNameChar( unsigned char ch ) {
   1.503 +        return IsNameStartChar( ch )
   1.504 +               || isdigit( ch )
   1.505 +               || ch == '.'
   1.506 +               || ch == '-';
   1.507 +    }
   1.508 +
   1.509 +    inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX )  {
   1.510 +        int n = 0;
   1.511 +        if ( p == q ) {
   1.512 +            return true;
   1.513 +        }
   1.514 +        while( *p && *q && *p == *q && n<nChar ) {
   1.515 +            ++p;
   1.516 +            ++q;
   1.517 +            ++n;
   1.518 +        }
   1.519 +        if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
   1.520 +            return true;
   1.521 +        }
   1.522 +        return false;
   1.523 +    }
   1.524 +    
   1.525 +    inline static int IsUTF8Continuation( const char p ) {
   1.526 +        return p & 0x80;
   1.527 +    }
   1.528 +
   1.529 +    static const char* ReadBOM( const char* p, bool* hasBOM );
   1.530 +    // p is the starting location,
   1.531 +    // the UTF-8 value of the entity will be placed in value, and length filled in.
   1.532 +    static const char* GetCharacterRef( const char* p, char* value, int* length );
   1.533 +    static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
   1.534 +
   1.535 +    // converts primitive types to strings
   1.536 +    static void ToStr( int v, char* buffer, int bufferSize );
   1.537 +    static void ToStr( unsigned v, char* buffer, int bufferSize );
   1.538 +    static void ToStr( bool v, char* buffer, int bufferSize );
   1.539 +    static void ToStr( float v, char* buffer, int bufferSize );
   1.540 +    static void ToStr( double v, char* buffer, int bufferSize );
   1.541 +
   1.542 +    // converts strings to primitive types
   1.543 +    static bool	ToInt( const char* str, int* value );
   1.544 +    static bool ToUnsigned( const char* str, unsigned* value );
   1.545 +    static bool	ToBool( const char* str, bool* value );
   1.546 +    static bool	ToFloat( const char* str, float* value );
   1.547 +    static bool ToDouble( const char* str, double* value );
   1.548 +};
   1.549 +
   1.550 +
   1.551 +/** XMLNode is a base class for every object that is in the
   1.552 +	XML Document Object Model (DOM), except XMLAttributes.
   1.553 +	Nodes have siblings, a parent, and children which can
   1.554 +	be navigated. A node is always in a XMLDocument.
   1.555 +	The type of a XMLNode can be queried, and it can
   1.556 +	be cast to its more defined type.
   1.557 +
   1.558 +	A XMLDocument allocates memory for all its Nodes.
   1.559 +	When the XMLDocument gets deleted, all its Nodes
   1.560 +	will also be deleted.
   1.561 +
   1.562 +	@verbatim
   1.563 +	A Document can contain:	Element	(container or leaf)
   1.564 +							Comment (leaf)
   1.565 +							Unknown (leaf)
   1.566 +							Declaration( leaf )
   1.567 +
   1.568 +	An Element can contain:	Element (container or leaf)
   1.569 +							Text	(leaf)
   1.570 +							Attributes (not on tree)
   1.571 +							Comment (leaf)
   1.572 +							Unknown (leaf)
   1.573 +
   1.574 +	@endverbatim
   1.575 +*/
   1.576 +class TINYXML2_LIB XMLNode
   1.577 +{
   1.578 +    friend class XMLDocument;
   1.579 +    friend class XMLElement;
   1.580 +public:
   1.581 +
   1.582 +    /// Get the XMLDocument that owns this XMLNode.
   1.583 +    const XMLDocument* GetDocument() const	{
   1.584 +        return _document;
   1.585 +    }
   1.586 +    /// Get the XMLDocument that owns this XMLNode.
   1.587 +    XMLDocument* GetDocument()				{
   1.588 +        return _document;
   1.589 +    }
   1.590 +
   1.591 +    /// Safely cast to an Element, or null.
   1.592 +    virtual XMLElement*		ToElement()		{
   1.593 +        return 0;
   1.594 +    }
   1.595 +    /// Safely cast to Text, or null.
   1.596 +    virtual XMLText*		ToText()		{
   1.597 +        return 0;
   1.598 +    }
   1.599 +    /// Safely cast to a Comment, or null.
   1.600 +    virtual XMLComment*		ToComment()		{
   1.601 +        return 0;
   1.602 +    }
   1.603 +    /// Safely cast to a Document, or null.
   1.604 +    virtual XMLDocument*	ToDocument()	{
   1.605 +        return 0;
   1.606 +    }
   1.607 +    /// Safely cast to a Declaration, or null.
   1.608 +    virtual XMLDeclaration*	ToDeclaration()	{
   1.609 +        return 0;
   1.610 +    }
   1.611 +    /// Safely cast to an Unknown, or null.
   1.612 +    virtual XMLUnknown*		ToUnknown()		{
   1.613 +        return 0;
   1.614 +    }
   1.615 +
   1.616 +    virtual const XMLElement*		ToElement() const		{
   1.617 +        return 0;
   1.618 +    }
   1.619 +    virtual const XMLText*			ToText() const			{
   1.620 +        return 0;
   1.621 +    }
   1.622 +    virtual const XMLComment*		ToComment() const		{
   1.623 +        return 0;
   1.624 +    }
   1.625 +    virtual const XMLDocument*		ToDocument() const		{
   1.626 +        return 0;
   1.627 +    }
   1.628 +    virtual const XMLDeclaration*	ToDeclaration() const	{
   1.629 +        return 0;
   1.630 +    }
   1.631 +    virtual const XMLUnknown*		ToUnknown() const		{
   1.632 +        return 0;
   1.633 +    }
   1.634 +
   1.635 +    /** The meaning of 'value' changes for the specific type.
   1.636 +    	@verbatim
   1.637 +    	Document:	empty
   1.638 +    	Element:	name of the element
   1.639 +    	Comment:	the comment text
   1.640 +    	Unknown:	the tag contents
   1.641 +    	Text:		the text string
   1.642 +    	@endverbatim
   1.643 +    */
   1.644 +    const char* Value() const			{
   1.645 +        return _value.GetStr();
   1.646 +    }
   1.647 +
   1.648 +    /** Set the Value of an XML node.
   1.649 +    	@sa Value()
   1.650 +    */
   1.651 +    void SetValue( const char* val, bool staticMem=false );
   1.652 +
   1.653 +    /// Get the parent of this node on the DOM.
   1.654 +    const XMLNode*	Parent() const			{
   1.655 +        return _parent;
   1.656 +    }
   1.657 +
   1.658 +    XMLNode* Parent()						{
   1.659 +        return _parent;
   1.660 +    }
   1.661 +
   1.662 +    /// Returns true if this node has no children.
   1.663 +    bool NoChildren() const					{
   1.664 +        return !_firstChild;
   1.665 +    }
   1.666 +
   1.667 +    /// Get the first child node, or null if none exists.
   1.668 +    const XMLNode*  FirstChild() const		{
   1.669 +        return _firstChild;
   1.670 +    }
   1.671 +
   1.672 +    XMLNode*		FirstChild()			{
   1.673 +        return _firstChild;
   1.674 +    }
   1.675 +
   1.676 +    /** Get the first child element, or optionally the first child
   1.677 +        element with the specified name.
   1.678 +    */
   1.679 +    const XMLElement* FirstChildElement( const char* value=0 ) const;
   1.680 +
   1.681 +    XMLElement* FirstChildElement( const char* value=0 )	{
   1.682 +        return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value ));
   1.683 +    }
   1.684 +
   1.685 +    /// Get the last child node, or null if none exists.
   1.686 +    const XMLNode*	LastChild() const						{
   1.687 +        return _lastChild;
   1.688 +    }
   1.689 +
   1.690 +    XMLNode*		LastChild()								{
   1.691 +        return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() );
   1.692 +    }
   1.693 +
   1.694 +    /** Get the last child element or optionally the last child
   1.695 +        element with the specified name.
   1.696 +    */
   1.697 +    const XMLElement* LastChildElement( const char* value=0 ) const;
   1.698 +
   1.699 +    XMLElement* LastChildElement( const char* value=0 )	{
   1.700 +        return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) );
   1.701 +    }
   1.702 +
   1.703 +    /// Get the previous (left) sibling node of this node.
   1.704 +    const XMLNode*	PreviousSibling() const					{
   1.705 +        return _prev;
   1.706 +    }
   1.707 +
   1.708 +    XMLNode*	PreviousSibling()							{
   1.709 +        return _prev;
   1.710 +    }
   1.711 +
   1.712 +    /// Get the previous (left) sibling element of this node, with an optionally supplied name.
   1.713 +    const XMLElement*	PreviousSiblingElement( const char* value=0 ) const ;
   1.714 +
   1.715 +    XMLElement*	PreviousSiblingElement( const char* value=0 ) {
   1.716 +        return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) );
   1.717 +    }
   1.718 +
   1.719 +    /// Get the next (right) sibling node of this node.
   1.720 +    const XMLNode*	NextSibling() const						{
   1.721 +        return _next;
   1.722 +    }
   1.723 +
   1.724 +    XMLNode*	NextSibling()								{
   1.725 +        return _next;
   1.726 +    }
   1.727 +
   1.728 +    /// Get the next (right) sibling element of this node, with an optionally supplied name.
   1.729 +    const XMLElement*	NextSiblingElement( const char* value=0 ) const;
   1.730 +
   1.731 +    XMLElement*	NextSiblingElement( const char* value=0 )	{
   1.732 +        return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) );
   1.733 +    }
   1.734 +
   1.735 +    /**
   1.736 +    	Add a child node as the last (right) child.
   1.737 +    */
   1.738 +    XMLNode* InsertEndChild( XMLNode* addThis );
   1.739 +
   1.740 +    XMLNode* LinkEndChild( XMLNode* addThis )	{
   1.741 +        return InsertEndChild( addThis );
   1.742 +    }
   1.743 +    /**
   1.744 +    	Add a child node as the first (left) child.
   1.745 +    */
   1.746 +    XMLNode* InsertFirstChild( XMLNode* addThis );
   1.747 +    /**
   1.748 +    	Add a node after the specified child node.
   1.749 +    */
   1.750 +    XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
   1.751 +
   1.752 +    /**
   1.753 +    	Delete all the children of this node.
   1.754 +    */
   1.755 +    void DeleteChildren();
   1.756 +
   1.757 +    /**
   1.758 +    	Delete a child of this node.
   1.759 +    */
   1.760 +    void DeleteChild( XMLNode* node );
   1.761 +
   1.762 +    /**
   1.763 +    	Make a copy of this node, but not its children.
   1.764 +    	You may pass in a Document pointer that will be
   1.765 +    	the owner of the new Node. If the 'document' is
   1.766 +    	null, then the node returned will be allocated
   1.767 +    	from the current Document. (this->GetDocument())
   1.768 +
   1.769 +    	Note: if called on a XMLDocument, this will return null.
   1.770 +    */
   1.771 +    virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
   1.772 +
   1.773 +    /**
   1.774 +    	Test if 2 nodes are the same, but don't test children.
   1.775 +    	The 2 nodes do not need to be in the same Document.
   1.776 +
   1.777 +    	Note: if called on a XMLDocument, this will return false.
   1.778 +    */
   1.779 +    virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
   1.780 +
   1.781 +    /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
   1.782 +    	XML tree will be conditionally visited and the host will be called back
   1.783 +    	via the XMLVisitor interface.
   1.784 +
   1.785 +    	This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
   1.786 +    	the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
   1.787 +    	interface versus any other.)
   1.788 +
   1.789 +    	The interface has been based on ideas from:
   1.790 +
   1.791 +    	- http://www.saxproject.org/
   1.792 +    	- http://c2.com/cgi/wiki?HierarchicalVisitorPattern
   1.793 +
   1.794 +    	Which are both good references for "visiting".
   1.795 +
   1.796 +    	An example of using Accept():
   1.797 +    	@verbatim
   1.798 +    	XMLPrinter printer;
   1.799 +    	tinyxmlDoc.Accept( &printer );
   1.800 +    	const char* xmlcstr = printer.CStr();
   1.801 +    	@endverbatim
   1.802 +    */
   1.803 +    virtual bool Accept( XMLVisitor* visitor ) const = 0;
   1.804 +
   1.805 +    // internal
   1.806 +    virtual char* ParseDeep( char*, StrPair* );
   1.807 +
   1.808 +protected:
   1.809 +    XMLNode( XMLDocument* );
   1.810 +    virtual ~XMLNode();
   1.811 +    XMLNode( const XMLNode& );	// not supported
   1.812 +    XMLNode& operator=( const XMLNode& );	// not supported
   1.813 +
   1.814 +    XMLDocument*	_document;
   1.815 +    XMLNode*		_parent;
   1.816 +    mutable StrPair	_value;
   1.817 +
   1.818 +    XMLNode*		_firstChild;
   1.819 +    XMLNode*		_lastChild;
   1.820 +
   1.821 +    XMLNode*		_prev;
   1.822 +    XMLNode*		_next;
   1.823 +
   1.824 +private:
   1.825 +    MemPool*		_memPool;
   1.826 +    void Unlink( XMLNode* child );
   1.827 +};
   1.828 +
   1.829 +
   1.830 +/** XML text.
   1.831 +
   1.832 +	Note that a text node can have child element nodes, for example:
   1.833 +	@verbatim
   1.834 +	<root>This is <b>bold</b></root>
   1.835 +	@endverbatim
   1.836 +
   1.837 +	A text node can have 2 ways to output the next. "normal" output
   1.838 +	and CDATA. It will default to the mode it was parsed from the XML file and
   1.839 +	you generally want to leave it alone, but you can change the output mode with
   1.840 +	SetCData() and query it with CData().
   1.841 +*/
   1.842 +class TINYXML2_LIB XMLText : public XMLNode
   1.843 +{
   1.844 +    friend class XMLBase;
   1.845 +    friend class XMLDocument;
   1.846 +public:
   1.847 +    virtual bool Accept( XMLVisitor* visitor ) const;
   1.848 +
   1.849 +    virtual XMLText* ToText()			{
   1.850 +        return this;
   1.851 +    }
   1.852 +    virtual const XMLText* ToText() const	{
   1.853 +        return this;
   1.854 +    }
   1.855 +
   1.856 +    /// Declare whether this should be CDATA or standard text.
   1.857 +    void SetCData( bool isCData )			{
   1.858 +        _isCData = isCData;
   1.859 +    }
   1.860 +    /// Returns true if this is a CDATA text element.
   1.861 +    bool CData() const						{
   1.862 +        return _isCData;
   1.863 +    }
   1.864 +
   1.865 +    char* ParseDeep( char*, StrPair* endTag );
   1.866 +    virtual XMLNode* ShallowClone( XMLDocument* document ) const;
   1.867 +    virtual bool ShallowEqual( const XMLNode* compare ) const;
   1.868 +
   1.869 +protected:
   1.870 +    XMLText( XMLDocument* doc )	: XMLNode( doc ), _isCData( false )	{}
   1.871 +    virtual ~XMLText()												{}
   1.872 +    XMLText( const XMLText& );	// not supported
   1.873 +    XMLText& operator=( const XMLText& );	// not supported
   1.874 +
   1.875 +private:
   1.876 +    bool _isCData;
   1.877 +};
   1.878 +
   1.879 +
   1.880 +/** An XML Comment. */
   1.881 +class TINYXML2_LIB XMLComment : public XMLNode
   1.882 +{
   1.883 +    friend class XMLDocument;
   1.884 +public:
   1.885 +    virtual XMLComment*	ToComment()					{
   1.886 +        return this;
   1.887 +    }
   1.888 +    virtual const XMLComment* ToComment() const		{
   1.889 +        return this;
   1.890 +    }
   1.891 +
   1.892 +    virtual bool Accept( XMLVisitor* visitor ) const;
   1.893 +
   1.894 +    char* ParseDeep( char*, StrPair* endTag );
   1.895 +    virtual XMLNode* ShallowClone( XMLDocument* document ) const;
   1.896 +    virtual bool ShallowEqual( const XMLNode* compare ) const;
   1.897 +
   1.898 +protected:
   1.899 +    XMLComment( XMLDocument* doc );
   1.900 +    virtual ~XMLComment();
   1.901 +    XMLComment( const XMLComment& );	// not supported
   1.902 +    XMLComment& operator=( const XMLComment& );	// not supported
   1.903 +
   1.904 +private:
   1.905 +};
   1.906 +
   1.907 +
   1.908 +/** In correct XML the declaration is the first entry in the file.
   1.909 +	@verbatim
   1.910 +		<?xml version="1.0" standalone="yes"?>
   1.911 +	@endverbatim
   1.912 +
   1.913 +	TinyXML-2 will happily read or write files without a declaration,
   1.914 +	however.
   1.915 +
   1.916 +	The text of the declaration isn't interpreted. It is parsed
   1.917 +	and written as a string.
   1.918 +*/
   1.919 +class TINYXML2_LIB XMLDeclaration : public XMLNode
   1.920 +{
   1.921 +    friend class XMLDocument;
   1.922 +public:
   1.923 +    virtual XMLDeclaration*	ToDeclaration()					{
   1.924 +        return this;
   1.925 +    }
   1.926 +    virtual const XMLDeclaration* ToDeclaration() const		{
   1.927 +        return this;
   1.928 +    }
   1.929 +
   1.930 +    virtual bool Accept( XMLVisitor* visitor ) const;
   1.931 +
   1.932 +    char* ParseDeep( char*, StrPair* endTag );
   1.933 +    virtual XMLNode* ShallowClone( XMLDocument* document ) const;
   1.934 +    virtual bool ShallowEqual( const XMLNode* compare ) const;
   1.935 +
   1.936 +protected:
   1.937 +    XMLDeclaration( XMLDocument* doc );
   1.938 +    virtual ~XMLDeclaration();
   1.939 +    XMLDeclaration( const XMLDeclaration& );	// not supported
   1.940 +    XMLDeclaration& operator=( const XMLDeclaration& );	// not supported
   1.941 +};
   1.942 +
   1.943 +
   1.944 +/** Any tag that TinyXML-2 doesn't recognize is saved as an
   1.945 +	unknown. It is a tag of text, but should not be modified.
   1.946 +	It will be written back to the XML, unchanged, when the file
   1.947 +	is saved.
   1.948 +
   1.949 +	DTD tags get thrown into XMLUnknowns.
   1.950 +*/
   1.951 +class TINYXML2_LIB XMLUnknown : public XMLNode
   1.952 +{
   1.953 +    friend class XMLDocument;
   1.954 +public:
   1.955 +    virtual XMLUnknown*	ToUnknown()					{
   1.956 +        return this;
   1.957 +    }
   1.958 +    virtual const XMLUnknown* ToUnknown() const		{
   1.959 +        return this;
   1.960 +    }
   1.961 +
   1.962 +    virtual bool Accept( XMLVisitor* visitor ) const;
   1.963 +
   1.964 +    char* ParseDeep( char*, StrPair* endTag );
   1.965 +    virtual XMLNode* ShallowClone( XMLDocument* document ) const;
   1.966 +    virtual bool ShallowEqual( const XMLNode* compare ) const;
   1.967 +
   1.968 +protected:
   1.969 +    XMLUnknown( XMLDocument* doc );
   1.970 +    virtual ~XMLUnknown();
   1.971 +    XMLUnknown( const XMLUnknown& );	// not supported
   1.972 +    XMLUnknown& operator=( const XMLUnknown& );	// not supported
   1.973 +};
   1.974 +
   1.975 +
   1.976 +enum XMLError {
   1.977 +    XML_NO_ERROR = 0,
   1.978 +    XML_SUCCESS = 0,
   1.979 +
   1.980 +    XML_NO_ATTRIBUTE,
   1.981 +    XML_WRONG_ATTRIBUTE_TYPE,
   1.982 +
   1.983 +    XML_ERROR_FILE_NOT_FOUND,
   1.984 +    XML_ERROR_FILE_COULD_NOT_BE_OPENED,
   1.985 +    XML_ERROR_FILE_READ_ERROR,
   1.986 +    XML_ERROR_ELEMENT_MISMATCH,
   1.987 +    XML_ERROR_PARSING_ELEMENT,
   1.988 +    XML_ERROR_PARSING_ATTRIBUTE,
   1.989 +    XML_ERROR_IDENTIFYING_TAG,
   1.990 +    XML_ERROR_PARSING_TEXT,
   1.991 +    XML_ERROR_PARSING_CDATA,
   1.992 +    XML_ERROR_PARSING_COMMENT,
   1.993 +    XML_ERROR_PARSING_DECLARATION,
   1.994 +    XML_ERROR_PARSING_UNKNOWN,
   1.995 +    XML_ERROR_EMPTY_DOCUMENT,
   1.996 +    XML_ERROR_MISMATCHED_ELEMENT,
   1.997 +    XML_ERROR_PARSING,
   1.998 +
   1.999 +    XML_CAN_NOT_CONVERT_TEXT,
  1.1000 +    XML_NO_TEXT_NODE
  1.1001 +};
  1.1002 +
  1.1003 +
  1.1004 +/** An attribute is a name-value pair. Elements have an arbitrary
  1.1005 +	number of attributes, each with a unique name.
  1.1006 +
  1.1007 +	@note The attributes are not XMLNodes. You may only query the
  1.1008 +	Next() attribute in a list.
  1.1009 +*/
  1.1010 +class TINYXML2_LIB XMLAttribute
  1.1011 +{
  1.1012 +    friend class XMLElement;
  1.1013 +public:
  1.1014 +    /// The name of the attribute.
  1.1015 +    const char* Name() const {
  1.1016 +        return _name.GetStr();
  1.1017 +    }
  1.1018 +    /// The value of the attribute.
  1.1019 +    const char* Value() const {
  1.1020 +        return _value.GetStr();
  1.1021 +    }
  1.1022 +    /// The next attribute in the list.
  1.1023 +    const XMLAttribute* Next() const {
  1.1024 +        return _next;
  1.1025 +    }
  1.1026 +
  1.1027 +    /** IntValue interprets the attribute as an integer, and returns the value.
  1.1028 +        If the value isn't an integer, 0 will be returned. There is no error checking;
  1.1029 +    	use QueryIntValue() if you need error checking.
  1.1030 +    */
  1.1031 +    int		 IntValue() const				{
  1.1032 +        int i=0;
  1.1033 +        QueryIntValue( &i );
  1.1034 +        return i;
  1.1035 +    }
  1.1036 +    /// Query as an unsigned integer. See IntValue()
  1.1037 +    unsigned UnsignedValue() const			{
  1.1038 +        unsigned i=0;
  1.1039 +        QueryUnsignedValue( &i );
  1.1040 +        return i;
  1.1041 +    }
  1.1042 +    /// Query as a boolean. See IntValue()
  1.1043 +    bool	 BoolValue() const				{
  1.1044 +        bool b=false;
  1.1045 +        QueryBoolValue( &b );
  1.1046 +        return b;
  1.1047 +    }
  1.1048 +    /// Query as a double. See IntValue()
  1.1049 +    double 	 DoubleValue() const			{
  1.1050 +        double d=0;
  1.1051 +        QueryDoubleValue( &d );
  1.1052 +        return d;
  1.1053 +    }
  1.1054 +    /// Query as a float. See IntValue()
  1.1055 +    float	 FloatValue() const				{
  1.1056 +        float f=0;
  1.1057 +        QueryFloatValue( &f );
  1.1058 +        return f;
  1.1059 +    }
  1.1060 +
  1.1061 +    /** QueryIntValue interprets the attribute as an integer, and returns the value
  1.1062 +    	in the provided parameter. The function will return XML_NO_ERROR on success,
  1.1063 +    	and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
  1.1064 +    */
  1.1065 +    XMLError QueryIntValue( int* value ) const;
  1.1066 +    /// See QueryIntValue
  1.1067 +    XMLError QueryUnsignedValue( unsigned int* value ) const;
  1.1068 +    /// See QueryIntValue
  1.1069 +    XMLError QueryBoolValue( bool* value ) const;
  1.1070 +    /// See QueryIntValue
  1.1071 +    XMLError QueryDoubleValue( double* value ) const;
  1.1072 +    /// See QueryIntValue
  1.1073 +    XMLError QueryFloatValue( float* value ) const;
  1.1074 +
  1.1075 +    /// Set the attribute to a string value.
  1.1076 +    void SetAttribute( const char* value );
  1.1077 +    /// Set the attribute to value.
  1.1078 +    void SetAttribute( int value );
  1.1079 +    /// Set the attribute to value.
  1.1080 +    void SetAttribute( unsigned value );
  1.1081 +    /// Set the attribute to value.
  1.1082 +    void SetAttribute( bool value );
  1.1083 +    /// Set the attribute to value.
  1.1084 +    void SetAttribute( double value );
  1.1085 +    /// Set the attribute to value.
  1.1086 +    void SetAttribute( float value );
  1.1087 +
  1.1088 +private:
  1.1089 +    enum { BUF_SIZE = 200 };
  1.1090 +
  1.1091 +    XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
  1.1092 +    virtual ~XMLAttribute()	{}
  1.1093 +
  1.1094 +    XMLAttribute( const XMLAttribute& );	// not supported
  1.1095 +    void operator=( const XMLAttribute& );	// not supported
  1.1096 +    void SetName( const char* name );
  1.1097 +
  1.1098 +    char* ParseDeep( char* p, bool processEntities );
  1.1099 +
  1.1100 +    mutable StrPair _name;
  1.1101 +    mutable StrPair _value;
  1.1102 +    XMLAttribute*   _next;
  1.1103 +    MemPool*        _memPool;
  1.1104 +};
  1.1105 +
  1.1106 +
  1.1107 +/** The element is a container class. It has a value, the element name,
  1.1108 +	and can contain other elements, text, comments, and unknowns.
  1.1109 +	Elements also contain an arbitrary number of attributes.
  1.1110 +*/
  1.1111 +class TINYXML2_LIB XMLElement : public XMLNode
  1.1112 +{
  1.1113 +    friend class XMLBase;
  1.1114 +    friend class XMLDocument;
  1.1115 +public:
  1.1116 +    /// Get the name of an element (which is the Value() of the node.)
  1.1117 +    const char* Name() const		{
  1.1118 +        return Value();
  1.1119 +    }
  1.1120 +    /// Set the name of the element.
  1.1121 +    void SetName( const char* str, bool staticMem=false )	{
  1.1122 +        SetValue( str, staticMem );
  1.1123 +    }
  1.1124 +
  1.1125 +    virtual XMLElement* ToElement()				{
  1.1126 +        return this;
  1.1127 +    }
  1.1128 +    virtual const XMLElement* ToElement() const {
  1.1129 +        return this;
  1.1130 +    }
  1.1131 +    virtual bool Accept( XMLVisitor* visitor ) const;
  1.1132 +
  1.1133 +    /** Given an attribute name, Attribute() returns the value
  1.1134 +    	for the attribute of that name, or null if none
  1.1135 +    	exists. For example:
  1.1136 +
  1.1137 +    	@verbatim
  1.1138 +    	const char* value = ele->Attribute( "foo" );
  1.1139 +    	@endverbatim
  1.1140 +
  1.1141 +    	The 'value' parameter is normally null. However, if specified,
  1.1142 +    	the attribute will only be returned if the 'name' and 'value'
  1.1143 +    	match. This allow you to write code:
  1.1144 +
  1.1145 +    	@verbatim
  1.1146 +    	if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
  1.1147 +    	@endverbatim
  1.1148 +
  1.1149 +    	rather than:
  1.1150 +    	@verbatim
  1.1151 +    	if ( ele->Attribute( "foo" ) ) {
  1.1152 +    		if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
  1.1153 +    	}
  1.1154 +    	@endverbatim
  1.1155 +    */
  1.1156 +    const char* Attribute( const char* name, const char* value=0 ) const;
  1.1157 +
  1.1158 +    /** Given an attribute name, IntAttribute() returns the value
  1.1159 +    	of the attribute interpreted as an integer. 0 will be
  1.1160 +    	returned if there is an error. For a method with error
  1.1161 +    	checking, see QueryIntAttribute()
  1.1162 +    */
  1.1163 +    int		 IntAttribute( const char* name ) const		{
  1.1164 +        int i=0;
  1.1165 +        QueryIntAttribute( name, &i );
  1.1166 +        return i;
  1.1167 +    }
  1.1168 +    /// See IntAttribute()
  1.1169 +    unsigned UnsignedAttribute( const char* name ) const {
  1.1170 +        unsigned i=0;
  1.1171 +        QueryUnsignedAttribute( name, &i );
  1.1172 +        return i;
  1.1173 +    }
  1.1174 +    /// See IntAttribute()
  1.1175 +    bool	 BoolAttribute( const char* name ) const	{
  1.1176 +        bool b=false;
  1.1177 +        QueryBoolAttribute( name, &b );
  1.1178 +        return b;
  1.1179 +    }
  1.1180 +    /// See IntAttribute()
  1.1181 +    double 	 DoubleAttribute( const char* name ) const	{
  1.1182 +        double d=0;
  1.1183 +        QueryDoubleAttribute( name, &d );
  1.1184 +        return d;
  1.1185 +    }
  1.1186 +    /// See IntAttribute()
  1.1187 +    float	 FloatAttribute( const char* name ) const	{
  1.1188 +        float f=0;
  1.1189 +        QueryFloatAttribute( name, &f );
  1.1190 +        return f;
  1.1191 +    }
  1.1192 +
  1.1193 +    /** Given an attribute name, QueryIntAttribute() returns
  1.1194 +    	XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
  1.1195 +    	can't be performed, or XML_NO_ATTRIBUTE if the attribute
  1.1196 +    	doesn't exist. If successful, the result of the conversion
  1.1197 +    	will be written to 'value'. If not successful, nothing will
  1.1198 +    	be written to 'value'. This allows you to provide default
  1.1199 +    	value:
  1.1200 +
  1.1201 +    	@verbatim
  1.1202 +    	int value = 10;
  1.1203 +    	QueryIntAttribute( "foo", &value );		// if "foo" isn't found, value will still be 10
  1.1204 +    	@endverbatim
  1.1205 +    */
  1.1206 +    XMLError QueryIntAttribute( const char* name, int* value ) const				{
  1.1207 +        const XMLAttribute* a = FindAttribute( name );
  1.1208 +        if ( !a ) {
  1.1209 +            return XML_NO_ATTRIBUTE;
  1.1210 +        }
  1.1211 +        return a->QueryIntValue( value );
  1.1212 +    }
  1.1213 +    /// See QueryIntAttribute()
  1.1214 +    XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const	{
  1.1215 +        const XMLAttribute* a = FindAttribute( name );
  1.1216 +        if ( !a ) {
  1.1217 +            return XML_NO_ATTRIBUTE;
  1.1218 +        }
  1.1219 +        return a->QueryUnsignedValue( value );
  1.1220 +    }
  1.1221 +    /// See QueryIntAttribute()
  1.1222 +    XMLError QueryBoolAttribute( const char* name, bool* value ) const				{
  1.1223 +        const XMLAttribute* a = FindAttribute( name );
  1.1224 +        if ( !a ) {
  1.1225 +            return XML_NO_ATTRIBUTE;
  1.1226 +        }
  1.1227 +        return a->QueryBoolValue( value );
  1.1228 +    }
  1.1229 +    /// See QueryIntAttribute()
  1.1230 +    XMLError QueryDoubleAttribute( const char* name, double* value ) const			{
  1.1231 +        const XMLAttribute* a = FindAttribute( name );
  1.1232 +        if ( !a ) {
  1.1233 +            return XML_NO_ATTRIBUTE;
  1.1234 +        }
  1.1235 +        return a->QueryDoubleValue( value );
  1.1236 +    }
  1.1237 +    /// See QueryIntAttribute()
  1.1238 +    XMLError QueryFloatAttribute( const char* name, float* value ) const			{
  1.1239 +        const XMLAttribute* a = FindAttribute( name );
  1.1240 +        if ( !a ) {
  1.1241 +            return XML_NO_ATTRIBUTE;
  1.1242 +        }
  1.1243 +        return a->QueryFloatValue( value );
  1.1244 +    }
  1.1245 +
  1.1246 +	
  1.1247 +    /** Given an attribute name, QueryAttribute() returns
  1.1248 +    	XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
  1.1249 +    	can't be performed, or XML_NO_ATTRIBUTE if the attribute
  1.1250 +    	doesn't exist. It is overloaded for the primitive types,
  1.1251 +		and is a generally more convenient replacement of
  1.1252 +		QueryIntAttribute() and related functions.
  1.1253 +		
  1.1254 +		If successful, the result of the conversion
  1.1255 +    	will be written to 'value'. If not successful, nothing will
  1.1256 +    	be written to 'value'. This allows you to provide default
  1.1257 +    	value:
  1.1258 +
  1.1259 +    	@verbatim
  1.1260 +    	int value = 10;
  1.1261 +    	QueryAttribute( "foo", &value );		// if "foo" isn't found, value will still be 10
  1.1262 +    	@endverbatim
  1.1263 +    */
  1.1264 +	int QueryAttribute( const char* name, int* value ) const {
  1.1265 +		return QueryIntAttribute( name, value );
  1.1266 +	}
  1.1267 +
  1.1268 +	int QueryAttribute( const char* name, unsigned int* value ) const {
  1.1269 +		return QueryUnsignedAttribute( name, value );
  1.1270 +	}
  1.1271 +
  1.1272 +	int QueryAttribute( const char* name, bool* value ) const {
  1.1273 +		return QueryBoolAttribute( name, value );
  1.1274 +	}
  1.1275 +
  1.1276 +	int QueryAttribute( const char* name, double* value ) const {
  1.1277 +		return QueryDoubleAttribute( name, value );
  1.1278 +	}
  1.1279 +
  1.1280 +	int QueryAttribute( const char* name, float* value ) const {
  1.1281 +		return QueryFloatAttribute( name, value );
  1.1282 +	}
  1.1283 +
  1.1284 +	/// Sets the named attribute to value.
  1.1285 +    void SetAttribute( const char* name, const char* value )	{
  1.1286 +        XMLAttribute* a = FindOrCreateAttribute( name );
  1.1287 +        a->SetAttribute( value );
  1.1288 +    }
  1.1289 +    /// Sets the named attribute to value.
  1.1290 +    void SetAttribute( const char* name, int value )			{
  1.1291 +        XMLAttribute* a = FindOrCreateAttribute( name );
  1.1292 +        a->SetAttribute( value );
  1.1293 +    }
  1.1294 +    /// Sets the named attribute to value.
  1.1295 +    void SetAttribute( const char* name, unsigned value )		{
  1.1296 +        XMLAttribute* a = FindOrCreateAttribute( name );
  1.1297 +        a->SetAttribute( value );
  1.1298 +    }
  1.1299 +    /// Sets the named attribute to value.
  1.1300 +    void SetAttribute( const char* name, bool value )			{
  1.1301 +        XMLAttribute* a = FindOrCreateAttribute( name );
  1.1302 +        a->SetAttribute( value );
  1.1303 +    }
  1.1304 +    /// Sets the named attribute to value.
  1.1305 +    void SetAttribute( const char* name, double value )		{
  1.1306 +        XMLAttribute* a = FindOrCreateAttribute( name );
  1.1307 +        a->SetAttribute( value );
  1.1308 +    }
  1.1309 +
  1.1310 +    /**
  1.1311 +    	Delete an attribute.
  1.1312 +    */
  1.1313 +    void DeleteAttribute( const char* name );
  1.1314 +
  1.1315 +    /// Return the first attribute in the list.
  1.1316 +    const XMLAttribute* FirstAttribute() const {
  1.1317 +        return _rootAttribute;
  1.1318 +    }
  1.1319 +    /// Query a specific attribute in the list.
  1.1320 +    const XMLAttribute* FindAttribute( const char* name ) const;
  1.1321 +
  1.1322 +    /** Convenience function for easy access to the text inside an element. Although easy
  1.1323 +    	and concise, GetText() is limited compared to getting the XMLText child
  1.1324 +    	and accessing it directly.
  1.1325 +
  1.1326 +    	If the first child of 'this' is a XMLText, the GetText()
  1.1327 +    	returns the character string of the Text node, else null is returned.
  1.1328 +
  1.1329 +    	This is a convenient method for getting the text of simple contained text:
  1.1330 +    	@verbatim
  1.1331 +    	<foo>This is text</foo>
  1.1332 +    		const char* str = fooElement->GetText();
  1.1333 +    	@endverbatim
  1.1334 +
  1.1335 +    	'str' will be a pointer to "This is text".
  1.1336 +
  1.1337 +    	Note that this function can be misleading. If the element foo was created from
  1.1338 +    	this XML:
  1.1339 +    	@verbatim
  1.1340 +    		<foo><b>This is text</b></foo>
  1.1341 +    	@endverbatim
  1.1342 +
  1.1343 +    	then the value of str would be null. The first child node isn't a text node, it is
  1.1344 +    	another element. From this XML:
  1.1345 +    	@verbatim
  1.1346 +    		<foo>This is <b>text</b></foo>
  1.1347 +    	@endverbatim
  1.1348 +    	GetText() will return "This is ".
  1.1349 +    */
  1.1350 +    const char* GetText() const;
  1.1351 +
  1.1352 +    /**
  1.1353 +    	Convenience method to query the value of a child text node. This is probably best
  1.1354 +    	shown by example. Given you have a document is this form:
  1.1355 +    	@verbatim
  1.1356 +    		<point>
  1.1357 +    			<x>1</x>
  1.1358 +    			<y>1.4</y>
  1.1359 +    		</point>
  1.1360 +    	@endverbatim
  1.1361 +
  1.1362 +    	The QueryIntText() and similar functions provide a safe and easier way to get to the
  1.1363 +    	"value" of x and y.
  1.1364 +
  1.1365 +    	@verbatim
  1.1366 +    		int x = 0;
  1.1367 +    		float y = 0;	// types of x and y are contrived for example
  1.1368 +    		const XMLElement* xElement = pointElement->FirstChildElement( "x" );
  1.1369 +    		const XMLElement* yElement = pointElement->FirstChildElement( "y" );
  1.1370 +    		xElement->QueryIntText( &x );
  1.1371 +    		yElement->QueryFloatText( &y );
  1.1372 +    	@endverbatim
  1.1373 +
  1.1374 +    	@returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
  1.1375 +    			 to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.
  1.1376 +
  1.1377 +    */
  1.1378 +    XMLError QueryIntText( int* ival ) const;
  1.1379 +    /// See QueryIntText()
  1.1380 +    XMLError QueryUnsignedText( unsigned* uval ) const;
  1.1381 +    /// See QueryIntText()
  1.1382 +    XMLError QueryBoolText( bool* bval ) const;
  1.1383 +    /// See QueryIntText()
  1.1384 +    XMLError QueryDoubleText( double* dval ) const;
  1.1385 +    /// See QueryIntText()
  1.1386 +    XMLError QueryFloatText( float* fval ) const;
  1.1387 +
  1.1388 +    // internal:
  1.1389 +    enum {
  1.1390 +        OPEN,		// <foo>
  1.1391 +        CLOSED,		// <foo/>
  1.1392 +        CLOSING		// </foo>
  1.1393 +    };
  1.1394 +    int ClosingType() const {
  1.1395 +        return _closingType;
  1.1396 +    }
  1.1397 +    char* ParseDeep( char* p, StrPair* endTag );
  1.1398 +    virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  1.1399 +    virtual bool ShallowEqual( const XMLNode* compare ) const;
  1.1400 +
  1.1401 +private:
  1.1402 +    XMLElement( XMLDocument* doc );
  1.1403 +    virtual ~XMLElement();
  1.1404 +    XMLElement( const XMLElement& );	// not supported
  1.1405 +    void operator=( const XMLElement& );	// not supported
  1.1406 +
  1.1407 +    XMLAttribute* FindAttribute( const char* name );
  1.1408 +    XMLAttribute* FindOrCreateAttribute( const char* name );
  1.1409 +    //void LinkAttribute( XMLAttribute* attrib );
  1.1410 +    char* ParseAttributes( char* p );
  1.1411 +
  1.1412 +    int _closingType;
  1.1413 +    // The attribute list is ordered; there is no 'lastAttribute'
  1.1414 +    // because the list needs to be scanned for dupes before adding
  1.1415 +    // a new attribute.
  1.1416 +    XMLAttribute* _rootAttribute;
  1.1417 +};
  1.1418 +
  1.1419 +
  1.1420 +enum Whitespace {
  1.1421 +    PRESERVE_WHITESPACE,
  1.1422 +    COLLAPSE_WHITESPACE
  1.1423 +};
  1.1424 +
  1.1425 +
  1.1426 +/** A Document binds together all the functionality.
  1.1427 +	It can be saved, loaded, and printed to the screen.
  1.1428 +	All Nodes are connected and allocated to a Document.
  1.1429 +	If the Document is deleted, all its Nodes are also deleted.
  1.1430 +*/
  1.1431 +class TINYXML2_LIB XMLDocument : public XMLNode
  1.1432 +{
  1.1433 +    friend class XMLElement;
  1.1434 +public:
  1.1435 +    /// constructor
  1.1436 +    XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
  1.1437 +    ~XMLDocument();
  1.1438 +
  1.1439 +    virtual XMLDocument* ToDocument()				{
  1.1440 +        return this;
  1.1441 +    }
  1.1442 +    virtual const XMLDocument* ToDocument() const	{
  1.1443 +        return this;
  1.1444 +    }
  1.1445 +
  1.1446 +    /**
  1.1447 +    	Parse an XML file from a character string.
  1.1448 +    	Returns XML_NO_ERROR (0) on success, or
  1.1449 +    	an errorID.
  1.1450 +
  1.1451 +    	You may optionally pass in the 'nBytes', which is
  1.1452 +    	the number of bytes which will be parsed. If not
  1.1453 +    	specified, TinyXML-2 will assume 'xml' points to a
  1.1454 +    	null terminated string.
  1.1455 +    */
  1.1456 +    XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
  1.1457 +
  1.1458 +    /**
  1.1459 +    	Load an XML file from disk.
  1.1460 +    	Returns XML_NO_ERROR (0) on success, or
  1.1461 +    	an errorID.
  1.1462 +    */
  1.1463 +    XMLError LoadFile( const char* filename );
  1.1464 +
  1.1465 +    /**
  1.1466 +    	Load an XML file from disk. You are responsible
  1.1467 +    	for providing and closing the FILE*.
  1.1468 +
  1.1469 +    	Returns XML_NO_ERROR (0) on success, or
  1.1470 +    	an errorID.
  1.1471 +    */
  1.1472 +    XMLError LoadFile( FILE* );
  1.1473 +
  1.1474 +    /**
  1.1475 +    	Save the XML file to disk.
  1.1476 +    	Returns XML_NO_ERROR (0) on success, or
  1.1477 +    	an errorID.
  1.1478 +    */
  1.1479 +    XMLError SaveFile( const char* filename, bool compact = false );
  1.1480 +
  1.1481 +    /**
  1.1482 +    	Save the XML file to disk. You are responsible
  1.1483 +    	for providing and closing the FILE*.
  1.1484 +
  1.1485 +    	Returns XML_NO_ERROR (0) on success, or
  1.1486 +    	an errorID.
  1.1487 +    */
  1.1488 +    XMLError SaveFile( FILE* fp, bool compact = false );
  1.1489 +
  1.1490 +    bool ProcessEntities() const		{
  1.1491 +        return _processEntities;
  1.1492 +    }
  1.1493 +    Whitespace WhitespaceMode() const	{
  1.1494 +        return _whitespace;
  1.1495 +    }
  1.1496 +
  1.1497 +    /**
  1.1498 +    	Returns true if this document has a leading Byte Order Mark of UTF8.
  1.1499 +    */
  1.1500 +    bool HasBOM() const {
  1.1501 +        return _writeBOM;
  1.1502 +    }
  1.1503 +    /** Sets whether to write the BOM when writing the file.
  1.1504 +    */
  1.1505 +    void SetBOM( bool useBOM ) {
  1.1506 +        _writeBOM = useBOM;
  1.1507 +    }
  1.1508 +
  1.1509 +    /** Return the root element of DOM. Equivalent to FirstChildElement().
  1.1510 +        To get the first node, use FirstChild().
  1.1511 +    */
  1.1512 +    XMLElement* RootElement()				{
  1.1513 +        return FirstChildElement();
  1.1514 +    }
  1.1515 +    const XMLElement* RootElement() const	{
  1.1516 +        return FirstChildElement();
  1.1517 +    }
  1.1518 +
  1.1519 +    /** Print the Document. If the Printer is not provided, it will
  1.1520 +        print to stdout. If you provide Printer, this can print to a file:
  1.1521 +    	@verbatim
  1.1522 +    	XMLPrinter printer( fp );
  1.1523 +    	doc.Print( &printer );
  1.1524 +    	@endverbatim
  1.1525 +
  1.1526 +    	Or you can use a printer to print to memory:
  1.1527 +    	@verbatim
  1.1528 +    	XMLPrinter printer;
  1.1529 +    	doc.Print( &printer );
  1.1530 +    	// printer.CStr() has a const char* to the XML
  1.1531 +    	@endverbatim
  1.1532 +    */
  1.1533 +    void Print( XMLPrinter* streamer=0 ) const;
  1.1534 +    virtual bool Accept( XMLVisitor* visitor ) const;
  1.1535 +
  1.1536 +    /**
  1.1537 +    	Create a new Element associated with
  1.1538 +    	this Document. The memory for the Element
  1.1539 +    	is managed by the Document.
  1.1540 +    */
  1.1541 +    XMLElement* NewElement( const char* name );
  1.1542 +    /**
  1.1543 +    	Create a new Comment associated with
  1.1544 +    	this Document. The memory for the Comment
  1.1545 +    	is managed by the Document.
  1.1546 +    */
  1.1547 +    XMLComment* NewComment( const char* comment );
  1.1548 +    /**
  1.1549 +    	Create a new Text associated with
  1.1550 +    	this Document. The memory for the Text
  1.1551 +    	is managed by the Document.
  1.1552 +    */
  1.1553 +    XMLText* NewText( const char* text );
  1.1554 +    /**
  1.1555 +    	Create a new Declaration associated with
  1.1556 +    	this Document. The memory for the object
  1.1557 +    	is managed by the Document.
  1.1558 +
  1.1559 +    	If the 'text' param is null, the standard
  1.1560 +    	declaration is used.:
  1.1561 +    	@verbatim
  1.1562 +    		<?xml version="1.0" encoding="UTF-8"?>
  1.1563 +    	@endverbatim
  1.1564 +    */
  1.1565 +    XMLDeclaration* NewDeclaration( const char* text=0 );
  1.1566 +    /**
  1.1567 +    	Create a new Unknown associated with
  1.1568 +    	this Document. The memory for the object
  1.1569 +    	is managed by the Document.
  1.1570 +    */
  1.1571 +    XMLUnknown* NewUnknown( const char* text );
  1.1572 +
  1.1573 +    /**
  1.1574 +    	Delete a node associated with this document.
  1.1575 +    	It will be unlinked from the DOM.
  1.1576 +    */
  1.1577 +    void DeleteNode( XMLNode* node )	{
  1.1578 +        node->_parent->DeleteChild( node );
  1.1579 +    }
  1.1580 +
  1.1581 +    void SetError( XMLError error, const char* str1, const char* str2 );
  1.1582 +
  1.1583 +    /// Return true if there was an error parsing the document.
  1.1584 +    bool Error() const {
  1.1585 +        return _errorID != XML_NO_ERROR;
  1.1586 +    }
  1.1587 +    /// Return the errorID.
  1.1588 +    XMLError  ErrorID() const {
  1.1589 +        return _errorID;
  1.1590 +    }
  1.1591 +    /// Return a possibly helpful diagnostic location or string.
  1.1592 +    const char* GetErrorStr1() const {
  1.1593 +        return _errorStr1;
  1.1594 +    }
  1.1595 +    /// Return a possibly helpful secondary diagnostic location or string.
  1.1596 +    const char* GetErrorStr2() const {
  1.1597 +        return _errorStr2;
  1.1598 +    }
  1.1599 +    /// If there is an error, print it to stdout.
  1.1600 +    void PrintError() const;
  1.1601 +    
  1.1602 +    /// Clear the document, resetting it to the initial state.
  1.1603 +    void Clear();
  1.1604 +
  1.1605 +    // internal
  1.1606 +    char* Identify( char* p, XMLNode** node );
  1.1607 +
  1.1608 +    virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const	{
  1.1609 +        return 0;
  1.1610 +    }
  1.1611 +    virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const	{
  1.1612 +        return false;
  1.1613 +    }
  1.1614 +
  1.1615 +private:
  1.1616 +    XMLDocument( const XMLDocument& );	// not supported
  1.1617 +    void operator=( const XMLDocument& );	// not supported
  1.1618 +
  1.1619 +    bool        _writeBOM;
  1.1620 +    bool        _processEntities;
  1.1621 +    XMLError    _errorID;
  1.1622 +    Whitespace  _whitespace;
  1.1623 +    const char* _errorStr1;
  1.1624 +    const char* _errorStr2;
  1.1625 +    char*       _charBuffer;
  1.1626 +
  1.1627 +    MemPoolT< sizeof(XMLElement) >	 _elementPool;
  1.1628 +    MemPoolT< sizeof(XMLAttribute) > _attributePool;
  1.1629 +    MemPoolT< sizeof(XMLText) >		 _textPool;
  1.1630 +    MemPoolT< sizeof(XMLComment) >	 _commentPool;
  1.1631 +};
  1.1632 +
  1.1633 +
  1.1634 +/**
  1.1635 +	A XMLHandle is a class that wraps a node pointer with null checks; this is
  1.1636 +	an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
  1.1637 +	DOM structure. It is a separate utility class.
  1.1638 +
  1.1639 +	Take an example:
  1.1640 +	@verbatim
  1.1641 +	<Document>
  1.1642 +		<Element attributeA = "valueA">
  1.1643 +			<Child attributeB = "value1" />
  1.1644 +			<Child attributeB = "value2" />
  1.1645 +		</Element>
  1.1646 +	</Document>
  1.1647 +	@endverbatim
  1.1648 +
  1.1649 +	Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
  1.1650 +	easy to write a *lot* of code that looks like:
  1.1651 +
  1.1652 +	@verbatim
  1.1653 +	XMLElement* root = document.FirstChildElement( "Document" );
  1.1654 +	if ( root )
  1.1655 +	{
  1.1656 +		XMLElement* element = root->FirstChildElement( "Element" );
  1.1657 +		if ( element )
  1.1658 +		{
  1.1659 +			XMLElement* child = element->FirstChildElement( "Child" );
  1.1660 +			if ( child )
  1.1661 +			{
  1.1662 +				XMLElement* child2 = child->NextSiblingElement( "Child" );
  1.1663 +				if ( child2 )
  1.1664 +				{
  1.1665 +					// Finally do something useful.
  1.1666 +	@endverbatim
  1.1667 +
  1.1668 +	And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
  1.1669 +	of such code. A XMLHandle checks for null pointers so it is perfectly safe
  1.1670 +	and correct to use:
  1.1671 +
  1.1672 +	@verbatim
  1.1673 +	XMLHandle docHandle( &document );
  1.1674 +	XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement();
  1.1675 +	if ( child2 )
  1.1676 +	{
  1.1677 +		// do something useful
  1.1678 +	@endverbatim
  1.1679 +
  1.1680 +	Which is MUCH more concise and useful.
  1.1681 +
  1.1682 +	It is also safe to copy handles - internally they are nothing more than node pointers.
  1.1683 +	@verbatim
  1.1684 +	XMLHandle handleCopy = handle;
  1.1685 +	@endverbatim
  1.1686 +
  1.1687 +	See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
  1.1688 +*/
  1.1689 +class TINYXML2_LIB XMLHandle
  1.1690 +{
  1.1691 +public:
  1.1692 +    /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
  1.1693 +    XMLHandle( XMLNode* node )												{
  1.1694 +        _node = node;
  1.1695 +    }
  1.1696 +    /// Create a handle from a node.
  1.1697 +    XMLHandle( XMLNode& node )												{
  1.1698 +        _node = &node;
  1.1699 +    }
  1.1700 +    /// Copy constructor
  1.1701 +    XMLHandle( const XMLHandle& ref )										{
  1.1702 +        _node = ref._node;
  1.1703 +    }
  1.1704 +    /// Assignment
  1.1705 +    XMLHandle& operator=( const XMLHandle& ref )							{
  1.1706 +        _node = ref._node;
  1.1707 +        return *this;
  1.1708 +    }
  1.1709 +
  1.1710 +    /// Get the first child of this handle.
  1.1711 +    XMLHandle FirstChild() 													{
  1.1712 +        return XMLHandle( _node ? _node->FirstChild() : 0 );
  1.1713 +    }
  1.1714 +    /// Get the first child element of this handle.
  1.1715 +    XMLHandle FirstChildElement( const char* value=0 )						{
  1.1716 +        return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 );
  1.1717 +    }
  1.1718 +    /// Get the last child of this handle.
  1.1719 +    XMLHandle LastChild()													{
  1.1720 +        return XMLHandle( _node ? _node->LastChild() : 0 );
  1.1721 +    }
  1.1722 +    /// Get the last child element of this handle.
  1.1723 +    XMLHandle LastChildElement( const char* _value=0 )						{
  1.1724 +        return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 );
  1.1725 +    }
  1.1726 +    /// Get the previous sibling of this handle.
  1.1727 +    XMLHandle PreviousSibling()												{
  1.1728 +        return XMLHandle( _node ? _node->PreviousSibling() : 0 );
  1.1729 +    }
  1.1730 +    /// Get the previous sibling element of this handle.
  1.1731 +    XMLHandle PreviousSiblingElement( const char* _value=0 )				{
  1.1732 +        return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
  1.1733 +    }
  1.1734 +    /// Get the next sibling of this handle.
  1.1735 +    XMLHandle NextSibling()													{
  1.1736 +        return XMLHandle( _node ? _node->NextSibling() : 0 );
  1.1737 +    }
  1.1738 +    /// Get the next sibling element of this handle.
  1.1739 +    XMLHandle NextSiblingElement( const char* _value=0 )					{
  1.1740 +        return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
  1.1741 +    }
  1.1742 +
  1.1743 +    /// Safe cast to XMLNode. This can return null.
  1.1744 +    XMLNode* ToNode()							{
  1.1745 +        return _node;
  1.1746 +    }
  1.1747 +    /// Safe cast to XMLElement. This can return null.
  1.1748 +    XMLElement* ToElement() 					{
  1.1749 +        return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
  1.1750 +    }
  1.1751 +    /// Safe cast to XMLText. This can return null.
  1.1752 +    XMLText* ToText() 							{
  1.1753 +        return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
  1.1754 +    }
  1.1755 +    /// Safe cast to XMLUnknown. This can return null.
  1.1756 +    XMLUnknown* ToUnknown() 					{
  1.1757 +        return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
  1.1758 +    }
  1.1759 +    /// Safe cast to XMLDeclaration. This can return null.
  1.1760 +    XMLDeclaration* ToDeclaration() 			{
  1.1761 +        return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
  1.1762 +    }
  1.1763 +
  1.1764 +private:
  1.1765 +    XMLNode* _node;
  1.1766 +};
  1.1767 +
  1.1768 +
  1.1769 +/**
  1.1770 +	A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
  1.1771 +	same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
  1.1772 +*/
  1.1773 +class TINYXML2_LIB XMLConstHandle
  1.1774 +{
  1.1775 +public:
  1.1776 +    XMLConstHandle( const XMLNode* node )											{
  1.1777 +        _node = node;
  1.1778 +    }
  1.1779 +    XMLConstHandle( const XMLNode& node )											{
  1.1780 +        _node = &node;
  1.1781 +    }
  1.1782 +    XMLConstHandle( const XMLConstHandle& ref )										{
  1.1783 +        _node = ref._node;
  1.1784 +    }
  1.1785 +
  1.1786 +    XMLConstHandle& operator=( const XMLConstHandle& ref )							{
  1.1787 +        _node = ref._node;
  1.1788 +        return *this;
  1.1789 +    }
  1.1790 +
  1.1791 +    const XMLConstHandle FirstChild() const											{
  1.1792 +        return XMLConstHandle( _node ? _node->FirstChild() : 0 );
  1.1793 +    }
  1.1794 +    const XMLConstHandle FirstChildElement( const char* value=0 ) const				{
  1.1795 +        return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
  1.1796 +    }
  1.1797 +    const XMLConstHandle LastChild()	const										{
  1.1798 +        return XMLConstHandle( _node ? _node->LastChild() : 0 );
  1.1799 +    }
  1.1800 +    const XMLConstHandle LastChildElement( const char* _value=0 ) const				{
  1.1801 +        return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
  1.1802 +    }
  1.1803 +    const XMLConstHandle PreviousSibling() const									{
  1.1804 +        return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
  1.1805 +    }
  1.1806 +    const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const		{
  1.1807 +        return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
  1.1808 +    }
  1.1809 +    const XMLConstHandle NextSibling() const										{
  1.1810 +        return XMLConstHandle( _node ? _node->NextSibling() : 0 );
  1.1811 +    }
  1.1812 +    const XMLConstHandle NextSiblingElement( const char* _value=0 ) const			{
  1.1813 +        return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
  1.1814 +    }
  1.1815 +
  1.1816 +
  1.1817 +    const XMLNode* ToNode() const				{
  1.1818 +        return _node;
  1.1819 +    }
  1.1820 +    const XMLElement* ToElement() const			{
  1.1821 +        return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
  1.1822 +    }
  1.1823 +    const XMLText* ToText() const				{
  1.1824 +        return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
  1.1825 +    }
  1.1826 +    const XMLUnknown* ToUnknown() const			{
  1.1827 +        return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
  1.1828 +    }
  1.1829 +    const XMLDeclaration* ToDeclaration() const	{
  1.1830 +        return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
  1.1831 +    }
  1.1832 +
  1.1833 +private:
  1.1834 +    const XMLNode* _node;
  1.1835 +};
  1.1836 +
  1.1837 +
  1.1838 +/**
  1.1839 +	Printing functionality. The XMLPrinter gives you more
  1.1840 +	options than the XMLDocument::Print() method.
  1.1841 +
  1.1842 +	It can:
  1.1843 +	-# Print to memory.
  1.1844 +	-# Print to a file you provide.
  1.1845 +	-# Print XML without a XMLDocument.
  1.1846 +
  1.1847 +	Print to Memory
  1.1848 +
  1.1849 +	@verbatim
  1.1850 +	XMLPrinter printer;
  1.1851 +	doc.Print( &printer );
  1.1852 +	SomeFunction( printer.CStr() );
  1.1853 +	@endverbatim
  1.1854 +
  1.1855 +	Print to a File
  1.1856 +
  1.1857 +	You provide the file pointer.
  1.1858 +	@verbatim
  1.1859 +	XMLPrinter printer( fp );
  1.1860 +	doc.Print( &printer );
  1.1861 +	@endverbatim
  1.1862 +
  1.1863 +	Print without a XMLDocument
  1.1864 +
  1.1865 +	When loading, an XML parser is very useful. However, sometimes
  1.1866 +	when saving, it just gets in the way. The code is often set up
  1.1867 +	for streaming, and constructing the DOM is just overhead.
  1.1868 +
  1.1869 +	The Printer supports the streaming case. The following code
  1.1870 +	prints out a trivially simple XML file without ever creating
  1.1871 +	an XML document.
  1.1872 +
  1.1873 +	@verbatim
  1.1874 +	XMLPrinter printer( fp );
  1.1875 +	printer.OpenElement( "foo" );
  1.1876 +	printer.PushAttribute( "foo", "bar" );
  1.1877 +	printer.CloseElement();
  1.1878 +	@endverbatim
  1.1879 +*/
  1.1880 +class TINYXML2_LIB XMLPrinter : public XMLVisitor
  1.1881 +{
  1.1882 +public:
  1.1883 +    /** Construct the printer. If the FILE* is specified,
  1.1884 +    	this will print to the FILE. Else it will print
  1.1885 +    	to memory, and the result is available in CStr().
  1.1886 +    	If 'compact' is set to true, then output is created
  1.1887 +    	with only required whitespace and newlines.
  1.1888 +    */
  1.1889 +    XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
  1.1890 +    ~XMLPrinter()	{}
  1.1891 +
  1.1892 +    /** If streaming, write the BOM and declaration. */
  1.1893 +    void PushHeader( bool writeBOM, bool writeDeclaration );
  1.1894 +    /** If streaming, start writing an element.
  1.1895 +        The element must be closed with CloseElement()
  1.1896 +    */
  1.1897 +    void OpenElement( const char* name );
  1.1898 +    /// If streaming, add an attribute to an open element.
  1.1899 +    void PushAttribute( const char* name, const char* value );
  1.1900 +    void PushAttribute( const char* name, int value );
  1.1901 +    void PushAttribute( const char* name, unsigned value );
  1.1902 +    void PushAttribute( const char* name, bool value );
  1.1903 +    void PushAttribute( const char* name, double value );
  1.1904 +    /// If streaming, close the Element.
  1.1905 +    void CloseElement();
  1.1906 +
  1.1907 +    /// Add a text node.
  1.1908 +    void PushText( const char* text, bool cdata=false );
  1.1909 +    /// Add a text node from an integer.
  1.1910 +    void PushText( int value );
  1.1911 +    /// Add a text node from an unsigned.
  1.1912 +    void PushText( unsigned value );
  1.1913 +    /// Add a text node from a bool.
  1.1914 +    void PushText( bool value );
  1.1915 +    /// Add a text node from a float.
  1.1916 +    void PushText( float value );
  1.1917 +    /// Add a text node from a double.
  1.1918 +    void PushText( double value );
  1.1919 +
  1.1920 +    /// Add a comment
  1.1921 +    void PushComment( const char* comment );
  1.1922 +
  1.1923 +    void PushDeclaration( const char* value );
  1.1924 +    void PushUnknown( const char* value );
  1.1925 +
  1.1926 +    virtual bool VisitEnter( const XMLDocument& /*doc*/ );
  1.1927 +    virtual bool VisitExit( const XMLDocument& /*doc*/ )			{
  1.1928 +        return true;
  1.1929 +    }
  1.1930 +
  1.1931 +    virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
  1.1932 +    virtual bool VisitExit( const XMLElement& element );
  1.1933 +
  1.1934 +    virtual bool Visit( const XMLText& text );
  1.1935 +    virtual bool Visit( const XMLComment& comment );
  1.1936 +    virtual bool Visit( const XMLDeclaration& declaration );
  1.1937 +    virtual bool Visit( const XMLUnknown& unknown );
  1.1938 +
  1.1939 +    /**
  1.1940 +    	If in print to memory mode, return a pointer to
  1.1941 +    	the XML file in memory.
  1.1942 +    */
  1.1943 +    const char* CStr() const {
  1.1944 +        return _buffer.Mem();
  1.1945 +    }
  1.1946 +    /**
  1.1947 +    	If in print to memory mode, return the size
  1.1948 +    	of the XML file in memory. (Note the size returned
  1.1949 +    	includes the terminating null.)
  1.1950 +    */
  1.1951 +    int CStrSize() const {
  1.1952 +        return _buffer.Size();
  1.1953 +    }
  1.1954 +
  1.1955 +private:
  1.1956 +    void SealElement();
  1.1957 +    void PrintSpace( int depth );
  1.1958 +    void PrintString( const char*, bool restrictedEntitySet );	// prints out, after detecting entities.
  1.1959 +    void Print( const char* format, ... );
  1.1960 +
  1.1961 +    bool _elementJustOpened;
  1.1962 +    bool _firstElement;
  1.1963 +    FILE* _fp;
  1.1964 +    int _depth;
  1.1965 +    int _textDepth;
  1.1966 +    bool _processEntities;
  1.1967 +    bool _compactMode;
  1.1968 +
  1.1969 +    enum {
  1.1970 +        ENTITY_RANGE = 64,
  1.1971 +        BUF_SIZE = 200
  1.1972 +    };
  1.1973 +    bool _entityFlag[ENTITY_RANGE];
  1.1974 +    bool _restrictedEntityFlag[ENTITY_RANGE];
  1.1975 +
  1.1976 +    DynArray< const char*, 10 > _stack;
  1.1977 +    DynArray< char, 20 > _buffer;
  1.1978 +#ifdef _MSC_VER
  1.1979 +    DynArray< char, 20 > _accumulator;
  1.1980 +#endif
  1.1981 +};
  1.1982 +
  1.1983 +
  1.1984 +}	// tinyxml2
  1.1985 +
  1.1986 +#if defined(_MSC_VER)
  1.1987 +#   pragma warning(pop)
  1.1988 +#endif
  1.1989 +
  1.1990 +#endif // TINYXML2_INCLUDED