blob: c95f8c314c526e3e4e33321aa5381b50fa95f46a [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
Lee Thomason (grinliz)db304252013-07-31 12:24:52 -070017
Lee Thomason (grinliz)28129862012-02-25 21:11:20 -0800182. Altered source versions must be plainly marked as such, and
19must not be misrepresented as being the original software.
20
213. This notice may not be removed or altered from any source
22distribution.
23*/
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -080024
Lee Thomason7d00b9a2012-02-27 17:54:22 -080025#ifndef TINYXML2_INCLUDED
U-Lama\Leee13c3e62011-12-28 14:36:55 -080026#define TINYXML2_INCLUDED
27
Jerome Martinez242c3ea2013-01-06 12:20:04 +010028#if defined(ANDROID_NDK) || defined(__BORLANDC__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070029# include <ctype.h>
30# include <limits.h>
31# include <stdio.h>
32# include <stdlib.h>
33# include <string.h>
34# include <stdarg.h>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070035#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -070036# include <cctype>
37# include <climits>
38# include <cstdio>
39# include <cstdlib>
40# include <cstring>
41# include <cstdarg>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070042#endif
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070043
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -070044/*
Lee Thomason7d00b9a2012-02-27 17:54:22 -080045 TODO: intern strings instead of allocation.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080046*/
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080047/*
Lee Thomasona9cf3f92012-10-11 16:56:51 -070048 gcc:
Lee Thomason5b0a6772012-11-19 13:54:42 -080049 g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
MortenMacFly4ee49f12013-01-14 20:03:14 +010050
Lee Thomasona9cf3f92012-10-11 16:56:51 -070051 Formatting, Artistic Style:
52 AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080053*/
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080054
U-Lama\Lee4cee6112011-12-31 14:58:18 -080055#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070056# ifndef DEBUG
57# define DEBUG
58# endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080059#endif
60
PKEuS95060352013-07-26 10:42:44 +020061#ifdef _MSC_VER
62# pragma warning(push)
63# pragma warning(disable: 4251)
64#endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080065
PKEuS16ed47d2013-07-06 12:02:43 +020066#ifdef _WIN32
67# ifdef TINYXML2_EXPORT
68# define TINYXML2_LIB __declspec(dllexport)
69# elif defined(TINYXML2_IMPORT)
70# define TINYXML2_LIB __declspec(dllimport)
71# else
72# define TINYXML2_LIB
73# endif
74#else
75# define TINYXML2_LIB
76#endif
77
78
U-Lama\Lee4cee6112011-12-31 14:58:18 -080079#if defined(DEBUG)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070080# if defined(_MSC_VER)
81# define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
82# elif defined (ANDROID_NDK)
83# include <android/log.h>
84# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
85# else
86# include <assert.h>
87# define TIXMLASSERT assert
88# endif
89# else
90# define TIXMLASSERT( x ) {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080091#endif
92
U-Lama\Leee13c3e62011-12-28 14:36:55 -080093
pffang91d34a02014-07-10 10:02:35 +080094#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070095// Microsoft visual studio, version 2005 and higher.
96/*int _snprintf_s(
97 char *buffer,
98 size_t sizeOfBuffer,
99 size_t count,
100 const char *format [,
101 argument] ...
102);*/
103inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
104{
105 va_list va;
106 va_start( va, format );
107 int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
108 va_end( va );
109 return result;
110}
111#define TIXML_SSCANF sscanf_s
pffang91d34a02014-07-10 10:02:35 +0800112#elif defined WINCE
113#define TIXML_SNPRINTF _snprintf
114#define TIXML_SSCANF sscanf
Lee Thomason (grinliz)b9e791f2012-04-06 21:27:10 -0700115#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700116// GCC version 3 and higher
117//#warning( "Using sn* functions." )
118#define TIXML_SNPRINTF snprintf
119#define TIXML_SSCANF sscanf
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800120#endif
Lee Thomason1ff38e02012-02-14 18:18:16 -0800121
Lee Thomasonc18eb232014-02-21 17:31:17 -0800122/* Versioning, past 1.0.14:
Lee Thomason85afe9c2014-02-23 21:42:16 -0800123 http://semver.org/
Lee Thomasonc18eb232014-02-21 17:31:17 -0800124*/
Lee Thomason5321a0e2014-09-14 12:47:24 -0700125static const int TIXML2_MAJOR_VERSION = 2;
126static const int TIXML2_MINOR_VERSION = 2;
127static const int TIXML2_PATCH_VERSION = 0;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800128
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800129namespace tinyxml2
130{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800131class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800132class XMLElement;
133class XMLAttribute;
134class XMLComment;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800135class XMLText;
Lee Thomason50f97b22012-02-11 16:33:40 -0800136class XMLDeclaration;
137class XMLUnknown;
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800138class XMLPrinter;
Lee Thomason5cae8972012-01-24 18:03:07 -0800139
U-Stream\Leeae25a442012-02-17 17:48:16 -0800140/*
141 A class that wraps strings. Normally stores the start and end
142 pointers into the XML file itself, and will apply normalization
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800143 and entity translation if actually read. Can also store (and memory
U-Stream\Leeae25a442012-02-17 17:48:16 -0800144 manage) a traditional char[]
145*/
PKEuS95060352013-07-26 10:42:44 +0200146class StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800147{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800148public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700149 enum {
150 NEEDS_ENTITY_PROCESSING = 0x01,
151 NEEDS_NEWLINE_NORMALIZATION = 0x02,
selfpoisede77e1952013-03-13 14:08:29 +0800152 COLLAPSE_WHITESPACE = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800153
selfpoisede77e1952013-03-13 14:08:29 +0800154 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700155 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
selfpoisede77e1952013-03-13 14:08:29 +0800156 ATTRIBUTE_NAME = 0,
157 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
158 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
159 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700160 };
Lee Thomason39ede242012-01-20 11:27:56 -0800161
Lee Thomason120b3a62012-10-12 10:06:59 -0700162 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700163 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800164
Lee Thomason120b3a62012-10-12 10:06:59 -0700165 void Set( char* start, char* end, int flags ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700166 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700167 _start = start;
168 _end = end;
169 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700170 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700171
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700172 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700173
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700174 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700175 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700176 }
Lee Thomason39ede242012-01-20 11:27:56 -0800177
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700178 void SetInternedStr( const char* str ) {
179 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700180 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700181 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700182
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700183 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800184
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700185 char* ParseText( char* in, const char* endTag, int strFlags );
186 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800187
Lee Thomason39ede242012-01-20 11:27:56 -0800188private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700189 void Reset();
190 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800191
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700192 enum {
193 NEEDS_FLUSH = 0x100,
194 NEEDS_DELETE = 0x200
195 };
Lee Thomasone4422302012-01-20 17:59:50 -0800196
selfpoised4dd59bc2013-03-13 16:54:15 +0800197 // After parsing, if *_end != 0, it can be set to zero.
Lee Thomason120b3a62012-10-12 10:06:59 -0700198 int _flags;
199 char* _start;
200 char* _end;
Lee Thomason39ede242012-01-20 11:27:56 -0800201};
202
U-Lama\Lee560bd472011-12-28 19:42:49 -0800203
U-Stream\Leeae25a442012-02-17 17:48:16 -0800204/*
205 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
206 Has a small initial memory pool, so that low or no usage will not
207 cause a call to new/delete
208*/
Lee Thomason2c85a712012-01-31 08:24:24 -0800209template <class T, int INIT>
PKEuS95060352013-07-26 10:42:44 +0200210class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800211{
212public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700213 DynArray< T, INIT >() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700214 _mem = _pool;
215 _allocated = INIT;
216 _size = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700217 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700218
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700219 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700220 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700221 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700222 }
223 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700224
Lee Thomasonce0510b2013-11-26 21:29:37 -0800225 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100226 _size = 0;
227 }
228
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700229 void Push( T t ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700230 EnsureCapacity( _size+1 );
231 _mem[_size++] = t;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700232 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800233
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700234 T* PushArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700235 EnsureCapacity( _size+count );
236 T* ret = &_mem[_size];
237 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700238 return ret;
239 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700240
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700241 T Pop() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700242 return _mem[--_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700243 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700244
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700245 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700246 TIXMLASSERT( _size >= count );
247 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700248 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800249
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700250 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700251 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700252 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700253
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700254 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700255 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700256 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700257 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700258
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700259 const T& operator[](int i) const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700260 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700261 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700262 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700263
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500264 const T& PeekTop() const {
265 TIXMLASSERT( _size > 0 );
266 return _mem[ _size - 1];
267 }
268
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700269 int Size() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700270 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700271 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700272
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700273 int Capacity() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700274 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700275 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700276
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700277 const T* Mem() const {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700278 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700279 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700280
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700281 T* Mem() {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700282 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700283 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800284
Lee Thomason2c85a712012-01-31 08:24:24 -0800285private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700286 void EnsureCapacity( int cap ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700287 if ( cap > _allocated ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700288 int newAllocated = cap * 2;
289 T* newMem = new T[newAllocated];
Lee Thomason624d43f2012-10-12 10:58:48 -0700290 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
291 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700292 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700293 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700294 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700295 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700296 }
297 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800298
Lee Thomason624d43f2012-10-12 10:58:48 -0700299 T* _mem;
300 T _pool[INIT];
301 int _allocated; // objects allocated
302 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800303};
304
Lee Thomason50adb4c2012-02-13 15:07:09 -0800305
U-Stream\Leeae25a442012-02-17 17:48:16 -0800306/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200307 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800308 and deallocation of objects.
309*/
PKEuS95060352013-07-26 10:42:44 +0200310class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800311{
312public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700313 MemPool() {}
314 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800315
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700316 virtual int ItemSize() const = 0;
317 virtual void* Alloc() = 0;
318 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800319 virtual void SetTracked() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800320};
321
Lee Thomason50adb4c2012-02-13 15:07:09 -0800322
U-Stream\Leeae25a442012-02-17 17:48:16 -0800323/*
324 Template child class to create pools of the correct type.
325*/
Lee Thomasond1983222012-02-06 08:41:24 -0800326template< int SIZE >
PKEuS95060352013-07-26 10:42:44 +0200327class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800328{
329public:
Lee Thomason5b0a6772012-11-19 13:54:42 -0800330 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700331 ~MemPoolT() {
332 // Delete the blocks.
Lee Thomason624d43f2012-10-12 10:58:48 -0700333 for( int i=0; i<_blockPtrs.Size(); ++i ) {
334 delete _blockPtrs[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700335 }
336 }
Lee Thomasond1983222012-02-06 08:41:24 -0800337
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700338 virtual int ItemSize() const {
339 return SIZE;
340 }
341 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700342 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700343 }
Lee Thomasond1983222012-02-06 08:41:24 -0800344
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700345 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700346 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700347 // Need a new block.
348 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700349 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800350
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700351 for( int i=0; i<COUNT-1; ++i ) {
352 block->chunk[i].next = &block->chunk[i+1];
353 }
354 block->chunk[COUNT-1].next = 0;
Lee Thomason624d43f2012-10-12 10:58:48 -0700355 _root = block->chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700356 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700357 void* result = _root;
358 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800359
Lee Thomason624d43f2012-10-12 10:58:48 -0700360 ++_currentAllocs;
361 if ( _currentAllocs > _maxAllocs ) {
362 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700363 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700364 _nAllocs++;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800365 _nUntracked++;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700366 return result;
367 }
368 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:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700405 union Chunk {
Lee Thomason624d43f2012-10-12 10:58:48 -0700406 Chunk* next;
407 char mem[SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700408 };
409 struct Block {
Lee Thomason (grinliz)856da212012-10-19 09:08:15 -0700410 Chunk chunk[COUNT];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700411 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700412 DynArray< Block*, 10 > _blockPtrs;
413 Chunk* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800414
Lee Thomason624d43f2012-10-12 10:58:48 -0700415 int _currentAllocs;
416 int _nAllocs;
417 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800418 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800419};
420
Lee Thomason2c85a712012-01-31 08:24:24 -0800421
Lee Thomason56bdd022012-02-09 18:16:58 -0800422
423/**
424 Implements the interface to the "Visitor pattern" (see the Accept() method.)
425 If you call the Accept() method, it requires being passed a XMLVisitor
426 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200427 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800428 are simply called with Visit().
429
430 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700431 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800432
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700433 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800434 visiting). You need to only override methods that are interesting to you.
435
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600436 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800437
438 You should never change the document from a callback.
439
440 @sa XMLNode::Accept()
441*/
PKEuS16ed47d2013-07-06 12:02:43 +0200442class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800443{
444public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700445 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800446
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700447 /// Visit a document.
448 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
449 return true;
450 }
451 /// Visit a document.
452 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
453 return true;
454 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800455
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700456 /// Visit an element.
457 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
458 return true;
459 }
460 /// Visit an element.
461 virtual bool VisitExit( const XMLElement& /*element*/ ) {
462 return true;
463 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800464
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700465 /// Visit a declaration.
466 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
467 return true;
468 }
469 /// Visit a text node.
470 virtual bool Visit( const XMLText& /*text*/ ) {
471 return true;
472 }
473 /// Visit a comment node.
474 virtual bool Visit( const XMLComment& /*comment*/ ) {
475 return true;
476 }
477 /// Visit an unknown node.
478 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
479 return true;
480 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800481};
482
Lee Thomason331596e2014-09-11 14:56:43 -0700483// WARNING: must match XMLErrorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900484enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900485 XML_SUCCESS = 0,
486 XML_NO_ERROR = 0,
487 XML_NO_ATTRIBUTE,
488 XML_WRONG_ATTRIBUTE_TYPE,
489 XML_ERROR_FILE_NOT_FOUND,
490 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
491 XML_ERROR_FILE_READ_ERROR,
492 XML_ERROR_ELEMENT_MISMATCH,
493 XML_ERROR_PARSING_ELEMENT,
494 XML_ERROR_PARSING_ATTRIBUTE,
495 XML_ERROR_IDENTIFYING_TAG,
496 XML_ERROR_PARSING_TEXT,
497 XML_ERROR_PARSING_CDATA,
498 XML_ERROR_PARSING_COMMENT,
499 XML_ERROR_PARSING_DECLARATION,
500 XML_ERROR_PARSING_UNKNOWN,
501 XML_ERROR_EMPTY_DOCUMENT,
502 XML_ERROR_MISMATCHED_ELEMENT,
503 XML_ERROR_PARSING,
504 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700505 XML_NO_TEXT_NODE,
506
507 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900508};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900509
numatrumpetcd8550c2014-09-08 16:59:39 +0900510
U-Stream\Leeae25a442012-02-17 17:48:16 -0800511/*
512 Utility functionality.
513*/
Lee Thomason56bdd022012-02-09 18:16:58 -0800514class XMLUtil
515{
Lee Thomasond1983222012-02-06 08:41:24 -0800516public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700517 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
518 // correct, but simple, and usually works.
519 static const char* SkipWhiteSpace( const char* p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100520 while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<const unsigned char*>(p) ) ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700521 ++p;
522 }
523 return p;
524 }
525 static char* SkipWhiteSpace( char* p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100526 while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<unsigned char*>(p) ) ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700527 ++p;
528 }
529 return p;
530 }
531 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100532 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700533 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200534
535 inline static bool IsNameStartChar( unsigned char ch ) {
536 return ( ( ch < 128 ) ? isalpha( ch ) : 1 )
537 || ch == ':'
538 || ch == '_';
539 }
540
541 inline static bool IsNameChar( unsigned char ch ) {
542 return IsNameStartChar( ch )
543 || isdigit( ch )
544 || ch == '.'
545 || ch == '-';
546 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800547
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700548 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
549 int n = 0;
550 if ( p == q ) {
551 return true;
552 }
553 while( *p && *q && *p == *q && n<nChar ) {
554 ++p;
555 ++q;
556 ++n;
557 }
558 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
559 return true;
560 }
561 return false;
562 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200563
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400564 inline static bool IsUTF8Continuation( const char p ) {
565 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700566 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800567
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700568 static const char* ReadBOM( const char* p, bool* hasBOM );
569 // p is the starting location,
570 // the UTF-8 value of the entity will be placed in value, and length filled in.
571 static const char* GetCharacterRef( const char* p, char* value, int* length );
572 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700573
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700574 // converts primitive types to strings
575 static void ToStr( int v, char* buffer, int bufferSize );
576 static void ToStr( unsigned v, char* buffer, int bufferSize );
577 static void ToStr( bool v, char* buffer, int bufferSize );
578 static void ToStr( float v, char* buffer, int bufferSize );
579 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason21be8822012-07-15 17:27:22 -0700580
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700581 // converts strings to primitive types
582 static bool ToInt( const char* str, int* value );
583 static bool ToUnsigned( const char* str, unsigned* value );
584 static bool ToBool( const char* str, bool* value );
585 static bool ToFloat( const char* str, float* value );
586 static bool ToDouble( const char* str, double* value );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800587};
588
Lee Thomason5cae8972012-01-24 18:03:07 -0800589
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800590/** XMLNode is a base class for every object that is in the
591 XML Document Object Model (DOM), except XMLAttributes.
592 Nodes have siblings, a parent, and children which can
593 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700594 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800595 be cast to its more defined type.
596
Thomas Roß08bdf502012-05-12 14:21:23 +0200597 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800598 When the XMLDocument gets deleted, all its Nodes
599 will also be deleted.
600
601 @verbatim
602 A Document can contain: Element (container or leaf)
603 Comment (leaf)
604 Unknown (leaf)
605 Declaration( leaf )
606
607 An Element can contain: Element (container or leaf)
608 Text (leaf)
609 Attributes (not on tree)
610 Comment (leaf)
611 Unknown (leaf)
612
613 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800614*/
PKEuS16ed47d2013-07-06 12:02:43 +0200615class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800616{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700617 friend class XMLDocument;
618 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800619public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800620
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700621 /// Get the XMLDocument that owns this XMLNode.
622 const XMLDocument* GetDocument() const {
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() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700627 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700628 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800629
Lee Thomason2fa81722012-11-09 12:37:46 -0800630 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700631 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100632 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700633 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800634 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700635 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100636 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700637 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800638 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700639 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100640 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700641 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800642 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700643 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100644 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700645 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800646 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700647 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100648 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700649 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800650 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700651 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100652 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700653 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800654
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700655 virtual const XMLElement* ToElement() const {
656 return 0;
657 }
658 virtual const XMLText* ToText() const {
659 return 0;
660 }
661 virtual const XMLComment* ToComment() const {
662 return 0;
663 }
664 virtual const XMLDocument* ToDocument() const {
665 return 0;
666 }
667 virtual const XMLDeclaration* ToDeclaration() const {
668 return 0;
669 }
670 virtual const XMLUnknown* ToUnknown() const {
671 return 0;
672 }
Lee Thomason751da522012-02-10 08:50:51 -0800673
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700674 /** The meaning of 'value' changes for the specific type.
675 @verbatim
676 Document: empty
677 Element: name of the element
678 Comment: the comment text
679 Unknown: the tag contents
680 Text: the text string
681 @endverbatim
682 */
Michael Daumling21626882013-10-22 17:03:37 +0200683 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100684
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700685 /** Set the Value of an XML node.
686 @sa Value()
687 */
688 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800689
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700690 /// Get the parent of this node on the DOM.
691 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700692 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700693 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100694
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700695 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700696 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700697 }
Lee Thomason751da522012-02-10 08:50:51 -0800698
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700699 /// Returns true if this node has no children.
700 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700701 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 }
Lee Thomason751da522012-02-10 08:50:51 -0800703
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700704 /// Get the first child node, or null if none exists.
705 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700706 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700707 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100708
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700709 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700710 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700711 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100712
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700713 /** Get the first child element, or optionally the first child
714 element with the specified name.
715 */
716 const XMLElement* FirstChildElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700717
718 XMLElement* FirstChildElement( const char* value=0 ) {
719 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700720 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800721
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700722 /// Get the last child node, or null if none exists.
723 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700724 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700725 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700726
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700727 XMLNode* LastChild() {
728 return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() );
729 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800730
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700731 /** Get the last child element or optionally the last child
732 element with the specified name.
733 */
734 const XMLElement* LastChildElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700735
736 XMLElement* LastChildElement( const char* value=0 ) {
737 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700738 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700739
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700740 /// Get the previous (left) sibling node of this node.
741 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700742 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700743 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700744
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700745 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700746 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700747 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800748
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700749 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700750 const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700751
752 XMLElement* PreviousSiblingElement( const char* value=0 ) {
753 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700754 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700755
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700756 /// Get the next (right) sibling node of this node.
757 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700758 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700759 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700760
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700761 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700762 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700763 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700764
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700765 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700766 const XMLElement* NextSiblingElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700767
768 XMLElement* NextSiblingElement( const char* value=0 ) {
769 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700770 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800771
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700772 /**
773 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200774 If the child node is already part of the document,
775 it is moved from its old location to the new location.
776 Returns the addThis argument or 0 if the node does not
777 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700778 */
779 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800780
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700781 XMLNode* LinkEndChild( XMLNode* addThis ) {
782 return InsertEndChild( addThis );
783 }
784 /**
785 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200786 If the child node is already part of the document,
787 it is moved from its old location to the new location.
788 Returns the addThis argument or 0 if the node does not
789 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700790 */
791 XMLNode* InsertFirstChild( XMLNode* addThis );
792 /**
793 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200794 If the child node is already part of the document,
795 it is moved from its old location to the new location.
796 Returns the addThis argument or 0 if the afterThis node
797 is not a child of this node, or if the node does not
798 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700799 */
800 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700801
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700802 /**
803 Delete all the children of this node.
804 */
805 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800806
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700807 /**
808 Delete a child of this node.
809 */
810 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800811
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700812 /**
813 Make a copy of this node, but not its children.
814 You may pass in a Document pointer that will be
815 the owner of the new Node. If the 'document' is
816 null, then the node returned will be allocated
817 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800818
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700819 Note: if called on a XMLDocument, this will return null.
820 */
821 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800822
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700823 /**
824 Test if 2 nodes are the same, but don't test children.
825 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800826
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700827 Note: if called on a XMLDocument, this will return false.
828 */
829 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800830
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600831 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700832 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600833 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800834
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600835 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
836 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700837 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800838
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700839 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800840
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700841 - http://www.saxproject.org/
842 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800843
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700844 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800845
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700846 An example of using Accept():
847 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600848 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700849 tinyxmlDoc.Accept( &printer );
850 const char* xmlcstr = printer.CStr();
851 @endverbatim
852 */
853 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800854
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700855 // internal
856 virtual char* ParseDeep( char*, StrPair* );
Lee Thomason3f57d272012-01-11 15:30:03 -0800857
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800858protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700859 XMLNode( XMLDocument* );
860 virtual ~XMLNode();
861 XMLNode( const XMLNode& ); // not supported
862 XMLNode& operator=( const XMLNode& ); // not supported
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700863
Lee Thomason624d43f2012-10-12 10:58:48 -0700864 XMLDocument* _document;
865 XMLNode* _parent;
866 mutable StrPair _value;
Lee Thomason3f57d272012-01-11 15:30:03 -0800867
Lee Thomason624d43f2012-10-12 10:58:48 -0700868 XMLNode* _firstChild;
869 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800870
Lee Thomason624d43f2012-10-12 10:58:48 -0700871 XMLNode* _prev;
872 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800873
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800874private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700875 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700876 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400877 static void DeleteNode( XMLNode* node );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800878};
879
880
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800881/** XML text.
882
883 Note that a text node can have child element nodes, for example:
884 @verbatim
885 <root>This is <b>bold</b></root>
886 @endverbatim
887
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700888 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800889 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 -0700890 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600891 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800892*/
PKEuS16ed47d2013-07-06 12:02:43 +0200893class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800894{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700895 friend class XMLBase;
896 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800897public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700898 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800899
Lee Thomason624d43f2012-10-12 10:58:48 -0700900 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700901 return this;
902 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700903 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700904 return this;
905 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800906
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700907 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700908 void SetCData( bool isCData ) {
909 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700910 }
911 /// Returns true if this is a CDATA text element.
912 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700913 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700914 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800915
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700916 char* ParseDeep( char*, StrPair* endTag );
917 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
918 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800919
Lee Thomason5492a1c2012-01-23 15:32:10 -0800920protected:
Lee Thomason624d43f2012-10-12 10:58:48 -0700921 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700922 virtual ~XMLText() {}
923 XMLText( const XMLText& ); // not supported
924 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -0800925
926private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700927 bool _isCData;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800928};
929
930
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800931/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +0200932class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800933{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700934 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -0800935public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700936 virtual XMLComment* ToComment() {
937 return this;
938 }
939 virtual const XMLComment* ToComment() const {
940 return this;
941 }
Lee Thomasonce0763e2012-01-11 15:43:54 -0800942
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700943 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800944
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700945 char* ParseDeep( char*, StrPair* endTag );
946 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
947 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -0800948
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800949protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700950 XMLComment( XMLDocument* doc );
951 virtual ~XMLComment();
952 XMLComment( const XMLComment& ); // not supported
953 XMLComment& operator=( const XMLComment& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800954
Lee Thomason3f57d272012-01-11 15:30:03 -0800955private:
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800956};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800957
958
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800959/** In correct XML the declaration is the first entry in the file.
960 @verbatim
961 <?xml version="1.0" standalone="yes"?>
962 @endverbatim
963
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600964 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800965 however.
966
967 The text of the declaration isn't interpreted. It is parsed
968 and written as a string.
969*/
PKEuS16ed47d2013-07-06 12:02:43 +0200970class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -0800971{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700972 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -0800973public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700974 virtual XMLDeclaration* ToDeclaration() {
975 return this;
976 }
977 virtual const XMLDeclaration* ToDeclaration() const {
978 return this;
979 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800980
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700981 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800982
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700983 char* ParseDeep( char*, StrPair* endTag );
984 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
985 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800986
987protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700988 XMLDeclaration( XMLDocument* doc );
989 virtual ~XMLDeclaration();
990 XMLDeclaration( const XMLDeclaration& ); // not supported
991 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -0800992};
993
994
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600995/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800996 unknown. It is a tag of text, but should not be modified.
997 It will be written back to the XML, unchanged, when the file
998 is saved.
999
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001000 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001001*/
PKEuS16ed47d2013-07-06 12:02:43 +02001002class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001003{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001004 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001005public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001006 virtual XMLUnknown* ToUnknown() {
1007 return this;
1008 }
1009 virtual const XMLUnknown* ToUnknown() const {
1010 return this;
1011 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001012
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001013 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001014
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001015 char* ParseDeep( char*, StrPair* endTag );
1016 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1017 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001018
1019protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001020 XMLUnknown( XMLDocument* doc );
1021 virtual ~XMLUnknown();
1022 XMLUnknown( const XMLUnknown& ); // not supported
1023 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001024};
1025
1026
Lee Thomason1ff38e02012-02-14 18:18:16 -08001027
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001028/** An attribute is a name-value pair. Elements have an arbitrary
1029 number of attributes, each with a unique name.
1030
1031 @note The attributes are not XMLNodes. You may only query the
1032 Next() attribute in a list.
1033*/
PKEuS16ed47d2013-07-06 12:02:43 +02001034class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001035{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001036 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001037public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001038 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001039 const char* Name() const;
1040
Lee Thomason2fa81722012-11-09 12:37:46 -08001041 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001042 const char* Value() const;
1043
Lee Thomason2fa81722012-11-09 12:37:46 -08001044 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001045 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001046 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001047 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001048
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001049 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001050 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001051 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001052 */
1053 int IntValue() const {
1054 int i=0;
1055 QueryIntValue( &i );
1056 return i;
1057 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001058 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001059 unsigned UnsignedValue() const {
1060 unsigned i=0;
1061 QueryUnsignedValue( &i );
1062 return i;
1063 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001064 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001065 bool BoolValue() const {
1066 bool b=false;
1067 QueryBoolValue( &b );
1068 return b;
1069 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001070 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001071 double DoubleValue() const {
1072 double d=0;
1073 QueryDoubleValue( &d );
1074 return d;
1075 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001076 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001077 float FloatValue() const {
1078 float f=0;
1079 QueryFloatValue( &f );
1080 return f;
1081 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001082
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001083 /** QueryIntValue interprets the attribute as an integer, and returns the value
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001084 in the provided parameter. The function will return XML_NO_ERROR on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001085 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1086 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001087 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001088 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001089 XMLError QueryUnsignedValue( unsigned int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001090 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001091 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001092 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001093 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001094 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001095 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001096
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001097 /// Set the attribute to a string value.
1098 void SetAttribute( const char* value );
1099 /// Set the attribute to value.
1100 void SetAttribute( int value );
1101 /// Set the attribute to value.
1102 void SetAttribute( unsigned value );
1103 /// Set the attribute to value.
1104 void SetAttribute( bool value );
1105 /// Set the attribute to value.
1106 void SetAttribute( double value );
1107 /// Set the attribute to value.
1108 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001109
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001110private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001111 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001112
Thomas Roß61892312013-05-12 14:07:38 +02001113 XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001114 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001115
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001116 XMLAttribute( const XMLAttribute& ); // not supported
1117 void operator=( const XMLAttribute& ); // not supported
1118 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001119
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001120 char* ParseDeep( char* p, bool processEntities );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001121
Lee Thomason624d43f2012-10-12 10:58:48 -07001122 mutable StrPair _name;
1123 mutable StrPair _value;
1124 XMLAttribute* _next;
1125 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001126};
1127
1128
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001129/** The element is a container class. It has a value, the element name,
1130 and can contain other elements, text, comments, and unknowns.
1131 Elements also contain an arbitrary number of attributes.
1132*/
PKEuS16ed47d2013-07-06 12:02:43 +02001133class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001134{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001135 friend class XMLBase;
1136 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001137public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001138 /// Get the name of an element (which is the Value() of the node.)
1139 const char* Name() const {
1140 return Value();
1141 }
1142 /// Set the name of the element.
1143 void SetName( const char* str, bool staticMem=false ) {
1144 SetValue( str, staticMem );
1145 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001146
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001147 virtual XMLElement* ToElement() {
1148 return this;
1149 }
1150 virtual const XMLElement* ToElement() const {
1151 return this;
1152 }
1153 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001154
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001155 /** Given an attribute name, Attribute() returns the value
1156 for the attribute of that name, or null if none
1157 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001158
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001159 @verbatim
1160 const char* value = ele->Attribute( "foo" );
1161 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001162
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001163 The 'value' parameter is normally null. However, if specified,
1164 the attribute will only be returned if the 'name' and 'value'
1165 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001166
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001167 @verbatim
1168 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1169 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001170
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001171 rather than:
1172 @verbatim
1173 if ( ele->Attribute( "foo" ) ) {
1174 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1175 }
1176 @endverbatim
1177 */
1178 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001179
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001180 /** Given an attribute name, IntAttribute() returns the value
1181 of the attribute interpreted as an integer. 0 will be
1182 returned if there is an error. For a method with error
1183 checking, see QueryIntAttribute()
1184 */
1185 int IntAttribute( const char* name ) const {
1186 int i=0;
1187 QueryIntAttribute( name, &i );
1188 return i;
1189 }
1190 /// See IntAttribute()
1191 unsigned UnsignedAttribute( const char* name ) const {
1192 unsigned i=0;
1193 QueryUnsignedAttribute( name, &i );
1194 return i;
1195 }
1196 /// See IntAttribute()
1197 bool BoolAttribute( const char* name ) const {
1198 bool b=false;
1199 QueryBoolAttribute( name, &b );
1200 return b;
1201 }
1202 /// See IntAttribute()
1203 double DoubleAttribute( const char* name ) const {
1204 double d=0;
1205 QueryDoubleAttribute( name, &d );
1206 return d;
1207 }
1208 /// See IntAttribute()
1209 float FloatAttribute( const char* name ) const {
1210 float f=0;
1211 QueryFloatAttribute( name, &f );
1212 return f;
1213 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001214
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001215 /** Given an attribute name, QueryIntAttribute() returns
1216 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1217 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1218 doesn't exist. If successful, the result of the conversion
1219 will be written to 'value'. If not successful, nothing will
1220 be written to 'value'. This allows you to provide default
1221 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001222
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001223 @verbatim
1224 int value = 10;
1225 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1226 @endverbatim
1227 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001228 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001229 const XMLAttribute* a = FindAttribute( name );
1230 if ( !a ) {
1231 return XML_NO_ATTRIBUTE;
1232 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001233 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001234 }
1235 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001236 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001237 const XMLAttribute* a = FindAttribute( name );
1238 if ( !a ) {
1239 return XML_NO_ATTRIBUTE;
1240 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001241 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001242 }
1243 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001244 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001245 const XMLAttribute* a = FindAttribute( name );
1246 if ( !a ) {
1247 return XML_NO_ATTRIBUTE;
1248 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001249 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001250 }
1251 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001252 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001253 const XMLAttribute* a = FindAttribute( name );
1254 if ( !a ) {
1255 return XML_NO_ATTRIBUTE;
1256 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001257 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001258 }
1259 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001260 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001261 const XMLAttribute* a = FindAttribute( name );
1262 if ( !a ) {
1263 return XML_NO_ATTRIBUTE;
1264 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001265 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001266 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001267
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001268
1269 /** Given an attribute name, QueryAttribute() returns
1270 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1271 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1272 doesn't exist. It is overloaded for the primitive types,
1273 and is a generally more convenient replacement of
1274 QueryIntAttribute() and related functions.
1275
1276 If successful, the result of the conversion
1277 will be written to 'value'. If not successful, nothing will
1278 be written to 'value'. This allows you to provide default
1279 value:
1280
1281 @verbatim
1282 int value = 10;
1283 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1284 @endverbatim
1285 */
1286 int QueryAttribute( const char* name, int* value ) const {
1287 return QueryIntAttribute( name, value );
1288 }
1289
1290 int QueryAttribute( const char* name, unsigned int* value ) const {
1291 return QueryUnsignedAttribute( name, value );
1292 }
1293
1294 int QueryAttribute( const char* name, bool* value ) const {
1295 return QueryBoolAttribute( name, value );
1296 }
1297
1298 int QueryAttribute( const char* name, double* value ) const {
1299 return QueryDoubleAttribute( name, value );
1300 }
1301
1302 int QueryAttribute( const char* name, float* value ) const {
1303 return QueryFloatAttribute( name, value );
1304 }
1305
1306 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001307 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001308 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001309 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001310 }
1311 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001312 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001313 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001314 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001315 }
1316 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001317 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001318 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001319 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001320 }
1321 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001322 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001323 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001324 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001325 }
1326 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001327 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001328 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001329 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001330 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001331 /// Sets the named attribute to value.
1332 void SetAttribute( const char* name, float value ) {
1333 XMLAttribute* a = FindOrCreateAttribute( name );
1334 a->SetAttribute( value );
1335 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001336
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001337 /**
1338 Delete an attribute.
1339 */
1340 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001341
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001342 /// Return the first attribute in the list.
1343 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001344 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001345 }
1346 /// Query a specific attribute in the list.
1347 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001348
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001349 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001350 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001351 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001352
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001353 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001354 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001355
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001356 This is a convenient method for getting the text of simple contained text:
1357 @verbatim
1358 <foo>This is text</foo>
1359 const char* str = fooElement->GetText();
1360 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001361
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001362 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001363
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001364 Note that this function can be misleading. If the element foo was created from
1365 this XML:
1366 @verbatim
1367 <foo><b>This is text</b></foo>
1368 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001369
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001370 then the value of str would be null. The first child node isn't a text node, it is
1371 another element. From this XML:
1372 @verbatim
1373 <foo>This is <b>text</b></foo>
1374 @endverbatim
1375 GetText() will return "This is ".
1376 */
1377 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001378
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001379 /** Convenience function for easy access to the text inside an element. Although easy
1380 and concise, SetText() is limited compared to creating an XMLText child
1381 and mutating it directly.
1382
1383 If the first child of 'this' is a XMLText, SetText() sets its value to
1384 the given string, otherwise it will create a first child that is an XMLText.
1385
1386 This is a convenient method for setting the text of simple contained text:
1387 @verbatim
1388 <foo>This is text</foo>
1389 fooElement->SetText( "Hullaballoo!" );
1390 <foo>Hullaballoo!</foo>
1391 @endverbatim
1392
1393 Note that this function can be misleading. If the element foo was created from
1394 this XML:
1395 @verbatim
1396 <foo><b>This is text</b></foo>
1397 @endverbatim
1398
1399 then it will not change "This is text", but rather prefix it with a text element:
1400 @verbatim
1401 <foo>Hullaballoo!<b>This is text</b></foo>
1402 @endverbatim
1403
1404 For this XML:
1405 @verbatim
1406 <foo />
1407 @endverbatim
1408 SetText() will generate
1409 @verbatim
1410 <foo>Hullaballoo!</foo>
1411 @endverbatim
1412 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001413 void SetText( const char* inText );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001414 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001415 void SetText( int value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001416 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001417 void SetText( unsigned value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001418 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001419 void SetText( bool value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001420 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001421 void SetText( double value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001422 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001423 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001424
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001425 /**
1426 Convenience method to query the value of a child text node. This is probably best
1427 shown by example. Given you have a document is this form:
1428 @verbatim
1429 <point>
1430 <x>1</x>
1431 <y>1.4</y>
1432 </point>
1433 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001434
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001435 The QueryIntText() and similar functions provide a safe and easier way to get to the
1436 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001437
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001438 @verbatim
1439 int x = 0;
1440 float y = 0; // types of x and y are contrived for example
1441 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1442 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1443 xElement->QueryIntText( &x );
1444 yElement->QueryFloatText( &y );
1445 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001446
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001447 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1448 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 -07001449
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001450 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001451 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001452 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001453 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001454 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001455 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001456 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001457 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001458 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001459 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001460
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001461 // internal:
1462 enum {
1463 OPEN, // <foo>
1464 CLOSED, // <foo/>
1465 CLOSING // </foo>
1466 };
1467 int ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001468 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001469 }
1470 char* ParseDeep( char* p, StrPair* endTag );
1471 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1472 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001473
Lee Thomason50adb4c2012-02-13 15:07:09 -08001474private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001475 XMLElement( XMLDocument* doc );
1476 virtual ~XMLElement();
1477 XMLElement( const XMLElement& ); // not supported
1478 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001479
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001480 XMLAttribute* FindAttribute( const char* name );
1481 XMLAttribute* FindOrCreateAttribute( const char* name );
1482 //void LinkAttribute( XMLAttribute* attrib );
1483 char* ParseAttributes( char* p );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001484 static void DeleteAttribute( XMLAttribute* attribute );
Lee Thomason67d61312012-01-24 16:01:51 -08001485
Lee Thomason5bb2d802014-01-24 10:42:57 -08001486 enum { BUF_SIZE = 200 };
Lee Thomason624d43f2012-10-12 10:58:48 -07001487 int _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001488 // The attribute list is ordered; there is no 'lastAttribute'
1489 // because the list needs to be scanned for dupes before adding
1490 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001491 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001492};
1493
1494
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001495enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001496 PRESERVE_WHITESPACE,
1497 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001498};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001499
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001500
1501/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001502 It can be saved, loaded, and printed to the screen.
1503 All Nodes are connected and allocated to a Document.
1504 If the Document is deleted, all its Nodes are also deleted.
1505*/
PKEuS16ed47d2013-07-06 12:02:43 +02001506class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001507{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001508 friend class XMLElement;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001509public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001510 /// constructor
1511 XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
1512 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001513
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001514 virtual XMLDocument* ToDocument() {
1515 return this;
1516 }
1517 virtual const XMLDocument* ToDocument() const {
1518 return this;
1519 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001520
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001521 /**
1522 Parse an XML file from a character string.
1523 Returns XML_NO_ERROR (0) on success, or
1524 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001525
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001526 You may optionally pass in the 'nBytes', which is
1527 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001528 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001529 null terminated string.
1530 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001531 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001532
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001533 /**
1534 Load an XML file from disk.
1535 Returns XML_NO_ERROR (0) on success, or
1536 an errorID.
1537 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001538 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001539
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001540 /**
1541 Load an XML file from disk. You are responsible
1542 for providing and closing the FILE*.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001543
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001544 Returns XML_NO_ERROR (0) on success, or
1545 an errorID.
1546 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001547 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001548
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001549 /**
1550 Save the XML file to disk.
1551 Returns XML_NO_ERROR (0) on success, or
1552 an errorID.
1553 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001554 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001555
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001556 /**
1557 Save the XML file to disk. You are responsible
1558 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001559
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001560 Returns XML_NO_ERROR (0) on success, or
1561 an errorID.
1562 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001563 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001564
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001565 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001566 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001567 }
1568 Whitespace WhitespaceMode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001569 return _whitespace;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001570 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001571
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001572 /**
1573 Returns true if this document has a leading Byte Order Mark of UTF8.
1574 */
1575 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001576 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001577 }
1578 /** Sets whether to write the BOM when writing the file.
1579 */
1580 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001581 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001582 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001583
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001584 /** Return the root element of DOM. Equivalent to FirstChildElement().
1585 To get the first node, use FirstChild().
1586 */
1587 XMLElement* RootElement() {
1588 return FirstChildElement();
1589 }
1590 const XMLElement* RootElement() const {
1591 return FirstChildElement();
1592 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001593
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001594 /** Print the Document. If the Printer is not provided, it will
1595 print to stdout. If you provide Printer, this can print to a file:
1596 @verbatim
1597 XMLPrinter printer( fp );
1598 doc.Print( &printer );
1599 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001600
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001601 Or you can use a printer to print to memory:
1602 @verbatim
1603 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001604 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001605 // printer.CStr() has a const char* to the XML
1606 @endverbatim
1607 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001608 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001609 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001610
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001611 /**
1612 Create a new Element associated with
1613 this Document. The memory for the Element
1614 is managed by the Document.
1615 */
1616 XMLElement* NewElement( const char* name );
1617 /**
1618 Create a new Comment associated with
1619 this Document. The memory for the Comment
1620 is managed by the Document.
1621 */
1622 XMLComment* NewComment( const char* comment );
1623 /**
1624 Create a new Text associated with
1625 this Document. The memory for the Text
1626 is managed by the Document.
1627 */
1628 XMLText* NewText( const char* text );
1629 /**
1630 Create a new Declaration associated with
1631 this Document. The memory for the object
1632 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001633
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001634 If the 'text' param is null, the standard
1635 declaration is used.:
1636 @verbatim
1637 <?xml version="1.0" encoding="UTF-8"?>
1638 @endverbatim
1639 */
1640 XMLDeclaration* NewDeclaration( const char* text=0 );
1641 /**
1642 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001643 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001644 is managed by the Document.
1645 */
1646 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001647
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001648 /**
1649 Delete a node associated with this document.
1650 It will be unlinked from the DOM.
1651 */
1652 void DeleteNode( XMLNode* node ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001653 node->_parent->DeleteChild( node );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001654 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001655
Lee Thomason2fa81722012-11-09 12:37:46 -08001656 void SetError( XMLError error, const char* str1, const char* str2 );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001657
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001658 /// Return true if there was an error parsing the document.
1659 bool Error() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001660 return _errorID != XML_NO_ERROR;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001661 }
1662 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001663 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001664 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001665 }
Lee Thomason331596e2014-09-11 14:56:43 -07001666 const char* ErrorName() const;
1667
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001668 /// Return a possibly helpful diagnostic location or string.
1669 const char* GetErrorStr1() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001670 return _errorStr1;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001671 }
1672 /// Return a possibly helpful secondary diagnostic location or string.
1673 const char* GetErrorStr2() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001674 return _errorStr2;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001675 }
1676 /// If there is an error, print it to stdout.
1677 void PrintError() const;
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001678
1679 /// Clear the document, resetting it to the initial state.
1680 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001681
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001682 // internal
1683 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001684
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001685 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1686 return 0;
1687 }
1688 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1689 return false;
1690 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001691
Lee Thomason3f57d272012-01-11 15:30:03 -08001692private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001693 XMLDocument( const XMLDocument& ); // not supported
1694 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001695
Lee Thomason2fa81722012-11-09 12:37:46 -08001696 bool _writeBOM;
1697 bool _processEntities;
1698 XMLError _errorID;
1699 Whitespace _whitespace;
Lee Thomason624d43f2012-10-12 10:58:48 -07001700 const char* _errorStr1;
1701 const char* _errorStr2;
Lee Thomason2fa81722012-11-09 12:37:46 -08001702 char* _charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -08001703
Lee Thomason624d43f2012-10-12 10:58:48 -07001704 MemPoolT< sizeof(XMLElement) > _elementPool;
1705 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1706 MemPoolT< sizeof(XMLText) > _textPool;
1707 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001708
1709 static const char* _errorNames[XML_ERROR_COUNT];
Lee Thomason5cae8972012-01-24 18:03:07 -08001710};
1711
Lee Thomason7c913cd2012-01-26 18:32:34 -08001712
Lee Thomason3ffdd392012-03-28 17:27:55 -07001713/**
1714 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001715 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001716 DOM structure. It is a separate utility class.
1717
1718 Take an example:
1719 @verbatim
1720 <Document>
1721 <Element attributeA = "valueA">
1722 <Child attributeB = "value1" />
1723 <Child attributeB = "value2" />
1724 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001725 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001726 @endverbatim
1727
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001728 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001729 easy to write a *lot* of code that looks like:
1730
1731 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001732 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001733 if ( root )
1734 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001735 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001736 if ( element )
1737 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001738 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001739 if ( child )
1740 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001741 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001742 if ( child2 )
1743 {
1744 // Finally do something useful.
1745 @endverbatim
1746
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001747 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001748 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001749 and correct to use:
1750
1751 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001752 XMLHandle docHandle( &document );
1753 XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001754 if ( child2 )
1755 {
1756 // do something useful
1757 @endverbatim
1758
1759 Which is MUCH more concise and useful.
1760
1761 It is also safe to copy handles - internally they are nothing more than node pointers.
1762 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001763 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001764 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001765
1766 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001767*/
PKEuS16ed47d2013-07-06 12:02:43 +02001768class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001769{
1770public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001771 /// 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 -07001772 XMLHandle( XMLNode* node ) {
1773 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001774 }
1775 /// Create a handle from a node.
Lee Thomason624d43f2012-10-12 10:58:48 -07001776 XMLHandle( XMLNode& node ) {
1777 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001778 }
1779 /// Copy constructor
1780 XMLHandle( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001781 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001782 }
1783 /// Assignment
1784 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001785 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001786 return *this;
1787 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001788
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001789 /// Get the first child of this handle.
1790 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001791 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001792 }
1793 /// Get the first child element of this handle.
1794 XMLHandle FirstChildElement( const char* value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001795 return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001796 }
1797 /// Get the last child of this handle.
1798 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001799 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001800 }
1801 /// Get the last child element of this handle.
1802 XMLHandle LastChildElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001803 return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001804 }
1805 /// Get the previous sibling of this handle.
1806 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001807 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001808 }
1809 /// Get the previous sibling element of this handle.
1810 XMLHandle PreviousSiblingElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001811 return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001812 }
1813 /// Get the next sibling of this handle.
1814 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001815 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001816 }
1817 /// Get the next sibling element of this handle.
1818 XMLHandle NextSiblingElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001819 return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001820 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001821
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001822 /// Safe cast to XMLNode. This can return null.
1823 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001824 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001825 }
1826 /// Safe cast to XMLElement. This can return null.
1827 XMLElement* ToElement() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001828 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001829 }
1830 /// Safe cast to XMLText. This can return null.
1831 XMLText* ToText() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001832 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001833 }
1834 /// Safe cast to XMLUnknown. This can return null.
1835 XMLUnknown* ToUnknown() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001836 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001837 }
1838 /// Safe cast to XMLDeclaration. This can return null.
1839 XMLDeclaration* ToDeclaration() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001840 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001841 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001842
1843private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001844 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001845};
1846
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001847
1848/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001849 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
1850 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07001851*/
PKEuS16ed47d2013-07-06 12:02:43 +02001852class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07001853{
1854public:
Lee Thomason624d43f2012-10-12 10:58:48 -07001855 XMLConstHandle( const XMLNode* node ) {
1856 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001857 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001858 XMLConstHandle( const XMLNode& node ) {
1859 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001860 }
1861 XMLConstHandle( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001862 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001863 }
Lee Thomason8b899812012-04-04 15:58:16 -07001864
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001865 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001866 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001867 return *this;
1868 }
Lee Thomason8b899812012-04-04 15:58:16 -07001869
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001870 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001871 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001872 }
1873 const XMLConstHandle FirstChildElement( const char* value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001874 return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001875 }
1876 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001877 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001878 }
1879 const XMLConstHandle LastChildElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001880 return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001881 }
1882 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001883 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001884 }
1885 const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001886 return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001887 }
1888 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001889 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001890 }
1891 const XMLConstHandle NextSiblingElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001892 return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001893 }
Lee Thomason8b899812012-04-04 15:58:16 -07001894
1895
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001896 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001897 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001898 }
1899 const XMLElement* ToElement() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001900 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001901 }
1902 const XMLText* ToText() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001903 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001904 }
1905 const XMLUnknown* ToUnknown() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001906 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001907 }
1908 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001909 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001910 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001911
Lee Thomason5cae8972012-01-24 18:03:07 -08001912private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001913 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08001914};
Lee Thomason6f381b72012-03-02 12:59:39 -08001915
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001916
1917/**
1918 Printing functionality. The XMLPrinter gives you more
1919 options than the XMLDocument::Print() method.
1920
1921 It can:
1922 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02001923 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001924 -# Print XML without a XMLDocument.
1925
1926 Print to Memory
1927
1928 @verbatim
1929 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001930 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02001931 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001932 @endverbatim
1933
1934 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001935
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001936 You provide the file pointer.
1937 @verbatim
1938 XMLPrinter printer( fp );
1939 doc.Print( &printer );
1940 @endverbatim
1941
1942 Print without a XMLDocument
1943
1944 When loading, an XML parser is very useful. However, sometimes
1945 when saving, it just gets in the way. The code is often set up
1946 for streaming, and constructing the DOM is just overhead.
1947
1948 The Printer supports the streaming case. The following code
1949 prints out a trivially simple XML file without ever creating
1950 an XML document.
1951
1952 @verbatim
1953 XMLPrinter printer( fp );
1954 printer.OpenElement( "foo" );
1955 printer.PushAttribute( "foo", "bar" );
1956 printer.CloseElement();
1957 @endverbatim
1958*/
PKEuS16ed47d2013-07-06 12:02:43 +02001959class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001960{
1961public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001962 /** Construct the printer. If the FILE* is specified,
1963 this will print to the FILE. Else it will print
1964 to memory, and the result is available in CStr().
1965 If 'compact' is set to true, then output is created
1966 with only required whitespace and newlines.
1967 */
PKEuS1bfb9542013-08-04 13:51:17 +02001968 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05001969 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001970
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001971 /** If streaming, write the BOM and declaration. */
1972 void PushHeader( bool writeBOM, bool writeDeclaration );
1973 /** If streaming, start writing an element.
1974 The element must be closed with CloseElement()
1975 */
Lee Thomason256adb62014-04-06 14:41:46 -07001976 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001977 /// If streaming, add an attribute to an open element.
1978 void PushAttribute( const char* name, const char* value );
1979 void PushAttribute( const char* name, int value );
1980 void PushAttribute( const char* name, unsigned value );
1981 void PushAttribute( const char* name, bool value );
1982 void PushAttribute( const char* name, double value );
1983 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07001984 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001985
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001986 /// Add a text node.
1987 void PushText( const char* text, bool cdata=false );
1988 /// Add a text node from an integer.
1989 void PushText( int value );
1990 /// Add a text node from an unsigned.
1991 void PushText( unsigned value );
1992 /// Add a text node from a bool.
1993 void PushText( bool value );
1994 /// Add a text node from a float.
1995 void PushText( float value );
1996 /// Add a text node from a double.
1997 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07001998
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001999 /// Add a comment
2000 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002001
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002002 void PushDeclaration( const char* value );
2003 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002004
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002005 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2006 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2007 return true;
2008 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002009
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002010 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2011 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002012
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002013 virtual bool Visit( const XMLText& text );
2014 virtual bool Visit( const XMLComment& comment );
2015 virtual bool Visit( const XMLDeclaration& declaration );
2016 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002017
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002018 /**
2019 If in print to memory mode, return a pointer to
2020 the XML file in memory.
2021 */
2022 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002023 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002024 }
2025 /**
2026 If in print to memory mode, return the size
2027 of the XML file in memory. (Note the size returned
2028 includes the terminating null.)
2029 */
2030 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002031 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002032 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002033 /**
2034 If in print to memory mode, reset the buffer to the
2035 beginning.
2036 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002037 void ClearBuffer() {
2038 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002039 _buffer.Push(0);
2040 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002041
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002042protected:
Alexander Maid740b642014-05-20 22:04:42 +02002043 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002044
Lee Thomasonc18eb232014-02-21 17:31:17 -08002045 /** Prints out the space before an element. You may override to change
2046 the space and tabs used. A PrintSpace() override should call Print().
2047 */
2048 virtual void PrintSpace( int depth );
2049 void Print( const char* format, ... );
2050
2051 void SealElement();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002052 bool _elementJustOpened;
2053 DynArray< const char*, 10 > _stack;
2054
2055private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002056 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002057
Lee Thomason624d43f2012-10-12 10:58:48 -07002058 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002059 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002060 int _depth;
2061 int _textDepth;
2062 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002063 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002064
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002065 enum {
2066 ENTITY_RANGE = 64,
2067 BUF_SIZE = 200
2068 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002069 bool _entityFlag[ENTITY_RANGE];
2070 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002071
Lee Thomason624d43f2012-10-12 10:58:48 -07002072 DynArray< char, 20 > _buffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002073};
2074
2075
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002076} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002077
PKEuS95060352013-07-26 10:42:44 +02002078#if defined(_MSC_VER)
2079# pragma warning(pop)
2080#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002081
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002082#endif // TINYXML2_INCLUDED