goat3d

annotate libs/tinyxml2/tinyxml2.h @ 88:7941e89798e5

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