blob: b90471b809a7bf0ab01a6625e4b4429e42dd0c23 [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
selfpoised4dd59bc2013-03-13 16:54:15 +0800169 // After parsing, if *_end != 0, it can be set to zero.
Lee Thomason120b3a62012-10-12 10:06:59 -0700170 int _flags;
171 char* _start;
172 char* _end;
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300173
174 StrPair( const StrPair& other ); // not supported
175 void operator=( StrPair& other ); // not supported, use TransferTo()
Lee Thomason39ede242012-01-20 11:27:56 -0800176};
177
U-Lama\Lee560bd472011-12-28 19:42:49 -0800178
U-Stream\Leeae25a442012-02-17 17:48:16 -0800179/*
180 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
181 Has a small initial memory pool, so that low or no usage will not
182 cause a call to new/delete
183*/
Dmitry-Me04009222015-04-06 18:07:18 +0300184template <class T, int INITIAL_SIZE>
PKEuS95060352013-07-26 10:42:44 +0200185class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800186{
187public:
Dmitry-Me9a5a48d2015-01-14 08:27:32 +0300188 DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700189 _mem = _pool;
Dmitry-Me04009222015-04-06 18:07:18 +0300190 _allocated = INITIAL_SIZE;
Lee Thomason624d43f2012-10-12 10:58:48 -0700191 _size = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700192 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700193
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700194 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700195 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700196 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700197 }
198 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700199
Lee Thomasonce0510b2013-11-26 21:29:37 -0800200 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100201 _size = 0;
202 }
203
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700204 void Push( T t ) {
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300205 TIXMLASSERT( _size < INT_MAX );
Lee Thomason624d43f2012-10-12 10:58:48 -0700206 EnsureCapacity( _size+1 );
207 _mem[_size++] = t;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700208 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800209
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700210 T* PushArr( int count ) {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300211 TIXMLASSERT( count >= 0 );
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300212 TIXMLASSERT( _size <= INT_MAX - count );
Lee Thomason624d43f2012-10-12 10:58:48 -0700213 EnsureCapacity( _size+count );
214 T* ret = &_mem[_size];
215 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700216 return ret;
217 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700218
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700219 T Pop() {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300220 TIXMLASSERT( _size > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700221 return _mem[--_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700222 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700223
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700224 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700225 TIXMLASSERT( _size >= count );
226 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700227 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800228
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700229 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700230 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700231 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700232
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700233 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700234 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700235 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700236 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700237
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700238 const T& operator[](int i) const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700239 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700240 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700241 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700242
Lee Thomasonf07b9522014-10-30 13:25:12 -0700243 const T& PeekTop() const {
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500244 TIXMLASSERT( _size > 0 );
245 return _mem[ _size - 1];
246 }
247
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700248 int Size() const {
Dmitry-Me30bdc972015-01-14 08:32:23 +0300249 TIXMLASSERT( _size >= 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700250 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700251 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700252
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700253 int Capacity() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300254 TIXMLASSERT( _allocated >= INITIAL_SIZE );
Lee Thomason624d43f2012-10-12 10:58:48 -0700255 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700256 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700257
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700258 const T* Mem() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300259 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700260 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700261 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700262
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700263 T* Mem() {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300264 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700265 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700266 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800267
Lee Thomason2c85a712012-01-31 08:24:24 -0800268private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300269 DynArray( const DynArray& ); // not supported
270 void operator=( const DynArray& ); // not supported
271
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700272 void EnsureCapacity( int cap ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300273 TIXMLASSERT( cap > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700274 if ( cap > _allocated ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300275 TIXMLASSERT( cap <= INT_MAX / 2 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700276 int newAllocated = cap * 2;
277 T* newMem = new T[newAllocated];
Lee Thomason624d43f2012-10-12 10:58:48 -0700278 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
279 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700280 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700281 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700282 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700283 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700284 }
285 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800286
Lee Thomason624d43f2012-10-12 10:58:48 -0700287 T* _mem;
Dmitry-Me04009222015-04-06 18:07:18 +0300288 T _pool[INITIAL_SIZE];
Lee Thomason624d43f2012-10-12 10:58:48 -0700289 int _allocated; // objects allocated
290 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800291};
292
Lee Thomason50adb4c2012-02-13 15:07:09 -0800293
U-Stream\Leeae25a442012-02-17 17:48:16 -0800294/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200295 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800296 and deallocation of objects.
297*/
PKEuS95060352013-07-26 10:42:44 +0200298class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800299{
300public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700301 MemPool() {}
302 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800303
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700304 virtual int ItemSize() const = 0;
305 virtual void* Alloc() = 0;
306 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800307 virtual void SetTracked() = 0;
Lee Thomasonf07b9522014-10-30 13:25:12 -0700308 virtual void Clear() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800309};
310
Lee Thomason50adb4c2012-02-13 15:07:09 -0800311
U-Stream\Leeae25a442012-02-17 17:48:16 -0800312/*
313 Template child class to create pools of the correct type.
314*/
Lee Thomasond1983222012-02-06 08:41:24 -0800315template< int SIZE >
PKEuS95060352013-07-26 10:42:44 +0200316class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800317{
318public:
Lee Thomason5b0a6772012-11-19 13:54:42 -0800319 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700320 ~MemPoolT() {
Lee Thomasonf07b9522014-10-30 13:25:12 -0700321 Clear();
322 }
323
324 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700325 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700326 while( !_blockPtrs.Empty()) {
327 Block* b = _blockPtrs.Pop();
328 delete b;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700329 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700330 _root = 0;
331 _currentAllocs = 0;
332 _nAllocs = 0;
333 _maxAllocs = 0;
334 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700335 }
Lee Thomasond1983222012-02-06 08:41:24 -0800336
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700337 virtual int ItemSize() const {
338 return SIZE;
339 }
340 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700341 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700342 }
Lee Thomasond1983222012-02-06 08:41:24 -0800343
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700344 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700345 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700346 // Need a new block.
347 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700348 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800349
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700350 for( int i=0; i<COUNT-1; ++i ) {
351 block->chunk[i].next = &block->chunk[i+1];
352 }
353 block->chunk[COUNT-1].next = 0;
Lee Thomason624d43f2012-10-12 10:58:48 -0700354 _root = block->chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700355 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700356 void* result = _root;
357 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800358
Lee Thomason624d43f2012-10-12 10:58:48 -0700359 ++_currentAllocs;
360 if ( _currentAllocs > _maxAllocs ) {
361 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700362 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700363 _nAllocs++;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800364 _nUntracked++;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700365 return result;
366 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700367
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700368 virtual void Free( void* mem ) {
369 if ( !mem ) {
370 return;
371 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700372 --_currentAllocs;
Dmitry-Me56571762014-08-15 11:03:47 +0400373 Chunk* chunk = static_cast<Chunk*>( mem );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700374#ifdef DEBUG
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700375 memset( chunk, 0xfe, sizeof(Chunk) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700376#endif
Lee Thomason624d43f2012-10-12 10:58:48 -0700377 chunk->next = _root;
378 _root = chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700379 }
380 void Trace( const char* name ) {
381 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Lee Thomason624d43f2012-10-12 10:58:48 -0700382 name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700383 }
Lee Thomasond1983222012-02-06 08:41:24 -0800384
Lee Thomason5b0a6772012-11-19 13:54:42 -0800385 void SetTracked() {
386 _nUntracked--;
387 }
388
389 int Untracked() const {
390 return _nUntracked;
391 }
392
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800393 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
394 // The test file is large, 170k.
395 // Release: VS2010 gcc(no opt)
396 // 1k: 4000
397 // 2k: 4000
398 // 4k: 3900 21000
399 // 16k: 5200
400 // 32k: 4300
401 // 64k: 4000 21000
402 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 +0200403
Lee Thomasond1983222012-02-06 08:41:24 -0800404private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300405 MemPoolT( const MemPoolT& ); // not supported
406 void operator=( const MemPoolT& ); // not supported
407
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700408 union Chunk {
Lee Thomason624d43f2012-10-12 10:58:48 -0700409 Chunk* next;
410 char mem[SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700411 };
412 struct Block {
Lee Thomason (grinliz)856da212012-10-19 09:08:15 -0700413 Chunk chunk[COUNT];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700414 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700415 DynArray< Block*, 10 > _blockPtrs;
416 Chunk* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800417
Lee Thomason624d43f2012-10-12 10:58:48 -0700418 int _currentAllocs;
419 int _nAllocs;
420 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800421 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800422};
423
Lee Thomason2c85a712012-01-31 08:24:24 -0800424
Lee Thomason56bdd022012-02-09 18:16:58 -0800425
426/**
427 Implements the interface to the "Visitor pattern" (see the Accept() method.)
428 If you call the Accept() method, it requires being passed a XMLVisitor
429 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200430 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800431 are simply called with Visit().
432
433 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700434 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800435
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700436 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800437 visiting). You need to only override methods that are interesting to you.
438
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600439 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800440
441 You should never change the document from a callback.
442
443 @sa XMLNode::Accept()
444*/
PKEuS16ed47d2013-07-06 12:02:43 +0200445class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800446{
447public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700448 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800449
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700450 /// Visit a document.
451 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
452 return true;
453 }
454 /// Visit a document.
455 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
456 return true;
457 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800458
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700459 /// Visit an element.
460 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
461 return true;
462 }
463 /// Visit an element.
464 virtual bool VisitExit( const XMLElement& /*element*/ ) {
465 return true;
466 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800467
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700468 /// Visit a declaration.
469 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
470 return true;
471 }
472 /// Visit a text node.
473 virtual bool Visit( const XMLText& /*text*/ ) {
474 return true;
475 }
476 /// Visit a comment node.
477 virtual bool Visit( const XMLComment& /*comment*/ ) {
478 return true;
479 }
480 /// Visit an unknown node.
481 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
482 return true;
483 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800484};
485
Dmitry-Me66d2a842014-11-08 15:24:52 +0300486// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900487enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900488 XML_SUCCESS = 0,
489 XML_NO_ERROR = 0,
490 XML_NO_ATTRIBUTE,
491 XML_WRONG_ATTRIBUTE_TYPE,
492 XML_ERROR_FILE_NOT_FOUND,
493 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
494 XML_ERROR_FILE_READ_ERROR,
495 XML_ERROR_ELEMENT_MISMATCH,
496 XML_ERROR_PARSING_ELEMENT,
497 XML_ERROR_PARSING_ATTRIBUTE,
498 XML_ERROR_IDENTIFYING_TAG,
499 XML_ERROR_PARSING_TEXT,
500 XML_ERROR_PARSING_CDATA,
501 XML_ERROR_PARSING_COMMENT,
502 XML_ERROR_PARSING_DECLARATION,
503 XML_ERROR_PARSING_UNKNOWN,
504 XML_ERROR_EMPTY_DOCUMENT,
505 XML_ERROR_MISMATCHED_ELEMENT,
506 XML_ERROR_PARSING,
507 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700508 XML_NO_TEXT_NODE,
509
510 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900511};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900512
numatrumpetcd8550c2014-09-08 16:59:39 +0900513
U-Stream\Leeae25a442012-02-17 17:48:16 -0800514/*
515 Utility functionality.
516*/
Lee Thomason56bdd022012-02-09 18:16:58 -0800517class XMLUtil
518{
Lee Thomasond1983222012-02-06 08:41:24 -0800519public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700520 static const char* SkipWhiteSpace( const char* p ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300521 TIXMLASSERT( p );
Dmitry-Mefa20b222014-10-31 12:53:04 +0300522 while( IsWhiteSpace(*p) ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700523 ++p;
524 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300525 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700526 return p;
527 }
528 static char* SkipWhiteSpace( char* p ) {
Dmitry-Me9de541f2014-09-24 14:21:36 +0400529 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p) ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700530 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300531
532 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
533 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700534 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100535 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700536 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200537
538 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300539 if ( ch >= 128 ) {
540 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
541 return true;
542 }
543 if ( isalpha( ch ) ) {
544 return true;
545 }
546 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200547 }
548
549 inline static bool IsNameChar( unsigned char ch ) {
550 return IsNameStartChar( ch )
551 || isdigit( ch )
552 || ch == '.'
553 || ch == '-';
554 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800555
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700556 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700557 if ( p == q ) {
558 return true;
559 }
Lee Thomason598a88d2015-10-09 14:42:12 -0700560 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700561 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200562
Dmitry-Me7865aad2015-06-19 16:23:35 +0300563 inline static bool IsUTF8Continuation( char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400564 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700565 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800566
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700567 static const char* ReadBOM( const char* p, bool* hasBOM );
568 // p is the starting location,
569 // the UTF-8 value of the entity will be placed in value, and length filled in.
570 static const char* GetCharacterRef( const char* p, char* value, int* length );
571 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700572
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700573 // converts primitive types to strings
574 static void ToStr( int v, char* buffer, int bufferSize );
575 static void ToStr( unsigned v, char* buffer, int bufferSize );
576 static void ToStr( bool v, char* buffer, int bufferSize );
577 static void ToStr( float v, char* buffer, int bufferSize );
578 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason21be8822012-07-15 17:27:22 -0700579
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700580 // converts strings to primitive types
581 static bool ToInt( const char* str, int* value );
582 static bool ToUnsigned( const char* str, unsigned* value );
583 static bool ToBool( const char* str, bool* value );
584 static bool ToFloat( const char* str, float* value );
585 static bool ToDouble( const char* str, double* value );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800586};
587
Lee Thomason5cae8972012-01-24 18:03:07 -0800588
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800589/** XMLNode is a base class for every object that is in the
590 XML Document Object Model (DOM), except XMLAttributes.
591 Nodes have siblings, a parent, and children which can
592 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700593 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800594 be cast to its more defined type.
595
Thomas Roß08bdf502012-05-12 14:21:23 +0200596 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800597 When the XMLDocument gets deleted, all its Nodes
598 will also be deleted.
599
600 @verbatim
601 A Document can contain: Element (container or leaf)
602 Comment (leaf)
603 Unknown (leaf)
604 Declaration( leaf )
605
606 An Element can contain: Element (container or leaf)
607 Text (leaf)
608 Attributes (not on tree)
609 Comment (leaf)
610 Unknown (leaf)
611
612 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800613*/
PKEuS16ed47d2013-07-06 12:02:43 +0200614class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800615{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700616 friend class XMLDocument;
617 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800618public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800619
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700620 /// Get the XMLDocument that owns this XMLNode.
621 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300622 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700623 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700624 }
625 /// Get the XMLDocument that owns this XMLNode.
626 XMLDocument* GetDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300627 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700628 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700629 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800630
Lee Thomason2fa81722012-11-09 12:37:46 -0800631 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700632 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100633 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700634 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800635 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700636 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100637 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700638 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800639 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700640 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100641 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700642 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800643 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700644 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100645 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700646 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800647 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700648 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100649 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700650 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800651 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700652 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100653 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700654 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800655
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700656 virtual const XMLElement* ToElement() const {
657 return 0;
658 }
659 virtual const XMLText* ToText() const {
660 return 0;
661 }
662 virtual const XMLComment* ToComment() const {
663 return 0;
664 }
665 virtual const XMLDocument* ToDocument() const {
666 return 0;
667 }
668 virtual const XMLDeclaration* ToDeclaration() const {
669 return 0;
670 }
671 virtual const XMLUnknown* ToUnknown() const {
672 return 0;
673 }
Lee Thomason751da522012-02-10 08:50:51 -0800674
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700675 /** The meaning of 'value' changes for the specific type.
676 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530677 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700678 Element: name of the element
679 Comment: the comment text
680 Unknown: the tag contents
681 Text: the text string
682 @endverbatim
683 */
Michael Daumling21626882013-10-22 17:03:37 +0200684 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100685
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700686 /** Set the Value of an XML node.
687 @sa Value()
688 */
689 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800690
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700691 /// Get the parent of this node on the DOM.
692 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700693 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700694 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100695
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700696 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700697 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700698 }
Lee Thomason751da522012-02-10 08:50:51 -0800699
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700700 /// Returns true if this node has no children.
701 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700702 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700703 }
Lee Thomason751da522012-02-10 08:50:51 -0800704
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700705 /// Get the first child node, or null if none exists.
706 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700707 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700708 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100709
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700710 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700711 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700712 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100713
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700714 /** Get the first child element, or optionally the first child
715 element with the specified name.
716 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300717 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700718
Dmitry-Me886ad972015-07-22 11:00:51 +0300719 XMLElement* FirstChildElement( const char* name = 0 ) {
720 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700721 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800722
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700723 /// Get the last child node, or null if none exists.
724 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700725 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700726 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700727
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700728 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300729 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700730 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800731
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700732 /** Get the last child element or optionally the last child
733 element with the specified name.
734 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300735 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700736
Dmitry-Me886ad972015-07-22 11:00:51 +0300737 XMLElement* LastChildElement( const char* name = 0 ) {
738 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700739 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700740
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700741 /// Get the previous (left) sibling node of this node.
742 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700743 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700744 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700745
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700746 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700747 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700748 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800749
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700750 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300751 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700752
Dmitry-Me886ad972015-07-22 11:00:51 +0300753 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
754 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700755 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700756
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700757 /// Get the next (right) sibling node of this node.
758 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700759 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700760 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700761
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700762 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700763 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700764 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700765
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700766 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300767 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700768
Dmitry-Me886ad972015-07-22 11:00:51 +0300769 XMLElement* NextSiblingElement( const char* name = 0 ) {
770 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700771 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800772
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700773 /**
774 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200775 If the child node is already part of the document,
776 it is moved from its old location to the new location.
777 Returns the addThis argument or 0 if the node does not
778 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700779 */
780 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800781
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700782 XMLNode* LinkEndChild( XMLNode* addThis ) {
783 return InsertEndChild( addThis );
784 }
785 /**
786 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200787 If the child node is already part of the document,
788 it is moved from its old location to the new location.
789 Returns the addThis argument or 0 if the node does not
790 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700791 */
792 XMLNode* InsertFirstChild( XMLNode* addThis );
793 /**
794 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200795 If the child node is already part of the document,
796 it is moved from its old location to the new location.
797 Returns the addThis argument or 0 if the afterThis node
798 is not a child of this node, or if the node does not
799 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700800 */
801 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700802
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700803 /**
804 Delete all the children of this node.
805 */
806 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800807
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700808 /**
809 Delete a child of this node.
810 */
811 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800812
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700813 /**
814 Make a copy of this node, but not its children.
815 You may pass in a Document pointer that will be
816 the owner of the new Node. If the 'document' is
817 null, then the node returned will be allocated
818 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800819
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700820 Note: if called on a XMLDocument, this will return null.
821 */
822 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800823
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700824 /**
825 Test if 2 nodes are the same, but don't test children.
826 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800827
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700828 Note: if called on a XMLDocument, this will return false.
829 */
830 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800831
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600832 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700833 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600834 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800835
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600836 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
837 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700838 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800839
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700840 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800841
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700842 - http://www.saxproject.org/
843 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800844
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700845 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800846
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700847 An example of using Accept():
848 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600849 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700850 tinyxmlDoc.Accept( &printer );
851 const char* xmlcstr = printer.CStr();
852 @endverbatim
853 */
854 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800855
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800856protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700857 XMLNode( XMLDocument* );
858 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700859
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300860 virtual char* ParseDeep( char*, StrPair* );
861
Lee Thomason624d43f2012-10-12 10:58:48 -0700862 XMLDocument* _document;
863 XMLNode* _parent;
864 mutable StrPair _value;
Lee Thomason3f57d272012-01-11 15:30:03 -0800865
Lee Thomason624d43f2012-10-12 10:58:48 -0700866 XMLNode* _firstChild;
867 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800868
Lee Thomason624d43f2012-10-12 10:58:48 -0700869 XMLNode* _prev;
870 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800871
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800872private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700873 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700874 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400875 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800876 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300877
878 XMLNode( const XMLNode& ); // not supported
879 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800880};
881
882
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800883/** XML text.
884
885 Note that a text node can have child element nodes, for example:
886 @verbatim
887 <root>This is <b>bold</b></root>
888 @endverbatim
889
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700890 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800891 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 -0700892 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600893 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800894*/
PKEuS16ed47d2013-07-06 12:02:43 +0200895class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800896{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700897 friend class XMLBase;
898 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800899public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700900 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800901
Lee Thomason624d43f2012-10-12 10:58:48 -0700902 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700903 return this;
904 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700905 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700906 return this;
907 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800908
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700909 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700910 void SetCData( bool isCData ) {
911 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700912 }
913 /// Returns true if this is a CDATA text element.
914 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700915 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700916 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800917
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700918 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
919 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800920
Lee Thomason5492a1c2012-01-23 15:32:10 -0800921protected:
Lee Thomason624d43f2012-10-12 10:58:48 -0700922 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700923 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -0800924
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300925 char* ParseDeep( char*, StrPair* endTag );
926
Lee Thomason5492a1c2012-01-23 15:32:10 -0800927private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700928 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +0300929
930 XMLText( const XMLText& ); // not supported
931 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -0800932};
933
934
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800935/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +0200936class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800937{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700938 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -0800939public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700940 virtual XMLComment* ToComment() {
941 return this;
942 }
943 virtual const XMLComment* ToComment() const {
944 return this;
945 }
Lee Thomasonce0763e2012-01-11 15:43:54 -0800946
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700947 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800948
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700949 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
950 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -0800951
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800952protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700953 XMLComment( XMLDocument* doc );
954 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800955
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300956 char* ParseDeep( char*, StrPair* endTag );
957
Lee Thomason3f57d272012-01-11 15:30:03 -0800958private:
Dmitry-Mef547a992015-01-09 15:17:09 +0300959 XMLComment( const XMLComment& ); // not supported
960 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800961};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800962
963
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800964/** In correct XML the declaration is the first entry in the file.
965 @verbatim
966 <?xml version="1.0" standalone="yes"?>
967 @endverbatim
968
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600969 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800970 however.
971
972 The text of the declaration isn't interpreted. It is parsed
973 and written as a string.
974*/
PKEuS16ed47d2013-07-06 12:02:43 +0200975class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -0800976{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700977 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -0800978public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700979 virtual XMLDeclaration* ToDeclaration() {
980 return this;
981 }
982 virtual const XMLDeclaration* ToDeclaration() const {
983 return this;
984 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800985
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700986 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800987
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700988 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
989 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800990
991protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700992 XMLDeclaration( XMLDocument* doc );
993 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +0300994
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300995 char* ParseDeep( char*, StrPair* endTag );
996
Dmitry-Mef547a992015-01-09 15:17:09 +0300997private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700998 XMLDeclaration( const XMLDeclaration& ); // not supported
999 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001000};
1001
1002
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001003/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001004 unknown. It is a tag of text, but should not be modified.
1005 It will be written back to the XML, unchanged, when the file
1006 is saved.
1007
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001008 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001009*/
PKEuS16ed47d2013-07-06 12:02:43 +02001010class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001011{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001012 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001013public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001014 virtual XMLUnknown* ToUnknown() {
1015 return this;
1016 }
1017 virtual const XMLUnknown* ToUnknown() const {
1018 return this;
1019 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001020
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001021 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001022
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001023 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1024 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001025
1026protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001027 XMLUnknown( XMLDocument* doc );
1028 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001029
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001030 char* ParseDeep( char*, StrPair* endTag );
1031
Dmitry-Mef547a992015-01-09 15:17:09 +03001032private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001033 XMLUnknown( const XMLUnknown& ); // not supported
1034 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001035};
1036
1037
Lee Thomason1ff38e02012-02-14 18:18:16 -08001038
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001039/** An attribute is a name-value pair. Elements have an arbitrary
1040 number of attributes, each with a unique name.
1041
1042 @note The attributes are not XMLNodes. You may only query the
1043 Next() attribute in a list.
1044*/
PKEuS16ed47d2013-07-06 12:02:43 +02001045class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001046{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001047 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001048public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001049 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001050 const char* Name() const;
1051
Lee Thomason2fa81722012-11-09 12:37:46 -08001052 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001053 const char* Value() const;
1054
Lee Thomason2fa81722012-11-09 12:37:46 -08001055 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001056 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001057 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001058 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001059
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001060 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001061 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001062 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001063 */
1064 int IntValue() const {
1065 int i=0;
1066 QueryIntValue( &i );
1067 return i;
1068 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001069 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001070 unsigned UnsignedValue() const {
1071 unsigned i=0;
1072 QueryUnsignedValue( &i );
1073 return i;
1074 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001075 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001076 bool BoolValue() const {
1077 bool b=false;
1078 QueryBoolValue( &b );
1079 return b;
1080 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001081 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001082 double DoubleValue() const {
1083 double d=0;
1084 QueryDoubleValue( &d );
1085 return d;
1086 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001087 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001088 float FloatValue() const {
1089 float f=0;
1090 QueryFloatValue( &f );
1091 return f;
1092 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001093
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001094 /** QueryIntValue interprets the attribute as an integer, and returns the value
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001095 in the provided parameter. The function will return XML_NO_ERROR on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001096 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1097 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001098 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001099 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001100 XMLError QueryUnsignedValue( unsigned int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001101 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001102 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001103 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001104 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001105 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001106 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001107
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001108 /// Set the attribute to a string value.
1109 void SetAttribute( const char* value );
1110 /// Set the attribute to value.
1111 void SetAttribute( int value );
1112 /// Set the attribute to value.
1113 void SetAttribute( unsigned value );
1114 /// Set the attribute to value.
1115 void SetAttribute( bool value );
1116 /// Set the attribute to value.
1117 void SetAttribute( double value );
1118 /// Set the attribute to value.
1119 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001120
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001121private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001122 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001123
Thomas Roß61892312013-05-12 14:07:38 +02001124 XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001125 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001126
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001127 XMLAttribute( const XMLAttribute& ); // not supported
1128 void operator=( const XMLAttribute& ); // not supported
1129 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001130
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001131 char* ParseDeep( char* p, bool processEntities );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001132
Lee Thomason624d43f2012-10-12 10:58:48 -07001133 mutable StrPair _name;
1134 mutable StrPair _value;
1135 XMLAttribute* _next;
1136 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001137};
1138
1139
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001140/** The element is a container class. It has a value, the element name,
1141 and can contain other elements, text, comments, and unknowns.
1142 Elements also contain an arbitrary number of attributes.
1143*/
PKEuS16ed47d2013-07-06 12:02:43 +02001144class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001145{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001146 friend class XMLBase;
1147 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001148public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001149 /// Get the name of an element (which is the Value() of the node.)
1150 const char* Name() const {
1151 return Value();
1152 }
1153 /// Set the name of the element.
1154 void SetName( const char* str, bool staticMem=false ) {
1155 SetValue( str, staticMem );
1156 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001157
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001158 virtual XMLElement* ToElement() {
1159 return this;
1160 }
1161 virtual const XMLElement* ToElement() const {
1162 return this;
1163 }
1164 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001165
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001166 /** Given an attribute name, Attribute() returns the value
1167 for the attribute of that name, or null if none
1168 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001169
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001170 @verbatim
1171 const char* value = ele->Attribute( "foo" );
1172 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001173
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001174 The 'value' parameter is normally null. However, if specified,
1175 the attribute will only be returned if the 'name' and 'value'
1176 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001177
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001178 @verbatim
1179 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1180 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001181
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001182 rather than:
1183 @verbatim
1184 if ( ele->Attribute( "foo" ) ) {
1185 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1186 }
1187 @endverbatim
1188 */
1189 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001190
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001191 /** Given an attribute name, IntAttribute() returns the value
1192 of the attribute interpreted as an integer. 0 will be
1193 returned if there is an error. For a method with error
1194 checking, see QueryIntAttribute()
1195 */
1196 int IntAttribute( const char* name ) const {
1197 int i=0;
1198 QueryIntAttribute( name, &i );
1199 return i;
1200 }
1201 /// See IntAttribute()
1202 unsigned UnsignedAttribute( const char* name ) const {
1203 unsigned i=0;
1204 QueryUnsignedAttribute( name, &i );
1205 return i;
1206 }
1207 /// See IntAttribute()
1208 bool BoolAttribute( const char* name ) const {
1209 bool b=false;
1210 QueryBoolAttribute( name, &b );
1211 return b;
1212 }
1213 /// See IntAttribute()
1214 double DoubleAttribute( const char* name ) const {
1215 double d=0;
1216 QueryDoubleAttribute( name, &d );
1217 return d;
1218 }
1219 /// See IntAttribute()
1220 float FloatAttribute( const char* name ) const {
1221 float f=0;
1222 QueryFloatAttribute( name, &f );
1223 return f;
1224 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001225
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001226 /** Given an attribute name, QueryIntAttribute() returns
1227 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1228 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1229 doesn't exist. If successful, the result of the conversion
1230 will be written to 'value'. If not successful, nothing will
1231 be written to 'value'. This allows you to provide default
1232 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001233
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001234 @verbatim
1235 int value = 10;
1236 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1237 @endverbatim
1238 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001239 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001240 const XMLAttribute* a = FindAttribute( name );
1241 if ( !a ) {
1242 return XML_NO_ATTRIBUTE;
1243 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001244 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001245 }
1246 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001247 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001248 const XMLAttribute* a = FindAttribute( name );
1249 if ( !a ) {
1250 return XML_NO_ATTRIBUTE;
1251 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001252 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001253 }
1254 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001255 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001256 const XMLAttribute* a = FindAttribute( name );
1257 if ( !a ) {
1258 return XML_NO_ATTRIBUTE;
1259 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001260 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001261 }
1262 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001263 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001264 const XMLAttribute* a = FindAttribute( name );
1265 if ( !a ) {
1266 return XML_NO_ATTRIBUTE;
1267 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001268 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001269 }
1270 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001271 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001272 const XMLAttribute* a = FindAttribute( name );
1273 if ( !a ) {
1274 return XML_NO_ATTRIBUTE;
1275 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001276 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001277 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001278
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001279
1280 /** Given an attribute name, QueryAttribute() returns
1281 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1282 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1283 doesn't exist. It is overloaded for the primitive types,
1284 and is a generally more convenient replacement of
1285 QueryIntAttribute() and related functions.
1286
1287 If successful, the result of the conversion
1288 will be written to 'value'. If not successful, nothing will
1289 be written to 'value'. This allows you to provide default
1290 value:
1291
1292 @verbatim
1293 int value = 10;
1294 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1295 @endverbatim
1296 */
1297 int QueryAttribute( const char* name, int* value ) const {
1298 return QueryIntAttribute( name, value );
1299 }
1300
1301 int QueryAttribute( const char* name, unsigned int* value ) const {
1302 return QueryUnsignedAttribute( name, value );
1303 }
1304
1305 int QueryAttribute( const char* name, bool* value ) const {
1306 return QueryBoolAttribute( name, value );
1307 }
1308
1309 int QueryAttribute( const char* name, double* value ) const {
1310 return QueryDoubleAttribute( name, value );
1311 }
1312
1313 int QueryAttribute( const char* name, float* value ) const {
1314 return QueryFloatAttribute( name, value );
1315 }
1316
1317 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001318 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001319 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001320 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001321 }
1322 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001323 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001324 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001325 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001326 }
1327 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001328 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001329 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001330 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001331 }
1332 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001333 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001334 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001335 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001336 }
1337 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001338 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001339 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001340 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001341 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001342 /// Sets the named attribute to value.
1343 void SetAttribute( const char* name, float value ) {
1344 XMLAttribute* a = FindOrCreateAttribute( name );
1345 a->SetAttribute( value );
1346 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001347
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001348 /**
1349 Delete an attribute.
1350 */
1351 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001352
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001353 /// Return the first attribute in the list.
1354 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001355 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001356 }
1357 /// Query a specific attribute in the list.
1358 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001359
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001360 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001361 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001362 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001363
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001364 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001365 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001366
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001367 This is a convenient method for getting the text of simple contained text:
1368 @verbatim
1369 <foo>This is text</foo>
1370 const char* str = fooElement->GetText();
1371 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001372
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001373 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001374
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001375 Note that this function can be misleading. If the element foo was created from
1376 this XML:
1377 @verbatim
1378 <foo><b>This is text</b></foo>
1379 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001380
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001381 then the value of str would be null. The first child node isn't a text node, it is
1382 another element. From this XML:
1383 @verbatim
1384 <foo>This is <b>text</b></foo>
1385 @endverbatim
1386 GetText() will return "This is ".
1387 */
1388 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001389
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001390 /** Convenience function for easy access to the text inside an element. Although easy
1391 and concise, SetText() is limited compared to creating an XMLText child
1392 and mutating it directly.
1393
1394 If the first child of 'this' is a XMLText, SetText() sets its value to
1395 the given string, otherwise it will create a first child that is an XMLText.
1396
1397 This is a convenient method for setting the text of simple contained text:
1398 @verbatim
1399 <foo>This is text</foo>
1400 fooElement->SetText( "Hullaballoo!" );
1401 <foo>Hullaballoo!</foo>
1402 @endverbatim
1403
1404 Note that this function can be misleading. If the element foo was created from
1405 this XML:
1406 @verbatim
1407 <foo><b>This is text</b></foo>
1408 @endverbatim
1409
1410 then it will not change "This is text", but rather prefix it with a text element:
1411 @verbatim
1412 <foo>Hullaballoo!<b>This is text</b></foo>
1413 @endverbatim
1414
1415 For this XML:
1416 @verbatim
1417 <foo />
1418 @endverbatim
1419 SetText() will generate
1420 @verbatim
1421 <foo>Hullaballoo!</foo>
1422 @endverbatim
1423 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001424 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001425 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001426 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001427 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001428 void SetText( unsigned value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001429 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001430 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001431 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001432 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001433 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001434 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001435
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001436 /**
1437 Convenience method to query the value of a child text node. This is probably best
1438 shown by example. Given you have a document is this form:
1439 @verbatim
1440 <point>
1441 <x>1</x>
1442 <y>1.4</y>
1443 </point>
1444 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001445
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001446 The QueryIntText() and similar functions provide a safe and easier way to get to the
1447 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001448
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001449 @verbatim
1450 int x = 0;
1451 float y = 0; // types of x and y are contrived for example
1452 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1453 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1454 xElement->QueryIntText( &x );
1455 yElement->QueryFloatText( &y );
1456 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001457
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001458 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1459 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 -07001460
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001461 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001462 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001463 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001464 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001465 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001466 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001467 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001468 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001469 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001470 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001471
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001472 // internal:
1473 enum {
1474 OPEN, // <foo>
1475 CLOSED, // <foo/>
1476 CLOSING // </foo>
1477 };
1478 int ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001479 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001480 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001481 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1482 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001483
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001484protected:
1485 char* ParseDeep( char* p, StrPair* endTag );
1486
Lee Thomason50adb4c2012-02-13 15:07:09 -08001487private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001488 XMLElement( XMLDocument* doc );
1489 virtual ~XMLElement();
1490 XMLElement( const XMLElement& ); // not supported
1491 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001492
Dmitry-Me1227d512014-12-05 13:41:45 +03001493 XMLAttribute* FindAttribute( const char* name ) {
1494 return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
1495 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001496 XMLAttribute* FindOrCreateAttribute( const char* name );
1497 //void LinkAttribute( XMLAttribute* attrib );
1498 char* ParseAttributes( char* p );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001499 static void DeleteAttribute( XMLAttribute* attribute );
Lee Thomason67d61312012-01-24 16:01:51 -08001500
Lee Thomason5bb2d802014-01-24 10:42:57 -08001501 enum { BUF_SIZE = 200 };
Lee Thomason624d43f2012-10-12 10:58:48 -07001502 int _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001503 // The attribute list is ordered; there is no 'lastAttribute'
1504 // because the list needs to be scanned for dupes before adding
1505 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001506 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001507};
1508
1509
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001510enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001511 PRESERVE_WHITESPACE,
1512 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001513};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001514
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001515
1516/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001517 It can be saved, loaded, and printed to the screen.
1518 All Nodes are connected and allocated to a Document.
1519 If the Document is deleted, all its Nodes are also deleted.
1520*/
PKEuS16ed47d2013-07-06 12:02:43 +02001521class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001522{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001523 friend class XMLElement;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001524public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001525 /// constructor
1526 XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
1527 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001528
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001529 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001530 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001531 return this;
1532 }
1533 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001534 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001535 return this;
1536 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001537
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001538 /**
1539 Parse an XML file from a character string.
1540 Returns XML_NO_ERROR (0) on success, or
1541 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001542
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001543 You may optionally pass in the 'nBytes', which is
1544 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001545 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001546 null terminated string.
1547 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001548 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001549
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001550 /**
1551 Load an XML file from disk.
1552 Returns XML_NO_ERROR (0) on success, or
1553 an errorID.
1554 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001555 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001556
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001557 /**
1558 Load an XML file from disk. You are responsible
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001559 for providing and closing the FILE*.
1560
1561 NOTE: The file should be opened as binary ("rb")
1562 not text in order for TinyXML-2 to correctly
1563 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001564
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001565 Returns XML_NO_ERROR (0) on success, or
1566 an errorID.
1567 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001568 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001569
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001570 /**
1571 Save the XML file to disk.
1572 Returns XML_NO_ERROR (0) on success, or
1573 an errorID.
1574 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001575 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001576
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001577 /**
1578 Save the XML file to disk. You are responsible
1579 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001580
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001581 Returns XML_NO_ERROR (0) on success, or
1582 an errorID.
1583 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001584 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001585
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001586 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001587 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001588 }
1589 Whitespace WhitespaceMode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001590 return _whitespace;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001591 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001592
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001593 /**
1594 Returns true if this document has a leading Byte Order Mark of UTF8.
1595 */
1596 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001597 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001598 }
1599 /** Sets whether to write the BOM when writing the file.
1600 */
1601 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001602 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001603 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001604
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001605 /** Return the root element of DOM. Equivalent to FirstChildElement().
1606 To get the first node, use FirstChild().
1607 */
1608 XMLElement* RootElement() {
1609 return FirstChildElement();
1610 }
1611 const XMLElement* RootElement() const {
1612 return FirstChildElement();
1613 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001614
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001615 /** Print the Document. If the Printer is not provided, it will
1616 print to stdout. If you provide Printer, this can print to a file:
1617 @verbatim
1618 XMLPrinter printer( fp );
1619 doc.Print( &printer );
1620 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001621
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001622 Or you can use a printer to print to memory:
1623 @verbatim
1624 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001625 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001626 // printer.CStr() has a const char* to the XML
1627 @endverbatim
1628 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001629 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001630 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001631
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001632 /**
1633 Create a new Element associated with
1634 this Document. The memory for the Element
1635 is managed by the Document.
1636 */
1637 XMLElement* NewElement( const char* name );
1638 /**
1639 Create a new Comment associated with
1640 this Document. The memory for the Comment
1641 is managed by the Document.
1642 */
1643 XMLComment* NewComment( const char* comment );
1644 /**
1645 Create a new Text associated with
1646 this Document. The memory for the Text
1647 is managed by the Document.
1648 */
1649 XMLText* NewText( const char* text );
1650 /**
1651 Create a new Declaration associated with
1652 this Document. The memory for the object
1653 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001654
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001655 If the 'text' param is null, the standard
1656 declaration is used.:
1657 @verbatim
1658 <?xml version="1.0" encoding="UTF-8"?>
1659 @endverbatim
1660 */
1661 XMLDeclaration* NewDeclaration( const char* text=0 );
1662 /**
1663 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001664 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001665 is managed by the Document.
1666 */
1667 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001668
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001669 /**
1670 Delete a node associated with this document.
1671 It will be unlinked from the DOM.
1672 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001673 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001674
Lee Thomason2fa81722012-11-09 12:37:46 -08001675 void SetError( XMLError error, const char* str1, const char* str2 );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001676
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001677 /// Return true if there was an error parsing the document.
1678 bool Error() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001679 return _errorID != XML_NO_ERROR;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001680 }
1681 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001682 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001683 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001684 }
Lee Thomason331596e2014-09-11 14:56:43 -07001685 const char* ErrorName() const;
1686
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001687 /// Return a possibly helpful diagnostic location or string.
1688 const char* GetErrorStr1() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001689 return _errorStr1;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001690 }
1691 /// Return a possibly helpful secondary diagnostic location or string.
1692 const char* GetErrorStr2() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001693 return _errorStr2;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001694 }
1695 /// If there is an error, print it to stdout.
1696 void PrintError() const;
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001697
1698 /// Clear the document, resetting it to the initial state.
1699 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001700
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001701 // internal
1702 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001703
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001704 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1705 return 0;
1706 }
1707 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1708 return false;
1709 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001710
Lee Thomason3f57d272012-01-11 15:30:03 -08001711private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001712 XMLDocument( const XMLDocument& ); // not supported
1713 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001714
Lee Thomason2fa81722012-11-09 12:37:46 -08001715 bool _writeBOM;
1716 bool _processEntities;
1717 XMLError _errorID;
1718 Whitespace _whitespace;
Lee Thomason624d43f2012-10-12 10:58:48 -07001719 const char* _errorStr1;
1720 const char* _errorStr2;
Lee Thomason2fa81722012-11-09 12:37:46 -08001721 char* _charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -08001722
Lee Thomason624d43f2012-10-12 10:58:48 -07001723 MemPoolT< sizeof(XMLElement) > _elementPool;
1724 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1725 MemPoolT< sizeof(XMLText) > _textPool;
1726 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001727
1728 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001729
1730 void Parse();
Lee Thomason5cae8972012-01-24 18:03:07 -08001731};
1732
Lee Thomason7c913cd2012-01-26 18:32:34 -08001733
Lee Thomason3ffdd392012-03-28 17:27:55 -07001734/**
1735 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001736 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001737 DOM structure. It is a separate utility class.
1738
1739 Take an example:
1740 @verbatim
1741 <Document>
1742 <Element attributeA = "valueA">
1743 <Child attributeB = "value1" />
1744 <Child attributeB = "value2" />
1745 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001746 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001747 @endverbatim
1748
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001749 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001750 easy to write a *lot* of code that looks like:
1751
1752 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001753 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001754 if ( root )
1755 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001756 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001757 if ( element )
1758 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001759 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001760 if ( child )
1761 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001762 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001763 if ( child2 )
1764 {
1765 // Finally do something useful.
1766 @endverbatim
1767
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001768 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001769 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001770 and correct to use:
1771
1772 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001773 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001774 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001775 if ( child2 )
1776 {
1777 // do something useful
1778 @endverbatim
1779
1780 Which is MUCH more concise and useful.
1781
1782 It is also safe to copy handles - internally they are nothing more than node pointers.
1783 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001784 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001785 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001786
1787 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001788*/
PKEuS16ed47d2013-07-06 12:02:43 +02001789class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001790{
1791public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001792 /// 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 -07001793 XMLHandle( XMLNode* node ) {
1794 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001795 }
1796 /// Create a handle from a node.
Lee Thomason624d43f2012-10-12 10:58:48 -07001797 XMLHandle( XMLNode& node ) {
1798 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001799 }
1800 /// Copy constructor
1801 XMLHandle( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001802 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001803 }
1804 /// Assignment
1805 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001806 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001807 return *this;
1808 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001809
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001810 /// Get the first child of this handle.
1811 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001812 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001813 }
1814 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001815 XMLHandle FirstChildElement( const char* name = 0 ) {
1816 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001817 }
1818 /// Get the last child of this handle.
1819 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001820 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001821 }
1822 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001823 XMLHandle LastChildElement( const char* name = 0 ) {
1824 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001825 }
1826 /// Get the previous sibling of this handle.
1827 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001828 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001829 }
1830 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001831 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
1832 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001833 }
1834 /// Get the next sibling of this handle.
1835 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001836 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001837 }
1838 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001839 XMLHandle NextSiblingElement( const char* name = 0 ) {
1840 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001841 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001842
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001843 /// Safe cast to XMLNode. This can return null.
1844 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001845 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001846 }
1847 /// Safe cast to XMLElement. This can return null.
1848 XMLElement* ToElement() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001849 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001850 }
1851 /// Safe cast to XMLText. This can return null.
1852 XMLText* ToText() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001853 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001854 }
1855 /// Safe cast to XMLUnknown. This can return null.
1856 XMLUnknown* ToUnknown() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001857 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001858 }
1859 /// Safe cast to XMLDeclaration. This can return null.
1860 XMLDeclaration* ToDeclaration() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001861 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001862 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001863
1864private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001865 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001866};
1867
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001868
1869/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001870 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
1871 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07001872*/
PKEuS16ed47d2013-07-06 12:02:43 +02001873class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07001874{
1875public:
Lee Thomason624d43f2012-10-12 10:58:48 -07001876 XMLConstHandle( const XMLNode* node ) {
1877 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001878 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001879 XMLConstHandle( const XMLNode& node ) {
1880 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001881 }
1882 XMLConstHandle( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001883 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001884 }
Lee Thomason8b899812012-04-04 15:58:16 -07001885
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001886 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001887 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001888 return *this;
1889 }
Lee Thomason8b899812012-04-04 15:58:16 -07001890
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001891 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001892 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001893 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001894 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
1895 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001896 }
1897 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001898 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001899 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001900 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
1901 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001902 }
1903 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001904 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001905 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001906 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
1907 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001908 }
1909 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001910 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001911 }
Dmitry-Me886ad972015-07-22 11:00:51 +03001912 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
1913 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001914 }
Lee Thomason8b899812012-04-04 15:58:16 -07001915
1916
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001917 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001918 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001919 }
1920 const XMLElement* ToElement() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001921 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001922 }
1923 const XMLText* ToText() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001924 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001925 }
1926 const XMLUnknown* ToUnknown() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001927 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001928 }
1929 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001930 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001931 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001932
Lee Thomason5cae8972012-01-24 18:03:07 -08001933private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001934 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08001935};
Lee Thomason6f381b72012-03-02 12:59:39 -08001936
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001937
1938/**
1939 Printing functionality. The XMLPrinter gives you more
1940 options than the XMLDocument::Print() method.
1941
1942 It can:
1943 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02001944 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001945 -# Print XML without a XMLDocument.
1946
1947 Print to Memory
1948
1949 @verbatim
1950 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001951 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02001952 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001953 @endverbatim
1954
1955 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001956
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001957 You provide the file pointer.
1958 @verbatim
1959 XMLPrinter printer( fp );
1960 doc.Print( &printer );
1961 @endverbatim
1962
1963 Print without a XMLDocument
1964
1965 When loading, an XML parser is very useful. However, sometimes
1966 when saving, it just gets in the way. The code is often set up
1967 for streaming, and constructing the DOM is just overhead.
1968
1969 The Printer supports the streaming case. The following code
1970 prints out a trivially simple XML file without ever creating
1971 an XML document.
1972
1973 @verbatim
1974 XMLPrinter printer( fp );
1975 printer.OpenElement( "foo" );
1976 printer.PushAttribute( "foo", "bar" );
1977 printer.CloseElement();
1978 @endverbatim
1979*/
PKEuS16ed47d2013-07-06 12:02:43 +02001980class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001981{
1982public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001983 /** Construct the printer. If the FILE* is specified,
1984 this will print to the FILE. Else it will print
1985 to memory, and the result is available in CStr().
1986 If 'compact' is set to true, then output is created
1987 with only required whitespace and newlines.
1988 */
PKEuS1bfb9542013-08-04 13:51:17 +02001989 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05001990 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001991
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001992 /** If streaming, write the BOM and declaration. */
1993 void PushHeader( bool writeBOM, bool writeDeclaration );
1994 /** If streaming, start writing an element.
1995 The element must be closed with CloseElement()
1996 */
Lee Thomason256adb62014-04-06 14:41:46 -07001997 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001998 /// If streaming, add an attribute to an open element.
1999 void PushAttribute( const char* name, const char* value );
2000 void PushAttribute( const char* name, int value );
2001 void PushAttribute( const char* name, unsigned value );
2002 void PushAttribute( const char* name, bool value );
2003 void PushAttribute( const char* name, double value );
2004 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002005 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002006
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002007 /// Add a text node.
2008 void PushText( const char* text, bool cdata=false );
2009 /// Add a text node from an integer.
2010 void PushText( int value );
2011 /// Add a text node from an unsigned.
2012 void PushText( unsigned value );
2013 /// Add a text node from a bool.
2014 void PushText( bool value );
2015 /// Add a text node from a float.
2016 void PushText( float value );
2017 /// Add a text node from a double.
2018 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002019
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002020 /// Add a comment
2021 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002022
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002023 void PushDeclaration( const char* value );
2024 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002025
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002026 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2027 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2028 return true;
2029 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002030
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002031 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2032 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002033
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002034 virtual bool Visit( const XMLText& text );
2035 virtual bool Visit( const XMLComment& comment );
2036 virtual bool Visit( const XMLDeclaration& declaration );
2037 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002038
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002039 /**
2040 If in print to memory mode, return a pointer to
2041 the XML file in memory.
2042 */
2043 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002044 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002045 }
2046 /**
2047 If in print to memory mode, return the size
2048 of the XML file in memory. (Note the size returned
2049 includes the terminating null.)
2050 */
2051 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002052 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002053 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002054 /**
2055 If in print to memory mode, reset the buffer to the
2056 beginning.
2057 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002058 void ClearBuffer() {
2059 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002060 _buffer.Push(0);
2061 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002062
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002063protected:
Alexander Maid740b642014-05-20 22:04:42 +02002064 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002065
Lee Thomasonc18eb232014-02-21 17:31:17 -08002066 /** Prints out the space before an element. You may override to change
2067 the space and tabs used. A PrintSpace() override should call Print().
2068 */
2069 virtual void PrintSpace( int depth );
2070 void Print( const char* format, ... );
2071
Dmitry-Mea092bc12014-12-23 17:57:05 +03002072 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002073 bool _elementJustOpened;
2074 DynArray< const char*, 10 > _stack;
2075
2076private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002077 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002078
Lee Thomason624d43f2012-10-12 10:58:48 -07002079 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002080 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002081 int _depth;
2082 int _textDepth;
2083 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002084 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002085
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002086 enum {
2087 ENTITY_RANGE = 64,
2088 BUF_SIZE = 200
2089 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002090 bool _entityFlag[ENTITY_RANGE];
2091 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002092
Lee Thomason624d43f2012-10-12 10:58:48 -07002093 DynArray< char, 20 > _buffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002094};
2095
2096
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002097} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002098
PKEuS95060352013-07-26 10:42:44 +02002099#if defined(_MSC_VER)
2100# pragma warning(pop)
2101#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002102
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002103#endif // TINYXML2_INCLUDED