blob: a09bd5bfc98d3a65ab81d414a211033d5547fb43 [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
193 void operator=( 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
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700291 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 Thomasonf07b9522014-10-30 13:25:12 -0700337 virtual void Clear() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800338};
339
Lee Thomason50adb4c2012-02-13 15:07:09 -0800340
U-Stream\Leeae25a442012-02-17 17:48:16 -0800341/*
342 Template child class to create pools of the correct type.
343*/
Dmitry-Me88145b82016-08-09 17:59:31 +0300344template< int ITEM_SIZE >
PKEuS95060352013-07-26 10:42:44 +0200345class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800346{
347public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200348 MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700349 ~MemPoolT() {
Lee Thomasonf07b9522014-10-30 13:25:12 -0700350 Clear();
351 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700352
Lee Thomasonf07b9522014-10-30 13:25:12 -0700353 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700354 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700355 while( !_blockPtrs.Empty()) {
Dmitry-Meeffab6f2017-07-26 17:57:50 +0300356 Block* lastBlock = _blockPtrs.Pop();
357 delete lastBlock;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700358 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700359 _root = 0;
360 _currentAllocs = 0;
361 _nAllocs = 0;
362 _maxAllocs = 0;
363 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700364 }
Lee Thomasond1983222012-02-06 08:41:24 -0800365
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700366 virtual int ItemSize() const {
Dmitry-Me88145b82016-08-09 17:59:31 +0300367 return ITEM_SIZE;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700368 }
369 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700370 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700371 }
Lee Thomasond1983222012-02-06 08:41:24 -0800372
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700373 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700374 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700375 // Need a new block.
376 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700377 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800378
Dmitry-Me88145b82016-08-09 17:59:31 +0300379 Item* blockItems = block->items;
380 for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
381 blockItems[i].next = &(blockItems[i + 1]);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700382 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300383 blockItems[ITEMS_PER_BLOCK - 1].next = 0;
384 _root = blockItems;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700385 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300386 Item* const result = _root;
387 TIXMLASSERT( result != 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700388 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800389
Lee Thomason624d43f2012-10-12 10:58:48 -0700390 ++_currentAllocs;
391 if ( _currentAllocs > _maxAllocs ) {
392 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700393 }
Dmitry-Me3161a332016-09-02 16:58:06 +0300394 ++_nAllocs;
395 ++_nUntracked;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700396 return result;
397 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700398
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700399 virtual void Free( void* mem ) {
400 if ( !mem ) {
401 return;
402 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700403 --_currentAllocs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300404 Item* item = static_cast<Item*>( mem );
Peter Matula50689912018-01-09 12:52:26 +0100405#ifdef TINYXML2_DEBUG
Dmitry-Me88145b82016-08-09 17:59:31 +0300406 memset( item, 0xfe, sizeof( *item ) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700407#endif
Dmitry-Me88145b82016-08-09 17:59:31 +0300408 item->next = _root;
409 _root = item;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700410 }
411 void Trace( const char* name ) {
412 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Dmitry-Me88145b82016-08-09 17:59:31 +0300413 name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
414 ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700415 }
Lee Thomasond1983222012-02-06 08:41:24 -0800416
Lee Thomason5b0a6772012-11-19 13:54:42 -0800417 void SetTracked() {
Dmitry-Me3161a332016-09-02 16:58:06 +0300418 --_nUntracked;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800419 }
420
421 int Untracked() const {
422 return _nUntracked;
423 }
424
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800425 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
426 // The test file is large, 170k.
427 // Release: VS2010 gcc(no opt)
428 // 1k: 4000
429 // 2k: 4000
430 // 4k: 3900 21000
431 // 16k: 5200
432 // 32k: 4300
433 // 64k: 4000 21000
Dmitry-Me88145b82016-08-09 17:59:31 +0300434 // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
435 // in private part if ITEMS_PER_BLOCK is private
436 enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
Jerome Martinez7921df12012-10-24 11:45:44 +0200437
Lee Thomasond1983222012-02-06 08:41:24 -0800438private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300439 MemPoolT( const MemPoolT& ); // not supported
440 void operator=( const MemPoolT& ); // not supported
441
Dmitry-Me88145b82016-08-09 17:59:31 +0300442 union Item {
443 Item* next;
444 char itemData[ITEM_SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700445 };
446 struct Block {
Dmitry-Me88145b82016-08-09 17:59:31 +0300447 Item items[ITEMS_PER_BLOCK];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700448 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700449 DynArray< Block*, 10 > _blockPtrs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300450 Item* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800451
Lee Thomason624d43f2012-10-12 10:58:48 -0700452 int _currentAllocs;
453 int _nAllocs;
454 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800455 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800456};
457
Lee Thomason2c85a712012-01-31 08:24:24 -0800458
Lee Thomason56bdd022012-02-09 18:16:58 -0800459
460/**
461 Implements the interface to the "Visitor pattern" (see the Accept() method.)
462 If you call the Accept() method, it requires being passed a XMLVisitor
463 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200464 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800465 are simply called with Visit().
466
467 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700468 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800469
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700470 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800471 visiting). You need to only override methods that are interesting to you.
472
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600473 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800474
475 You should never change the document from a callback.
476
477 @sa XMLNode::Accept()
478*/
PKEuS16ed47d2013-07-06 12:02:43 +0200479class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800480{
481public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700482 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800483
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700484 /// Visit a document.
485 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
486 return true;
487 }
488 /// Visit a document.
489 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
490 return true;
491 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800492
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700493 /// Visit an element.
494 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
495 return true;
496 }
497 /// Visit an element.
498 virtual bool VisitExit( const XMLElement& /*element*/ ) {
499 return true;
500 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800501
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700502 /// Visit a declaration.
503 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
504 return true;
505 }
506 /// Visit a text node.
507 virtual bool Visit( const XMLText& /*text*/ ) {
508 return true;
509 }
510 /// Visit a comment node.
511 virtual bool Visit( const XMLComment& /*comment*/ ) {
512 return true;
513 }
514 /// Visit an unknown node.
515 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
516 return true;
517 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800518};
519
Dmitry-Me66d2a842014-11-08 15:24:52 +0300520// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900521enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900522 XML_SUCCESS = 0,
numatrumpetcd8550c2014-09-08 16:59:39 +0900523 XML_NO_ATTRIBUTE,
524 XML_WRONG_ATTRIBUTE_TYPE,
525 XML_ERROR_FILE_NOT_FOUND,
526 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
527 XML_ERROR_FILE_READ_ERROR,
numatrumpetcd8550c2014-09-08 16:59:39 +0900528 XML_ERROR_PARSING_ELEMENT,
529 XML_ERROR_PARSING_ATTRIBUTE,
numatrumpetcd8550c2014-09-08 16:59:39 +0900530 XML_ERROR_PARSING_TEXT,
531 XML_ERROR_PARSING_CDATA,
532 XML_ERROR_PARSING_COMMENT,
533 XML_ERROR_PARSING_DECLARATION,
534 XML_ERROR_PARSING_UNKNOWN,
535 XML_ERROR_EMPTY_DOCUMENT,
536 XML_ERROR_MISMATCHED_ELEMENT,
537 XML_ERROR_PARSING,
538 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700539 XML_NO_TEXT_NODE,
Lee Thomasond946dda2018-04-05 09:11:08 -0700540 XML_ELEMENT_DEPTH_EXCEEDED,
Lee Thomason331596e2014-09-11 14:56:43 -0700541
542 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900543};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900544
numatrumpetcd8550c2014-09-08 16:59:39 +0900545
U-Stream\Leeae25a442012-02-17 17:48:16 -0800546/*
547 Utility functionality.
548*/
Lee Thomasonce667c92016-12-26 16:45:30 -0800549class TINYXML2_LIB XMLUtil
Lee Thomason56bdd022012-02-09 18:16:58 -0800550{
Lee Thomasond1983222012-02-06 08:41:24 -0800551public:
kezenator4f756162016-11-29 19:46:27 +1000552 static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300553 TIXMLASSERT( p );
Lee Thomasone90e9012016-12-24 07:34:39 -0800554
Dmitry-Mefa20b222014-10-31 12:53:04 +0300555 while( IsWhiteSpace(*p) ) {
Lee Thomasone90e9012016-12-24 07:34:39 -0800556 if (curLineNumPtr && *p == '\n') {
kezenator4f756162016-11-29 19:46:27 +1000557 ++(*curLineNumPtr);
kezenatorec694152016-11-26 17:21:43 +1000558 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700559 ++p;
560 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300561 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700562 return p;
563 }
kezenator4f756162016-11-29 19:46:27 +1000564 static char* SkipWhiteSpace( char* p, int* curLineNumPtr ) {
565 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700566 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300567
568 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
569 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700570 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100571 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700572 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700573
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200574 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300575 if ( ch >= 128 ) {
576 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
577 return true;
578 }
579 if ( isalpha( ch ) ) {
580 return true;
581 }
582 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200583 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700584
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200585 inline static bool IsNameChar( unsigned char ch ) {
586 return IsNameStartChar( ch )
587 || isdigit( ch )
588 || ch == '.'
589 || ch == '-';
590 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800591
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700592 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700593 if ( p == q ) {
594 return true;
595 }
Dmitry-Me21f99692016-10-03 13:01:57 +0300596 TIXMLASSERT( p );
597 TIXMLASSERT( q );
598 TIXMLASSERT( nChar >= 0 );
Lee Thomason598a88d2015-10-09 14:42:12 -0700599 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700600 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700601
Dmitry-Me7865aad2015-06-19 16:23:35 +0300602 inline static bool IsUTF8Continuation( char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400603 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700604 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800605
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700606 static const char* ReadBOM( const char* p, bool* hasBOM );
607 // p is the starting location,
608 // the UTF-8 value of the entity will be placed in value, and length filled in.
609 static const char* GetCharacterRef( const char* p, char* value, int* length );
610 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700611
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700612 // converts primitive types to strings
613 static void ToStr( int v, char* buffer, int bufferSize );
614 static void ToStr( unsigned v, char* buffer, int bufferSize );
615 static void ToStr( bool v, char* buffer, int bufferSize );
616 static void ToStr( float v, char* buffer, int bufferSize );
617 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason51c12712016-06-04 20:18:49 -0700618 static void ToStr(int64_t v, char* buffer, int bufferSize);
Lee Thomason21be8822012-07-15 17:27:22 -0700619
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700620 // converts strings to primitive types
621 static bool ToInt( const char* str, int* value );
622 static bool ToUnsigned( const char* str, unsigned* value );
623 static bool ToBool( const char* str, bool* value );
624 static bool ToFloat( const char* str, float* value );
625 static bool ToDouble( const char* str, double* value );
Lee Thomason51c12712016-06-04 20:18:49 -0700626 static bool ToInt64(const char* str, int64_t* value);
Lee Thomasonce667c92016-12-26 16:45:30 -0800627
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800628 // Changes what is serialized for a boolean value.
629 // Default to "true" and "false". Shouldn't be changed
630 // unless you have a special testing or compatibility need.
Lee Thomasonce667c92016-12-26 16:45:30 -0800631 // Be careful: static, global, & not thread safe.
632 // Be sure to set static const memory as parameters.
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800633 static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
Lee Thomasonce667c92016-12-26 16:45:30 -0800634
635private:
Lee Thomasonf458d262016-12-26 22:47:25 -0800636 static const char* writeBoolTrue;
637 static const char* writeBoolFalse;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800638};
639
Lee Thomason5cae8972012-01-24 18:03:07 -0800640
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800641/** XMLNode is a base class for every object that is in the
642 XML Document Object Model (DOM), except XMLAttributes.
643 Nodes have siblings, a parent, and children which can
644 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700645 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800646 be cast to its more defined type.
647
Thomas Roß08bdf502012-05-12 14:21:23 +0200648 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800649 When the XMLDocument gets deleted, all its Nodes
650 will also be deleted.
651
652 @verbatim
653 A Document can contain: Element (container or leaf)
654 Comment (leaf)
655 Unknown (leaf)
656 Declaration( leaf )
657
658 An Element can contain: Element (container or leaf)
659 Text (leaf)
660 Attributes (not on tree)
661 Comment (leaf)
662 Unknown (leaf)
663
664 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800665*/
PKEuS16ed47d2013-07-06 12:02:43 +0200666class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800667{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700668 friend class XMLDocument;
669 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800670public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800671
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700672 /// Get the XMLDocument that owns this XMLNode.
673 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300674 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700675 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700676 }
677 /// Get the XMLDocument that owns this XMLNode.
678 XMLDocument* GetDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300679 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700680 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700681 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800682
Lee Thomason2fa81722012-11-09 12:37:46 -0800683 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700684 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100685 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700686 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800687 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700688 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100689 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700690 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800691 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700692 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100693 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700694 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800695 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700696 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100697 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700698 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800699 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700700 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100701 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800703 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700704 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100705 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700706 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800707
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700708 virtual const XMLElement* ToElement() const {
709 return 0;
710 }
711 virtual const XMLText* ToText() const {
712 return 0;
713 }
714 virtual const XMLComment* ToComment() const {
715 return 0;
716 }
717 virtual const XMLDocument* ToDocument() const {
718 return 0;
719 }
720 virtual const XMLDeclaration* ToDeclaration() const {
721 return 0;
722 }
723 virtual const XMLUnknown* ToUnknown() const {
724 return 0;
725 }
Lee Thomason751da522012-02-10 08:50:51 -0800726
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700727 /** The meaning of 'value' changes for the specific type.
728 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530729 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700730 Element: name of the element
731 Comment: the comment text
732 Unknown: the tag contents
733 Text: the text string
734 @endverbatim
735 */
Michael Daumling21626882013-10-22 17:03:37 +0200736 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100737
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700738 /** Set the Value of an XML node.
739 @sa Value()
740 */
741 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800742
kezenatorec694152016-11-26 17:21:43 +1000743 /// Gets the line number the node is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +1000744 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +1000745
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700746 /// Get the parent of this node on the DOM.
747 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700748 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700749 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100750
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700751 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700752 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700753 }
Lee Thomason751da522012-02-10 08:50:51 -0800754
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700755 /// Returns true if this node has no children.
756 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700757 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700758 }
Lee Thomason751da522012-02-10 08:50:51 -0800759
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700760 /// Get the first child node, or null if none exists.
761 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700762 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700763 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100764
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700765 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700766 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700767 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100768
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700769 /** Get the first child element, or optionally the first child
770 element with the specified name.
771 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300772 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700773
Dmitry-Me886ad972015-07-22 11:00:51 +0300774 XMLElement* FirstChildElement( const char* name = 0 ) {
775 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700776 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800777
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700778 /// Get the last child node, or null if none exists.
779 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700780 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700781 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700782
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700783 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300784 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700785 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800786
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700787 /** Get the last child element or optionally the last child
788 element with the specified name.
789 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300790 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700791
Dmitry-Me886ad972015-07-22 11:00:51 +0300792 XMLElement* LastChildElement( const char* name = 0 ) {
793 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700794 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700795
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700796 /// Get the previous (left) sibling node of this node.
797 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700798 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700799 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700800
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700801 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700802 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700803 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800804
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700805 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300806 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700807
Dmitry-Me886ad972015-07-22 11:00:51 +0300808 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
809 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700810 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700811
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700812 /// Get the next (right) sibling node of this node.
813 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700814 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700815 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700816
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700817 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700818 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700819 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700820
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700821 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300822 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700823
Dmitry-Me886ad972015-07-22 11:00:51 +0300824 XMLElement* NextSiblingElement( const char* name = 0 ) {
825 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700826 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800827
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700828 /**
829 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200830 If the child node is already part of the document,
831 it is moved from its old location to the new location.
832 Returns the addThis argument or 0 if the node does not
833 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700834 */
835 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800836
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700837 XMLNode* LinkEndChild( XMLNode* addThis ) {
838 return InsertEndChild( addThis );
839 }
840 /**
841 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200842 If the child node is already part of the document,
843 it is moved from its old location to the new location.
844 Returns the addThis argument or 0 if the node does not
845 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700846 */
847 XMLNode* InsertFirstChild( XMLNode* addThis );
848 /**
849 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200850 If the child node is already part of the document,
851 it is moved from its old location to the new location.
852 Returns the addThis argument or 0 if the afterThis node
853 is not a child of this node, or if the node does not
854 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700855 */
856 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700857
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700858 /**
859 Delete all the children of this node.
860 */
861 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800862
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700863 /**
864 Delete a child of this node.
865 */
866 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800867
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700868 /**
869 Make a copy of this node, but not its children.
870 You may pass in a Document pointer that will be
871 the owner of the new Node. If the 'document' is
872 null, then the node returned will be allocated
873 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800874
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700875 Note: if called on a XMLDocument, this will return null.
876 */
877 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800878
Lee Thomason7085f002017-06-01 18:09:43 -0700879 /**
Lee Thomason1346a172017-06-14 15:14:19 -0700880 Make a copy of this node and all its children.
Lee Thomason7085f002017-06-01 18:09:43 -0700881
Dmitry-Me3f63f212017-06-19 18:25:19 +0300882 If the 'target' is null, then the nodes will
Lee Thomason70d942e2018-06-29 15:31:59 -0700883 be allocated in the current document. If 'target'
884 is specified, the memory will be allocated is the
Lee Thomason1346a172017-06-14 15:14:19 -0700885 specified XMLDocument.
Lee Thomason7085f002017-06-01 18:09:43 -0700886
Lee Thomason70d942e2018-06-29 15:31:59 -0700887 NOTE: This is probably not the correct tool to
Lee Thomason7085f002017-06-01 18:09:43 -0700888 copy a document, since XMLDocuments can have multiple
Lee Thomason1346a172017-06-14 15:14:19 -0700889 top level XMLNodes. You probably want to use
890 XMLDocument::DeepCopy()
Lee Thomason7085f002017-06-01 18:09:43 -0700891 */
Dmitry-Me3f63f212017-06-19 18:25:19 +0300892 XMLNode* DeepClone( XMLDocument* target ) const;
Lee Thomason7085f002017-06-01 18:09:43 -0700893
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700894 /**
895 Test if 2 nodes are the same, but don't test children.
896 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800897
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700898 Note: if called on a XMLDocument, this will return false.
899 */
900 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800901
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600902 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700903 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600904 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800905
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600906 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
907 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700908 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800909
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700910 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800911
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700912 - http://www.saxproject.org/
913 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800914
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700915 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800916
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700917 An example of using Accept():
918 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600919 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700920 tinyxmlDoc.Accept( &printer );
921 const char* xmlcstr = printer.CStr();
922 @endverbatim
923 */
924 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800925
Lee Thomason70d942e2018-06-29 15:31:59 -0700926 /**
927 Set user data into the XMLNode. TinyXML-2 in
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700928 no way processes or interprets user data.
929 It is initially 0.
930 */
931 void SetUserData(void* userData) { _userData = userData; }
932
933 /**
934 Get user data set into the XMLNode. TinyXML-2 in
935 no way processes or interprets user data.
936 It is initially 0.
937 */
938 void* GetUserData() const { return _userData; }
939
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800940protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700941 XMLNode( XMLDocument* );
942 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700943
Dmitry-Me10b8ecc2017-04-12 17:57:44 +0300944 virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300945
Lee Thomason624d43f2012-10-12 10:58:48 -0700946 XMLDocument* _document;
947 XMLNode* _parent;
948 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +1000949 int _parseLineNum;
Lee Thomason3f57d272012-01-11 15:30:03 -0800950
Lee Thomason624d43f2012-10-12 10:58:48 -0700951 XMLNode* _firstChild;
952 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800953
Lee Thomason624d43f2012-10-12 10:58:48 -0700954 XMLNode* _prev;
955 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800956
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700957 void* _userData;
958
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800959private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700960 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700961 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400962 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800963 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Meecb9b072016-10-12 16:44:59 +0300964 const XMLElement* ToElementWithName( const char* name ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300965
966 XMLNode( const XMLNode& ); // not supported
967 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800968};
969
970
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800971/** XML text.
972
973 Note that a text node can have child element nodes, for example:
974 @verbatim
975 <root>This is <b>bold</b></root>
976 @endverbatim
977
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700978 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800979 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 -0700980 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600981 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800982*/
PKEuS16ed47d2013-07-06 12:02:43 +0200983class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800984{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700985 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800986public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700987 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800988
Lee Thomason624d43f2012-10-12 10:58:48 -0700989 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700990 return this;
991 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700992 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700993 return this;
994 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800995
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700996 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700997 void SetCData( bool isCData ) {
998 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700999 }
1000 /// Returns true if this is a CDATA text element.
1001 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001002 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001003 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001004
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001005 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1006 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001007
Lee Thomason5492a1c2012-01-23 15:32:10 -08001008protected:
Lee Thomason624d43f2012-10-12 10:58:48 -07001009 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001010 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -08001011
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001012 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001013
Lee Thomason5492a1c2012-01-23 15:32:10 -08001014private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001015 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +03001016
1017 XMLText( const XMLText& ); // not supported
1018 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -08001019};
1020
1021
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001022/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +02001023class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001024{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001025 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -08001026public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001027 virtual XMLComment* ToComment() {
1028 return this;
1029 }
1030 virtual const XMLComment* ToComment() const {
1031 return this;
1032 }
Lee Thomasonce0763e2012-01-11 15:43:54 -08001033
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001034 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001035
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001036 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1037 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -08001038
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001039protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001040 XMLComment( XMLDocument* doc );
1041 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001042
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001043 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001044
Lee Thomason3f57d272012-01-11 15:30:03 -08001045private:
Dmitry-Mef547a992015-01-09 15:17:09 +03001046 XMLComment( const XMLComment& ); // not supported
1047 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001048};
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001049
1050
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001051/** In correct XML the declaration is the first entry in the file.
1052 @verbatim
1053 <?xml version="1.0" standalone="yes"?>
1054 @endverbatim
1055
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001056 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001057 however.
1058
1059 The text of the declaration isn't interpreted. It is parsed
1060 and written as a string.
1061*/
PKEuS16ed47d2013-07-06 12:02:43 +02001062class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001063{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001064 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001065public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001066 virtual XMLDeclaration* ToDeclaration() {
1067 return this;
1068 }
1069 virtual const XMLDeclaration* ToDeclaration() const {
1070 return this;
1071 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001072
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001073 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001074
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001075 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1076 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001077
1078protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001079 XMLDeclaration( XMLDocument* doc );
1080 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +03001081
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001082 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001083
Dmitry-Mef547a992015-01-09 15:17:09 +03001084private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001085 XMLDeclaration( const XMLDeclaration& ); // not supported
1086 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001087};
1088
1089
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001090/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001091 unknown. It is a tag of text, but should not be modified.
1092 It will be written back to the XML, unchanged, when the file
1093 is saved.
1094
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001095 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001096*/
PKEuS16ed47d2013-07-06 12:02:43 +02001097class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001098{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001099 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001100public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001101 virtual XMLUnknown* ToUnknown() {
1102 return this;
1103 }
1104 virtual const XMLUnknown* ToUnknown() const {
1105 return this;
1106 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001107
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001108 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001109
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001110 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1111 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001112
1113protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001114 XMLUnknown( XMLDocument* doc );
1115 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001116
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001117 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001118
Dmitry-Mef547a992015-01-09 15:17:09 +03001119private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001120 XMLUnknown( const XMLUnknown& ); // not supported
1121 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001122};
1123
1124
Lee Thomason1ff38e02012-02-14 18:18:16 -08001125
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001126/** An attribute is a name-value pair. Elements have an arbitrary
1127 number of attributes, each with a unique name.
1128
1129 @note The attributes are not XMLNodes. You may only query the
1130 Next() attribute in a list.
1131*/
PKEuS16ed47d2013-07-06 12:02:43 +02001132class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001133{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001134 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001135public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001136 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001137 const char* Name() const;
1138
Lee Thomason2fa81722012-11-09 12:37:46 -08001139 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001140 const char* Value() const;
1141
kezenatorec694152016-11-26 17:21:43 +10001142 /// Gets the line number the attribute is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +10001143 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +10001144
Lee Thomason2fa81722012-11-09 12:37:46 -08001145 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001146 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001147 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001148 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001149
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001150 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001151 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001152 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001153 */
Lee Thomason51c12712016-06-04 20:18:49 -07001154 int IntValue() const {
1155 int i = 0;
1156 QueryIntValue(&i);
1157 return i;
1158 }
1159
1160 int64_t Int64Value() const {
1161 int64_t i = 0;
1162 QueryInt64Value(&i);
1163 return i;
1164 }
1165
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001166 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001167 unsigned UnsignedValue() const {
1168 unsigned i=0;
1169 QueryUnsignedValue( &i );
1170 return i;
1171 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001172 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001173 bool BoolValue() const {
1174 bool b=false;
1175 QueryBoolValue( &b );
1176 return b;
1177 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001178 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001179 double DoubleValue() const {
1180 double d=0;
1181 QueryDoubleValue( &d );
1182 return d;
1183 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001184 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001185 float FloatValue() const {
1186 float f=0;
1187 QueryFloatValue( &f );
1188 return f;
1189 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001190
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001191 /** QueryIntValue interprets the attribute as an integer, and returns the value
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001192 in the provided parameter. The function will return XML_SUCCESS on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001193 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1194 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001195 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001196 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001197 XMLError QueryUnsignedValue( unsigned int* value ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001198 /// See QueryIntValue
1199 XMLError QueryInt64Value(int64_t* value) const;
1200 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001201 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001202 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001203 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001204 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001205 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001206
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001207 /// Set the attribute to a string value.
1208 void SetAttribute( const char* value );
1209 /// Set the attribute to value.
1210 void SetAttribute( int value );
1211 /// Set the attribute to value.
1212 void SetAttribute( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001213 /// Set the attribute to value.
1214 void SetAttribute(int64_t value);
1215 /// Set the attribute to value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001216 void SetAttribute( bool value );
1217 /// Set the attribute to value.
1218 void SetAttribute( double value );
1219 /// Set the attribute to value.
1220 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001221
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001222private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001223 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001224
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001225 XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001226 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001227
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001228 XMLAttribute( const XMLAttribute& ); // not supported
1229 void operator=( const XMLAttribute& ); // not supported
1230 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001231
kezenator4f756162016-11-29 19:46:27 +10001232 char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001233
Lee Thomason624d43f2012-10-12 10:58:48 -07001234 mutable StrPair _name;
1235 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +10001236 int _parseLineNum;
Lee Thomason624d43f2012-10-12 10:58:48 -07001237 XMLAttribute* _next;
1238 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001239};
1240
1241
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001242/** The element is a container class. It has a value, the element name,
1243 and can contain other elements, text, comments, and unknowns.
1244 Elements also contain an arbitrary number of attributes.
1245*/
PKEuS16ed47d2013-07-06 12:02:43 +02001246class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001247{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001248 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001249public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001250 /// Get the name of an element (which is the Value() of the node.)
1251 const char* Name() const {
1252 return Value();
1253 }
1254 /// Set the name of the element.
1255 void SetName( const char* str, bool staticMem=false ) {
1256 SetValue( str, staticMem );
1257 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001258
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001259 virtual XMLElement* ToElement() {
1260 return this;
1261 }
1262 virtual const XMLElement* ToElement() const {
1263 return this;
1264 }
1265 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001266
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001267 /** Given an attribute name, Attribute() returns the value
1268 for the attribute of that name, or null if none
1269 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001270
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001271 @verbatim
1272 const char* value = ele->Attribute( "foo" );
1273 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001274
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001275 The 'value' parameter is normally null. However, if specified,
1276 the attribute will only be returned if the 'name' and 'value'
1277 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001278
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001279 @verbatim
1280 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1281 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001282
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001283 rather than:
1284 @verbatim
1285 if ( ele->Attribute( "foo" ) ) {
1286 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1287 }
1288 @endverbatim
1289 */
1290 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001291
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001292 /** Given an attribute name, IntAttribute() returns the value
Lee Thomason13cbc9a2016-10-27 14:55:07 -07001293 of the attribute interpreted as an integer. The default
1294 value will be returned if the attribute isn't present,
1295 or if there is an error. (For a method with error
1296 checking, see QueryIntAttribute()).
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001297 */
Josh Wittnercf3dd092016-10-11 18:57:17 -07001298 int IntAttribute(const char* name, int defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001299 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001300 unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001301 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001302 int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001303 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001304 bool BoolAttribute(const char* name, bool defaultValue = false) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001305 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001306 double DoubleAttribute(const char* name, double defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001307 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001308 float FloatAttribute(const char* name, float defaultValue = 0) const;
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001309
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001310 /** Given an attribute name, QueryIntAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001311 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001312 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1313 doesn't exist. If successful, the result of the conversion
1314 will be written to 'value'. If not successful, nothing will
1315 be written to 'value'. This allows you to provide default
1316 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001317
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001318 @verbatim
1319 int value = 10;
1320 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1321 @endverbatim
1322 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001323 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001324 const XMLAttribute* a = FindAttribute( name );
1325 if ( !a ) {
1326 return XML_NO_ATTRIBUTE;
1327 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001328 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001329 }
Lee Thomason51c12712016-06-04 20:18:49 -07001330
1331 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001332 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001333 const XMLAttribute* a = FindAttribute( name );
1334 if ( !a ) {
1335 return XML_NO_ATTRIBUTE;
1336 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001337 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001338 }
Lee Thomason51c12712016-06-04 20:18:49 -07001339
1340 /// See QueryIntAttribute()
1341 XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1342 const XMLAttribute* a = FindAttribute(name);
1343 if (!a) {
1344 return XML_NO_ATTRIBUTE;
1345 }
1346 return a->QueryInt64Value(value);
1347 }
1348
1349 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001350 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001351 const XMLAttribute* a = FindAttribute( name );
1352 if ( !a ) {
1353 return XML_NO_ATTRIBUTE;
1354 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001355 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001356 }
1357 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001358 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001359 const XMLAttribute* a = FindAttribute( name );
1360 if ( !a ) {
1361 return XML_NO_ATTRIBUTE;
1362 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001363 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001364 }
1365 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001366 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001367 const XMLAttribute* a = FindAttribute( name );
1368 if ( !a ) {
1369 return XML_NO_ATTRIBUTE;
1370 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001371 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001372 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001373
Lee Thomason5b00e062017-12-28 14:07:47 -08001374 /// See QueryIntAttribute()
1375 XMLError QueryStringAttribute(const char* name, const char** value) const {
1376 const XMLAttribute* a = FindAttribute(name);
1377 if (!a) {
1378 return XML_NO_ATTRIBUTE;
1379 }
1380 *value = a->Value();
1381 return XML_SUCCESS;
1382 }
1383
1384
Lee Thomason70d942e2018-06-29 15:31:59 -07001385
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001386 /** Given an attribute name, QueryAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001387 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001388 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1389 doesn't exist. It is overloaded for the primitive types,
1390 and is a generally more convenient replacement of
1391 QueryIntAttribute() and related functions.
Lee Thomason70d942e2018-06-29 15:31:59 -07001392
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001393 If successful, the result of the conversion
1394 will be written to 'value'. If not successful, nothing will
1395 be written to 'value'. This allows you to provide default
1396 value:
1397
1398 @verbatim
1399 int value = 10;
1400 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1401 @endverbatim
1402 */
Lee Thomasonfc80df32018-06-29 15:37:29 -07001403 XMLError QueryAttribute( const char* name, int* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001404 return QueryIntAttribute( name, value );
1405 }
1406
Lee Thomasonfc80df32018-06-29 15:37:29 -07001407 XMLError QueryAttribute( const char* name, unsigned int* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001408 return QueryUnsignedAttribute( name, value );
1409 }
1410
Lee Thomasonfc80df32018-06-29 15:37:29 -07001411 XMLError QueryAttribute(const char* name, int64_t* value) const {
Lee Thomason51c12712016-06-04 20:18:49 -07001412 return QueryInt64Attribute(name, value);
1413 }
1414
Lee Thomasonfc80df32018-06-29 15:37:29 -07001415 XMLError QueryAttribute( const char* name, bool* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001416 return QueryBoolAttribute( name, value );
1417 }
1418
Lee Thomasonfc80df32018-06-29 15:37:29 -07001419 XMLError QueryAttribute( const char* name, double* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001420 return QueryDoubleAttribute( name, value );
1421 }
1422
Lee Thomasonfc80df32018-06-29 15:37:29 -07001423 XMLError QueryAttribute( const char* name, float* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001424 return QueryFloatAttribute( name, value );
1425 }
1426
1427 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001428 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001429 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001430 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001431 }
1432 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001433 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001434 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001435 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001436 }
1437 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001438 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001439 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001440 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001441 }
Lee Thomason51c12712016-06-04 20:18:49 -07001442
1443 /// Sets the named attribute to value.
1444 void SetAttribute(const char* name, int64_t value) {
1445 XMLAttribute* a = FindOrCreateAttribute(name);
1446 a->SetAttribute(value);
1447 }
1448
1449 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001450 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001451 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001452 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001453 }
1454 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001455 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001456 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001457 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001458 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001459 /// Sets the named attribute to value.
1460 void SetAttribute( const char* name, float value ) {
1461 XMLAttribute* a = FindOrCreateAttribute( name );
1462 a->SetAttribute( value );
1463 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001464
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001465 /**
1466 Delete an attribute.
1467 */
1468 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001469
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001470 /// Return the first attribute in the list.
1471 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001472 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001473 }
1474 /// Query a specific attribute in the list.
1475 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001476
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001477 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001478 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001479 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001480
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001481 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001482 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001483
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001484 This is a convenient method for getting the text of simple contained text:
1485 @verbatim
1486 <foo>This is text</foo>
1487 const char* str = fooElement->GetText();
1488 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001489
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001490 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001491
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001492 Note that this function can be misleading. If the element foo was created from
1493 this XML:
1494 @verbatim
1495 <foo><b>This is text</b></foo>
1496 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001497
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001498 then the value of str would be null. The first child node isn't a text node, it is
1499 another element. From this XML:
1500 @verbatim
1501 <foo>This is <b>text</b></foo>
1502 @endverbatim
1503 GetText() will return "This is ".
1504 */
1505 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001506
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001507 /** Convenience function for easy access to the text inside an element. Although easy
1508 and concise, SetText() is limited compared to creating an XMLText child
1509 and mutating it directly.
1510
1511 If the first child of 'this' is a XMLText, SetText() sets its value to
1512 the given string, otherwise it will create a first child that is an XMLText.
1513
1514 This is a convenient method for setting the text of simple contained text:
1515 @verbatim
1516 <foo>This is text</foo>
1517 fooElement->SetText( "Hullaballoo!" );
1518 <foo>Hullaballoo!</foo>
1519 @endverbatim
1520
1521 Note that this function can be misleading. If the element foo was created from
1522 this XML:
1523 @verbatim
1524 <foo><b>This is text</b></foo>
1525 @endverbatim
1526
1527 then it will not change "This is text", but rather prefix it with a text element:
1528 @verbatim
1529 <foo>Hullaballoo!<b>This is text</b></foo>
1530 @endverbatim
Lee Thomason70d942e2018-06-29 15:31:59 -07001531
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001532 For this XML:
1533 @verbatim
1534 <foo />
1535 @endverbatim
1536 SetText() will generate
1537 @verbatim
1538 <foo>Hullaballoo!</foo>
1539 @endverbatim
1540 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001541 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001542 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001543 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001544 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001545 void SetText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001546 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1547 void SetText(int64_t value);
1548 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001549 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001550 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001551 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001552 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001553 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001554
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001555 /**
1556 Convenience method to query the value of a child text node. This is probably best
1557 shown by example. Given you have a document is this form:
1558 @verbatim
1559 <point>
1560 <x>1</x>
1561 <y>1.4</y>
1562 </point>
1563 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001564
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001565 The QueryIntText() and similar functions provide a safe and easier way to get to the
1566 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001567
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001568 @verbatim
1569 int x = 0;
1570 float y = 0; // types of x and y are contrived for example
1571 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1572 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1573 xElement->QueryIntText( &x );
1574 yElement->QueryFloatText( &y );
1575 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001576
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001577 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1578 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 -07001579
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001580 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001581 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001582 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001583 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001584 /// See QueryIntText()
1585 XMLError QueryInt64Text(int64_t* uval) const;
1586 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001587 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001588 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001589 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001590 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001591 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001592
Josh Wittnercf3dd092016-10-11 18:57:17 -07001593 int IntText(int defaultValue = 0) const;
1594
Josh Wittner3a621f52016-09-12 19:17:54 -07001595 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001596 unsigned UnsignedText(unsigned defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001597 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001598 int64_t Int64Text(int64_t defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001599 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001600 bool BoolText(bool defaultValue = false) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001601 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001602 double DoubleText(double defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001603 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001604 float FloatText(float defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001605
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001606 // internal:
Dmitry-Mee5035632017-04-05 18:02:40 +03001607 enum ElementClosingType {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001608 OPEN, // <foo>
1609 CLOSED, // <foo/>
1610 CLOSING // </foo>
1611 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001612 ElementClosingType ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001613 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001614 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001615 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1616 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001617
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001618protected:
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001619 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001620
Lee Thomason50adb4c2012-02-13 15:07:09 -08001621private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001622 XMLElement( XMLDocument* doc );
1623 virtual ~XMLElement();
1624 XMLElement( const XMLElement& ); // not supported
1625 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001626
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001627 XMLAttribute* FindOrCreateAttribute( const char* name );
kezenator4f756162016-11-29 19:46:27 +10001628 char* ParseAttributes( char* p, int* curLineNumPtr );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001629 static void DeleteAttribute( XMLAttribute* attribute );
Dmitry-Mea60caa22016-11-22 18:28:08 +03001630 XMLAttribute* CreateAttribute();
Lee Thomason67d61312012-01-24 16:01:51 -08001631
Lee Thomason5bb2d802014-01-24 10:42:57 -08001632 enum { BUF_SIZE = 200 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001633 ElementClosingType _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001634 // The attribute list is ordered; there is no 'lastAttribute'
1635 // because the list needs to be scanned for dupes before adding
1636 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001637 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001638};
1639
1640
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001641enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001642 PRESERVE_WHITESPACE,
1643 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001644};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001645
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001646
1647/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001648 It can be saved, loaded, and printed to the screen.
1649 All Nodes are connected and allocated to a Document.
1650 If the Document is deleted, all its Nodes are also deleted.
1651*/
PKEuS16ed47d2013-07-06 12:02:43 +02001652class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001653{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001654 friend class XMLElement;
Lee Thomasond946dda2018-04-05 09:11:08 -07001655 // Gives access to SetError and Push/PopDepth, but over-access for everything else.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001656 // Wishing C++ had "internal" scope.
Lee Thomason70d942e2018-06-29 15:31:59 -07001657 friend class XMLNode;
Lee Thomasonf49b9652017-10-11 10:57:49 -07001658 friend class XMLText;
1659 friend class XMLComment;
1660 friend class XMLDeclaration;
1661 friend class XMLUnknown;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001662public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001663 /// constructor
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001664 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001665 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001666
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001667 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001668 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001669 return this;
1670 }
1671 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001672 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001673 return this;
1674 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001675
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001676 /**
1677 Parse an XML file from a character string.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001678 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001679 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001680
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001681 You may optionally pass in the 'nBytes', which is
1682 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001683 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001684 null terminated string.
1685 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001686 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001687
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001688 /**
1689 Load an XML file from disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001690 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001691 an errorID.
1692 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001693 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001694
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001695 /**
1696 Load an XML file from disk. You are responsible
Lee Thomason70d942e2018-06-29 15:31:59 -07001697 for providing and closing the FILE*.
1698
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001699 NOTE: The file should be opened as binary ("rb")
1700 not text in order for TinyXML-2 to correctly
1701 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001702
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001703 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001704 an errorID.
1705 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001706 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001707
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001708 /**
1709 Save the XML file to disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001710 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001711 an errorID.
1712 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001713 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001714
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001715 /**
1716 Save the XML file to disk. You are responsible
1717 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001718
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001719 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001720 an errorID.
1721 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001722 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001723
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001724 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001725 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001726 }
1727 Whitespace WhitespaceMode() const {
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001728 return _whitespaceMode;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001729 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001730
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001731 /**
1732 Returns true if this document has a leading Byte Order Mark of UTF8.
1733 */
1734 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001735 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001736 }
1737 /** Sets whether to write the BOM when writing the file.
1738 */
1739 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001740 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001741 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001742
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001743 /** Return the root element of DOM. Equivalent to FirstChildElement().
1744 To get the first node, use FirstChild().
1745 */
1746 XMLElement* RootElement() {
1747 return FirstChildElement();
1748 }
1749 const XMLElement* RootElement() const {
1750 return FirstChildElement();
1751 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001752
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001753 /** Print the Document. If the Printer is not provided, it will
1754 print to stdout. If you provide Printer, this can print to a file:
1755 @verbatim
1756 XMLPrinter printer( fp );
1757 doc.Print( &printer );
1758 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001759
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001760 Or you can use a printer to print to memory:
1761 @verbatim
1762 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001763 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001764 // printer.CStr() has a const char* to the XML
1765 @endverbatim
1766 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001767 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001768 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001769
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001770 /**
1771 Create a new Element associated with
1772 this Document. The memory for the Element
1773 is managed by the Document.
1774 */
1775 XMLElement* NewElement( const char* name );
1776 /**
1777 Create a new Comment associated with
1778 this Document. The memory for the Comment
1779 is managed by the Document.
1780 */
1781 XMLComment* NewComment( const char* comment );
1782 /**
1783 Create a new Text associated with
1784 this Document. The memory for the Text
1785 is managed by the Document.
1786 */
1787 XMLText* NewText( const char* text );
1788 /**
1789 Create a new Declaration associated with
1790 this Document. The memory for the object
1791 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001792
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001793 If the 'text' param is null, the standard
1794 declaration is used.:
1795 @verbatim
1796 <?xml version="1.0" encoding="UTF-8"?>
1797 @endverbatim
1798 */
1799 XMLDeclaration* NewDeclaration( const char* text=0 );
1800 /**
1801 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001802 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001803 is managed by the Document.
1804 */
1805 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001806
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001807 /**
1808 Delete a node associated with this document.
1809 It will be unlinked from the DOM.
1810 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001811 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001812
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001813 void ClearError() {
Lee Thomasonaa188392017-09-19 17:54:31 -07001814 SetError(XML_SUCCESS, 0, 0);
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001815 }
1816
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001817 /// Return true if there was an error parsing the document.
1818 bool Error() const {
Lee Thomason85536252016-06-04 19:10:53 -07001819 return _errorID != XML_SUCCESS;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001820 }
1821 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001822 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001823 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001824 }
Lee Thomason331596e2014-09-11 14:56:43 -07001825 const char* ErrorName() const;
Lee Thomasone90e9012016-12-24 07:34:39 -08001826 static const char* ErrorIDToName(XMLError errorID);
Lee Thomason331596e2014-09-11 14:56:43 -07001827
Lee Thomason70d942e2018-06-29 15:31:59 -07001828 /** Returns a "long form" error description. A hopefully helpful
Lee Thomasonf49b9652017-10-11 10:57:49 -07001829 diagnostic with location, line number, and/or additional info.
1830 */
1831 const char* ErrorStr() const;
1832
1833 /// A (trivial) utility function that prints the ErrorStr() to stdout.
1834 void PrintError() const;
Lee Thomason8c9e3132017-06-26 16:55:01 -07001835
orbitcowboy22b21ec2018-07-17 11:52:57 +02001836 /// Return the line where the error occurred, or zero if unknown.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001837 int ErrorLineNum() const
kezenatorec694152016-11-26 17:21:43 +10001838 {
1839 return _errorLineNum;
1840 }
Lee Thomason70d942e2018-06-29 15:31:59 -07001841
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001842 /// Clear the document, resetting it to the initial state.
1843 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001844
Lee Thomason7085f002017-06-01 18:09:43 -07001845 /**
Lee Thomasonb29f5562017-06-01 18:45:32 -07001846 Copies this document to a target document.
Lee Thomason7085f002017-06-01 18:09:43 -07001847 The target will be completely cleared before the copy.
Lee Thomasonb29f5562017-06-01 18:45:32 -07001848 If you want to copy a sub-tree, see XMLNode::DeepClone().
Lee Thomason7085f002017-06-01 18:09:43 -07001849
Lee Thomason1346a172017-06-14 15:14:19 -07001850 NOTE: that the 'target' must be non-null.
Lee Thomason7085f002017-06-01 18:09:43 -07001851 */
Shuichiro Suzuki87cd4e02017-08-24 11:20:26 +09001852 void DeepCopy(XMLDocument* target) const;
Lee Thomason7085f002017-06-01 18:09:43 -07001853
1854 // internal
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001855 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001856
Lee Thomason816d3fa2017-06-05 14:35:55 -07001857 // internal
1858 void MarkInUse(XMLNode*);
1859
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001860 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1861 return 0;
1862 }
1863 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1864 return false;
1865 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001866
Lee Thomason3f57d272012-01-11 15:30:03 -08001867private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001868 XMLDocument( const XMLDocument& ); // not supported
1869 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001870
Lee Thomasone90e9012016-12-24 07:34:39 -08001871 bool _writeBOM;
1872 bool _processEntities;
1873 XMLError _errorID;
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001874 Whitespace _whitespaceMode;
Lee Thomasonaa188392017-09-19 17:54:31 -07001875 mutable StrPair _errorStr;
Lee Thomasone90e9012016-12-24 07:34:39 -08001876 int _errorLineNum;
1877 char* _charBuffer;
1878 int _parseCurLineNum;
Lee Thomasond946dda2018-04-05 09:11:08 -07001879 int _parsingDepth;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001880 // Memory tracking does add some overhead.
1881 // However, the code assumes that you don't
1882 // have a bunch of unlinked nodes around.
1883 // Therefore it takes less memory to track
1884 // in the document vs. a linked list in the XMLNode,
1885 // and the performance is the same.
1886 DynArray<XMLNode*, 10> _unlinked;
Lee Thomasond1983222012-02-06 08:41:24 -08001887
Lee Thomason624d43f2012-10-12 10:58:48 -07001888 MemPoolT< sizeof(XMLElement) > _elementPool;
1889 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1890 MemPoolT< sizeof(XMLText) > _textPool;
1891 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001892
1893 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001894
1895 void Parse();
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001896
Lee Thomasonf49b9652017-10-11 10:57:49 -07001897 void SetError( XMLError error, int lineNum, const char* format, ... );
1898
Lee Thomasond946dda2018-04-05 09:11:08 -07001899 // Something of an obvious security hole, once it was discovered.
1900 // Either an ill-formed XML or an excessively deep one can overflow
1901 // the stack. Track stack depth, and error out if needed.
1902 class DepthTracker {
1903 public:
Lee Thomason70d942e2018-06-29 15:31:59 -07001904 DepthTracker(XMLDocument * document) {
1905 this->_document = document;
Lee Thomasond946dda2018-04-05 09:11:08 -07001906 document->PushDepth();
1907 }
1908 ~DepthTracker() {
1909 _document->PopDepth();
1910 }
1911 private:
1912 XMLDocument * _document;
1913 };
Lee Thomasonf928c352018-04-05 09:24:20 -07001914 void PushDepth();
1915 void PopDepth();
Lee Thomasond946dda2018-04-05 09:11:08 -07001916
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001917 template<class NodeType, int PoolElementSize>
1918 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
Lee Thomason5cae8972012-01-24 18:03:07 -08001919};
1920
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001921template<class NodeType, int PoolElementSize>
1922inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1923{
1924 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1925 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1926 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1927 TIXMLASSERT( returnNode );
1928 returnNode->_memPool = &pool;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001929
1930 _unlinked.Push(returnNode);
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001931 return returnNode;
1932}
Lee Thomason7c913cd2012-01-26 18:32:34 -08001933
Lee Thomason3ffdd392012-03-28 17:27:55 -07001934/**
1935 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001936 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001937 DOM structure. It is a separate utility class.
1938
1939 Take an example:
1940 @verbatim
1941 <Document>
1942 <Element attributeA = "valueA">
1943 <Child attributeB = "value1" />
1944 <Child attributeB = "value2" />
1945 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001946 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001947 @endverbatim
1948
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001949 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001950 easy to write a *lot* of code that looks like:
1951
1952 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001953 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001954 if ( root )
1955 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001956 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001957 if ( element )
1958 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001959 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001960 if ( child )
1961 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001962 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001963 if ( child2 )
1964 {
1965 // Finally do something useful.
1966 @endverbatim
1967
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001968 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001969 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001970 and correct to use:
1971
1972 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001973 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001974 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001975 if ( child2 )
1976 {
1977 // do something useful
1978 @endverbatim
1979
1980 Which is MUCH more concise and useful.
1981
1982 It is also safe to copy handles - internally they are nothing more than node pointers.
1983 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001984 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001985 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001986
1987 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001988*/
PKEuS16ed47d2013-07-06 12:02:43 +02001989class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001990{
1991public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001992 /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001993 XMLHandle( XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001994 }
1995 /// Create a handle from a node.
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001996 XMLHandle( XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001997 }
1998 /// Copy constructor
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001999 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002000 }
2001 /// Assignment
2002 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002003 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002004 return *this;
2005 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002006
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002007 /// Get the first child of this handle.
2008 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002009 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002010 }
2011 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002012 XMLHandle FirstChildElement( const char* name = 0 ) {
2013 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002014 }
2015 /// Get the last child of this handle.
2016 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002017 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002018 }
2019 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002020 XMLHandle LastChildElement( const char* name = 0 ) {
2021 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002022 }
2023 /// Get the previous sibling of this handle.
2024 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002025 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002026 }
2027 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002028 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
2029 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002030 }
2031 /// Get the next sibling of this handle.
2032 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002033 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002034 }
2035 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002036 XMLHandle NextSiblingElement( const char* name = 0 ) {
2037 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002038 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002039
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002040 /// Safe cast to XMLNode. This can return null.
2041 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002042 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002043 }
2044 /// Safe cast to XMLElement. This can return null.
2045 XMLElement* ToElement() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002046 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002047 }
2048 /// Safe cast to XMLText. This can return null.
2049 XMLText* ToText() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002050 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002051 }
2052 /// Safe cast to XMLUnknown. This can return null.
2053 XMLUnknown* ToUnknown() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002054 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002055 }
2056 /// Safe cast to XMLDeclaration. This can return null.
2057 XMLDeclaration* ToDeclaration() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002058 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002059 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002060
2061private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002062 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002063};
2064
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08002065
2066/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002067 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
2068 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07002069*/
PKEuS16ed47d2013-07-06 12:02:43 +02002070class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07002071{
2072public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002073 XMLConstHandle( const XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002074 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002075 XMLConstHandle( const XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002076 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002077 XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002078 }
Lee Thomason8b899812012-04-04 15:58:16 -07002079
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002080 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002081 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002082 return *this;
2083 }
Lee Thomason8b899812012-04-04 15:58:16 -07002084
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002085 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002086 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002087 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002088 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2089 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002090 }
2091 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002092 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002093 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002094 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2095 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002096 }
2097 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002098 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002099 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002100 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2101 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002102 }
2103 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002104 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002105 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002106 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2107 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002108 }
Lee Thomason8b899812012-04-04 15:58:16 -07002109
2110
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002111 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002112 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002113 }
2114 const XMLElement* ToElement() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002115 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002116 }
2117 const XMLText* ToText() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002118 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002119 }
2120 const XMLUnknown* ToUnknown() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002121 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002122 }
2123 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002124 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002125 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08002126
Lee Thomason5cae8972012-01-24 18:03:07 -08002127private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002128 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08002129};
Lee Thomason6f381b72012-03-02 12:59:39 -08002130
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002131
2132/**
2133 Printing functionality. The XMLPrinter gives you more
2134 options than the XMLDocument::Print() method.
2135
2136 It can:
2137 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02002138 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002139 -# Print XML without a XMLDocument.
2140
2141 Print to Memory
2142
2143 @verbatim
2144 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06002145 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02002146 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002147 @endverbatim
2148
2149 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002150
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002151 You provide the file pointer.
2152 @verbatim
2153 XMLPrinter printer( fp );
2154 doc.Print( &printer );
2155 @endverbatim
2156
2157 Print without a XMLDocument
2158
2159 When loading, an XML parser is very useful. However, sometimes
2160 when saving, it just gets in the way. The code is often set up
2161 for streaming, and constructing the DOM is just overhead.
2162
2163 The Printer supports the streaming case. The following code
2164 prints out a trivially simple XML file without ever creating
2165 an XML document.
2166
2167 @verbatim
2168 XMLPrinter printer( fp );
2169 printer.OpenElement( "foo" );
2170 printer.PushAttribute( "foo", "bar" );
2171 printer.CloseElement();
2172 @endverbatim
2173*/
PKEuS16ed47d2013-07-06 12:02:43 +02002174class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002175{
2176public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002177 /** Construct the printer. If the FILE* is specified,
2178 this will print to the FILE. Else it will print
2179 to memory, and the result is available in CStr().
2180 If 'compact' is set to true, then output is created
2181 with only required whitespace and newlines.
2182 */
PKEuS1bfb9542013-08-04 13:51:17 +02002183 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002184 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002185
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002186 /** If streaming, write the BOM and declaration. */
2187 void PushHeader( bool writeBOM, bool writeDeclaration );
2188 /** If streaming, start writing an element.
2189 The element must be closed with CloseElement()
2190 */
Lee Thomason256adb62014-04-06 14:41:46 -07002191 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002192 /// If streaming, add an attribute to an open element.
2193 void PushAttribute( const char* name, const char* value );
2194 void PushAttribute( const char* name, int value );
2195 void PushAttribute( const char* name, unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002196 void PushAttribute(const char* name, int64_t value);
2197 void PushAttribute( const char* name, bool value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002198 void PushAttribute( const char* name, double value );
2199 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002200 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002201
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002202 /// Add a text node.
2203 void PushText( const char* text, bool cdata=false );
2204 /// Add a text node from an integer.
2205 void PushText( int value );
2206 /// Add a text node from an unsigned.
2207 void PushText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002208 /// Add a text node from an unsigned.
2209 void PushText(int64_t value);
2210 /// Add a text node from a bool.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002211 void PushText( bool value );
2212 /// Add a text node from a float.
2213 void PushText( float value );
2214 /// Add a text node from a double.
2215 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002216
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002217 /// Add a comment
2218 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002219
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002220 void PushDeclaration( const char* value );
2221 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002222
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002223 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2224 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2225 return true;
2226 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002227
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002228 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2229 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002230
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002231 virtual bool Visit( const XMLText& text );
2232 virtual bool Visit( const XMLComment& comment );
2233 virtual bool Visit( const XMLDeclaration& declaration );
2234 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002235
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002236 /**
2237 If in print to memory mode, return a pointer to
2238 the XML file in memory.
2239 */
2240 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002241 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002242 }
2243 /**
2244 If in print to memory mode, return the size
2245 of the XML file in memory. (Note the size returned
2246 includes the terminating null.)
2247 */
2248 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002249 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002250 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002251 /**
2252 If in print to memory mode, reset the buffer to the
2253 beginning.
2254 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002255 void ClearBuffer() {
2256 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002257 _buffer.Push(0);
Lee Thomason53858b42017-06-01 19:09:16 -07002258 _firstElement = true;
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002259 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002260
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002261protected:
Alexander Maid740b642014-05-20 22:04:42 +02002262 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002263
Lee Thomasonc18eb232014-02-21 17:31:17 -08002264 /** Prints out the space before an element. You may override to change
2265 the space and tabs used. A PrintSpace() override should call Print().
2266 */
2267 virtual void PrintSpace( int depth );
2268 void Print( const char* format, ... );
Alexander Golubevb2e08e42016-03-28 19:51:59 +03002269 void Write( const char* data, size_t size );
2270 inline void Write( const char* data ) { Write( data, strlen( data ) ); }
2271 void Putc( char ch );
Lee Thomasonc18eb232014-02-21 17:31:17 -08002272
Dmitry-Mea092bc12014-12-23 17:57:05 +03002273 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002274 bool _elementJustOpened;
2275 DynArray< const char*, 10 > _stack;
2276
2277private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002278 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002279
Lee Thomason624d43f2012-10-12 10:58:48 -07002280 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002281 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002282 int _depth;
2283 int _textDepth;
2284 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002285 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002286
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002287 enum {
2288 ENTITY_RANGE = 64,
2289 BUF_SIZE = 200
2290 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002291 bool _entityFlag[ENTITY_RANGE];
2292 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002293
Lee Thomason624d43f2012-10-12 10:58:48 -07002294 DynArray< char, 20 > _buffer;
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002295
2296 // Prohibit cloning, intentionally not implemented
2297 XMLPrinter( const XMLPrinter& );
2298 XMLPrinter& operator=( const XMLPrinter& );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002299};
2300
2301
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002302} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002303
PKEuS95060352013-07-26 10:42:44 +02002304#if defined(_MSC_VER)
2305# pragma warning(pop)
2306#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002307
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002308#endif // TINYXML2_INCLUDED