blob: a0d2693938a83f42334f39c43257b7304bef8dc8 [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
Anton Indrawanf59e2d62014-11-18 20:50:42 +010027#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
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>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070033#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -070034# include <cctype>
35# include <climits>
36# include <cstdio>
37# include <cstdlib>
38# include <cstring>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070039#endif
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070040
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -070041/*
Lee Thomason7d00b9a2012-02-27 17:54:22 -080042 TODO: intern strings instead of allocation.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080043*/
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080044/*
Lee Thomasona9cf3f92012-10-11 16:56:51 -070045 gcc:
Lee Thomason5b0a6772012-11-19 13:54:42 -080046 g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
MortenMacFly4ee49f12013-01-14 20:03:14 +010047
Lee Thomasona9cf3f92012-10-11 16:56:51 -070048 Formatting, Artistic Style:
49 AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080050*/
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080051
U-Lama\Lee4cee6112011-12-31 14:58:18 -080052#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070053# ifndef DEBUG
54# define DEBUG
55# endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080056#endif
57
PKEuS95060352013-07-26 10:42:44 +020058#ifdef _MSC_VER
59# pragma warning(push)
60# pragma warning(disable: 4251)
61#endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080062
PKEuS16ed47d2013-07-06 12:02:43 +020063#ifdef _WIN32
64# ifdef TINYXML2_EXPORT
65# define TINYXML2_LIB __declspec(dllexport)
66# elif defined(TINYXML2_IMPORT)
67# define TINYXML2_LIB __declspec(dllimport)
68# else
69# define TINYXML2_LIB
70# endif
71#else
72# define TINYXML2_LIB
73#endif
74
75
U-Lama\Lee4cee6112011-12-31 14:58:18 -080076#if defined(DEBUG)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070077# if defined(_MSC_VER)
Dmitry-Me4bcbf142014-12-25 19:05:18 +030078# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
Lee Thomason598a88d2015-10-09 14:42:12 -070079# define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
Lee Thomasona9cf3f92012-10-11 16:56:51 -070080# elif defined (ANDROID_NDK)
81# include <android/log.h>
82# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
83# else
84# include <assert.h>
85# define TIXMLASSERT assert
86# endif
Lee Thomason598a88d2015-10-09 14:42:12 -070087#else
88# define TIXMLASSERT( x ) {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080089#endif
90
U-Lama\Leee13c3e62011-12-28 14:36:55 -080091
Lee Thomasonc18eb232014-02-21 17:31:17 -080092/* Versioning, past 1.0.14:
Lee Thomason85afe9c2014-02-23 21:42:16 -080093 http://semver.org/
Lee Thomasonc18eb232014-02-21 17:31:17 -080094*/
Sarat Addepalli9afd1d02015-05-19 12:56:27 +053095static const int TIXML2_MAJOR_VERSION = 3;
96static const int TIXML2_MINOR_VERSION = 0;
97static const int TIXML2_PATCH_VERSION = 0;
Lee Thomason1ff38e02012-02-14 18:18:16 -080098
U-Lama\Leee13c3e62011-12-28 14:36:55 -080099namespace tinyxml2
100{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800101class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800102class XMLElement;
103class XMLAttribute;
104class XMLComment;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800105class XMLText;
Lee Thomason50f97b22012-02-11 16:33:40 -0800106class XMLDeclaration;
107class XMLUnknown;
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800108class XMLPrinter;
Lee Thomason5cae8972012-01-24 18:03:07 -0800109
U-Stream\Leeae25a442012-02-17 17:48:16 -0800110/*
111 A class that wraps strings. Normally stores the start and end
112 pointers into the XML file itself, and will apply normalization
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800113 and entity translation if actually read. Can also store (and memory
U-Stream\Leeae25a442012-02-17 17:48:16 -0800114 manage) a traditional char[]
115*/
PKEuS95060352013-07-26 10:42:44 +0200116class StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800117{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800118public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700119 enum {
120 NEEDS_ENTITY_PROCESSING = 0x01,
121 NEEDS_NEWLINE_NORMALIZATION = 0x02,
Dmitry-Me5420e542015-05-20 10:51:26 +0300122 NEEDS_WHITESPACE_COLLAPSING = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800123
selfpoisede77e1952013-03-13 14:08:29 +0800124 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700125 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
selfpoisede77e1952013-03-13 14:08:29 +0800126 ATTRIBUTE_NAME = 0,
127 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
128 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
129 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700130 };
Lee Thomason39ede242012-01-20 11:27:56 -0800131
Lee Thomason120b3a62012-10-12 10:06:59 -0700132 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700133 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800134
Lee Thomason120b3a62012-10-12 10:06:59 -0700135 void Set( char* start, char* end, int flags ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700136 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700137 _start = start;
138 _end = end;
139 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700140 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700141
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700142 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700143
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700144 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700145 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700146 }
Lee Thomason39ede242012-01-20 11:27:56 -0800147
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700148 void SetInternedStr( const char* str ) {
149 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700150 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700151 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700152
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700153 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800154
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700155 char* ParseText( char* in, const char* endTag, int strFlags );
156 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800157
Lee Thomason29658802014-11-27 22:31:11 -0800158 void TransferTo( StrPair* other );
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300159
Lee Thomason39ede242012-01-20 11:27:56 -0800160private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700161 void Reset();
162 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800163
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700164 enum {
165 NEEDS_FLUSH = 0x100,
166 NEEDS_DELETE = 0x200
167 };
Lee Thomasone4422302012-01-20 17:59:50 -0800168
Lee Thomason120b3a62012-10-12 10:06:59 -0700169 int _flags;
170 char* _start;
171 char* _end;
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300172
173 StrPair( const StrPair& other ); // not supported
174 void operator=( StrPair& other ); // not supported, use TransferTo()
Lee Thomason39ede242012-01-20 11:27:56 -0800175};
176
U-Lama\Lee560bd472011-12-28 19:42:49 -0800177
U-Stream\Leeae25a442012-02-17 17:48:16 -0800178/*
179 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
180 Has a small initial memory pool, so that low or no usage will not
181 cause a call to new/delete
182*/
Dmitry-Me04009222015-04-06 18:07:18 +0300183template <class T, int INITIAL_SIZE>
PKEuS95060352013-07-26 10:42:44 +0200184class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800185{
186public:
Dmitry-Me9a5a48d2015-01-14 08:27:32 +0300187 DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700188 _mem = _pool;
Dmitry-Me04009222015-04-06 18:07:18 +0300189 _allocated = INITIAL_SIZE;
Lee Thomason624d43f2012-10-12 10:58:48 -0700190 _size = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700191 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700192
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700193 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700194 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700195 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700196 }
197 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700198
Lee Thomasonce0510b2013-11-26 21:29:37 -0800199 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100200 _size = 0;
201 }
202
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700203 void Push( T t ) {
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300204 TIXMLASSERT( _size < INT_MAX );
Lee Thomason624d43f2012-10-12 10:58:48 -0700205 EnsureCapacity( _size+1 );
206 _mem[_size++] = t;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700207 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800208
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700209 T* PushArr( int count ) {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300210 TIXMLASSERT( count >= 0 );
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300211 TIXMLASSERT( _size <= INT_MAX - count );
Lee Thomason624d43f2012-10-12 10:58:48 -0700212 EnsureCapacity( _size+count );
213 T* ret = &_mem[_size];
214 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700215 return ret;
216 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700217
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700218 T Pop() {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300219 TIXMLASSERT( _size > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700220 return _mem[--_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700221 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700222
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700223 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700224 TIXMLASSERT( _size >= count );
225 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700226 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800227
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700228 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700229 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700230 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700231
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700232 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700233 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700234 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700235 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700236
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700237 const T& operator[](int i) const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700238 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700239 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700240 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700241
Lee Thomasonf07b9522014-10-30 13:25:12 -0700242 const T& PeekTop() const {
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500243 TIXMLASSERT( _size > 0 );
244 return _mem[ _size - 1];
245 }
246
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700247 int Size() const {
Dmitry-Me30bdc972015-01-14 08:32:23 +0300248 TIXMLASSERT( _size >= 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700249 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700250 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700251
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700252 int Capacity() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300253 TIXMLASSERT( _allocated >= INITIAL_SIZE );
Lee Thomason624d43f2012-10-12 10:58:48 -0700254 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700255 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700256
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700257 const T* Mem() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300258 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700259 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700260 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700261
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700262 T* Mem() {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300263 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700264 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700265 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800266
Lee Thomason2c85a712012-01-31 08:24:24 -0800267private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300268 DynArray( const DynArray& ); // not supported
269 void operator=( const DynArray& ); // not supported
270
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700271 void EnsureCapacity( int cap ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300272 TIXMLASSERT( cap > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700273 if ( cap > _allocated ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300274 TIXMLASSERT( cap <= INT_MAX / 2 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700275 int newAllocated = cap * 2;
276 T* newMem = new T[newAllocated];
Lee Thomason624d43f2012-10-12 10:58:48 -0700277 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
278 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700279 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700280 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700281 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700282 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700283 }
284 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800285
Lee Thomason624d43f2012-10-12 10:58:48 -0700286 T* _mem;
Dmitry-Me04009222015-04-06 18:07:18 +0300287 T _pool[INITIAL_SIZE];
Lee Thomason624d43f2012-10-12 10:58:48 -0700288 int _allocated; // objects allocated
289 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800290};
291
Lee Thomason50adb4c2012-02-13 15:07:09 -0800292
U-Stream\Leeae25a442012-02-17 17:48:16 -0800293/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200294 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800295 and deallocation of objects.
296*/
PKEuS95060352013-07-26 10:42:44 +0200297class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800298{
299public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700300 MemPool() {}
301 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800302
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700303 virtual int ItemSize() const = 0;
304 virtual void* Alloc() = 0;
305 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800306 virtual void SetTracked() = 0;
Lee Thomasonf07b9522014-10-30 13:25:12 -0700307 virtual void Clear() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800308};
309
Lee Thomason50adb4c2012-02-13 15:07:09 -0800310
U-Stream\Leeae25a442012-02-17 17:48:16 -0800311/*
312 Template child class to create pools of the correct type.
313*/
Lee Thomasond1983222012-02-06 08:41:24 -0800314template< int SIZE >
PKEuS95060352013-07-26 10:42:44 +0200315class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800316{
317public:
Lee Thomason5b0a6772012-11-19 13:54:42 -0800318 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700319 ~MemPoolT() {
Lee Thomasonf07b9522014-10-30 13:25:12 -0700320 Clear();
321 }
322
323 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700324 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700325 while( !_blockPtrs.Empty()) {
326 Block* b = _blockPtrs.Pop();
327 delete b;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700328 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700329 _root = 0;
330 _currentAllocs = 0;
331 _nAllocs = 0;
332 _maxAllocs = 0;
333 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700334 }
Lee Thomasond1983222012-02-06 08:41:24 -0800335
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700336 virtual int ItemSize() const {
337 return SIZE;
338 }
339 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700340 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700341 }
Lee Thomasond1983222012-02-06 08:41:24 -0800342
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700343 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700344 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700345 // Need a new block.
346 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700347 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800348
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700349 for( int i=0; i<COUNT-1; ++i ) {
350 block->chunk[i].next = &block->chunk[i+1];
351 }
352 block->chunk[COUNT-1].next = 0;
Lee Thomason624d43f2012-10-12 10:58:48 -0700353 _root = block->chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700354 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700355 void* result = _root;
356 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800357
Lee Thomason624d43f2012-10-12 10:58:48 -0700358 ++_currentAllocs;
359 if ( _currentAllocs > _maxAllocs ) {
360 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700361 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700362 _nAllocs++;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800363 _nUntracked++;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700364 return result;
365 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700366
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700367 virtual void Free( void* mem ) {
368 if ( !mem ) {
369 return;
370 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700371 --_currentAllocs;
Dmitry-Me56571762014-08-15 11:03:47 +0400372 Chunk* chunk = static_cast<Chunk*>( mem );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700373#ifdef DEBUG
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700374 memset( chunk, 0xfe, sizeof(Chunk) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700375#endif
Lee Thomason624d43f2012-10-12 10:58:48 -0700376 chunk->next = _root;
377 _root = chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700378 }
379 void Trace( const char* name ) {
380 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Lee Thomason624d43f2012-10-12 10:58:48 -0700381 name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700382 }
Lee Thomasond1983222012-02-06 08:41:24 -0800383
Lee Thomason5b0a6772012-11-19 13:54:42 -0800384 void SetTracked() {
385 _nUntracked--;
386 }
387
388 int Untracked() const {
389 return _nUntracked;
390 }
391
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800392 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
393 // The test file is large, 170k.
394 // Release: VS2010 gcc(no opt)
395 // 1k: 4000
396 // 2k: 4000
397 // 4k: 3900 21000
398 // 16k: 5200
399 // 32k: 4300
400 // 64k: 4000 21000
401 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 +0200402
Lee Thomasond1983222012-02-06 08:41:24 -0800403private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300404 MemPoolT( const MemPoolT& ); // not supported
405 void operator=( const MemPoolT& ); // not supported
406
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700407 union Chunk {
Lee Thomason624d43f2012-10-12 10:58:48 -0700408 Chunk* next;
409 char mem[SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700410 };
411 struct Block {
Lee Thomason (grinliz)856da212012-10-19 09:08:15 -0700412 Chunk chunk[COUNT];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700413 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700414 DynArray< Block*, 10 > _blockPtrs;
415 Chunk* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800416
Lee Thomason624d43f2012-10-12 10:58:48 -0700417 int _currentAllocs;
418 int _nAllocs;
419 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800420 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800421};
422
Lee Thomason2c85a712012-01-31 08:24:24 -0800423
Lee Thomason56bdd022012-02-09 18:16:58 -0800424
425/**
426 Implements the interface to the "Visitor pattern" (see the Accept() method.)
427 If you call the Accept() method, it requires being passed a XMLVisitor
428 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200429 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800430 are simply called with Visit().
431
432 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700433 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800434
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700435 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800436 visiting). You need to only override methods that are interesting to you.
437
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600438 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800439
440 You should never change the document from a callback.
441
442 @sa XMLNode::Accept()
443*/
PKEuS16ed47d2013-07-06 12:02:43 +0200444class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800445{
446public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700447 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800448
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700449 /// Visit a document.
450 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
451 return true;
452 }
453 /// Visit a document.
454 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
455 return true;
456 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800457
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700458 /// Visit an element.
459 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
460 return true;
461 }
462 /// Visit an element.
463 virtual bool VisitExit( const XMLElement& /*element*/ ) {
464 return true;
465 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800466
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700467 /// Visit a declaration.
468 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
469 return true;
470 }
471 /// Visit a text node.
472 virtual bool Visit( const XMLText& /*text*/ ) {
473 return true;
474 }
475 /// Visit a comment node.
476 virtual bool Visit( const XMLComment& /*comment*/ ) {
477 return true;
478 }
479 /// Visit an unknown node.
480 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
481 return true;
482 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800483};
484
Dmitry-Me66d2a842014-11-08 15:24:52 +0300485// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900486enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900487 XML_SUCCESS = 0,
488 XML_NO_ERROR = 0,
489 XML_NO_ATTRIBUTE,
490 XML_WRONG_ATTRIBUTE_TYPE,
491 XML_ERROR_FILE_NOT_FOUND,
492 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
493 XML_ERROR_FILE_READ_ERROR,
494 XML_ERROR_ELEMENT_MISMATCH,
495 XML_ERROR_PARSING_ELEMENT,
496 XML_ERROR_PARSING_ATTRIBUTE,
497 XML_ERROR_IDENTIFYING_TAG,
498 XML_ERROR_PARSING_TEXT,
499 XML_ERROR_PARSING_CDATA,
500 XML_ERROR_PARSING_COMMENT,
501 XML_ERROR_PARSING_DECLARATION,
502 XML_ERROR_PARSING_UNKNOWN,
503 XML_ERROR_EMPTY_DOCUMENT,
504 XML_ERROR_MISMATCHED_ELEMENT,
505 XML_ERROR_PARSING,
506 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700507 XML_NO_TEXT_NODE,
508
509 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900510};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900511
numatrumpetcd8550c2014-09-08 16:59:39 +0900512
U-Stream\Leeae25a442012-02-17 17:48:16 -0800513/*
514 Utility functionality.
515*/
Lee Thomason56bdd022012-02-09 18:16:58 -0800516class XMLUtil
517{
Lee Thomasond1983222012-02-06 08:41:24 -0800518public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700519 static const char* SkipWhiteSpace( const char* p ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300520 TIXMLASSERT( p );
Dmitry-Mefa20b222014-10-31 12:53:04 +0300521 while( IsWhiteSpace(*p) ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700522 ++p;
523 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300524 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700525 return p;
526 }
527 static char* SkipWhiteSpace( char* p ) {
Dmitry-Me9de541f2014-09-24 14:21:36 +0400528 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p) ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700529 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300530
531 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
532 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700533 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100534 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700535 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200536
537 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300538 if ( ch >= 128 ) {
539 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
540 return true;
541 }
542 if ( isalpha( ch ) ) {
543 return true;
544 }
545 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200546 }
547
548 inline static bool IsNameChar( unsigned char ch ) {
549 return IsNameStartChar( ch )
550 || isdigit( ch )
551 || ch == '.'
552 || ch == '-';
553 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800554
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700555 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700556 if ( p == q ) {
557 return true;
558 }
Lee Thomason598a88d2015-10-09 14:42:12 -0700559 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700560 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200561
Dmitry-Me7865aad2015-06-19 16:23:35 +0300562 inline static bool IsUTF8Continuation( char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400563 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700564 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800565
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700566 static const char* ReadBOM( const char* p, bool* hasBOM );
567 // p is the starting location,
568 // the UTF-8 value of the entity will be placed in value, and length filled in.
569 static const char* GetCharacterRef( const char* p, char* value, int* length );
570 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700571
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700572 // converts primitive types to strings
573 static void ToStr( int v, char* buffer, int bufferSize );
574 static void ToStr( unsigned v, char* buffer, int bufferSize );
575 static void ToStr( bool v, char* buffer, int bufferSize );
576 static void ToStr( float v, char* buffer, int bufferSize );
577 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason21be8822012-07-15 17:27:22 -0700578
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700579 // converts strings to primitive types
580 static bool ToInt( const char* str, int* value );
581 static bool ToUnsigned( const char* str, unsigned* value );
582 static bool ToBool( const char* str, bool* value );
583 static bool ToFloat( const char* str, float* value );
584 static bool ToDouble( const char* str, double* value );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800585};
586
Lee Thomason5cae8972012-01-24 18:03:07 -0800587
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800588/** XMLNode is a base class for every object that is in the
589 XML Document Object Model (DOM), except XMLAttributes.
590 Nodes have siblings, a parent, and children which can
591 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700592 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800593 be cast to its more defined type.
594
Thomas Roß08bdf502012-05-12 14:21:23 +0200595 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800596 When the XMLDocument gets deleted, all its Nodes
597 will also be deleted.
598
599 @verbatim
600 A Document can contain: Element (container or leaf)
601 Comment (leaf)
602 Unknown (leaf)
603 Declaration( leaf )
604
605 An Element can contain: Element (container or leaf)
606 Text (leaf)
607 Attributes (not on tree)
608 Comment (leaf)
609 Unknown (leaf)
610
611 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800612*/
PKEuS16ed47d2013-07-06 12:02:43 +0200613class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800614{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700615 friend class XMLDocument;
616 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800617public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800618
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700619 /// Get the XMLDocument that owns this XMLNode.
620 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300621 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700622 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700623 }
624 /// Get the XMLDocument that owns this XMLNode.
625 XMLDocument* GetDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300626 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700627 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700628 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800629
Lee Thomason2fa81722012-11-09 12:37:46 -0800630 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700631 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100632 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700633 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800634 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700635 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100636 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700637 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800638 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700639 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100640 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700641 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800642 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700643 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100644 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700645 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800646 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700647 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100648 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700649 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800650 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700651 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100652 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700653 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800654
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700655 virtual const XMLElement* ToElement() const {
656 return 0;
657 }
658 virtual const XMLText* ToText() const {
659 return 0;
660 }
661 virtual const XMLComment* ToComment() const {
662 return 0;
663 }
664 virtual const XMLDocument* ToDocument() const {
665 return 0;
666 }
667 virtual const XMLDeclaration* ToDeclaration() const {
668 return 0;
669 }
670 virtual const XMLUnknown* ToUnknown() const {
671 return 0;
672 }
Lee Thomason751da522012-02-10 08:50:51 -0800673
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700674 /** The meaning of 'value' changes for the specific type.
675 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530676 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700677 Element: name of the element
678 Comment: the comment text
679 Unknown: the tag contents
680 Text: the text string
681 @endverbatim
682 */
Michael Daumling21626882013-10-22 17:03:37 +0200683 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100684
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700685 /** Set the Value of an XML node.
686 @sa Value()
687 */
688 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800689
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700690 /// Get the parent of this node on the DOM.
691 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700692 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700693 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100694
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700695 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700696 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700697 }
Lee Thomason751da522012-02-10 08:50:51 -0800698
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700699 /// Returns true if this node has no children.
700 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700701 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 }
Lee Thomason751da522012-02-10 08:50:51 -0800703
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700704 /// Get the first child node, or null if none exists.
705 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700706 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700707 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100708
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700709 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700710 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700711 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100712
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700713 /** Get the first child element, or optionally the first child
714 element with the specified name.
715 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300716 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700717
Dmitry-Me886ad972015-07-22 11:00:51 +0300718 XMLElement* FirstChildElement( const char* name = 0 ) {
719 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700720 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800721
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700722 /// Get the last child node, or null if none exists.
723 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700724 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700725 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700726
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700727 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300728 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700729 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800730
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700731 /** Get the last child element or optionally the last child
732 element with the specified name.
733 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300734 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700735
Dmitry-Me886ad972015-07-22 11:00:51 +0300736 XMLElement* LastChildElement( const char* name = 0 ) {
737 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700738 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700739
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700740 /// Get the previous (left) sibling node of this node.
741 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700742 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700743 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700744
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700745 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700746 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700747 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800748
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700749 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300750 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700751
Dmitry-Me886ad972015-07-22 11:00:51 +0300752 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
753 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700754 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700755
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700756 /// Get the next (right) sibling node of this node.
757 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700758 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700759 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700760
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700761 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700762 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700763 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700764
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700765 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300766 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700767
Dmitry-Me886ad972015-07-22 11:00:51 +0300768 XMLElement* NextSiblingElement( const char* name = 0 ) {
769 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700770 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800771
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700772 /**
773 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200774 If the child node is already part of the document,
775 it is moved from its old location to the new location.
776 Returns the addThis argument or 0 if the node does not
777 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700778 */
779 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800780
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700781 XMLNode* LinkEndChild( XMLNode* addThis ) {
782 return InsertEndChild( addThis );
783 }
784 /**
785 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200786 If the child node is already part of the document,
787 it is moved from its old location to the new location.
788 Returns the addThis argument or 0 if the node does not
789 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700790 */
791 XMLNode* InsertFirstChild( XMLNode* addThis );
792 /**
793 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200794 If the child node is already part of the document,
795 it is moved from its old location to the new location.
796 Returns the addThis argument or 0 if the afterThis node
797 is not a child of this node, or if the node does not
798 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700799 */
800 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700801
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700802 /**
803 Delete all the children of this node.
804 */
805 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800806
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700807 /**
808 Delete a child of this node.
809 */
810 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800811
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700812 /**
813 Make a copy of this node, but not its children.
814 You may pass in a Document pointer that will be
815 the owner of the new Node. If the 'document' is
816 null, then the node returned will be allocated
817 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800818
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700819 Note: if called on a XMLDocument, this will return null.
820 */
821 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800822
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700823 /**
824 Test if 2 nodes are the same, but don't test children.
825 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800826
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700827 Note: if called on a XMLDocument, this will return false.
828 */
829 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800830
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600831 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700832 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600833 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800834
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600835 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
836 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700837 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800838
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700839 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800840
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700841 - http://www.saxproject.org/
842 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800843
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700844 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800845
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700846 An example of using Accept():
847 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600848 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700849 tinyxmlDoc.Accept( &printer );
850 const char* xmlcstr = printer.CStr();
851 @endverbatim
852 */
853 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800854
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800855protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700856 XMLNode( XMLDocument* );
857 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700858
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300859 virtual char* ParseDeep( char*, StrPair* );
860
Lee Thomason624d43f2012-10-12 10:58:48 -0700861 XMLDocument* _document;
862 XMLNode* _parent;
863 mutable StrPair _value;
Lee Thomason3f57d272012-01-11 15:30:03 -0800864
Lee Thomason624d43f2012-10-12 10:58:48 -0700865 XMLNode* _firstChild;
866 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800867
Lee Thomason624d43f2012-10-12 10:58:48 -0700868 XMLNode* _prev;
869 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800870
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800871private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700872 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700873 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400874 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800875 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300876
877 XMLNode( const XMLNode& ); // not supported
878 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800879};
880
881
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800882/** XML text.
883
884 Note that a text node can have child element nodes, for example:
885 @verbatim
886 <root>This is <b>bold</b></root>
887 @endverbatim
888
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700889 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800890 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 -0700891 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600892 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800893*/
PKEuS16ed47d2013-07-06 12:02:43 +0200894class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800895{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700896 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800897public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700898 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800899
Lee Thomason624d43f2012-10-12 10:58:48 -0700900 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700901 return this;
902 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700903 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700904 return this;
905 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800906
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700907 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700908 void SetCData( bool isCData ) {
909 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700910 }
911 /// Returns true if this is a CDATA text element.
912 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700913 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700914 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800915
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700916 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
917 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800918
Lee Thomason5492a1c2012-01-23 15:32:10 -0800919protected:
Lee Thomason624d43f2012-10-12 10:58:48 -0700920 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700921 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -0800922
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300923 char* ParseDeep( char*, StrPair* endTag );
924
Lee Thomason5492a1c2012-01-23 15:32:10 -0800925private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700926 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +0300927
928 XMLText( const XMLText& ); // not supported
929 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -0800930};
931
932
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800933/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +0200934class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800935{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700936 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -0800937public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700938 virtual XMLComment* ToComment() {
939 return this;
940 }
941 virtual const XMLComment* ToComment() const {
942 return this;
943 }
Lee Thomasonce0763e2012-01-11 15:43:54 -0800944
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700945 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800946
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700947 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
948 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -0800949
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800950protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700951 XMLComment( XMLDocument* doc );
952 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800953
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300954 char* ParseDeep( char*, StrPair* endTag );
955
Lee Thomason3f57d272012-01-11 15:30:03 -0800956private:
Dmitry-Mef547a992015-01-09 15:17:09 +0300957 XMLComment( const XMLComment& ); // not supported
958 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800959};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800960
961
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800962/** In correct XML the declaration is the first entry in the file.
963 @verbatim
964 <?xml version="1.0" standalone="yes"?>
965 @endverbatim
966
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600967 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800968 however.
969
970 The text of the declaration isn't interpreted. It is parsed
971 and written as a string.
972*/
PKEuS16ed47d2013-07-06 12:02:43 +0200973class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -0800974{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700975 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -0800976public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700977 virtual XMLDeclaration* ToDeclaration() {
978 return this;
979 }
980 virtual const XMLDeclaration* ToDeclaration() const {
981 return this;
982 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800983
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700984 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800985
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700986 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
987 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800988
989protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700990 XMLDeclaration( XMLDocument* doc );
991 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +0300992
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300993 char* ParseDeep( char*, StrPair* endTag );
994
Dmitry-Mef547a992015-01-09 15:17:09 +0300995private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700996 XMLDeclaration( const XMLDeclaration& ); // not supported
997 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -0800998};
999
1000
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001001/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001002 unknown. It is a tag of text, but should not be modified.
1003 It will be written back to the XML, unchanged, when the file
1004 is saved.
1005
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001006 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001007*/
PKEuS16ed47d2013-07-06 12:02:43 +02001008class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001009{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001010 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001011public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001012 virtual XMLUnknown* ToUnknown() {
1013 return this;
1014 }
1015 virtual const XMLUnknown* ToUnknown() const {
1016 return this;
1017 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001018
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001019 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001020
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001021 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1022 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001023
1024protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001025 XMLUnknown( XMLDocument* doc );
1026 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001027
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001028 char* ParseDeep( char*, StrPair* endTag );
1029
Dmitry-Mef547a992015-01-09 15:17:09 +03001030private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001031 XMLUnknown( const XMLUnknown& ); // not supported
1032 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001033};
1034
1035
Lee Thomason1ff38e02012-02-14 18:18:16 -08001036
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001037/** An attribute is a name-value pair. Elements have an arbitrary
1038 number of attributes, each with a unique name.
1039
1040 @note The attributes are not XMLNodes. You may only query the
1041 Next() attribute in a list.
1042*/
PKEuS16ed47d2013-07-06 12:02:43 +02001043class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001044{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001045 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001046public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001047 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001048 const char* Name() const;
1049
Lee Thomason2fa81722012-11-09 12:37:46 -08001050 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001051 const char* Value() const;
1052
Lee Thomason2fa81722012-11-09 12:37:46 -08001053 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001054 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001055 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001056 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001057
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001058 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001059 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001060 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001061 */
1062 int IntValue() const {
1063 int i=0;
1064 QueryIntValue( &i );
1065 return i;
1066 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001067 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001068 unsigned UnsignedValue() const {
1069 unsigned i=0;
1070 QueryUnsignedValue( &i );
1071 return i;
1072 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001073 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001074 bool BoolValue() const {
1075 bool b=false;
1076 QueryBoolValue( &b );
1077 return b;
1078 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001079 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001080 double DoubleValue() const {
1081 double d=0;
1082 QueryDoubleValue( &d );
1083 return d;
1084 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001085 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001086 float FloatValue() const {
1087 float f=0;
1088 QueryFloatValue( &f );
1089 return f;
1090 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001091
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001092 /** QueryIntValue interprets the attribute as an integer, and returns the value
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001093 in the provided parameter. The function will return XML_NO_ERROR on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001094 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1095 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001096 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001097 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001098 XMLError QueryUnsignedValue( unsigned int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001099 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001100 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001101 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001102 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001103 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001104 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001105
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001106 /// Set the attribute to a string value.
1107 void SetAttribute( const char* value );
1108 /// Set the attribute to value.
1109 void SetAttribute( int value );
1110 /// Set the attribute to value.
1111 void SetAttribute( unsigned value );
1112 /// Set the attribute to value.
1113 void SetAttribute( bool value );
1114 /// Set the attribute to value.
1115 void SetAttribute( double value );
1116 /// Set the attribute to value.
1117 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001118
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001119private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001120 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001121
Thomas Roß61892312013-05-12 14:07:38 +02001122 XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001123 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001124
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001125 XMLAttribute( const XMLAttribute& ); // not supported
1126 void operator=( const XMLAttribute& ); // not supported
1127 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001128
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001129 char* ParseDeep( char* p, bool processEntities );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001130
Lee Thomason624d43f2012-10-12 10:58:48 -07001131 mutable StrPair _name;
1132 mutable StrPair _value;
1133 XMLAttribute* _next;
1134 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001135};
1136
1137
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001138/** The element is a container class. It has a value, the element name,
1139 and can contain other elements, text, comments, and unknowns.
1140 Elements also contain an arbitrary number of attributes.
1141*/
PKEuS16ed47d2013-07-06 12:02:43 +02001142class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001143{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001144 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001145public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001146 /// Get the name of an element (which is the Value() of the node.)
1147 const char* Name() const {
1148 return Value();
1149 }
1150 /// Set the name of the element.
1151 void SetName( const char* str, bool staticMem=false ) {
1152 SetValue( str, staticMem );
1153 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001154
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001155 virtual XMLElement* ToElement() {
1156 return this;
1157 }
1158 virtual const XMLElement* ToElement() const {
1159 return this;
1160 }
1161 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001162
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001163 /** Given an attribute name, Attribute() returns the value
1164 for the attribute of that name, or null if none
1165 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001166
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001167 @verbatim
1168 const char* value = ele->Attribute( "foo" );
1169 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001170
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001171 The 'value' parameter is normally null. However, if specified,
1172 the attribute will only be returned if the 'name' and 'value'
1173 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001174
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001175 @verbatim
1176 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1177 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001178
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001179 rather than:
1180 @verbatim
1181 if ( ele->Attribute( "foo" ) ) {
1182 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1183 }
1184 @endverbatim
1185 */
1186 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001187
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001188 /** Given an attribute name, IntAttribute() returns the value
1189 of the attribute interpreted as an integer. 0 will be
1190 returned if there is an error. For a method with error
1191 checking, see QueryIntAttribute()
1192 */
1193 int IntAttribute( const char* name ) const {
1194 int i=0;
1195 QueryIntAttribute( name, &i );
1196 return i;
1197 }
1198 /// See IntAttribute()
1199 unsigned UnsignedAttribute( const char* name ) const {
1200 unsigned i=0;
1201 QueryUnsignedAttribute( name, &i );
1202 return i;
1203 }
1204 /// See IntAttribute()
1205 bool BoolAttribute( const char* name ) const {
1206 bool b=false;
1207 QueryBoolAttribute( name, &b );
1208 return b;
1209 }
1210 /// See IntAttribute()
1211 double DoubleAttribute( const char* name ) const {
1212 double d=0;
1213 QueryDoubleAttribute( name, &d );
1214 return d;
1215 }
1216 /// See IntAttribute()
1217 float FloatAttribute( const char* name ) const {
1218 float f=0;
1219 QueryFloatAttribute( name, &f );
1220 return f;
1221 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001222
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001223 /** Given an attribute name, QueryIntAttribute() returns
1224 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1225 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1226 doesn't exist. If successful, the result of the conversion
1227 will be written to 'value'. If not successful, nothing will
1228 be written to 'value'. This allows you to provide default
1229 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001230
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001231 @verbatim
1232 int value = 10;
1233 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1234 @endverbatim
1235 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001236 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001237 const XMLAttribute* a = FindAttribute( name );
1238 if ( !a ) {
1239 return XML_NO_ATTRIBUTE;
1240 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001241 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001242 }
1243 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001244 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001245 const XMLAttribute* a = FindAttribute( name );
1246 if ( !a ) {
1247 return XML_NO_ATTRIBUTE;
1248 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001249 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001250 }
1251 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001252 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001253 const XMLAttribute* a = FindAttribute( name );
1254 if ( !a ) {
1255 return XML_NO_ATTRIBUTE;
1256 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001257 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001258 }
1259 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001260 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001261 const XMLAttribute* a = FindAttribute( name );
1262 if ( !a ) {
1263 return XML_NO_ATTRIBUTE;
1264 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001265 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001266 }
1267 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001268 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001269 const XMLAttribute* a = FindAttribute( name );
1270 if ( !a ) {
1271 return XML_NO_ATTRIBUTE;
1272 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001273 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001274 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001275
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001276
1277 /** Given an attribute name, QueryAttribute() returns
1278 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1279 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1280 doesn't exist. It is overloaded for the primitive types,
1281 and is a generally more convenient replacement of
1282 QueryIntAttribute() and related functions.
1283
1284 If successful, the result of the conversion
1285 will be written to 'value'. If not successful, nothing will
1286 be written to 'value'. This allows you to provide default
1287 value:
1288
1289 @verbatim
1290 int value = 10;
1291 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1292 @endverbatim
1293 */
1294 int QueryAttribute( const char* name, int* value ) const {
1295 return QueryIntAttribute( name, value );
1296 }
1297
1298 int QueryAttribute( const char* name, unsigned int* value ) const {
1299 return QueryUnsignedAttribute( name, value );
1300 }
1301
1302 int QueryAttribute( const char* name, bool* value ) const {
1303 return QueryBoolAttribute( name, value );
1304 }
1305
1306 int QueryAttribute( const char* name, double* value ) const {
1307 return QueryDoubleAttribute( name, value );
1308 }
1309
1310 int QueryAttribute( const char* name, float* value ) const {
1311 return QueryFloatAttribute( name, value );
1312 }
1313
1314 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001315 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001316 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001317 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001318 }
1319 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001320 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001321 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001322 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001323 }
1324 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001325 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001326 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001327 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001328 }
1329 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001330 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001331 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001332 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001333 }
1334 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001335 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001336 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001337 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001338 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001339 /// Sets the named attribute to value.
1340 void SetAttribute( const char* name, float value ) {
1341 XMLAttribute* a = FindOrCreateAttribute( name );
1342 a->SetAttribute( value );
1343 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001344
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001345 /**
1346 Delete an attribute.
1347 */
1348 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001349
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001350 /// Return the first attribute in the list.
1351 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001352 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001353 }
1354 /// Query a specific attribute in the list.
1355 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001356
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001357 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001358 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001359 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001360
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001361 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001362 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001363
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001364 This is a convenient method for getting the text of simple contained text:
1365 @verbatim
1366 <foo>This is text</foo>
1367 const char* str = fooElement->GetText();
1368 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001369
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001370 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001371
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001372 Note that this function can be misleading. If the element foo was created from
1373 this XML:
1374 @verbatim
1375 <foo><b>This is text</b></foo>
1376 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001377
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001378 then the value of str would be null. The first child node isn't a text node, it is
1379 another element. From this XML:
1380 @verbatim
1381 <foo>This is <b>text</b></foo>
1382 @endverbatim
1383 GetText() will return "This is ".
1384 */
1385 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001386
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001387 /** Convenience function for easy access to the text inside an element. Although easy
1388 and concise, SetText() is limited compared to creating an XMLText child
1389 and mutating it directly.
1390
1391 If the first child of 'this' is a XMLText, SetText() sets its value to
1392 the given string, otherwise it will create a first child that is an XMLText.
1393
1394 This is a convenient method for setting the text of simple contained text:
1395 @verbatim
1396 <foo>This is text</foo>
1397 fooElement->SetText( "Hullaballoo!" );
1398 <foo>Hullaballoo!</foo>
1399 @endverbatim
1400
1401 Note that this function can be misleading. If the element foo was created from
1402 this XML:
1403 @verbatim
1404 <foo><b>This is text</b></foo>
1405 @endverbatim
1406
1407 then it will not change "This is text", but rather prefix it with a text element:
1408 @verbatim
1409 <foo>Hullaballoo!<b>This is text</b></foo>
1410 @endverbatim
1411
1412 For this XML:
1413 @verbatim
1414 <foo />
1415 @endverbatim
1416 SetText() will generate
1417 @verbatim
1418 <foo>Hullaballoo!</foo>
1419 @endverbatim
1420 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001421 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001422 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001423 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001424 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001425 void SetText( unsigned value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001426 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001427 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001428 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001429 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001430 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001431 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001432
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001433 /**
1434 Convenience method to query the value of a child text node. This is probably best
1435 shown by example. Given you have a document is this form:
1436 @verbatim
1437 <point>
1438 <x>1</x>
1439 <y>1.4</y>
1440 </point>
1441 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001442
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001443 The QueryIntText() and similar functions provide a safe and easier way to get to the
1444 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001445
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001446 @verbatim
1447 int x = 0;
1448 float y = 0; // types of x and y are contrived for example
1449 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1450 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1451 xElement->QueryIntText( &x );
1452 yElement->QueryFloatText( &y );
1453 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001454
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001455 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1456 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 -07001457
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001458 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001459 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001460 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001461 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001462 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001463 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001464 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001465 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001466 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001467 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001468
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001469 // internal:
1470 enum {
1471 OPEN, // <foo>
1472 CLOSED, // <foo/>
1473 CLOSING // </foo>
1474 };
1475 int ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001476 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001477 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001478 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1479 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001480
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001481protected:
1482 char* ParseDeep( char* p, StrPair* endTag );
1483
Lee Thomason50adb4c2012-02-13 15:07:09 -08001484private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001485 XMLElement( XMLDocument* doc );
1486 virtual ~XMLElement();
1487 XMLElement( const XMLElement& ); // not supported
1488 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001489
Dmitry-Me1227d512014-12-05 13:41:45 +03001490 XMLAttribute* FindAttribute( const char* name ) {
1491 return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
1492 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001493 XMLAttribute* FindOrCreateAttribute( const char* name );
1494 //void LinkAttribute( XMLAttribute* attrib );
1495 char* ParseAttributes( char* p );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001496 static void DeleteAttribute( XMLAttribute* attribute );
Lee Thomason67d61312012-01-24 16:01:51 -08001497
Lee Thomason5bb2d802014-01-24 10:42:57 -08001498 enum { BUF_SIZE = 200 };
Lee Thomason624d43f2012-10-12 10:58:48 -07001499 int _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001500 // The attribute list is ordered; there is no 'lastAttribute'
1501 // because the list needs to be scanned for dupes before adding
1502 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001503 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001504};
1505
1506
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001507enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001508 PRESERVE_WHITESPACE,
1509 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001510};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001511
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001512
1513/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001514 It can be saved, loaded, and printed to the screen.
1515 All Nodes are connected and allocated to a Document.
1516 If the Document is deleted, all its Nodes are also deleted.
1517*/
PKEuS16ed47d2013-07-06 12:02:43 +02001518class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001519{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001520 friend class XMLElement;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001521public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001522 /// constructor
1523 XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
1524 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001525
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001526 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001527 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001528 return this;
1529 }
1530 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001531 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001532 return this;
1533 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001534
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001535 /**
1536 Parse an XML file from a character string.
1537 Returns XML_NO_ERROR (0) on success, or
1538 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001539
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001540 You may optionally pass in the 'nBytes', which is
1541 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001542 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001543 null terminated string.
1544 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001545 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001546
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001547 /**
1548 Load an XML file from disk.
1549 Returns XML_NO_ERROR (0) on success, or
1550 an errorID.
1551 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001552 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001553
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001554 /**
1555 Load an XML file from disk. You are responsible
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001556 for providing and closing the FILE*.
1557
1558 NOTE: The file should be opened as binary ("rb")
1559 not text in order for TinyXML-2 to correctly
1560 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001561
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001562 Returns XML_NO_ERROR (0) on success, or
1563 an errorID.
1564 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001565 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001566
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001567 /**
1568 Save the XML file to disk.
1569 Returns XML_NO_ERROR (0) on success, or
1570 an errorID.
1571 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001572 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001573
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001574 /**
1575 Save the XML file to disk. You are responsible
1576 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001577
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001578 Returns XML_NO_ERROR (0) on success, or
1579 an errorID.
1580 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001581 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001582
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001583 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001584 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001585 }
1586 Whitespace WhitespaceMode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001587 return _whitespace;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001588 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001589
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001590 /**
1591 Returns true if this document has a leading Byte Order Mark of UTF8.
1592 */
1593 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001594 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001595 }
1596 /** Sets whether to write the BOM when writing the file.
1597 */
1598 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001599 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001600 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001601
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001602 /** Return the root element of DOM. Equivalent to FirstChildElement().
1603 To get the first node, use FirstChild().
1604 */
1605 XMLElement* RootElement() {
1606 return FirstChildElement();
1607 }
1608 const XMLElement* RootElement() const {
1609 return FirstChildElement();
1610 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001611
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001612 /** Print the Document. If the Printer is not provided, it will
1613 print to stdout. If you provide Printer, this can print to a file:
1614 @verbatim
1615 XMLPrinter printer( fp );
1616 doc.Print( &printer );
1617 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001618
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001619 Or you can use a printer to print to memory:
1620 @verbatim
1621 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001622 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001623 // printer.CStr() has a const char* to the XML
1624 @endverbatim
1625 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001626 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001627 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001628
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001629 /**
1630 Create a new Element associated with
1631 this Document. The memory for the Element
1632 is managed by the Document.
1633 */
1634 XMLElement* NewElement( const char* name );
1635 /**
1636 Create a new Comment associated with
1637 this Document. The memory for the Comment
1638 is managed by the Document.
1639 */
1640 XMLComment* NewComment( const char* comment );
1641 /**
1642 Create a new Text associated with
1643 this Document. The memory for the Text
1644 is managed by the Document.
1645 */
1646 XMLText* NewText( const char* text );
1647 /**
1648 Create a new Declaration associated with
1649 this Document. The memory for the object
1650 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001651
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001652 If the 'text' param is null, the standard
1653 declaration is used.:
1654 @verbatim
1655 <?xml version="1.0" encoding="UTF-8"?>
1656 @endverbatim
1657 */
1658 XMLDeclaration* NewDeclaration( const char* text=0 );
1659 /**
1660 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001661 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001662 is managed by the Document.
1663 */
1664 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001665
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001666 /**
1667 Delete a node associated with this document.
1668 It will be unlinked from the DOM.
1669 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001670 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001671
Lee Thomason2fa81722012-11-09 12:37:46 -08001672 void SetError( XMLError error, const char* str1, const char* str2 );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001673
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001674 /// Return true if there was an error parsing the document.
1675 bool Error() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001676 return _errorID != XML_NO_ERROR;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001677 }
1678 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001679 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001680 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001681 }
Lee Thomason331596e2014-09-11 14:56:43 -07001682 const char* ErrorName() const;
1683
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001684 /// Return a possibly helpful diagnostic location or string.
1685 const char* GetErrorStr1() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001686 return _errorStr1;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001687 }
1688 /// Return a possibly helpful secondary diagnostic location or string.
1689 const char* GetErrorStr2() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001690 return _errorStr2;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001691 }
1692 /// If there is an error, print it to stdout.
1693 void PrintError() const;
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001694
1695 /// Clear the document, resetting it to the initial state.
1696 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001697
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001698 // internal
1699 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001700
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001701 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1702 return 0;
1703 }
1704 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1705 return false;
1706 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001707
Lee Thomason3f57d272012-01-11 15:30:03 -08001708private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001709 XMLDocument( const XMLDocument& ); // not supported
1710 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001711
Lee Thomason2fa81722012-11-09 12:37:46 -08001712 bool _writeBOM;
1713 bool _processEntities;
1714 XMLError _errorID;
1715 Whitespace _whitespace;
Lee Thomason624d43f2012-10-12 10:58:48 -07001716 const char* _errorStr1;
1717 const char* _errorStr2;
Lee Thomason2fa81722012-11-09 12:37:46 -08001718 char* _charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -08001719
Lee Thomason624d43f2012-10-12 10:58:48 -07001720 MemPoolT< sizeof(XMLElement) > _elementPool;
1721 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1722 MemPoolT< sizeof(XMLText) > _textPool;
1723 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001724
1725 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001726
1727 void Parse();
Lee Thomason5cae8972012-01-24 18:03:07 -08001728};
1729
Lee Thomason7c913cd2012-01-26 18:32:34 -08001730
Lee Thomason3ffdd392012-03-28 17:27:55 -07001731/**
1732 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001733 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001734 DOM structure. It is a separate utility class.
1735
1736 Take an example:
1737 @verbatim
1738 <Document>
1739 <Element attributeA = "valueA">
1740 <Child attributeB = "value1" />
1741 <Child attributeB = "value2" />
1742 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001743 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001744 @endverbatim
1745
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001746 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001747 easy to write a *lot* of code that looks like:
1748
1749 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001750 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001751 if ( root )
1752 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001753 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001754 if ( element )
1755 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001756 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001757 if ( child )
1758 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001759 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001760 if ( child2 )
1761 {
1762 // Finally do something useful.
1763 @endverbatim
1764
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001765 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001766 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001767 and correct to use:
1768
1769 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001770 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001771 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001772 if ( child2 )
1773 {
1774 // do something useful
1775 @endverbatim
1776
1777 Which is MUCH more concise and useful.
1778
1779 It is also safe to copy handles - internally they are nothing more than node pointers.
1780 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001781 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001782 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001783
1784 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001785*/
PKEuS16ed47d2013-07-06 12:02:43 +02001786class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001787{
1788public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001789 /// 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 -07001790 XMLHandle( XMLNode* node ) {
1791 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001792 }
1793 /// Create a handle from a node.
Lee Thomason624d43f2012-10-12 10:58:48 -07001794 XMLHandle( XMLNode& node ) {
1795 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001796 }
1797 /// Copy constructor
1798 XMLHandle( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001799 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001800 }
1801 /// Assignment
1802 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001803 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001804 return *this;
1805 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001806
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001807 /// Get the first child of this handle.
1808 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001809 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001810 }
1811 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001812 XMLHandle FirstChildElement( const char* name = 0 ) {
1813 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001814 }
1815 /// Get the last child of this handle.
1816 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001817 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001818 }
1819 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001820 XMLHandle LastChildElement( const char* name = 0 ) {
1821 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001822 }
1823 /// Get the previous sibling of this handle.
1824 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001825 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001826 }
1827 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001828 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
1829 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001830 }
1831 /// Get the next sibling of this handle.
1832 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001833 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001834 }
1835 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001836 XMLHandle NextSiblingElement( const char* name = 0 ) {
1837 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001838 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001839
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001840 /// Safe cast to XMLNode. This can return null.
1841 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001842 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001843 }
1844 /// Safe cast to XMLElement. This can return null.
1845 XMLElement* ToElement() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001846 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001847 }
1848 /// Safe cast to XMLText. This can return null.
1849 XMLText* ToText() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001850 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001851 }
1852 /// Safe cast to XMLUnknown. This can return null.
1853 XMLUnknown* ToUnknown() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001854 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001855 }
1856 /// Safe cast to XMLDeclaration. This can return null.
1857 XMLDeclaration* ToDeclaration() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001858 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001859 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001860
1861private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001862 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001863};
1864
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001865
1866/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001867 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
1868 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07001869*/
PKEuS16ed47d2013-07-06 12:02:43 +02001870class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07001871{
1872public:
Lee Thomason624d43f2012-10-12 10:58:48 -07001873 XMLConstHandle( const XMLNode* node ) {
1874 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001875 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001876 XMLConstHandle( const XMLNode& node ) {
1877 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001878 }
1879 XMLConstHandle( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001880 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001881 }
Lee Thomason8b899812012-04-04 15:58:16 -07001882
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001883 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001884 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001885 return *this;
1886 }
Lee Thomason8b899812012-04-04 15:58:16 -07001887
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001888 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001889 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001890 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001891 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
1892 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001893 }
1894 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001895 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001896 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001897 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
1898 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001899 }
1900 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001901 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001902 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001903 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
1904 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001905 }
1906 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001907 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001908 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001909 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
1910 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001911 }
Lee Thomason8b899812012-04-04 15:58:16 -07001912
1913
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001914 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001915 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001916 }
1917 const XMLElement* ToElement() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001918 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001919 }
1920 const XMLText* ToText() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001921 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001922 }
1923 const XMLUnknown* ToUnknown() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001924 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001925 }
1926 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001927 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001928 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001929
Lee Thomason5cae8972012-01-24 18:03:07 -08001930private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001931 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08001932};
Lee Thomason6f381b72012-03-02 12:59:39 -08001933
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001934
1935/**
1936 Printing functionality. The XMLPrinter gives you more
1937 options than the XMLDocument::Print() method.
1938
1939 It can:
1940 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02001941 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001942 -# Print XML without a XMLDocument.
1943
1944 Print to Memory
1945
1946 @verbatim
1947 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001948 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02001949 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001950 @endverbatim
1951
1952 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001953
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001954 You provide the file pointer.
1955 @verbatim
1956 XMLPrinter printer( fp );
1957 doc.Print( &printer );
1958 @endverbatim
1959
1960 Print without a XMLDocument
1961
1962 When loading, an XML parser is very useful. However, sometimes
1963 when saving, it just gets in the way. The code is often set up
1964 for streaming, and constructing the DOM is just overhead.
1965
1966 The Printer supports the streaming case. The following code
1967 prints out a trivially simple XML file without ever creating
1968 an XML document.
1969
1970 @verbatim
1971 XMLPrinter printer( fp );
1972 printer.OpenElement( "foo" );
1973 printer.PushAttribute( "foo", "bar" );
1974 printer.CloseElement();
1975 @endverbatim
1976*/
PKEuS16ed47d2013-07-06 12:02:43 +02001977class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001978{
1979public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001980 /** Construct the printer. If the FILE* is specified,
1981 this will print to the FILE. Else it will print
1982 to memory, and the result is available in CStr().
1983 If 'compact' is set to true, then output is created
1984 with only required whitespace and newlines.
1985 */
PKEuS1bfb9542013-08-04 13:51:17 +02001986 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05001987 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001988
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001989 /** If streaming, write the BOM and declaration. */
1990 void PushHeader( bool writeBOM, bool writeDeclaration );
1991 /** If streaming, start writing an element.
1992 The element must be closed with CloseElement()
1993 */
Lee Thomason256adb62014-04-06 14:41:46 -07001994 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001995 /// If streaming, add an attribute to an open element.
1996 void PushAttribute( const char* name, const char* value );
1997 void PushAttribute( const char* name, int value );
1998 void PushAttribute( const char* name, unsigned value );
1999 void PushAttribute( const char* name, bool value );
2000 void PushAttribute( const char* name, double value );
2001 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002002 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002003
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002004 /// Add a text node.
2005 void PushText( const char* text, bool cdata=false );
2006 /// Add a text node from an integer.
2007 void PushText( int value );
2008 /// Add a text node from an unsigned.
2009 void PushText( unsigned value );
2010 /// Add a text node from a bool.
2011 void PushText( bool value );
2012 /// Add a text node from a float.
2013 void PushText( float value );
2014 /// Add a text node from a double.
2015 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002016
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002017 /// Add a comment
2018 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002019
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002020 void PushDeclaration( const char* value );
2021 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002022
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002023 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2024 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2025 return true;
2026 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002027
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002028 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2029 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002030
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002031 virtual bool Visit( const XMLText& text );
2032 virtual bool Visit( const XMLComment& comment );
2033 virtual bool Visit( const XMLDeclaration& declaration );
2034 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002035
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002036 /**
2037 If in print to memory mode, return a pointer to
2038 the XML file in memory.
2039 */
2040 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002041 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002042 }
2043 /**
2044 If in print to memory mode, return the size
2045 of the XML file in memory. (Note the size returned
2046 includes the terminating null.)
2047 */
2048 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002049 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002050 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002051 /**
2052 If in print to memory mode, reset the buffer to the
2053 beginning.
2054 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002055 void ClearBuffer() {
2056 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002057 _buffer.Push(0);
2058 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002059
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002060protected:
Alexander Maid740b642014-05-20 22:04:42 +02002061 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002062
Lee Thomasonc18eb232014-02-21 17:31:17 -08002063 /** Prints out the space before an element. You may override to change
2064 the space and tabs used. A PrintSpace() override should call Print().
2065 */
2066 virtual void PrintSpace( int depth );
2067 void Print( const char* format, ... );
2068
Dmitry-Mea092bc12014-12-23 17:57:05 +03002069 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002070 bool _elementJustOpened;
2071 DynArray< const char*, 10 > _stack;
2072
2073private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002074 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002075
Lee Thomason624d43f2012-10-12 10:58:48 -07002076 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002077 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002078 int _depth;
2079 int _textDepth;
2080 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002081 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002082
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002083 enum {
2084 ENTITY_RANGE = 64,
2085 BUF_SIZE = 200
2086 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002087 bool _entityFlag[ENTITY_RANGE];
2088 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002089
Lee Thomason624d43f2012-10-12 10:58:48 -07002090 DynArray< char, 20 > _buffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002091};
2092
2093
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002094} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002095
PKEuS95060352013-07-26 10:42:44 +02002096#if defined(_MSC_VER)
2097# pragma warning(pop)
2098#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002099
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002100#endif // TINYXML2_INCLUDED