goat3d

annotate libs/tinyxml2/tinyxml2.h @ 51:fa5c52ea9d59

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Jan 2014 18:16:09 +0200
parents
children 9862541fdcf5
rev   line source
nuclear@19 1 /*
nuclear@19 2 Original code by Lee Thomason (www.grinninglizard.com)
nuclear@19 3
nuclear@19 4 This software is provided 'as-is', without any express or implied
nuclear@19 5 warranty. In no event will the authors be held liable for any
nuclear@19 6 damages arising from the use of this software.
nuclear@19 7
nuclear@19 8 Permission is granted to anyone to use this software for any
nuclear@19 9 purpose, including commercial applications, and to alter it and
nuclear@19 10 redistribute it freely, subject to the following restrictions:
nuclear@19 11
nuclear@19 12 1. The origin of this software must not be misrepresented; you must
nuclear@19 13 not claim that you wrote the original software. If you use this
nuclear@19 14 software in a product, an acknowledgment in the product documentation
nuclear@19 15 would be appreciated but is not required.
nuclear@19 16
nuclear@19 17
nuclear@19 18 2. Altered source versions must be plainly marked as such, and
nuclear@19 19 must not be misrepresented as being the original software.
nuclear@19 20
nuclear@19 21 3. This notice may not be removed or altered from any source
nuclear@19 22 distribution.
nuclear@19 23 */
nuclear@19 24
nuclear@19 25 #ifndef TINYXML2_INCLUDED
nuclear@19 26 #define TINYXML2_INCLUDED
nuclear@19 27
nuclear@19 28 #if defined(ANDROID_NDK) || defined(__BORLANDC__)
nuclear@19 29 # include <ctype.h>
nuclear@19 30 # include <limits.h>
nuclear@19 31 # include <stdio.h>
nuclear@19 32 # include <stdlib.h>
nuclear@19 33 # include <string.h>
nuclear@19 34 # include <stdarg.h>
nuclear@19 35 #else
nuclear@19 36 # include <cctype>
nuclear@19 37 # include <climits>
nuclear@19 38 # include <cstdio>
nuclear@19 39 # include <cstdlib>
nuclear@19 40 # include <cstring>
nuclear@19 41 # include <cstdarg>
nuclear@19 42 #endif
nuclear@19 43
nuclear@19 44 /*
nuclear@19 45 TODO: intern strings instead of allocation.
nuclear@19 46 */
nuclear@19 47 /*
nuclear@19 48 gcc:
nuclear@19 49 g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
nuclear@19 50
nuclear@19 51 Formatting, Artistic Style:
nuclear@19 52 AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
nuclear@19 53 */
nuclear@19 54
nuclear@19 55 #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
nuclear@19 56 # ifndef DEBUG
nuclear@19 57 # define DEBUG
nuclear@19 58 # endif
nuclear@19 59 #endif
nuclear@19 60
nuclear@19 61 #ifdef _MSC_VER
nuclear@19 62 # pragma warning(push)
nuclear@19 63 # pragma warning(disable: 4251)
nuclear@19 64 #endif
nuclear@19 65
nuclear@19 66 #ifdef _WIN32
nuclear@19 67 # ifdef TINYXML2_EXPORT
nuclear@19 68 # define TINYXML2_LIB __declspec(dllexport)
nuclear@19 69 # elif defined(TINYXML2_IMPORT)
nuclear@19 70 # define TINYXML2_LIB __declspec(dllimport)
nuclear@19 71 # else
nuclear@19 72 # define TINYXML2_LIB
nuclear@19 73 # endif
nuclear@19 74 #else
nuclear@19 75 # define TINYXML2_LIB
nuclear@19 76 #endif
nuclear@19 77
nuclear@19 78
nuclear@19 79 #if defined(DEBUG)
nuclear@19 80 # if defined(_MSC_VER)
nuclear@19 81 # define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
nuclear@19 82 # elif defined (ANDROID_NDK)
nuclear@19 83 # include <android/log.h>
nuclear@19 84 # define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
nuclear@19 85 # else
nuclear@19 86 # include <assert.h>
nuclear@19 87 # define TIXMLASSERT assert
nuclear@19 88 # endif
nuclear@19 89 # else
nuclear@19 90 # define TIXMLASSERT( x ) {}
nuclear@19 91 #endif
nuclear@19 92
nuclear@19 93
nuclear@19 94 #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
nuclear@19 95 // Microsoft visual studio, version 2005 and higher.
nuclear@19 96 /*int _snprintf_s(
nuclear@19 97 char *buffer,
nuclear@19 98 size_t sizeOfBuffer,
nuclear@19 99 size_t count,
nuclear@19 100 const char *format [,
nuclear@19 101 argument] ...
nuclear@19 102 );*/
nuclear@19 103 inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
nuclear@19 104 {
nuclear@19 105 va_list va;
nuclear@19 106 va_start( va, format );
nuclear@19 107 int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
nuclear@19 108 va_end( va );
nuclear@19 109 return result;
nuclear@19 110 }
nuclear@19 111 #define TIXML_SSCANF sscanf_s
nuclear@19 112 #else
nuclear@19 113 // GCC version 3 and higher
nuclear@19 114 //#warning( "Using sn* functions." )
nuclear@19 115 #define TIXML_SNPRINTF snprintf
nuclear@19 116 #define TIXML_SSCANF sscanf
nuclear@19 117 #endif
nuclear@19 118
nuclear@19 119 static const int TIXML2_MAJOR_VERSION = 1;
nuclear@19 120 static const int TIXML2_MINOR_VERSION = 0;
nuclear@19 121 static const int TIXML2_PATCH_VERSION = 11;
nuclear@19 122
nuclear@19 123 namespace tinyxml2
nuclear@19 124 {
nuclear@19 125 class XMLDocument;
nuclear@19 126 class XMLElement;
nuclear@19 127 class XMLAttribute;
nuclear@19 128 class XMLComment;
nuclear@19 129 class XMLText;
nuclear@19 130 class XMLDeclaration;
nuclear@19 131 class XMLUnknown;
nuclear@19 132 class XMLPrinter;
nuclear@19 133
nuclear@19 134 /*
nuclear@19 135 A class that wraps strings. Normally stores the start and end
nuclear@19 136 pointers into the XML file itself, and will apply normalization
nuclear@19 137 and entity translation if actually read. Can also store (and memory
nuclear@19 138 manage) a traditional char[]
nuclear@19 139 */
nuclear@19 140 class StrPair
nuclear@19 141 {
nuclear@19 142 public:
nuclear@19 143 enum {
nuclear@19 144 NEEDS_ENTITY_PROCESSING = 0x01,
nuclear@19 145 NEEDS_NEWLINE_NORMALIZATION = 0x02,
nuclear@19 146 COLLAPSE_WHITESPACE = 0x04,
nuclear@19 147
nuclear@19 148 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
nuclear@19 149 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
nuclear@19 150 ATTRIBUTE_NAME = 0,
nuclear@19 151 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
nuclear@19 152 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
nuclear@19 153 COMMENT = NEEDS_NEWLINE_NORMALIZATION
nuclear@19 154 };
nuclear@19 155
nuclear@19 156 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
nuclear@19 157 ~StrPair();
nuclear@19 158
nuclear@19 159 void Set( char* start, char* end, int flags ) {
nuclear@19 160 Reset();
nuclear@19 161 _start = start;
nuclear@19 162 _end = end;
nuclear@19 163 _flags = flags | NEEDS_FLUSH;
nuclear@19 164 }
nuclear@19 165
nuclear@19 166 const char* GetStr();
nuclear@19 167
nuclear@19 168 bool Empty() const {
nuclear@19 169 return _start == _end;
nuclear@19 170 }
nuclear@19 171
nuclear@19 172 void SetInternedStr( const char* str ) {
nuclear@19 173 Reset();
nuclear@19 174 _start = const_cast<char*>(str);
nuclear@19 175 }
nuclear@19 176
nuclear@19 177 void SetStr( const char* str, int flags=0 );
nuclear@19 178
nuclear@19 179 char* ParseText( char* in, const char* endTag, int strFlags );
nuclear@19 180 char* ParseName( char* in );
nuclear@19 181
nuclear@19 182 private:
nuclear@19 183 void Reset();
nuclear@19 184 void CollapseWhitespace();
nuclear@19 185
nuclear@19 186 enum {
nuclear@19 187 NEEDS_FLUSH = 0x100,
nuclear@19 188 NEEDS_DELETE = 0x200
nuclear@19 189 };
nuclear@19 190
nuclear@19 191 // After parsing, if *_end != 0, it can be set to zero.
nuclear@19 192 int _flags;
nuclear@19 193 char* _start;
nuclear@19 194 char* _end;
nuclear@19 195 };
nuclear@19 196
nuclear@19 197
nuclear@19 198 /*
nuclear@19 199 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
nuclear@19 200 Has a small initial memory pool, so that low or no usage will not
nuclear@19 201 cause a call to new/delete
nuclear@19 202 */
nuclear@19 203 template <class T, int INIT>
nuclear@19 204 class DynArray
nuclear@19 205 {
nuclear@19 206 public:
nuclear@19 207 DynArray< T, INIT >() {
nuclear@19 208 _mem = _pool;
nuclear@19 209 _allocated = INIT;
nuclear@19 210 _size = 0;
nuclear@19 211 }
nuclear@19 212
nuclear@19 213 ~DynArray() {
nuclear@19 214 if ( _mem != _pool ) {
nuclear@19 215 delete [] _mem;
nuclear@19 216 }
nuclear@19 217 }
nuclear@19 218
nuclear@19 219 void Push( T t ) {
nuclear@19 220 EnsureCapacity( _size+1 );
nuclear@19 221 _mem[_size++] = t;
nuclear@19 222 }
nuclear@19 223
nuclear@19 224 T* PushArr( int count ) {
nuclear@19 225 EnsureCapacity( _size+count );
nuclear@19 226 T* ret = &_mem[_size];
nuclear@19 227 _size += count;
nuclear@19 228 return ret;
nuclear@19 229 }
nuclear@19 230
nuclear@19 231 T Pop() {
nuclear@19 232 return _mem[--_size];
nuclear@19 233 }
nuclear@19 234
nuclear@19 235 void PopArr( int count ) {
nuclear@19 236 TIXMLASSERT( _size >= count );
nuclear@19 237 _size -= count;
nuclear@19 238 }
nuclear@19 239
nuclear@19 240 bool Empty() const {
nuclear@19 241 return _size == 0;
nuclear@19 242 }
nuclear@19 243
nuclear@19 244 T& operator[](int i) {
nuclear@19 245 TIXMLASSERT( i>= 0 && i < _size );
nuclear@19 246 return _mem[i];
nuclear@19 247 }
nuclear@19 248
nuclear@19 249 const T& operator[](int i) const {
nuclear@19 250 TIXMLASSERT( i>= 0 && i < _size );
nuclear@19 251 return _mem[i];
nuclear@19 252 }
nuclear@19 253
nuclear@19 254 int Size() const {
nuclear@19 255 return _size;
nuclear@19 256 }
nuclear@19 257
nuclear@19 258 int Capacity() const {
nuclear@19 259 return _allocated;
nuclear@19 260 }
nuclear@19 261
nuclear@19 262 const T* Mem() const {
nuclear@19 263 return _mem;
nuclear@19 264 }
nuclear@19 265
nuclear@19 266 T* Mem() {
nuclear@19 267 return _mem;
nuclear@19 268 }
nuclear@19 269
nuclear@19 270 private:
nuclear@19 271 void EnsureCapacity( int cap ) {
nuclear@19 272 if ( cap > _allocated ) {
nuclear@19 273 int newAllocated = cap * 2;
nuclear@19 274 T* newMem = new T[newAllocated];
nuclear@19 275 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
nuclear@19 276 if ( _mem != _pool ) {
nuclear@19 277 delete [] _mem;
nuclear@19 278 }
nuclear@19 279 _mem = newMem;
nuclear@19 280 _allocated = newAllocated;
nuclear@19 281 }
nuclear@19 282 }
nuclear@19 283
nuclear@19 284 T* _mem;
nuclear@19 285 T _pool[INIT];
nuclear@19 286 int _allocated; // objects allocated
nuclear@19 287 int _size; // number objects in use
nuclear@19 288 };
nuclear@19 289
nuclear@19 290
nuclear@19 291 /*
nuclear@19 292 Parent virtual class of a pool for fast allocation
nuclear@19 293 and deallocation of objects.
nuclear@19 294 */
nuclear@19 295 class MemPool
nuclear@19 296 {
nuclear@19 297 public:
nuclear@19 298 MemPool() {}
nuclear@19 299 virtual ~MemPool() {}
nuclear@19 300
nuclear@19 301 virtual int ItemSize() const = 0;
nuclear@19 302 virtual void* Alloc() = 0;
nuclear@19 303 virtual void Free( void* ) = 0;
nuclear@19 304 virtual void SetTracked() = 0;
nuclear@19 305 };
nuclear@19 306
nuclear@19 307
nuclear@19 308 /*
nuclear@19 309 Template child class to create pools of the correct type.
nuclear@19 310 */
nuclear@19 311 template< int SIZE >
nuclear@19 312 class MemPoolT : public MemPool
nuclear@19 313 {
nuclear@19 314 public:
nuclear@19 315 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
nuclear@19 316 ~MemPoolT() {
nuclear@19 317 // Delete the blocks.
nuclear@19 318 for( int i=0; i<_blockPtrs.Size(); ++i ) {
nuclear@19 319 delete _blockPtrs[i];
nuclear@19 320 }
nuclear@19 321 }
nuclear@19 322
nuclear@19 323 virtual int ItemSize() const {
nuclear@19 324 return SIZE;
nuclear@19 325 }
nuclear@19 326 int CurrentAllocs() const {
nuclear@19 327 return _currentAllocs;
nuclear@19 328 }
nuclear@19 329
nuclear@19 330 virtual void* Alloc() {
nuclear@19 331 if ( !_root ) {
nuclear@19 332 // Need a new block.
nuclear@19 333 Block* block = new Block();
nuclear@19 334 _blockPtrs.Push( block );
nuclear@19 335
nuclear@19 336 for( int i=0; i<COUNT-1; ++i ) {
nuclear@19 337 block->chunk[i].next = &block->chunk[i+1];
nuclear@19 338 }
nuclear@19 339 block->chunk[COUNT-1].next = 0;
nuclear@19 340 _root = block->chunk;
nuclear@19 341 }
nuclear@19 342 void* result = _root;
nuclear@19 343 _root = _root->next;
nuclear@19 344
nuclear@19 345 ++_currentAllocs;
nuclear@19 346 if ( _currentAllocs > _maxAllocs ) {
nuclear@19 347 _maxAllocs = _currentAllocs;
nuclear@19 348 }
nuclear@19 349 _nAllocs++;
nuclear@19 350 _nUntracked++;
nuclear@19 351 return result;
nuclear@19 352 }
nuclear@19 353 virtual void Free( void* mem ) {
nuclear@19 354 if ( !mem ) {
nuclear@19 355 return;
nuclear@19 356 }
nuclear@19 357 --_currentAllocs;
nuclear@19 358 Chunk* chunk = (Chunk*)mem;
nuclear@19 359 #ifdef DEBUG
nuclear@19 360 memset( chunk, 0xfe, sizeof(Chunk) );
nuclear@19 361 #endif
nuclear@19 362 chunk->next = _root;
nuclear@19 363 _root = chunk;
nuclear@19 364 }
nuclear@19 365 void Trace( const char* name ) {
nuclear@19 366 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
nuclear@19 367 name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
nuclear@19 368 }
nuclear@19 369
nuclear@19 370 void SetTracked() {
nuclear@19 371 _nUntracked--;
nuclear@19 372 }
nuclear@19 373
nuclear@19 374 int Untracked() const {
nuclear@19 375 return _nUntracked;
nuclear@19 376 }
nuclear@19 377
nuclear@19 378 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
nuclear@19 379 // The test file is large, 170k.
nuclear@19 380 // Release: VS2010 gcc(no opt)
nuclear@19 381 // 1k: 4000
nuclear@19 382 // 2k: 4000
nuclear@19 383 // 4k: 3900 21000
nuclear@19 384 // 16k: 5200
nuclear@19 385 // 32k: 4300
nuclear@19 386 // 64k: 4000 21000
nuclear@19 387 enum { COUNT = (4*1024)/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private
nuclear@19 388
nuclear@19 389 private:
nuclear@19 390 union Chunk {
nuclear@19 391 Chunk* next;
nuclear@19 392 char mem[SIZE];
nuclear@19 393 };
nuclear@19 394 struct Block {
nuclear@19 395 Chunk chunk[COUNT];
nuclear@19 396 };
nuclear@19 397 DynArray< Block*, 10 > _blockPtrs;
nuclear@19 398 Chunk* _root;
nuclear@19 399
nuclear@19 400 int _currentAllocs;
nuclear@19 401 int _nAllocs;
nuclear@19 402 int _maxAllocs;
nuclear@19 403 int _nUntracked;
nuclear@19 404 };
nuclear@19 405
nuclear@19 406
nuclear@19 407
nuclear@19 408 /**
nuclear@19 409 Implements the interface to the "Visitor pattern" (see the Accept() method.)
nuclear@19 410 If you call the Accept() method, it requires being passed a XMLVisitor
nuclear@19 411 class to handle callbacks. For nodes that contain other nodes (Document, Element)
nuclear@19 412 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
nuclear@19 413 are simply called with Visit().
nuclear@19 414
nuclear@19 415 If you return 'true' from a Visit method, recursive parsing will continue. If you return
nuclear@19 416 false, <b>no children of this node or its siblings</b> will be visited.
nuclear@19 417
nuclear@19 418 All flavors of Visit methods have a default implementation that returns 'true' (continue
nuclear@19 419 visiting). You need to only override methods that are interesting to you.
nuclear@19 420
nuclear@19 421 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
nuclear@19 422
nuclear@19 423 You should never change the document from a callback.
nuclear@19 424
nuclear@19 425 @sa XMLNode::Accept()
nuclear@19 426 */
nuclear@19 427 class TINYXML2_LIB XMLVisitor
nuclear@19 428 {
nuclear@19 429 public:
nuclear@19 430 virtual ~XMLVisitor() {}
nuclear@19 431
nuclear@19 432 /// Visit a document.
nuclear@19 433 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
nuclear@19 434 return true;
nuclear@19 435 }
nuclear@19 436 /// Visit a document.
nuclear@19 437 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
nuclear@19 438 return true;
nuclear@19 439 }
nuclear@19 440
nuclear@19 441 /// Visit an element.
nuclear@19 442 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
nuclear@19 443 return true;
nuclear@19 444 }
nuclear@19 445 /// Visit an element.
nuclear@19 446 virtual bool VisitExit( const XMLElement& /*element*/ ) {
nuclear@19 447 return true;
nuclear@19 448 }
nuclear@19 449
nuclear@19 450 /// Visit a declaration.
nuclear@19 451 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
nuclear@19 452 return true;
nuclear@19 453 }
nuclear@19 454 /// Visit a text node.
nuclear@19 455 virtual bool Visit( const XMLText& /*text*/ ) {
nuclear@19 456 return true;
nuclear@19 457 }
nuclear@19 458 /// Visit a comment node.
nuclear@19 459 virtual bool Visit( const XMLComment& /*comment*/ ) {
nuclear@19 460 return true;
nuclear@19 461 }
nuclear@19 462 /// Visit an unknown node.
nuclear@19 463 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
nuclear@19 464 return true;
nuclear@19 465 }
nuclear@19 466 };
nuclear@19 467
nuclear@19 468
nuclear@19 469 /*
nuclear@19 470 Utility functionality.
nuclear@19 471 */
nuclear@19 472 class XMLUtil
nuclear@19 473 {
nuclear@19 474 public:
nuclear@19 475 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
nuclear@19 476 // correct, but simple, and usually works.
nuclear@19 477 static const char* SkipWhiteSpace( const char* p ) {
nuclear@19 478 while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<const unsigned char*>(p) ) ) {
nuclear@19 479 ++p;
nuclear@19 480 }
nuclear@19 481 return p;
nuclear@19 482 }
nuclear@19 483 static char* SkipWhiteSpace( char* p ) {
nuclear@19 484 while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<unsigned char*>(p) ) ) {
nuclear@19 485 ++p;
nuclear@19 486 }
nuclear@19 487 return p;
nuclear@19 488 }
nuclear@19 489 static bool IsWhiteSpace( char p ) {
nuclear@19 490 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
nuclear@19 491 }
nuclear@19 492
nuclear@19 493 inline static bool IsNameStartChar( unsigned char ch ) {
nuclear@19 494 return ( ( ch < 128 ) ? isalpha( ch ) : 1 )
nuclear@19 495 || ch == ':'
nuclear@19 496 || ch == '_';
nuclear@19 497 }
nuclear@19 498
nuclear@19 499 inline static bool IsNameChar( unsigned char ch ) {
nuclear@19 500 return IsNameStartChar( ch )
nuclear@19 501 || isdigit( ch )
nuclear@19 502 || ch == '.'
nuclear@19 503 || ch == '-';
nuclear@19 504 }
nuclear@19 505
nuclear@19 506 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
nuclear@19 507 int n = 0;
nuclear@19 508 if ( p == q ) {
nuclear@19 509 return true;
nuclear@19 510 }
nuclear@19 511 while( *p && *q && *p == *q && n<nChar ) {
nuclear@19 512 ++p;
nuclear@19 513 ++q;
nuclear@19 514 ++n;
nuclear@19 515 }
nuclear@19 516 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
nuclear@19 517 return true;
nuclear@19 518 }
nuclear@19 519 return false;
nuclear@19 520 }
nuclear@19 521
nuclear@19 522 inline static int IsUTF8Continuation( const char p ) {
nuclear@19 523 return p & 0x80;
nuclear@19 524 }
nuclear@19 525
nuclear@19 526 static const char* ReadBOM( const char* p, bool* hasBOM );
nuclear@19 527 // p is the starting location,
nuclear@19 528 // the UTF-8 value of the entity will be placed in value, and length filled in.
nuclear@19 529 static const char* GetCharacterRef( const char* p, char* value, int* length );
nuclear@19 530 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
nuclear@19 531
nuclear@19 532 // converts primitive types to strings
nuclear@19 533 static void ToStr( int v, char* buffer, int bufferSize );
nuclear@19 534 static void ToStr( unsigned v, char* buffer, int bufferSize );
nuclear@19 535 static void ToStr( bool v, char* buffer, int bufferSize );
nuclear@19 536 static void ToStr( float v, char* buffer, int bufferSize );
nuclear@19 537 static void ToStr( double v, char* buffer, int bufferSize );
nuclear@19 538
nuclear@19 539 // converts strings to primitive types
nuclear@19 540 static bool ToInt( const char* str, int* value );
nuclear@19 541 static bool ToUnsigned( const char* str, unsigned* value );
nuclear@19 542 static bool ToBool( const char* str, bool* value );
nuclear@19 543 static bool ToFloat( const char* str, float* value );
nuclear@19 544 static bool ToDouble( const char* str, double* value );
nuclear@19 545 };
nuclear@19 546
nuclear@19 547
nuclear@19 548 /** XMLNode is a base class for every object that is in the
nuclear@19 549 XML Document Object Model (DOM), except XMLAttributes.
nuclear@19 550 Nodes have siblings, a parent, and children which can
nuclear@19 551 be navigated. A node is always in a XMLDocument.
nuclear@19 552 The type of a XMLNode can be queried, and it can
nuclear@19 553 be cast to its more defined type.
nuclear@19 554
nuclear@19 555 A XMLDocument allocates memory for all its Nodes.
nuclear@19 556 When the XMLDocument gets deleted, all its Nodes
nuclear@19 557 will also be deleted.
nuclear@19 558
nuclear@19 559 @verbatim
nuclear@19 560 A Document can contain: Element (container or leaf)
nuclear@19 561 Comment (leaf)
nuclear@19 562 Unknown (leaf)
nuclear@19 563 Declaration( leaf )
nuclear@19 564
nuclear@19 565 An Element can contain: Element (container or leaf)
nuclear@19 566 Text (leaf)
nuclear@19 567 Attributes (not on tree)
nuclear@19 568 Comment (leaf)
nuclear@19 569 Unknown (leaf)
nuclear@19 570
nuclear@19 571 @endverbatim
nuclear@19 572 */
nuclear@19 573 class TINYXML2_LIB XMLNode
nuclear@19 574 {
nuclear@19 575 friend class XMLDocument;
nuclear@19 576 friend class XMLElement;
nuclear@19 577 public:
nuclear@19 578
nuclear@19 579 /// Get the XMLDocument that owns this XMLNode.
nuclear@19 580 const XMLDocument* GetDocument() const {
nuclear@19 581 return _document;
nuclear@19 582 }
nuclear@19 583 /// Get the XMLDocument that owns this XMLNode.
nuclear@19 584 XMLDocument* GetDocument() {
nuclear@19 585 return _document;
nuclear@19 586 }
nuclear@19 587
nuclear@19 588 /// Safely cast to an Element, or null.
nuclear@19 589 virtual XMLElement* ToElement() {
nuclear@19 590 return 0;
nuclear@19 591 }
nuclear@19 592 /// Safely cast to Text, or null.
nuclear@19 593 virtual XMLText* ToText() {
nuclear@19 594 return 0;
nuclear@19 595 }
nuclear@19 596 /// Safely cast to a Comment, or null.
nuclear@19 597 virtual XMLComment* ToComment() {
nuclear@19 598 return 0;
nuclear@19 599 }
nuclear@19 600 /// Safely cast to a Document, or null.
nuclear@19 601 virtual XMLDocument* ToDocument() {
nuclear@19 602 return 0;
nuclear@19 603 }
nuclear@19 604 /// Safely cast to a Declaration, or null.
nuclear@19 605 virtual XMLDeclaration* ToDeclaration() {
nuclear@19 606 return 0;
nuclear@19 607 }
nuclear@19 608 /// Safely cast to an Unknown, or null.
nuclear@19 609 virtual XMLUnknown* ToUnknown() {
nuclear@19 610 return 0;
nuclear@19 611 }
nuclear@19 612
nuclear@19 613 virtual const XMLElement* ToElement() const {
nuclear@19 614 return 0;
nuclear@19 615 }
nuclear@19 616 virtual const XMLText* ToText() const {
nuclear@19 617 return 0;
nuclear@19 618 }
nuclear@19 619 virtual const XMLComment* ToComment() const {
nuclear@19 620 return 0;
nuclear@19 621 }
nuclear@19 622 virtual const XMLDocument* ToDocument() const {
nuclear@19 623 return 0;
nuclear@19 624 }
nuclear@19 625 virtual const XMLDeclaration* ToDeclaration() const {
nuclear@19 626 return 0;
nuclear@19 627 }
nuclear@19 628 virtual const XMLUnknown* ToUnknown() const {
nuclear@19 629 return 0;
nuclear@19 630 }
nuclear@19 631
nuclear@19 632 /** The meaning of 'value' changes for the specific type.
nuclear@19 633 @verbatim
nuclear@19 634 Document: empty
nuclear@19 635 Element: name of the element
nuclear@19 636 Comment: the comment text
nuclear@19 637 Unknown: the tag contents
nuclear@19 638 Text: the text string
nuclear@19 639 @endverbatim
nuclear@19 640 */
nuclear@19 641 const char* Value() const {
nuclear@19 642 return _value.GetStr();
nuclear@19 643 }
nuclear@19 644
nuclear@19 645 /** Set the Value of an XML node.
nuclear@19 646 @sa Value()
nuclear@19 647 */
nuclear@19 648 void SetValue( const char* val, bool staticMem=false );
nuclear@19 649
nuclear@19 650 /// Get the parent of this node on the DOM.
nuclear@19 651 const XMLNode* Parent() const {
nuclear@19 652 return _parent;
nuclear@19 653 }
nuclear@19 654
nuclear@19 655 XMLNode* Parent() {
nuclear@19 656 return _parent;
nuclear@19 657 }
nuclear@19 658
nuclear@19 659 /// Returns true if this node has no children.
nuclear@19 660 bool NoChildren() const {
nuclear@19 661 return !_firstChild;
nuclear@19 662 }
nuclear@19 663
nuclear@19 664 /// Get the first child node, or null if none exists.
nuclear@19 665 const XMLNode* FirstChild() const {
nuclear@19 666 return _firstChild;
nuclear@19 667 }
nuclear@19 668
nuclear@19 669 XMLNode* FirstChild() {
nuclear@19 670 return _firstChild;
nuclear@19 671 }
nuclear@19 672
nuclear@19 673 /** Get the first child element, or optionally the first child
nuclear@19 674 element with the specified name.
nuclear@19 675 */
nuclear@19 676 const XMLElement* FirstChildElement( const char* value=0 ) const;
nuclear@19 677
nuclear@19 678 XMLElement* FirstChildElement( const char* value=0 ) {
nuclear@19 679 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value ));
nuclear@19 680 }
nuclear@19 681
nuclear@19 682 /// Get the last child node, or null if none exists.
nuclear@19 683 const XMLNode* LastChild() const {
nuclear@19 684 return _lastChild;
nuclear@19 685 }
nuclear@19 686
nuclear@19 687 XMLNode* LastChild() {
nuclear@19 688 return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() );
nuclear@19 689 }
nuclear@19 690
nuclear@19 691 /** Get the last child element or optionally the last child
nuclear@19 692 element with the specified name.
nuclear@19 693 */
nuclear@19 694 const XMLElement* LastChildElement( const char* value=0 ) const;
nuclear@19 695
nuclear@19 696 XMLElement* LastChildElement( const char* value=0 ) {
nuclear@19 697 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) );
nuclear@19 698 }
nuclear@19 699
nuclear@19 700 /// Get the previous (left) sibling node of this node.
nuclear@19 701 const XMLNode* PreviousSibling() const {
nuclear@19 702 return _prev;
nuclear@19 703 }
nuclear@19 704
nuclear@19 705 XMLNode* PreviousSibling() {
nuclear@19 706 return _prev;
nuclear@19 707 }
nuclear@19 708
nuclear@19 709 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
nuclear@19 710 const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
nuclear@19 711
nuclear@19 712 XMLElement* PreviousSiblingElement( const char* value=0 ) {
nuclear@19 713 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) );
nuclear@19 714 }
nuclear@19 715
nuclear@19 716 /// Get the next (right) sibling node of this node.
nuclear@19 717 const XMLNode* NextSibling() const {
nuclear@19 718 return _next;
nuclear@19 719 }
nuclear@19 720
nuclear@19 721 XMLNode* NextSibling() {
nuclear@19 722 return _next;
nuclear@19 723 }
nuclear@19 724
nuclear@19 725 /// Get the next (right) sibling element of this node, with an optionally supplied name.
nuclear@19 726 const XMLElement* NextSiblingElement( const char* value=0 ) const;
nuclear@19 727
nuclear@19 728 XMLElement* NextSiblingElement( const char* value=0 ) {
nuclear@19 729 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) );
nuclear@19 730 }
nuclear@19 731
nuclear@19 732 /**
nuclear@19 733 Add a child node as the last (right) child.
nuclear@19 734 */
nuclear@19 735 XMLNode* InsertEndChild( XMLNode* addThis );
nuclear@19 736
nuclear@19 737 XMLNode* LinkEndChild( XMLNode* addThis ) {
nuclear@19 738 return InsertEndChild( addThis );
nuclear@19 739 }
nuclear@19 740 /**
nuclear@19 741 Add a child node as the first (left) child.
nuclear@19 742 */
nuclear@19 743 XMLNode* InsertFirstChild( XMLNode* addThis );
nuclear@19 744 /**
nuclear@19 745 Add a node after the specified child node.
nuclear@19 746 */
nuclear@19 747 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
nuclear@19 748
nuclear@19 749 /**
nuclear@19 750 Delete all the children of this node.
nuclear@19 751 */
nuclear@19 752 void DeleteChildren();
nuclear@19 753
nuclear@19 754 /**
nuclear@19 755 Delete a child of this node.
nuclear@19 756 */
nuclear@19 757 void DeleteChild( XMLNode* node );
nuclear@19 758
nuclear@19 759 /**
nuclear@19 760 Make a copy of this node, but not its children.
nuclear@19 761 You may pass in a Document pointer that will be
nuclear@19 762 the owner of the new Node. If the 'document' is
nuclear@19 763 null, then the node returned will be allocated
nuclear@19 764 from the current Document. (this->GetDocument())
nuclear@19 765
nuclear@19 766 Note: if called on a XMLDocument, this will return null.
nuclear@19 767 */
nuclear@19 768 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
nuclear@19 769
nuclear@19 770 /**
nuclear@19 771 Test if 2 nodes are the same, but don't test children.
nuclear@19 772 The 2 nodes do not need to be in the same Document.
nuclear@19 773
nuclear@19 774 Note: if called on a XMLDocument, this will return false.
nuclear@19 775 */
nuclear@19 776 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
nuclear@19 777
nuclear@19 778 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
nuclear@19 779 XML tree will be conditionally visited and the host will be called back
nuclear@19 780 via the XMLVisitor interface.
nuclear@19 781
nuclear@19 782 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
nuclear@19 783 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
nuclear@19 784 interface versus any other.)
nuclear@19 785
nuclear@19 786 The interface has been based on ideas from:
nuclear@19 787
nuclear@19 788 - http://www.saxproject.org/
nuclear@19 789 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
nuclear@19 790
nuclear@19 791 Which are both good references for "visiting".
nuclear@19 792
nuclear@19 793 An example of using Accept():
nuclear@19 794 @verbatim
nuclear@19 795 XMLPrinter printer;
nuclear@19 796 tinyxmlDoc.Accept( &printer );
nuclear@19 797 const char* xmlcstr = printer.CStr();
nuclear@19 798 @endverbatim
nuclear@19 799 */
nuclear@19 800 virtual bool Accept( XMLVisitor* visitor ) const = 0;
nuclear@19 801
nuclear@19 802 // internal
nuclear@19 803 virtual char* ParseDeep( char*, StrPair* );
nuclear@19 804
nuclear@19 805 protected:
nuclear@19 806 XMLNode( XMLDocument* );
nuclear@19 807 virtual ~XMLNode();
nuclear@19 808 XMLNode( const XMLNode& ); // not supported
nuclear@19 809 XMLNode& operator=( const XMLNode& ); // not supported
nuclear@19 810
nuclear@19 811 XMLDocument* _document;
nuclear@19 812 XMLNode* _parent;
nuclear@19 813 mutable StrPair _value;
nuclear@19 814
nuclear@19 815 XMLNode* _firstChild;
nuclear@19 816 XMLNode* _lastChild;
nuclear@19 817
nuclear@19 818 XMLNode* _prev;
nuclear@19 819 XMLNode* _next;
nuclear@19 820
nuclear@19 821 private:
nuclear@19 822 MemPool* _memPool;
nuclear@19 823 void Unlink( XMLNode* child );
nuclear@19 824 };
nuclear@19 825
nuclear@19 826
nuclear@19 827 /** XML text.
nuclear@19 828
nuclear@19 829 Note that a text node can have child element nodes, for example:
nuclear@19 830 @verbatim
nuclear@19 831 <root>This is <b>bold</b></root>
nuclear@19 832 @endverbatim
nuclear@19 833
nuclear@19 834 A text node can have 2 ways to output the next. "normal" output
nuclear@19 835 and CDATA. It will default to the mode it was parsed from the XML file and
nuclear@19 836 you generally want to leave it alone, but you can change the output mode with
nuclear@19 837 SetCData() and query it with CData().
nuclear@19 838 */
nuclear@19 839 class TINYXML2_LIB XMLText : public XMLNode
nuclear@19 840 {
nuclear@19 841 friend class XMLBase;
nuclear@19 842 friend class XMLDocument;
nuclear@19 843 public:
nuclear@19 844 virtual bool Accept( XMLVisitor* visitor ) const;
nuclear@19 845
nuclear@19 846 virtual XMLText* ToText() {
nuclear@19 847 return this;
nuclear@19 848 }
nuclear@19 849 virtual const XMLText* ToText() const {
nuclear@19 850 return this;
nuclear@19 851 }
nuclear@19 852
nuclear@19 853 /// Declare whether this should be CDATA or standard text.
nuclear@19 854 void SetCData( bool isCData ) {
nuclear@19 855 _isCData = isCData;
nuclear@19 856 }
nuclear@19 857 /// Returns true if this is a CDATA text element.
nuclear@19 858 bool CData() const {
nuclear@19 859 return _isCData;
nuclear@19 860 }
nuclear@19 861
nuclear@19 862 char* ParseDeep( char*, StrPair* endTag );
nuclear@19 863 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
nuclear@19 864 virtual bool ShallowEqual( const XMLNode* compare ) const;
nuclear@19 865
nuclear@19 866 protected:
nuclear@19 867 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
nuclear@19 868 virtual ~XMLText() {}
nuclear@19 869 XMLText( const XMLText& ); // not supported
nuclear@19 870 XMLText& operator=( const XMLText& ); // not supported
nuclear@19 871
nuclear@19 872 private:
nuclear@19 873 bool _isCData;
nuclear@19 874 };
nuclear@19 875
nuclear@19 876
nuclear@19 877 /** An XML Comment. */
nuclear@19 878 class TINYXML2_LIB XMLComment : public XMLNode
nuclear@19 879 {
nuclear@19 880 friend class XMLDocument;
nuclear@19 881 public:
nuclear@19 882 virtual XMLComment* ToComment() {
nuclear@19 883 return this;
nuclear@19 884 }
nuclear@19 885 virtual const XMLComment* ToComment() const {
nuclear@19 886 return this;
nuclear@19 887 }
nuclear@19 888
nuclear@19 889 virtual bool Accept( XMLVisitor* visitor ) const;
nuclear@19 890
nuclear@19 891 char* ParseDeep( char*, StrPair* endTag );
nuclear@19 892 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
nuclear@19 893 virtual bool ShallowEqual( const XMLNode* compare ) const;
nuclear@19 894
nuclear@19 895 protected:
nuclear@19 896 XMLComment( XMLDocument* doc );
nuclear@19 897 virtual ~XMLComment();
nuclear@19 898 XMLComment( const XMLComment& ); // not supported
nuclear@19 899 XMLComment& operator=( const XMLComment& ); // not supported
nuclear@19 900
nuclear@19 901 private:
nuclear@19 902 };
nuclear@19 903
nuclear@19 904
nuclear@19 905 /** In correct XML the declaration is the first entry in the file.
nuclear@19 906 @verbatim
nuclear@19 907 <?xml version="1.0" standalone="yes"?>
nuclear@19 908 @endverbatim
nuclear@19 909
nuclear@19 910 TinyXML-2 will happily read or write files without a declaration,
nuclear@19 911 however.
nuclear@19 912
nuclear@19 913 The text of the declaration isn't interpreted. It is parsed
nuclear@19 914 and written as a string.
nuclear@19 915 */
nuclear@19 916 class TINYXML2_LIB XMLDeclaration : public XMLNode
nuclear@19 917 {
nuclear@19 918 friend class XMLDocument;
nuclear@19 919 public:
nuclear@19 920 virtual XMLDeclaration* ToDeclaration() {
nuclear@19 921 return this;
nuclear@19 922 }
nuclear@19 923 virtual const XMLDeclaration* ToDeclaration() const {
nuclear@19 924 return this;
nuclear@19 925 }
nuclear@19 926
nuclear@19 927 virtual bool Accept( XMLVisitor* visitor ) const;
nuclear@19 928
nuclear@19 929 char* ParseDeep( char*, StrPair* endTag );
nuclear@19 930 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
nuclear@19 931 virtual bool ShallowEqual( const XMLNode* compare ) const;
nuclear@19 932
nuclear@19 933 protected:
nuclear@19 934 XMLDeclaration( XMLDocument* doc );
nuclear@19 935 virtual ~XMLDeclaration();
nuclear@19 936 XMLDeclaration( const XMLDeclaration& ); // not supported
nuclear@19 937 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
nuclear@19 938 };
nuclear@19 939
nuclear@19 940
nuclear@19 941 /** Any tag that TinyXML-2 doesn't recognize is saved as an
nuclear@19 942 unknown. It is a tag of text, but should not be modified.
nuclear@19 943 It will be written back to the XML, unchanged, when the file
nuclear@19 944 is saved.
nuclear@19 945
nuclear@19 946 DTD tags get thrown into XMLUnknowns.
nuclear@19 947 */
nuclear@19 948 class TINYXML2_LIB XMLUnknown : public XMLNode
nuclear@19 949 {
nuclear@19 950 friend class XMLDocument;
nuclear@19 951 public:
nuclear@19 952 virtual XMLUnknown* ToUnknown() {
nuclear@19 953 return this;
nuclear@19 954 }
nuclear@19 955 virtual const XMLUnknown* ToUnknown() const {
nuclear@19 956 return this;
nuclear@19 957 }
nuclear@19 958
nuclear@19 959 virtual bool Accept( XMLVisitor* visitor ) const;
nuclear@19 960
nuclear@19 961 char* ParseDeep( char*, StrPair* endTag );
nuclear@19 962 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
nuclear@19 963 virtual bool ShallowEqual( const XMLNode* compare ) const;
nuclear@19 964
nuclear@19 965 protected:
nuclear@19 966 XMLUnknown( XMLDocument* doc );
nuclear@19 967 virtual ~XMLUnknown();
nuclear@19 968 XMLUnknown( const XMLUnknown& ); // not supported
nuclear@19 969 XMLUnknown& operator=( const XMLUnknown& ); // not supported
nuclear@19 970 };
nuclear@19 971
nuclear@19 972
nuclear@19 973 enum XMLError {
nuclear@19 974 XML_NO_ERROR = 0,
nuclear@19 975 XML_SUCCESS = 0,
nuclear@19 976
nuclear@19 977 XML_NO_ATTRIBUTE,
nuclear@19 978 XML_WRONG_ATTRIBUTE_TYPE,
nuclear@19 979
nuclear@19 980 XML_ERROR_FILE_NOT_FOUND,
nuclear@19 981 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
nuclear@19 982 XML_ERROR_FILE_READ_ERROR,
nuclear@19 983 XML_ERROR_ELEMENT_MISMATCH,
nuclear@19 984 XML_ERROR_PARSING_ELEMENT,
nuclear@19 985 XML_ERROR_PARSING_ATTRIBUTE,
nuclear@19 986 XML_ERROR_IDENTIFYING_TAG,
nuclear@19 987 XML_ERROR_PARSING_TEXT,
nuclear@19 988 XML_ERROR_PARSING_CDATA,
nuclear@19 989 XML_ERROR_PARSING_COMMENT,
nuclear@19 990 XML_ERROR_PARSING_DECLARATION,
nuclear@19 991 XML_ERROR_PARSING_UNKNOWN,
nuclear@19 992 XML_ERROR_EMPTY_DOCUMENT,
nuclear@19 993 XML_ERROR_MISMATCHED_ELEMENT,
nuclear@19 994 XML_ERROR_PARSING,
nuclear@19 995
nuclear@19 996 XML_CAN_NOT_CONVERT_TEXT,
nuclear@19 997 XML_NO_TEXT_NODE
nuclear@19 998 };
nuclear@19 999
nuclear@19 1000
nuclear@19 1001 /** An attribute is a name-value pair. Elements have an arbitrary
nuclear@19 1002 number of attributes, each with a unique name.
nuclear@19 1003
nuclear@19 1004 @note The attributes are not XMLNodes. You may only query the
nuclear@19 1005 Next() attribute in a list.
nuclear@19 1006 */
nuclear@19 1007 class TINYXML2_LIB XMLAttribute
nuclear@19 1008 {
nuclear@19 1009 friend class XMLElement;
nuclear@19 1010 public:
nuclear@19 1011 /// The name of the attribute.
nuclear@19 1012 const char* Name() const {
nuclear@19 1013 return _name.GetStr();
nuclear@19 1014 }
nuclear@19 1015 /// The value of the attribute.
nuclear@19 1016 const char* Value() const {
nuclear@19 1017 return _value.GetStr();
nuclear@19 1018 }
nuclear@19 1019 /// The next attribute in the list.
nuclear@19 1020 const XMLAttribute* Next() const {
nuclear@19 1021 return _next;
nuclear@19 1022 }
nuclear@19 1023
nuclear@19 1024 /** IntValue interprets the attribute as an integer, and returns the value.
nuclear@19 1025 If the value isn't an integer, 0 will be returned. There is no error checking;
nuclear@19 1026 use QueryIntValue() if you need error checking.
nuclear@19 1027 */
nuclear@19 1028 int IntValue() const {
nuclear@19 1029 int i=0;
nuclear@19 1030 QueryIntValue( &i );
nuclear@19 1031 return i;
nuclear@19 1032 }
nuclear@19 1033 /// Query as an unsigned integer. See IntValue()
nuclear@19 1034 unsigned UnsignedValue() const {
nuclear@19 1035 unsigned i=0;
nuclear@19 1036 QueryUnsignedValue( &i );
nuclear@19 1037 return i;
nuclear@19 1038 }
nuclear@19 1039 /// Query as a boolean. See IntValue()
nuclear@19 1040 bool BoolValue() const {
nuclear@19 1041 bool b=false;
nuclear@19 1042 QueryBoolValue( &b );
nuclear@19 1043 return b;
nuclear@19 1044 }
nuclear@19 1045 /// Query as a double. See IntValue()
nuclear@19 1046 double DoubleValue() const {
nuclear@19 1047 double d=0;
nuclear@19 1048 QueryDoubleValue( &d );
nuclear@19 1049 return d;
nuclear@19 1050 }
nuclear@19 1051 /// Query as a float. See IntValue()
nuclear@19 1052 float FloatValue() const {
nuclear@19 1053 float f=0;
nuclear@19 1054 QueryFloatValue( &f );
nuclear@19 1055 return f;
nuclear@19 1056 }
nuclear@19 1057
nuclear@19 1058 /** QueryIntValue interprets the attribute as an integer, and returns the value
nuclear@19 1059 in the provided parameter. The function will return XML_NO_ERROR on success,
nuclear@19 1060 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
nuclear@19 1061 */
nuclear@19 1062 XMLError QueryIntValue( int* value ) const;
nuclear@19 1063 /// See QueryIntValue
nuclear@19 1064 XMLError QueryUnsignedValue( unsigned int* value ) const;
nuclear@19 1065 /// See QueryIntValue
nuclear@19 1066 XMLError QueryBoolValue( bool* value ) const;
nuclear@19 1067 /// See QueryIntValue
nuclear@19 1068 XMLError QueryDoubleValue( double* value ) const;
nuclear@19 1069 /// See QueryIntValue
nuclear@19 1070 XMLError QueryFloatValue( float* value ) const;
nuclear@19 1071
nuclear@19 1072 /// Set the attribute to a string value.
nuclear@19 1073 void SetAttribute( const char* value );
nuclear@19 1074 /// Set the attribute to value.
nuclear@19 1075 void SetAttribute( int value );
nuclear@19 1076 /// Set the attribute to value.
nuclear@19 1077 void SetAttribute( unsigned value );
nuclear@19 1078 /// Set the attribute to value.
nuclear@19 1079 void SetAttribute( bool value );
nuclear@19 1080 /// Set the attribute to value.
nuclear@19 1081 void SetAttribute( double value );
nuclear@19 1082 /// Set the attribute to value.
nuclear@19 1083 void SetAttribute( float value );
nuclear@19 1084
nuclear@19 1085 private:
nuclear@19 1086 enum { BUF_SIZE = 200 };
nuclear@19 1087
nuclear@19 1088 XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
nuclear@19 1089 virtual ~XMLAttribute() {}
nuclear@19 1090
nuclear@19 1091 XMLAttribute( const XMLAttribute& ); // not supported
nuclear@19 1092 void operator=( const XMLAttribute& ); // not supported
nuclear@19 1093 void SetName( const char* name );
nuclear@19 1094
nuclear@19 1095 char* ParseDeep( char* p, bool processEntities );
nuclear@19 1096
nuclear@19 1097 mutable StrPair _name;
nuclear@19 1098 mutable StrPair _value;
nuclear@19 1099 XMLAttribute* _next;
nuclear@19 1100 MemPool* _memPool;
nuclear@19 1101 };
nuclear@19 1102
nuclear@19 1103
nuclear@19 1104 /** The element is a container class. It has a value, the element name,
nuclear@19 1105 and can contain other elements, text, comments, and unknowns.
nuclear@19 1106 Elements also contain an arbitrary number of attributes.
nuclear@19 1107 */
nuclear@19 1108 class TINYXML2_LIB XMLElement : public XMLNode
nuclear@19 1109 {
nuclear@19 1110 friend class XMLBase;
nuclear@19 1111 friend class XMLDocument;
nuclear@19 1112 public:
nuclear@19 1113 /// Get the name of an element (which is the Value() of the node.)
nuclear@19 1114 const char* Name() const {
nuclear@19 1115 return Value();
nuclear@19 1116 }
nuclear@19 1117 /// Set the name of the element.
nuclear@19 1118 void SetName( const char* str, bool staticMem=false ) {
nuclear@19 1119 SetValue( str, staticMem );
nuclear@19 1120 }
nuclear@19 1121
nuclear@19 1122 virtual XMLElement* ToElement() {
nuclear@19 1123 return this;
nuclear@19 1124 }
nuclear@19 1125 virtual const XMLElement* ToElement() const {
nuclear@19 1126 return this;
nuclear@19 1127 }
nuclear@19 1128 virtual bool Accept( XMLVisitor* visitor ) const;
nuclear@19 1129
nuclear@19 1130 /** Given an attribute name, Attribute() returns the value
nuclear@19 1131 for the attribute of that name, or null if none
nuclear@19 1132 exists. For example:
nuclear@19 1133
nuclear@19 1134 @verbatim
nuclear@19 1135 const char* value = ele->Attribute( "foo" );
nuclear@19 1136 @endverbatim
nuclear@19 1137
nuclear@19 1138 The 'value' parameter is normally null. However, if specified,
nuclear@19 1139 the attribute will only be returned if the 'name' and 'value'
nuclear@19 1140 match. This allow you to write code:
nuclear@19 1141
nuclear@19 1142 @verbatim
nuclear@19 1143 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
nuclear@19 1144 @endverbatim
nuclear@19 1145
nuclear@19 1146 rather than:
nuclear@19 1147 @verbatim
nuclear@19 1148 if ( ele->Attribute( "foo" ) ) {
nuclear@19 1149 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
nuclear@19 1150 }
nuclear@19 1151 @endverbatim
nuclear@19 1152 */
nuclear@19 1153 const char* Attribute( const char* name, const char* value=0 ) const;
nuclear@19 1154
nuclear@19 1155 /** Given an attribute name, IntAttribute() returns the value
nuclear@19 1156 of the attribute interpreted as an integer. 0 will be
nuclear@19 1157 returned if there is an error. For a method with error
nuclear@19 1158 checking, see QueryIntAttribute()
nuclear@19 1159 */
nuclear@19 1160 int IntAttribute( const char* name ) const {
nuclear@19 1161 int i=0;
nuclear@19 1162 QueryIntAttribute( name, &i );
nuclear@19 1163 return i;
nuclear@19 1164 }
nuclear@19 1165 /// See IntAttribute()
nuclear@19 1166 unsigned UnsignedAttribute( const char* name ) const {
nuclear@19 1167 unsigned i=0;
nuclear@19 1168 QueryUnsignedAttribute( name, &i );
nuclear@19 1169 return i;
nuclear@19 1170 }
nuclear@19 1171 /// See IntAttribute()
nuclear@19 1172 bool BoolAttribute( const char* name ) const {
nuclear@19 1173 bool b=false;
nuclear@19 1174 QueryBoolAttribute( name, &b );
nuclear@19 1175 return b;
nuclear@19 1176 }
nuclear@19 1177 /// See IntAttribute()
nuclear@19 1178 double DoubleAttribute( const char* name ) const {
nuclear@19 1179 double d=0;
nuclear@19 1180 QueryDoubleAttribute( name, &d );
nuclear@19 1181 return d;
nuclear@19 1182 }
nuclear@19 1183 /// See IntAttribute()
nuclear@19 1184 float FloatAttribute( const char* name ) const {
nuclear@19 1185 float f=0;
nuclear@19 1186 QueryFloatAttribute( name, &f );
nuclear@19 1187 return f;
nuclear@19 1188 }
nuclear@19 1189
nuclear@19 1190 /** Given an attribute name, QueryIntAttribute() returns
nuclear@19 1191 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
nuclear@19 1192 can't be performed, or XML_NO_ATTRIBUTE if the attribute
nuclear@19 1193 doesn't exist. If successful, the result of the conversion
nuclear@19 1194 will be written to 'value'. If not successful, nothing will
nuclear@19 1195 be written to 'value'. This allows you to provide default
nuclear@19 1196 value:
nuclear@19 1197
nuclear@19 1198 @verbatim
nuclear@19 1199 int value = 10;
nuclear@19 1200 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
nuclear@19 1201 @endverbatim
nuclear@19 1202 */
nuclear@19 1203 XMLError QueryIntAttribute( const char* name, int* value ) const {
nuclear@19 1204 const XMLAttribute* a = FindAttribute( name );
nuclear@19 1205 if ( !a ) {
nuclear@19 1206 return XML_NO_ATTRIBUTE;
nuclear@19 1207 }
nuclear@19 1208 return a->QueryIntValue( value );
nuclear@19 1209 }
nuclear@19 1210 /// See QueryIntAttribute()
nuclear@19 1211 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
nuclear@19 1212 const XMLAttribute* a = FindAttribute( name );
nuclear@19 1213 if ( !a ) {
nuclear@19 1214 return XML_NO_ATTRIBUTE;
nuclear@19 1215 }
nuclear@19 1216 return a->QueryUnsignedValue( value );
nuclear@19 1217 }
nuclear@19 1218 /// See QueryIntAttribute()
nuclear@19 1219 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
nuclear@19 1220 const XMLAttribute* a = FindAttribute( name );
nuclear@19 1221 if ( !a ) {
nuclear@19 1222 return XML_NO_ATTRIBUTE;
nuclear@19 1223 }
nuclear@19 1224 return a->QueryBoolValue( value );
nuclear@19 1225 }
nuclear@19 1226 /// See QueryIntAttribute()
nuclear@19 1227 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
nuclear@19 1228 const XMLAttribute* a = FindAttribute( name );
nuclear@19 1229 if ( !a ) {
nuclear@19 1230 return XML_NO_ATTRIBUTE;
nuclear@19 1231 }
nuclear@19 1232 return a->QueryDoubleValue( value );
nuclear@19 1233 }
nuclear@19 1234 /// See QueryIntAttribute()
nuclear@19 1235 XMLError QueryFloatAttribute( const char* name, float* value ) const {
nuclear@19 1236 const XMLAttribute* a = FindAttribute( name );
nuclear@19 1237 if ( !a ) {
nuclear@19 1238 return XML_NO_ATTRIBUTE;
nuclear@19 1239 }
nuclear@19 1240 return a->QueryFloatValue( value );
nuclear@19 1241 }
nuclear@19 1242
nuclear@19 1243
nuclear@19 1244 /** Given an attribute name, QueryAttribute() returns
nuclear@19 1245 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
nuclear@19 1246 can't be performed, or XML_NO_ATTRIBUTE if the attribute
nuclear@19 1247 doesn't exist. It is overloaded for the primitive types,
nuclear@19 1248 and is a generally more convenient replacement of
nuclear@19 1249 QueryIntAttribute() and related functions.
nuclear@19 1250
nuclear@19 1251 If successful, the result of the conversion
nuclear@19 1252 will be written to 'value'. If not successful, nothing will
nuclear@19 1253 be written to 'value'. This allows you to provide default
nuclear@19 1254 value:
nuclear@19 1255
nuclear@19 1256 @verbatim
nuclear@19 1257 int value = 10;
nuclear@19 1258 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
nuclear@19 1259 @endverbatim
nuclear@19 1260 */
nuclear@19 1261 int QueryAttribute( const char* name, int* value ) const {
nuclear@19 1262 return QueryIntAttribute( name, value );
nuclear@19 1263 }
nuclear@19 1264
nuclear@19 1265 int QueryAttribute( const char* name, unsigned int* value ) const {
nuclear@19 1266 return QueryUnsignedAttribute( name, value );
nuclear@19 1267 }
nuclear@19 1268
nuclear@19 1269 int QueryAttribute( const char* name, bool* value ) const {
nuclear@19 1270 return QueryBoolAttribute( name, value );
nuclear@19 1271 }
nuclear@19 1272
nuclear@19 1273 int QueryAttribute( const char* name, double* value ) const {
nuclear@19 1274 return QueryDoubleAttribute( name, value );
nuclear@19 1275 }
nuclear@19 1276
nuclear@19 1277 int QueryAttribute( const char* name, float* value ) const {
nuclear@19 1278 return QueryFloatAttribute( name, value );
nuclear@19 1279 }
nuclear@19 1280
nuclear@19 1281 /// Sets the named attribute to value.
nuclear@19 1282 void SetAttribute( const char* name, const char* value ) {
nuclear@19 1283 XMLAttribute* a = FindOrCreateAttribute( name );
nuclear@19 1284 a->SetAttribute( value );
nuclear@19 1285 }
nuclear@19 1286 /// Sets the named attribute to value.
nuclear@19 1287 void SetAttribute( const char* name, int value ) {
nuclear@19 1288 XMLAttribute* a = FindOrCreateAttribute( name );
nuclear@19 1289 a->SetAttribute( value );
nuclear@19 1290 }
nuclear@19 1291 /// Sets the named attribute to value.
nuclear@19 1292 void SetAttribute( const char* name, unsigned value ) {
nuclear@19 1293 XMLAttribute* a = FindOrCreateAttribute( name );
nuclear@19 1294 a->SetAttribute( value );
nuclear@19 1295 }
nuclear@19 1296 /// Sets the named attribute to value.
nuclear@19 1297 void SetAttribute( const char* name, bool value ) {
nuclear@19 1298 XMLAttribute* a = FindOrCreateAttribute( name );
nuclear@19 1299 a->SetAttribute( value );
nuclear@19 1300 }
nuclear@19 1301 /// Sets the named attribute to value.
nuclear@19 1302 void SetAttribute( const char* name, double value ) {
nuclear@19 1303 XMLAttribute* a = FindOrCreateAttribute( name );
nuclear@19 1304 a->SetAttribute( value );
nuclear@19 1305 }
nuclear@19 1306
nuclear@19 1307 /**
nuclear@19 1308 Delete an attribute.
nuclear@19 1309 */
nuclear@19 1310 void DeleteAttribute( const char* name );
nuclear@19 1311
nuclear@19 1312 /// Return the first attribute in the list.
nuclear@19 1313 const XMLAttribute* FirstAttribute() const {
nuclear@19 1314 return _rootAttribute;
nuclear@19 1315 }
nuclear@19 1316 /// Query a specific attribute in the list.
nuclear@19 1317 const XMLAttribute* FindAttribute( const char* name ) const;
nuclear@19 1318
nuclear@19 1319 /** Convenience function for easy access to the text inside an element. Although easy
nuclear@19 1320 and concise, GetText() is limited compared to getting the XMLText child
nuclear@19 1321 and accessing it directly.
nuclear@19 1322
nuclear@19 1323 If the first child of 'this' is a XMLText, the GetText()
nuclear@19 1324 returns the character string of the Text node, else null is returned.
nuclear@19 1325
nuclear@19 1326 This is a convenient method for getting the text of simple contained text:
nuclear@19 1327 @verbatim
nuclear@19 1328 <foo>This is text</foo>
nuclear@19 1329 const char* str = fooElement->GetText();
nuclear@19 1330 @endverbatim
nuclear@19 1331
nuclear@19 1332 'str' will be a pointer to "This is text".
nuclear@19 1333
nuclear@19 1334 Note that this function can be misleading. If the element foo was created from
nuclear@19 1335 this XML:
nuclear@19 1336 @verbatim
nuclear@19 1337 <foo><b>This is text</b></foo>
nuclear@19 1338 @endverbatim
nuclear@19 1339
nuclear@19 1340 then the value of str would be null. The first child node isn't a text node, it is
nuclear@19 1341 another element. From this XML:
nuclear@19 1342 @verbatim
nuclear@19 1343 <foo>This is <b>text</b></foo>
nuclear@19 1344 @endverbatim
nuclear@19 1345 GetText() will return "This is ".
nuclear@19 1346 */
nuclear@19 1347 const char* GetText() const;
nuclear@19 1348
nuclear@19 1349 /**
nuclear@19 1350 Convenience method to query the value of a child text node. This is probably best
nuclear@19 1351 shown by example. Given you have a document is this form:
nuclear@19 1352 @verbatim
nuclear@19 1353 <point>
nuclear@19 1354 <x>1</x>
nuclear@19 1355 <y>1.4</y>
nuclear@19 1356 </point>
nuclear@19 1357 @endverbatim
nuclear@19 1358
nuclear@19 1359 The QueryIntText() and similar functions provide a safe and easier way to get to the
nuclear@19 1360 "value" of x and y.
nuclear@19 1361
nuclear@19 1362 @verbatim
nuclear@19 1363 int x = 0;
nuclear@19 1364 float y = 0; // types of x and y are contrived for example
nuclear@19 1365 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
nuclear@19 1366 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
nuclear@19 1367 xElement->QueryIntText( &x );
nuclear@19 1368 yElement->QueryFloatText( &y );
nuclear@19 1369 @endverbatim
nuclear@19 1370
nuclear@19 1371 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
nuclear@19 1372 to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.
nuclear@19 1373
nuclear@19 1374 */
nuclear@19 1375 XMLError QueryIntText( int* ival ) const;
nuclear@19 1376 /// See QueryIntText()
nuclear@19 1377 XMLError QueryUnsignedText( unsigned* uval ) const;
nuclear@19 1378 /// See QueryIntText()
nuclear@19 1379 XMLError QueryBoolText( bool* bval ) const;
nuclear@19 1380 /// See QueryIntText()
nuclear@19 1381 XMLError QueryDoubleText( double* dval ) const;
nuclear@19 1382 /// See QueryIntText()
nuclear@19 1383 XMLError QueryFloatText( float* fval ) const;
nuclear@19 1384
nuclear@19 1385 // internal:
nuclear@19 1386 enum {
nuclear@19 1387 OPEN, // <foo>
nuclear@19 1388 CLOSED, // <foo/>
nuclear@19 1389 CLOSING // </foo>
nuclear@19 1390 };
nuclear@19 1391 int ClosingType() const {
nuclear@19 1392 return _closingType;
nuclear@19 1393 }
nuclear@19 1394 char* ParseDeep( char* p, StrPair* endTag );
nuclear@19 1395 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
nuclear@19 1396 virtual bool ShallowEqual( const XMLNode* compare ) const;
nuclear@19 1397
nuclear@19 1398 private:
nuclear@19 1399 XMLElement( XMLDocument* doc );
nuclear@19 1400 virtual ~XMLElement();
nuclear@19 1401 XMLElement( const XMLElement& ); // not supported
nuclear@19 1402 void operator=( const XMLElement& ); // not supported
nuclear@19 1403
nuclear@19 1404 XMLAttribute* FindAttribute( const char* name );
nuclear@19 1405 XMLAttribute* FindOrCreateAttribute( const char* name );
nuclear@19 1406 //void LinkAttribute( XMLAttribute* attrib );
nuclear@19 1407 char* ParseAttributes( char* p );
nuclear@19 1408
nuclear@19 1409 int _closingType;
nuclear@19 1410 // The attribute list is ordered; there is no 'lastAttribute'
nuclear@19 1411 // because the list needs to be scanned for dupes before adding
nuclear@19 1412 // a new attribute.
nuclear@19 1413 XMLAttribute* _rootAttribute;
nuclear@19 1414 };
nuclear@19 1415
nuclear@19 1416
nuclear@19 1417 enum Whitespace {
nuclear@19 1418 PRESERVE_WHITESPACE,
nuclear@19 1419 COLLAPSE_WHITESPACE
nuclear@19 1420 };
nuclear@19 1421
nuclear@19 1422
nuclear@19 1423 /** A Document binds together all the functionality.
nuclear@19 1424 It can be saved, loaded, and printed to the screen.
nuclear@19 1425 All Nodes are connected and allocated to a Document.
nuclear@19 1426 If the Document is deleted, all its Nodes are also deleted.
nuclear@19 1427 */
nuclear@19 1428 class TINYXML2_LIB XMLDocument : public XMLNode
nuclear@19 1429 {
nuclear@19 1430 friend class XMLElement;
nuclear@19 1431 public:
nuclear@19 1432 /// constructor
nuclear@19 1433 XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
nuclear@19 1434 ~XMLDocument();
nuclear@19 1435
nuclear@19 1436 virtual XMLDocument* ToDocument() {
nuclear@19 1437 return this;
nuclear@19 1438 }
nuclear@19 1439 virtual const XMLDocument* ToDocument() const {
nuclear@19 1440 return this;
nuclear@19 1441 }
nuclear@19 1442
nuclear@19 1443 /**
nuclear@19 1444 Parse an XML file from a character string.
nuclear@19 1445 Returns XML_NO_ERROR (0) on success, or
nuclear@19 1446 an errorID.
nuclear@19 1447
nuclear@19 1448 You may optionally pass in the 'nBytes', which is
nuclear@19 1449 the number of bytes which will be parsed. If not
nuclear@19 1450 specified, TinyXML-2 will assume 'xml' points to a
nuclear@19 1451 null terminated string.
nuclear@19 1452 */
nuclear@19 1453 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
nuclear@19 1454
nuclear@19 1455 /**
nuclear@19 1456 Load an XML file from disk.
nuclear@19 1457 Returns XML_NO_ERROR (0) on success, or
nuclear@19 1458 an errorID.
nuclear@19 1459 */
nuclear@19 1460 XMLError LoadFile( const char* filename );
nuclear@19 1461
nuclear@19 1462 /**
nuclear@19 1463 Load an XML file from disk. You are responsible
nuclear@19 1464 for providing and closing the FILE*.
nuclear@19 1465
nuclear@19 1466 Returns XML_NO_ERROR (0) on success, or
nuclear@19 1467 an errorID.
nuclear@19 1468 */
nuclear@19 1469 XMLError LoadFile( FILE* );
nuclear@19 1470
nuclear@19 1471 /**
nuclear@19 1472 Save the XML file to disk.
nuclear@19 1473 Returns XML_NO_ERROR (0) on success, or
nuclear@19 1474 an errorID.
nuclear@19 1475 */
nuclear@19 1476 XMLError SaveFile( const char* filename, bool compact = false );
nuclear@19 1477
nuclear@19 1478 /**
nuclear@19 1479 Save the XML file to disk. You are responsible
nuclear@19 1480 for providing and closing the FILE*.
nuclear@19 1481
nuclear@19 1482 Returns XML_NO_ERROR (0) on success, or
nuclear@19 1483 an errorID.
nuclear@19 1484 */
nuclear@19 1485 XMLError SaveFile( FILE* fp, bool compact = false );
nuclear@19 1486
nuclear@19 1487 bool ProcessEntities() const {
nuclear@19 1488 return _processEntities;
nuclear@19 1489 }
nuclear@19 1490 Whitespace WhitespaceMode() const {
nuclear@19 1491 return _whitespace;
nuclear@19 1492 }
nuclear@19 1493
nuclear@19 1494 /**
nuclear@19 1495 Returns true if this document has a leading Byte Order Mark of UTF8.
nuclear@19 1496 */
nuclear@19 1497 bool HasBOM() const {
nuclear@19 1498 return _writeBOM;
nuclear@19 1499 }
nuclear@19 1500 /** Sets whether to write the BOM when writing the file.
nuclear@19 1501 */
nuclear@19 1502 void SetBOM( bool useBOM ) {
nuclear@19 1503 _writeBOM = useBOM;
nuclear@19 1504 }
nuclear@19 1505
nuclear@19 1506 /** Return the root element of DOM. Equivalent to FirstChildElement().
nuclear@19 1507 To get the first node, use FirstChild().
nuclear@19 1508 */
nuclear@19 1509 XMLElement* RootElement() {
nuclear@19 1510 return FirstChildElement();
nuclear@19 1511 }
nuclear@19 1512 const XMLElement* RootElement() const {
nuclear@19 1513 return FirstChildElement();
nuclear@19 1514 }
nuclear@19 1515
nuclear@19 1516 /** Print the Document. If the Printer is not provided, it will
nuclear@19 1517 print to stdout. If you provide Printer, this can print to a file:
nuclear@19 1518 @verbatim
nuclear@19 1519 XMLPrinter printer( fp );
nuclear@19 1520 doc.Print( &printer );
nuclear@19 1521 @endverbatim
nuclear@19 1522
nuclear@19 1523 Or you can use a printer to print to memory:
nuclear@19 1524 @verbatim
nuclear@19 1525 XMLPrinter printer;
nuclear@19 1526 doc.Print( &printer );
nuclear@19 1527 // printer.CStr() has a const char* to the XML
nuclear@19 1528 @endverbatim
nuclear@19 1529 */
nuclear@19 1530 void Print( XMLPrinter* streamer=0 ) const;
nuclear@19 1531 virtual bool Accept( XMLVisitor* visitor ) const;
nuclear@19 1532
nuclear@19 1533 /**
nuclear@19 1534 Create a new Element associated with
nuclear@19 1535 this Document. The memory for the Element
nuclear@19 1536 is managed by the Document.
nuclear@19 1537 */
nuclear@19 1538 XMLElement* NewElement( const char* name );
nuclear@19 1539 /**
nuclear@19 1540 Create a new Comment associated with
nuclear@19 1541 this Document. The memory for the Comment
nuclear@19 1542 is managed by the Document.
nuclear@19 1543 */
nuclear@19 1544 XMLComment* NewComment( const char* comment );
nuclear@19 1545 /**
nuclear@19 1546 Create a new Text associated with
nuclear@19 1547 this Document. The memory for the Text
nuclear@19 1548 is managed by the Document.
nuclear@19 1549 */
nuclear@19 1550 XMLText* NewText( const char* text );
nuclear@19 1551 /**
nuclear@19 1552 Create a new Declaration associated with
nuclear@19 1553 this Document. The memory for the object
nuclear@19 1554 is managed by the Document.
nuclear@19 1555
nuclear@19 1556 If the 'text' param is null, the standard
nuclear@19 1557 declaration is used.:
nuclear@19 1558 @verbatim
nuclear@19 1559 <?xml version="1.0" encoding="UTF-8"?>
nuclear@19 1560 @endverbatim
nuclear@19 1561 */
nuclear@19 1562 XMLDeclaration* NewDeclaration( const char* text=0 );
nuclear@19 1563 /**
nuclear@19 1564 Create a new Unknown associated with
nuclear@19 1565 this Document. The memory for the object
nuclear@19 1566 is managed by the Document.
nuclear@19 1567 */
nuclear@19 1568 XMLUnknown* NewUnknown( const char* text );
nuclear@19 1569
nuclear@19 1570 /**
nuclear@19 1571 Delete a node associated with this document.
nuclear@19 1572 It will be unlinked from the DOM.
nuclear@19 1573 */
nuclear@19 1574 void DeleteNode( XMLNode* node ) {
nuclear@19 1575 node->_parent->DeleteChild( node );
nuclear@19 1576 }
nuclear@19 1577
nuclear@19 1578 void SetError( XMLError error, const char* str1, const char* str2 );
nuclear@19 1579
nuclear@19 1580 /// Return true if there was an error parsing the document.
nuclear@19 1581 bool Error() const {
nuclear@19 1582 return _errorID != XML_NO_ERROR;
nuclear@19 1583 }
nuclear@19 1584 /// Return the errorID.
nuclear@19 1585 XMLError ErrorID() const {
nuclear@19 1586 return _errorID;
nuclear@19 1587 }
nuclear@19 1588 /// Return a possibly helpful diagnostic location or string.
nuclear@19 1589 const char* GetErrorStr1() const {
nuclear@19 1590 return _errorStr1;
nuclear@19 1591 }
nuclear@19 1592 /// Return a possibly helpful secondary diagnostic location or string.
nuclear@19 1593 const char* GetErrorStr2() const {
nuclear@19 1594 return _errorStr2;
nuclear@19 1595 }
nuclear@19 1596 /// If there is an error, print it to stdout.
nuclear@19 1597 void PrintError() const;
nuclear@19 1598
nuclear@19 1599 /// Clear the document, resetting it to the initial state.
nuclear@19 1600 void Clear();
nuclear@19 1601
nuclear@19 1602 // internal
nuclear@19 1603 char* Identify( char* p, XMLNode** node );
nuclear@19 1604
nuclear@19 1605 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
nuclear@19 1606 return 0;
nuclear@19 1607 }
nuclear@19 1608 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
nuclear@19 1609 return false;
nuclear@19 1610 }
nuclear@19 1611
nuclear@19 1612 private:
nuclear@19 1613 XMLDocument( const XMLDocument& ); // not supported
nuclear@19 1614 void operator=( const XMLDocument& ); // not supported
nuclear@19 1615
nuclear@19 1616 bool _writeBOM;
nuclear@19 1617 bool _processEntities;
nuclear@19 1618 XMLError _errorID;
nuclear@19 1619 Whitespace _whitespace;
nuclear@19 1620 const char* _errorStr1;
nuclear@19 1621 const char* _errorStr2;
nuclear@19 1622 char* _charBuffer;
nuclear@19 1623
nuclear@19 1624 MemPoolT< sizeof(XMLElement) > _elementPool;
nuclear@19 1625 MemPoolT< sizeof(XMLAttribute) > _attributePool;
nuclear@19 1626 MemPoolT< sizeof(XMLText) > _textPool;
nuclear@19 1627 MemPoolT< sizeof(XMLComment) > _commentPool;
nuclear@19 1628 };
nuclear@19 1629
nuclear@19 1630
nuclear@19 1631 /**
nuclear@19 1632 A XMLHandle is a class that wraps a node pointer with null checks; this is
nuclear@19 1633 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
nuclear@19 1634 DOM structure. It is a separate utility class.
nuclear@19 1635
nuclear@19 1636 Take an example:
nuclear@19 1637 @verbatim
nuclear@19 1638 <Document>
nuclear@19 1639 <Element attributeA = "valueA">
nuclear@19 1640 <Child attributeB = "value1" />
nuclear@19 1641 <Child attributeB = "value2" />
nuclear@19 1642 </Element>
nuclear@19 1643 </Document>
nuclear@19 1644 @endverbatim
nuclear@19 1645
nuclear@19 1646 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
nuclear@19 1647 easy to write a *lot* of code that looks like:
nuclear@19 1648
nuclear@19 1649 @verbatim
nuclear@19 1650 XMLElement* root = document.FirstChildElement( "Document" );
nuclear@19 1651 if ( root )
nuclear@19 1652 {
nuclear@19 1653 XMLElement* element = root->FirstChildElement( "Element" );
nuclear@19 1654 if ( element )
nuclear@19 1655 {
nuclear@19 1656 XMLElement* child = element->FirstChildElement( "Child" );
nuclear@19 1657 if ( child )
nuclear@19 1658 {
nuclear@19 1659 XMLElement* child2 = child->NextSiblingElement( "Child" );
nuclear@19 1660 if ( child2 )
nuclear@19 1661 {
nuclear@19 1662 // Finally do something useful.
nuclear@19 1663 @endverbatim
nuclear@19 1664
nuclear@19 1665 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
nuclear@19 1666 of such code. A XMLHandle checks for null pointers so it is perfectly safe
nuclear@19 1667 and correct to use:
nuclear@19 1668
nuclear@19 1669 @verbatim
nuclear@19 1670 XMLHandle docHandle( &document );
nuclear@19 1671 XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement();
nuclear@19 1672 if ( child2 )
nuclear@19 1673 {
nuclear@19 1674 // do something useful
nuclear@19 1675 @endverbatim
nuclear@19 1676
nuclear@19 1677 Which is MUCH more concise and useful.
nuclear@19 1678
nuclear@19 1679 It is also safe to copy handles - internally they are nothing more than node pointers.
nuclear@19 1680 @verbatim
nuclear@19 1681 XMLHandle handleCopy = handle;
nuclear@19 1682 @endverbatim
nuclear@19 1683
nuclear@19 1684 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
nuclear@19 1685 */
nuclear@19 1686 class TINYXML2_LIB XMLHandle
nuclear@19 1687 {
nuclear@19 1688 public:
nuclear@19 1689 /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
nuclear@19 1690 XMLHandle( XMLNode* node ) {
nuclear@19 1691 _node = node;
nuclear@19 1692 }
nuclear@19 1693 /// Create a handle from a node.
nuclear@19 1694 XMLHandle( XMLNode& node ) {
nuclear@19 1695 _node = &node;
nuclear@19 1696 }
nuclear@19 1697 /// Copy constructor
nuclear@19 1698 XMLHandle( const XMLHandle& ref ) {
nuclear@19 1699 _node = ref._node;
nuclear@19 1700 }
nuclear@19 1701 /// Assignment
nuclear@19 1702 XMLHandle& operator=( const XMLHandle& ref ) {
nuclear@19 1703 _node = ref._node;
nuclear@19 1704 return *this;
nuclear@19 1705 }
nuclear@19 1706
nuclear@19 1707 /// Get the first child of this handle.
nuclear@19 1708 XMLHandle FirstChild() {
nuclear@19 1709 return XMLHandle( _node ? _node->FirstChild() : 0 );
nuclear@19 1710 }
nuclear@19 1711 /// Get the first child element of this handle.
nuclear@19 1712 XMLHandle FirstChildElement( const char* value=0 ) {
nuclear@19 1713 return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 );
nuclear@19 1714 }
nuclear@19 1715 /// Get the last child of this handle.
nuclear@19 1716 XMLHandle LastChild() {
nuclear@19 1717 return XMLHandle( _node ? _node->LastChild() : 0 );
nuclear@19 1718 }
nuclear@19 1719 /// Get the last child element of this handle.
nuclear@19 1720 XMLHandle LastChildElement( const char* _value=0 ) {
nuclear@19 1721 return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 );
nuclear@19 1722 }
nuclear@19 1723 /// Get the previous sibling of this handle.
nuclear@19 1724 XMLHandle PreviousSibling() {
nuclear@19 1725 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
nuclear@19 1726 }
nuclear@19 1727 /// Get the previous sibling element of this handle.
nuclear@19 1728 XMLHandle PreviousSiblingElement( const char* _value=0 ) {
nuclear@19 1729 return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
nuclear@19 1730 }
nuclear@19 1731 /// Get the next sibling of this handle.
nuclear@19 1732 XMLHandle NextSibling() {
nuclear@19 1733 return XMLHandle( _node ? _node->NextSibling() : 0 );
nuclear@19 1734 }
nuclear@19 1735 /// Get the next sibling element of this handle.
nuclear@19 1736 XMLHandle NextSiblingElement( const char* _value=0 ) {
nuclear@19 1737 return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
nuclear@19 1738 }
nuclear@19 1739
nuclear@19 1740 /// Safe cast to XMLNode. This can return null.
nuclear@19 1741 XMLNode* ToNode() {
nuclear@19 1742 return _node;
nuclear@19 1743 }
nuclear@19 1744 /// Safe cast to XMLElement. This can return null.
nuclear@19 1745 XMLElement* ToElement() {
nuclear@19 1746 return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
nuclear@19 1747 }
nuclear@19 1748 /// Safe cast to XMLText. This can return null.
nuclear@19 1749 XMLText* ToText() {
nuclear@19 1750 return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
nuclear@19 1751 }
nuclear@19 1752 /// Safe cast to XMLUnknown. This can return null.
nuclear@19 1753 XMLUnknown* ToUnknown() {
nuclear@19 1754 return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
nuclear@19 1755 }
nuclear@19 1756 /// Safe cast to XMLDeclaration. This can return null.
nuclear@19 1757 XMLDeclaration* ToDeclaration() {
nuclear@19 1758 return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
nuclear@19 1759 }
nuclear@19 1760
nuclear@19 1761 private:
nuclear@19 1762 XMLNode* _node;
nuclear@19 1763 };
nuclear@19 1764
nuclear@19 1765
nuclear@19 1766 /**
nuclear@19 1767 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
nuclear@19 1768 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
nuclear@19 1769 */
nuclear@19 1770 class TINYXML2_LIB XMLConstHandle
nuclear@19 1771 {
nuclear@19 1772 public:
nuclear@19 1773 XMLConstHandle( const XMLNode* node ) {
nuclear@19 1774 _node = node;
nuclear@19 1775 }
nuclear@19 1776 XMLConstHandle( const XMLNode& node ) {
nuclear@19 1777 _node = &node;
nuclear@19 1778 }
nuclear@19 1779 XMLConstHandle( const XMLConstHandle& ref ) {
nuclear@19 1780 _node = ref._node;
nuclear@19 1781 }
nuclear@19 1782
nuclear@19 1783 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
nuclear@19 1784 _node = ref._node;
nuclear@19 1785 return *this;
nuclear@19 1786 }
nuclear@19 1787
nuclear@19 1788 const XMLConstHandle FirstChild() const {
nuclear@19 1789 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
nuclear@19 1790 }
nuclear@19 1791 const XMLConstHandle FirstChildElement( const char* value=0 ) const {
nuclear@19 1792 return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
nuclear@19 1793 }
nuclear@19 1794 const XMLConstHandle LastChild() const {
nuclear@19 1795 return XMLConstHandle( _node ? _node->LastChild() : 0 );
nuclear@19 1796 }
nuclear@19 1797 const XMLConstHandle LastChildElement( const char* _value=0 ) const {
nuclear@19 1798 return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
nuclear@19 1799 }
nuclear@19 1800 const XMLConstHandle PreviousSibling() const {
nuclear@19 1801 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
nuclear@19 1802 }
nuclear@19 1803 const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const {
nuclear@19 1804 return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
nuclear@19 1805 }
nuclear@19 1806 const XMLConstHandle NextSibling() const {
nuclear@19 1807 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
nuclear@19 1808 }
nuclear@19 1809 const XMLConstHandle NextSiblingElement( const char* _value=0 ) const {
nuclear@19 1810 return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
nuclear@19 1811 }
nuclear@19 1812
nuclear@19 1813
nuclear@19 1814 const XMLNode* ToNode() const {
nuclear@19 1815 return _node;
nuclear@19 1816 }
nuclear@19 1817 const XMLElement* ToElement() const {
nuclear@19 1818 return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
nuclear@19 1819 }
nuclear@19 1820 const XMLText* ToText() const {
nuclear@19 1821 return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
nuclear@19 1822 }
nuclear@19 1823 const XMLUnknown* ToUnknown() const {
nuclear@19 1824 return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
nuclear@19 1825 }
nuclear@19 1826 const XMLDeclaration* ToDeclaration() const {
nuclear@19 1827 return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
nuclear@19 1828 }
nuclear@19 1829
nuclear@19 1830 private:
nuclear@19 1831 const XMLNode* _node;
nuclear@19 1832 };
nuclear@19 1833
nuclear@19 1834
nuclear@19 1835 /**
nuclear@19 1836 Printing functionality. The XMLPrinter gives you more
nuclear@19 1837 options than the XMLDocument::Print() method.
nuclear@19 1838
nuclear@19 1839 It can:
nuclear@19 1840 -# Print to memory.
nuclear@19 1841 -# Print to a file you provide.
nuclear@19 1842 -# Print XML without a XMLDocument.
nuclear@19 1843
nuclear@19 1844 Print to Memory
nuclear@19 1845
nuclear@19 1846 @verbatim
nuclear@19 1847 XMLPrinter printer;
nuclear@19 1848 doc.Print( &printer );
nuclear@19 1849 SomeFunction( printer.CStr() );
nuclear@19 1850 @endverbatim
nuclear@19 1851
nuclear@19 1852 Print to a File
nuclear@19 1853
nuclear@19 1854 You provide the file pointer.
nuclear@19 1855 @verbatim
nuclear@19 1856 XMLPrinter printer( fp );
nuclear@19 1857 doc.Print( &printer );
nuclear@19 1858 @endverbatim
nuclear@19 1859
nuclear@19 1860 Print without a XMLDocument
nuclear@19 1861
nuclear@19 1862 When loading, an XML parser is very useful. However, sometimes
nuclear@19 1863 when saving, it just gets in the way. The code is often set up
nuclear@19 1864 for streaming, and constructing the DOM is just overhead.
nuclear@19 1865
nuclear@19 1866 The Printer supports the streaming case. The following code
nuclear@19 1867 prints out a trivially simple XML file without ever creating
nuclear@19 1868 an XML document.
nuclear@19 1869
nuclear@19 1870 @verbatim
nuclear@19 1871 XMLPrinter printer( fp );
nuclear@19 1872 printer.OpenElement( "foo" );
nuclear@19 1873 printer.PushAttribute( "foo", "bar" );
nuclear@19 1874 printer.CloseElement();
nuclear@19 1875 @endverbatim
nuclear@19 1876 */
nuclear@19 1877 class TINYXML2_LIB XMLPrinter : public XMLVisitor
nuclear@19 1878 {
nuclear@19 1879 public:
nuclear@19 1880 /** Construct the printer. If the FILE* is specified,
nuclear@19 1881 this will print to the FILE. Else it will print
nuclear@19 1882 to memory, and the result is available in CStr().
nuclear@19 1883 If 'compact' is set to true, then output is created
nuclear@19 1884 with only required whitespace and newlines.
nuclear@19 1885 */
nuclear@19 1886 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
nuclear@19 1887 ~XMLPrinter() {}
nuclear@19 1888
nuclear@19 1889 /** If streaming, write the BOM and declaration. */
nuclear@19 1890 void PushHeader( bool writeBOM, bool writeDeclaration );
nuclear@19 1891 /** If streaming, start writing an element.
nuclear@19 1892 The element must be closed with CloseElement()
nuclear@19 1893 */
nuclear@19 1894 void OpenElement( const char* name );
nuclear@19 1895 /// If streaming, add an attribute to an open element.
nuclear@19 1896 void PushAttribute( const char* name, const char* value );
nuclear@19 1897 void PushAttribute( const char* name, int value );
nuclear@19 1898 void PushAttribute( const char* name, unsigned value );
nuclear@19 1899 void PushAttribute( const char* name, bool value );
nuclear@19 1900 void PushAttribute( const char* name, double value );
nuclear@19 1901 /// If streaming, close the Element.
nuclear@19 1902 void CloseElement();
nuclear@19 1903
nuclear@19 1904 /// Add a text node.
nuclear@19 1905 void PushText( const char* text, bool cdata=false );
nuclear@19 1906 /// Add a text node from an integer.
nuclear@19 1907 void PushText( int value );
nuclear@19 1908 /// Add a text node from an unsigned.
nuclear@19 1909 void PushText( unsigned value );
nuclear@19 1910 /// Add a text node from a bool.
nuclear@19 1911 void PushText( bool value );
nuclear@19 1912 /// Add a text node from a float.
nuclear@19 1913 void PushText( float value );
nuclear@19 1914 /// Add a text node from a double.
nuclear@19 1915 void PushText( double value );
nuclear@19 1916
nuclear@19 1917 /// Add a comment
nuclear@19 1918 void PushComment( const char* comment );
nuclear@19 1919
nuclear@19 1920 void PushDeclaration( const char* value );
nuclear@19 1921 void PushUnknown( const char* value );
nuclear@19 1922
nuclear@19 1923 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
nuclear@19 1924 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
nuclear@19 1925 return true;
nuclear@19 1926 }
nuclear@19 1927
nuclear@19 1928 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
nuclear@19 1929 virtual bool VisitExit( const XMLElement& element );
nuclear@19 1930
nuclear@19 1931 virtual bool Visit( const XMLText& text );
nuclear@19 1932 virtual bool Visit( const XMLComment& comment );
nuclear@19 1933 virtual bool Visit( const XMLDeclaration& declaration );
nuclear@19 1934 virtual bool Visit( const XMLUnknown& unknown );
nuclear@19 1935
nuclear@19 1936 /**
nuclear@19 1937 If in print to memory mode, return a pointer to
nuclear@19 1938 the XML file in memory.
nuclear@19 1939 */
nuclear@19 1940 const char* CStr() const {
nuclear@19 1941 return _buffer.Mem();
nuclear@19 1942 }
nuclear@19 1943 /**
nuclear@19 1944 If in print to memory mode, return the size
nuclear@19 1945 of the XML file in memory. (Note the size returned
nuclear@19 1946 includes the terminating null.)
nuclear@19 1947 */
nuclear@19 1948 int CStrSize() const {
nuclear@19 1949 return _buffer.Size();
nuclear@19 1950 }
nuclear@19 1951
nuclear@19 1952 private:
nuclear@19 1953 void SealElement();
nuclear@19 1954 void PrintSpace( int depth );
nuclear@19 1955 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
nuclear@19 1956 void Print( const char* format, ... );
nuclear@19 1957
nuclear@19 1958 bool _elementJustOpened;
nuclear@19 1959 bool _firstElement;
nuclear@19 1960 FILE* _fp;
nuclear@19 1961 int _depth;
nuclear@19 1962 int _textDepth;
nuclear@19 1963 bool _processEntities;
nuclear@19 1964 bool _compactMode;
nuclear@19 1965
nuclear@19 1966 enum {
nuclear@19 1967 ENTITY_RANGE = 64,
nuclear@19 1968 BUF_SIZE = 200
nuclear@19 1969 };
nuclear@19 1970 bool _entityFlag[ENTITY_RANGE];
nuclear@19 1971 bool _restrictedEntityFlag[ENTITY_RANGE];
nuclear@19 1972
nuclear@19 1973 DynArray< const char*, 10 > _stack;
nuclear@19 1974 DynArray< char, 20 > _buffer;
nuclear@19 1975 #ifdef _MSC_VER
nuclear@19 1976 DynArray< char, 20 > _accumulator;
nuclear@19 1977 #endif
nuclear@19 1978 };
nuclear@19 1979
nuclear@19 1980
nuclear@19 1981 } // tinyxml2
nuclear@19 1982
nuclear@19 1983 #if defined(_MSC_VER)
nuclear@19 1984 # pragma warning(pop)
nuclear@19 1985 #endif
nuclear@19 1986
nuclear@19 1987 #endif // TINYXML2_INCLUDED