blob: 5f8bfcde69d4b7fb76d1a906af242c2b00aabbc5 [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 Thomasonf07b9522014-10-30 13:25:12 -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
Lee Thomasonf07b9522014-10-30 13:25:12 -0700264 const T& PeekTop() const {
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500265 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 Thomasonf07b9522014-10-30 13:25:12 -0700320 virtual void Clear() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800321};
322
Lee Thomason50adb4c2012-02-13 15:07:09 -0800323
U-Stream\Leeae25a442012-02-17 17:48:16 -0800324/*
325 Template child class to create pools of the correct type.
326*/
Lee Thomasond1983222012-02-06 08:41:24 -0800327template< int SIZE >
PKEuS95060352013-07-26 10:42:44 +0200328class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800329{
330public:
Lee Thomason5b0a6772012-11-19 13:54:42 -0800331 MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700332 ~MemPoolT() {
Lee Thomasonf07b9522014-10-30 13:25:12 -0700333 Clear();
334 }
335
336 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700337 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700338 while( !_blockPtrs.Empty()) {
339 Block* b = _blockPtrs.Pop();
340 delete b;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700341 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700342 _root = 0;
343 _currentAllocs = 0;
344 _nAllocs = 0;
345 _maxAllocs = 0;
346 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700347 }
Lee Thomasond1983222012-02-06 08:41:24 -0800348
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700349 virtual int ItemSize() const {
350 return SIZE;
351 }
352 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700353 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700354 }
Lee Thomasond1983222012-02-06 08:41:24 -0800355
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700356 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700357 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700358 // Need a new block.
359 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700360 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800361
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700362 for( int i=0; i<COUNT-1; ++i ) {
363 block->chunk[i].next = &block->chunk[i+1];
364 }
365 block->chunk[COUNT-1].next = 0;
Lee Thomason624d43f2012-10-12 10:58:48 -0700366 _root = block->chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700367 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700368 void* result = _root;
369 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800370
Lee Thomason624d43f2012-10-12 10:58:48 -0700371 ++_currentAllocs;
372 if ( _currentAllocs > _maxAllocs ) {
373 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700374 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700375 _nAllocs++;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800376 _nUntracked++;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700377 return result;
378 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700379
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700380 virtual void Free( void* mem ) {
381 if ( !mem ) {
382 return;
383 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700384 --_currentAllocs;
Dmitry-Me56571762014-08-15 11:03:47 +0400385 Chunk* chunk = static_cast<Chunk*>( mem );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700386#ifdef DEBUG
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700387 memset( chunk, 0xfe, sizeof(Chunk) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700388#endif
Lee Thomason624d43f2012-10-12 10:58:48 -0700389 chunk->next = _root;
390 _root = chunk;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700391 }
392 void Trace( const char* name ) {
393 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Lee Thomason624d43f2012-10-12 10:58:48 -0700394 name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700395 }
Lee Thomasond1983222012-02-06 08:41:24 -0800396
Lee Thomason5b0a6772012-11-19 13:54:42 -0800397 void SetTracked() {
398 _nUntracked--;
399 }
400
401 int Untracked() const {
402 return _nUntracked;
403 }
404
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800405 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
406 // The test file is large, 170k.
407 // Release: VS2010 gcc(no opt)
408 // 1k: 4000
409 // 2k: 4000
410 // 4k: 3900 21000
411 // 16k: 5200
412 // 32k: 4300
413 // 64k: 4000 21000
414 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 +0200415
Lee Thomasond1983222012-02-06 08:41:24 -0800416private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700417 union Chunk {
Lee Thomason624d43f2012-10-12 10:58:48 -0700418 Chunk* next;
419 char mem[SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700420 };
421 struct Block {
Lee Thomason (grinliz)856da212012-10-19 09:08:15 -0700422 Chunk chunk[COUNT];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700423 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700424 DynArray< Block*, 10 > _blockPtrs;
425 Chunk* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800426
Lee Thomason624d43f2012-10-12 10:58:48 -0700427 int _currentAllocs;
428 int _nAllocs;
429 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800430 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800431};
432
Lee Thomason2c85a712012-01-31 08:24:24 -0800433
Lee Thomason56bdd022012-02-09 18:16:58 -0800434
435/**
436 Implements the interface to the "Visitor pattern" (see the Accept() method.)
437 If you call the Accept() method, it requires being passed a XMLVisitor
438 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200439 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800440 are simply called with Visit().
441
442 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700443 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800444
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700445 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800446 visiting). You need to only override methods that are interesting to you.
447
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600448 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800449
450 You should never change the document from a callback.
451
452 @sa XMLNode::Accept()
453*/
PKEuS16ed47d2013-07-06 12:02:43 +0200454class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800455{
456public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700457 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800458
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700459 /// Visit a document.
460 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
461 return true;
462 }
463 /// Visit a document.
464 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
465 return true;
466 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800467
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700468 /// Visit an element.
469 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
470 return true;
471 }
472 /// Visit an element.
473 virtual bool VisitExit( const XMLElement& /*element*/ ) {
474 return true;
475 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800476
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700477 /// Visit a declaration.
478 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
479 return true;
480 }
481 /// Visit a text node.
482 virtual bool Visit( const XMLText& /*text*/ ) {
483 return true;
484 }
485 /// Visit a comment node.
486 virtual bool Visit( const XMLComment& /*comment*/ ) {
487 return true;
488 }
489 /// Visit an unknown node.
490 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
491 return true;
492 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800493};
494
Lee Thomason331596e2014-09-11 14:56:43 -0700495// WARNING: must match XMLErrorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900496enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900497 XML_SUCCESS = 0,
498 XML_NO_ERROR = 0,
499 XML_NO_ATTRIBUTE,
500 XML_WRONG_ATTRIBUTE_TYPE,
501 XML_ERROR_FILE_NOT_FOUND,
502 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
503 XML_ERROR_FILE_READ_ERROR,
504 XML_ERROR_ELEMENT_MISMATCH,
505 XML_ERROR_PARSING_ELEMENT,
506 XML_ERROR_PARSING_ATTRIBUTE,
507 XML_ERROR_IDENTIFYING_TAG,
508 XML_ERROR_PARSING_TEXT,
509 XML_ERROR_PARSING_CDATA,
510 XML_ERROR_PARSING_COMMENT,
511 XML_ERROR_PARSING_DECLARATION,
512 XML_ERROR_PARSING_UNKNOWN,
513 XML_ERROR_EMPTY_DOCUMENT,
514 XML_ERROR_MISMATCHED_ELEMENT,
515 XML_ERROR_PARSING,
516 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700517 XML_NO_TEXT_NODE,
518
519 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900520};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900521
numatrumpetcd8550c2014-09-08 16:59:39 +0900522
U-Stream\Leeae25a442012-02-17 17:48:16 -0800523/*
524 Utility functionality.
525*/
Lee Thomason56bdd022012-02-09 18:16:58 -0800526class XMLUtil
527{
Lee Thomasond1983222012-02-06 08:41:24 -0800528public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700529 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
530 // correct, but simple, and usually works.
531 static const char* SkipWhiteSpace( const char* p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100532 while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<const unsigned char*>(p) ) ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700533 ++p;
534 }
535 return p;
536 }
537 static char* SkipWhiteSpace( char* p ) {
Dmitry-Me9de541f2014-09-24 14:21:36 +0400538 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p) ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700539 }
540 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100541 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700542 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200543
544 inline static bool IsNameStartChar( unsigned char ch ) {
545 return ( ( ch < 128 ) ? isalpha( ch ) : 1 )
546 || ch == ':'
547 || ch == '_';
548 }
549
550 inline static bool IsNameChar( unsigned char ch ) {
551 return IsNameStartChar( ch )
552 || isdigit( ch )
553 || ch == '.'
554 || ch == '-';
555 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800556
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700557 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
558 int n = 0;
559 if ( p == q ) {
560 return true;
561 }
562 while( *p && *q && *p == *q && n<nChar ) {
563 ++p;
564 ++q;
565 ++n;
566 }
567 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
568 return true;
569 }
570 return false;
571 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200572
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400573 inline static bool IsUTF8Continuation( const char p ) {
574 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700575 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800576
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700577 static const char* ReadBOM( const char* p, bool* hasBOM );
578 // p is the starting location,
579 // the UTF-8 value of the entity will be placed in value, and length filled in.
580 static const char* GetCharacterRef( const char* p, char* value, int* length );
581 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700582
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700583 // converts primitive types to strings
584 static void ToStr( int v, char* buffer, int bufferSize );
585 static void ToStr( unsigned v, char* buffer, int bufferSize );
586 static void ToStr( bool v, char* buffer, int bufferSize );
587 static void ToStr( float v, char* buffer, int bufferSize );
588 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason21be8822012-07-15 17:27:22 -0700589
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700590 // converts strings to primitive types
591 static bool ToInt( const char* str, int* value );
592 static bool ToUnsigned( const char* str, unsigned* value );
593 static bool ToBool( const char* str, bool* value );
594 static bool ToFloat( const char* str, float* value );
595 static bool ToDouble( const char* str, double* value );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800596};
597
Lee Thomason5cae8972012-01-24 18:03:07 -0800598
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800599/** XMLNode is a base class for every object that is in the
600 XML Document Object Model (DOM), except XMLAttributes.
601 Nodes have siblings, a parent, and children which can
602 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700603 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800604 be cast to its more defined type.
605
Thomas Roß08bdf502012-05-12 14:21:23 +0200606 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800607 When the XMLDocument gets deleted, all its Nodes
608 will also be deleted.
609
610 @verbatim
611 A Document can contain: Element (container or leaf)
612 Comment (leaf)
613 Unknown (leaf)
614 Declaration( leaf )
615
616 An Element can contain: Element (container or leaf)
617 Text (leaf)
618 Attributes (not on tree)
619 Comment (leaf)
620 Unknown (leaf)
621
622 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800623*/
PKEuS16ed47d2013-07-06 12:02:43 +0200624class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800625{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700626 friend class XMLDocument;
627 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800628public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800629
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700630 /// Get the XMLDocument that owns this XMLNode.
631 const XMLDocument* GetDocument() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700632 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700633 }
634 /// Get the XMLDocument that owns this XMLNode.
635 XMLDocument* GetDocument() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700636 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700637 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800638
Lee Thomason2fa81722012-11-09 12:37:46 -0800639 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700640 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100641 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700642 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800643 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700644 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100645 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700646 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800647 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700648 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100649 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700650 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800651 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700652 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100653 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700654 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800655 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700656 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100657 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700658 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800659 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700660 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100661 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700662 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800663
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700664 virtual const XMLElement* ToElement() const {
665 return 0;
666 }
667 virtual const XMLText* ToText() const {
668 return 0;
669 }
670 virtual const XMLComment* ToComment() const {
671 return 0;
672 }
673 virtual const XMLDocument* ToDocument() const {
674 return 0;
675 }
676 virtual const XMLDeclaration* ToDeclaration() const {
677 return 0;
678 }
679 virtual const XMLUnknown* ToUnknown() const {
680 return 0;
681 }
Lee Thomason751da522012-02-10 08:50:51 -0800682
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700683 /** The meaning of 'value' changes for the specific type.
684 @verbatim
685 Document: empty
686 Element: name of the element
687 Comment: the comment text
688 Unknown: the tag contents
689 Text: the text string
690 @endverbatim
691 */
Michael Daumling21626882013-10-22 17:03:37 +0200692 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100693
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700694 /** Set the Value of an XML node.
695 @sa Value()
696 */
697 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800698
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700699 /// Get the parent of this node on the DOM.
700 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700701 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100703
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700704 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700705 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700706 }
Lee Thomason751da522012-02-10 08:50:51 -0800707
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700708 /// Returns true if this node has no children.
709 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700710 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700711 }
Lee Thomason751da522012-02-10 08:50:51 -0800712
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700713 /// Get the first child node, or null if none exists.
714 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700715 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700716 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100717
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700718 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700719 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700720 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100721
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700722 /** Get the first child element, or optionally the first child
723 element with the specified name.
724 */
725 const XMLElement* FirstChildElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700726
727 XMLElement* FirstChildElement( const char* value=0 ) {
728 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700729 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800730
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700731 /// Get the last child node, or null if none exists.
732 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700733 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700734 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700735
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700736 XMLNode* LastChild() {
737 return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() );
738 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800739
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700740 /** Get the last child element or optionally the last child
741 element with the specified name.
742 */
743 const XMLElement* LastChildElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700744
745 XMLElement* LastChildElement( const char* value=0 ) {
746 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700747 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700748
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700749 /// Get the previous (left) sibling node of this node.
750 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700751 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700752 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700753
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700754 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700755 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700756 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800757
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700758 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700759 const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700760
761 XMLElement* PreviousSiblingElement( const char* value=0 ) {
762 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700763 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700764
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700765 /// Get the next (right) sibling node of this node.
766 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700767 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700768 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700769
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700770 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700771 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700772 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700773
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700774 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700775 const XMLElement* NextSiblingElement( const char* value=0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700776
777 XMLElement* NextSiblingElement( const char* value=0 ) {
778 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700779 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800780
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700781 /**
782 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200783 If the child node is already part of the document,
784 it is moved from its old location to the new location.
785 Returns the addThis argument or 0 if the node does not
786 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700787 */
788 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800789
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700790 XMLNode* LinkEndChild( XMLNode* addThis ) {
791 return InsertEndChild( addThis );
792 }
793 /**
794 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200795 If the child node is already part of the document,
796 it is moved from its old location to the new location.
797 Returns the addThis argument or 0 if the node does not
798 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700799 */
800 XMLNode* InsertFirstChild( XMLNode* addThis );
801 /**
802 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200803 If the child node is already part of the document,
804 it is moved from its old location to the new location.
805 Returns the addThis argument or 0 if the afterThis node
806 is not a child of this node, or if the node does not
807 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700808 */
809 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700810
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700811 /**
812 Delete all the children of this node.
813 */
814 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800815
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700816 /**
817 Delete a child of this node.
818 */
819 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800820
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700821 /**
822 Make a copy of this node, but not its children.
823 You may pass in a Document pointer that will be
824 the owner of the new Node. If the 'document' is
825 null, then the node returned will be allocated
826 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800827
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700828 Note: if called on a XMLDocument, this will return null.
829 */
830 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800831
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700832 /**
833 Test if 2 nodes are the same, but don't test children.
834 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800835
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700836 Note: if called on a XMLDocument, this will return false.
837 */
838 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800839
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600840 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700841 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600842 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800843
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600844 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
845 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700846 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800847
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700848 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800849
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700850 - http://www.saxproject.org/
851 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800852
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700853 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800854
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700855 An example of using Accept():
856 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600857 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700858 tinyxmlDoc.Accept( &printer );
859 const char* xmlcstr = printer.CStr();
860 @endverbatim
861 */
862 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800863
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700864 // internal
865 virtual char* ParseDeep( char*, StrPair* );
Lee Thomason3f57d272012-01-11 15:30:03 -0800866
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800867protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700868 XMLNode( XMLDocument* );
869 virtual ~XMLNode();
870 XMLNode( const XMLNode& ); // not supported
871 XMLNode& operator=( const XMLNode& ); // not supported
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700872
Lee Thomason624d43f2012-10-12 10:58:48 -0700873 XMLDocument* _document;
874 XMLNode* _parent;
875 mutable StrPair _value;
Lee Thomason3f57d272012-01-11 15:30:03 -0800876
Lee Thomason624d43f2012-10-12 10:58:48 -0700877 XMLNode* _firstChild;
878 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800879
Lee Thomason624d43f2012-10-12 10:58:48 -0700880 XMLNode* _prev;
881 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800882
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800883private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700884 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700885 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400886 static void DeleteNode( XMLNode* node );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800887};
888
889
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800890/** XML text.
891
892 Note that a text node can have child element nodes, for example:
893 @verbatim
894 <root>This is <b>bold</b></root>
895 @endverbatim
896
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700897 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800898 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 -0700899 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600900 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800901*/
PKEuS16ed47d2013-07-06 12:02:43 +0200902class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800903{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700904 friend class XMLBase;
905 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800906public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700907 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800908
Lee Thomason624d43f2012-10-12 10:58:48 -0700909 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700910 return this;
911 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700912 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700913 return this;
914 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800915
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700916 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700917 void SetCData( bool isCData ) {
918 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700919 }
920 /// Returns true if this is a CDATA text element.
921 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700922 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700923 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800924
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700925 char* ParseDeep( char*, StrPair* endTag );
926 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
927 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800928
Lee Thomason5492a1c2012-01-23 15:32:10 -0800929protected:
Lee Thomason624d43f2012-10-12 10:58:48 -0700930 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700931 virtual ~XMLText() {}
932 XMLText( const XMLText& ); // not supported
933 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -0800934
935private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700936 bool _isCData;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800937};
938
939
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800940/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +0200941class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800942{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700943 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -0800944public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700945 virtual XMLComment* ToComment() {
946 return this;
947 }
948 virtual const XMLComment* ToComment() const {
949 return this;
950 }
Lee Thomasonce0763e2012-01-11 15:43:54 -0800951
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700952 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800953
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700954 char* ParseDeep( char*, StrPair* endTag );
955 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
956 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -0800957
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800958protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700959 XMLComment( XMLDocument* doc );
960 virtual ~XMLComment();
961 XMLComment( const XMLComment& ); // not supported
962 XMLComment& operator=( const XMLComment& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800963
Lee Thomason3f57d272012-01-11 15:30:03 -0800964private:
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800965};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800966
967
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800968/** In correct XML the declaration is the first entry in the file.
969 @verbatim
970 <?xml version="1.0" standalone="yes"?>
971 @endverbatim
972
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600973 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800974 however.
975
976 The text of the declaration isn't interpreted. It is parsed
977 and written as a string.
978*/
PKEuS16ed47d2013-07-06 12:02:43 +0200979class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -0800980{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700981 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -0800982public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700983 virtual XMLDeclaration* ToDeclaration() {
984 return this;
985 }
986 virtual const XMLDeclaration* ToDeclaration() const {
987 return this;
988 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800989
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700990 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800991
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700992 char* ParseDeep( char*, StrPair* endTag );
993 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
994 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -0800995
996protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700997 XMLDeclaration( XMLDocument* doc );
998 virtual ~XMLDeclaration();
999 XMLDeclaration( const XMLDeclaration& ); // not supported
1000 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001001};
1002
1003
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001004/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001005 unknown. It is a tag of text, but should not be modified.
1006 It will be written back to the XML, unchanged, when the file
1007 is saved.
1008
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001009 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001010*/
PKEuS16ed47d2013-07-06 12:02:43 +02001011class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001012{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001013 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001014public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001015 virtual XMLUnknown* ToUnknown() {
1016 return this;
1017 }
1018 virtual const XMLUnknown* ToUnknown() const {
1019 return this;
1020 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001021
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001022 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001023
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001024 char* ParseDeep( char*, StrPair* endTag );
1025 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1026 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001027
1028protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001029 XMLUnknown( XMLDocument* doc );
1030 virtual ~XMLUnknown();
1031 XMLUnknown( const XMLUnknown& ); // not supported
1032 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001033};
1034
1035
Lee Thomason1ff38e02012-02-14 18:18:16 -08001036
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001037/** An attribute is a name-value pair. Elements have an arbitrary
1038 number of attributes, each with a unique name.
1039
1040 @note The attributes are not XMLNodes. You may only query the
1041 Next() attribute in a list.
1042*/
PKEuS16ed47d2013-07-06 12:02:43 +02001043class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001044{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001045 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001046public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001047 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001048 const char* Name() const;
1049
Lee Thomason2fa81722012-11-09 12:37:46 -08001050 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001051 const char* Value() const;
1052
Lee Thomason2fa81722012-11-09 12:37:46 -08001053 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001054 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001055 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001056 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001057
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001058 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001059 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001060 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001061 */
1062 int IntValue() const {
1063 int i=0;
1064 QueryIntValue( &i );
1065 return i;
1066 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001067 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001068 unsigned UnsignedValue() const {
1069 unsigned i=0;
1070 QueryUnsignedValue( &i );
1071 return i;
1072 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001073 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001074 bool BoolValue() const {
1075 bool b=false;
1076 QueryBoolValue( &b );
1077 return b;
1078 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001079 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001080 double DoubleValue() const {
1081 double d=0;
1082 QueryDoubleValue( &d );
1083 return d;
1084 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001085 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001086 float FloatValue() const {
1087 float f=0;
1088 QueryFloatValue( &f );
1089 return f;
1090 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001091
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001092 /** QueryIntValue interprets the attribute as an integer, and returns the value
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001093 in the provided parameter. The function will return XML_NO_ERROR on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001094 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1095 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001096 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001097 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001098 XMLError QueryUnsignedValue( unsigned int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001099 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001100 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001101 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001102 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001103 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001104 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001105
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001106 /// Set the attribute to a string value.
1107 void SetAttribute( const char* value );
1108 /// Set the attribute to value.
1109 void SetAttribute( int value );
1110 /// Set the attribute to value.
1111 void SetAttribute( unsigned value );
1112 /// Set the attribute to value.
1113 void SetAttribute( bool value );
1114 /// Set the attribute to value.
1115 void SetAttribute( double value );
1116 /// Set the attribute to value.
1117 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001118
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001119private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001120 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001121
Thomas Roß61892312013-05-12 14:07:38 +02001122 XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001123 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001124
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001125 XMLAttribute( const XMLAttribute& ); // not supported
1126 void operator=( const XMLAttribute& ); // not supported
1127 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001128
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001129 char* ParseDeep( char* p, bool processEntities );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001130
Lee Thomason624d43f2012-10-12 10:58:48 -07001131 mutable StrPair _name;
1132 mutable StrPair _value;
1133 XMLAttribute* _next;
1134 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001135};
1136
1137
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001138/** The element is a container class. It has a value, the element name,
1139 and can contain other elements, text, comments, and unknowns.
1140 Elements also contain an arbitrary number of attributes.
1141*/
PKEuS16ed47d2013-07-06 12:02:43 +02001142class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001143{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001144 friend class XMLBase;
1145 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001146public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001147 /// Get the name of an element (which is the Value() of the node.)
1148 const char* Name() const {
1149 return Value();
1150 }
1151 /// Set the name of the element.
1152 void SetName( const char* str, bool staticMem=false ) {
1153 SetValue( str, staticMem );
1154 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001155
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001156 virtual XMLElement* ToElement() {
1157 return this;
1158 }
1159 virtual const XMLElement* ToElement() const {
1160 return this;
1161 }
1162 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001163
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001164 /** Given an attribute name, Attribute() returns the value
1165 for the attribute of that name, or null if none
1166 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001167
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001168 @verbatim
1169 const char* value = ele->Attribute( "foo" );
1170 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001171
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001172 The 'value' parameter is normally null. However, if specified,
1173 the attribute will only be returned if the 'name' and 'value'
1174 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001175
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001176 @verbatim
1177 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1178 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001179
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001180 rather than:
1181 @verbatim
1182 if ( ele->Attribute( "foo" ) ) {
1183 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1184 }
1185 @endverbatim
1186 */
1187 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001188
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001189 /** Given an attribute name, IntAttribute() returns the value
1190 of the attribute interpreted as an integer. 0 will be
1191 returned if there is an error. For a method with error
1192 checking, see QueryIntAttribute()
1193 */
1194 int IntAttribute( const char* name ) const {
1195 int i=0;
1196 QueryIntAttribute( name, &i );
1197 return i;
1198 }
1199 /// See IntAttribute()
1200 unsigned UnsignedAttribute( const char* name ) const {
1201 unsigned i=0;
1202 QueryUnsignedAttribute( name, &i );
1203 return i;
1204 }
1205 /// See IntAttribute()
1206 bool BoolAttribute( const char* name ) const {
1207 bool b=false;
1208 QueryBoolAttribute( name, &b );
1209 return b;
1210 }
1211 /// See IntAttribute()
1212 double DoubleAttribute( const char* name ) const {
1213 double d=0;
1214 QueryDoubleAttribute( name, &d );
1215 return d;
1216 }
1217 /// See IntAttribute()
1218 float FloatAttribute( const char* name ) const {
1219 float f=0;
1220 QueryFloatAttribute( name, &f );
1221 return f;
1222 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001223
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001224 /** Given an attribute name, QueryIntAttribute() returns
1225 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1226 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1227 doesn't exist. If successful, the result of the conversion
1228 will be written to 'value'. If not successful, nothing will
1229 be written to 'value'. This allows you to provide default
1230 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001231
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001232 @verbatim
1233 int value = 10;
1234 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1235 @endverbatim
1236 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001237 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001238 const XMLAttribute* a = FindAttribute( name );
1239 if ( !a ) {
1240 return XML_NO_ATTRIBUTE;
1241 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001242 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001243 }
1244 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001245 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001246 const XMLAttribute* a = FindAttribute( name );
1247 if ( !a ) {
1248 return XML_NO_ATTRIBUTE;
1249 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001250 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001251 }
1252 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001253 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001254 const XMLAttribute* a = FindAttribute( name );
1255 if ( !a ) {
1256 return XML_NO_ATTRIBUTE;
1257 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001258 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001259 }
1260 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001261 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001262 const XMLAttribute* a = FindAttribute( name );
1263 if ( !a ) {
1264 return XML_NO_ATTRIBUTE;
1265 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001266 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001267 }
1268 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001269 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001270 const XMLAttribute* a = FindAttribute( name );
1271 if ( !a ) {
1272 return XML_NO_ATTRIBUTE;
1273 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001274 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001275 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001276
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001277
1278 /** Given an attribute name, QueryAttribute() returns
1279 XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
1280 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1281 doesn't exist. It is overloaded for the primitive types,
1282 and is a generally more convenient replacement of
1283 QueryIntAttribute() and related functions.
1284
1285 If successful, the result of the conversion
1286 will be written to 'value'. If not successful, nothing will
1287 be written to 'value'. This allows you to provide default
1288 value:
1289
1290 @verbatim
1291 int value = 10;
1292 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1293 @endverbatim
1294 */
1295 int QueryAttribute( const char* name, int* value ) const {
1296 return QueryIntAttribute( name, value );
1297 }
1298
1299 int QueryAttribute( const char* name, unsigned int* value ) const {
1300 return QueryUnsignedAttribute( name, value );
1301 }
1302
1303 int QueryAttribute( const char* name, bool* value ) const {
1304 return QueryBoolAttribute( name, value );
1305 }
1306
1307 int QueryAttribute( const char* name, double* value ) const {
1308 return QueryDoubleAttribute( name, value );
1309 }
1310
1311 int QueryAttribute( const char* name, float* value ) const {
1312 return QueryFloatAttribute( name, value );
1313 }
1314
1315 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001316 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001317 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001318 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001319 }
1320 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001321 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001322 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001323 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001324 }
1325 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001326 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001327 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001328 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001329 }
1330 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001331 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001332 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001333 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001334 }
1335 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001336 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001337 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001338 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001339 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001340 /// Sets the named attribute to value.
1341 void SetAttribute( const char* name, float value ) {
1342 XMLAttribute* a = FindOrCreateAttribute( name );
1343 a->SetAttribute( value );
1344 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001345
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001346 /**
1347 Delete an attribute.
1348 */
1349 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001350
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001351 /// Return the first attribute in the list.
1352 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001353 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001354 }
1355 /// Query a specific attribute in the list.
1356 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001357
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001358 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001359 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001360 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001361
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001362 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001363 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001364
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001365 This is a convenient method for getting the text of simple contained text:
1366 @verbatim
1367 <foo>This is text</foo>
1368 const char* str = fooElement->GetText();
1369 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001370
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001371 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001372
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001373 Note that this function can be misleading. If the element foo was created from
1374 this XML:
1375 @verbatim
1376 <foo><b>This is text</b></foo>
1377 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001378
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001379 then the value of str would be null. The first child node isn't a text node, it is
1380 another element. From this XML:
1381 @verbatim
1382 <foo>This is <b>text</b></foo>
1383 @endverbatim
1384 GetText() will return "This is ".
1385 */
1386 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001387
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001388 /** Convenience function for easy access to the text inside an element. Although easy
1389 and concise, SetText() is limited compared to creating an XMLText child
1390 and mutating it directly.
1391
1392 If the first child of 'this' is a XMLText, SetText() sets its value to
1393 the given string, otherwise it will create a first child that is an XMLText.
1394
1395 This is a convenient method for setting the text of simple contained text:
1396 @verbatim
1397 <foo>This is text</foo>
1398 fooElement->SetText( "Hullaballoo!" );
1399 <foo>Hullaballoo!</foo>
1400 @endverbatim
1401
1402 Note that this function can be misleading. If the element foo was created from
1403 this XML:
1404 @verbatim
1405 <foo><b>This is text</b></foo>
1406 @endverbatim
1407
1408 then it will not change "This is text", but rather prefix it with a text element:
1409 @verbatim
1410 <foo>Hullaballoo!<b>This is text</b></foo>
1411 @endverbatim
1412
1413 For this XML:
1414 @verbatim
1415 <foo />
1416 @endverbatim
1417 SetText() will generate
1418 @verbatim
1419 <foo>Hullaballoo!</foo>
1420 @endverbatim
1421 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001422 void SetText( const char* inText );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001423 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001424 void SetText( int value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001425 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001426 void SetText( unsigned value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001427 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001428 void SetText( bool value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001429 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001430 void SetText( double value );
Lee Thomasonc18eb232014-02-21 17:31:17 -08001431 /// Convenience method for setting text inside and element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001432 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001433
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001434 /**
1435 Convenience method to query the value of a child text node. This is probably best
1436 shown by example. Given you have a document is this form:
1437 @verbatim
1438 <point>
1439 <x>1</x>
1440 <y>1.4</y>
1441 </point>
1442 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001443
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001444 The QueryIntText() and similar functions provide a safe and easier way to get to the
1445 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001446
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001447 @verbatim
1448 int x = 0;
1449 float y = 0; // types of x and y are contrived for example
1450 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1451 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1452 xElement->QueryIntText( &x );
1453 yElement->QueryFloatText( &y );
1454 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001455
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001456 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1457 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 -07001458
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001459 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001460 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001461 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001462 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001463 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001464 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001465 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001466 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001467 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001468 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001469
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001470 // internal:
1471 enum {
1472 OPEN, // <foo>
1473 CLOSED, // <foo/>
1474 CLOSING // </foo>
1475 };
1476 int ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001477 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001478 }
1479 char* ParseDeep( char* p, StrPair* endTag );
1480 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1481 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001482
Lee Thomason50adb4c2012-02-13 15:07:09 -08001483private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001484 XMLElement( XMLDocument* doc );
1485 virtual ~XMLElement();
1486 XMLElement( const XMLElement& ); // not supported
1487 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001488
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001489 XMLAttribute* FindAttribute( const char* name );
1490 XMLAttribute* FindOrCreateAttribute( const char* name );
1491 //void LinkAttribute( XMLAttribute* attrib );
1492 char* ParseAttributes( char* p );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001493 static void DeleteAttribute( XMLAttribute* attribute );
Lee Thomason67d61312012-01-24 16:01:51 -08001494
Lee Thomason5bb2d802014-01-24 10:42:57 -08001495 enum { BUF_SIZE = 200 };
Lee Thomason624d43f2012-10-12 10:58:48 -07001496 int _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001497 // The attribute list is ordered; there is no 'lastAttribute'
1498 // because the list needs to be scanned for dupes before adding
1499 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001500 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001501};
1502
1503
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001504enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001505 PRESERVE_WHITESPACE,
1506 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001507};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001508
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001509
1510/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001511 It can be saved, loaded, and printed to the screen.
1512 All Nodes are connected and allocated to a Document.
1513 If the Document is deleted, all its Nodes are also deleted.
1514*/
PKEuS16ed47d2013-07-06 12:02:43 +02001515class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001516{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001517 friend class XMLElement;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001518public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001519 /// constructor
1520 XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
1521 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001522
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001523 virtual XMLDocument* ToDocument() {
1524 return this;
1525 }
1526 virtual const XMLDocument* ToDocument() const {
1527 return this;
1528 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001529
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001530 /**
1531 Parse an XML file from a character string.
1532 Returns XML_NO_ERROR (0) on success, or
1533 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001534
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001535 You may optionally pass in the 'nBytes', which is
1536 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001537 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001538 null terminated string.
1539 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001540 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001541
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001542 /**
1543 Load an XML file from disk.
1544 Returns XML_NO_ERROR (0) on success, or
1545 an errorID.
1546 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001547 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001548
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001549 /**
1550 Load an XML file from disk. You are responsible
1551 for providing and closing the FILE*.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001552
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001553 Returns XML_NO_ERROR (0) on success, or
1554 an errorID.
1555 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001556 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001557
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001558 /**
1559 Save the XML file to disk.
1560 Returns XML_NO_ERROR (0) on success, or
1561 an errorID.
1562 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001563 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001564
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001565 /**
1566 Save the XML file to disk. You are responsible
1567 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001568
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001569 Returns XML_NO_ERROR (0) on success, or
1570 an errorID.
1571 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001572 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001573
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001574 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001575 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001576 }
1577 Whitespace WhitespaceMode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001578 return _whitespace;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001579 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001580
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001581 /**
1582 Returns true if this document has a leading Byte Order Mark of UTF8.
1583 */
1584 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001585 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001586 }
1587 /** Sets whether to write the BOM when writing the file.
1588 */
1589 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001590 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001591 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001592
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001593 /** Return the root element of DOM. Equivalent to FirstChildElement().
1594 To get the first node, use FirstChild().
1595 */
1596 XMLElement* RootElement() {
1597 return FirstChildElement();
1598 }
1599 const XMLElement* RootElement() const {
1600 return FirstChildElement();
1601 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001602
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001603 /** Print the Document. If the Printer is not provided, it will
1604 print to stdout. If you provide Printer, this can print to a file:
1605 @verbatim
1606 XMLPrinter printer( fp );
1607 doc.Print( &printer );
1608 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001609
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001610 Or you can use a printer to print to memory:
1611 @verbatim
1612 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001613 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001614 // printer.CStr() has a const char* to the XML
1615 @endverbatim
1616 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001617 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001618 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001619
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001620 /**
1621 Create a new Element associated with
1622 this Document. The memory for the Element
1623 is managed by the Document.
1624 */
1625 XMLElement* NewElement( const char* name );
1626 /**
1627 Create a new Comment associated with
1628 this Document. The memory for the Comment
1629 is managed by the Document.
1630 */
1631 XMLComment* NewComment( const char* comment );
1632 /**
1633 Create a new Text associated with
1634 this Document. The memory for the Text
1635 is managed by the Document.
1636 */
1637 XMLText* NewText( const char* text );
1638 /**
1639 Create a new Declaration associated with
1640 this Document. The memory for the object
1641 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001642
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001643 If the 'text' param is null, the standard
1644 declaration is used.:
1645 @verbatim
1646 <?xml version="1.0" encoding="UTF-8"?>
1647 @endverbatim
1648 */
1649 XMLDeclaration* NewDeclaration( const char* text=0 );
1650 /**
1651 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001652 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001653 is managed by the Document.
1654 */
1655 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001656
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001657 /**
1658 Delete a node associated with this document.
1659 It will be unlinked from the DOM.
1660 */
1661 void DeleteNode( XMLNode* node ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001662 node->_parent->DeleteChild( node );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001663 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001664
Lee Thomason2fa81722012-11-09 12:37:46 -08001665 void SetError( XMLError error, const char* str1, const char* str2 );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001666
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001667 /// Return true if there was an error parsing the document.
1668 bool Error() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001669 return _errorID != XML_NO_ERROR;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001670 }
1671 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001672 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001673 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001674 }
Lee Thomason331596e2014-09-11 14:56:43 -07001675 const char* ErrorName() const;
1676
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001677 /// Return a possibly helpful diagnostic location or string.
1678 const char* GetErrorStr1() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001679 return _errorStr1;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001680 }
1681 /// Return a possibly helpful secondary diagnostic location or string.
1682 const char* GetErrorStr2() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001683 return _errorStr2;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001684 }
1685 /// If there is an error, print it to stdout.
1686 void PrintError() const;
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001687
1688 /// Clear the document, resetting it to the initial state.
1689 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001690
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001691 // internal
1692 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001693
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001694 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1695 return 0;
1696 }
1697 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1698 return false;
1699 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001700
Lee Thomason3f57d272012-01-11 15:30:03 -08001701private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001702 XMLDocument( const XMLDocument& ); // not supported
1703 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001704
Lee Thomason2fa81722012-11-09 12:37:46 -08001705 bool _writeBOM;
1706 bool _processEntities;
1707 XMLError _errorID;
1708 Whitespace _whitespace;
Lee Thomason624d43f2012-10-12 10:58:48 -07001709 const char* _errorStr1;
1710 const char* _errorStr2;
Lee Thomason2fa81722012-11-09 12:37:46 -08001711 char* _charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -08001712
Lee Thomason624d43f2012-10-12 10:58:48 -07001713 MemPoolT< sizeof(XMLElement) > _elementPool;
1714 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1715 MemPoolT< sizeof(XMLText) > _textPool;
1716 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001717
1718 static const char* _errorNames[XML_ERROR_COUNT];
Lee Thomason5cae8972012-01-24 18:03:07 -08001719};
1720
Lee Thomason7c913cd2012-01-26 18:32:34 -08001721
Lee Thomason3ffdd392012-03-28 17:27:55 -07001722/**
1723 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001724 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001725 DOM structure. It is a separate utility class.
1726
1727 Take an example:
1728 @verbatim
1729 <Document>
1730 <Element attributeA = "valueA">
1731 <Child attributeB = "value1" />
1732 <Child attributeB = "value2" />
1733 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001734 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001735 @endverbatim
1736
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001737 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001738 easy to write a *lot* of code that looks like:
1739
1740 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001741 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001742 if ( root )
1743 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001744 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001745 if ( element )
1746 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001747 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001748 if ( child )
1749 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001750 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001751 if ( child2 )
1752 {
1753 // Finally do something useful.
1754 @endverbatim
1755
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001756 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001757 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001758 and correct to use:
1759
1760 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001761 XMLHandle docHandle( &document );
1762 XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001763 if ( child2 )
1764 {
1765 // do something useful
1766 @endverbatim
1767
1768 Which is MUCH more concise and useful.
1769
1770 It is also safe to copy handles - internally they are nothing more than node pointers.
1771 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001772 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001773 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001774
1775 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001776*/
PKEuS16ed47d2013-07-06 12:02:43 +02001777class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001778{
1779public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001780 /// 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 -07001781 XMLHandle( XMLNode* node ) {
1782 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001783 }
1784 /// Create a handle from a node.
Lee Thomason624d43f2012-10-12 10:58:48 -07001785 XMLHandle( XMLNode& node ) {
1786 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001787 }
1788 /// Copy constructor
1789 XMLHandle( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001790 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001791 }
1792 /// Assignment
1793 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001794 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001795 return *this;
1796 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001797
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001798 /// Get the first child of this handle.
1799 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001800 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001801 }
1802 /// Get the first child element of this handle.
1803 XMLHandle FirstChildElement( const char* value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001804 return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001805 }
1806 /// Get the last child of this handle.
1807 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001808 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001809 }
1810 /// Get the last child element of this handle.
1811 XMLHandle LastChildElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001812 return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001813 }
1814 /// Get the previous sibling of this handle.
1815 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001816 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001817 }
1818 /// Get the previous sibling element of this handle.
1819 XMLHandle PreviousSiblingElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001820 return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001821 }
1822 /// Get the next sibling of this handle.
1823 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001824 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001825 }
1826 /// Get the next sibling element of this handle.
1827 XMLHandle NextSiblingElement( const char* _value=0 ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001828 return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001829 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001830
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001831 /// Safe cast to XMLNode. This can return null.
1832 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001833 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001834 }
1835 /// Safe cast to XMLElement. This can return null.
1836 XMLElement* ToElement() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001837 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001838 }
1839 /// Safe cast to XMLText. This can return null.
1840 XMLText* ToText() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001841 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001842 }
1843 /// Safe cast to XMLUnknown. This can return null.
1844 XMLUnknown* ToUnknown() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001845 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001846 }
1847 /// Safe cast to XMLDeclaration. This can return null.
1848 XMLDeclaration* ToDeclaration() {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001849 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001850 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001851
1852private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001853 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001854};
1855
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001856
1857/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001858 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
1859 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07001860*/
PKEuS16ed47d2013-07-06 12:02:43 +02001861class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07001862{
1863public:
Lee Thomason624d43f2012-10-12 10:58:48 -07001864 XMLConstHandle( const XMLNode* node ) {
1865 _node = node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001866 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001867 XMLConstHandle( const XMLNode& node ) {
1868 _node = &node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001869 }
1870 XMLConstHandle( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001871 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001872 }
Lee Thomason8b899812012-04-04 15:58:16 -07001873
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001874 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001875 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001876 return *this;
1877 }
Lee Thomason8b899812012-04-04 15:58:16 -07001878
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001879 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001880 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001881 }
1882 const XMLConstHandle FirstChildElement( const char* value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001883 return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001884 }
1885 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001886 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001887 }
1888 const XMLConstHandle LastChildElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001889 return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001890 }
1891 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001892 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001893 }
1894 const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001895 return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001896 }
1897 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001898 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001899 }
1900 const XMLConstHandle NextSiblingElement( const char* _value=0 ) const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001901 return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001902 }
Lee Thomason8b899812012-04-04 15:58:16 -07001903
1904
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001905 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001906 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001907 }
1908 const XMLElement* ToElement() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001909 return ( ( _node == 0 ) ? 0 : _node->ToElement() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001910 }
1911 const XMLText* ToText() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001912 return ( ( _node == 0 ) ? 0 : _node->ToText() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001913 }
1914 const XMLUnknown* ToUnknown() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001915 return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001916 }
1917 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meb6b4e822014-08-27 17:17:47 +04001918 return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001919 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001920
Lee Thomason5cae8972012-01-24 18:03:07 -08001921private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001922 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08001923};
Lee Thomason6f381b72012-03-02 12:59:39 -08001924
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001925
1926/**
1927 Printing functionality. The XMLPrinter gives you more
1928 options than the XMLDocument::Print() method.
1929
1930 It can:
1931 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02001932 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001933 -# Print XML without a XMLDocument.
1934
1935 Print to Memory
1936
1937 @verbatim
1938 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001939 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02001940 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001941 @endverbatim
1942
1943 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001944
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001945 You provide the file pointer.
1946 @verbatim
1947 XMLPrinter printer( fp );
1948 doc.Print( &printer );
1949 @endverbatim
1950
1951 Print without a XMLDocument
1952
1953 When loading, an XML parser is very useful. However, sometimes
1954 when saving, it just gets in the way. The code is often set up
1955 for streaming, and constructing the DOM is just overhead.
1956
1957 The Printer supports the streaming case. The following code
1958 prints out a trivially simple XML file without ever creating
1959 an XML document.
1960
1961 @verbatim
1962 XMLPrinter printer( fp );
1963 printer.OpenElement( "foo" );
1964 printer.PushAttribute( "foo", "bar" );
1965 printer.CloseElement();
1966 @endverbatim
1967*/
PKEuS16ed47d2013-07-06 12:02:43 +02001968class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001969{
1970public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001971 /** Construct the printer. If the FILE* is specified,
1972 this will print to the FILE. Else it will print
1973 to memory, and the result is available in CStr().
1974 If 'compact' is set to true, then output is created
1975 with only required whitespace and newlines.
1976 */
PKEuS1bfb9542013-08-04 13:51:17 +02001977 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05001978 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001979
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001980 /** If streaming, write the BOM and declaration. */
1981 void PushHeader( bool writeBOM, bool writeDeclaration );
1982 /** If streaming, start writing an element.
1983 The element must be closed with CloseElement()
1984 */
Lee Thomason256adb62014-04-06 14:41:46 -07001985 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001986 /// If streaming, add an attribute to an open element.
1987 void PushAttribute( const char* name, const char* value );
1988 void PushAttribute( const char* name, int value );
1989 void PushAttribute( const char* name, unsigned value );
1990 void PushAttribute( const char* name, bool value );
1991 void PushAttribute( const char* name, double value );
1992 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07001993 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001994
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001995 /// Add a text node.
1996 void PushText( const char* text, bool cdata=false );
1997 /// Add a text node from an integer.
1998 void PushText( int value );
1999 /// Add a text node from an unsigned.
2000 void PushText( unsigned value );
2001 /// Add a text node from a bool.
2002 void PushText( bool value );
2003 /// Add a text node from a float.
2004 void PushText( float value );
2005 /// Add a text node from a double.
2006 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002007
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002008 /// Add a comment
2009 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002010
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002011 void PushDeclaration( const char* value );
2012 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002013
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002014 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2015 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2016 return true;
2017 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002018
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002019 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2020 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002021
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002022 virtual bool Visit( const XMLText& text );
2023 virtual bool Visit( const XMLComment& comment );
2024 virtual bool Visit( const XMLDeclaration& declaration );
2025 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002026
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002027 /**
2028 If in print to memory mode, return a pointer to
2029 the XML file in memory.
2030 */
2031 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002032 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002033 }
2034 /**
2035 If in print to memory mode, return the size
2036 of the XML file in memory. (Note the size returned
2037 includes the terminating null.)
2038 */
2039 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002040 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002041 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002042 /**
2043 If in print to memory mode, reset the buffer to the
2044 beginning.
2045 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002046 void ClearBuffer() {
2047 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002048 _buffer.Push(0);
2049 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002050
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002051protected:
Alexander Maid740b642014-05-20 22:04:42 +02002052 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002053
Lee Thomasonc18eb232014-02-21 17:31:17 -08002054 /** Prints out the space before an element. You may override to change
2055 the space and tabs used. A PrintSpace() override should call Print().
2056 */
2057 virtual void PrintSpace( int depth );
2058 void Print( const char* format, ... );
2059
2060 void SealElement();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002061 bool _elementJustOpened;
2062 DynArray< const char*, 10 > _stack;
2063
2064private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002065 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002066
Lee Thomason624d43f2012-10-12 10:58:48 -07002067 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002068 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002069 int _depth;
2070 int _textDepth;
2071 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002072 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002073
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002074 enum {
2075 ENTITY_RANGE = 64,
2076 BUF_SIZE = 200
2077 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002078 bool _entityFlag[ENTITY_RANGE];
2079 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002080
Lee Thomason624d43f2012-10-12 10:58:48 -07002081 DynArray< char, 20 > _buffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002082};
2083
2084
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002085} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002086
PKEuS95060352013-07-26 10:42:44 +02002087#if defined(_MSC_VER)
2088# pragma warning(pop)
2089#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002090
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002091#endif // TINYXML2_INCLUDED