blob: 31f2ec7e076c1198e1c0f42538e85fed0613d0ea [file] [log] [blame]
Lee Thomason (grinliz)28129862012-02-25 21:11:20 -08001/*
2Original code by Lee Thomason (www.grinninglizard.com)
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any
6damages arising from the use of this software.
7
8Permission is granted to anyone to use this software for any
9purpose, including commercial applications, and to alter it and
10redistribute it freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must
13not claim that you wrote the original software. If you use this
14software in a product, an acknowledgment in the product documentation
15would be appreciated but is not required.
16
172. Altered source versions must be plainly marked as such, and
18must not be misrepresented as being the original software.
19
203. This notice may not be removed or altered from any source
21distribution.
22*/
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -080023
Lee Thomason7d00b9a2012-02-27 17:54:22 -080024#ifndef TINYXML2_INCLUDED
U-Lama\Leee13c3e62011-12-28 14:36:55 -080025#define TINYXML2_INCLUDED
26
Jerome Martinez242c3ea2013-01-06 12:20:04 +010027#if defined(ANDROID_NDK) || defined(__BORLANDC__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070028# include <ctype.h>
29# include <limits.h>
30# include <stdio.h>
31# include <stdlib.h>
32# include <string.h>
33# include <stdarg.h>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070034#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -070035# include <cctype>
36# include <climits>
37# include <cstdio>
38# include <cstdlib>
39# include <cstring>
40# include <cstdarg>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070041#endif
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070042
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -070043/*
Lee Thomason7d00b9a2012-02-27 17:54:22 -080044 TODO: intern strings instead of allocation.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080045*/
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080046/*
Lee Thomasona9cf3f92012-10-11 16:56:51 -070047 gcc:
Lee Thomason5b0a6772012-11-19 13:54:42 -080048 g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
MortenMacFly4ee49f12013-01-14 20:03:14 +010049
Lee Thomasona9cf3f92012-10-11 16:56:51 -070050 Formatting, Artistic Style:
51 AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080052*/
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080053
U-Lama\Lee4cee6112011-12-31 14:58:18 -080054#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070055# ifndef DEBUG
56# define DEBUG
57# endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080058#endif
59
PKEuS95060352013-07-26 10:42:44 +020060#ifdef _MSC_VER
61# pragma warning(push)
62# pragma warning(disable: 4251)
63#endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080064
PKEuS16ed47d2013-07-06 12:02:43 +020065#ifdef _WIN32
66# ifdef TINYXML2_EXPORT
67# define TINYXML2_LIB __declspec(dllexport)
68# elif defined(TINYXML2_IMPORT)
69# define TINYXML2_LIB __declspec(dllimport)
70# else
71# define TINYXML2_LIB
72# endif
73#else
74# define TINYXML2_LIB
75#endif
76
77
U-Lama\Lee4cee6112011-12-31 14:58:18 -080078#if defined(DEBUG)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070079# if defined(_MSC_VER)
80# define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
81# elif defined (ANDROID_NDK)
82# include <android/log.h>
83# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
84# else
85# include <assert.h>
86# define TIXMLASSERT assert
87# endif
88# else
89# define TIXMLASSERT( x ) {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080090#endif
91
U-Lama\Leee13c3e62011-12-28 14:36:55 -080092
Lee Thomason1a1d4a72012-02-15 09:09:25 -080093#if defined(_MSC_VER) && (_MSC_VER >= 1400 )
Lee Thomasona9cf3f92012-10-11 16:56:51 -070094// Microsoft visual studio, version 2005 and higher.
95/*int _snprintf_s(
96 char *buffer,
97 size_t sizeOfBuffer,
98 size_t count,
99 const char *format [,
100 argument] ...
101);*/
102inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
103{
104 va_list va;
105 va_start( va, format );
106 int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
107 va_end( va );
108 return result;
109}
110#define TIXML_SSCANF sscanf_s
Lee Thomason (grinliz)b9e791f2012-04-06 21:27:10 -0700111#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700112// GCC version 3 and higher
113//#warning( "Using sn* functions." )
114#define TIXML_SNPRINTF snprintf
115#define TIXML_SSCANF sscanf
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800116#endif
Lee Thomason1ff38e02012-02-14 18:18:16 -0800117
selfpoisede77e1952013-03-13 14:08:29 +0800118static const int TIXML2_MAJOR_VERSION = 1;
119static const int TIXML2_MINOR_VERSION = 0;
120static const int TIXML2_PATCH_VERSION = 11;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800121
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800122namespace tinyxml2
123{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800124class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800125class XMLElement;
126class XMLAttribute;
127class XMLComment;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800128class XMLText;
Lee Thomason50f97b22012-02-11 16:33:40 -0800129class XMLDeclaration;
130class XMLUnknown;
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800131class XMLPrinter;
Lee Thomason5cae8972012-01-24 18:03:07 -0800132
U-Stream\Leeae25a442012-02-17 17:48:16 -0800133/*
134 A class that wraps strings. Normally stores the start and end
135 pointers into the XML file itself, and will apply normalization
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800136 and entity translation if actually read. Can also store (and memory
U-Stream\Leeae25a442012-02-17 17:48:16 -0800137 manage) a traditional char[]
138*/
PKEuS95060352013-07-26 10:42:44 +0200139class StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800140{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800141public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700142 enum {
143 NEEDS_ENTITY_PROCESSING = 0x01,
144 NEEDS_NEWLINE_NORMALIZATION = 0x02,
selfpoisede77e1952013-03-13 14:08:29 +0800145 COLLAPSE_WHITESPACE = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800146
selfpoisede77e1952013-03-13 14:08:29 +0800147 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700148 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
selfpoisede77e1952013-03-13 14:08:29 +0800149 ATTRIBUTE_NAME = 0,
150 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
151 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
152 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700153 };
Lee Thomason39ede242012-01-20 11:27:56 -0800154
Lee Thomason120b3a62012-10-12 10:06:59 -0700155 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700156 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800157
Lee Thomason120b3a62012-10-12 10:06:59 -0700158 void Set( char* start, char* end, int flags ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700159 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700160 _start = start;
161 _end = end;
162 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700163 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700164
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700165 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700166
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700167 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700168 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700169 }
Lee Thomason39ede242012-01-20 11:27:56 -0800170
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700171 void SetInternedStr( const char* str ) {
172 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700173 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700174 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700175
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700176 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800177
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700178 char* ParseText( char* in, const char* endTag, int strFlags );
179 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800180
Lee Thomason39ede242012-01-20 11:27:56 -0800181private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700182 void Reset();
183 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800184
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700185 enum {
186 NEEDS_FLUSH = 0x100,
187 NEEDS_DELETE = 0x200
188 };
Lee Thomasone4422302012-01-20 17:59:50 -0800189
selfpoised4dd59bc2013-03-13 16:54:15 +0800190 // After parsing, if *_end != 0, it can be set to zero.
Lee Thomason120b3a62012-10-12 10:06:59 -0700191 int _flags;
192 char* _start;
193 char* _end;
Lee Thomason39ede242012-01-20 11:27:56 -0800194};
195
U-Lama\Lee560bd472011-12-28 19:42:49 -0800196
U-Stream\Leeae25a442012-02-17 17:48:16 -0800197/*
198 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
199 Has a small initial memory pool, so that low or no usage will not
200 cause a call to new/delete
201*/
Lee Thomason2c85a712012-01-31 08:24:24 -0800202template <class T, int INIT>
PKEuS95060352013-07-26 10:42:44 +0200203class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800204{
205public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700206 DynArray< T, INIT >() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700207 _mem = _pool;
208 _allocated = INIT;
209 _size = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700210 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700211
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700212 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700213 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700214 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700215 }
216 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700217
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700218 void Push( T t ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700219 EnsureCapacity( _size+1 );
220 _mem[_size++] = t;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700221 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800222
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700223 T* PushArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700224 EnsureCapacity( _size+count );
225 T* ret = &_mem[_size];
226 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700227 return ret;
228 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700229
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700230 T Pop() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700231 return _mem[--_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700232 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700233
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700234 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700235 TIXMLASSERT( _size >= count );
236 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700237 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800238
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700239 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700240 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700241 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700242
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700243 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700244 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700245 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700246 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700247
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700248 const T& operator[](int i) const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700249 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700250 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700251 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700252
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700253 int Size() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700254 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700255 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700256
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700257 int Capacity() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700258 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700259 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700260
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700261 const T* Mem() const {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700262 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700263 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700264
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700265 T* Mem() {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700266 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700267 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800268
Lee Thomason2c85a712012-01-31 08:24:24 -0800269private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700270 void EnsureCapacity( int cap ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700271 if ( cap > _allocated ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700272 int newAllocated = cap * 2;
273 T* newMem = new T[newAllocated];
Lee Thomason624d43f2012-10-12 10:58:48 -0700274 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
275 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700276 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700277 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700278 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700279 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700280 }
281 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800282
Lee Thomason624d43f2012-10-12 10:58:48 -0700283 T* _mem;
284 T _pool[INIT];
285 int _allocated; // objects allocated
286 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800287};
288
Lee Thomason50adb4c2012-02-13 15:07:09 -0800289
U-Stream\Leeae25a442012-02-17 17:48:16 -0800290/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200291 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800292 and deallocation of objects.
293*/
PKEuS95060352013-07-26 10:42:44 +0200294class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800295{
296public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700297 MemPool() {}
298 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800299
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700300 virtual int ItemSize() const = 0;
301 virtual void* Alloc() = 0;
302 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800303 virtual void SetTracked() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800304};
305
Lee Thomason50adb4c2012-02-13 15:07:09 -0800306
U-Stream\Leeae25a442012-02-17 17:48:16 -0800307/*
308 Template child class to create pools of the correct type.
309*/
Lee Thomasond1983222012-02-06 08:41:24 -0800310template< int SIZE >
PKEuS95060352013-07-26 10:42:44 +0200311class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800312{
313public:
Lee Thomason5b0a6772012-11-19 13:54:42 -0800314 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700315 ~MemPoolT() {
316 // Delete the blocks.
Lee Thomason624d43f2012-10-12 10:58:48 -0700317 for( int i=0; i<_blockPtrs.Size(); ++i ) {
318 delete _blockPtrs[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700319 }
320 }
Lee Thomasond1983222012-02-06 08:41:24 -0800321
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700322 virtual int ItemSize() const {
323 return SIZE;
324 }
325 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700326 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700327 }
Lee Thomasond1983222012-02-06 08:41:24 -0800328
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700329 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700330 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700331 // Need a new block.
332 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700333 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800334
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700335 for( int i=0; i<COUNT-1; ++i ) {
336 block->chunk[i].next = &block->chunk[i+1];
337 }
338 block->chunk[COUNT-1].next = 0;
Lee Thomason624d43f2012-10-12 10:58:48 -0700339 _root = block->chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700340 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700341 void* result = _root;
342 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800343
Lee Thomason624d43f2012-10-12 10:58:48 -0700344 ++_currentAllocs;
345 if ( _currentAllocs > _maxAllocs ) {
346 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700347 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700348 _nAllocs++;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800349 _nUntracked++;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700350 return result;
351 }
352 virtual void Free( void* mem ) {
353 if ( !mem ) {
354 return;
355 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700356 --_currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700357 Chunk* chunk = (Chunk*)mem;
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700358#ifdef DEBUG
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700359 memset( chunk, 0xfe, sizeof(Chunk) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700360#endif
Lee Thomason624d43f2012-10-12 10:58:48 -0700361 chunk->next = _root;
362 _root = chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700363 }
364 void Trace( const char* name ) {
365 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Lee Thomason624d43f2012-10-12 10:58:48 -0700366 name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700367 }
Lee Thomasond1983222012-02-06 08:41:24 -0800368
Lee Thomason5b0a6772012-11-19 13:54:42 -0800369 void SetTracked() {
370 _nUntracked--;
371 }
372
373 int Untracked() const {
374 return _nUntracked;
375 }
376
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800377 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
378 // The test file is large, 170k.
379 // Release: VS2010 gcc(no opt)
380 // 1k: 4000
381 // 2k: 4000
382 // 4k: 3900 21000
383 // 16k: 5200
384 // 32k: 4300
385 // 64k: 4000 21000
386 enum { COUNT = (4*1024)/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private
Jerome Martinez7921df12012-10-24 11:45:44 +0200387
Lee Thomasond1983222012-02-06 08:41:24 -0800388private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700389 union Chunk {
Lee Thomason624d43f2012-10-12 10:58:48 -0700390 Chunk* next;
391 char mem[SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700392 };
393 struct Block {
Lee Thomason (grinliz)856da212012-10-19 09:08:15 -0700394 Chunk chunk[COUNT];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700395 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700396 DynArray< Block*, 10 > _blockPtrs;
397 Chunk* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800398
Lee Thomason624d43f2012-10-12 10:58:48 -0700399 int _currentAllocs;
400 int _nAllocs;
401 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800402 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800403};
404
Lee Thomason2c85a712012-01-31 08:24:24 -0800405
Lee Thomason56bdd022012-02-09 18:16:58 -0800406
407/**
408 Implements the interface to the "Visitor pattern" (see the Accept() method.)
409 If you call the Accept() method, it requires being passed a XMLVisitor
410 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200411 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800412 are simply called with Visit().
413
414 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700415 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800416
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700417 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800418 visiting). You need to only override methods that are interesting to you.
419
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600420 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800421
422 You should never change the document from a callback.
423
424 @sa XMLNode::Accept()
425*/
PKEuS16ed47d2013-07-06 12:02:43 +0200426class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800427{
428public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700429 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800430
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700431 /// Visit a document.
432 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
433 return true;
434 }
435 /// Visit a document.
436 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
437 return true;
438 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800439
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700440 /// Visit an element.
441 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
442 return true;
443 }
444 /// Visit an element.
445 virtual bool VisitExit( const XMLElement& /*element*/ ) {
446 return true;
447 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800448
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700449 /// Visit a declaration.
450 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
451 return true;
452 }
453 /// Visit a text node.
454 virtual bool Visit( const XMLText& /*text*/ ) {
455 return true;
456 }
457 /// Visit a comment node.
458 virtual bool Visit( const XMLComment& /*comment*/ ) {
459 return true;
460 }
461 /// Visit an unknown node.
462 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
463 return true;
464 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800465};
466
467
U-Stream\Leeae25a442012-02-17 17:48:16 -0800468/*
469 Utility functionality.
470*/
Lee Thomason56bdd022012-02-09 18:16:58 -0800471class XMLUtil
472{
Lee Thomasond1983222012-02-06 08:41:24 -0800473public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700474 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
475 // correct, but simple, and usually works.
476 static const char* SkipWhiteSpace( const char* p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100477 while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<const unsigned char*>(p) ) ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700478 ++p;
479 }
480 return p;
481 }
482 static char* SkipWhiteSpace( char* p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100483 while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<unsigned char*>(p) ) ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700484 ++p;
485 }
486 return p;
487 }
488 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100489 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700490 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200491
492 inline static bool IsNameStartChar( unsigned char ch ) {
493 return ( ( ch < 128 ) ? isalpha( ch ) : 1 )
494 || ch == ':'
495 || ch == '_';
496 }
497
498 inline static bool IsNameChar( unsigned char ch ) {
499 return IsNameStartChar( ch )
500 || isdigit( ch )
501 || ch == '.'
502 || ch == '-';
503 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800504
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700505 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
506 int n = 0;
507 if ( p == q ) {
508 return true;
509 }
510 while( *p && *q && *p == *q && n<nChar ) {
511 ++p;
512 ++q;
513 ++n;
514 }
515 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
516 return true;
517 }
518 return false;
519 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200520
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700521 inline static int IsUTF8Continuation( const char p ) {
522 return p & 0x80;
523 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800524
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700525 static const char* ReadBOM( const char* p, bool* hasBOM );
526 // p is the starting location,
527 // the UTF-8 value of the entity will be placed in value, and length filled in.
528 static const char* GetCharacterRef( const char* p, char* value, int* length );
529 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700530
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700531 // converts primitive types to strings
532 static void ToStr( int v, char* buffer, int bufferSize );
533 static void ToStr( unsigned v, char* buffer, int bufferSize );
534 static void ToStr( bool v, char* buffer, int bufferSize );
535 static void ToStr( float v, char* buffer, int bufferSize );
536 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason21be8822012-07-15 17:27:22 -0700537
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700538 // converts strings to primitive types
539 static bool ToInt( const char* str, int* value );
540 static bool ToUnsigned( const char* str, unsigned* value );
541 static bool ToBool( const char* str, bool* value );
542 static bool ToFloat( const char* str, float* value );
543 static bool ToDouble( const char* str, double* value );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800544};
545
Lee Thomason5cae8972012-01-24 18:03:07 -0800546
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800547/** XMLNode is a base class for every object that is in the
548 XML Document Object Model (DOM), except XMLAttributes.
549 Nodes have siblings, a parent, and children which can
550 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700551 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800552 be cast to its more defined type.
553
Thomas Roß08bdf502012-05-12 14:21:23 +0200554 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800555 When the XMLDocument gets deleted, all its Nodes
556 will also be deleted.
557
558 @verbatim
559 A Document can contain: Element (container or leaf)
560 Comment (leaf)
561 Unknown (leaf)
562 Declaration( leaf )
563
564 An Element can contain: Element (container or leaf)
565 Text (leaf)
566 Attributes (not on tree)
567 Comment (leaf)
568 Unknown (leaf)
569
570 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800571*/
PKEuS16ed47d2013-07-06 12:02:43 +0200572class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800573{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700574 friend class XMLDocument;
575 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800576public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800577
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700578 /// Get the XMLDocument that owns this XMLNode.
579 const XMLDocument* GetDocument() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700580 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700581 }
582 /// Get the XMLDocument that owns this XMLNode.
583 XMLDocument* GetDocument() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700584 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700585 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800586
Lee Thomason2fa81722012-11-09 12:37:46 -0800587 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700588 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100589 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700590 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800591 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700592 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100593 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700594 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800595 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700596 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100597 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700598 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800599 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700600 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100601 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700602 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800603 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700604 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100605 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700606 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800607 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700608 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100609 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700610 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800611
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700612 virtual const XMLElement* ToElement() const {
613 return 0;
614 }
615 virtual const XMLText* ToText() const {
616 return 0;
617 }
618 virtual const XMLComment* ToComment() const {
619 return 0;
620 }
621 virtual const XMLDocument* ToDocument() const {
622 return 0;
623 }
624 virtual const XMLDeclaration* ToDeclaration() const {
625 return 0;
626 }
627 virtual const XMLUnknown* ToUnknown() const {
628 return 0;
629 }
Lee Thomason751da522012-02-10 08:50:51 -0800630
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700631 /** The meaning of 'value' changes for the specific type.
632 @verbatim
633 Document: empty
634 Element: name of the element
635 Comment: the comment text
636 Unknown: the tag contents
637 Text: the text string
638 @endverbatim
639 */
640 const char* Value() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700641 return _value.GetStr();
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700642 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100643
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700644 /** Set the Value of an XML node.
645 @sa Value()
646 */
647 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800648
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700649 /// Get the parent of this node on the DOM.
650 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700651 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700652 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100653
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700654 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700655 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700656 }
Lee Thomason751da522012-02-10 08:50:51 -0800657
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700658 /// Returns true if this node has no children.
659 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700660 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700661 }
Lee Thomason751da522012-02-10 08:50:51 -0800662
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700663 /// Get the first child node, or null if none exists.
664 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700665 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700666 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100667
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700668 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700669 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700670 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100671
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700672 /** Get the first child element, or optionally the first child
673 element with the specified name.
674 */
675 const XMLElement* FirstChildElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700676
677 XMLElement* FirstChildElement( const char* value=0 ) {
678 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700679 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800680
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700681 /// Get the last child node, or null if none exists.
682 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700683 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700684 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700685
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700686 XMLNode* LastChild() {
687 return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() );
688 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800689
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700690 /** Get the last child element or optionally the last child
691 element with the specified name.
692 */
693 const XMLElement* LastChildElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700694
695 XMLElement* LastChildElement( const char* value=0 ) {
696 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700697 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700698
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700699 /// Get the previous (left) sibling node of this node.
700 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700701 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700703
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700704 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700705 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700706 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800707
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700708 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700709 const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700710
711 XMLElement* PreviousSiblingElement( const char* value=0 ) {
712 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700713 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700714
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700715 /// Get the next (right) sibling node of this node.
716 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700717 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700718 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700719
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700720 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700721 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700722 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700723
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700724 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700725 const XMLElement* NextSiblingElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700726
727 XMLElement* NextSiblingElement( const char* value=0 ) {
728 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700729 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800730
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700731 /**
732 Add a child node as the last (right) child.
733 */
734 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800735
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700736 XMLNode* LinkEndChild( XMLNode* addThis ) {
737 return InsertEndChild( addThis );
738 }
739 /**
740 Add a child node as the first (left) child.
741 */
742 XMLNode* InsertFirstChild( XMLNode* addThis );
743 /**
744 Add a node after the specified child node.
745 */
746 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700747
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700748 /**
749 Delete all the children of this node.
750 */
751 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800752
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700753 /**
754 Delete a child of this node.
755 */
756 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800757
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700758 /**
759 Make a copy of this node, but not its children.
760 You may pass in a Document pointer that will be
761 the owner of the new Node. If the 'document' is
762 null, then the node returned will be allocated
763 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800764
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700765 Note: if called on a XMLDocument, this will return null.
766 */
767 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800768
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700769 /**
770 Test if 2 nodes are the same, but don't test children.
771 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800772
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700773 Note: if called on a XMLDocument, this will return false.
774 */
775 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800776
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600777 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700778 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600779 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800780
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600781 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
782 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700783 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800784
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700785 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800786
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700787 - http://www.saxproject.org/
788 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800789
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700790 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800791
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700792 An example of using Accept():
793 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600794 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700795 tinyxmlDoc.Accept( &printer );
796 const char* xmlcstr = printer.CStr();
797 @endverbatim
798 */
799 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800800
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700801 // internal
802 virtual char* ParseDeep( char*, StrPair* );
Lee Thomason3f57d272012-01-11 15:30:03 -0800803
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800804protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700805 XMLNode( XMLDocument* );
806 virtual ~XMLNode();
807 XMLNode( const XMLNode& ); // not supported
808 XMLNode& operator=( const XMLNode& ); // not supported
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700809
Lee Thomason624d43f2012-10-12 10:58:48 -0700810 XMLDocument* _document;
811 XMLNode* _parent;
812 mutable StrPair _value;
Lee Thomason3f57d272012-01-11 15:30:03 -0800813
Lee Thomason624d43f2012-10-12 10:58:48 -0700814 XMLNode* _firstChild;
815 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800816
Lee Thomason624d43f2012-10-12 10:58:48 -0700817 XMLNode* _prev;
818 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800819
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800820private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700821 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700822 void Unlink( XMLNode* child );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800823};
824
825
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800826/** XML text.
827
828 Note that a text node can have child element nodes, for example:
829 @verbatim
830 <root>This is <b>bold</b></root>
831 @endverbatim
832
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700833 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800834 and CDATA. It will default to the mode it was parsed from the XML file and
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700835 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600836 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800837*/
PKEuS16ed47d2013-07-06 12:02:43 +0200838class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800839{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700840 friend class XMLBase;
841 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800842public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700843 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800844
Lee Thomason624d43f2012-10-12 10:58:48 -0700845 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700846 return this;
847 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700848 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700849 return this;
850 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800851
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700852 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700853 void SetCData( bool isCData ) {
854 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700855 }
856 /// Returns true if this is a CDATA text element.
857 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700858 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700859 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800860
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700861 char* ParseDeep( char*, StrPair* endTag );
862 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
863 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800864
Lee Thomason5492a1c2012-01-23 15:32:10 -0800865protected:
Lee Thomason624d43f2012-10-12 10:58:48 -0700866 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700867 virtual ~XMLText() {}
868 XMLText( const XMLText& ); // not supported
869 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -0800870
871private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700872 bool _isCData;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800873};
874
875
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800876/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +0200877class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800878{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700879 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -0800880public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700881 virtual XMLComment* ToComment() {
882 return this;
883 }
884 virtual const XMLComment* ToComment() const {
885 return this;
886 }
Lee Thomasonce0763e2012-01-11 15:43:54 -0800887
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700888 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800889
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700890 char* ParseDeep( char*, StrPair* endTag );
891 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
892 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -0800893
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800894protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700895 XMLComment( XMLDocument* doc );
896 virtual ~XMLComment();
897 XMLComment( const XMLComment& ); // not supported
898 XMLComment& operator=( const XMLComment& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800899
Lee Thomason3f57d272012-01-11 15:30:03 -0800900private:
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800901};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800902
903
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800904/** In correct XML the declaration is the first entry in the file.
905 @verbatim
906 <?xml version="1.0" standalone="yes"?>
907 @endverbatim
908
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600909 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800910 however.
911
912 The text of the declaration isn't interpreted. It is parsed
913 and written as a string.
914*/
PKEuS16ed47d2013-07-06 12:02:43 +0200915class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -0800916{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700917 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -0800918public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700919 virtual XMLDeclaration* ToDeclaration() {
920 return this;
921 }
922 virtual const XMLDeclaration* ToDeclaration() const {
923 return this;
924 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800925
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700926 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800927
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700928 char* ParseDeep( char*, StrPair* endTag );
929 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
930 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800931
932protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700933 XMLDeclaration( XMLDocument* doc );
934 virtual ~XMLDeclaration();
935 XMLDeclaration( const XMLDeclaration& ); // not supported
936 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -0800937};
938
939
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600940/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800941 unknown. It is a tag of text, but should not be modified.
942 It will be written back to the XML, unchanged, when the file
943 is saved.
944
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600945 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800946*/
PKEuS16ed47d2013-07-06 12:02:43 +0200947class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -0800948{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700949 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -0800950public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700951 virtual XMLUnknown* ToUnknown() {
952 return this;
953 }
954 virtual const XMLUnknown* ToUnknown() const {
955 return this;
956 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800957
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700958 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800959
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700960 char* ParseDeep( char*, StrPair* endTag );
961 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
962 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800963
964protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700965 XMLUnknown( XMLDocument* doc );
966 virtual ~XMLUnknown();
967 XMLUnknown( const XMLUnknown& ); // not supported
968 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -0800969};
970
971
Lee Thomason2fa81722012-11-09 12:37:46 -0800972enum XMLError {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700973 XML_NO_ERROR = 0,
974 XML_SUCCESS = 0,
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800975
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700976 XML_NO_ATTRIBUTE,
977 XML_WRONG_ATTRIBUTE_TYPE,
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800978
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700979 XML_ERROR_FILE_NOT_FOUND,
980 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
981 XML_ERROR_FILE_READ_ERROR,
982 XML_ERROR_ELEMENT_MISMATCH,
983 XML_ERROR_PARSING_ELEMENT,
984 XML_ERROR_PARSING_ATTRIBUTE,
985 XML_ERROR_IDENTIFYING_TAG,
986 XML_ERROR_PARSING_TEXT,
987 XML_ERROR_PARSING_CDATA,
988 XML_ERROR_PARSING_COMMENT,
989 XML_ERROR_PARSING_DECLARATION,
990 XML_ERROR_PARSING_UNKNOWN,
991 XML_ERROR_EMPTY_DOCUMENT,
992 XML_ERROR_MISMATCHED_ELEMENT,
993 XML_ERROR_PARSING,
Lee Thomason21be8822012-07-15 17:27:22 -0700994
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700995 XML_CAN_NOT_CONVERT_TEXT,
996 XML_NO_TEXT_NODE
Lee Thomason1ff38e02012-02-14 18:18:16 -0800997};
998
999
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001000/** An attribute is a name-value pair. Elements have an arbitrary
1001 number of attributes, each with a unique name.
1002
1003 @note The attributes are not XMLNodes. You may only query the
1004 Next() attribute in a list.
1005*/
PKEuS16ed47d2013-07-06 12:02:43 +02001006class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001007{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001008 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001009public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001010 /// The name of the attribute.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001011 const char* Name() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001012 return _name.GetStr();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001013 }
Lee Thomason2fa81722012-11-09 12:37:46 -08001014 /// The value of the attribute.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001015 const char* Value() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001016 return _value.GetStr();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001017 }
Lee Thomason2fa81722012-11-09 12:37:46 -08001018 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001019 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001020 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001021 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001022
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001023 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001024 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001025 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001026 */
1027 int IntValue() const {
1028 int i=0;
1029 QueryIntValue( &i );
1030 return i;
1031 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001032 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001033 unsigned UnsignedValue() const {
1034 unsigned i=0;
1035 QueryUnsignedValue( &i );
1036 return i;
1037 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001038 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001039 bool BoolValue() const {
1040 bool b=false;
1041 QueryBoolValue( &b );
1042 return b;
1043 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001044 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001045 double DoubleValue() const {
1046 double d=0;
1047 QueryDoubleValue( &d );
1048 return d;
1049 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001050 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001051 float FloatValue() const {
1052 float f=0;
1053 QueryFloatValue( &f );
1054 return f;
1055 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001056
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001057 /** QueryIntValue interprets the attribute as an integer, and returns the value
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001058 in the provided parameter. The function will return XML_NO_ERROR on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001059 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1060 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001061 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001062 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001063 XMLError QueryUnsignedValue( unsigned int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001064 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001065 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001066 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001067 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001068 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001069 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001070
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001071 /// Set the attribute to a string value.
1072 void SetAttribute( const char* value );
1073 /// Set the attribute to value.
1074 void SetAttribute( int value );
1075 /// Set the attribute to value.
1076 void SetAttribute( unsigned value );
1077 /// Set the attribute to value.
1078 void SetAttribute( bool value );
1079 /// Set the attribute to value.
1080 void SetAttribute( double value );
1081 /// Set the attribute to value.
1082 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001083
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001084private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001085 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001086
Thomas Roß61892312013-05-12 14:07:38 +02001087 XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001088 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001089
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001090 XMLAttribute( const XMLAttribute& ); // not supported
1091 void operator=( const XMLAttribute& ); // not supported
1092 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001093
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001094 char* ParseDeep( char* p, bool processEntities );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001095
Lee Thomason624d43f2012-10-12 10:58:48 -07001096 mutable StrPair _name;
1097 mutable StrPair _value;
1098 XMLAttribute* _next;
1099 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001100};
1101
1102
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001103/** The element is a container class. It has a value, the element name,
1104 and can contain other elements, text, comments, and unknowns.
1105 Elements also contain an arbitrary number of attributes.
1106*/
PKEuS16ed47d2013-07-06 12:02:43 +02001107class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001108{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001109 friend class XMLBase;
1110 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001111public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001112 /// Get the name of an element (which is the Value() of the node.)
1113 const char* Name() const {
1114 return Value();
1115 }
1116 /// Set the name of the element.
1117 void SetName( const char* str, bool staticMem=false ) {
1118 SetValue( str, staticMem );
1119 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001120
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001121 virtual XMLElement* ToElement() {
1122 return this;
1123 }
1124 virtual const XMLElement* ToElement() const {
1125 return this;
1126 }
1127 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001128
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001129 /** Given an attribute name, Attribute() returns the value
1130 for the attribute of that name, or null if none
1131 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001132
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001133 @verbatim
1134 const char* value = ele->Attribute( "foo" );
1135 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001136
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001137 The 'value' parameter is normally null. However, if specified,
1138 the attribute will only be returned if the 'name' and 'value'
1139 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001140
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001141 @verbatim
1142 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1143 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001144
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001145 rather than:
1146 @verbatim
1147 if ( ele->Attribute( "foo" ) ) {
1148 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1149 }
1150 @endverbatim
1151 */
1152 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001153
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001154 /** Given an attribute name, IntAttribute() returns the value
1155 of the attribute interpreted as an integer. 0 will be
1156 returned if there is an error. For a method with error
1157 checking, see QueryIntAttribute()
1158 */
1159 int IntAttribute( const char* name ) const {
1160 int i=0;
1161 QueryIntAttribute( name, &i );
1162 return i;
1163 }
1164 /// See IntAttribute()
1165 unsigned UnsignedAttribute( const char* name ) const {
1166 unsigned i=0;
1167 QueryUnsignedAttribute( name, &i );
1168 return i;
1169 }
1170 /// See IntAttribute()
1171 bool BoolAttribute( const char* name ) const {
1172 bool b=false;
1173 QueryBoolAttribute( name, &b );
1174 return b;
1175 }
1176 /// See IntAttribute()
1177 double DoubleAttribute( const char* name ) const {
1178 double d=0;
1179 QueryDoubleAttribute( name, &d );
1180 return d;
1181 }
1182 /// See IntAttribute()
1183 float FloatAttribute( const char* name ) const {
1184 float f=0;
1185 QueryFloatAttribute( name, &f );
1186 return f;
1187 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001188
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001189 /** Given an attribute name, QueryIntAttribute() returns
1190 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1191 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1192 doesn't exist. If successful, the result of the conversion
1193 will be written to 'value'. If not successful, nothing will
1194 be written to 'value'. This allows you to provide default
1195 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001196
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001197 @verbatim
1198 int value = 10;
1199 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1200 @endverbatim
1201 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001202 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001203 const XMLAttribute* a = FindAttribute( name );
1204 if ( !a ) {
1205 return XML_NO_ATTRIBUTE;
1206 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001207 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001208 }
1209 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001210 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001211 const XMLAttribute* a = FindAttribute( name );
1212 if ( !a ) {
1213 return XML_NO_ATTRIBUTE;
1214 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001215 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001216 }
1217 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001218 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001219 const XMLAttribute* a = FindAttribute( name );
1220 if ( !a ) {
1221 return XML_NO_ATTRIBUTE;
1222 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001223 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001224 }
1225 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001226 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001227 const XMLAttribute* a = FindAttribute( name );
1228 if ( !a ) {
1229 return XML_NO_ATTRIBUTE;
1230 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001231 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001232 }
1233 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001234 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001235 const XMLAttribute* a = FindAttribute( name );
1236 if ( !a ) {
1237 return XML_NO_ATTRIBUTE;
1238 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001239 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001240 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001241
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001242
1243 /** Given an attribute name, QueryAttribute() returns
1244 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1245 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1246 doesn't exist. It is overloaded for the primitive types,
1247 and is a generally more convenient replacement of
1248 QueryIntAttribute() and related functions.
1249
1250 If successful, the result of the conversion
1251 will be written to 'value'. If not successful, nothing will
1252 be written to 'value'. This allows you to provide default
1253 value:
1254
1255 @verbatim
1256 int value = 10;
1257 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1258 @endverbatim
1259 */
1260 int QueryAttribute( const char* name, int* value ) const {
1261 return QueryIntAttribute( name, value );
1262 }
1263
1264 int QueryAttribute( const char* name, unsigned int* value ) const {
1265 return QueryUnsignedAttribute( name, value );
1266 }
1267
1268 int QueryAttribute( const char* name, bool* value ) const {
1269 return QueryBoolAttribute( name, value );
1270 }
1271
1272 int QueryAttribute( const char* name, double* value ) const {
1273 return QueryDoubleAttribute( name, value );
1274 }
1275
1276 int QueryAttribute( const char* name, float* value ) const {
1277 return QueryFloatAttribute( name, value );
1278 }
1279
1280 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001281 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001282 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001283 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001284 }
1285 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001286 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001287 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001288 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001289 }
1290 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001291 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001292 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001293 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001294 }
1295 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001296 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001297 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001298 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001299 }
1300 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001301 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001302 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001303 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001304 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001305
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001306 /**
1307 Delete an attribute.
1308 */
1309 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001310
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001311 /// Return the first attribute in the list.
1312 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001313 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001314 }
1315 /// Query a specific attribute in the list.
1316 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001317
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001318 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001319 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001320 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001321
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001322 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001323 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001324
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001325 This is a convenient method for getting the text of simple contained text:
1326 @verbatim
1327 <foo>This is text</foo>
1328 const char* str = fooElement->GetText();
1329 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001330
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001331 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001332
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001333 Note that this function can be misleading. If the element foo was created from
1334 this XML:
1335 @verbatim
1336 <foo><b>This is text</b></foo>
1337 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001338
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001339 then the value of str would be null. The first child node isn't a text node, it is
1340 another element. From this XML:
1341 @verbatim
1342 <foo>This is <b>text</b></foo>
1343 @endverbatim
1344 GetText() will return "This is ".
1345 */
1346 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001347
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001348 /**
1349 Convenience method to query the value of a child text node. This is probably best
1350 shown by example. Given you have a document is this form:
1351 @verbatim
1352 <point>
1353 <x>1</x>
1354 <y>1.4</y>
1355 </point>
1356 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001357
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001358 The QueryIntText() and similar functions provide a safe and easier way to get to the
1359 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001360
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001361 @verbatim
1362 int x = 0;
1363 float y = 0; // types of x and y are contrived for example
1364 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1365 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1366 xElement->QueryIntText( &x );
1367 yElement->QueryFloatText( &y );
1368 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001369
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001370 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1371 to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001372
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001373 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001374 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001375 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001376 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001377 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001378 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001379 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001380 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001381 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001382 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001383
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001384 // internal:
1385 enum {
1386 OPEN, // <foo>
1387 CLOSED, // <foo/>
1388 CLOSING // </foo>
1389 };
1390 int ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001391 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001392 }
1393 char* ParseDeep( char* p, StrPair* endTag );
1394 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1395 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001396
Lee Thomason50adb4c2012-02-13 15:07:09 -08001397private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001398 XMLElement( XMLDocument* doc );
1399 virtual ~XMLElement();
1400 XMLElement( const XMLElement& ); // not supported
1401 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001402
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001403 XMLAttribute* FindAttribute( const char* name );
1404 XMLAttribute* FindOrCreateAttribute( const char* name );
1405 //void LinkAttribute( XMLAttribute* attrib );
1406 char* ParseAttributes( char* p );
Lee Thomason67d61312012-01-24 16:01:51 -08001407
Lee Thomason624d43f2012-10-12 10:58:48 -07001408 int _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001409 // The attribute list is ordered; there is no 'lastAttribute'
1410 // because the list needs to be scanned for dupes before adding
1411 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001412 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001413};
1414
1415
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001416enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001417 PRESERVE_WHITESPACE,
1418 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001419};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001420
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001421
1422/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001423 It can be saved, loaded, and printed to the screen.
1424 All Nodes are connected and allocated to a Document.
1425 If the Document is deleted, all its Nodes are also deleted.
1426*/
PKEuS16ed47d2013-07-06 12:02:43 +02001427class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001428{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001429 friend class XMLElement;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001430public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001431 /// constructor
1432 XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
1433 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001434
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001435 virtual XMLDocument* ToDocument() {
1436 return this;
1437 }
1438 virtual const XMLDocument* ToDocument() const {
1439 return this;
1440 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001441
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001442 /**
1443 Parse an XML file from a character string.
1444 Returns XML_NO_ERROR (0) on success, or
1445 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001446
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001447 You may optionally pass in the 'nBytes', which is
1448 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001449 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001450 null terminated string.
1451 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001452 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001453
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001454 /**
1455 Load an XML file from disk.
1456 Returns XML_NO_ERROR (0) on success, or
1457 an errorID.
1458 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001459 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001460
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001461 /**
1462 Load an XML file from disk. You are responsible
1463 for providing and closing the FILE*.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001464
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001465 Returns XML_NO_ERROR (0) on success, or
1466 an errorID.
1467 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001468 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001469
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001470 /**
1471 Save the XML file to disk.
1472 Returns XML_NO_ERROR (0) on success, or
1473 an errorID.
1474 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001475 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001476
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001477 /**
1478 Save the XML file to disk. You are responsible
1479 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001480
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001481 Returns XML_NO_ERROR (0) on success, or
1482 an errorID.
1483 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001484 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001485
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001486 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001487 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001488 }
1489 Whitespace WhitespaceMode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001490 return _whitespace;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001491 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001492
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001493 /**
1494 Returns true if this document has a leading Byte Order Mark of UTF8.
1495 */
1496 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001497 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001498 }
1499 /** Sets whether to write the BOM when writing the file.
1500 */
1501 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001502 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001503 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001504
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001505 /** Return the root element of DOM. Equivalent to FirstChildElement().
1506 To get the first node, use FirstChild().
1507 */
1508 XMLElement* RootElement() {
1509 return FirstChildElement();
1510 }
1511 const XMLElement* RootElement() const {
1512 return FirstChildElement();
1513 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001514
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001515 /** Print the Document. If the Printer is not provided, it will
1516 print to stdout. If you provide Printer, this can print to a file:
1517 @verbatim
1518 XMLPrinter printer( fp );
1519 doc.Print( &printer );
1520 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001521
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001522 Or you can use a printer to print to memory:
1523 @verbatim
1524 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001525 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001526 // printer.CStr() has a const char* to the XML
1527 @endverbatim
1528 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001529 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001530 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001531
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001532 /**
1533 Create a new Element associated with
1534 this Document. The memory for the Element
1535 is managed by the Document.
1536 */
1537 XMLElement* NewElement( const char* name );
1538 /**
1539 Create a new Comment associated with
1540 this Document. The memory for the Comment
1541 is managed by the Document.
1542 */
1543 XMLComment* NewComment( const char* comment );
1544 /**
1545 Create a new Text associated with
1546 this Document. The memory for the Text
1547 is managed by the Document.
1548 */
1549 XMLText* NewText( const char* text );
1550 /**
1551 Create a new Declaration associated with
1552 this Document. The memory for the object
1553 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001554
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001555 If the 'text' param is null, the standard
1556 declaration is used.:
1557 @verbatim
1558 <?xml version="1.0" encoding="UTF-8"?>
1559 @endverbatim
1560 */
1561 XMLDeclaration* NewDeclaration( const char* text=0 );
1562 /**
1563 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001564 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001565 is managed by the Document.
1566 */
1567 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001568
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001569 /**
1570 Delete a node associated with this document.
1571 It will be unlinked from the DOM.
1572 */
1573 void DeleteNode( XMLNode* node ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001574 node->_parent->DeleteChild( node );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001575 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001576
Lee Thomason2fa81722012-11-09 12:37:46 -08001577 void SetError( XMLError error, const char* str1, const char* str2 );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001578
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001579 /// Return true if there was an error parsing the document.
1580 bool Error() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001581 return _errorID != XML_NO_ERROR;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001582 }
1583 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001584 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001585 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001586 }
1587 /// Return a possibly helpful diagnostic location or string.
1588 const char* GetErrorStr1() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001589 return _errorStr1;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001590 }
1591 /// Return a possibly helpful secondary diagnostic location or string.
1592 const char* GetErrorStr2() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001593 return _errorStr2;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001594 }
1595 /// If there is an error, print it to stdout.
1596 void PrintError() const;
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001597
1598 /// Clear the document, resetting it to the initial state.
1599 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001600
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001601 // internal
1602 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001603
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001604 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1605 return 0;
1606 }
1607 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1608 return false;
1609 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001610
Lee Thomason3f57d272012-01-11 15:30:03 -08001611private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001612 XMLDocument( const XMLDocument& ); // not supported
1613 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001614
Lee Thomason2fa81722012-11-09 12:37:46 -08001615 bool _writeBOM;
1616 bool _processEntities;
1617 XMLError _errorID;
1618 Whitespace _whitespace;
Lee Thomason624d43f2012-10-12 10:58:48 -07001619 const char* _errorStr1;
1620 const char* _errorStr2;
Lee Thomason2fa81722012-11-09 12:37:46 -08001621 char* _charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -08001622
Lee Thomason624d43f2012-10-12 10:58:48 -07001623 MemPoolT< sizeof(XMLElement) > _elementPool;
1624 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1625 MemPoolT< sizeof(XMLText) > _textPool;
1626 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason5cae8972012-01-24 18:03:07 -08001627};
1628
Lee Thomason7c913cd2012-01-26 18:32:34 -08001629
Lee Thomason3ffdd392012-03-28 17:27:55 -07001630/**
1631 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001632 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001633 DOM structure. It is a separate utility class.
1634
1635 Take an example:
1636 @verbatim
1637 <Document>
1638 <Element attributeA = "valueA">
1639 <Child attributeB = "value1" />
1640 <Child attributeB = "value2" />
1641 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001642 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001643 @endverbatim
1644
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001645 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001646 easy to write a *lot* of code that looks like:
1647
1648 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001649 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001650 if ( root )
1651 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001652 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001653 if ( element )
1654 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001655 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001656 if ( child )
1657 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001658 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001659 if ( child2 )
1660 {
1661 // Finally do something useful.
1662 @endverbatim
1663
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001664 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001665 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001666 and correct to use:
1667
1668 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001669 XMLHandle docHandle( &document );
1670 XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001671 if ( child2 )
1672 {
1673 // do something useful
1674 @endverbatim
1675
1676 Which is MUCH more concise and useful.
1677
1678 It is also safe to copy handles - internally they are nothing more than node pointers.
1679 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001680 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001681 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001682
1683 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001684*/
PKEuS16ed47d2013-07-06 12:02:43 +02001685class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001686{
1687public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001688 /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
Lee Thomason624d43f2012-10-12 10:58:48 -07001689 XMLHandle( XMLNode* node ) {
1690 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001691 }
1692 /// Create a handle from a node.
Lee Thomason624d43f2012-10-12 10:58:48 -07001693 XMLHandle( XMLNode& node ) {
1694 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001695 }
1696 /// Copy constructor
1697 XMLHandle( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001698 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001699 }
1700 /// Assignment
1701 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001702 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001703 return *this;
1704 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001705
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001706 /// Get the first child of this handle.
1707 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001708 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001709 }
1710 /// Get the first child element of this handle.
1711 XMLHandle FirstChildElement( const char* value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001712 return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001713 }
1714 /// Get the last child of this handle.
1715 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001716 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001717 }
1718 /// Get the last child element of this handle.
1719 XMLHandle LastChildElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001720 return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001721 }
1722 /// Get the previous sibling of this handle.
1723 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001724 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001725 }
1726 /// Get the previous sibling element of this handle.
1727 XMLHandle PreviousSiblingElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001728 return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001729 }
1730 /// Get the next sibling of this handle.
1731 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001732 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001733 }
1734 /// Get the next sibling element of this handle.
1735 XMLHandle NextSiblingElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001736 return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001737 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001738
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001739 /// Safe cast to XMLNode. This can return null.
1740 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001741 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001742 }
1743 /// Safe cast to XMLElement. This can return null.
1744 XMLElement* ToElement() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001745 return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001746 }
1747 /// Safe cast to XMLText. This can return null.
1748 XMLText* ToText() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001749 return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001750 }
1751 /// Safe cast to XMLUnknown. This can return null.
1752 XMLUnknown* ToUnknown() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001753 return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001754 }
1755 /// Safe cast to XMLDeclaration. This can return null.
1756 XMLDeclaration* ToDeclaration() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001757 return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001758 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001759
1760private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001761 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001762};
1763
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001764
1765/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001766 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
1767 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07001768*/
PKEuS16ed47d2013-07-06 12:02:43 +02001769class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07001770{
1771public:
Lee Thomason624d43f2012-10-12 10:58:48 -07001772 XMLConstHandle( const XMLNode* node ) {
1773 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001774 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001775 XMLConstHandle( const XMLNode& node ) {
1776 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001777 }
1778 XMLConstHandle( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001779 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001780 }
Lee Thomason8b899812012-04-04 15:58:16 -07001781
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001782 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001783 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001784 return *this;
1785 }
Lee Thomason8b899812012-04-04 15:58:16 -07001786
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001787 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001788 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001789 }
1790 const XMLConstHandle FirstChildElement( const char* value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001791 return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001792 }
1793 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001794 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001795 }
1796 const XMLConstHandle LastChildElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001797 return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001798 }
1799 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001800 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001801 }
1802 const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001803 return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001804 }
1805 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001806 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001807 }
1808 const XMLConstHandle NextSiblingElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001809 return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001810 }
Lee Thomason8b899812012-04-04 15:58:16 -07001811
1812
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001813 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001814 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001815 }
1816 const XMLElement* ToElement() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001817 return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001818 }
1819 const XMLText* ToText() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001820 return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001821 }
1822 const XMLUnknown* ToUnknown() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001823 return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001824 }
1825 const XMLDeclaration* ToDeclaration() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001826 return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001827 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001828
Lee Thomason5cae8972012-01-24 18:03:07 -08001829private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001830 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08001831};
Lee Thomason6f381b72012-03-02 12:59:39 -08001832
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001833
1834/**
1835 Printing functionality. The XMLPrinter gives you more
1836 options than the XMLDocument::Print() method.
1837
1838 It can:
1839 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02001840 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001841 -# Print XML without a XMLDocument.
1842
1843 Print to Memory
1844
1845 @verbatim
1846 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001847 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02001848 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001849 @endverbatim
1850
1851 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001852
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001853 You provide the file pointer.
1854 @verbatim
1855 XMLPrinter printer( fp );
1856 doc.Print( &printer );
1857 @endverbatim
1858
1859 Print without a XMLDocument
1860
1861 When loading, an XML parser is very useful. However, sometimes
1862 when saving, it just gets in the way. The code is often set up
1863 for streaming, and constructing the DOM is just overhead.
1864
1865 The Printer supports the streaming case. The following code
1866 prints out a trivially simple XML file without ever creating
1867 an XML document.
1868
1869 @verbatim
1870 XMLPrinter printer( fp );
1871 printer.OpenElement( "foo" );
1872 printer.PushAttribute( "foo", "bar" );
1873 printer.CloseElement();
1874 @endverbatim
1875*/
PKEuS16ed47d2013-07-06 12:02:43 +02001876class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001877{
1878public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001879 /** Construct the printer. If the FILE* is specified,
1880 this will print to the FILE. Else it will print
1881 to memory, and the result is available in CStr().
1882 If 'compact' is set to true, then output is created
1883 with only required whitespace and newlines.
1884 */
PKEuS1bfb9542013-08-04 13:51:17 +02001885 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001886 ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001887
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001888 /** If streaming, write the BOM and declaration. */
1889 void PushHeader( bool writeBOM, bool writeDeclaration );
1890 /** If streaming, start writing an element.
1891 The element must be closed with CloseElement()
1892 */
1893 void OpenElement( const char* name );
1894 /// If streaming, add an attribute to an open element.
1895 void PushAttribute( const char* name, const char* value );
1896 void PushAttribute( const char* name, int value );
1897 void PushAttribute( const char* name, unsigned value );
1898 void PushAttribute( const char* name, bool value );
1899 void PushAttribute( const char* name, double value );
1900 /// If streaming, close the Element.
1901 void CloseElement();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001902
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001903 /// Add a text node.
1904 void PushText( const char* text, bool cdata=false );
1905 /// Add a text node from an integer.
1906 void PushText( int value );
1907 /// Add a text node from an unsigned.
1908 void PushText( unsigned value );
1909 /// Add a text node from a bool.
1910 void PushText( bool value );
1911 /// Add a text node from a float.
1912 void PushText( float value );
1913 /// Add a text node from a double.
1914 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07001915
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001916 /// Add a comment
1917 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001918
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001919 void PushDeclaration( const char* value );
1920 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001921
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001922 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
1923 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
1924 return true;
1925 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001926
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001927 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
1928 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001929
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001930 virtual bool Visit( const XMLText& text );
1931 virtual bool Visit( const XMLComment& comment );
1932 virtual bool Visit( const XMLDeclaration& declaration );
1933 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001934
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001935 /**
1936 If in print to memory mode, return a pointer to
1937 the XML file in memory.
1938 */
1939 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001940 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001941 }
1942 /**
1943 If in print to memory mode, return the size
1944 of the XML file in memory. (Note the size returned
1945 includes the terminating null.)
1946 */
1947 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001948 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001949 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001950
1951private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001952 void SealElement();
1953 void PrintSpace( int depth );
1954 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
1955 void Print( const char* format, ... );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001956
Lee Thomason624d43f2012-10-12 10:58:48 -07001957 bool _elementJustOpened;
1958 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001959 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07001960 int _depth;
1961 int _textDepth;
1962 bool _processEntities;
1963 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001964
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001965 enum {
1966 ENTITY_RANGE = 64,
1967 BUF_SIZE = 200
1968 };
Lee Thomason624d43f2012-10-12 10:58:48 -07001969 bool _entityFlag[ENTITY_RANGE];
1970 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001971
Lee Thomason624d43f2012-10-12 10:58:48 -07001972 DynArray< const char*, 10 > _stack;
1973 DynArray< char, 20 > _buffer;
PKEuSe736f292012-07-16 03:27:55 -07001974#ifdef _MSC_VER
Lee Thomason624d43f2012-10-12 10:58:48 -07001975 DynArray< char, 20 > _accumulator;
PKEuSe736f292012-07-16 03:27:55 -07001976#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001977};
1978
1979
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07001980} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001981
PKEuS95060352013-07-26 10:42:44 +02001982#if defined(_MSC_VER)
1983# pragma warning(pop)
1984#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001985
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001986#endif // TINYXML2_INCLUDED