blob: 8548b35a1112a3769526755b4634f859476cad55 [file] [log] [blame]
Lee Thomason (grinliz)28129862012-02-25 21:11:20 -08001/*
2Original code by Lee Thomason (www.grinninglizard.com)
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any
6damages arising from the use of this software.
7
8Permission is granted to anyone to use this software for any
9purpose, including commercial applications, and to alter it and
10redistribute it freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must
13not claim that you wrote the original software. If you use this
14software in a product, an acknowledgment in the product documentation
15would be appreciated but is not required.
16
172. Altered source versions must be plainly marked as such, and
18must not be misrepresented as being the original software.
19
203. This notice may not be removed or altered from any source
21distribution.
22*/
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -080023
Lee Thomason7d00b9a2012-02-27 17:54:22 -080024#ifndef TINYXML2_INCLUDED
U-Lama\Leee13c3e62011-12-28 14:36:55 -080025#define TINYXML2_INCLUDED
26
Anton Indrawanf59e2d62014-11-18 20:50:42 +010027#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070028# include <ctype.h>
29# include <limits.h>
30# include <stdio.h>
31# include <stdlib.h>
32# include <string.h>
Lee Thomasona572db12016-06-04 19:16:24 -070033# if defined(__PS3__)
34# include <stddef.h>
35# endif
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070036#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -070037# include <cctype>
38# include <climits>
39# include <cstdio>
40# include <cstdlib>
41# include <cstring>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070042#endif
Lee Thomason1889c3e2016-06-04 20:22:57 -070043#include <stdint.h>
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070044
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -070045/*
Lee Thomason7d00b9a2012-02-27 17:54:22 -080046 TODO: intern strings instead of allocation.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080047*/
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080048/*
Lee Thomasona9cf3f92012-10-11 16:56:51 -070049 gcc:
Peter Matula50689912018-01-09 12:52:26 +010050 g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
MortenMacFly4ee49f12013-01-14 20:03:14 +010051
Lee Thomasona9cf3f92012-10-11 16:56:51 -070052 Formatting, Artistic Style:
53 AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080054*/
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080055
Lee Thomason7085f002017-06-01 18:09:43 -070056#if defined( _DEBUG ) || defined (__DEBUG__)
Peter Matula50689912018-01-09 12:52:26 +010057# ifndef TINYXML2_DEBUG
58# define TINYXML2_DEBUG
Lee Thomasona9cf3f92012-10-11 16:56:51 -070059# endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080060#endif
61
PKEuS95060352013-07-26 10:42:44 +020062#ifdef _MSC_VER
63# pragma warning(push)
64# pragma warning(disable: 4251)
65#endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080066
PKEuS16ed47d2013-07-06 12:02:43 +020067#ifdef _WIN32
68# ifdef TINYXML2_EXPORT
69# define TINYXML2_LIB __declspec(dllexport)
70# elif defined(TINYXML2_IMPORT)
71# define TINYXML2_LIB __declspec(dllimport)
72# else
73# define TINYXML2_LIB
74# endif
Matthew Woehlkea8e7ea72016-08-09 13:16:26 -040075#elif __GNUC__ >= 4
76# define TINYXML2_LIB __attribute__((visibility("default")))
PKEuS16ed47d2013-07-06 12:02:43 +020077#else
78# define TINYXML2_LIB
79#endif
80
81
Peter Matula50689912018-01-09 12:52:26 +010082#if defined(TINYXML2_DEBUG)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070083# if defined(_MSC_VER)
Dmitry-Me4bcbf142014-12-25 19:05:18 +030084# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
Lee Thomason598a88d2015-10-09 14:42:12 -070085# define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
Lee Thomasona9cf3f92012-10-11 16:56:51 -070086# elif defined (ANDROID_NDK)
87# include <android/log.h>
88# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
89# else
90# include <assert.h>
91# define TIXMLASSERT assert
92# endif
Lee Thomason598a88d2015-10-09 14:42:12 -070093#else
94# define TIXMLASSERT( x ) {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080095#endif
96
U-Lama\Leee13c3e62011-12-28 14:36:55 -080097
Lee Thomasonc18eb232014-02-21 17:31:17 -080098/* Versioning, past 1.0.14:
Lee Thomason85afe9c2014-02-23 21:42:16 -080099 http://semver.org/
Lee Thomasonc18eb232014-02-21 17:31:17 -0800100*/
Lee Thomason8c8293b2017-12-10 19:55:35 -0800101static const int TIXML2_MAJOR_VERSION = 6;
Lee Thomasonf26a5472017-12-29 09:30:39 -0800102static const int TIXML2_MINOR_VERSION = 1;
Lee Thomason8c8293b2017-12-10 19:55:35 -0800103static const int TIXML2_PATCH_VERSION = 0;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800104
Lee Thomasonf26a5472017-12-29 09:30:39 -0800105#define TINYXML2_MAJOR_VERSION 6
106#define TINYXML2_MINOR_VERSION 1
107#define TINYXML2_PATCH_VERSION 0
Lee Thomasonbd197872017-12-28 13:31:48 -0800108
Lee Thomasonf928c352018-04-05 09:24:20 -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,
113// so there needs to be a limit in place.
114static const int TINYXML2_MAX_ELEMENT_DEPTH = 100;
Lee Thomasond946dda2018-04-05 09:11:08 -0700115
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800116namespace tinyxml2
117{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800118class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800119class XMLElement;
120class XMLAttribute;
121class XMLComment;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800122class XMLText;
Lee Thomason50f97b22012-02-11 16:33:40 -0800123class XMLDeclaration;
124class XMLUnknown;
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800125class XMLPrinter;
Lee Thomason5cae8972012-01-24 18:03:07 -0800126
U-Stream\Leeae25a442012-02-17 17:48:16 -0800127/*
128 A class that wraps strings. Normally stores the start and end
129 pointers into the XML file itself, and will apply normalization
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800130 and entity translation if actually read. Can also store (and memory
U-Stream\Leeae25a442012-02-17 17:48:16 -0800131 manage) a traditional char[]
132*/
PKEuS95060352013-07-26 10:42:44 +0200133class StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800134{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800135public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700136 enum {
137 NEEDS_ENTITY_PROCESSING = 0x01,
138 NEEDS_NEWLINE_NORMALIZATION = 0x02,
Dmitry-Me5420e542015-05-20 10:51:26 +0300139 NEEDS_WHITESPACE_COLLAPSING = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800140
Lee Thomason584af572016-09-05 14:14:16 -0700141 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700142 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
Lee Thomason584af572016-09-05 14:14:16 -0700143 ATTRIBUTE_NAME = 0,
144 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
145 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
146 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700147 };
Lee Thomason39ede242012-01-20 11:27:56 -0800148
Lee Thomason120b3a62012-10-12 10:06:59 -0700149 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700150 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800151
Lee Thomason120b3a62012-10-12 10:06:59 -0700152 void Set( char* start, char* end, int flags ) {
Dmitry-Me6fc38ec2016-09-01 17:59:44 +0300153 TIXMLASSERT( start );
154 TIXMLASSERT( end );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700155 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700156 _start = start;
157 _end = end;
158 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700159 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700160
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700161 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700162
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700163 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700164 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700165 }
Lee Thomason39ede242012-01-20 11:27:56 -0800166
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700167 void SetInternedStr( const char* str ) {
168 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700169 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700170 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700171
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700172 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800173
kezenator4f756162016-11-29 19:46:27 +1000174 char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700175 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800176
Lee Thomason29658802014-11-27 22:31:11 -0800177 void TransferTo( StrPair* other );
Lee Thomason584af572016-09-05 14:14:16 -0700178 void Reset();
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300179
Lee Thomason39ede242012-01-20 11:27:56 -0800180private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700181 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800182
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700183 enum {
184 NEEDS_FLUSH = 0x100,
185 NEEDS_DELETE = 0x200
186 };
Lee Thomasone4422302012-01-20 17:59:50 -0800187
Lee Thomason120b3a62012-10-12 10:06:59 -0700188 int _flags;
189 char* _start;
190 char* _end;
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300191
192 StrPair( const StrPair& other ); // not supported
193 void operator=( StrPair& other ); // not supported, use TransferTo()
Lee Thomason39ede242012-01-20 11:27:56 -0800194};
195
U-Lama\Lee560bd472011-12-28 19:42:49 -0800196
U-Stream\Leeae25a442012-02-17 17:48:16 -0800197/*
198 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
199 Has a small initial memory pool, so that low or no usage will not
200 cause a call to new/delete
201*/
Dmitry-Me04009222015-04-06 18:07:18 +0300202template <class T, int INITIAL_SIZE>
PKEuS95060352013-07-26 10:42:44 +0200203class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800204{
205public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200206 DynArray() :
207 _mem( _pool ),
208 _allocated( INITIAL_SIZE ),
209 _size( 0 )
210 {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700211 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700212
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700213 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700214 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700215 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700216 }
217 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700218
Lee Thomasonce0510b2013-11-26 21:29:37 -0800219 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100220 _size = 0;
221 }
222
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700223 void Push( T t ) {
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300224 TIXMLASSERT( _size < INT_MAX );
Lee Thomason624d43f2012-10-12 10:58:48 -0700225 EnsureCapacity( _size+1 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300226 _mem[_size] = t;
227 ++_size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700228 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800229
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700230 T* PushArr( int count ) {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300231 TIXMLASSERT( count >= 0 );
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300232 TIXMLASSERT( _size <= INT_MAX - count );
Lee Thomason624d43f2012-10-12 10:58:48 -0700233 EnsureCapacity( _size+count );
234 T* ret = &_mem[_size];
235 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700236 return ret;
237 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700238
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700239 T Pop() {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300240 TIXMLASSERT( _size > 0 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300241 --_size;
242 return _mem[_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700243 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700244
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700245 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700246 TIXMLASSERT( _size >= count );
247 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700248 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800249
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700250 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700251 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700252 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700253
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700254 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700255 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700256 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700257 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700258
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700259 const T& operator[](int i) const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700260 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700261 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700262 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700263
Lee Thomasonf07b9522014-10-30 13:25:12 -0700264 const T& PeekTop() const {
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500265 TIXMLASSERT( _size > 0 );
266 return _mem[ _size - 1];
267 }
268
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700269 int Size() const {
Dmitry-Me30bdc972015-01-14 08:32:23 +0300270 TIXMLASSERT( _size >= 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700271 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700272 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700273
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700274 int Capacity() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300275 TIXMLASSERT( _allocated >= INITIAL_SIZE );
Lee Thomason624d43f2012-10-12 10:58:48 -0700276 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700277 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700278
Lee Thomason816d3fa2017-06-05 14:35:55 -0700279 void SwapRemove(int i) {
Dmitry-Mec2f677b2017-06-15 12:44:27 +0300280 TIXMLASSERT(i >= 0 && i < _size);
281 TIXMLASSERT(_size > 0);
Lee Thomason816d3fa2017-06-05 14:35:55 -0700282 _mem[i] = _mem[_size - 1];
283 --_size;
284 }
285
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700286 const T* Mem() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300287 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700288 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700289 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700290
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700291 T* Mem() {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300292 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700293 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700294 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800295
Lee Thomason2c85a712012-01-31 08:24:24 -0800296private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300297 DynArray( const DynArray& ); // not supported
298 void operator=( const DynArray& ); // not supported
299
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700300 void EnsureCapacity( int cap ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300301 TIXMLASSERT( cap > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700302 if ( cap > _allocated ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300303 TIXMLASSERT( cap <= INT_MAX / 2 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700304 int newAllocated = cap * 2;
305 T* newMem = new T[newAllocated];
Dmitry-Me243ddf52017-05-18 17:27:14 +0300306 TIXMLASSERT( newAllocated >= _size );
Lee Thomason624d43f2012-10-12 10:58:48 -0700307 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
308 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700309 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700310 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700311 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700312 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700313 }
314 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800315
Lee Thomason624d43f2012-10-12 10:58:48 -0700316 T* _mem;
Dmitry-Me04009222015-04-06 18:07:18 +0300317 T _pool[INITIAL_SIZE];
Lee Thomason624d43f2012-10-12 10:58:48 -0700318 int _allocated; // objects allocated
319 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800320};
321
Lee Thomason50adb4c2012-02-13 15:07:09 -0800322
U-Stream\Leeae25a442012-02-17 17:48:16 -0800323/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200324 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800325 and deallocation of objects.
326*/
PKEuS95060352013-07-26 10:42:44 +0200327class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800328{
329public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700330 MemPool() {}
331 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800332
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700333 virtual int ItemSize() const = 0;
334 virtual void* Alloc() = 0;
335 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800336 virtual void SetTracked() = 0;
Lee Thomasonf07b9522014-10-30 13:25:12 -0700337 virtual void Clear() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800338};
339
Lee Thomason50adb4c2012-02-13 15:07:09 -0800340
U-Stream\Leeae25a442012-02-17 17:48:16 -0800341/*
342 Template child class to create pools of the correct type.
343*/
Dmitry-Me88145b82016-08-09 17:59:31 +0300344template< int ITEM_SIZE >
PKEuS95060352013-07-26 10:42:44 +0200345class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800346{
347public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200348 MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700349 ~MemPoolT() {
Lee Thomasonf07b9522014-10-30 13:25:12 -0700350 Clear();
351 }
352
353 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700354 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700355 while( !_blockPtrs.Empty()) {
Dmitry-Meeffab6f2017-07-26 17:57:50 +0300356 Block* lastBlock = _blockPtrs.Pop();
357 delete lastBlock;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700358 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700359 _root = 0;
360 _currentAllocs = 0;
361 _nAllocs = 0;
362 _maxAllocs = 0;
363 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700364 }
Lee Thomasond1983222012-02-06 08:41:24 -0800365
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700366 virtual int ItemSize() const {
Dmitry-Me88145b82016-08-09 17:59:31 +0300367 return ITEM_SIZE;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700368 }
369 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700370 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700371 }
Lee Thomasond1983222012-02-06 08:41:24 -0800372
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700373 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700374 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700375 // Need a new block.
376 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700377 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800378
Dmitry-Me88145b82016-08-09 17:59:31 +0300379 Item* blockItems = block->items;
380 for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
381 blockItems[i].next = &(blockItems[i + 1]);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700382 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300383 blockItems[ITEMS_PER_BLOCK - 1].next = 0;
384 _root = blockItems;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700385 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300386 Item* const result = _root;
387 TIXMLASSERT( result != 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700388 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800389
Lee Thomason624d43f2012-10-12 10:58:48 -0700390 ++_currentAllocs;
391 if ( _currentAllocs > _maxAllocs ) {
392 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700393 }
Dmitry-Me3161a332016-09-02 16:58:06 +0300394 ++_nAllocs;
395 ++_nUntracked;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700396 return result;
397 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700398
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700399 virtual void Free( void* mem ) {
400 if ( !mem ) {
401 return;
402 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700403 --_currentAllocs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300404 Item* item = static_cast<Item*>( mem );
Peter Matula50689912018-01-09 12:52:26 +0100405#ifdef TINYXML2_DEBUG
Dmitry-Me88145b82016-08-09 17:59:31 +0300406 memset( item, 0xfe, sizeof( *item ) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700407#endif
Dmitry-Me88145b82016-08-09 17:59:31 +0300408 item->next = _root;
409 _root = item;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700410 }
411 void Trace( const char* name ) {
412 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Dmitry-Me88145b82016-08-09 17:59:31 +0300413 name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
414 ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700415 }
Lee Thomasond1983222012-02-06 08:41:24 -0800416
Lee Thomason5b0a6772012-11-19 13:54:42 -0800417 void SetTracked() {
Dmitry-Me3161a332016-09-02 16:58:06 +0300418 --_nUntracked;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800419 }
420
421 int Untracked() const {
422 return _nUntracked;
423 }
424
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800425 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
426 // The test file is large, 170k.
427 // Release: VS2010 gcc(no opt)
428 // 1k: 4000
429 // 2k: 4000
430 // 4k: 3900 21000
431 // 16k: 5200
432 // 32k: 4300
433 // 64k: 4000 21000
Dmitry-Me88145b82016-08-09 17:59:31 +0300434 // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
435 // in private part if ITEMS_PER_BLOCK is private
436 enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
Jerome Martinez7921df12012-10-24 11:45:44 +0200437
Lee Thomasond1983222012-02-06 08:41:24 -0800438private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300439 MemPoolT( const MemPoolT& ); // not supported
440 void operator=( const MemPoolT& ); // not supported
441
Dmitry-Me88145b82016-08-09 17:59:31 +0300442 union Item {
443 Item* next;
444 char itemData[ITEM_SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700445 };
446 struct Block {
Dmitry-Me88145b82016-08-09 17:59:31 +0300447 Item items[ITEMS_PER_BLOCK];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700448 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700449 DynArray< Block*, 10 > _blockPtrs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300450 Item* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800451
Lee Thomason624d43f2012-10-12 10:58:48 -0700452 int _currentAllocs;
453 int _nAllocs;
454 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800455 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800456};
457
Lee Thomason2c85a712012-01-31 08:24:24 -0800458
Lee Thomason56bdd022012-02-09 18:16:58 -0800459
460/**
461 Implements the interface to the "Visitor pattern" (see the Accept() method.)
462 If you call the Accept() method, it requires being passed a XMLVisitor
463 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200464 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800465 are simply called with Visit().
466
467 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700468 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800469
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700470 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800471 visiting). You need to only override methods that are interesting to you.
472
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600473 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800474
475 You should never change the document from a callback.
476
477 @sa XMLNode::Accept()
478*/
PKEuS16ed47d2013-07-06 12:02:43 +0200479class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800480{
481public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700482 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800483
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700484 /// Visit a document.
485 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
486 return true;
487 }
488 /// Visit a document.
489 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
490 return true;
491 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800492
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700493 /// Visit an element.
494 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
495 return true;
496 }
497 /// Visit an element.
498 virtual bool VisitExit( const XMLElement& /*element*/ ) {
499 return true;
500 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800501
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700502 /// Visit a declaration.
503 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
504 return true;
505 }
506 /// Visit a text node.
507 virtual bool Visit( const XMLText& /*text*/ ) {
508 return true;
509 }
510 /// Visit a comment node.
511 virtual bool Visit( const XMLComment& /*comment*/ ) {
512 return true;
513 }
514 /// Visit an unknown node.
515 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
516 return true;
517 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800518};
519
Dmitry-Me66d2a842014-11-08 15:24:52 +0300520// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900521enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900522 XML_SUCCESS = 0,
numatrumpetcd8550c2014-09-08 16:59:39 +0900523 XML_NO_ATTRIBUTE,
524 XML_WRONG_ATTRIBUTE_TYPE,
525 XML_ERROR_FILE_NOT_FOUND,
526 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
527 XML_ERROR_FILE_READ_ERROR,
Lee Thomason8bba8b42017-06-20 09:18:41 -0700528 UNUSED_XML_ERROR_ELEMENT_MISMATCH, // remove at next major version
numatrumpetcd8550c2014-09-08 16:59:39 +0900529 XML_ERROR_PARSING_ELEMENT,
530 XML_ERROR_PARSING_ATTRIBUTE,
Lee Thomason8bba8b42017-06-20 09:18:41 -0700531 UNUSED_XML_ERROR_IDENTIFYING_TAG, // remove at next major version
numatrumpetcd8550c2014-09-08 16:59:39 +0900532 XML_ERROR_PARSING_TEXT,
533 XML_ERROR_PARSING_CDATA,
534 XML_ERROR_PARSING_COMMENT,
535 XML_ERROR_PARSING_DECLARATION,
536 XML_ERROR_PARSING_UNKNOWN,
537 XML_ERROR_EMPTY_DOCUMENT,
538 XML_ERROR_MISMATCHED_ELEMENT,
539 XML_ERROR_PARSING,
540 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700541 XML_NO_TEXT_NODE,
Lee Thomasond946dda2018-04-05 09:11:08 -0700542 XML_ELEMENT_DEPTH_EXCEEDED,
Lee Thomason331596e2014-09-11 14:56:43 -0700543
544 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900545};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900546
numatrumpetcd8550c2014-09-08 16:59:39 +0900547
U-Stream\Leeae25a442012-02-17 17:48:16 -0800548/*
549 Utility functionality.
550*/
Lee Thomasonce667c92016-12-26 16:45:30 -0800551class TINYXML2_LIB XMLUtil
Lee Thomason56bdd022012-02-09 18:16:58 -0800552{
Lee Thomasond1983222012-02-06 08:41:24 -0800553public:
kezenator4f756162016-11-29 19:46:27 +1000554 static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300555 TIXMLASSERT( p );
Lee Thomasone90e9012016-12-24 07:34:39 -0800556
Dmitry-Mefa20b222014-10-31 12:53:04 +0300557 while( IsWhiteSpace(*p) ) {
Lee Thomasone90e9012016-12-24 07:34:39 -0800558 if (curLineNumPtr && *p == '\n') {
kezenator4f756162016-11-29 19:46:27 +1000559 ++(*curLineNumPtr);
kezenatorec694152016-11-26 17:21:43 +1000560 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700561 ++p;
562 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300563 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700564 return p;
565 }
kezenator4f756162016-11-29 19:46:27 +1000566 static char* SkipWhiteSpace( char* p, int* curLineNumPtr ) {
567 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700568 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300569
570 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
571 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700572 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100573 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700574 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200575
576 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300577 if ( ch >= 128 ) {
578 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
579 return true;
580 }
581 if ( isalpha( ch ) ) {
582 return true;
583 }
584 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200585 }
586
587 inline static bool IsNameChar( unsigned char ch ) {
588 return IsNameStartChar( ch )
589 || isdigit( ch )
590 || ch == '.'
591 || ch == '-';
592 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800593
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700594 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700595 if ( p == q ) {
596 return true;
597 }
Dmitry-Me21f99692016-10-03 13:01:57 +0300598 TIXMLASSERT( p );
599 TIXMLASSERT( q );
600 TIXMLASSERT( nChar >= 0 );
Lee Thomason598a88d2015-10-09 14:42:12 -0700601 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700602 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200603
Dmitry-Me7865aad2015-06-19 16:23:35 +0300604 inline static bool IsUTF8Continuation( char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400605 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700606 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800607
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700608 static const char* ReadBOM( const char* p, bool* hasBOM );
609 // p is the starting location,
610 // the UTF-8 value of the entity will be placed in value, and length filled in.
611 static const char* GetCharacterRef( const char* p, char* value, int* length );
612 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700613
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700614 // converts primitive types to strings
615 static void ToStr( int v, char* buffer, int bufferSize );
616 static void ToStr( unsigned v, char* buffer, int bufferSize );
617 static void ToStr( bool v, char* buffer, int bufferSize );
618 static void ToStr( float v, char* buffer, int bufferSize );
619 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason51c12712016-06-04 20:18:49 -0700620 static void ToStr(int64_t v, char* buffer, int bufferSize);
Lee Thomason21be8822012-07-15 17:27:22 -0700621
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700622 // converts strings to primitive types
623 static bool ToInt( const char* str, int* value );
624 static bool ToUnsigned( const char* str, unsigned* value );
625 static bool ToBool( const char* str, bool* value );
626 static bool ToFloat( const char* str, float* value );
627 static bool ToDouble( const char* str, double* value );
Lee Thomason51c12712016-06-04 20:18:49 -0700628 static bool ToInt64(const char* str, int64_t* value);
Lee Thomasonce667c92016-12-26 16:45:30 -0800629
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800630 // Changes what is serialized for a boolean value.
631 // Default to "true" and "false". Shouldn't be changed
632 // unless you have a special testing or compatibility need.
Lee Thomasonce667c92016-12-26 16:45:30 -0800633 // Be careful: static, global, & not thread safe.
634 // Be sure to set static const memory as parameters.
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800635 static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
Lee Thomasonce667c92016-12-26 16:45:30 -0800636
637private:
Lee Thomasonf458d262016-12-26 22:47:25 -0800638 static const char* writeBoolTrue;
639 static const char* writeBoolFalse;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800640};
641
Lee Thomason5cae8972012-01-24 18:03:07 -0800642
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800643/** XMLNode is a base class for every object that is in the
644 XML Document Object Model (DOM), except XMLAttributes.
645 Nodes have siblings, a parent, and children which can
646 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700647 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800648 be cast to its more defined type.
649
Thomas Roß08bdf502012-05-12 14:21:23 +0200650 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800651 When the XMLDocument gets deleted, all its Nodes
652 will also be deleted.
653
654 @verbatim
655 A Document can contain: Element (container or leaf)
656 Comment (leaf)
657 Unknown (leaf)
658 Declaration( leaf )
659
660 An Element can contain: Element (container or leaf)
661 Text (leaf)
662 Attributes (not on tree)
663 Comment (leaf)
664 Unknown (leaf)
665
666 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800667*/
PKEuS16ed47d2013-07-06 12:02:43 +0200668class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800669{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700670 friend class XMLDocument;
671 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800672public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800673
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700674 /// Get the XMLDocument that owns this XMLNode.
675 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300676 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700677 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700678 }
679 /// Get the XMLDocument that owns this XMLNode.
680 XMLDocument* GetDocument() {
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 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800684
Lee Thomason2fa81722012-11-09 12:37:46 -0800685 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700686 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100687 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700688 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800689 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700690 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100691 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700692 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800693 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700694 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100695 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700696 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800697 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700698 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100699 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700700 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800701 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100703 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700704 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800705 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700706 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100707 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700708 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800709
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700710 virtual const XMLElement* ToElement() const {
711 return 0;
712 }
713 virtual const XMLText* ToText() const {
714 return 0;
715 }
716 virtual const XMLComment* ToComment() const {
717 return 0;
718 }
719 virtual const XMLDocument* ToDocument() const {
720 return 0;
721 }
722 virtual const XMLDeclaration* ToDeclaration() const {
723 return 0;
724 }
725 virtual const XMLUnknown* ToUnknown() const {
726 return 0;
727 }
Lee Thomason751da522012-02-10 08:50:51 -0800728
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700729 /** The meaning of 'value' changes for the specific type.
730 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530731 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700732 Element: name of the element
733 Comment: the comment text
734 Unknown: the tag contents
735 Text: the text string
736 @endverbatim
737 */
Michael Daumling21626882013-10-22 17:03:37 +0200738 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100739
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700740 /** Set the Value of an XML node.
741 @sa Value()
742 */
743 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800744
kezenatorec694152016-11-26 17:21:43 +1000745 /// Gets the line number the node is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +1000746 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +1000747
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700748 /// Get the parent of this node on the DOM.
749 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700750 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700751 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100752
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700753 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700754 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700755 }
Lee Thomason751da522012-02-10 08:50:51 -0800756
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700757 /// Returns true if this node has no children.
758 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700759 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700760 }
Lee Thomason751da522012-02-10 08:50:51 -0800761
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700762 /// Get the first child node, or null if none exists.
763 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700764 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700765 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100766
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700767 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700768 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700769 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100770
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700771 /** Get the first child element, or optionally the first child
772 element with the specified name.
773 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300774 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700775
Dmitry-Me886ad972015-07-22 11:00:51 +0300776 XMLElement* FirstChildElement( const char* name = 0 ) {
777 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700778 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800779
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700780 /// Get the last child node, or null if none exists.
781 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700782 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700783 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700784
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700785 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300786 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700787 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800788
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700789 /** Get the last child element or optionally the last child
790 element with the specified name.
791 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300792 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700793
Dmitry-Me886ad972015-07-22 11:00:51 +0300794 XMLElement* LastChildElement( const char* name = 0 ) {
795 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700796 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700797
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700798 /// Get the previous (left) sibling node of this node.
799 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700800 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700801 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700802
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700803 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700804 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700805 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800806
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700807 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300808 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700809
Dmitry-Me886ad972015-07-22 11:00:51 +0300810 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
811 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700812 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700813
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700814 /// Get the next (right) sibling node of this node.
815 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700816 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700817 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700818
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700819 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700820 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700821 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700822
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700823 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300824 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700825
Dmitry-Me886ad972015-07-22 11:00:51 +0300826 XMLElement* NextSiblingElement( const char* name = 0 ) {
827 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700828 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800829
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700830 /**
831 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200832 If the child node is already part of the document,
833 it is moved from its old location to the new location.
834 Returns the addThis argument or 0 if the node does not
835 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700836 */
837 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800838
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700839 XMLNode* LinkEndChild( XMLNode* addThis ) {
840 return InsertEndChild( addThis );
841 }
842 /**
843 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200844 If the child node is already part of the document,
845 it is moved from its old location to the new location.
846 Returns the addThis argument or 0 if the node does not
847 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700848 */
849 XMLNode* InsertFirstChild( XMLNode* addThis );
850 /**
851 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200852 If the child node is already part of the document,
853 it is moved from its old location to the new location.
854 Returns the addThis argument or 0 if the afterThis node
855 is not a child of this node, or if the node does not
856 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700857 */
858 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700859
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700860 /**
861 Delete all the children of this node.
862 */
863 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800864
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700865 /**
866 Delete a child of this node.
867 */
868 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800869
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700870 /**
871 Make a copy of this node, but not its children.
872 You may pass in a Document pointer that will be
873 the owner of the new Node. If the 'document' is
874 null, then the node returned will be allocated
875 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800876
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700877 Note: if called on a XMLDocument, this will return null.
878 */
879 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800880
Lee Thomason7085f002017-06-01 18:09:43 -0700881 /**
Lee Thomason1346a172017-06-14 15:14:19 -0700882 Make a copy of this node and all its children.
Lee Thomason7085f002017-06-01 18:09:43 -0700883
Dmitry-Me3f63f212017-06-19 18:25:19 +0300884 If the 'target' is null, then the nodes will
885 be allocated in the current document. If 'target'
Lee Thomason1346a172017-06-14 15:14:19 -0700886 is specified, the memory will be allocated is the
887 specified XMLDocument.
Lee Thomason7085f002017-06-01 18:09:43 -0700888
889 NOTE: This is probably not the correct tool to
890 copy a document, since XMLDocuments can have multiple
Lee Thomason1346a172017-06-14 15:14:19 -0700891 top level XMLNodes. You probably want to use
892 XMLDocument::DeepCopy()
Lee Thomason7085f002017-06-01 18:09:43 -0700893 */
Dmitry-Me3f63f212017-06-19 18:25:19 +0300894 XMLNode* DeepClone( XMLDocument* target ) const;
Lee Thomason7085f002017-06-01 18:09:43 -0700895
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700896 /**
897 Test if 2 nodes are the same, but don't test children.
898 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800899
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700900 Note: if called on a XMLDocument, this will return false.
901 */
902 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800903
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600904 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700905 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600906 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800907
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600908 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
909 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700910 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800911
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700912 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800913
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700914 - http://www.saxproject.org/
915 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800916
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700917 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800918
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700919 An example of using Accept():
920 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600921 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700922 tinyxmlDoc.Accept( &printer );
923 const char* xmlcstr = printer.CStr();
924 @endverbatim
925 */
926 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800927
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700928 /**
929 Set user data into the XMLNode. TinyXML-2 in
930 no way processes or interprets user data.
931 It is initially 0.
932 */
933 void SetUserData(void* userData) { _userData = userData; }
934
935 /**
936 Get user data set into the XMLNode. TinyXML-2 in
937 no way processes or interprets user data.
938 It is initially 0.
939 */
940 void* GetUserData() const { return _userData; }
941
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800942protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700943 XMLNode( XMLDocument* );
944 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700945
Dmitry-Me10b8ecc2017-04-12 17:57:44 +0300946 virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300947
Lee Thomason624d43f2012-10-12 10:58:48 -0700948 XMLDocument* _document;
949 XMLNode* _parent;
950 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +1000951 int _parseLineNum;
Lee Thomason3f57d272012-01-11 15:30:03 -0800952
Lee Thomason624d43f2012-10-12 10:58:48 -0700953 XMLNode* _firstChild;
954 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800955
Lee Thomason624d43f2012-10-12 10:58:48 -0700956 XMLNode* _prev;
957 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800958
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700959 void* _userData;
960
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800961private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700962 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700963 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400964 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800965 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Meecb9b072016-10-12 16:44:59 +0300966 const XMLElement* ToElementWithName( const char* name ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300967
968 XMLNode( const XMLNode& ); // not supported
969 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800970};
971
972
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800973/** XML text.
974
975 Note that a text node can have child element nodes, for example:
976 @verbatim
977 <root>This is <b>bold</b></root>
978 @endverbatim
979
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700980 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800981 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 -0700982 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600983 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800984*/
PKEuS16ed47d2013-07-06 12:02:43 +0200985class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800986{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700987 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800988public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700989 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800990
Lee Thomason624d43f2012-10-12 10:58:48 -0700991 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700992 return this;
993 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700994 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700995 return this;
996 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800997
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700998 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700999 void SetCData( bool isCData ) {
1000 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001001 }
1002 /// Returns true if this is a CDATA text element.
1003 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001004 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001005 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001006
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001007 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1008 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001009
Lee Thomason5492a1c2012-01-23 15:32:10 -08001010protected:
Lee Thomason624d43f2012-10-12 10:58:48 -07001011 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001012 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -08001013
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001014 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001015
Lee Thomason5492a1c2012-01-23 15:32:10 -08001016private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001017 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +03001018
1019 XMLText( const XMLText& ); // not supported
1020 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -08001021};
1022
1023
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001024/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +02001025class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001026{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001027 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -08001028public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001029 virtual XMLComment* ToComment() {
1030 return this;
1031 }
1032 virtual const XMLComment* ToComment() const {
1033 return this;
1034 }
Lee Thomasonce0763e2012-01-11 15:43:54 -08001035
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001036 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001037
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001038 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1039 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -08001040
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001041protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001042 XMLComment( XMLDocument* doc );
1043 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001044
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001045 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001046
Lee Thomason3f57d272012-01-11 15:30:03 -08001047private:
Dmitry-Mef547a992015-01-09 15:17:09 +03001048 XMLComment( const XMLComment& ); // not supported
1049 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001050};
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001051
1052
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001053/** In correct XML the declaration is the first entry in the file.
1054 @verbatim
1055 <?xml version="1.0" standalone="yes"?>
1056 @endverbatim
1057
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001058 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001059 however.
1060
1061 The text of the declaration isn't interpreted. It is parsed
1062 and written as a string.
1063*/
PKEuS16ed47d2013-07-06 12:02:43 +02001064class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001065{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001066 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001067public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001068 virtual XMLDeclaration* ToDeclaration() {
1069 return this;
1070 }
1071 virtual const XMLDeclaration* ToDeclaration() const {
1072 return this;
1073 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001074
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001075 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001076
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001077 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1078 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001079
1080protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001081 XMLDeclaration( XMLDocument* doc );
1082 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +03001083
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001084 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001085
Dmitry-Mef547a992015-01-09 15:17:09 +03001086private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001087 XMLDeclaration( const XMLDeclaration& ); // not supported
1088 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001089};
1090
1091
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001092/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001093 unknown. It is a tag of text, but should not be modified.
1094 It will be written back to the XML, unchanged, when the file
1095 is saved.
1096
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001097 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001098*/
PKEuS16ed47d2013-07-06 12:02:43 +02001099class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001100{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001101 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001102public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001103 virtual XMLUnknown* ToUnknown() {
1104 return this;
1105 }
1106 virtual const XMLUnknown* ToUnknown() const {
1107 return this;
1108 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001109
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001110 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001111
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001112 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1113 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001114
1115protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001116 XMLUnknown( XMLDocument* doc );
1117 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001118
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001119 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001120
Dmitry-Mef547a992015-01-09 15:17:09 +03001121private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001122 XMLUnknown( const XMLUnknown& ); // not supported
1123 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001124};
1125
1126
Lee Thomason1ff38e02012-02-14 18:18:16 -08001127
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001128/** An attribute is a name-value pair. Elements have an arbitrary
1129 number of attributes, each with a unique name.
1130
1131 @note The attributes are not XMLNodes. You may only query the
1132 Next() attribute in a list.
1133*/
PKEuS16ed47d2013-07-06 12:02:43 +02001134class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001135{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001136 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001137public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001138 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001139 const char* Name() const;
1140
Lee Thomason2fa81722012-11-09 12:37:46 -08001141 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001142 const char* Value() const;
1143
kezenatorec694152016-11-26 17:21:43 +10001144 /// Gets the line number the attribute is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +10001145 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +10001146
Lee Thomason2fa81722012-11-09 12:37:46 -08001147 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001148 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001149 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001150 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001151
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001152 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001153 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001154 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001155 */
Lee Thomason51c12712016-06-04 20:18:49 -07001156 int IntValue() const {
1157 int i = 0;
1158 QueryIntValue(&i);
1159 return i;
1160 }
1161
1162 int64_t Int64Value() const {
1163 int64_t i = 0;
1164 QueryInt64Value(&i);
1165 return i;
1166 }
1167
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001168 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001169 unsigned UnsignedValue() const {
1170 unsigned i=0;
1171 QueryUnsignedValue( &i );
1172 return i;
1173 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001174 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001175 bool BoolValue() const {
1176 bool b=false;
1177 QueryBoolValue( &b );
1178 return b;
1179 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001180 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001181 double DoubleValue() const {
1182 double d=0;
1183 QueryDoubleValue( &d );
1184 return d;
1185 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001186 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001187 float FloatValue() const {
1188 float f=0;
1189 QueryFloatValue( &f );
1190 return f;
1191 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001192
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001193 /** QueryIntValue interprets the attribute as an integer, and returns the value
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001194 in the provided parameter. The function will return XML_SUCCESS on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001195 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1196 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001197 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001198 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001199 XMLError QueryUnsignedValue( unsigned int* value ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001200 /// See QueryIntValue
1201 XMLError QueryInt64Value(int64_t* value) const;
1202 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001203 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001204 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001205 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001206 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001207 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001208
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001209 /// Set the attribute to a string value.
1210 void SetAttribute( const char* value );
1211 /// Set the attribute to value.
1212 void SetAttribute( int value );
1213 /// Set the attribute to value.
1214 void SetAttribute( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001215 /// Set the attribute to value.
1216 void SetAttribute(int64_t value);
1217 /// Set the attribute to value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001218 void SetAttribute( bool value );
1219 /// Set the attribute to value.
1220 void SetAttribute( double value );
1221 /// Set the attribute to value.
1222 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001223
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001224private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001225 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001226
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001227 XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001228 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001229
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001230 XMLAttribute( const XMLAttribute& ); // not supported
1231 void operator=( const XMLAttribute& ); // not supported
1232 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001233
kezenator4f756162016-11-29 19:46:27 +10001234 char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001235
Lee Thomason624d43f2012-10-12 10:58:48 -07001236 mutable StrPair _name;
1237 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +10001238 int _parseLineNum;
Lee Thomason624d43f2012-10-12 10:58:48 -07001239 XMLAttribute* _next;
1240 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001241};
1242
1243
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001244/** The element is a container class. It has a value, the element name,
1245 and can contain other elements, text, comments, and unknowns.
1246 Elements also contain an arbitrary number of attributes.
1247*/
PKEuS16ed47d2013-07-06 12:02:43 +02001248class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001249{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001250 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001251public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001252 /// Get the name of an element (which is the Value() of the node.)
1253 const char* Name() const {
1254 return Value();
1255 }
1256 /// Set the name of the element.
1257 void SetName( const char* str, bool staticMem=false ) {
1258 SetValue( str, staticMem );
1259 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001260
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001261 virtual XMLElement* ToElement() {
1262 return this;
1263 }
1264 virtual const XMLElement* ToElement() const {
1265 return this;
1266 }
1267 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001268
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001269 /** Given an attribute name, Attribute() returns the value
1270 for the attribute of that name, or null if none
1271 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001272
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001273 @verbatim
1274 const char* value = ele->Attribute( "foo" );
1275 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001276
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001277 The 'value' parameter is normally null. However, if specified,
1278 the attribute will only be returned if the 'name' and 'value'
1279 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001280
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001281 @verbatim
1282 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1283 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001284
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001285 rather than:
1286 @verbatim
1287 if ( ele->Attribute( "foo" ) ) {
1288 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1289 }
1290 @endverbatim
1291 */
1292 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001293
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001294 /** Given an attribute name, IntAttribute() returns the value
Lee Thomason13cbc9a2016-10-27 14:55:07 -07001295 of the attribute interpreted as an integer. The default
1296 value will be returned if the attribute isn't present,
1297 or if there is an error. (For a method with error
1298 checking, see QueryIntAttribute()).
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001299 */
Josh Wittnercf3dd092016-10-11 18:57:17 -07001300 int IntAttribute(const char* name, int defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001301 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001302 unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001303 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001304 int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001305 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001306 bool BoolAttribute(const char* name, bool defaultValue = false) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001307 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001308 double DoubleAttribute(const char* name, double defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001309 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001310 float FloatAttribute(const char* name, float defaultValue = 0) const;
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001311
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001312 /** Given an attribute name, QueryIntAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001313 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001314 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1315 doesn't exist. If successful, the result of the conversion
1316 will be written to 'value'. If not successful, nothing will
1317 be written to 'value'. This allows you to provide default
1318 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001319
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001320 @verbatim
1321 int value = 10;
1322 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1323 @endverbatim
1324 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001325 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001326 const XMLAttribute* a = FindAttribute( name );
1327 if ( !a ) {
1328 return XML_NO_ATTRIBUTE;
1329 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001330 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001331 }
Lee Thomason51c12712016-06-04 20:18:49 -07001332
1333 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001334 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001335 const XMLAttribute* a = FindAttribute( name );
1336 if ( !a ) {
1337 return XML_NO_ATTRIBUTE;
1338 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001339 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001340 }
Lee Thomason51c12712016-06-04 20:18:49 -07001341
1342 /// See QueryIntAttribute()
1343 XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1344 const XMLAttribute* a = FindAttribute(name);
1345 if (!a) {
1346 return XML_NO_ATTRIBUTE;
1347 }
1348 return a->QueryInt64Value(value);
1349 }
1350
1351 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001352 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001353 const XMLAttribute* a = FindAttribute( name );
1354 if ( !a ) {
1355 return XML_NO_ATTRIBUTE;
1356 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001357 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001358 }
1359 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001360 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001361 const XMLAttribute* a = FindAttribute( name );
1362 if ( !a ) {
1363 return XML_NO_ATTRIBUTE;
1364 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001365 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001366 }
1367 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001368 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001369 const XMLAttribute* a = FindAttribute( name );
1370 if ( !a ) {
1371 return XML_NO_ATTRIBUTE;
1372 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001373 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001374 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001375
Lee Thomason5b00e062017-12-28 14:07:47 -08001376 /// See QueryIntAttribute()
1377 XMLError QueryStringAttribute(const char* name, const char** value) const {
1378 const XMLAttribute* a = FindAttribute(name);
1379 if (!a) {
1380 return XML_NO_ATTRIBUTE;
1381 }
1382 *value = a->Value();
1383 return XML_SUCCESS;
1384 }
1385
1386
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001387
1388 /** Given an attribute name, QueryAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001389 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001390 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1391 doesn't exist. It is overloaded for the primitive types,
1392 and is a generally more convenient replacement of
1393 QueryIntAttribute() and related functions.
1394
1395 If successful, the result of the conversion
1396 will be written to 'value'. If not successful, nothing will
1397 be written to 'value'. This allows you to provide default
1398 value:
1399
1400 @verbatim
1401 int value = 10;
1402 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1403 @endverbatim
1404 */
1405 int QueryAttribute( const char* name, int* value ) const {
1406 return QueryIntAttribute( name, value );
1407 }
1408
1409 int QueryAttribute( const char* name, unsigned int* value ) const {
1410 return QueryUnsignedAttribute( name, value );
1411 }
1412
Lee Thomason51c12712016-06-04 20:18:49 -07001413 int QueryAttribute(const char* name, int64_t* value) const {
1414 return QueryInt64Attribute(name, value);
1415 }
1416
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001417 int QueryAttribute( const char* name, bool* value ) const {
1418 return QueryBoolAttribute( name, value );
1419 }
1420
1421 int QueryAttribute( const char* name, double* value ) const {
1422 return QueryDoubleAttribute( name, value );
1423 }
1424
1425 int QueryAttribute( const char* name, float* value ) const {
1426 return QueryFloatAttribute( name, value );
1427 }
1428
1429 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001430 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001431 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001432 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001433 }
1434 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001435 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001436 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001437 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001438 }
1439 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001440 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001441 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001442 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001443 }
Lee Thomason51c12712016-06-04 20:18:49 -07001444
1445 /// Sets the named attribute to value.
1446 void SetAttribute(const char* name, int64_t value) {
1447 XMLAttribute* a = FindOrCreateAttribute(name);
1448 a->SetAttribute(value);
1449 }
1450
1451 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001452 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001453 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001454 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001455 }
1456 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001457 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001458 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001459 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001460 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001461 /// Sets the named attribute to value.
1462 void SetAttribute( const char* name, float value ) {
1463 XMLAttribute* a = FindOrCreateAttribute( name );
1464 a->SetAttribute( value );
1465 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001466
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001467 /**
1468 Delete an attribute.
1469 */
1470 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001471
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001472 /// Return the first attribute in the list.
1473 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001474 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001475 }
1476 /// Query a specific attribute in the list.
1477 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001478
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001479 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001480 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001481 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001482
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001483 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001484 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001485
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001486 This is a convenient method for getting the text of simple contained text:
1487 @verbatim
1488 <foo>This is text</foo>
1489 const char* str = fooElement->GetText();
1490 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001491
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001492 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001493
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001494 Note that this function can be misleading. If the element foo was created from
1495 this XML:
1496 @verbatim
1497 <foo><b>This is text</b></foo>
1498 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001499
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001500 then the value of str would be null. The first child node isn't a text node, it is
1501 another element. From this XML:
1502 @verbatim
1503 <foo>This is <b>text</b></foo>
1504 @endverbatim
1505 GetText() will return "This is ".
1506 */
1507 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001508
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001509 /** Convenience function for easy access to the text inside an element. Although easy
1510 and concise, SetText() is limited compared to creating an XMLText child
1511 and mutating it directly.
1512
1513 If the first child of 'this' is a XMLText, SetText() sets its value to
1514 the given string, otherwise it will create a first child that is an XMLText.
1515
1516 This is a convenient method for setting the text of simple contained text:
1517 @verbatim
1518 <foo>This is text</foo>
1519 fooElement->SetText( "Hullaballoo!" );
1520 <foo>Hullaballoo!</foo>
1521 @endverbatim
1522
1523 Note that this function can be misleading. If the element foo was created from
1524 this XML:
1525 @verbatim
1526 <foo><b>This is text</b></foo>
1527 @endverbatim
1528
1529 then it will not change "This is text", but rather prefix it with a text element:
1530 @verbatim
1531 <foo>Hullaballoo!<b>This is text</b></foo>
1532 @endverbatim
1533
1534 For this XML:
1535 @verbatim
1536 <foo />
1537 @endverbatim
1538 SetText() will generate
1539 @verbatim
1540 <foo>Hullaballoo!</foo>
1541 @endverbatim
1542 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001543 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001544 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001545 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001546 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001547 void SetText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001548 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1549 void SetText(int64_t value);
1550 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001551 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001552 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001553 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001554 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001555 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001556
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001557 /**
1558 Convenience method to query the value of a child text node. This is probably best
1559 shown by example. Given you have a document is this form:
1560 @verbatim
1561 <point>
1562 <x>1</x>
1563 <y>1.4</y>
1564 </point>
1565 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001566
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001567 The QueryIntText() and similar functions provide a safe and easier way to get to the
1568 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001569
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001570 @verbatim
1571 int x = 0;
1572 float y = 0; // types of x and y are contrived for example
1573 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1574 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1575 xElement->QueryIntText( &x );
1576 yElement->QueryFloatText( &y );
1577 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001578
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001579 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1580 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 -07001581
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001582 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001583 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001584 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001585 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001586 /// See QueryIntText()
1587 XMLError QueryInt64Text(int64_t* uval) const;
1588 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001589 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001590 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001591 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001592 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001593 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001594
Josh Wittnercf3dd092016-10-11 18:57:17 -07001595 int IntText(int defaultValue = 0) const;
1596
Josh Wittner3a621f52016-09-12 19:17:54 -07001597 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001598 unsigned UnsignedText(unsigned defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001599 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001600 int64_t Int64Text(int64_t defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001601 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001602 bool BoolText(bool defaultValue = false) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001603 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001604 double DoubleText(double defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001605 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001606 float FloatText(float defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001607
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001608 // internal:
Dmitry-Mee5035632017-04-05 18:02:40 +03001609 enum ElementClosingType {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001610 OPEN, // <foo>
1611 CLOSED, // <foo/>
1612 CLOSING // </foo>
1613 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001614 ElementClosingType ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001615 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001616 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001617 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1618 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001619
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001620protected:
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001621 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001622
Lee Thomason50adb4c2012-02-13 15:07:09 -08001623private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001624 XMLElement( XMLDocument* doc );
1625 virtual ~XMLElement();
1626 XMLElement( const XMLElement& ); // not supported
1627 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001628
Dmitry-Me1227d512014-12-05 13:41:45 +03001629 XMLAttribute* FindAttribute( const char* name ) {
1630 return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
1631 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001632 XMLAttribute* FindOrCreateAttribute( const char* name );
1633 //void LinkAttribute( XMLAttribute* attrib );
kezenator4f756162016-11-29 19:46:27 +10001634 char* ParseAttributes( char* p, int* curLineNumPtr );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001635 static void DeleteAttribute( XMLAttribute* attribute );
Dmitry-Mea60caa22016-11-22 18:28:08 +03001636 XMLAttribute* CreateAttribute();
Lee Thomason67d61312012-01-24 16:01:51 -08001637
Lee Thomason5bb2d802014-01-24 10:42:57 -08001638 enum { BUF_SIZE = 200 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001639 ElementClosingType _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001640 // The attribute list is ordered; there is no 'lastAttribute'
1641 // because the list needs to be scanned for dupes before adding
1642 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001643 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001644};
1645
1646
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001647enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001648 PRESERVE_WHITESPACE,
1649 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001650};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001651
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001652
1653/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001654 It can be saved, loaded, and printed to the screen.
1655 All Nodes are connected and allocated to a Document.
1656 If the Document is deleted, all its Nodes are also deleted.
1657*/
PKEuS16ed47d2013-07-06 12:02:43 +02001658class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001659{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001660 friend class XMLElement;
Lee Thomasond946dda2018-04-05 09:11:08 -07001661 // Gives access to SetError and Push/PopDepth, but over-access for everything else.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001662 // Wishing C++ had "internal" scope.
1663 friend class XMLNode;
1664 friend class XMLText;
1665 friend class XMLComment;
1666 friend class XMLDeclaration;
1667 friend class XMLUnknown;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001668public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001669 /// constructor
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001670 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001671 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001672
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001673 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001674 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001675 return this;
1676 }
1677 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001678 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001679 return this;
1680 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001681
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001682 /**
1683 Parse an XML file from a character string.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001684 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001685 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001686
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001687 You may optionally pass in the 'nBytes', which is
1688 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001689 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001690 null terminated string.
1691 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001692 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001693
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001694 /**
1695 Load an XML file from disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001696 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001697 an errorID.
1698 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001699 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001700
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001701 /**
1702 Load an XML file from disk. You are responsible
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001703 for providing and closing the FILE*.
1704
1705 NOTE: The file should be opened as binary ("rb")
1706 not text in order for TinyXML-2 to correctly
1707 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001708
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001709 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001710 an errorID.
1711 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001712 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001713
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001714 /**
1715 Save the XML file to disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001716 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001717 an errorID.
1718 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001719 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001720
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001721 /**
1722 Save the XML file to disk. You are responsible
1723 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001724
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001725 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001726 an errorID.
1727 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001728 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001729
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001730 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001731 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001732 }
1733 Whitespace WhitespaceMode() const {
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001734 return _whitespaceMode;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001735 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001736
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001737 /**
1738 Returns true if this document has a leading Byte Order Mark of UTF8.
1739 */
1740 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001741 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001742 }
1743 /** Sets whether to write the BOM when writing the file.
1744 */
1745 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001746 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001747 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001748
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001749 /** Return the root element of DOM. Equivalent to FirstChildElement().
1750 To get the first node, use FirstChild().
1751 */
1752 XMLElement* RootElement() {
1753 return FirstChildElement();
1754 }
1755 const XMLElement* RootElement() const {
1756 return FirstChildElement();
1757 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001758
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001759 /** Print the Document. If the Printer is not provided, it will
1760 print to stdout. If you provide Printer, this can print to a file:
1761 @verbatim
1762 XMLPrinter printer( fp );
1763 doc.Print( &printer );
1764 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001765
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001766 Or you can use a printer to print to memory:
1767 @verbatim
1768 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001769 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001770 // printer.CStr() has a const char* to the XML
1771 @endverbatim
1772 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001773 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001774 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001775
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001776 /**
1777 Create a new Element associated with
1778 this Document. The memory for the Element
1779 is managed by the Document.
1780 */
1781 XMLElement* NewElement( const char* name );
1782 /**
1783 Create a new Comment associated with
1784 this Document. The memory for the Comment
1785 is managed by the Document.
1786 */
1787 XMLComment* NewComment( const char* comment );
1788 /**
1789 Create a new Text associated with
1790 this Document. The memory for the Text
1791 is managed by the Document.
1792 */
1793 XMLText* NewText( const char* text );
1794 /**
1795 Create a new Declaration associated with
1796 this Document. The memory for the object
1797 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001798
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001799 If the 'text' param is null, the standard
1800 declaration is used.:
1801 @verbatim
1802 <?xml version="1.0" encoding="UTF-8"?>
1803 @endverbatim
1804 */
1805 XMLDeclaration* NewDeclaration( const char* text=0 );
1806 /**
1807 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001808 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001809 is managed by the Document.
1810 */
1811 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001812
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001813 /**
1814 Delete a node associated with this document.
1815 It will be unlinked from the DOM.
1816 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001817 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001818
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001819 void ClearError() {
Lee Thomasonaa188392017-09-19 17:54:31 -07001820 SetError(XML_SUCCESS, 0, 0);
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001821 }
1822
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001823 /// Return true if there was an error parsing the document.
1824 bool Error() const {
Lee Thomason85536252016-06-04 19:10:53 -07001825 return _errorID != XML_SUCCESS;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001826 }
1827 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001828 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001829 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001830 }
Lee Thomason331596e2014-09-11 14:56:43 -07001831 const char* ErrorName() const;
Lee Thomasone90e9012016-12-24 07:34:39 -08001832 static const char* ErrorIDToName(XMLError errorID);
Lee Thomason331596e2014-09-11 14:56:43 -07001833
Lee Thomasonf49b9652017-10-11 10:57:49 -07001834 /** Returns a "long form" error description. A hopefully helpful
1835 diagnostic with location, line number, and/or additional info.
1836 */
1837 const char* ErrorStr() const;
1838
1839 /// A (trivial) utility function that prints the ErrorStr() to stdout.
1840 void PrintError() const;
Lee Thomason8c9e3132017-06-26 16:55:01 -07001841
kezenatorec694152016-11-26 17:21:43 +10001842 /// Return the line where the error occured, or zero if unknown.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001843 int ErrorLineNum() const
kezenatorec694152016-11-26 17:21:43 +10001844 {
1845 return _errorLineNum;
1846 }
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001847
1848 /// Clear the document, resetting it to the initial state.
1849 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001850
Lee Thomason7085f002017-06-01 18:09:43 -07001851 /**
Lee Thomasonb29f5562017-06-01 18:45:32 -07001852 Copies this document to a target document.
Lee Thomason7085f002017-06-01 18:09:43 -07001853 The target will be completely cleared before the copy.
Lee Thomasonb29f5562017-06-01 18:45:32 -07001854 If you want to copy a sub-tree, see XMLNode::DeepClone().
Lee Thomason7085f002017-06-01 18:09:43 -07001855
Lee Thomason1346a172017-06-14 15:14:19 -07001856 NOTE: that the 'target' must be non-null.
Lee Thomason7085f002017-06-01 18:09:43 -07001857 */
Shuichiro Suzuki87cd4e02017-08-24 11:20:26 +09001858 void DeepCopy(XMLDocument* target) const;
Lee Thomason7085f002017-06-01 18:09:43 -07001859
1860 // internal
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001861 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001862
Lee Thomason816d3fa2017-06-05 14:35:55 -07001863 // internal
1864 void MarkInUse(XMLNode*);
1865
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001866 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1867 return 0;
1868 }
1869 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1870 return false;
1871 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001872
Lee Thomason3f57d272012-01-11 15:30:03 -08001873private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001874 XMLDocument( const XMLDocument& ); // not supported
1875 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001876
Lee Thomasone90e9012016-12-24 07:34:39 -08001877 bool _writeBOM;
1878 bool _processEntities;
1879 XMLError _errorID;
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001880 Whitespace _whitespaceMode;
Lee Thomasonaa188392017-09-19 17:54:31 -07001881 mutable StrPair _errorStr;
Lee Thomasone90e9012016-12-24 07:34:39 -08001882 int _errorLineNum;
1883 char* _charBuffer;
1884 int _parseCurLineNum;
Lee Thomasond946dda2018-04-05 09:11:08 -07001885 int _parsingDepth;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001886 // Memory tracking does add some overhead.
1887 // However, the code assumes that you don't
1888 // have a bunch of unlinked nodes around.
1889 // Therefore it takes less memory to track
1890 // in the document vs. a linked list in the XMLNode,
1891 // and the performance is the same.
1892 DynArray<XMLNode*, 10> _unlinked;
Lee Thomasond1983222012-02-06 08:41:24 -08001893
Lee Thomason624d43f2012-10-12 10:58:48 -07001894 MemPoolT< sizeof(XMLElement) > _elementPool;
1895 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1896 MemPoolT< sizeof(XMLText) > _textPool;
1897 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001898
1899 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001900
1901 void Parse();
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001902
Lee Thomasonf49b9652017-10-11 10:57:49 -07001903 void SetError( XMLError error, int lineNum, const char* format, ... );
1904
Lee Thomasond946dda2018-04-05 09:11:08 -07001905 // Something of an obvious security hole, once it was discovered.
1906 // Either an ill-formed XML or an excessively deep one can overflow
1907 // the stack. Track stack depth, and error out if needed.
1908 class DepthTracker {
1909 public:
1910 DepthTracker(XMLDocument * document) {
1911 this->_document = document;
1912 document->PushDepth();
1913 }
1914 ~DepthTracker() {
1915 _document->PopDepth();
1916 }
1917 private:
1918 XMLDocument * _document;
1919 };
Lee Thomasonf928c352018-04-05 09:24:20 -07001920 void PushDepth();
1921 void PopDepth();
Lee Thomasond946dda2018-04-05 09:11:08 -07001922
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001923 template<class NodeType, int PoolElementSize>
1924 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
Lee Thomason5cae8972012-01-24 18:03:07 -08001925};
1926
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001927template<class NodeType, int PoolElementSize>
1928inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1929{
1930 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1931 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1932 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1933 TIXMLASSERT( returnNode );
1934 returnNode->_memPool = &pool;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001935
1936 _unlinked.Push(returnNode);
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001937 return returnNode;
1938}
Lee Thomason7c913cd2012-01-26 18:32:34 -08001939
Lee Thomason3ffdd392012-03-28 17:27:55 -07001940/**
1941 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001942 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001943 DOM structure. It is a separate utility class.
1944
1945 Take an example:
1946 @verbatim
1947 <Document>
1948 <Element attributeA = "valueA">
1949 <Child attributeB = "value1" />
1950 <Child attributeB = "value2" />
1951 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001952 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001953 @endverbatim
1954
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001955 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001956 easy to write a *lot* of code that looks like:
1957
1958 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001959 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001960 if ( root )
1961 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001962 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001963 if ( element )
1964 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001965 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001966 if ( child )
1967 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001968 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001969 if ( child2 )
1970 {
1971 // Finally do something useful.
1972 @endverbatim
1973
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001974 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001975 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001976 and correct to use:
1977
1978 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001979 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001980 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001981 if ( child2 )
1982 {
1983 // do something useful
1984 @endverbatim
1985
1986 Which is MUCH more concise and useful.
1987
1988 It is also safe to copy handles - internally they are nothing more than node pointers.
1989 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001990 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001991 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001992
1993 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001994*/
PKEuS16ed47d2013-07-06 12:02:43 +02001995class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001996{
1997public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001998 /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001999 XMLHandle( XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002000 }
2001 /// Create a handle from a node.
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002002 XMLHandle( XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002003 }
2004 /// Copy constructor
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002005 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002006 }
2007 /// Assignment
2008 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002009 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002010 return *this;
2011 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002012
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002013 /// Get the first child of this handle.
2014 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002015 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002016 }
2017 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002018 XMLHandle FirstChildElement( const char* name = 0 ) {
2019 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002020 }
2021 /// Get the last child of this handle.
2022 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002023 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002024 }
2025 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002026 XMLHandle LastChildElement( const char* name = 0 ) {
2027 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002028 }
2029 /// Get the previous sibling of this handle.
2030 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002031 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002032 }
2033 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002034 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
2035 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002036 }
2037 /// Get the next sibling of this handle.
2038 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002039 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002040 }
2041 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002042 XMLHandle NextSiblingElement( const char* name = 0 ) {
2043 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002044 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002045
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002046 /// Safe cast to XMLNode. This can return null.
2047 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002048 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002049 }
2050 /// Safe cast to XMLElement. This can return null.
2051 XMLElement* ToElement() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002052 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002053 }
2054 /// Safe cast to XMLText. This can return null.
2055 XMLText* ToText() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002056 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002057 }
2058 /// Safe cast to XMLUnknown. This can return null.
2059 XMLUnknown* ToUnknown() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002060 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002061 }
2062 /// Safe cast to XMLDeclaration. This can return null.
2063 XMLDeclaration* ToDeclaration() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002064 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002065 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002066
2067private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002068 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002069};
2070
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08002071
2072/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002073 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
2074 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07002075*/
PKEuS16ed47d2013-07-06 12:02:43 +02002076class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07002077{
2078public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002079 XMLConstHandle( const XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002080 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002081 XMLConstHandle( const XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002082 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002083 XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002084 }
Lee Thomason8b899812012-04-04 15:58:16 -07002085
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002086 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002087 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002088 return *this;
2089 }
Lee Thomason8b899812012-04-04 15:58:16 -07002090
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002091 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002092 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002093 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002094 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2095 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002096 }
2097 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002098 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002099 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002100 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2101 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002102 }
2103 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002104 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002105 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002106 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2107 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002108 }
2109 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002110 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002111 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002112 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2113 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002114 }
Lee Thomason8b899812012-04-04 15:58:16 -07002115
2116
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002117 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002118 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002119 }
2120 const XMLElement* ToElement() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002121 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002122 }
2123 const XMLText* ToText() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002124 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002125 }
2126 const XMLUnknown* ToUnknown() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002127 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002128 }
2129 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002130 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002131 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08002132
Lee Thomason5cae8972012-01-24 18:03:07 -08002133private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002134 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08002135};
Lee Thomason6f381b72012-03-02 12:59:39 -08002136
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002137
2138/**
2139 Printing functionality. The XMLPrinter gives you more
2140 options than the XMLDocument::Print() method.
2141
2142 It can:
2143 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02002144 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002145 -# Print XML without a XMLDocument.
2146
2147 Print to Memory
2148
2149 @verbatim
2150 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06002151 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02002152 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002153 @endverbatim
2154
2155 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002156
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002157 You provide the file pointer.
2158 @verbatim
2159 XMLPrinter printer( fp );
2160 doc.Print( &printer );
2161 @endverbatim
2162
2163 Print without a XMLDocument
2164
2165 When loading, an XML parser is very useful. However, sometimes
2166 when saving, it just gets in the way. The code is often set up
2167 for streaming, and constructing the DOM is just overhead.
2168
2169 The Printer supports the streaming case. The following code
2170 prints out a trivially simple XML file without ever creating
2171 an XML document.
2172
2173 @verbatim
2174 XMLPrinter printer( fp );
2175 printer.OpenElement( "foo" );
2176 printer.PushAttribute( "foo", "bar" );
2177 printer.CloseElement();
2178 @endverbatim
2179*/
PKEuS16ed47d2013-07-06 12:02:43 +02002180class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002181{
2182public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002183 /** Construct the printer. If the FILE* is specified,
2184 this will print to the FILE. Else it will print
2185 to memory, and the result is available in CStr().
2186 If 'compact' is set to true, then output is created
2187 with only required whitespace and newlines.
2188 */
PKEuS1bfb9542013-08-04 13:51:17 +02002189 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002190 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002191
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002192 /** If streaming, write the BOM and declaration. */
2193 void PushHeader( bool writeBOM, bool writeDeclaration );
2194 /** If streaming, start writing an element.
2195 The element must be closed with CloseElement()
2196 */
Lee Thomason256adb62014-04-06 14:41:46 -07002197 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002198 /// If streaming, add an attribute to an open element.
2199 void PushAttribute( const char* name, const char* value );
2200 void PushAttribute( const char* name, int value );
2201 void PushAttribute( const char* name, unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002202 void PushAttribute(const char* name, int64_t value);
2203 void PushAttribute( const char* name, bool value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002204 void PushAttribute( const char* name, double value );
2205 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002206 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002207
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002208 /// Add a text node.
2209 void PushText( const char* text, bool cdata=false );
2210 /// Add a text node from an integer.
2211 void PushText( int value );
2212 /// Add a text node from an unsigned.
2213 void PushText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002214 /// Add a text node from an unsigned.
2215 void PushText(int64_t value);
2216 /// Add a text node from a bool.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002217 void PushText( bool value );
2218 /// Add a text node from a float.
2219 void PushText( float value );
2220 /// Add a text node from a double.
2221 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002222
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002223 /// Add a comment
2224 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002225
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002226 void PushDeclaration( const char* value );
2227 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002228
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002229 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2230 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2231 return true;
2232 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002233
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002234 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2235 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002236
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002237 virtual bool Visit( const XMLText& text );
2238 virtual bool Visit( const XMLComment& comment );
2239 virtual bool Visit( const XMLDeclaration& declaration );
2240 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002241
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002242 /**
2243 If in print to memory mode, return a pointer to
2244 the XML file in memory.
2245 */
2246 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002247 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002248 }
2249 /**
2250 If in print to memory mode, return the size
2251 of the XML file in memory. (Note the size returned
2252 includes the terminating null.)
2253 */
2254 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002255 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002256 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002257 /**
2258 If in print to memory mode, reset the buffer to the
2259 beginning.
2260 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002261 void ClearBuffer() {
2262 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002263 _buffer.Push(0);
Lee Thomason53858b42017-06-01 19:09:16 -07002264 _firstElement = true;
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002265 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002266
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002267protected:
Alexander Maid740b642014-05-20 22:04:42 +02002268 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002269
Lee Thomasonc18eb232014-02-21 17:31:17 -08002270 /** Prints out the space before an element. You may override to change
2271 the space and tabs used. A PrintSpace() override should call Print().
2272 */
2273 virtual void PrintSpace( int depth );
2274 void Print( const char* format, ... );
Alexander Golubevb2e08e42016-03-28 19:51:59 +03002275 void Write( const char* data, size_t size );
2276 inline void Write( const char* data ) { Write( data, strlen( data ) ); }
2277 void Putc( char ch );
Lee Thomasonc18eb232014-02-21 17:31:17 -08002278
Dmitry-Mea092bc12014-12-23 17:57:05 +03002279 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002280 bool _elementJustOpened;
2281 DynArray< const char*, 10 > _stack;
2282
2283private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002284 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002285
Lee Thomason624d43f2012-10-12 10:58:48 -07002286 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002287 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002288 int _depth;
2289 int _textDepth;
2290 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002291 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002292
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002293 enum {
2294 ENTITY_RANGE = 64,
2295 BUF_SIZE = 200
2296 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002297 bool _entityFlag[ENTITY_RANGE];
2298 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002299
Lee Thomason624d43f2012-10-12 10:58:48 -07002300 DynArray< char, 20 > _buffer;
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002301
2302 // Prohibit cloning, intentionally not implemented
2303 XMLPrinter( const XMLPrinter& );
2304 XMLPrinter& operator=( const XMLPrinter& );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002305};
2306
2307
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002308} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002309
PKEuS95060352013-07-26 10:42:44 +02002310#if defined(_MSC_VER)
2311# pragma warning(pop)
2312#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002313
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002314#endif // TINYXML2_INCLUDED