blob: 771332c14cd866777e512704af0962773cc947a1 [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 Thomasonbf152332020-03-01 17:40:07 -0800101static const int TIXML2_MAJOR_VERSION = 8;
102static const int TIXML2_MINOR_VERSION = 0;
Lee Thomason9c740e82019-08-10 18:05:22 -0700103static const int TIXML2_PATCH_VERSION = 0;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800104
Lee Thomasonbf152332020-03-01 17:40:07 -0800105#define TINYXML2_MAJOR_VERSION 8
106#define TINYXML2_MINOR_VERSION 0
Lee Thomason9c740e82019-08-10 18:05:22 -0700107#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[]
Lee Thomasonde6d1642018-11-17 19:53:46 -0800132
133 Isn't clear why TINYXML2_LIB is needed; but seems to fix #719
U-Stream\Leeae25a442012-02-17 17:48:16 -0800134*/
Lee Thomasonde6d1642018-11-17 19:53:46 -0800135class TINYXML2_LIB StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800136{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800137public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700138 enum {
139 NEEDS_ENTITY_PROCESSING = 0x01,
140 NEEDS_NEWLINE_NORMALIZATION = 0x02,
Dmitry-Me5420e542015-05-20 10:51:26 +0300141 NEEDS_WHITESPACE_COLLAPSING = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800142
Lee Thomason584af572016-09-05 14:14:16 -0700143 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700144 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
Lee Thomason584af572016-09-05 14:14:16 -0700145 ATTRIBUTE_NAME = 0,
146 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
147 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
148 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700149 };
Lee Thomason39ede242012-01-20 11:27:56 -0800150
Lee Thomason120b3a62012-10-12 10:06:59 -0700151 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700152 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800153
Lee Thomason120b3a62012-10-12 10:06:59 -0700154 void Set( char* start, char* end, int flags ) {
Dmitry-Me6fc38ec2016-09-01 17:59:44 +0300155 TIXMLASSERT( start );
156 TIXMLASSERT( end );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700157 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700158 _start = start;
159 _end = end;
160 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700161 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700162
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700163 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700164
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700165 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700166 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700167 }
Lee Thomason39ede242012-01-20 11:27:56 -0800168
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700169 void SetInternedStr( const char* str ) {
170 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700171 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700172 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700173
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700174 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800175
kezenator4f756162016-11-29 19:46:27 +1000176 char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700177 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800178
Lee Thomason29658802014-11-27 22:31:11 -0800179 void TransferTo( StrPair* other );
Lee Thomason584af572016-09-05 14:14:16 -0700180 void Reset();
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300181
Lee Thomason39ede242012-01-20 11:27:56 -0800182private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700183 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800184
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700185 enum {
186 NEEDS_FLUSH = 0x100,
187 NEEDS_DELETE = 0x200
188 };
Lee Thomasone4422302012-01-20 17:59:50 -0800189
Lee Thomason120b3a62012-10-12 10:06:59 -0700190 int _flags;
191 char* _start;
192 char* _end;
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300193
194 StrPair( const StrPair& other ); // not supported
Bo Rydberg65c1b862018-10-19 18:45:53 +0200195 void operator=( const StrPair& other ); // not supported, use TransferTo()
Lee Thomason39ede242012-01-20 11:27:56 -0800196};
197
U-Lama\Lee560bd472011-12-28 19:42:49 -0800198
U-Stream\Leeae25a442012-02-17 17:48:16 -0800199/*
200 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
201 Has a small initial memory pool, so that low or no usage will not
202 cause a call to new/delete
203*/
Dmitry-Me04009222015-04-06 18:07:18 +0300204template <class T, int INITIAL_SIZE>
PKEuS95060352013-07-26 10:42:44 +0200205class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800206{
207public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200208 DynArray() :
209 _mem( _pool ),
210 _allocated( INITIAL_SIZE ),
211 _size( 0 )
212 {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700213 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700214
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700215 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700216 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700217 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700218 }
219 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700220
Lee Thomasonce0510b2013-11-26 21:29:37 -0800221 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100222 _size = 0;
223 }
224
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700225 void Push( T t ) {
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300226 TIXMLASSERT( _size < INT_MAX );
Lee Thomason624d43f2012-10-12 10:58:48 -0700227 EnsureCapacity( _size+1 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300228 _mem[_size] = t;
229 ++_size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700230 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800231
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700232 T* PushArr( int count ) {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300233 TIXMLASSERT( count >= 0 );
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300234 TIXMLASSERT( _size <= INT_MAX - count );
Lee Thomason624d43f2012-10-12 10:58:48 -0700235 EnsureCapacity( _size+count );
236 T* ret = &_mem[_size];
237 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700238 return ret;
239 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700240
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700241 T Pop() {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300242 TIXMLASSERT( _size > 0 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300243 --_size;
244 return _mem[_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700245 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700246
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700247 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700248 TIXMLASSERT( _size >= count );
249 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700250 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800251
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700252 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700253 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700254 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700255
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700256 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700257 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700258 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700259 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700260
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700261 const T& operator[](int i) const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700262 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700263 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700264 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700265
Lee Thomasonf07b9522014-10-30 13:25:12 -0700266 const T& PeekTop() const {
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500267 TIXMLASSERT( _size > 0 );
268 return _mem[ _size - 1];
269 }
270
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700271 int Size() const {
Dmitry-Me30bdc972015-01-14 08:32:23 +0300272 TIXMLASSERT( _size >= 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700273 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700274 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700275
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700276 int Capacity() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300277 TIXMLASSERT( _allocated >= INITIAL_SIZE );
Lee Thomason624d43f2012-10-12 10:58:48 -0700278 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700279 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700280
Lee Thomason816d3fa2017-06-05 14:35:55 -0700281 void SwapRemove(int i) {
Dmitry-Mec2f677b2017-06-15 12:44:27 +0300282 TIXMLASSERT(i >= 0 && i < _size);
283 TIXMLASSERT(_size > 0);
Lee Thomason816d3fa2017-06-05 14:35:55 -0700284 _mem[i] = _mem[_size - 1];
285 --_size;
286 }
287
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700288 const T* Mem() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300289 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700290 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700291 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700292
orbitcowboy2cc8a4c2018-09-05 08:46:21 +0200293 T* Mem() {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300294 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700295 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700296 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800297
Lee Thomason2c85a712012-01-31 08:24:24 -0800298private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300299 DynArray( const DynArray& ); // not supported
300 void operator=( const DynArray& ); // not supported
301
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700302 void EnsureCapacity( int cap ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300303 TIXMLASSERT( cap > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700304 if ( cap > _allocated ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300305 TIXMLASSERT( cap <= INT_MAX / 2 );
orbitcowboy0e7f2892019-01-15 11:28:49 +0100306 const int newAllocated = cap * 2;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700307 T* newMem = new T[newAllocated];
Dmitry-Me243ddf52017-05-18 17:27:14 +0300308 TIXMLASSERT( newAllocated >= _size );
Lee Thomason624d43f2012-10-12 10:58:48 -0700309 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
310 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700311 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700312 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700313 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700314 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700315 }
316 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800317
Lee Thomason624d43f2012-10-12 10:58:48 -0700318 T* _mem;
Dmitry-Me04009222015-04-06 18:07:18 +0300319 T _pool[INITIAL_SIZE];
Lee Thomason624d43f2012-10-12 10:58:48 -0700320 int _allocated; // objects allocated
321 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800322};
323
Lee Thomason50adb4c2012-02-13 15:07:09 -0800324
U-Stream\Leeae25a442012-02-17 17:48:16 -0800325/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200326 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800327 and deallocation of objects.
328*/
PKEuS95060352013-07-26 10:42:44 +0200329class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800330{
331public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700332 MemPool() {}
333 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800334
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700335 virtual int ItemSize() const = 0;
336 virtual void* Alloc() = 0;
337 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800338 virtual void SetTracked() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800339};
340
Lee Thomason50adb4c2012-02-13 15:07:09 -0800341
U-Stream\Leeae25a442012-02-17 17:48:16 -0800342/*
343 Template child class to create pools of the correct type.
344*/
Dmitry-Me88145b82016-08-09 17:59:31 +0300345template< int ITEM_SIZE >
PKEuS95060352013-07-26 10:42:44 +0200346class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800347{
348public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200349 MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700350 ~MemPoolT() {
Lee Thomasonc07409b2018-08-25 11:04:13 -0700351 MemPoolT< ITEM_SIZE >::Clear();
Lee Thomasonf07b9522014-10-30 13:25:12 -0700352 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700353
Lee Thomasonf07b9522014-10-30 13:25:12 -0700354 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700355 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700356 while( !_blockPtrs.Empty()) {
Dmitry-Meeffab6f2017-07-26 17:57:50 +0300357 Block* lastBlock = _blockPtrs.Pop();
358 delete lastBlock;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700359 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700360 _root = 0;
361 _currentAllocs = 0;
362 _nAllocs = 0;
363 _maxAllocs = 0;
364 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700365 }
Lee Thomasond1983222012-02-06 08:41:24 -0800366
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700367 virtual int ItemSize() const {
Dmitry-Me88145b82016-08-09 17:59:31 +0300368 return ITEM_SIZE;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700369 }
370 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700371 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700372 }
Lee Thomasond1983222012-02-06 08:41:24 -0800373
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700374 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700375 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700376 // Need a new block.
377 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700378 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800379
Dmitry-Me88145b82016-08-09 17:59:31 +0300380 Item* blockItems = block->items;
381 for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
382 blockItems[i].next = &(blockItems[i + 1]);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700383 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300384 blockItems[ITEMS_PER_BLOCK - 1].next = 0;
385 _root = blockItems;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700386 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300387 Item* const result = _root;
388 TIXMLASSERT( result != 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700389 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800390
Lee Thomason624d43f2012-10-12 10:58:48 -0700391 ++_currentAllocs;
392 if ( _currentAllocs > _maxAllocs ) {
393 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700394 }
Dmitry-Me3161a332016-09-02 16:58:06 +0300395 ++_nAllocs;
396 ++_nUntracked;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700397 return result;
398 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700399
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700400 virtual void Free( void* mem ) {
401 if ( !mem ) {
402 return;
403 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700404 --_currentAllocs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300405 Item* item = static_cast<Item*>( mem );
Peter Matula50689912018-01-09 12:52:26 +0100406#ifdef TINYXML2_DEBUG
Dmitry-Me88145b82016-08-09 17:59:31 +0300407 memset( item, 0xfe, sizeof( *item ) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700408#endif
Dmitry-Me88145b82016-08-09 17:59:31 +0300409 item->next = _root;
410 _root = item;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700411 }
412 void Trace( const char* name ) {
413 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Dmitry-Me88145b82016-08-09 17:59:31 +0300414 name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
415 ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700416 }
Lee Thomasond1983222012-02-06 08:41:24 -0800417
Lee Thomason5b0a6772012-11-19 13:54:42 -0800418 void SetTracked() {
Dmitry-Me3161a332016-09-02 16:58:06 +0300419 --_nUntracked;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800420 }
421
422 int Untracked() const {
423 return _nUntracked;
424 }
425
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800426 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
427 // The test file is large, 170k.
428 // Release: VS2010 gcc(no opt)
429 // 1k: 4000
430 // 2k: 4000
431 // 4k: 3900 21000
432 // 16k: 5200
433 // 32k: 4300
434 // 64k: 4000 21000
Dmitry-Me88145b82016-08-09 17:59:31 +0300435 // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
436 // in private part if ITEMS_PER_BLOCK is private
437 enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
Jerome Martinez7921df12012-10-24 11:45:44 +0200438
Lee Thomasond1983222012-02-06 08:41:24 -0800439private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300440 MemPoolT( const MemPoolT& ); // not supported
441 void operator=( const MemPoolT& ); // not supported
442
Dmitry-Me88145b82016-08-09 17:59:31 +0300443 union Item {
444 Item* next;
445 char itemData[ITEM_SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700446 };
447 struct Block {
Dmitry-Me88145b82016-08-09 17:59:31 +0300448 Item items[ITEMS_PER_BLOCK];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700449 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700450 DynArray< Block*, 10 > _blockPtrs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300451 Item* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800452
Lee Thomason624d43f2012-10-12 10:58:48 -0700453 int _currentAllocs;
454 int _nAllocs;
455 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800456 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800457};
458
Lee Thomason2c85a712012-01-31 08:24:24 -0800459
Lee Thomason56bdd022012-02-09 18:16:58 -0800460
461/**
462 Implements the interface to the "Visitor pattern" (see the Accept() method.)
463 If you call the Accept() method, it requires being passed a XMLVisitor
464 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200465 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800466 are simply called with Visit().
467
468 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700469 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800470
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700471 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800472 visiting). You need to only override methods that are interesting to you.
473
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600474 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800475
476 You should never change the document from a callback.
477
478 @sa XMLNode::Accept()
479*/
PKEuS16ed47d2013-07-06 12:02:43 +0200480class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800481{
482public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700483 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800484
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700485 /// Visit a document.
486 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
487 return true;
488 }
489 /// Visit a document.
490 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
491 return true;
492 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800493
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700494 /// Visit an element.
495 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
496 return true;
497 }
498 /// Visit an element.
499 virtual bool VisitExit( const XMLElement& /*element*/ ) {
500 return true;
501 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800502
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700503 /// Visit a declaration.
504 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
505 return true;
506 }
507 /// Visit a text node.
508 virtual bool Visit( const XMLText& /*text*/ ) {
509 return true;
510 }
511 /// Visit a comment node.
512 virtual bool Visit( const XMLComment& /*comment*/ ) {
513 return true;
514 }
515 /// Visit an unknown node.
516 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
517 return true;
518 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800519};
520
Dmitry-Me66d2a842014-11-08 15:24:52 +0300521// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900522enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900523 XML_SUCCESS = 0,
numatrumpetcd8550c2014-09-08 16:59:39 +0900524 XML_NO_ATTRIBUTE,
525 XML_WRONG_ATTRIBUTE_TYPE,
526 XML_ERROR_FILE_NOT_FOUND,
527 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
528 XML_ERROR_FILE_READ_ERROR,
numatrumpetcd8550c2014-09-08 16:59:39 +0900529 XML_ERROR_PARSING_ELEMENT,
530 XML_ERROR_PARSING_ATTRIBUTE,
numatrumpetcd8550c2014-09-08 16:59:39 +0900531 XML_ERROR_PARSING_TEXT,
532 XML_ERROR_PARSING_CDATA,
533 XML_ERROR_PARSING_COMMENT,
534 XML_ERROR_PARSING_DECLARATION,
535 XML_ERROR_PARSING_UNKNOWN,
536 XML_ERROR_EMPTY_DOCUMENT,
537 XML_ERROR_MISMATCHED_ELEMENT,
538 XML_ERROR_PARSING,
539 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700540 XML_NO_TEXT_NODE,
Lee Thomasond946dda2018-04-05 09:11:08 -0700541 XML_ELEMENT_DEPTH_EXCEEDED,
Lee Thomason331596e2014-09-11 14:56:43 -0700542
543 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900544};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900545
numatrumpetcd8550c2014-09-08 16:59:39 +0900546
U-Stream\Leeae25a442012-02-17 17:48:16 -0800547/*
548 Utility functionality.
549*/
Lee Thomasonce667c92016-12-26 16:45:30 -0800550class TINYXML2_LIB XMLUtil
Lee Thomason56bdd022012-02-09 18:16:58 -0800551{
Lee Thomasond1983222012-02-06 08:41:24 -0800552public:
kezenator4f756162016-11-29 19:46:27 +1000553 static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300554 TIXMLASSERT( p );
Lee Thomasone90e9012016-12-24 07:34:39 -0800555
Dmitry-Mefa20b222014-10-31 12:53:04 +0300556 while( IsWhiteSpace(*p) ) {
Lee Thomasone90e9012016-12-24 07:34:39 -0800557 if (curLineNumPtr && *p == '\n') {
kezenator4f756162016-11-29 19:46:27 +1000558 ++(*curLineNumPtr);
kezenatorec694152016-11-26 17:21:43 +1000559 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700560 ++p;
561 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300562 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700563 return p;
564 }
orbitcowboy73f54092019-08-14 09:30:30 +0200565 static char* SkipWhiteSpace( char* const p, int* curLineNumPtr ) {
kezenator4f756162016-11-29 19:46:27 +1000566 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700567 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300568
569 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
570 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700571 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100572 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700573 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700574
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200575 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300576 if ( ch >= 128 ) {
577 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
578 return true;
579 }
580 if ( isalpha( ch ) ) {
581 return true;
582 }
583 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200584 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700585
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200586 inline static bool IsNameChar( unsigned char ch ) {
587 return IsNameStartChar( ch )
588 || isdigit( ch )
589 || ch == '.'
590 || ch == '-';
591 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800592
netcandfb45cb2020-02-15 21:35:58 +0800593 inline static bool IsPrefixHex( const char* p) {
Lee Thomason18468b82020-06-13 17:35:21 -0700594 p = SkipWhiteSpace(p, 0);
595 return p && *p == '0' && ( *(p + 1) == 'x' || *(p + 1) == 'X');
netcandfb45cb2020-02-15 21:35:58 +0800596 }
597
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700598 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700599 if ( p == q ) {
600 return true;
601 }
Dmitry-Me21f99692016-10-03 13:01:57 +0300602 TIXMLASSERT( p );
603 TIXMLASSERT( q );
604 TIXMLASSERT( nChar >= 0 );
Lee Thomason598a88d2015-10-09 14:42:12 -0700605 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700606 }
Lee Thomason70d942e2018-06-29 15:31:59 -0700607
orbitcowboy73f54092019-08-14 09:30:30 +0200608 inline static bool IsUTF8Continuation( const char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400609 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700610 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800611
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700612 static const char* ReadBOM( const char* p, bool* hasBOM );
613 // p is the starting location,
614 // the UTF-8 value of the entity will be placed in value, and length filled in.
615 static const char* GetCharacterRef( const char* p, char* value, int* length );
616 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700617
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700618 // converts primitive types to strings
619 static void ToStr( int v, char* buffer, int bufferSize );
620 static void ToStr( unsigned v, char* buffer, int bufferSize );
621 static void ToStr( bool v, char* buffer, int bufferSize );
622 static void ToStr( float v, char* buffer, int bufferSize );
623 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason51c12712016-06-04 20:18:49 -0700624 static void ToStr(int64_t v, char* buffer, int bufferSize);
cugone47e229e2019-04-12 17:27:28 -0500625 static void ToStr(uint64_t v, char* buffer, int bufferSize);
Lee Thomason21be8822012-07-15 17:27:22 -0700626
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700627 // converts strings to primitive types
628 static bool ToInt( const char* str, int* value );
629 static bool ToUnsigned( const char* str, unsigned* value );
630 static bool ToBool( const char* str, bool* value );
631 static bool ToFloat( const char* str, float* value );
632 static bool ToDouble( const char* str, double* value );
Lee Thomason51c12712016-06-04 20:18:49 -0700633 static bool ToInt64(const char* str, int64_t* value);
cugone75a5acc2019-04-11 22:03:43 -0500634 static bool ToUnsigned64(const char* str, uint64_t* value);
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800635 // Changes what is serialized for a boolean value.
636 // Default to "true" and "false". Shouldn't be changed
637 // unless you have a special testing or compatibility need.
Lee Thomasonce667c92016-12-26 16:45:30 -0800638 // Be careful: static, global, & not thread safe.
639 // Be sure to set static const memory as parameters.
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800640 static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
Lee Thomasonce667c92016-12-26 16:45:30 -0800641
642private:
Lee Thomasonf458d262016-12-26 22:47:25 -0800643 static const char* writeBoolTrue;
644 static const char* writeBoolFalse;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800645};
646
Lee Thomason5cae8972012-01-24 18:03:07 -0800647
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800648/** XMLNode is a base class for every object that is in the
649 XML Document Object Model (DOM), except XMLAttributes.
650 Nodes have siblings, a parent, and children which can
651 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700652 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800653 be cast to its more defined type.
654
Thomas Roß08bdf502012-05-12 14:21:23 +0200655 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800656 When the XMLDocument gets deleted, all its Nodes
657 will also be deleted.
658
659 @verbatim
660 A Document can contain: Element (container or leaf)
661 Comment (leaf)
662 Unknown (leaf)
663 Declaration( leaf )
664
665 An Element can contain: Element (container or leaf)
666 Text (leaf)
667 Attributes (not on tree)
668 Comment (leaf)
669 Unknown (leaf)
670
671 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800672*/
PKEuS16ed47d2013-07-06 12:02:43 +0200673class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800674{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700675 friend class XMLDocument;
676 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800677public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800678
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700679 /// Get the XMLDocument that owns this XMLNode.
680 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300681 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700682 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700683 }
684 /// Get the XMLDocument that owns this XMLNode.
685 XMLDocument* GetDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300686 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700687 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700688 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800689
Lee Thomason2fa81722012-11-09 12:37:46 -0800690 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700691 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100692 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700693 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800694 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700695 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100696 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700697 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800698 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700699 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100700 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700701 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800702 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700703 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100704 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700705 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800706 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700707 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100708 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700709 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800710 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700711 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100712 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700713 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800714
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700715 virtual const XMLElement* ToElement() const {
716 return 0;
717 }
718 virtual const XMLText* ToText() const {
719 return 0;
720 }
721 virtual const XMLComment* ToComment() const {
722 return 0;
723 }
724 virtual const XMLDocument* ToDocument() const {
725 return 0;
726 }
727 virtual const XMLDeclaration* ToDeclaration() const {
728 return 0;
729 }
730 virtual const XMLUnknown* ToUnknown() const {
731 return 0;
732 }
Lee Thomason751da522012-02-10 08:50:51 -0800733
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700734 /** The meaning of 'value' changes for the specific type.
735 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530736 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700737 Element: name of the element
738 Comment: the comment text
739 Unknown: the tag contents
740 Text: the text string
741 @endverbatim
742 */
Michael Daumling21626882013-10-22 17:03:37 +0200743 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100744
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700745 /** Set the Value of an XML node.
746 @sa Value()
747 */
748 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800749
kezenatorec694152016-11-26 17:21:43 +1000750 /// Gets the line number the node is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +1000751 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +1000752
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700753 /// Get the parent of this node on the DOM.
754 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700755 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700756 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100757
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700758 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700759 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700760 }
Lee Thomason751da522012-02-10 08:50:51 -0800761
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700762 /// Returns true if this node has no children.
763 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700764 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700765 }
Lee Thomason751da522012-02-10 08:50:51 -0800766
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700767 /// Get the first child node, or null if none exists.
768 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700769 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700770 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100771
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700772 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700773 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700774 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100775
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700776 /** Get the first child element, or optionally the first child
777 element with the specified name.
778 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300779 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700780
Dmitry-Me886ad972015-07-22 11:00:51 +0300781 XMLElement* FirstChildElement( const char* name = 0 ) {
782 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700783 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800784
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700785 /// Get the last child node, or null if none exists.
786 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700787 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700788 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700789
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700790 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300791 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700792 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800793
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700794 /** Get the last child element or optionally the last child
795 element with the specified name.
796 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300797 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700798
Dmitry-Me886ad972015-07-22 11:00:51 +0300799 XMLElement* LastChildElement( const char* name = 0 ) {
800 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700801 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700802
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700803 /// Get the previous (left) sibling node of this node.
804 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700805 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700806 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700807
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700808 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700809 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700810 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800811
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700812 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300813 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700814
Dmitry-Me886ad972015-07-22 11:00:51 +0300815 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
816 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700817 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700818
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700819 /// Get the next (right) sibling node of this node.
820 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700821 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700822 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700823
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700824 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700825 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700826 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700827
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700828 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300829 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700830
Dmitry-Me886ad972015-07-22 11:00:51 +0300831 XMLElement* NextSiblingElement( const char* name = 0 ) {
832 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700833 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800834
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700835 /**
836 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200837 If the child node is already part of the document,
838 it is moved from its old location to the new location.
839 Returns the addThis argument or 0 if the node does not
840 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700841 */
842 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800843
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700844 XMLNode* LinkEndChild( XMLNode* addThis ) {
845 return InsertEndChild( addThis );
846 }
847 /**
848 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200849 If the child node is already part of the document,
850 it is moved from its old location to the new location.
851 Returns the addThis argument or 0 if the node does not
852 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700853 */
854 XMLNode* InsertFirstChild( XMLNode* addThis );
855 /**
856 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200857 If the child node is already part of the document,
858 it is moved from its old location to the new location.
859 Returns the addThis argument or 0 if the afterThis node
860 is not a child of this node, or if the node does not
861 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700862 */
863 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700864
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700865 /**
866 Delete all the children of this node.
867 */
868 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800869
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700870 /**
871 Delete a child of this node.
872 */
873 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800874
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700875 /**
876 Make a copy of this node, but not its children.
877 You may pass in a Document pointer that will be
878 the owner of the new Node. If the 'document' is
879 null, then the node returned will be allocated
880 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800881
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700882 Note: if called on a XMLDocument, this will return null.
883 */
884 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800885
Lee Thomason7085f002017-06-01 18:09:43 -0700886 /**
Lee Thomason1346a172017-06-14 15:14:19 -0700887 Make a copy of this node and all its children.
Lee Thomason7085f002017-06-01 18:09:43 -0700888
Dmitry-Me3f63f212017-06-19 18:25:19 +0300889 If the 'target' is null, then the nodes will
Lee Thomason70d942e2018-06-29 15:31:59 -0700890 be allocated in the current document. If 'target'
891 is specified, the memory will be allocated is the
Lee Thomason1346a172017-06-14 15:14:19 -0700892 specified XMLDocument.
Lee Thomason7085f002017-06-01 18:09:43 -0700893
Lee Thomason70d942e2018-06-29 15:31:59 -0700894 NOTE: This is probably not the correct tool to
Lee Thomason7085f002017-06-01 18:09:43 -0700895 copy a document, since XMLDocuments can have multiple
Lee Thomason1346a172017-06-14 15:14:19 -0700896 top level XMLNodes. You probably want to use
897 XMLDocument::DeepCopy()
Lee Thomason7085f002017-06-01 18:09:43 -0700898 */
Dmitry-Me3f63f212017-06-19 18:25:19 +0300899 XMLNode* DeepClone( XMLDocument* target ) const;
Lee Thomason7085f002017-06-01 18:09:43 -0700900
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700901 /**
902 Test if 2 nodes are the same, but don't test children.
903 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800904
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700905 Note: if called on a XMLDocument, this will return false.
906 */
907 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800908
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600909 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700910 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600911 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800912
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600913 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
914 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700915 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800916
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700917 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800918
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700919 - http://www.saxproject.org/
920 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800921
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700922 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800923
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700924 An example of using Accept():
925 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600926 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700927 tinyxmlDoc.Accept( &printer );
928 const char* xmlcstr = printer.CStr();
929 @endverbatim
930 */
931 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800932
Lee Thomason70d942e2018-06-29 15:31:59 -0700933 /**
934 Set user data into the XMLNode. TinyXML-2 in
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700935 no way processes or interprets user data.
936 It is initially 0.
937 */
938 void SetUserData(void* userData) { _userData = userData; }
939
940 /**
941 Get user data set into the XMLNode. TinyXML-2 in
942 no way processes or interprets user data.
943 It is initially 0.
944 */
945 void* GetUserData() const { return _userData; }
946
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800947protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +0200948 explicit XMLNode( XMLDocument* );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700949 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700950
Dmitry-Me10b8ecc2017-04-12 17:57:44 +0300951 virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300952
Lee Thomason624d43f2012-10-12 10:58:48 -0700953 XMLDocument* _document;
954 XMLNode* _parent;
955 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +1000956 int _parseLineNum;
Lee Thomason3f57d272012-01-11 15:30:03 -0800957
Lee Thomason624d43f2012-10-12 10:58:48 -0700958 XMLNode* _firstChild;
959 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800960
Lee Thomason624d43f2012-10-12 10:58:48 -0700961 XMLNode* _prev;
962 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800963
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700964 void* _userData;
965
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800966private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700967 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700968 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400969 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800970 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Meecb9b072016-10-12 16:44:59 +0300971 const XMLElement* ToElementWithName( const char* name ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300972
973 XMLNode( const XMLNode& ); // not supported
974 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800975};
976
977
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800978/** XML text.
979
980 Note that a text node can have child element nodes, for example:
981 @verbatim
982 <root>This is <b>bold</b></root>
983 @endverbatim
984
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700985 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800986 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 -0700987 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600988 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800989*/
PKEuS16ed47d2013-07-06 12:02:43 +0200990class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800991{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700992 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800993public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700994 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800995
Lee Thomason624d43f2012-10-12 10:58:48 -0700996 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700997 return this;
998 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700999 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001000 return this;
1001 }
Lee Thomason5492a1c2012-01-23 15:32:10 -08001002
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001003 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -07001004 void SetCData( bool isCData ) {
1005 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001006 }
1007 /// Returns true if this is a CDATA text element.
1008 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001009 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001010 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001011
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001012 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1013 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001014
Lee Thomason5492a1c2012-01-23 15:32:10 -08001015protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001016 explicit XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001017 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -08001018
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001019 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001020
Lee Thomason5492a1c2012-01-23 15:32:10 -08001021private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001022 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +03001023
1024 XMLText( const XMLText& ); // not supported
1025 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -08001026};
1027
1028
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001029/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +02001030class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001031{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001032 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -08001033public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001034 virtual XMLComment* ToComment() {
1035 return this;
1036 }
1037 virtual const XMLComment* ToComment() const {
1038 return this;
1039 }
Lee Thomasonce0763e2012-01-11 15:43:54 -08001040
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001041 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001042
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001043 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1044 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -08001045
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001046protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001047 explicit XMLComment( XMLDocument* doc );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001048 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001049
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001050 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001051
Lee Thomason3f57d272012-01-11 15:30:03 -08001052private:
Dmitry-Mef547a992015-01-09 15:17:09 +03001053 XMLComment( const XMLComment& ); // not supported
1054 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001055};
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001056
1057
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001058/** In correct XML the declaration is the first entry in the file.
1059 @verbatim
1060 <?xml version="1.0" standalone="yes"?>
1061 @endverbatim
1062
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001063 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001064 however.
1065
1066 The text of the declaration isn't interpreted. It is parsed
1067 and written as a string.
1068*/
PKEuS16ed47d2013-07-06 12:02:43 +02001069class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001070{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001071 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001072public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001073 virtual XMLDeclaration* ToDeclaration() {
1074 return this;
1075 }
1076 virtual const XMLDeclaration* ToDeclaration() const {
1077 return this;
1078 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001079
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001080 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001081
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001082 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1083 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001084
1085protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001086 explicit XMLDeclaration( XMLDocument* doc );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001087 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +03001088
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001089 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001090
Dmitry-Mef547a992015-01-09 15:17:09 +03001091private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001092 XMLDeclaration( const XMLDeclaration& ); // not supported
1093 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001094};
1095
1096
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001097/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001098 unknown. It is a tag of text, but should not be modified.
1099 It will be written back to the XML, unchanged, when the file
1100 is saved.
1101
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001102 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001103*/
PKEuS16ed47d2013-07-06 12:02:43 +02001104class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001105{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001106 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001107public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001108 virtual XMLUnknown* ToUnknown() {
1109 return this;
1110 }
1111 virtual const XMLUnknown* ToUnknown() const {
1112 return this;
1113 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001114
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001115 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001116
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001117 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1118 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001119
1120protected:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001121 explicit XMLUnknown( XMLDocument* doc );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001122 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001123
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001124 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001125
Dmitry-Mef547a992015-01-09 15:17:09 +03001126private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001127 XMLUnknown( const XMLUnknown& ); // not supported
1128 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001129};
1130
1131
Lee Thomason1ff38e02012-02-14 18:18:16 -08001132
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001133/** An attribute is a name-value pair. Elements have an arbitrary
1134 number of attributes, each with a unique name.
1135
1136 @note The attributes are not XMLNodes. You may only query the
1137 Next() attribute in a list.
1138*/
PKEuS16ed47d2013-07-06 12:02:43 +02001139class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001140{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001141 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001142public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001143 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001144 const char* Name() const;
1145
Lee Thomason2fa81722012-11-09 12:37:46 -08001146 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001147 const char* Value() const;
1148
kezenatorec694152016-11-26 17:21:43 +10001149 /// Gets the line number the attribute is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +10001150 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +10001151
Lee Thomason2fa81722012-11-09 12:37:46 -08001152 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001153 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001154 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001155 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001156
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001157 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001158 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001159 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001160 */
Lee Thomason51c12712016-06-04 20:18:49 -07001161 int IntValue() const {
1162 int i = 0;
1163 QueryIntValue(&i);
1164 return i;
1165 }
1166
1167 int64_t Int64Value() const {
1168 int64_t i = 0;
1169 QueryInt64Value(&i);
1170 return i;
1171 }
1172
cugone75a5acc2019-04-11 22:03:43 -05001173 uint64_t Unsigned64Value() const {
1174 uint64_t i = 0;
1175 QueryUnsigned64Value(&i);
1176 return i;
1177 }
1178
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001179 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001180 unsigned UnsignedValue() const {
1181 unsigned i=0;
1182 QueryUnsignedValue( &i );
1183 return i;
1184 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001185 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001186 bool BoolValue() const {
1187 bool b=false;
1188 QueryBoolValue( &b );
1189 return b;
1190 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001191 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001192 double DoubleValue() const {
1193 double d=0;
1194 QueryDoubleValue( &d );
1195 return d;
1196 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001197 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001198 float FloatValue() const {
1199 float f=0;
1200 QueryFloatValue( &f );
1201 return f;
1202 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001203
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001204 /** QueryIntValue interprets the attribute as an integer, and returns the value
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001205 in the provided parameter. The function will return XML_SUCCESS on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001206 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1207 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001208 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001209 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001210 XMLError QueryUnsignedValue( unsigned int* value ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001211 /// See QueryIntValue
1212 XMLError QueryInt64Value(int64_t* value) const;
cugone75a5acc2019-04-11 22:03:43 -05001213 /// See QueryIntValue
1214 XMLError QueryUnsigned64Value(uint64_t* value) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001215 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001216 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001217 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001218 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001219 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001220 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001221
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001222 /// Set the attribute to a string value.
1223 void SetAttribute( const char* value );
1224 /// Set the attribute to value.
1225 void SetAttribute( int value );
1226 /// Set the attribute to value.
1227 void SetAttribute( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001228 /// Set the attribute to value.
1229 void SetAttribute(int64_t value);
Lee Thomasoneffdf952019-08-10 17:49:42 -07001230 /// Set the attribute to value.
1231 void SetAttribute(uint64_t value);
1232 /// Set the attribute to value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001233 void SetAttribute( bool value );
1234 /// Set the attribute to value.
1235 void SetAttribute( double value );
1236 /// Set the attribute to value.
1237 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001238
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001239private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001240 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001241
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001242 XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001243 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001244
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001245 XMLAttribute( const XMLAttribute& ); // not supported
1246 void operator=( const XMLAttribute& ); // not supported
1247 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001248
kezenator4f756162016-11-29 19:46:27 +10001249 char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001250
Lee Thomason624d43f2012-10-12 10:58:48 -07001251 mutable StrPair _name;
1252 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +10001253 int _parseLineNum;
Lee Thomason624d43f2012-10-12 10:58:48 -07001254 XMLAttribute* _next;
1255 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001256};
1257
1258
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001259/** The element is a container class. It has a value, the element name,
1260 and can contain other elements, text, comments, and unknowns.
1261 Elements also contain an arbitrary number of attributes.
1262*/
PKEuS16ed47d2013-07-06 12:02:43 +02001263class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001264{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001265 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001266public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001267 /// Get the name of an element (which is the Value() of the node.)
1268 const char* Name() const {
1269 return Value();
1270 }
1271 /// Set the name of the element.
1272 void SetName( const char* str, bool staticMem=false ) {
1273 SetValue( str, staticMem );
1274 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001275
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001276 virtual XMLElement* ToElement() {
1277 return this;
1278 }
1279 virtual const XMLElement* ToElement() const {
1280 return this;
1281 }
1282 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001283
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001284 /** Given an attribute name, Attribute() returns the value
1285 for the attribute of that name, or null if none
1286 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001287
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001288 @verbatim
1289 const char* value = ele->Attribute( "foo" );
1290 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001291
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001292 The 'value' parameter is normally null. However, if specified,
1293 the attribute will only be returned if the 'name' and 'value'
1294 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001295
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001296 @verbatim
1297 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1298 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001299
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001300 rather than:
1301 @verbatim
1302 if ( ele->Attribute( "foo" ) ) {
1303 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1304 }
1305 @endverbatim
1306 */
1307 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001308
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001309 /** Given an attribute name, IntAttribute() returns the value
Lee Thomason13cbc9a2016-10-27 14:55:07 -07001310 of the attribute interpreted as an integer. The default
1311 value will be returned if the attribute isn't present,
1312 or if there is an error. (For a method with error
1313 checking, see QueryIntAttribute()).
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001314 */
Josh Wittnercf3dd092016-10-11 18:57:17 -07001315 int IntAttribute(const char* name, int defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001316 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001317 unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001318 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001319 int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
cugone75a5acc2019-04-11 22:03:43 -05001320 /// See IntAttribute()
1321 uint64_t Unsigned64Attribute(const char* name, uint64_t defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001322 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001323 bool BoolAttribute(const char* name, bool defaultValue = false) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001324 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001325 double DoubleAttribute(const char* name, double defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001326 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001327 float FloatAttribute(const char* name, float defaultValue = 0) const;
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001328
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001329 /** Given an attribute name, QueryIntAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001330 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001331 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1332 doesn't exist. If successful, the result of the conversion
1333 will be written to 'value'. If not successful, nothing will
1334 be written to 'value'. This allows you to provide default
1335 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001336
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001337 @verbatim
1338 int value = 10;
1339 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1340 @endverbatim
1341 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001342 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001343 const XMLAttribute* a = FindAttribute( name );
1344 if ( !a ) {
1345 return XML_NO_ATTRIBUTE;
1346 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001347 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001348 }
Lee Thomason51c12712016-06-04 20:18:49 -07001349
1350 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001351 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001352 const XMLAttribute* a = FindAttribute( name );
1353 if ( !a ) {
1354 return XML_NO_ATTRIBUTE;
1355 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001356 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001357 }
Lee Thomason51c12712016-06-04 20:18:49 -07001358
1359 /// See QueryIntAttribute()
1360 XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1361 const XMLAttribute* a = FindAttribute(name);
1362 if (!a) {
1363 return XML_NO_ATTRIBUTE;
1364 }
1365 return a->QueryInt64Value(value);
1366 }
1367
cugone75a5acc2019-04-11 22:03:43 -05001368 /// See QueryIntAttribute()
1369 XMLError QueryUnsigned64Attribute(const char* name, uint64_t* value) const {
1370 const XMLAttribute* a = FindAttribute(name);
1371 if(!a) {
1372 return XML_NO_ATTRIBUTE;
1373 }
1374 return a->QueryUnsigned64Value(value);
1375 }
1376
Lee Thomason51c12712016-06-04 20:18:49 -07001377 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001378 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001379 const XMLAttribute* a = FindAttribute( name );
1380 if ( !a ) {
1381 return XML_NO_ATTRIBUTE;
1382 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001383 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001384 }
1385 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001386 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001387 const XMLAttribute* a = FindAttribute( name );
1388 if ( !a ) {
1389 return XML_NO_ATTRIBUTE;
1390 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001391 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001392 }
1393 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001394 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001395 const XMLAttribute* a = FindAttribute( name );
1396 if ( !a ) {
1397 return XML_NO_ATTRIBUTE;
1398 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001399 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001400 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001401
Lee Thomason5b00e062017-12-28 14:07:47 -08001402 /// See QueryIntAttribute()
1403 XMLError QueryStringAttribute(const char* name, const char** value) const {
1404 const XMLAttribute* a = FindAttribute(name);
1405 if (!a) {
1406 return XML_NO_ATTRIBUTE;
1407 }
1408 *value = a->Value();
1409 return XML_SUCCESS;
1410 }
1411
1412
Lee Thomason70d942e2018-06-29 15:31:59 -07001413
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001414 /** Given an attribute name, QueryAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001415 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001416 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1417 doesn't exist. It is overloaded for the primitive types,
1418 and is a generally more convenient replacement of
1419 QueryIntAttribute() and related functions.
Lee Thomason70d942e2018-06-29 15:31:59 -07001420
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001421 If successful, the result of the conversion
1422 will be written to 'value'. If not successful, nothing will
1423 be written to 'value'. This allows you to provide default
1424 value:
1425
1426 @verbatim
1427 int value = 10;
1428 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1429 @endverbatim
1430 */
Lee Thomasonfc80df32018-06-29 15:37:29 -07001431 XMLError QueryAttribute( const char* name, int* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001432 return QueryIntAttribute( name, value );
1433 }
1434
Lee Thomasonfc80df32018-06-29 15:37:29 -07001435 XMLError QueryAttribute( const char* name, unsigned int* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001436 return QueryUnsignedAttribute( name, value );
1437 }
1438
Lee Thomasonfc80df32018-06-29 15:37:29 -07001439 XMLError QueryAttribute(const char* name, int64_t* value) const {
Lee Thomason51c12712016-06-04 20:18:49 -07001440 return QueryInt64Attribute(name, value);
1441 }
1442
Lee Thomasoneffdf952019-08-10 17:49:42 -07001443 XMLError QueryAttribute(const char* name, uint64_t* value) const {
1444 return QueryUnsigned64Attribute(name, value);
1445 }
1446
1447 XMLError QueryAttribute( const char* name, bool* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001448 return QueryBoolAttribute( name, value );
1449 }
1450
Lee Thomasonfc80df32018-06-29 15:37:29 -07001451 XMLError QueryAttribute( const char* name, double* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001452 return QueryDoubleAttribute( name, value );
1453 }
1454
Lee Thomasonfc80df32018-06-29 15:37:29 -07001455 XMLError QueryAttribute( const char* name, float* value ) const {
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001456 return QueryFloatAttribute( name, value );
1457 }
1458
justnope5ee20fe2020-06-15 00:47:48 +02001459 XMLError QueryAttribute(const char* name, const char** value) const {
1460 return QueryStringAttribute(name, value);
Lee Thomason46379652020-07-02 15:23:00 -07001461 }
justnope5ee20fe2020-06-15 00:47:48 +02001462
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001463 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001464 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001465 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001466 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001467 }
1468 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001469 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001470 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001471 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001472 }
1473 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001474 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001475 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001476 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001477 }
Lee Thomason51c12712016-06-04 20:18:49 -07001478
1479 /// Sets the named attribute to value.
1480 void SetAttribute(const char* name, int64_t value) {
1481 XMLAttribute* a = FindOrCreateAttribute(name);
1482 a->SetAttribute(value);
1483 }
1484
Lee Thomasoneffdf952019-08-10 17:49:42 -07001485 /// Sets the named attribute to value.
1486 void SetAttribute(const char* name, uint64_t value) {
1487 XMLAttribute* a = FindOrCreateAttribute(name);
1488 a->SetAttribute(value);
1489 }
netcandfb45cb2020-02-15 21:35:58 +08001490
Lee Thomasoneffdf952019-08-10 17:49:42 -07001491 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001492 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001493 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001494 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001495 }
1496 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001497 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001498 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001499 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001500 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001501 /// Sets the named attribute to value.
1502 void SetAttribute( const char* name, float value ) {
1503 XMLAttribute* a = FindOrCreateAttribute( name );
1504 a->SetAttribute( value );
1505 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001506
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001507 /**
1508 Delete an attribute.
1509 */
1510 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001511
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001512 /// Return the first attribute in the list.
1513 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001514 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001515 }
1516 /// Query a specific attribute in the list.
1517 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001518
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001519 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001520 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001521 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001522
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001523 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001524 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001525
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001526 This is a convenient method for getting the text of simple contained text:
1527 @verbatim
1528 <foo>This is text</foo>
1529 const char* str = fooElement->GetText();
1530 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001531
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001532 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001533
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001534 Note that this function can be misleading. If the element foo was created from
1535 this XML:
1536 @verbatim
1537 <foo><b>This is text</b></foo>
1538 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001539
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001540 then the value of str would be null. The first child node isn't a text node, it is
1541 another element. From this XML:
1542 @verbatim
1543 <foo>This is <b>text</b></foo>
1544 @endverbatim
1545 GetText() will return "This is ".
1546 */
1547 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001548
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001549 /** Convenience function for easy access to the text inside an element. Although easy
1550 and concise, SetText() is limited compared to creating an XMLText child
1551 and mutating it directly.
1552
1553 If the first child of 'this' is a XMLText, SetText() sets its value to
1554 the given string, otherwise it will create a first child that is an XMLText.
1555
1556 This is a convenient method for setting the text of simple contained text:
1557 @verbatim
1558 <foo>This is text</foo>
1559 fooElement->SetText( "Hullaballoo!" );
1560 <foo>Hullaballoo!</foo>
1561 @endverbatim
1562
1563 Note that this function can be misleading. If the element foo was created from
1564 this XML:
1565 @verbatim
1566 <foo><b>This is text</b></foo>
1567 @endverbatim
1568
1569 then it will not change "This is text", but rather prefix it with a text element:
1570 @verbatim
1571 <foo>Hullaballoo!<b>This is text</b></foo>
1572 @endverbatim
Lee Thomason70d942e2018-06-29 15:31:59 -07001573
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001574 For this XML:
1575 @verbatim
1576 <foo />
1577 @endverbatim
1578 SetText() will generate
1579 @verbatim
1580 <foo>Hullaballoo!</foo>
1581 @endverbatim
1582 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001583 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001584 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001585 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001586 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001587 void SetText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001588 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1589 void SetText(int64_t value);
cugone47e229e2019-04-12 17:27:28 -05001590 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1591 void SetText(uint64_t value);
Lee Thomason51c12712016-06-04 20:18:49 -07001592 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001593 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001594 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001595 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001596 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason70d942e2018-06-29 15:31:59 -07001597 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001598
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001599 /**
1600 Convenience method to query the value of a child text node. This is probably best
1601 shown by example. Given you have a document is this form:
1602 @verbatim
1603 <point>
1604 <x>1</x>
1605 <y>1.4</y>
1606 </point>
1607 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001608
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001609 The QueryIntText() and similar functions provide a safe and easier way to get to the
1610 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001611
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001612 @verbatim
1613 int x = 0;
1614 float y = 0; // types of x and y are contrived for example
1615 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1616 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1617 xElement->QueryIntText( &x );
1618 yElement->QueryFloatText( &y );
1619 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001620
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001621 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1622 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 -07001623
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001624 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001625 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001626 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001627 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001628 /// See QueryIntText()
1629 XMLError QueryInt64Text(int64_t* uval) const;
1630 /// See QueryIntText()
cugone47e229e2019-04-12 17:27:28 -05001631 XMLError QueryUnsigned64Text(uint64_t* uval) const;
1632 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001633 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001634 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001635 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001636 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001637 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001638
Josh Wittnercf3dd092016-10-11 18:57:17 -07001639 int IntText(int defaultValue = 0) const;
1640
Josh Wittner3a621f52016-09-12 19:17:54 -07001641 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001642 unsigned UnsignedText(unsigned defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001643 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001644 int64_t Int64Text(int64_t defaultValue = 0) const;
cugone47e229e2019-04-12 17:27:28 -05001645 /// See QueryIntText()
1646 uint64_t Unsigned64Text(uint64_t defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001647 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001648 bool BoolText(bool defaultValue = false) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001649 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001650 double DoubleText(double defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001651 /// See QueryIntText()
ngc92e6202452019-12-03 16:25:53 +01001652 float FloatText(float defaultValue = 0) const;
1653
1654 /**
1655 Convenience method to create a new XMLElement and add it as last (right)
1656 child of this node. Returns the created and inserted element.
1657 */
Lee Thomasone4442682020-03-01 17:10:38 -08001658 XMLElement* InsertNewChildElement(const char* name);
1659 /// See InsertNewChildElement()
1660 XMLComment* InsertNewComment(const char* comment);
1661 /// See InsertNewChildElement()
1662 XMLText* InsertNewText(const char* text);
1663 /// See InsertNewChildElement()
1664 XMLDeclaration* InsertNewDeclaration(const char* text);
1665 /// See InsertNewChildElement()
1666 XMLUnknown* InsertNewUnknown(const char* text);
ngc92e6202452019-12-03 16:25:53 +01001667
Josh Wittner3a621f52016-09-12 19:17:54 -07001668
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001669 // internal:
Dmitry-Mee5035632017-04-05 18:02:40 +03001670 enum ElementClosingType {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001671 OPEN, // <foo>
1672 CLOSED, // <foo/>
1673 CLOSING // </foo>
1674 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001675 ElementClosingType ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001676 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001677 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001678 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1679 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001680
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001681protected:
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001682 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001683
Lee Thomason50adb4c2012-02-13 15:07:09 -08001684private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001685 XMLElement( XMLDocument* doc );
1686 virtual ~XMLElement();
1687 XMLElement( const XMLElement& ); // not supported
1688 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001689
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001690 XMLAttribute* FindOrCreateAttribute( const char* name );
kezenator4f756162016-11-29 19:46:27 +10001691 char* ParseAttributes( char* p, int* curLineNumPtr );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001692 static void DeleteAttribute( XMLAttribute* attribute );
Dmitry-Mea60caa22016-11-22 18:28:08 +03001693 XMLAttribute* CreateAttribute();
Lee Thomason67d61312012-01-24 16:01:51 -08001694
Lee Thomason5bb2d802014-01-24 10:42:57 -08001695 enum { BUF_SIZE = 200 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001696 ElementClosingType _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001697 // The attribute list is ordered; there is no 'lastAttribute'
1698 // because the list needs to be scanned for dupes before adding
1699 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001700 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001701};
1702
1703
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001704enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001705 PRESERVE_WHITESPACE,
1706 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001707};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001708
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001709
1710/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001711 It can be saved, loaded, and printed to the screen.
1712 All Nodes are connected and allocated to a Document.
1713 If the Document is deleted, all its Nodes are also deleted.
1714*/
PKEuS16ed47d2013-07-06 12:02:43 +02001715class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001716{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001717 friend class XMLElement;
Lee Thomasond946dda2018-04-05 09:11:08 -07001718 // Gives access to SetError and Push/PopDepth, but over-access for everything else.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001719 // Wishing C++ had "internal" scope.
Lee Thomason70d942e2018-06-29 15:31:59 -07001720 friend class XMLNode;
Lee Thomasonf49b9652017-10-11 10:57:49 -07001721 friend class XMLText;
1722 friend class XMLComment;
1723 friend class XMLDeclaration;
1724 friend class XMLUnknown;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001725public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001726 /// constructor
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001727 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001728 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001729
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001730 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001731 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001732 return this;
1733 }
1734 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001735 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001736 return this;
1737 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001738
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001739 /**
1740 Parse an XML file from a character string.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001741 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001742 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001743
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001744 You may optionally pass in the 'nBytes', which is
1745 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001746 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001747 null terminated string.
1748 */
orbitcowboy710a3322019-05-16 15:30:47 +02001749 XMLError Parse( const char* xml, size_t nBytes=static_cast<size_t>(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001750
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001751 /**
1752 Load an XML file from disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001753 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001754 an errorID.
1755 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001756 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001757
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001758 /**
1759 Load an XML file from disk. You are responsible
Lee Thomason70d942e2018-06-29 15:31:59 -07001760 for providing and closing the FILE*.
1761
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001762 NOTE: The file should be opened as binary ("rb")
1763 not text in order for TinyXML-2 to correctly
1764 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001765
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001766 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001767 an errorID.
1768 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001769 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001770
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001771 /**
1772 Save the XML file to disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001773 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001774 an errorID.
1775 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001776 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001777
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001778 /**
1779 Save the XML file to disk. You are responsible
1780 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001781
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001782 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001783 an errorID.
1784 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001785 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001786
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001787 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001788 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001789 }
1790 Whitespace WhitespaceMode() const {
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001791 return _whitespaceMode;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001792 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001793
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001794 /**
1795 Returns true if this document has a leading Byte Order Mark of UTF8.
1796 */
1797 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001798 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001799 }
1800 /** Sets whether to write the BOM when writing the file.
1801 */
1802 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001803 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001804 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001805
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001806 /** Return the root element of DOM. Equivalent to FirstChildElement().
1807 To get the first node, use FirstChild().
1808 */
1809 XMLElement* RootElement() {
1810 return FirstChildElement();
1811 }
1812 const XMLElement* RootElement() const {
1813 return FirstChildElement();
1814 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001815
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001816 /** Print the Document. If the Printer is not provided, it will
1817 print to stdout. If you provide Printer, this can print to a file:
1818 @verbatim
1819 XMLPrinter printer( fp );
1820 doc.Print( &printer );
1821 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001822
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001823 Or you can use a printer to print to memory:
1824 @verbatim
1825 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001826 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001827 // printer.CStr() has a const char* to the XML
1828 @endverbatim
1829 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001830 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001831 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001832
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001833 /**
1834 Create a new Element associated with
1835 this Document. The memory for the Element
1836 is managed by the Document.
1837 */
1838 XMLElement* NewElement( const char* name );
1839 /**
1840 Create a new Comment associated with
1841 this Document. The memory for the Comment
1842 is managed by the Document.
1843 */
1844 XMLComment* NewComment( const char* comment );
1845 /**
1846 Create a new Text associated with
1847 this Document. The memory for the Text
1848 is managed by the Document.
1849 */
1850 XMLText* NewText( const char* text );
1851 /**
1852 Create a new Declaration associated with
1853 this Document. The memory for the object
1854 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001855
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001856 If the 'text' param is null, the standard
1857 declaration is used.:
1858 @verbatim
1859 <?xml version="1.0" encoding="UTF-8"?>
1860 @endverbatim
1861 */
1862 XMLDeclaration* NewDeclaration( const char* text=0 );
1863 /**
1864 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001865 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001866 is managed by the Document.
1867 */
1868 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001869
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001870 /**
1871 Delete a node associated with this document.
1872 It will be unlinked from the DOM.
1873 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001874 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001875
ngc922c6a41a2020-07-30 23:51:30 +03001876 /// Clears the error flags.
1877 void ClearError();
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001878
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001879 /// Return true if there was an error parsing the document.
1880 bool Error() const {
Lee Thomason85536252016-06-04 19:10:53 -07001881 return _errorID != XML_SUCCESS;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001882 }
1883 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001884 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001885 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001886 }
Lee Thomason331596e2014-09-11 14:56:43 -07001887 const char* ErrorName() const;
Lee Thomasone90e9012016-12-24 07:34:39 -08001888 static const char* ErrorIDToName(XMLError errorID);
Lee Thomason331596e2014-09-11 14:56:43 -07001889
Lee Thomason70d942e2018-06-29 15:31:59 -07001890 /** Returns a "long form" error description. A hopefully helpful
Lee Thomasonf49b9652017-10-11 10:57:49 -07001891 diagnostic with location, line number, and/or additional info.
1892 */
1893 const char* ErrorStr() const;
1894
1895 /// A (trivial) utility function that prints the ErrorStr() to stdout.
1896 void PrintError() const;
Lee Thomason8c9e3132017-06-26 16:55:01 -07001897
orbitcowboy22b21ec2018-07-17 11:52:57 +02001898 /// Return the line where the error occurred, or zero if unknown.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001899 int ErrorLineNum() const
kezenatorec694152016-11-26 17:21:43 +10001900 {
1901 return _errorLineNum;
1902 }
Lee Thomason70d942e2018-06-29 15:31:59 -07001903
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001904 /// Clear the document, resetting it to the initial state.
1905 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001906
Lee Thomason7085f002017-06-01 18:09:43 -07001907 /**
Lee Thomasonb29f5562017-06-01 18:45:32 -07001908 Copies this document to a target document.
Lee Thomason7085f002017-06-01 18:09:43 -07001909 The target will be completely cleared before the copy.
Lee Thomasonb29f5562017-06-01 18:45:32 -07001910 If you want to copy a sub-tree, see XMLNode::DeepClone().
Lee Thomason7085f002017-06-01 18:09:43 -07001911
Lee Thomason1346a172017-06-14 15:14:19 -07001912 NOTE: that the 'target' must be non-null.
Lee Thomason7085f002017-06-01 18:09:43 -07001913 */
Shuichiro Suzuki87cd4e02017-08-24 11:20:26 +09001914 void DeepCopy(XMLDocument* target) const;
Lee Thomason7085f002017-06-01 18:09:43 -07001915
1916 // internal
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001917 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001918
Lee Thomason816d3fa2017-06-05 14:35:55 -07001919 // internal
orbitcowboy73f54092019-08-14 09:30:30 +02001920 void MarkInUse(const XMLNode* const);
Lee Thomason816d3fa2017-06-05 14:35:55 -07001921
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001922 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1923 return 0;
1924 }
1925 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1926 return false;
1927 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001928
Lee Thomason3f57d272012-01-11 15:30:03 -08001929private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001930 XMLDocument( const XMLDocument& ); // not supported
1931 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001932
Lee Thomasone90e9012016-12-24 07:34:39 -08001933 bool _writeBOM;
1934 bool _processEntities;
1935 XMLError _errorID;
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001936 Whitespace _whitespaceMode;
Lee Thomasonaa188392017-09-19 17:54:31 -07001937 mutable StrPair _errorStr;
Lee Thomasone90e9012016-12-24 07:34:39 -08001938 int _errorLineNum;
1939 char* _charBuffer;
1940 int _parseCurLineNum;
Lee Thomasond946dda2018-04-05 09:11:08 -07001941 int _parsingDepth;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001942 // Memory tracking does add some overhead.
1943 // However, the code assumes that you don't
1944 // have a bunch of unlinked nodes around.
1945 // Therefore it takes less memory to track
1946 // in the document vs. a linked list in the XMLNode,
1947 // and the performance is the same.
1948 DynArray<XMLNode*, 10> _unlinked;
Lee Thomasond1983222012-02-06 08:41:24 -08001949
Lee Thomason624d43f2012-10-12 10:58:48 -07001950 MemPoolT< sizeof(XMLElement) > _elementPool;
1951 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1952 MemPoolT< sizeof(XMLText) > _textPool;
1953 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001954
1955 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001956
1957 void Parse();
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001958
Lee Thomasonf49b9652017-10-11 10:57:49 -07001959 void SetError( XMLError error, int lineNum, const char* format, ... );
1960
Lee Thomasond946dda2018-04-05 09:11:08 -07001961 // Something of an obvious security hole, once it was discovered.
1962 // Either an ill-formed XML or an excessively deep one can overflow
1963 // the stack. Track stack depth, and error out if needed.
1964 class DepthTracker {
1965 public:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02001966 explicit DepthTracker(XMLDocument * document) {
Lee Thomason70d942e2018-06-29 15:31:59 -07001967 this->_document = document;
Lee Thomasond946dda2018-04-05 09:11:08 -07001968 document->PushDepth();
1969 }
1970 ~DepthTracker() {
1971 _document->PopDepth();
1972 }
1973 private:
1974 XMLDocument * _document;
1975 };
Lee Thomasonf928c352018-04-05 09:24:20 -07001976 void PushDepth();
1977 void PopDepth();
Lee Thomasond946dda2018-04-05 09:11:08 -07001978
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001979 template<class NodeType, int PoolElementSize>
1980 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
Lee Thomason5cae8972012-01-24 18:03:07 -08001981};
1982
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001983template<class NodeType, int PoolElementSize>
1984inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1985{
1986 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1987 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1988 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1989 TIXMLASSERT( returnNode );
1990 returnNode->_memPool = &pool;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001991
1992 _unlinked.Push(returnNode);
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001993 return returnNode;
1994}
Lee Thomason7c913cd2012-01-26 18:32:34 -08001995
Lee Thomason3ffdd392012-03-28 17:27:55 -07001996/**
1997 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001998 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001999 DOM structure. It is a separate utility class.
2000
2001 Take an example:
2002 @verbatim
2003 <Document>
2004 <Element attributeA = "valueA">
2005 <Child attributeB = "value1" />
2006 <Child attributeB = "value2" />
2007 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02002008 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07002009 @endverbatim
2010
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002011 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07002012 easy to write a *lot* of code that looks like:
2013
2014 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002015 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07002016 if ( root )
2017 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002018 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07002019 if ( element )
2020 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002021 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07002022 if ( child )
2023 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002024 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07002025 if ( child2 )
2026 {
2027 // Finally do something useful.
2028 @endverbatim
2029
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002030 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002031 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07002032 and correct to use:
2033
2034 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07002035 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03002036 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07002037 if ( child2 )
2038 {
2039 // do something useful
2040 @endverbatim
2041
2042 Which is MUCH more concise and useful.
2043
2044 It is also safe to copy handles - internally they are nothing more than node pointers.
2045 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002046 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002047 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002048
2049 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07002050*/
PKEuS16ed47d2013-07-06 12:02:43 +02002051class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07002052{
2053public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002054 /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02002055 explicit XMLHandle( XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002056 }
2057 /// Create a handle from a node.
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02002058 explicit XMLHandle( XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002059 }
2060 /// Copy constructor
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002061 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002062 }
2063 /// Assignment
2064 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002065 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002066 return *this;
2067 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002068
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002069 /// Get the first child of this handle.
2070 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002071 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002072 }
2073 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002074 XMLHandle FirstChildElement( const char* name = 0 ) {
2075 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002076 }
2077 /// Get the last child of this handle.
2078 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002079 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002080 }
2081 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002082 XMLHandle LastChildElement( const char* name = 0 ) {
2083 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002084 }
2085 /// Get the previous sibling of this handle.
2086 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002087 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002088 }
2089 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002090 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
2091 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002092 }
2093 /// Get the next sibling of this handle.
2094 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002095 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002096 }
2097 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002098 XMLHandle NextSiblingElement( const char* name = 0 ) {
2099 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002100 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002101
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002102 /// Safe cast to XMLNode. This can return null.
2103 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002104 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002105 }
2106 /// Safe cast to XMLElement. This can return null.
2107 XMLElement* ToElement() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002108 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002109 }
2110 /// Safe cast to XMLText. This can return null.
2111 XMLText* ToText() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002112 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002113 }
2114 /// Safe cast to XMLUnknown. This can return null.
2115 XMLUnknown* ToUnknown() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002116 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002117 }
2118 /// Safe cast to XMLDeclaration. This can return null.
2119 XMLDeclaration* ToDeclaration() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002120 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002121 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002122
2123private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002124 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002125};
2126
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08002127
2128/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002129 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
2130 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07002131*/
PKEuS16ed47d2013-07-06 12:02:43 +02002132class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07002133{
2134public:
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02002135 explicit XMLConstHandle( const XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002136 }
orbitcowboy2cc8a4c2018-09-05 08:46:21 +02002137 explicit XMLConstHandle( const XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002138 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002139 XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002140 }
Lee Thomason8b899812012-04-04 15:58:16 -07002141
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002142 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002143 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002144 return *this;
2145 }
Lee Thomason8b899812012-04-04 15:58:16 -07002146
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002147 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002148 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002149 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002150 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2151 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002152 }
2153 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002154 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002155 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002156 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2157 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002158 }
2159 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002160 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002161 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002162 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2163 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002164 }
2165 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002166 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002167 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002168 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2169 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002170 }
Lee Thomason8b899812012-04-04 15:58:16 -07002171
2172
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002173 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002174 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002175 }
2176 const XMLElement* ToElement() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002177 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002178 }
2179 const XMLText* ToText() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002180 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002181 }
2182 const XMLUnknown* ToUnknown() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002183 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002184 }
2185 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002186 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002187 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08002188
Lee Thomason5cae8972012-01-24 18:03:07 -08002189private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002190 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08002191};
Lee Thomason6f381b72012-03-02 12:59:39 -08002192
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002193
2194/**
2195 Printing functionality. The XMLPrinter gives you more
2196 options than the XMLDocument::Print() method.
2197
2198 It can:
2199 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02002200 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002201 -# Print XML without a XMLDocument.
2202
2203 Print to Memory
2204
2205 @verbatim
2206 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06002207 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02002208 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002209 @endverbatim
2210
2211 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002212
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002213 You provide the file pointer.
2214 @verbatim
2215 XMLPrinter printer( fp );
2216 doc.Print( &printer );
2217 @endverbatim
2218
2219 Print without a XMLDocument
2220
2221 When loading, an XML parser is very useful. However, sometimes
2222 when saving, it just gets in the way. The code is often set up
2223 for streaming, and constructing the DOM is just overhead.
2224
2225 The Printer supports the streaming case. The following code
2226 prints out a trivially simple XML file without ever creating
2227 an XML document.
2228
2229 @verbatim
2230 XMLPrinter printer( fp );
2231 printer.OpenElement( "foo" );
2232 printer.PushAttribute( "foo", "bar" );
2233 printer.CloseElement();
2234 @endverbatim
2235*/
PKEuS16ed47d2013-07-06 12:02:43 +02002236class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002237{
2238public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002239 /** Construct the printer. If the FILE* is specified,
2240 this will print to the FILE. Else it will print
2241 to memory, and the result is available in CStr().
2242 If 'compact' is set to true, then output is created
2243 with only required whitespace and newlines.
2244 */
PKEuS1bfb9542013-08-04 13:51:17 +02002245 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002246 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002247
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002248 /** If streaming, write the BOM and declaration. */
2249 void PushHeader( bool writeBOM, bool writeDeclaration );
2250 /** If streaming, start writing an element.
2251 The element must be closed with CloseElement()
2252 */
Lee Thomason256adb62014-04-06 14:41:46 -07002253 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002254 /// If streaming, add an attribute to an open element.
2255 void PushAttribute( const char* name, const char* value );
2256 void PushAttribute( const char* name, int value );
2257 void PushAttribute( const char* name, unsigned value );
aaronkirkham07@gmail.comc341cea2019-05-22 00:05:27 +01002258 void PushAttribute( const char* name, int64_t value );
2259 void PushAttribute( const char* name, uint64_t value );
Lee Thomason51c12712016-06-04 20:18:49 -07002260 void PushAttribute( const char* name, bool value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002261 void PushAttribute( const char* name, double value );
2262 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002263 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002264
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002265 /// Add a text node.
2266 void PushText( const char* text, bool cdata=false );
2267 /// Add a text node from an integer.
2268 void PushText( int value );
2269 /// Add a text node from an unsigned.
2270 void PushText( unsigned value );
aaronkirkham07@gmail.comc341cea2019-05-22 00:05:27 +01002271 /// Add a text node from a signed 64bit integer.
2272 void PushText( int64_t value );
2273 /// Add a text node from an unsigned 64bit integer.
2274 void PushText( uint64_t value );
Lee Thomason51c12712016-06-04 20:18:49 -07002275 /// Add a text node from a bool.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002276 void PushText( bool value );
2277 /// Add a text node from a float.
2278 void PushText( float value );
2279 /// Add a text node from a double.
2280 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002281
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002282 /// Add a comment
2283 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002284
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002285 void PushDeclaration( const char* value );
2286 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002287
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002288 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2289 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2290 return true;
2291 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002292
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002293 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2294 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002295
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002296 virtual bool Visit( const XMLText& text );
2297 virtual bool Visit( const XMLComment& comment );
2298 virtual bool Visit( const XMLDeclaration& declaration );
2299 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002300
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002301 /**
2302 If in print to memory mode, return a pointer to
2303 the XML file in memory.
2304 */
2305 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002306 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002307 }
2308 /**
2309 If in print to memory mode, return the size
2310 of the XML file in memory. (Note the size returned
2311 includes the terminating null.)
2312 */
2313 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002314 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002315 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002316 /**
2317 If in print to memory mode, reset the buffer to the
2318 beginning.
2319 */
Stephan Friedrichse1b30132019-03-07 21:30:23 +01002320 void ClearBuffer( bool resetToFirstElement = true ) {
Lee Thomasonce0510b2013-11-26 21:29:37 -08002321 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002322 _buffer.Push(0);
Stephan Friedrichse1b30132019-03-07 21:30:23 +01002323 _firstElement = resetToFirstElement;
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002324 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002325
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002326protected:
Alexander Maid740b642014-05-20 22:04:42 +02002327 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002328
Lee Thomasonc18eb232014-02-21 17:31:17 -08002329 /** Prints out the space before an element. You may override to change
2330 the space and tabs used. A PrintSpace() override should call Print().
2331 */
2332 virtual void PrintSpace( int depth );
Lee Thomason8812f192020-06-13 17:46:44 -07002333 virtual void Print( const char* format, ... );
2334 virtual void Write( const char* data, size_t size );
2335 virtual void Putc( char ch );
2336
2337 inline void Write(const char* data) { Write(data, strlen(data)); }
Lee Thomasonc18eb232014-02-21 17:31:17 -08002338
Dmitry-Mea092bc12014-12-23 17:57:05 +03002339 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002340 bool _elementJustOpened;
2341 DynArray< const char*, 10 > _stack;
2342
2343private:
Chow Loong Jin6a514392020-03-26 16:30:37 +08002344 /**
2345 Prepares to write a new node. This includes sealing an element that was
2346 just opened, and writing any whitespace necessary if not in compact mode.
2347 */
2348 void PrepareForNewNode( bool compactMode );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002349 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002350
Lee Thomason624d43f2012-10-12 10:58:48 -07002351 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002352 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002353 int _depth;
2354 int _textDepth;
2355 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002356 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002357
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002358 enum {
2359 ENTITY_RANGE = 64,
2360 BUF_SIZE = 200
2361 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002362 bool _entityFlag[ENTITY_RANGE];
2363 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002364
Lee Thomason624d43f2012-10-12 10:58:48 -07002365 DynArray< char, 20 > _buffer;
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002366
2367 // Prohibit cloning, intentionally not implemented
2368 XMLPrinter( const XMLPrinter& );
2369 XMLPrinter& operator=( const XMLPrinter& );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002370};
2371
2372
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002373} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002374
PKEuS95060352013-07-26 10:42:44 +02002375#if defined(_MSC_VER)
2376# pragma warning(pop)
2377#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002378
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002379#endif // TINYXML2_INCLUDED