blob: a6408186c9d63ac2b60294fe10907240a48c9136 [file] [log] [blame]
Lee Thomason (grinliz)28129862012-02-25 21:11:20 -08001/*
2Original code by Lee Thomason (www.grinninglizard.com)
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any
6damages arising from the use of this software.
7
8Permission is granted to anyone to use this software for any
9purpose, including commercial applications, and to alter it and
10redistribute it freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must
13not claim that you wrote the original software. If you use this
14software in a product, an acknowledgment in the product documentation
15would be appreciated but is not required.
16
172. Altered source versions must be plainly marked as such, and
18must not be misrepresented as being the original software.
19
203. This notice may not be removed or altered from any source
21distribution.
22*/
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -080023
Lee Thomason7d00b9a2012-02-27 17:54:22 -080024#ifndef TINYXML2_INCLUDED
U-Lama\Leee13c3e62011-12-28 14:36:55 -080025#define TINYXML2_INCLUDED
26
Anton Indrawanf59e2d62014-11-18 20:50:42 +010027#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070028# include <ctype.h>
29# include <limits.h>
30# include <stdio.h>
31# include <stdlib.h>
32# include <string.h>
Lee Thomasona572db12016-06-04 19:16:24 -070033# if defined(__PS3__)
34# include <stddef.h>
35# endif
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070036#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -070037# include <cctype>
38# include <climits>
39# include <cstdio>
40# include <cstdlib>
41# include <cstring>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070042#endif
Lee Thomason1889c3e2016-06-04 20:22:57 -070043#include <stdint.h>
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070044
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -070045/*
Lee Thomason7d00b9a2012-02-27 17:54:22 -080046 TODO: intern strings instead of allocation.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080047*/
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080048/*
Lee Thomasona9cf3f92012-10-11 16:56:51 -070049 gcc:
Peter Matula50689912018-01-09 12:52:26 +010050 g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
MortenMacFly4ee49f12013-01-14 20:03:14 +010051
Lee Thomasona9cf3f92012-10-11 16:56:51 -070052 Formatting, Artistic Style:
53 AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080054*/
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080055
Lee Thomason7085f002017-06-01 18:09:43 -070056#if defined( _DEBUG ) || defined (__DEBUG__)
Peter Matula50689912018-01-09 12:52:26 +010057# ifndef TINYXML2_DEBUG
58# define TINYXML2_DEBUG
Lee Thomasona9cf3f92012-10-11 16:56:51 -070059# endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080060#endif
61
PKEuS95060352013-07-26 10:42:44 +020062#ifdef _MSC_VER
63# pragma warning(push)
64# pragma warning(disable: 4251)
65#endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080066
PKEuS16ed47d2013-07-06 12:02:43 +020067#ifdef _WIN32
68# ifdef TINYXML2_EXPORT
69# define TINYXML2_LIB __declspec(dllexport)
70# elif defined(TINYXML2_IMPORT)
71# define TINYXML2_LIB __declspec(dllimport)
72# else
73# define TINYXML2_LIB
74# endif
Matthew Woehlkea8e7ea72016-08-09 13:16:26 -040075#elif __GNUC__ >= 4
76# define TINYXML2_LIB __attribute__((visibility("default")))
PKEuS16ed47d2013-07-06 12:02:43 +020077#else
78# define TINYXML2_LIB
79#endif
80
81
Peter Matula50689912018-01-09 12:52:26 +010082#if defined(TINYXML2_DEBUG)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070083# if defined(_MSC_VER)
Dmitry-Me4bcbf142014-12-25 19:05:18 +030084# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
Lee Thomason598a88d2015-10-09 14:42:12 -070085# define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
Lee Thomasona9cf3f92012-10-11 16:56:51 -070086# elif defined (ANDROID_NDK)
87# include <android/log.h>
88# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
89# else
90# include <assert.h>
91# define TIXMLASSERT assert
92# endif
Lee Thomason598a88d2015-10-09 14:42:12 -070093#else
94# define TIXMLASSERT( x ) {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080095#endif
96
U-Lama\Leee13c3e62011-12-28 14:36:55 -080097
Lee Thomasonc18eb232014-02-21 17:31:17 -080098/* Versioning, past 1.0.14:
Lee Thomason85afe9c2014-02-23 21:42:16 -080099 http://semver.org/
Lee Thomasonc18eb232014-02-21 17:31:17 -0800100*/
Lee Thomason8c8293b2017-12-10 19:55:35 -0800101static const int TIXML2_MAJOR_VERSION = 6;
Lee Thomasonc1424ee2018-04-05 23:09:23 -0700102static const int TIXML2_MINOR_VERSION = 2;
Lee Thomason8c8293b2017-12-10 19:55:35 -0800103static const int TIXML2_PATCH_VERSION = 0;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800104
Lee Thomasonf26a5472017-12-29 09:30:39 -0800105#define TINYXML2_MAJOR_VERSION 6
Lee Thomasonc1424ee2018-04-05 23:09:23 -0700106#define TINYXML2_MINOR_VERSION 2
Lee Thomasonf26a5472017-12-29 09:30:39 -0800107#define TINYXML2_PATCH_VERSION 0
Lee Thomasonbd197872017-12-28 13:31:48 -0800108
Lee Thomason70d942e2018-06-29 15:31:59 -0700109// A fixed element depth limit is problematic. There needs to be a
110// limit to avoid a stack overflow. However, that limit varies per
111// system, and the capacity of the stack. On the other hand, it's a trivial
112// attack that can result from ill, malicious, or even correctly formed XML,
Lee Thomasonf928c352018-04-05 09:24:20 -0700113// so there needs to be a limit in place.
114static const int TINYXML2_MAX_ELEMENT_DEPTH = 100;
Lee Thomasond946dda2018-04-05 09:11:08 -0700115
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800116namespace tinyxml2
117{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800118class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800119class XMLElement;
120class XMLAttribute;
121class XMLComment;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800122class XMLText;
Lee Thomason50f97b22012-02-11 16:33:40 -0800123class XMLDeclaration;
124class XMLUnknown;
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800125class XMLPrinter;
Lee Thomason5cae8972012-01-24 18:03:07 -0800126
U-Stream\Leeae25a442012-02-17 17:48:16 -0800127/*
128 A class that wraps strings. Normally stores the start and end
129 pointers into the XML file itself, and will apply normalization
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800130 and entity translation if actually read. Can also store (and memory
U-Stream\Leeae25a442012-02-17 17:48:16 -0800131 manage) a traditional char[]
132*/
PKEuS95060352013-07-26 10:42:44 +0200133class StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800134{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800135public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700136 enum {
137 NEEDS_ENTITY_PROCESSING = 0x01,
138 NEEDS_NEWLINE_NORMALIZATION = 0x02,
Dmitry-Me5420e542015-05-20 10:51:26 +0300139 NEEDS_WHITESPACE_COLLAPSING = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800140
Lee Thomason584af572016-09-05 14:14:16 -0700141 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700142 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
Lee Thomason584af572016-09-05 14:14:16 -0700143 ATTRIBUTE_NAME = 0,
144 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
145 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
146 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700147 };
Lee Thomason39ede242012-01-20 11:27:56 -0800148
Lee Thomason120b3a62012-10-12 10:06:59 -0700149 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700150 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800151
Lee Thomason120b3a62012-10-12 10:06:59 -0700152 void Set( char* start, char* end, int flags ) {
Dmitry-Me6fc38ec2016-09-01 17:59:44 +0300153 TIXMLASSERT( start );
154 TIXMLASSERT( end );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700155 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700156 _start = start;
157 _end = end;
158 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700159 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700160
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700161 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700162
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700163 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700164 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700165 }
Lee Thomason39ede242012-01-20 11:27:56 -0800166
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700167 void SetInternedStr( const char* str ) {
168 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700169 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700170 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700171
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700172 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800173
kezenator4f756162016-11-29 19:46:27 +1000174 char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700175 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800176
Lee Thomason29658802014-11-27 22:31:11 -0800177 void TransferTo( StrPair* other );
Lee Thomason584af572016-09-05 14:14:16 -0700178 void Reset();
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300179
Lee Thomason39ede242012-01-20 11:27:56 -0800180private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700181 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800182
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700183 enum {
184 NEEDS_FLUSH = 0x100,
185 NEEDS_DELETE = 0x200
186 };
Lee Thomasone4422302012-01-20 17:59:50 -0800187
Lee Thomason120b3a62012-10-12 10:06:59 -0700188 int _flags;
189 char* _start;
190 char* _end;
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300191
192 StrPair( const StrPair& other ); // not supported
Bo Rydberg65c1b862018-10-19 18:45:53 +0200193 void operator=( const StrPair& other ); // not supported, use TransferTo()
Lee Thomason39ede242012-01-20 11:27:56 -0800194};
195
U-Lama\Lee560bd472011-12-28 19:42:49 -0800196
U-Stream\Leeae25a442012-02-17 17:48:16 -0800197/*
198 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
199 Has a small initial memory pool, so that low or no usage will not
200 cause a call to new/delete
201*/
Dmitry-Me04009222015-04-06 18:07:18 +0300202template <class T, int INITIAL_SIZE>
PKEuS95060352013-07-26 10:42:44 +0200203class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800204{
205public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200206 DynArray() :
207 _mem( _pool ),
208 _allocated( INITIAL_SIZE ),
209 _size( 0 )
210 {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700211 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700212
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700213 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700214 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700215 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700216 }
217 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700218
Lee Thomasonce0510b2013-11-26 21:29:37 -0800219 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100220 _size = 0;
221 }
222
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700223 void Push( T t ) {
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300224 TIXMLASSERT( _size < INT_MAX );
Lee Thomason624d43f2012-10-12 10:58:48 -0700225 EnsureCapacity( _size+1 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300226 _mem[_size] = t;
227 ++_size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700228 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800229
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700230 T* PushArr( int count ) {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300231 TIXMLASSERT( count >= 0 );
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300232 TIXMLASSERT( _size <= INT_MAX - count );
Lee Thomason624d43f2012-10-12 10:58:48 -0700233 EnsureCapacity( _size+count );
234 T* ret = &_mem[_size];
235 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700236 return ret;
237 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700238
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700239 T Pop() {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300240 TIXMLASSERT( _size > 0 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300241 --_size;
242 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 {
Dmitry-Me30bdc972015-01-14 08:32:23 +0300270 TIXMLASSERT( _size >= 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700271 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700272 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700273
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700274 int Capacity() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300275 TIXMLASSERT( _allocated >= INITIAL_SIZE );
Lee Thomason624d43f2012-10-12 10:58:48 -0700276 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700277 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700278
Lee Thomason816d3fa2017-06-05 14:35:55 -0700279 void SwapRemove(int i) {
Dmitry-Mec2f677b2017-06-15 12:44:27 +0300280 TIXMLASSERT(i >= 0 && i < _size);
281 TIXMLASSERT(_size > 0);
Lee Thomason816d3fa2017-06-05 14:35:55 -0700282 _mem[i] = _mem[_size - 1];
283 --_size;
284 }
285
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700286 const T* Mem() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300287 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700288 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700289 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700290
orbitcowboy2cc8a4c2018-09-05 08:46:21 +0200291 T* Mem() {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300292 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700293 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700294 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800295
Lee Thomason2c85a712012-01-31 08:24:24 -0800296private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300297 DynArray( const DynArray& ); // not supported
298 void operator=( const DynArray& ); // not supported
299
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700300 void EnsureCapacity( int cap ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300301 TIXMLASSERT( cap > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700302 if ( cap > _allocated ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300303 TIXMLASSERT( cap <= INT_MAX / 2 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700304 int newAllocated = cap * 2;
305 T* newMem = new T[newAllocated];
Dmitry-Me243ddf52017-05-18 17:27:14 +0300306 TIXMLASSERT( newAllocated >= _size );
Lee Thomason624d43f2012-10-12 10:58:48 -0700307 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
308 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700309 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700310 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700311 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700312 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700313 }
314 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800315
Lee Thomason624d43f2012-10-12 10:58:48 -0700316 T* _mem;
Dmitry-Me04009222015-04-06 18:07:18 +0300317 T _pool[INITIAL_SIZE];
Lee Thomason624d43f2012-10-12 10:58:48 -0700318 int _allocated; // objects allocated
319 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800320};
321
Lee Thomason50adb4c2012-02-13 15:07:09 -0800322
U-Stream\Leeae25a442012-02-17 17:48:16 -0800323/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200324 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800325 and deallocation of objects.
326*/
PKEuS95060352013-07-26 10:42:44 +0200327class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800328{
329public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700330 MemPool() {}
331 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800332
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700333 virtual int ItemSize() const = 0;
334 virtual void* Alloc() = 0;
335 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800336 virtual void SetTracked() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800337};
338
Lee Thomason50adb4c2012-02-13 15:07:09 -0800339
U-Stream\Leeae25a442012-02-17 17:48:16 -0800340/*
341 Template child class to create pools of the correct type.
342*/
Dmitry-Me88145b82016-08-09 17:59:31 +0300343template< int ITEM_SIZE >
PKEuS95060352013-07-26 10:42:44 +0200344class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800345{
346public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200347 MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700348 ~MemPoolT() {
Lee Thomasonc07409b2018-08-25 11:04:13 -0700349 MemPoolT< ITEM_SIZE >::Clear();
Lee Thomasonf07b9522014-10-30 13:25:12 -0700350 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700351
Lee Thomasonf07b9522014-10-30 13:25:12 -0700352 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700353 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700354 while( !_blockPtrs.Empty()) {
Dmitry-Meeffab6f2017-07-26 17:57:50 +0300355 Block* lastBlock = _blockPtrs.Pop();
356 delete lastBlock;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700357 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700358 _root = 0;
359 _currentAllocs = 0;
360 _nAllocs = 0;
361 _maxAllocs = 0;
362 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700363 }
Lee Thomasond1983222012-02-06 08:41:24 -0800364
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700365 virtual int ItemSize() const {
Dmitry-Me88145b82016-08-09 17:59:31 +0300366 return ITEM_SIZE;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700367 }
368 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700369 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700370 }
Lee Thomasond1983222012-02-06 08:41:24 -0800371
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700372 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700373 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700374 // Need a new block.
375 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700376 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800377
Dmitry-Me88145b82016-08-09 17:59:31 +0300378 Item* blockItems = block->items;
379 for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
380 blockItems[i].next = &(blockItems[i + 1]);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700381 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300382 blockItems[ITEMS_PER_BLOCK - 1].next = 0;
383 _root = blockItems;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700384 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300385 Item* const result = _root;
386 TIXMLASSERT( result != 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700387 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800388
Lee Thomason624d43f2012-10-12 10:58:48 -0700389 ++_currentAllocs;
390 if ( _currentAllocs > _maxAllocs ) {
391 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700392 }
Dmitry-Me3161a332016-09-02 16:58:06 +0300393 ++_nAllocs;
394 ++_nUntracked;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700395 return result;
396 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700397
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700398 virtual void Free( void* mem ) {
399 if ( !mem ) {
400 return;
401 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700402 --_currentAllocs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300403 Item* item = static_cast<Item*>( mem );
Peter Matula50689912018-01-09 12:52:26 +0100404#ifdef TINYXML2_DEBUG
Dmitry-Me88145b82016-08-09 17:59:31 +0300405 memset( item, 0xfe, sizeof( *item ) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700406#endif
Dmitry-Me88145b82016-08-09 17:59:31 +0300407 item->next = _root;
408 _root = item;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700409 }
410 void Trace( const char* name ) {
411 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Dmitry-Me88145b82016-08-09 17:59:31 +0300412 name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
413 ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700414 }
Lee Thomasond1983222012-02-06 08:41:24 -0800415
Lee Thomason5b0a6772012-11-19 13:54:42 -0800416 void SetTracked() {
Dmitry-Me3161a332016-09-02 16:58:06 +0300417 --_nUntracked;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800418 }
419
420 int Untracked() const {
421 return _nUntracked;
422 }
423
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800424 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
425 // The test file is large, 170k.
426 // Release: VS2010 gcc(no opt)
427 // 1k: 4000
428 // 2k: 4000
429 // 4k: 3900 21000
430 // 16k: 5200
431 // 32k: 4300
432 // 64k: 4000 21000
Dmitry-Me88145b82016-08-09 17:59:31 +0300433 // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
434 // in private part if ITEMS_PER_BLOCK is private
435 enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
Jerome Martinez7921df12012-10-24 11:45:44 +0200436
Lee Thomasond1983222012-02-06 08:41:24 -0800437private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300438 MemPoolT( const MemPoolT& ); // not supported
439 void operator=( const MemPoolT& ); // not supported
440
Dmitry-Me88145b82016-08-09 17:59:31 +0300441 union Item {
442 Item* next;
443 char itemData[ITEM_SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700444 };
445 struct Block {
Dmitry-Me88145b82016-08-09 17:59:31 +0300446 Item items[ITEMS_PER_BLOCK];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700447 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700448 DynArray< Block*, 10 > _blockPtrs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300449 Item* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800450
Lee Thomason624d43f2012-10-12 10:58:48 -0700451 int _currentAllocs;
452 int _nAllocs;
453 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800454 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800455};
456
Lee Thomason2c85a712012-01-31 08:24:24 -0800457
Lee Thomason56bdd022012-02-09 18:16:58 -0800458
459/**
460 Implements the interface to the "Visitor pattern" (see the Accept() method.)
461 If you call the Accept() method, it requires being passed a XMLVisitor
462 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200463 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800464 are simply called with Visit().
465
466 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700467 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800468
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700469 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800470 visiting). You need to only override methods that are interesting to you.
471
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600472 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800473
474 You should never change the document from a callback.
475
476 @sa XMLNode::Accept()
477*/
PKEuS16ed47d2013-07-06 12:02:43 +0200478class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800479{
480public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700481 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800482
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700483 /// Visit a document.
484 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
485 return true;
486 }
487 /// Visit a document.
488 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
489 return true;
490 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800491
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700492 /// Visit an element.
493 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
494 return true;
495 }
496 /// Visit an element.
497 virtual bool VisitExit( const XMLElement& /*element*/ ) {
498 return true;
499 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800500
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700501 /// Visit a declaration.
502 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
503 return true;
504 }
505 /// Visit a text node.
506 virtual bool Visit( const XMLText& /*text*/ ) {
507 return true;
508 }
509 /// Visit a comment node.
510 virtual bool Visit( const XMLComment& /*comment*/ ) {
511 return true;
512 }
513 /// Visit an unknown node.
514 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
515 return true;
516 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800517};
518
Dmitry-Me66d2a842014-11-08 15:24:52 +0300519// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900520enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900521 XML_SUCCESS = 0,
numatrumpetcd8550c2014-09-08 16:59:39 +0900522 XML_NO_ATTRIBUTE,
523 XML_WRONG_ATTRIBUTE_TYPE,
524 XML_ERROR_FILE_NOT_FOUND,
525 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
526 XML_ERROR_FILE_READ_ERROR,
numatrumpetcd8550c2014-09-08 16:59:39 +0900527 XML_ERROR_PARSING_ELEMENT,
528 XML_ERROR_PARSING_ATTRIBUTE,
numatrumpetcd8550c2014-09-08 16:59:39 +0900529 XML_ERROR_PARSING_TEXT,
530 XML_ERROR_PARSING_CDATA,
531 XML_ERROR_PARSING_COMMENT,
532 XML_ERROR_PARSING_DECLARATION,
533 XML_ERROR_PARSING_UNKNOWN,
534 XML_ERROR_EMPTY_DOCUMENT,
535 XML_ERROR_MISMATCHED_ELEMENT,
536 XML_ERROR_PARSING,
537 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700538 XML_NO_TEXT_NODE,
Lee Thomasond946dda2018-04-05 09:11:08 -0700539 XML_ELEMENT_DEPTH_EXCEEDED,
Lee Thomason331596e2014-09-11 14:56:43 -0700540
541 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900542};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900543
numatrumpetcd8550c2014-09-08 16:59:39 +0900544
U-Stream\Leeae25a442012-02-17 17:48:16 -0800545/*
546 Utility functionality.
547*/
Lee Thomasonce667c92016-12-26 16:45:30 -0800548class TINYXML2_LIB XMLUtil
Lee Thomason56bdd022012-02-09 18:16:58 -0800549{
Lee Thomasond1983222012-02-06 08:41:24 -0800550public:
kezenator4f756162016-11-29 19:46:27 +1000551 static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300552 TIXMLASSERT( p );
Lee Thomasone90e9012016-12-24 07:34:39 -0800553
Dmitry-Mefa20b222014-10-31 12:53:04 +0300554 while( IsWhiteSpace(*p) ) {
Lee Thomasone90e9012016-12-24 07:34:39 -0800555 if (curLineNumPtr && *p == '\n') {
kezenator4f756162016-11-29 19:46:27 +1000556 ++(*curLineNumPtr);
kezenatorec694152016-11-26 17:21:43 +1000557 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700558 ++p;
559 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300560 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700561 return p;
562 }
kezenator4f756162016-11-29 19:46:27 +1000563 static char* SkipWhiteSpace( char* p, int* curLineNumPtr ) {
564 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700565 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300566
567 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
568 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700569 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100570 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700571 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700572
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200573 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300574 if ( ch >= 128 ) {
575 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
576 return true;
577 }
578 if ( isalpha( ch ) ) {
579 return true;
580 }
581 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200582 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700583
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200584 inline static bool IsNameChar( unsigned char ch ) {
585 return IsNameStartChar( ch )
586 || isdigit( ch )
587 || ch == '.'
588 || ch == '-';
589 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800590
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700591 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700592 if ( p == q ) {
593 return true;
594 }
Dmitry-Me21f99692016-10-03 13:01:57 +0300595 TIXMLASSERT( p );
596 TIXMLASSERT( q );
597 TIXMLASSERT( nChar >= 0 );
Lee Thomason598a88d2015-10-09 14:42:12 -0700598 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700599 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700600
Dmitry-Me7865aad2015-06-19 16:23:35 +0300601 inline static bool IsUTF8Continuation( char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400602 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700603 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800604
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700605 static const char* ReadBOM( const char* p, bool* hasBOM );
606 // p is the starting location,
607 // the UTF-8 value of the entity will be placed in value, and length filled in.
608 static const char* GetCharacterRef( const char* p, char* value, int* length );
609 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700610
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700611 // converts primitive types to strings
612 static void ToStr( int v, char* buffer, int bufferSize );
613 static void ToStr( unsigned v, char* buffer, int bufferSize );
614 static void ToStr( bool v, char* buffer, int bufferSize );
615 static void ToStr( float v, char* buffer, int bufferSize );
616 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason51c12712016-06-04 20:18:49 -0700617 static void ToStr(int64_t v, char* buffer, int bufferSize);
Lee Thomason21be8822012-07-15 17:27:22 -0700618
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700619 // converts strings to primitive types
620 static bool ToInt( const char* str, int* value );
621 static bool ToUnsigned( const char* str, unsigned* value );
622 static bool ToBool( const char* str, bool* value );
623 static bool ToFloat( const char* str, float* value );
624 static bool ToDouble( const char* str, double* value );
Lee Thomason51c12712016-06-04 20:18:49 -0700625 static bool ToInt64(const char* str, int64_t* value);
Lee Thomasonce667c92016-12-26 16:45:30 -0800626
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800627 // Changes what is serialized for a boolean value.
628 // Default to "true" and "false". Shouldn't be changed
629 // unless you have a special testing or compatibility need.
Lee Thomasonce667c92016-12-26 16:45:30 -0800630 // Be careful: static, global, & not thread safe.
631 // Be sure to set static const memory as parameters.
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800632 static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
Lee Thomasonce667c92016-12-26 16:45:30 -0800633
634private:
Lee Thomasonf458d262016-12-26 22:47:25 -0800635 static const char* writeBoolTrue;
636 static const char* writeBoolFalse;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800637};
638
Lee Thomason5cae8972012-01-24 18:03:07 -0800639
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800640/** XMLNode is a base class for every object that is in the
641 XML Document Object Model (DOM), except XMLAttributes.
642 Nodes have siblings, a parent, and children which can
643 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700644 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800645 be cast to its more defined type.
646
Thomas Roß08bdf502012-05-12 14:21:23 +0200647 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800648 When the XMLDocument gets deleted, all its Nodes
649 will also be deleted.
650
651 @verbatim
652 A Document can contain: Element (container or leaf)
653 Comment (leaf)
654 Unknown (leaf)
655 Declaration( leaf )
656
657 An Element can contain: Element (container or leaf)
658 Text (leaf)
659 Attributes (not on tree)
660 Comment (leaf)
661 Unknown (leaf)
662
663 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800664*/
PKEuS16ed47d2013-07-06 12:02:43 +0200665class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800666{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700667 friend class XMLDocument;
668 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800669public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800670
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700671 /// Get the XMLDocument that owns this XMLNode.
672 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300673 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700674 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700675 }
676 /// Get the XMLDocument that owns this XMLNode.
677 XMLDocument* GetDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300678 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700679 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700680 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800681
Lee Thomason2fa81722012-11-09 12:37:46 -0800682 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700683 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100684 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700685 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800686 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700687 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100688 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700689 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800690 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700691 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100692 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700693 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800694 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700695 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100696 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700697 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800698 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700699 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100700 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700701 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800702 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700703 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100704 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700705 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800706
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700707 virtual const XMLElement* ToElement() const {
708 return 0;
709 }
710 virtual const XMLText* ToText() const {
711 return 0;
712 }
713 virtual const XMLComment* ToComment() const {
714 return 0;
715 }
716 virtual const XMLDocument* ToDocument() const {
717 return 0;
718 }
719 virtual const XMLDeclaration* ToDeclaration() const {
720 return 0;
721 }
722 virtual const XMLUnknown* ToUnknown() const {
723 return 0;
724 }
Lee Thomason751da522012-02-10 08:50:51 -0800725
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700726 /** The meaning of 'value' changes for the specific type.
727 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530728 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700729 Element: name of the element
730 Comment: the comment text
731 Unknown: the tag contents
732 Text: the text string
733 @endverbatim
734 */
Michael Daumling21626882013-10-22 17:03:37 +0200735 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100736
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700737 /** Set the Value of an XML node.
738 @sa Value()
739 */
740 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800741
kezenatorec694152016-11-26 17:21:43 +1000742 /// Gets the line number the node is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +1000743 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +1000744
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700745 /// Get the parent of this node on the DOM.
746 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700747 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700748 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100749
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700750 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700751 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700752 }
Lee Thomason751da522012-02-10 08:50:51 -0800753
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700754 /// Returns true if this node has no children.
755 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700756 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700757 }
Lee Thomason751da522012-02-10 08:50:51 -0800758
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700759 /// Get the first child node, or null if none exists.
760 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700761 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700762 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100763
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700764 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700765 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700766 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100767
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700768 /** Get the first child element, or optionally the first child
769 element with the specified name.
770 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300771 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700772
Dmitry-Me886ad972015-07-22 11:00:51 +0300773 XMLElement* FirstChildElement( const char* name = 0 ) {
774 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700775 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800776
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700777 /// Get the last child node, or null if none exists.
778 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700779 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700780 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700781
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700782 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300783 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700784 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800785
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700786 /** Get the last child element or optionally the last child
787 element with the specified name.
788 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300789 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700790
Dmitry-Me886ad972015-07-22 11:00:51 +0300791 XMLElement* LastChildElement( const char* name = 0 ) {
792 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700793 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700794
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700795 /// Get the previous (left) sibling node of this node.
796 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700797 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700798 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700799
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700800 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700801 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700802 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800803
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700804 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300805 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700806
Dmitry-Me886ad972015-07-22 11:00:51 +0300807 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
808 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700809 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700810
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700811 /// Get the next (right) sibling node of this node.
812 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700813 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700814 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700815
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700816 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700817 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700818 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700819
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700820 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300821 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700822
Dmitry-Me886ad972015-07-22 11:00:51 +0300823 XMLElement* NextSiblingElement( const char* name = 0 ) {
824 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700825 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800826
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700827 /**
828 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200829 If the child node is already part of the document,
830 it is moved from its old location to the new location.
831 Returns the addThis argument or 0 if the node does not
832 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700833 */
834 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800835
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700836 XMLNode* LinkEndChild( XMLNode* addThis ) {
837 return InsertEndChild( addThis );
838 }
839 /**
840 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200841 If the child node is already part of the document,
842 it is moved from its old location to the new location.
843 Returns the addThis argument or 0 if the node does not
844 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700845 */
846 XMLNode* InsertFirstChild( XMLNode* addThis );
847 /**
848 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200849 If the child node is already part of the document,
850 it is moved from its old location to the new location.
851 Returns the addThis argument or 0 if the afterThis node
852 is not a child of this node, or if the node does not
853 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700854 */
855 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700856
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700857 /**
858 Delete all the children of this node.
859 */
860 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800861
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700862 /**
863 Delete a child of this node.
864 */
865 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800866
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700867 /**
868 Make a copy of this node, but not its children.
869 You may pass in a Document pointer that will be
870 the owner of the new Node. If the 'document' is
871 null, then the node returned will be allocated
872 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800873
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700874 Note: if called on a XMLDocument, this will return null.
875 */
876 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800877
Lee Thomason7085f002017-06-01 18:09:43 -0700878 /**
Lee Thomason1346a172017-06-14 15:14:19 -0700879 Make a copy of this node and all its children.
Lee Thomason7085f002017-06-01 18:09:43 -0700880
Dmitry-Me3f63f212017-06-19 18:25:19 +0300881 If the 'target' is null, then the nodes will
Lee Thomason70d942e2018-06-29 15:31:59 -0700882 be allocated in the current document. If 'target'
883 is specified, the memory will be allocated is the
Lee Thomason1346a172017-06-14 15:14:19 -0700884 specified XMLDocument.
Lee Thomason7085f002017-06-01 18:09:43 -0700885
Lee Thomason70d942e2018-06-29 15:31:59 -0700886 NOTE: This is probably not the correct tool to
Lee Thomason7085f002017-06-01 18:09:43 -0700887 copy a document, since XMLDocuments can have multiple
Lee Thomason1346a172017-06-14 15:14:19 -0700888 top level XMLNodes. You probably want to use
889 XMLDocument::DeepCopy()
Lee Thomason7085f002017-06-01 18:09:43 -0700890 */
Dmitry-Me3f63f212017-06-19 18:25:19 +0300891 XMLNode* DeepClone( XMLDocument* target ) const;
Lee Thomason7085f002017-06-01 18:09:43 -0700892
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700893 /**
894 Test if 2 nodes are the same, but don't test children.
895 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800896
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700897 Note: if called on a XMLDocument, this will return false.
898 */
899 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800900
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600901 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700902 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600903 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800904
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600905 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
906 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700907 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800908
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700909 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800910
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700911 - http://www.saxproject.org/
912 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800913
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700914 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800915
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700916 An example of using Accept():
917 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600918 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700919 tinyxmlDoc.Accept( &printer );
920 const char* xmlcstr = printer.CStr();
921 @endverbatim
922 */
923 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800924
Lee Thomason70d942e2018-06-29 15:31:59 -0700925 /**
926 Set user data into the XMLNode. TinyXML-2 in
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700927 no way processes or interprets user data.
928 It is initially 0.
929 */
930 void SetUserData(void* userData) { _userData = userData; }
931
932 /**
933 Get user data set into the XMLNode. TinyXML-2 in
934 no way processes or interprets user data.
935 It is initially 0.
936 */
937 void* GetUserData() const { return _userData; }
938
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800939protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +0200940 explicit XMLNode( XMLDocument* );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700941 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700942
Dmitry-Me10b8ecc2017-04-12 17:57:44 +0300943 virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300944
Lee Thomason624d43f2012-10-12 10:58:48 -0700945 XMLDocument* _document;
946 XMLNode* _parent;
947 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +1000948 int _parseLineNum;
Lee Thomason3f57d272012-01-11 15:30:03 -0800949
Lee Thomason624d43f2012-10-12 10:58:48 -0700950 XMLNode* _firstChild;
951 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800952
Lee Thomason624d43f2012-10-12 10:58:48 -0700953 XMLNode* _prev;
954 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800955
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700956 void* _userData;
957
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800958private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700959 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700960 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400961 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800962 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Meecb9b072016-10-12 16:44:59 +0300963 const XMLElement* ToElementWithName( const char* name ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300964
965 XMLNode( const XMLNode& ); // not supported
966 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800967};
968
969
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800970/** XML text.
971
972 Note that a text node can have child element nodes, for example:
973 @verbatim
974 <root>This is <b>bold</b></root>
975 @endverbatim
976
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700977 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800978 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 -0700979 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600980 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800981*/
PKEuS16ed47d2013-07-06 12:02:43 +0200982class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800983{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700984 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800985public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700986 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800987
Lee Thomason624d43f2012-10-12 10:58:48 -0700988 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700989 return this;
990 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700991 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700992 return this;
993 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800994
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700995 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700996 void SetCData( bool isCData ) {
997 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700998 }
999 /// Returns true if this is a CDATA text element.
1000 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001001 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001002 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001003
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001004 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1005 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001006
Lee Thomason5492a1c2012-01-23 15:32:10 -08001007protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001008 explicit XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001009 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -08001010
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001011 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001012
Lee Thomason5492a1c2012-01-23 15:32:10 -08001013private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001014 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +03001015
1016 XMLText( const XMLText& ); // not supported
1017 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -08001018};
1019
1020
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001021/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +02001022class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001023{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001024 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -08001025public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001026 virtual XMLComment* ToComment() {
1027 return this;
1028 }
1029 virtual const XMLComment* ToComment() const {
1030 return this;
1031 }
Lee Thomasonce0763e2012-01-11 15:43:54 -08001032
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001033 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001034
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001035 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1036 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -08001037
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001038protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001039 explicit XMLComment( XMLDocument* doc );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001040 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001041
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001042 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001043
Lee Thomason3f57d272012-01-11 15:30:03 -08001044private:
Dmitry-Mef547a992015-01-09 15:17:09 +03001045 XMLComment( const XMLComment& ); // not supported
1046 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001047};
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001048
1049
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001050/** In correct XML the declaration is the first entry in the file.
1051 @verbatim
1052 <?xml version="1.0" standalone="yes"?>
1053 @endverbatim
1054
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001055 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001056 however.
1057
1058 The text of the declaration isn't interpreted. It is parsed
1059 and written as a string.
1060*/
PKEuS16ed47d2013-07-06 12:02:43 +02001061class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001062{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001063 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001064public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001065 virtual XMLDeclaration* ToDeclaration() {
1066 return this;
1067 }
1068 virtual const XMLDeclaration* ToDeclaration() const {
1069 return this;
1070 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001071
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001072 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001073
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001074 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1075 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001076
1077protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001078 explicit XMLDeclaration( XMLDocument* doc );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001079 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +03001080
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001081 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001082
Dmitry-Mef547a992015-01-09 15:17:09 +03001083private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001084 XMLDeclaration( const XMLDeclaration& ); // not supported
1085 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001086};
1087
1088
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001089/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001090 unknown. It is a tag of text, but should not be modified.
1091 It will be written back to the XML, unchanged, when the file
1092 is saved.
1093
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001094 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001095*/
PKEuS16ed47d2013-07-06 12:02:43 +02001096class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001097{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001098 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001099public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001100 virtual XMLUnknown* ToUnknown() {
1101 return this;
1102 }
1103 virtual const XMLUnknown* ToUnknown() const {
1104 return this;
1105 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001106
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001107 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001108
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001109 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1110 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001111
1112protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001113 explicit XMLUnknown( XMLDocument* doc );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001114 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001115
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001116 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001117
Dmitry-Mef547a992015-01-09 15:17:09 +03001118private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001119 XMLUnknown( const XMLUnknown& ); // not supported
1120 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001121};
1122
1123
Lee Thomason1ff38e02012-02-14 18:18:16 -08001124
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001125/** An attribute is a name-value pair. Elements have an arbitrary
1126 number of attributes, each with a unique name.
1127
1128 @note The attributes are not XMLNodes. You may only query the
1129 Next() attribute in a list.
1130*/
PKEuS16ed47d2013-07-06 12:02:43 +02001131class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001132{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001133 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001134public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001135 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001136 const char* Name() const;
1137
Lee Thomason2fa81722012-11-09 12:37:46 -08001138 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001139 const char* Value() const;
1140
kezenatorec694152016-11-26 17:21:43 +10001141 /// Gets the line number the attribute is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +10001142 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +10001143
Lee Thomason2fa81722012-11-09 12:37:46 -08001144 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001145 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001146 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001147 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001148
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001149 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001150 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001151 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001152 */
Lee Thomason51c12712016-06-04 20:18:49 -07001153 int IntValue() const {
1154 int i = 0;
1155 QueryIntValue(&i);
1156 return i;
1157 }
1158
1159 int64_t Int64Value() const {
1160 int64_t i = 0;
1161 QueryInt64Value(&i);
1162 return i;
1163 }
1164
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001165 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001166 unsigned UnsignedValue() const {
1167 unsigned i=0;
1168 QueryUnsignedValue( &i );
1169 return i;
1170 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001171 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001172 bool BoolValue() const {
1173 bool b=false;
1174 QueryBoolValue( &b );
1175 return b;
1176 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001177 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001178 double DoubleValue() const {
1179 double d=0;
1180 QueryDoubleValue( &d );
1181 return d;
1182 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001183 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001184 float FloatValue() const {
1185 float f=0;
1186 QueryFloatValue( &f );
1187 return f;
1188 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001189
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001190 /** QueryIntValue interprets the attribute as an integer, and returns the value
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001191 in the provided parameter. The function will return XML_SUCCESS on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001192 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1193 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001194 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001195 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001196 XMLError QueryUnsignedValue( unsigned int* value ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001197 /// See QueryIntValue
1198 XMLError QueryInt64Value(int64_t* value) const;
1199 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001200 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001201 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001202 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001203 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001204 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001205
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001206 /// Set the attribute to a string value.
1207 void SetAttribute( const char* value );
1208 /// Set the attribute to value.
1209 void SetAttribute( int value );
1210 /// Set the attribute to value.
1211 void SetAttribute( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001212 /// Set the attribute to value.
1213 void SetAttribute(int64_t value);
1214 /// Set the attribute to value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001215 void SetAttribute( bool value );
1216 /// Set the attribute to value.
1217 void SetAttribute( double value );
1218 /// Set the attribute to value.
1219 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001220
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001221private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001222 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001223
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001224 XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001225 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001226
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001227 XMLAttribute( const XMLAttribute& ); // not supported
1228 void operator=( const XMLAttribute& ); // not supported
1229 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001230
kezenator4f756162016-11-29 19:46:27 +10001231 char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001232
Lee Thomason624d43f2012-10-12 10:58:48 -07001233 mutable StrPair _name;
1234 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +10001235 int _parseLineNum;
Lee Thomason624d43f2012-10-12 10:58:48 -07001236 XMLAttribute* _next;
1237 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001238};
1239
1240
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001241/** The element is a container class. It has a value, the element name,
1242 and can contain other elements, text, comments, and unknowns.
1243 Elements also contain an arbitrary number of attributes.
1244*/
PKEuS16ed47d2013-07-06 12:02:43 +02001245class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001246{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001247 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001248public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001249 /// Get the name of an element (which is the Value() of the node.)
1250 const char* Name() const {
1251 return Value();
1252 }
1253 /// Set the name of the element.
1254 void SetName( const char* str, bool staticMem=false ) {
1255 SetValue( str, staticMem );
1256 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001257
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001258 virtual XMLElement* ToElement() {
1259 return this;
1260 }
1261 virtual const XMLElement* ToElement() const {
1262 return this;
1263 }
1264 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001265
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001266 /** Given an attribute name, Attribute() returns the value
1267 for the attribute of that name, or null if none
1268 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001269
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001270 @verbatim
1271 const char* value = ele->Attribute( "foo" );
1272 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001273
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001274 The 'value' parameter is normally null. However, if specified,
1275 the attribute will only be returned if the 'name' and 'value'
1276 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001277
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001278 @verbatim
1279 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1280 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001281
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001282 rather than:
1283 @verbatim
1284 if ( ele->Attribute( "foo" ) ) {
1285 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1286 }
1287 @endverbatim
1288 */
1289 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001290
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001291 /** Given an attribute name, IntAttribute() returns the value
Lee Thomason13cbc9a2016-10-27 14:55:07 -07001292 of the attribute interpreted as an integer. The default
1293 value will be returned if the attribute isn't present,
1294 or if there is an error. (For a method with error
1295 checking, see QueryIntAttribute()).
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001296 */
Josh Wittnercf3dd092016-10-11 18:57:17 -07001297 int IntAttribute(const char* name, int defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001298 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001299 unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001300 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001301 int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001302 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001303 bool BoolAttribute(const char* name, bool defaultValue = false) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001304 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001305 double DoubleAttribute(const char* name, double defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001306 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001307 float FloatAttribute(const char* name, float defaultValue = 0) const;
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001308
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001309 /** Given an attribute name, QueryIntAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001310 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001311 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1312 doesn't exist. If successful, the result of the conversion
1313 will be written to 'value'. If not successful, nothing will
1314 be written to 'value'. This allows you to provide default
1315 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001316
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001317 @verbatim
1318 int value = 10;
1319 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1320 @endverbatim
1321 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001322 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001323 const XMLAttribute* a = FindAttribute( name );
1324 if ( !a ) {
1325 return XML_NO_ATTRIBUTE;
1326 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001327 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001328 }
Lee Thomason51c12712016-06-04 20:18:49 -07001329
1330 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001331 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001332 const XMLAttribute* a = FindAttribute( name );
1333 if ( !a ) {
1334 return XML_NO_ATTRIBUTE;
1335 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001336 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001337 }
Lee Thomason51c12712016-06-04 20:18:49 -07001338
1339 /// See QueryIntAttribute()
1340 XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1341 const XMLAttribute* a = FindAttribute(name);
1342 if (!a) {
1343 return XML_NO_ATTRIBUTE;
1344 }
1345 return a->QueryInt64Value(value);
1346 }
1347
1348 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001349 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001350 const XMLAttribute* a = FindAttribute( name );
1351 if ( !a ) {
1352 return XML_NO_ATTRIBUTE;
1353 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001354 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001355 }
1356 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001357 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001358 const XMLAttribute* a = FindAttribute( name );
1359 if ( !a ) {
1360 return XML_NO_ATTRIBUTE;
1361 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001362 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001363 }
1364 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001365 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001366 const XMLAttribute* a = FindAttribute( name );
1367 if ( !a ) {
1368 return XML_NO_ATTRIBUTE;
1369 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001370 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001371 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001372
Lee Thomason5b00e062017-12-28 14:07:47 -08001373 /// See QueryIntAttribute()
1374 XMLError QueryStringAttribute(const char* name, const char** value) const {
1375 const XMLAttribute* a = FindAttribute(name);
1376 if (!a) {
1377 return XML_NO_ATTRIBUTE;
1378 }
1379 *value = a->Value();
1380 return XML_SUCCESS;
1381 }
1382
1383
Lee Thomason70d942e2018-06-29 15:31:59 -07001384
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001385 /** Given an attribute name, QueryAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001386 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001387 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1388 doesn't exist. It is overloaded for the primitive types,
1389 and is a generally more convenient replacement of
1390 QueryIntAttribute() and related functions.
Lee Thomason70d942e2018-06-29 15:31:59 -07001391
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001392 If successful, the result of the conversion
1393 will be written to 'value'. If not successful, nothing will
1394 be written to 'value'. This allows you to provide default
1395 value:
1396
1397 @verbatim
1398 int value = 10;
1399 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1400 @endverbatim
1401 */
Lee Thomasonfc80df32018-06-29 15:37:29 -07001402 XMLError QueryAttribute( const char* name, int* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001403 return QueryIntAttribute( name, value );
1404 }
1405
Lee Thomasonfc80df32018-06-29 15:37:29 -07001406 XMLError QueryAttribute( const char* name, unsigned int* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001407 return QueryUnsignedAttribute( name, value );
1408 }
1409
Lee Thomasonfc80df32018-06-29 15:37:29 -07001410 XMLError QueryAttribute(const char* name, int64_t* value) const {
Lee Thomason51c12712016-06-04 20:18:49 -07001411 return QueryInt64Attribute(name, value);
1412 }
1413
Lee Thomasonfc80df32018-06-29 15:37:29 -07001414 XMLError QueryAttribute( const char* name, bool* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001415 return QueryBoolAttribute( name, value );
1416 }
1417
Lee Thomasonfc80df32018-06-29 15:37:29 -07001418 XMLError QueryAttribute( const char* name, double* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001419 return QueryDoubleAttribute( name, value );
1420 }
1421
Lee Thomasonfc80df32018-06-29 15:37:29 -07001422 XMLError QueryAttribute( const char* name, float* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001423 return QueryFloatAttribute( name, value );
1424 }
1425
1426 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001427 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001428 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001429 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001430 }
1431 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001432 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001433 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001434 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001435 }
1436 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001437 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001438 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001439 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001440 }
Lee Thomason51c12712016-06-04 20:18:49 -07001441
1442 /// Sets the named attribute to value.
1443 void SetAttribute(const char* name, int64_t value) {
1444 XMLAttribute* a = FindOrCreateAttribute(name);
1445 a->SetAttribute(value);
1446 }
1447
1448 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001449 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001450 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001451 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001452 }
1453 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001454 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001455 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001456 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001457 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001458 /// Sets the named attribute to value.
1459 void SetAttribute( const char* name, float value ) {
1460 XMLAttribute* a = FindOrCreateAttribute( name );
1461 a->SetAttribute( value );
1462 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001463
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001464 /**
1465 Delete an attribute.
1466 */
1467 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001468
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001469 /// Return the first attribute in the list.
1470 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001471 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001472 }
1473 /// Query a specific attribute in the list.
1474 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001475
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001476 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001477 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001478 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001479
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001480 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001481 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001482
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001483 This is a convenient method for getting the text of simple contained text:
1484 @verbatim
1485 <foo>This is text</foo>
1486 const char* str = fooElement->GetText();
1487 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001488
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001489 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001490
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001491 Note that this function can be misleading. If the element foo was created from
1492 this XML:
1493 @verbatim
1494 <foo><b>This is text</b></foo>
1495 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001496
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001497 then the value of str would be null. The first child node isn't a text node, it is
1498 another element. From this XML:
1499 @verbatim
1500 <foo>This is <b>text</b></foo>
1501 @endverbatim
1502 GetText() will return "This is ".
1503 */
1504 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001505
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001506 /** Convenience function for easy access to the text inside an element. Although easy
1507 and concise, SetText() is limited compared to creating an XMLText child
1508 and mutating it directly.
1509
1510 If the first child of 'this' is a XMLText, SetText() sets its value to
1511 the given string, otherwise it will create a first child that is an XMLText.
1512
1513 This is a convenient method for setting the text of simple contained text:
1514 @verbatim
1515 <foo>This is text</foo>
1516 fooElement->SetText( "Hullaballoo!" );
1517 <foo>Hullaballoo!</foo>
1518 @endverbatim
1519
1520 Note that this function can be misleading. If the element foo was created from
1521 this XML:
1522 @verbatim
1523 <foo><b>This is text</b></foo>
1524 @endverbatim
1525
1526 then it will not change "This is text", but rather prefix it with a text element:
1527 @verbatim
1528 <foo>Hullaballoo!<b>This is text</b></foo>
1529 @endverbatim
Lee Thomason70d942e2018-06-29 15:31:59 -07001530
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001531 For this XML:
1532 @verbatim
1533 <foo />
1534 @endverbatim
1535 SetText() will generate
1536 @verbatim
1537 <foo>Hullaballoo!</foo>
1538 @endverbatim
1539 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001540 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001541 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001542 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001543 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001544 void SetText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001545 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1546 void SetText(int64_t value);
1547 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001548 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001549 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001550 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001551 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001552 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001553
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001554 /**
1555 Convenience method to query the value of a child text node. This is probably best
1556 shown by example. Given you have a document is this form:
1557 @verbatim
1558 <point>
1559 <x>1</x>
1560 <y>1.4</y>
1561 </point>
1562 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001563
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001564 The QueryIntText() and similar functions provide a safe and easier way to get to the
1565 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001566
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001567 @verbatim
1568 int x = 0;
1569 float y = 0; // types of x and y are contrived for example
1570 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1571 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1572 xElement->QueryIntText( &x );
1573 yElement->QueryFloatText( &y );
1574 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001575
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001576 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1577 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 -07001578
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001579 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001580 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001581 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001582 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001583 /// See QueryIntText()
1584 XMLError QueryInt64Text(int64_t* uval) const;
1585 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001586 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001587 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001588 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001589 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001590 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001591
Josh Wittnercf3dd092016-10-11 18:57:17 -07001592 int IntText(int defaultValue = 0) const;
1593
Josh Wittner3a621f52016-09-12 19:17:54 -07001594 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001595 unsigned UnsignedText(unsigned defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001596 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001597 int64_t Int64Text(int64_t defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001598 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001599 bool BoolText(bool defaultValue = false) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001600 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001601 double DoubleText(double defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001602 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001603 float FloatText(float defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001604
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001605 // internal:
Dmitry-Mee5035632017-04-05 18:02:40 +03001606 enum ElementClosingType {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001607 OPEN, // <foo>
1608 CLOSED, // <foo/>
1609 CLOSING // </foo>
1610 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001611 ElementClosingType ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001612 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001613 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001614 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1615 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001616
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001617protected:
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001618 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001619
Lee Thomason50adb4c2012-02-13 15:07:09 -08001620private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001621 XMLElement( XMLDocument* doc );
1622 virtual ~XMLElement();
1623 XMLElement( const XMLElement& ); // not supported
1624 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001625
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001626 XMLAttribute* FindOrCreateAttribute( const char* name );
kezenator4f756162016-11-29 19:46:27 +10001627 char* ParseAttributes( char* p, int* curLineNumPtr );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001628 static void DeleteAttribute( XMLAttribute* attribute );
Dmitry-Mea60caa22016-11-22 18:28:08 +03001629 XMLAttribute* CreateAttribute();
Lee Thomason67d61312012-01-24 16:01:51 -08001630
Lee Thomason5bb2d802014-01-24 10:42:57 -08001631 enum { BUF_SIZE = 200 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001632 ElementClosingType _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001633 // The attribute list is ordered; there is no 'lastAttribute'
1634 // because the list needs to be scanned for dupes before adding
1635 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001636 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001637};
1638
1639
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001640enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001641 PRESERVE_WHITESPACE,
1642 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001643};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001644
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001645
1646/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001647 It can be saved, loaded, and printed to the screen.
1648 All Nodes are connected and allocated to a Document.
1649 If the Document is deleted, all its Nodes are also deleted.
1650*/
PKEuS16ed47d2013-07-06 12:02:43 +02001651class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001652{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001653 friend class XMLElement;
Lee Thomasond946dda2018-04-05 09:11:08 -07001654 // Gives access to SetError and Push/PopDepth, but over-access for everything else.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001655 // Wishing C++ had "internal" scope.
Lee Thomason70d942e2018-06-29 15:31:59 -07001656 friend class XMLNode;
Lee Thomasonf49b9652017-10-11 10:57:49 -07001657 friend class XMLText;
1658 friend class XMLComment;
1659 friend class XMLDeclaration;
1660 friend class XMLUnknown;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001661public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001662 /// constructor
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001663 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001664 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001665
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001666 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001667 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001668 return this;
1669 }
1670 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001671 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001672 return this;
1673 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001674
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001675 /**
1676 Parse an XML file from a character string.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001677 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001678 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001679
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001680 You may optionally pass in the 'nBytes', which is
1681 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001682 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001683 null terminated string.
1684 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001685 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001686
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001687 /**
1688 Load an XML file from disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001689 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001690 an errorID.
1691 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001692 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001693
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001694 /**
1695 Load an XML file from disk. You are responsible
Lee Thomason70d942e2018-06-29 15:31:59 -07001696 for providing and closing the FILE*.
1697
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001698 NOTE: The file should be opened as binary ("rb")
1699 not text in order for TinyXML-2 to correctly
1700 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001701
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001702 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001703 an errorID.
1704 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001705 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001706
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001707 /**
1708 Save the XML file to disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001709 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001710 an errorID.
1711 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001712 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001713
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001714 /**
1715 Save the XML file to disk. You are responsible
1716 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001717
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001718 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001719 an errorID.
1720 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001721 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001722
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001723 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001724 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001725 }
1726 Whitespace WhitespaceMode() const {
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001727 return _whitespaceMode;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001728 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001729
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001730 /**
1731 Returns true if this document has a leading Byte Order Mark of UTF8.
1732 */
1733 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001734 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001735 }
1736 /** Sets whether to write the BOM when writing the file.
1737 */
1738 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001739 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001740 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001741
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001742 /** Return the root element of DOM. Equivalent to FirstChildElement().
1743 To get the first node, use FirstChild().
1744 */
1745 XMLElement* RootElement() {
1746 return FirstChildElement();
1747 }
1748 const XMLElement* RootElement() const {
1749 return FirstChildElement();
1750 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001751
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001752 /** Print the Document. If the Printer is not provided, it will
1753 print to stdout. If you provide Printer, this can print to a file:
1754 @verbatim
1755 XMLPrinter printer( fp );
1756 doc.Print( &printer );
1757 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001758
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001759 Or you can use a printer to print to memory:
1760 @verbatim
1761 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001762 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001763 // printer.CStr() has a const char* to the XML
1764 @endverbatim
1765 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001766 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001767 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001768
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001769 /**
1770 Create a new Element associated with
1771 this Document. The memory for the Element
1772 is managed by the Document.
1773 */
1774 XMLElement* NewElement( const char* name );
1775 /**
1776 Create a new Comment associated with
1777 this Document. The memory for the Comment
1778 is managed by the Document.
1779 */
1780 XMLComment* NewComment( const char* comment );
1781 /**
1782 Create a new Text associated with
1783 this Document. The memory for the Text
1784 is managed by the Document.
1785 */
1786 XMLText* NewText( const char* text );
1787 /**
1788 Create a new Declaration associated with
1789 this Document. The memory for the object
1790 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001791
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001792 If the 'text' param is null, the standard
1793 declaration is used.:
1794 @verbatim
1795 <?xml version="1.0" encoding="UTF-8"?>
1796 @endverbatim
1797 */
1798 XMLDeclaration* NewDeclaration( const char* text=0 );
1799 /**
1800 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001801 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001802 is managed by the Document.
1803 */
1804 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001805
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001806 /**
1807 Delete a node associated with this document.
1808 It will be unlinked from the DOM.
1809 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001810 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001811
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001812 void ClearError() {
Lee Thomasonaa188392017-09-19 17:54:31 -07001813 SetError(XML_SUCCESS, 0, 0);
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001814 }
1815
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001816 /// Return true if there was an error parsing the document.
1817 bool Error() const {
Lee Thomason85536252016-06-04 19:10:53 -07001818 return _errorID != XML_SUCCESS;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001819 }
1820 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001821 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001822 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001823 }
Lee Thomason331596e2014-09-11 14:56:43 -07001824 const char* ErrorName() const;
Lee Thomasone90e9012016-12-24 07:34:39 -08001825 static const char* ErrorIDToName(XMLError errorID);
Lee Thomason331596e2014-09-11 14:56:43 -07001826
Lee Thomason70d942e2018-06-29 15:31:59 -07001827 /** Returns a "long form" error description. A hopefully helpful
Lee Thomasonf49b9652017-10-11 10:57:49 -07001828 diagnostic with location, line number, and/or additional info.
1829 */
1830 const char* ErrorStr() const;
1831
1832 /// A (trivial) utility function that prints the ErrorStr() to stdout.
1833 void PrintError() const;
Lee Thomason8c9e3132017-06-26 16:55:01 -07001834
orbitcowboy22b21ec2018-07-17 11:52:57 +02001835 /// Return the line where the error occurred, or zero if unknown.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001836 int ErrorLineNum() const
kezenatorec694152016-11-26 17:21:43 +10001837 {
1838 return _errorLineNum;
1839 }
Lee Thomason70d942e2018-06-29 15:31:59 -07001840
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001841 /// Clear the document, resetting it to the initial state.
1842 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001843
Lee Thomason7085f002017-06-01 18:09:43 -07001844 /**
Lee Thomasonb29f5562017-06-01 18:45:32 -07001845 Copies this document to a target document.
Lee Thomason7085f002017-06-01 18:09:43 -07001846 The target will be completely cleared before the copy.
Lee Thomasonb29f5562017-06-01 18:45:32 -07001847 If you want to copy a sub-tree, see XMLNode::DeepClone().
Lee Thomason7085f002017-06-01 18:09:43 -07001848
Lee Thomason1346a172017-06-14 15:14:19 -07001849 NOTE: that the 'target' must be non-null.
Lee Thomason7085f002017-06-01 18:09:43 -07001850 */
Shuichiro Suzuki87cd4e02017-08-24 11:20:26 +09001851 void DeepCopy(XMLDocument* target) const;
Lee Thomason7085f002017-06-01 18:09:43 -07001852
1853 // internal
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001854 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001855
Lee Thomason816d3fa2017-06-05 14:35:55 -07001856 // internal
1857 void MarkInUse(XMLNode*);
1858
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001859 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1860 return 0;
1861 }
1862 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1863 return false;
1864 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001865
Lee Thomason3f57d272012-01-11 15:30:03 -08001866private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001867 XMLDocument( const XMLDocument& ); // not supported
1868 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001869
Lee Thomasone90e9012016-12-24 07:34:39 -08001870 bool _writeBOM;
1871 bool _processEntities;
1872 XMLError _errorID;
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001873 Whitespace _whitespaceMode;
Lee Thomasonaa188392017-09-19 17:54:31 -07001874 mutable StrPair _errorStr;
Lee Thomasone90e9012016-12-24 07:34:39 -08001875 int _errorLineNum;
1876 char* _charBuffer;
1877 int _parseCurLineNum;
Lee Thomasond946dda2018-04-05 09:11:08 -07001878 int _parsingDepth;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001879 // Memory tracking does add some overhead.
1880 // However, the code assumes that you don't
1881 // have a bunch of unlinked nodes around.
1882 // Therefore it takes less memory to track
1883 // in the document vs. a linked list in the XMLNode,
1884 // and the performance is the same.
1885 DynArray<XMLNode*, 10> _unlinked;
Lee Thomasond1983222012-02-06 08:41:24 -08001886
Lee Thomason624d43f2012-10-12 10:58:48 -07001887 MemPoolT< sizeof(XMLElement) > _elementPool;
1888 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1889 MemPoolT< sizeof(XMLText) > _textPool;
1890 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001891
1892 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001893
1894 void Parse();
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001895
Lee Thomasonf49b9652017-10-11 10:57:49 -07001896 void SetError( XMLError error, int lineNum, const char* format, ... );
1897
Lee Thomasond946dda2018-04-05 09:11:08 -07001898 // Something of an obvious security hole, once it was discovered.
1899 // Either an ill-formed XML or an excessively deep one can overflow
1900 // the stack. Track stack depth, and error out if needed.
1901 class DepthTracker {
1902 public:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001903 explicit DepthTracker(XMLDocument * document) {
Lee Thomason70d942e2018-06-29 15:31:59 -07001904 this->_document = document;
Lee Thomasond946dda2018-04-05 09:11:08 -07001905 document->PushDepth();
1906 }
1907 ~DepthTracker() {
1908 _document->PopDepth();
1909 }
1910 private:
1911 XMLDocument * _document;
1912 };
Lee Thomasonf928c352018-04-05 09:24:20 -07001913 void PushDepth();
1914 void PopDepth();
Lee Thomasond946dda2018-04-05 09:11:08 -07001915
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001916 template<class NodeType, int PoolElementSize>
1917 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
Lee Thomason5cae8972012-01-24 18:03:07 -08001918};
1919
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001920template<class NodeType, int PoolElementSize>
1921inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1922{
1923 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1924 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1925 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1926 TIXMLASSERT( returnNode );
1927 returnNode->_memPool = &pool;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001928
1929 _unlinked.Push(returnNode);
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001930 return returnNode;
1931}
Lee Thomason7c913cd2012-01-26 18:32:34 -08001932
Lee Thomason3ffdd392012-03-28 17:27:55 -07001933/**
1934 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001935 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001936 DOM structure. It is a separate utility class.
1937
1938 Take an example:
1939 @verbatim
1940 <Document>
1941 <Element attributeA = "valueA">
1942 <Child attributeB = "value1" />
1943 <Child attributeB = "value2" />
1944 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001945 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001946 @endverbatim
1947
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001948 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001949 easy to write a *lot* of code that looks like:
1950
1951 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001952 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001953 if ( root )
1954 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001955 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001956 if ( element )
1957 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001958 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001959 if ( child )
1960 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001961 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001962 if ( child2 )
1963 {
1964 // Finally do something useful.
1965 @endverbatim
1966
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001967 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001968 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001969 and correct to use:
1970
1971 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001972 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001973 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001974 if ( child2 )
1975 {
1976 // do something useful
1977 @endverbatim
1978
1979 Which is MUCH more concise and useful.
1980
1981 It is also safe to copy handles - internally they are nothing more than node pointers.
1982 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001983 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001984 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001985
1986 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001987*/
PKEuS16ed47d2013-07-06 12:02:43 +02001988class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001989{
1990public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001991 /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001992 explicit XMLHandle( XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001993 }
1994 /// Create a handle from a node.
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001995 explicit XMLHandle( XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001996 }
1997 /// Copy constructor
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001998 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001999 }
2000 /// Assignment
2001 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002002 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002003 return *this;
2004 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002005
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002006 /// Get the first child of this handle.
2007 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002008 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002009 }
2010 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002011 XMLHandle FirstChildElement( const char* name = 0 ) {
2012 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002013 }
2014 /// Get the last child of this handle.
2015 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002016 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002017 }
2018 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002019 XMLHandle LastChildElement( const char* name = 0 ) {
2020 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002021 }
2022 /// Get the previous sibling of this handle.
2023 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002024 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002025 }
2026 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002027 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
2028 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002029 }
2030 /// Get the next sibling of this handle.
2031 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002032 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002033 }
2034 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002035 XMLHandle NextSiblingElement( const char* name = 0 ) {
2036 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002037 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002038
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002039 /// Safe cast to XMLNode. This can return null.
2040 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002041 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002042 }
2043 /// Safe cast to XMLElement. This can return null.
2044 XMLElement* ToElement() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002045 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002046 }
2047 /// Safe cast to XMLText. This can return null.
2048 XMLText* ToText() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002049 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002050 }
2051 /// Safe cast to XMLUnknown. This can return null.
2052 XMLUnknown* ToUnknown() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002053 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002054 }
2055 /// Safe cast to XMLDeclaration. This can return null.
2056 XMLDeclaration* ToDeclaration() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002057 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002058 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002059
2060private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002061 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002062};
2063
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08002064
2065/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002066 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
2067 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07002068*/
PKEuS16ed47d2013-07-06 12:02:43 +02002069class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07002070{
2071public:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02002072 explicit XMLConstHandle( const XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002073 }
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02002074 explicit XMLConstHandle( const XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002075 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002076 XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002077 }
Lee Thomason8b899812012-04-04 15:58:16 -07002078
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002079 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002080 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002081 return *this;
2082 }
Lee Thomason8b899812012-04-04 15:58:16 -07002083
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002084 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002085 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002086 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002087 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2088 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002089 }
2090 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002091 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002092 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002093 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2094 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002095 }
2096 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002097 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002098 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002099 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2100 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002101 }
2102 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002103 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002104 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002105 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2106 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002107 }
Lee Thomason8b899812012-04-04 15:58:16 -07002108
2109
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002110 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002111 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002112 }
2113 const XMLElement* ToElement() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002114 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002115 }
2116 const XMLText* ToText() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002117 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002118 }
2119 const XMLUnknown* ToUnknown() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002120 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002121 }
2122 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002123 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002124 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08002125
Lee Thomason5cae8972012-01-24 18:03:07 -08002126private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002127 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08002128};
Lee Thomason6f381b72012-03-02 12:59:39 -08002129
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002130
2131/**
2132 Printing functionality. The XMLPrinter gives you more
2133 options than the XMLDocument::Print() method.
2134
2135 It can:
2136 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02002137 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002138 -# Print XML without a XMLDocument.
2139
2140 Print to Memory
2141
2142 @verbatim
2143 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06002144 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02002145 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002146 @endverbatim
2147
2148 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002149
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002150 You provide the file pointer.
2151 @verbatim
2152 XMLPrinter printer( fp );
2153 doc.Print( &printer );
2154 @endverbatim
2155
2156 Print without a XMLDocument
2157
2158 When loading, an XML parser is very useful. However, sometimes
2159 when saving, it just gets in the way. The code is often set up
2160 for streaming, and constructing the DOM is just overhead.
2161
2162 The Printer supports the streaming case. The following code
2163 prints out a trivially simple XML file without ever creating
2164 an XML document.
2165
2166 @verbatim
2167 XMLPrinter printer( fp );
2168 printer.OpenElement( "foo" );
2169 printer.PushAttribute( "foo", "bar" );
2170 printer.CloseElement();
2171 @endverbatim
2172*/
PKEuS16ed47d2013-07-06 12:02:43 +02002173class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002174{
2175public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002176 /** Construct the printer. If the FILE* is specified,
2177 this will print to the FILE. Else it will print
2178 to memory, and the result is available in CStr().
2179 If 'compact' is set to true, then output is created
2180 with only required whitespace and newlines.
2181 */
PKEuS1bfb9542013-08-04 13:51:17 +02002182 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002183 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002184
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002185 /** If streaming, write the BOM and declaration. */
2186 void PushHeader( bool writeBOM, bool writeDeclaration );
2187 /** If streaming, start writing an element.
2188 The element must be closed with CloseElement()
2189 */
Lee Thomason256adb62014-04-06 14:41:46 -07002190 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002191 /// If streaming, add an attribute to an open element.
2192 void PushAttribute( const char* name, const char* value );
2193 void PushAttribute( const char* name, int value );
2194 void PushAttribute( const char* name, unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002195 void PushAttribute(const char* name, int64_t value);
2196 void PushAttribute( const char* name, bool value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002197 void PushAttribute( const char* name, double value );
2198 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002199 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002200
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002201 /// Add a text node.
2202 void PushText( const char* text, bool cdata=false );
2203 /// Add a text node from an integer.
2204 void PushText( int value );
2205 /// Add a text node from an unsigned.
2206 void PushText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002207 /// Add a text node from an unsigned.
2208 void PushText(int64_t value);
2209 /// Add a text node from a bool.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002210 void PushText( bool value );
2211 /// Add a text node from a float.
2212 void PushText( float value );
2213 /// Add a text node from a double.
2214 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002215
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002216 /// Add a comment
2217 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002218
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002219 void PushDeclaration( const char* value );
2220 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002221
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002222 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2223 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2224 return true;
2225 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002226
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002227 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2228 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002229
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002230 virtual bool Visit( const XMLText& text );
2231 virtual bool Visit( const XMLComment& comment );
2232 virtual bool Visit( const XMLDeclaration& declaration );
2233 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002234
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002235 /**
2236 If in print to memory mode, return a pointer to
2237 the XML file in memory.
2238 */
2239 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002240 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002241 }
2242 /**
2243 If in print to memory mode, return the size
2244 of the XML file in memory. (Note the size returned
2245 includes the terminating null.)
2246 */
2247 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002248 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002249 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002250 /**
2251 If in print to memory mode, reset the buffer to the
2252 beginning.
2253 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002254 void ClearBuffer() {
2255 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002256 _buffer.Push(0);
Lee Thomason53858b42017-06-01 19:09:16 -07002257 _firstElement = true;
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002258 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002259
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002260protected:
Alexander Maid740b642014-05-20 22:04:42 +02002261 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002262
Lee Thomasonc18eb232014-02-21 17:31:17 -08002263 /** Prints out the space before an element. You may override to change
2264 the space and tabs used. A PrintSpace() override should call Print().
2265 */
2266 virtual void PrintSpace( int depth );
2267 void Print( const char* format, ... );
Alexander Golubevb2e08e42016-03-28 19:51:59 +03002268 void Write( const char* data, size_t size );
2269 inline void Write( const char* data ) { Write( data, strlen( data ) ); }
2270 void Putc( char ch );
Lee Thomasonc18eb232014-02-21 17:31:17 -08002271
Dmitry-Mea092bc12014-12-23 17:57:05 +03002272 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002273 bool _elementJustOpened;
2274 DynArray< const char*, 10 > _stack;
2275
2276private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002277 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002278
Lee Thomason624d43f2012-10-12 10:58:48 -07002279 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002280 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002281 int _depth;
2282 int _textDepth;
2283 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002284 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002285
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002286 enum {
2287 ENTITY_RANGE = 64,
2288 BUF_SIZE = 200
2289 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002290 bool _entityFlag[ENTITY_RANGE];
2291 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002292
Lee Thomason624d43f2012-10-12 10:58:48 -07002293 DynArray< char, 20 > _buffer;
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002294
2295 // Prohibit cloning, intentionally not implemented
2296 XMLPrinter( const XMLPrinter& );
2297 XMLPrinter& operator=( const XMLPrinter& );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002298};
2299
2300
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002301} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002302
PKEuS95060352013-07-26 10:42:44 +02002303#if defined(_MSC_VER)
2304# pragma warning(pop)
2305#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002306
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002307#endif // TINYXML2_INCLUDED