blob: e8b430f20b02fcddd27f231a6cf34e2c00de733c [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 Thomasond946dda2018-04-05 09:11:08 -0700109// This is problematic. There needs to be a limit to avoid a stack
110// overflow. However, that limit varies per system. Going with
111// the MS value for now. May adjust in future versions.
112static const int TINYXML2_MAX_ELEMENT_DEPTH = 256;
113
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800114namespace tinyxml2
115{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800116class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800117class XMLElement;
118class XMLAttribute;
119class XMLComment;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800120class XMLText;
Lee Thomason50f97b22012-02-11 16:33:40 -0800121class XMLDeclaration;
122class XMLUnknown;
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800123class XMLPrinter;
Lee Thomason5cae8972012-01-24 18:03:07 -0800124
U-Stream\Leeae25a442012-02-17 17:48:16 -0800125/*
126 A class that wraps strings. Normally stores the start and end
127 pointers into the XML file itself, and will apply normalization
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800128 and entity translation if actually read. Can also store (and memory
U-Stream\Leeae25a442012-02-17 17:48:16 -0800129 manage) a traditional char[]
130*/
PKEuS95060352013-07-26 10:42:44 +0200131class StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800132{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800133public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700134 enum {
135 NEEDS_ENTITY_PROCESSING = 0x01,
136 NEEDS_NEWLINE_NORMALIZATION = 0x02,
Dmitry-Me5420e542015-05-20 10:51:26 +0300137 NEEDS_WHITESPACE_COLLAPSING = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800138
Lee Thomason584af572016-09-05 14:14:16 -0700139 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700140 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
Lee Thomason584af572016-09-05 14:14:16 -0700141 ATTRIBUTE_NAME = 0,
142 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
143 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
144 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700145 };
Lee Thomason39ede242012-01-20 11:27:56 -0800146
Lee Thomason120b3a62012-10-12 10:06:59 -0700147 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700148 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800149
Lee Thomason120b3a62012-10-12 10:06:59 -0700150 void Set( char* start, char* end, int flags ) {
Dmitry-Me6fc38ec2016-09-01 17:59:44 +0300151 TIXMLASSERT( start );
152 TIXMLASSERT( end );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700153 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700154 _start = start;
155 _end = end;
156 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700157 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700158
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700159 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700160
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700161 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700162 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700163 }
Lee Thomason39ede242012-01-20 11:27:56 -0800164
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700165 void SetInternedStr( const char* str ) {
166 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700167 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700168 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700169
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700170 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800171
kezenator4f756162016-11-29 19:46:27 +1000172 char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700173 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800174
Lee Thomason29658802014-11-27 22:31:11 -0800175 void TransferTo( StrPair* other );
Lee Thomason584af572016-09-05 14:14:16 -0700176 void Reset();
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300177
Lee Thomason39ede242012-01-20 11:27:56 -0800178private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700179 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800180
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700181 enum {
182 NEEDS_FLUSH = 0x100,
183 NEEDS_DELETE = 0x200
184 };
Lee Thomasone4422302012-01-20 17:59:50 -0800185
Lee Thomason120b3a62012-10-12 10:06:59 -0700186 int _flags;
187 char* _start;
188 char* _end;
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300189
190 StrPair( const StrPair& other ); // not supported
191 void operator=( StrPair& other ); // not supported, use TransferTo()
Lee Thomason39ede242012-01-20 11:27:56 -0800192};
193
U-Lama\Lee560bd472011-12-28 19:42:49 -0800194
U-Stream\Leeae25a442012-02-17 17:48:16 -0800195/*
196 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
197 Has a small initial memory pool, so that low or no usage will not
198 cause a call to new/delete
199*/
Dmitry-Me04009222015-04-06 18:07:18 +0300200template <class T, int INITIAL_SIZE>
PKEuS95060352013-07-26 10:42:44 +0200201class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800202{
203public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200204 DynArray() :
205 _mem( _pool ),
206 _allocated( INITIAL_SIZE ),
207 _size( 0 )
208 {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700209 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700210
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700211 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700212 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700213 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700214 }
215 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700216
Lee Thomasonce0510b2013-11-26 21:29:37 -0800217 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100218 _size = 0;
219 }
220
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700221 void Push( T t ) {
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300222 TIXMLASSERT( _size < INT_MAX );
Lee Thomason624d43f2012-10-12 10:58:48 -0700223 EnsureCapacity( _size+1 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300224 _mem[_size] = t;
225 ++_size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700226 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800227
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700228 T* PushArr( int count ) {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300229 TIXMLASSERT( count >= 0 );
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300230 TIXMLASSERT( _size <= INT_MAX - count );
Lee Thomason624d43f2012-10-12 10:58:48 -0700231 EnsureCapacity( _size+count );
232 T* ret = &_mem[_size];
233 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700234 return ret;
235 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700236
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700237 T Pop() {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300238 TIXMLASSERT( _size > 0 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300239 --_size;
240 return _mem[_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700241 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700242
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700243 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700244 TIXMLASSERT( _size >= count );
245 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700246 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800247
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700248 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700249 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700250 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700251
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700252 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700253 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700254 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700255 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700256
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700257 const T& operator[](int i) const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700258 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700259 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700260 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700261
Lee Thomasonf07b9522014-10-30 13:25:12 -0700262 const T& PeekTop() const {
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500263 TIXMLASSERT( _size > 0 );
264 return _mem[ _size - 1];
265 }
266
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700267 int Size() const {
Dmitry-Me30bdc972015-01-14 08:32:23 +0300268 TIXMLASSERT( _size >= 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700269 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700270 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700271
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700272 int Capacity() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300273 TIXMLASSERT( _allocated >= INITIAL_SIZE );
Lee Thomason624d43f2012-10-12 10:58:48 -0700274 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700275 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700276
Lee Thomason816d3fa2017-06-05 14:35:55 -0700277 void SwapRemove(int i) {
Dmitry-Mec2f677b2017-06-15 12:44:27 +0300278 TIXMLASSERT(i >= 0 && i < _size);
279 TIXMLASSERT(_size > 0);
Lee Thomason816d3fa2017-06-05 14:35:55 -0700280 _mem[i] = _mem[_size - 1];
281 --_size;
282 }
283
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700284 const T* Mem() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300285 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700286 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700287 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700288
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700289 T* Mem() {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300290 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700291 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700292 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800293
Lee Thomason2c85a712012-01-31 08:24:24 -0800294private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300295 DynArray( const DynArray& ); // not supported
296 void operator=( const DynArray& ); // not supported
297
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700298 void EnsureCapacity( int cap ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300299 TIXMLASSERT( cap > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700300 if ( cap > _allocated ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300301 TIXMLASSERT( cap <= INT_MAX / 2 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700302 int newAllocated = cap * 2;
303 T* newMem = new T[newAllocated];
Dmitry-Me243ddf52017-05-18 17:27:14 +0300304 TIXMLASSERT( newAllocated >= _size );
Lee Thomason624d43f2012-10-12 10:58:48 -0700305 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
306 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700307 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700308 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700309 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700310 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700311 }
312 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800313
Lee Thomason624d43f2012-10-12 10:58:48 -0700314 T* _mem;
Dmitry-Me04009222015-04-06 18:07:18 +0300315 T _pool[INITIAL_SIZE];
Lee Thomason624d43f2012-10-12 10:58:48 -0700316 int _allocated; // objects allocated
317 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800318};
319
Lee Thomason50adb4c2012-02-13 15:07:09 -0800320
U-Stream\Leeae25a442012-02-17 17:48:16 -0800321/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200322 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800323 and deallocation of objects.
324*/
PKEuS95060352013-07-26 10:42:44 +0200325class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800326{
327public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700328 MemPool() {}
329 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800330
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700331 virtual int ItemSize() const = 0;
332 virtual void* Alloc() = 0;
333 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800334 virtual void SetTracked() = 0;
Lee Thomasonf07b9522014-10-30 13:25:12 -0700335 virtual void Clear() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800336};
337
Lee Thomason50adb4c2012-02-13 15:07:09 -0800338
U-Stream\Leeae25a442012-02-17 17:48:16 -0800339/*
340 Template child class to create pools of the correct type.
341*/
Dmitry-Me88145b82016-08-09 17:59:31 +0300342template< int ITEM_SIZE >
PKEuS95060352013-07-26 10:42:44 +0200343class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800344{
345public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200346 MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700347 ~MemPoolT() {
Lee Thomasonf07b9522014-10-30 13:25:12 -0700348 Clear();
349 }
350
351 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700352 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700353 while( !_blockPtrs.Empty()) {
Dmitry-Meeffab6f2017-07-26 17:57:50 +0300354 Block* lastBlock = _blockPtrs.Pop();
355 delete lastBlock;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700356 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700357 _root = 0;
358 _currentAllocs = 0;
359 _nAllocs = 0;
360 _maxAllocs = 0;
361 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700362 }
Lee Thomasond1983222012-02-06 08:41:24 -0800363
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700364 virtual int ItemSize() const {
Dmitry-Me88145b82016-08-09 17:59:31 +0300365 return ITEM_SIZE;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700366 }
367 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700368 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700369 }
Lee Thomasond1983222012-02-06 08:41:24 -0800370
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700371 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700372 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700373 // Need a new block.
374 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700375 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800376
Dmitry-Me88145b82016-08-09 17:59:31 +0300377 Item* blockItems = block->items;
378 for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
379 blockItems[i].next = &(blockItems[i + 1]);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700380 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300381 blockItems[ITEMS_PER_BLOCK - 1].next = 0;
382 _root = blockItems;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700383 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300384 Item* const result = _root;
385 TIXMLASSERT( result != 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700386 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800387
Lee Thomason624d43f2012-10-12 10:58:48 -0700388 ++_currentAllocs;
389 if ( _currentAllocs > _maxAllocs ) {
390 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700391 }
Dmitry-Me3161a332016-09-02 16:58:06 +0300392 ++_nAllocs;
393 ++_nUntracked;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700394 return result;
395 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700396
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700397 virtual void Free( void* mem ) {
398 if ( !mem ) {
399 return;
400 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700401 --_currentAllocs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300402 Item* item = static_cast<Item*>( mem );
Peter Matula50689912018-01-09 12:52:26 +0100403#ifdef TINYXML2_DEBUG
Dmitry-Me88145b82016-08-09 17:59:31 +0300404 memset( item, 0xfe, sizeof( *item ) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700405#endif
Dmitry-Me88145b82016-08-09 17:59:31 +0300406 item->next = _root;
407 _root = item;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700408 }
409 void Trace( const char* name ) {
410 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Dmitry-Me88145b82016-08-09 17:59:31 +0300411 name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
412 ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700413 }
Lee Thomasond1983222012-02-06 08:41:24 -0800414
Lee Thomason5b0a6772012-11-19 13:54:42 -0800415 void SetTracked() {
Dmitry-Me3161a332016-09-02 16:58:06 +0300416 --_nUntracked;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800417 }
418
419 int Untracked() const {
420 return _nUntracked;
421 }
422
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800423 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
424 // The test file is large, 170k.
425 // Release: VS2010 gcc(no opt)
426 // 1k: 4000
427 // 2k: 4000
428 // 4k: 3900 21000
429 // 16k: 5200
430 // 32k: 4300
431 // 64k: 4000 21000
Dmitry-Me88145b82016-08-09 17:59:31 +0300432 // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
433 // in private part if ITEMS_PER_BLOCK is private
434 enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
Jerome Martinez7921df12012-10-24 11:45:44 +0200435
Lee Thomasond1983222012-02-06 08:41:24 -0800436private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300437 MemPoolT( const MemPoolT& ); // not supported
438 void operator=( const MemPoolT& ); // not supported
439
Dmitry-Me88145b82016-08-09 17:59:31 +0300440 union Item {
441 Item* next;
442 char itemData[ITEM_SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700443 };
444 struct Block {
Dmitry-Me88145b82016-08-09 17:59:31 +0300445 Item items[ITEMS_PER_BLOCK];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700446 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700447 DynArray< Block*, 10 > _blockPtrs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300448 Item* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800449
Lee Thomason624d43f2012-10-12 10:58:48 -0700450 int _currentAllocs;
451 int _nAllocs;
452 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800453 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800454};
455
Lee Thomason2c85a712012-01-31 08:24:24 -0800456
Lee Thomason56bdd022012-02-09 18:16:58 -0800457
458/**
459 Implements the interface to the "Visitor pattern" (see the Accept() method.)
460 If you call the Accept() method, it requires being passed a XMLVisitor
461 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200462 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800463 are simply called with Visit().
464
465 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700466 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800467
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700468 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800469 visiting). You need to only override methods that are interesting to you.
470
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600471 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800472
473 You should never change the document from a callback.
474
475 @sa XMLNode::Accept()
476*/
PKEuS16ed47d2013-07-06 12:02:43 +0200477class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800478{
479public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700480 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800481
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700482 /// Visit a document.
483 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
484 return true;
485 }
486 /// Visit a document.
487 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
488 return true;
489 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800490
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700491 /// Visit an element.
492 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
493 return true;
494 }
495 /// Visit an element.
496 virtual bool VisitExit( const XMLElement& /*element*/ ) {
497 return true;
498 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800499
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700500 /// Visit a declaration.
501 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
502 return true;
503 }
504 /// Visit a text node.
505 virtual bool Visit( const XMLText& /*text*/ ) {
506 return true;
507 }
508 /// Visit a comment node.
509 virtual bool Visit( const XMLComment& /*comment*/ ) {
510 return true;
511 }
512 /// Visit an unknown node.
513 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
514 return true;
515 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800516};
517
Dmitry-Me66d2a842014-11-08 15:24:52 +0300518// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900519enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900520 XML_SUCCESS = 0,
numatrumpetcd8550c2014-09-08 16:59:39 +0900521 XML_NO_ATTRIBUTE,
522 XML_WRONG_ATTRIBUTE_TYPE,
523 XML_ERROR_FILE_NOT_FOUND,
524 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
525 XML_ERROR_FILE_READ_ERROR,
Lee Thomason8bba8b42017-06-20 09:18:41 -0700526 UNUSED_XML_ERROR_ELEMENT_MISMATCH, // remove at next major version
numatrumpetcd8550c2014-09-08 16:59:39 +0900527 XML_ERROR_PARSING_ELEMENT,
528 XML_ERROR_PARSING_ATTRIBUTE,
Lee Thomason8bba8b42017-06-20 09:18:41 -0700529 UNUSED_XML_ERROR_IDENTIFYING_TAG, // remove at next major version
numatrumpetcd8550c2014-09-08 16:59:39 +0900530 XML_ERROR_PARSING_TEXT,
531 XML_ERROR_PARSING_CDATA,
532 XML_ERROR_PARSING_COMMENT,
533 XML_ERROR_PARSING_DECLARATION,
534 XML_ERROR_PARSING_UNKNOWN,
535 XML_ERROR_EMPTY_DOCUMENT,
536 XML_ERROR_MISMATCHED_ELEMENT,
537 XML_ERROR_PARSING,
538 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700539 XML_NO_TEXT_NODE,
Lee Thomasond946dda2018-04-05 09:11:08 -0700540 XML_ELEMENT_DEPTH_EXCEEDED,
Lee Thomason331596e2014-09-11 14:56:43 -0700541
542 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900543};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900544
numatrumpetcd8550c2014-09-08 16:59:39 +0900545
U-Stream\Leeae25a442012-02-17 17:48:16 -0800546/*
547 Utility functionality.
548*/
Lee Thomasonce667c92016-12-26 16:45:30 -0800549class TINYXML2_LIB XMLUtil
Lee Thomason56bdd022012-02-09 18:16:58 -0800550{
Lee Thomasond1983222012-02-06 08:41:24 -0800551public:
kezenator4f756162016-11-29 19:46:27 +1000552 static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300553 TIXMLASSERT( p );
Lee Thomasone90e9012016-12-24 07:34:39 -0800554
Dmitry-Mefa20b222014-10-31 12:53:04 +0300555 while( IsWhiteSpace(*p) ) {
Lee Thomasone90e9012016-12-24 07:34:39 -0800556 if (curLineNumPtr && *p == '\n') {
kezenator4f756162016-11-29 19:46:27 +1000557 ++(*curLineNumPtr);
kezenatorec694152016-11-26 17:21:43 +1000558 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700559 ++p;
560 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300561 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700562 return p;
563 }
kezenator4f756162016-11-29 19:46:27 +1000564 static char* SkipWhiteSpace( char* p, int* curLineNumPtr ) {
565 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700566 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300567
568 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
569 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700570 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100571 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700572 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200573
574 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300575 if ( ch >= 128 ) {
576 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
577 return true;
578 }
579 if ( isalpha( ch ) ) {
580 return true;
581 }
582 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200583 }
584
585 inline static bool IsNameChar( unsigned char ch ) {
586 return IsNameStartChar( ch )
587 || isdigit( ch )
588 || ch == '.'
589 || ch == '-';
590 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800591
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700592 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700593 if ( p == q ) {
594 return true;
595 }
Dmitry-Me21f99692016-10-03 13:01:57 +0300596 TIXMLASSERT( p );
597 TIXMLASSERT( q );
598 TIXMLASSERT( nChar >= 0 );
Lee Thomason598a88d2015-10-09 14:42:12 -0700599 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700600 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200601
Dmitry-Me7865aad2015-06-19 16:23:35 +0300602 inline static bool IsUTF8Continuation( char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400603 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700604 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800605
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700606 static const char* ReadBOM( const char* p, bool* hasBOM );
607 // p is the starting location,
608 // the UTF-8 value of the entity will be placed in value, and length filled in.
609 static const char* GetCharacterRef( const char* p, char* value, int* length );
610 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700611
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700612 // converts primitive types to strings
613 static void ToStr( int v, char* buffer, int bufferSize );
614 static void ToStr( unsigned v, char* buffer, int bufferSize );
615 static void ToStr( bool v, char* buffer, int bufferSize );
616 static void ToStr( float v, char* buffer, int bufferSize );
617 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason51c12712016-06-04 20:18:49 -0700618 static void ToStr(int64_t v, char* buffer, int bufferSize);
Lee Thomason21be8822012-07-15 17:27:22 -0700619
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700620 // converts strings to primitive types
621 static bool ToInt( const char* str, int* value );
622 static bool ToUnsigned( const char* str, unsigned* value );
623 static bool ToBool( const char* str, bool* value );
624 static bool ToFloat( const char* str, float* value );
625 static bool ToDouble( const char* str, double* value );
Lee Thomason51c12712016-06-04 20:18:49 -0700626 static bool ToInt64(const char* str, int64_t* value);
Lee Thomasonce667c92016-12-26 16:45:30 -0800627
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800628 // Changes what is serialized for a boolean value.
629 // Default to "true" and "false". Shouldn't be changed
630 // unless you have a special testing or compatibility need.
Lee Thomasonce667c92016-12-26 16:45:30 -0800631 // Be careful: static, global, & not thread safe.
632 // Be sure to set static const memory as parameters.
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800633 static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
Lee Thomasonce667c92016-12-26 16:45:30 -0800634
635private:
Lee Thomasonf458d262016-12-26 22:47:25 -0800636 static const char* writeBoolTrue;
637 static const char* writeBoolFalse;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800638};
639
Lee Thomason5cae8972012-01-24 18:03:07 -0800640
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800641/** XMLNode is a base class for every object that is in the
642 XML Document Object Model (DOM), except XMLAttributes.
643 Nodes have siblings, a parent, and children which can
644 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700645 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800646 be cast to its more defined type.
647
Thomas Roß08bdf502012-05-12 14:21:23 +0200648 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800649 When the XMLDocument gets deleted, all its Nodes
650 will also be deleted.
651
652 @verbatim
653 A Document can contain: Element (container or leaf)
654 Comment (leaf)
655 Unknown (leaf)
656 Declaration( leaf )
657
658 An Element can contain: Element (container or leaf)
659 Text (leaf)
660 Attributes (not on tree)
661 Comment (leaf)
662 Unknown (leaf)
663
664 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800665*/
PKEuS16ed47d2013-07-06 12:02:43 +0200666class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800667{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700668 friend class XMLDocument;
669 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800670public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800671
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700672 /// Get the XMLDocument that owns this XMLNode.
673 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300674 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700675 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700676 }
677 /// Get the XMLDocument that owns this XMLNode.
678 XMLDocument* GetDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300679 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700680 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700681 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800682
Lee Thomason2fa81722012-11-09 12:37:46 -0800683 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700684 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100685 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700686 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800687 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700688 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100689 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700690 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800691 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700692 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100693 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700694 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800695 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700696 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100697 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700698 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800699 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700700 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100701 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800703 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700704 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100705 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700706 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800707
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700708 virtual const XMLElement* ToElement() const {
709 return 0;
710 }
711 virtual const XMLText* ToText() const {
712 return 0;
713 }
714 virtual const XMLComment* ToComment() const {
715 return 0;
716 }
717 virtual const XMLDocument* ToDocument() const {
718 return 0;
719 }
720 virtual const XMLDeclaration* ToDeclaration() const {
721 return 0;
722 }
723 virtual const XMLUnknown* ToUnknown() const {
724 return 0;
725 }
Lee Thomason751da522012-02-10 08:50:51 -0800726
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700727 /** The meaning of 'value' changes for the specific type.
728 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530729 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700730 Element: name of the element
731 Comment: the comment text
732 Unknown: the tag contents
733 Text: the text string
734 @endverbatim
735 */
Michael Daumling21626882013-10-22 17:03:37 +0200736 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100737
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700738 /** Set the Value of an XML node.
739 @sa Value()
740 */
741 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800742
kezenatorec694152016-11-26 17:21:43 +1000743 /// Gets the line number the node is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +1000744 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +1000745
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700746 /// Get the parent of this node on the DOM.
747 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700748 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700749 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100750
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700751 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700752 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700753 }
Lee Thomason751da522012-02-10 08:50:51 -0800754
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700755 /// Returns true if this node has no children.
756 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700757 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700758 }
Lee Thomason751da522012-02-10 08:50:51 -0800759
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700760 /// Get the first child node, or null if none exists.
761 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700762 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700763 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100764
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700765 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700766 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700767 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100768
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700769 /** Get the first child element, or optionally the first child
770 element with the specified name.
771 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300772 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700773
Dmitry-Me886ad972015-07-22 11:00:51 +0300774 XMLElement* FirstChildElement( const char* name = 0 ) {
775 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700776 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800777
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700778 /// Get the last child node, or null if none exists.
779 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700780 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700781 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700782
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700783 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300784 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700785 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800786
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700787 /** Get the last child element or optionally the last child
788 element with the specified name.
789 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300790 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700791
Dmitry-Me886ad972015-07-22 11:00:51 +0300792 XMLElement* LastChildElement( const char* name = 0 ) {
793 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700794 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700795
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700796 /// Get the previous (left) sibling node of this node.
797 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700798 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700799 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700800
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700801 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700802 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700803 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800804
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700805 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300806 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700807
Dmitry-Me886ad972015-07-22 11:00:51 +0300808 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
809 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700810 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700811
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700812 /// Get the next (right) sibling node of this node.
813 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700814 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700815 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700816
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700817 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700818 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700819 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700820
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700821 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300822 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700823
Dmitry-Me886ad972015-07-22 11:00:51 +0300824 XMLElement* NextSiblingElement( const char* name = 0 ) {
825 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700826 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800827
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700828 /**
829 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200830 If the child node is already part of the document,
831 it is moved from its old location to the new location.
832 Returns the addThis argument or 0 if the node does not
833 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700834 */
835 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800836
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700837 XMLNode* LinkEndChild( XMLNode* addThis ) {
838 return InsertEndChild( addThis );
839 }
840 /**
841 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200842 If the child node is already part of the document,
843 it is moved from its old location to the new location.
844 Returns the addThis argument or 0 if the node does not
845 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700846 */
847 XMLNode* InsertFirstChild( XMLNode* addThis );
848 /**
849 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200850 If the child node is already part of the document,
851 it is moved from its old location to the new location.
852 Returns the addThis argument or 0 if the afterThis node
853 is not a child of this node, or if the node does not
854 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700855 */
856 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700857
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700858 /**
859 Delete all the children of this node.
860 */
861 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800862
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700863 /**
864 Delete a child of this node.
865 */
866 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800867
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700868 /**
869 Make a copy of this node, but not its children.
870 You may pass in a Document pointer that will be
871 the owner of the new Node. If the 'document' is
872 null, then the node returned will be allocated
873 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800874
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700875 Note: if called on a XMLDocument, this will return null.
876 */
877 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800878
Lee Thomason7085f002017-06-01 18:09:43 -0700879 /**
Lee Thomason1346a172017-06-14 15:14:19 -0700880 Make a copy of this node and all its children.
Lee Thomason7085f002017-06-01 18:09:43 -0700881
Dmitry-Me3f63f212017-06-19 18:25:19 +0300882 If the 'target' is null, then the nodes will
883 be allocated in the current document. If 'target'
Lee Thomason1346a172017-06-14 15:14:19 -0700884 is specified, the memory will be allocated is the
885 specified XMLDocument.
Lee Thomason7085f002017-06-01 18:09:43 -0700886
887 NOTE: This is probably not the correct tool to
888 copy a document, since XMLDocuments can have multiple
Lee Thomason1346a172017-06-14 15:14:19 -0700889 top level XMLNodes. You probably want to use
890 XMLDocument::DeepCopy()
Lee Thomason7085f002017-06-01 18:09:43 -0700891 */
Dmitry-Me3f63f212017-06-19 18:25:19 +0300892 XMLNode* DeepClone( XMLDocument* target ) const;
Lee Thomason7085f002017-06-01 18:09:43 -0700893
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700894 /**
895 Test if 2 nodes are the same, but don't test children.
896 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800897
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700898 Note: if called on a XMLDocument, this will return false.
899 */
900 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800901
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600902 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700903 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600904 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800905
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600906 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
907 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700908 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800909
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700910 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800911
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700912 - http://www.saxproject.org/
913 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800914
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700915 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800916
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700917 An example of using Accept():
918 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600919 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700920 tinyxmlDoc.Accept( &printer );
921 const char* xmlcstr = printer.CStr();
922 @endverbatim
923 */
924 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800925
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700926 /**
927 Set user data into the XMLNode. TinyXML-2 in
928 no way processes or interprets user data.
929 It is initially 0.
930 */
931 void SetUserData(void* userData) { _userData = userData; }
932
933 /**
934 Get user data set into the XMLNode. TinyXML-2 in
935 no way processes or interprets user data.
936 It is initially 0.
937 */
938 void* GetUserData() const { return _userData; }
939
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800940protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700941 XMLNode( XMLDocument* );
942 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700943
Dmitry-Me10b8ecc2017-04-12 17:57:44 +0300944 virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300945
Lee Thomason624d43f2012-10-12 10:58:48 -0700946 XMLDocument* _document;
947 XMLNode* _parent;
948 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +1000949 int _parseLineNum;
Lee Thomason3f57d272012-01-11 15:30:03 -0800950
Lee Thomason624d43f2012-10-12 10:58:48 -0700951 XMLNode* _firstChild;
952 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800953
Lee Thomason624d43f2012-10-12 10:58:48 -0700954 XMLNode* _prev;
955 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800956
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700957 void* _userData;
958
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800959private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700960 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700961 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400962 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800963 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Meecb9b072016-10-12 16:44:59 +0300964 const XMLElement* ToElementWithName( const char* name ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300965
966 XMLNode( const XMLNode& ); // not supported
967 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800968};
969
970
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800971/** XML text.
972
973 Note that a text node can have child element nodes, for example:
974 @verbatim
975 <root>This is <b>bold</b></root>
976 @endverbatim
977
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700978 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800979 and CDATA. It will default to the mode it was parsed from the XML file and
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700980 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600981 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800982*/
PKEuS16ed47d2013-07-06 12:02:43 +0200983class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800984{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700985 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800986public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700987 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800988
Lee Thomason624d43f2012-10-12 10:58:48 -0700989 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700990 return this;
991 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700992 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700993 return this;
994 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800995
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700996 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700997 void SetCData( bool isCData ) {
998 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700999 }
1000 /// Returns true if this is a CDATA text element.
1001 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001002 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001003 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001004
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001005 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1006 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001007
Lee Thomason5492a1c2012-01-23 15:32:10 -08001008protected:
Lee Thomason624d43f2012-10-12 10:58:48 -07001009 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001010 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -08001011
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001012 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001013
Lee Thomason5492a1c2012-01-23 15:32:10 -08001014private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001015 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +03001016
1017 XMLText( const XMLText& ); // not supported
1018 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -08001019};
1020
1021
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001022/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +02001023class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001024{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001025 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -08001026public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001027 virtual XMLComment* ToComment() {
1028 return this;
1029 }
1030 virtual const XMLComment* ToComment() const {
1031 return this;
1032 }
Lee Thomasonce0763e2012-01-11 15:43:54 -08001033
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001034 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001035
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001036 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1037 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -08001038
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001039protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001040 XMLComment( XMLDocument* doc );
1041 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001042
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001043 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001044
Lee Thomason3f57d272012-01-11 15:30:03 -08001045private:
Dmitry-Mef547a992015-01-09 15:17:09 +03001046 XMLComment( const XMLComment& ); // not supported
1047 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001048};
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001049
1050
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001051/** In correct XML the declaration is the first entry in the file.
1052 @verbatim
1053 <?xml version="1.0" standalone="yes"?>
1054 @endverbatim
1055
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001056 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001057 however.
1058
1059 The text of the declaration isn't interpreted. It is parsed
1060 and written as a string.
1061*/
PKEuS16ed47d2013-07-06 12:02:43 +02001062class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001063{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001064 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001065public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001066 virtual XMLDeclaration* ToDeclaration() {
1067 return this;
1068 }
1069 virtual const XMLDeclaration* ToDeclaration() const {
1070 return this;
1071 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001072
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001073 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001074
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001075 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1076 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001077
1078protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001079 XMLDeclaration( XMLDocument* doc );
1080 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +03001081
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001082 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001083
Dmitry-Mef547a992015-01-09 15:17:09 +03001084private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001085 XMLDeclaration( const XMLDeclaration& ); // not supported
1086 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001087};
1088
1089
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001090/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001091 unknown. It is a tag of text, but should not be modified.
1092 It will be written back to the XML, unchanged, when the file
1093 is saved.
1094
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001095 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001096*/
PKEuS16ed47d2013-07-06 12:02:43 +02001097class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001098{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001099 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001100public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001101 virtual XMLUnknown* ToUnknown() {
1102 return this;
1103 }
1104 virtual const XMLUnknown* ToUnknown() const {
1105 return this;
1106 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001107
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001108 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001109
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001110 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1111 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001112
1113protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001114 XMLUnknown( XMLDocument* doc );
1115 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001116
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001117 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001118
Dmitry-Mef547a992015-01-09 15:17:09 +03001119private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001120 XMLUnknown( const XMLUnknown& ); // not supported
1121 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001122};
1123
1124
Lee Thomason1ff38e02012-02-14 18:18:16 -08001125
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001126/** An attribute is a name-value pair. Elements have an arbitrary
1127 number of attributes, each with a unique name.
1128
1129 @note The attributes are not XMLNodes. You may only query the
1130 Next() attribute in a list.
1131*/
PKEuS16ed47d2013-07-06 12:02:43 +02001132class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001133{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001134 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001135public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001136 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001137 const char* Name() const;
1138
Lee Thomason2fa81722012-11-09 12:37:46 -08001139 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001140 const char* Value() const;
1141
kezenatorec694152016-11-26 17:21:43 +10001142 /// Gets the line number the attribute is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +10001143 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +10001144
Lee Thomason2fa81722012-11-09 12:37:46 -08001145 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001146 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001147 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001148 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001149
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001150 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001151 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001152 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001153 */
Lee Thomason51c12712016-06-04 20:18:49 -07001154 int IntValue() const {
1155 int i = 0;
1156 QueryIntValue(&i);
1157 return i;
1158 }
1159
1160 int64_t Int64Value() const {
1161 int64_t i = 0;
1162 QueryInt64Value(&i);
1163 return i;
1164 }
1165
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001166 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001167 unsigned UnsignedValue() const {
1168 unsigned i=0;
1169 QueryUnsignedValue( &i );
1170 return i;
1171 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001172 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001173 bool BoolValue() const {
1174 bool b=false;
1175 QueryBoolValue( &b );
1176 return b;
1177 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001178 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001179 double DoubleValue() const {
1180 double d=0;
1181 QueryDoubleValue( &d );
1182 return d;
1183 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001184 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001185 float FloatValue() const {
1186 float f=0;
1187 QueryFloatValue( &f );
1188 return f;
1189 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001190
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001191 /** QueryIntValue interprets the attribute as an integer, and returns the value
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001192 in the provided parameter. The function will return XML_SUCCESS on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001193 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1194 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001195 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001196 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001197 XMLError QueryUnsignedValue( unsigned int* value ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001198 /// See QueryIntValue
1199 XMLError QueryInt64Value(int64_t* value) const;
1200 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001201 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001202 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001203 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001204 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001205 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001206
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001207 /// Set the attribute to a string value.
1208 void SetAttribute( const char* value );
1209 /// Set the attribute to value.
1210 void SetAttribute( int value );
1211 /// Set the attribute to value.
1212 void SetAttribute( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001213 /// Set the attribute to value.
1214 void SetAttribute(int64_t value);
1215 /// Set the attribute to value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001216 void SetAttribute( bool value );
1217 /// Set the attribute to value.
1218 void SetAttribute( double value );
1219 /// Set the attribute to value.
1220 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001221
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001222private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001223 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001224
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001225 XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001226 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001227
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001228 XMLAttribute( const XMLAttribute& ); // not supported
1229 void operator=( const XMLAttribute& ); // not supported
1230 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001231
kezenator4f756162016-11-29 19:46:27 +10001232 char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001233
Lee Thomason624d43f2012-10-12 10:58:48 -07001234 mutable StrPair _name;
1235 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +10001236 int _parseLineNum;
Lee Thomason624d43f2012-10-12 10:58:48 -07001237 XMLAttribute* _next;
1238 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001239};
1240
1241
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001242/** The element is a container class. It has a value, the element name,
1243 and can contain other elements, text, comments, and unknowns.
1244 Elements also contain an arbitrary number of attributes.
1245*/
PKEuS16ed47d2013-07-06 12:02:43 +02001246class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001247{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001248 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001249public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001250 /// Get the name of an element (which is the Value() of the node.)
1251 const char* Name() const {
1252 return Value();
1253 }
1254 /// Set the name of the element.
1255 void SetName( const char* str, bool staticMem=false ) {
1256 SetValue( str, staticMem );
1257 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001258
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001259 virtual XMLElement* ToElement() {
1260 return this;
1261 }
1262 virtual const XMLElement* ToElement() const {
1263 return this;
1264 }
1265 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001266
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001267 /** Given an attribute name, Attribute() returns the value
1268 for the attribute of that name, or null if none
1269 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001270
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001271 @verbatim
1272 const char* value = ele->Attribute( "foo" );
1273 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001274
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001275 The 'value' parameter is normally null. However, if specified,
1276 the attribute will only be returned if the 'name' and 'value'
1277 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001278
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001279 @verbatim
1280 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1281 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001282
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001283 rather than:
1284 @verbatim
1285 if ( ele->Attribute( "foo" ) ) {
1286 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1287 }
1288 @endverbatim
1289 */
1290 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001291
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001292 /** Given an attribute name, IntAttribute() returns the value
Lee Thomason13cbc9a2016-10-27 14:55:07 -07001293 of the attribute interpreted as an integer. The default
1294 value will be returned if the attribute isn't present,
1295 or if there is an error. (For a method with error
1296 checking, see QueryIntAttribute()).
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001297 */
Josh Wittnercf3dd092016-10-11 18:57:17 -07001298 int IntAttribute(const char* name, int defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001299 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001300 unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001301 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001302 int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001303 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001304 bool BoolAttribute(const char* name, bool defaultValue = false) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001305 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001306 double DoubleAttribute(const char* name, double defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001307 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001308 float FloatAttribute(const char* name, float defaultValue = 0) const;
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001309
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001310 /** Given an attribute name, QueryIntAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001311 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001312 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1313 doesn't exist. If successful, the result of the conversion
1314 will be written to 'value'. If not successful, nothing will
1315 be written to 'value'. This allows you to provide default
1316 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001317
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001318 @verbatim
1319 int value = 10;
1320 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1321 @endverbatim
1322 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001323 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001324 const XMLAttribute* a = FindAttribute( name );
1325 if ( !a ) {
1326 return XML_NO_ATTRIBUTE;
1327 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001328 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001329 }
Lee Thomason51c12712016-06-04 20:18:49 -07001330
1331 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001332 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001333 const XMLAttribute* a = FindAttribute( name );
1334 if ( !a ) {
1335 return XML_NO_ATTRIBUTE;
1336 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001337 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001338 }
Lee Thomason51c12712016-06-04 20:18:49 -07001339
1340 /// See QueryIntAttribute()
1341 XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1342 const XMLAttribute* a = FindAttribute(name);
1343 if (!a) {
1344 return XML_NO_ATTRIBUTE;
1345 }
1346 return a->QueryInt64Value(value);
1347 }
1348
1349 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001350 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001351 const XMLAttribute* a = FindAttribute( name );
1352 if ( !a ) {
1353 return XML_NO_ATTRIBUTE;
1354 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001355 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001356 }
1357 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001358 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001359 const XMLAttribute* a = FindAttribute( name );
1360 if ( !a ) {
1361 return XML_NO_ATTRIBUTE;
1362 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001363 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001364 }
1365 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001366 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001367 const XMLAttribute* a = FindAttribute( name );
1368 if ( !a ) {
1369 return XML_NO_ATTRIBUTE;
1370 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001371 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001372 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001373
Lee Thomason5b00e062017-12-28 14:07:47 -08001374 /// See QueryIntAttribute()
1375 XMLError QueryStringAttribute(const char* name, const char** value) const {
1376 const XMLAttribute* a = FindAttribute(name);
1377 if (!a) {
1378 return XML_NO_ATTRIBUTE;
1379 }
1380 *value = a->Value();
1381 return XML_SUCCESS;
1382 }
1383
1384
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001385
1386 /** Given an attribute name, QueryAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001387 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001388 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1389 doesn't exist. It is overloaded for the primitive types,
1390 and is a generally more convenient replacement of
1391 QueryIntAttribute() and related functions.
1392
1393 If successful, the result of the conversion
1394 will be written to 'value'. If not successful, nothing will
1395 be written to 'value'. This allows you to provide default
1396 value:
1397
1398 @verbatim
1399 int value = 10;
1400 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1401 @endverbatim
1402 */
1403 int QueryAttribute( const char* name, int* value ) const {
1404 return QueryIntAttribute( name, value );
1405 }
1406
1407 int QueryAttribute( const char* name, unsigned int* value ) const {
1408 return QueryUnsignedAttribute( name, value );
1409 }
1410
Lee Thomason51c12712016-06-04 20:18:49 -07001411 int QueryAttribute(const char* name, int64_t* value) const {
1412 return QueryInt64Attribute(name, value);
1413 }
1414
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001415 int QueryAttribute( const char* name, bool* value ) const {
1416 return QueryBoolAttribute( name, value );
1417 }
1418
1419 int QueryAttribute( const char* name, double* value ) const {
1420 return QueryDoubleAttribute( name, value );
1421 }
1422
1423 int QueryAttribute( const char* name, float* value ) const {
1424 return QueryFloatAttribute( name, value );
1425 }
1426
1427 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001428 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001429 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001430 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001431 }
1432 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001433 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001434 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001435 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001436 }
1437 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001438 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001439 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001440 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001441 }
Lee Thomason51c12712016-06-04 20:18:49 -07001442
1443 /// Sets the named attribute to value.
1444 void SetAttribute(const char* name, int64_t value) {
1445 XMLAttribute* a = FindOrCreateAttribute(name);
1446 a->SetAttribute(value);
1447 }
1448
1449 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001450 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001451 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001452 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001453 }
1454 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001455 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001456 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001457 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001458 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001459 /// Sets the named attribute to value.
1460 void SetAttribute( const char* name, float value ) {
1461 XMLAttribute* a = FindOrCreateAttribute( name );
1462 a->SetAttribute( value );
1463 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001464
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001465 /**
1466 Delete an attribute.
1467 */
1468 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001469
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001470 /// Return the first attribute in the list.
1471 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001472 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001473 }
1474 /// Query a specific attribute in the list.
1475 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001476
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001477 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001478 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001479 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001480
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001481 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001482 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001483
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001484 This is a convenient method for getting the text of simple contained text:
1485 @verbatim
1486 <foo>This is text</foo>
1487 const char* str = fooElement->GetText();
1488 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001489
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001490 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001491
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001492 Note that this function can be misleading. If the element foo was created from
1493 this XML:
1494 @verbatim
1495 <foo><b>This is text</b></foo>
1496 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001497
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001498 then the value of str would be null. The first child node isn't a text node, it is
1499 another element. From this XML:
1500 @verbatim
1501 <foo>This is <b>text</b></foo>
1502 @endverbatim
1503 GetText() will return "This is ".
1504 */
1505 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001506
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001507 /** Convenience function for easy access to the text inside an element. Although easy
1508 and concise, SetText() is limited compared to creating an XMLText child
1509 and mutating it directly.
1510
1511 If the first child of 'this' is a XMLText, SetText() sets its value to
1512 the given string, otherwise it will create a first child that is an XMLText.
1513
1514 This is a convenient method for setting the text of simple contained text:
1515 @verbatim
1516 <foo>This is text</foo>
1517 fooElement->SetText( "Hullaballoo!" );
1518 <foo>Hullaballoo!</foo>
1519 @endverbatim
1520
1521 Note that this function can be misleading. If the element foo was created from
1522 this XML:
1523 @verbatim
1524 <foo><b>This is text</b></foo>
1525 @endverbatim
1526
1527 then it will not change "This is text", but rather prefix it with a text element:
1528 @verbatim
1529 <foo>Hullaballoo!<b>This is text</b></foo>
1530 @endverbatim
1531
1532 For this XML:
1533 @verbatim
1534 <foo />
1535 @endverbatim
1536 SetText() will generate
1537 @verbatim
1538 <foo>Hullaballoo!</foo>
1539 @endverbatim
1540 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001541 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001542 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001543 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001544 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001545 void SetText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001546 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1547 void SetText(int64_t value);
1548 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001549 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001550 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001551 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001552 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001553 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001554
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001555 /**
1556 Convenience method to query the value of a child text node. This is probably best
1557 shown by example. Given you have a document is this form:
1558 @verbatim
1559 <point>
1560 <x>1</x>
1561 <y>1.4</y>
1562 </point>
1563 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001564
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001565 The QueryIntText() and similar functions provide a safe and easier way to get to the
1566 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001567
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001568 @verbatim
1569 int x = 0;
1570 float y = 0; // types of x and y are contrived for example
1571 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1572 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1573 xElement->QueryIntText( &x );
1574 yElement->QueryFloatText( &y );
1575 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001576
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001577 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1578 to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001579
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001580 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001581 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001582 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001583 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001584 /// See QueryIntText()
1585 XMLError QueryInt64Text(int64_t* uval) const;
1586 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001587 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001588 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001589 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001590 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001591 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001592
Josh Wittnercf3dd092016-10-11 18:57:17 -07001593 int IntText(int defaultValue = 0) const;
1594
Josh Wittner3a621f52016-09-12 19:17:54 -07001595 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001596 unsigned UnsignedText(unsigned defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001597 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001598 int64_t Int64Text(int64_t defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001599 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001600 bool BoolText(bool defaultValue = false) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001601 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001602 double DoubleText(double defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001603 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001604 float FloatText(float defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001605
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001606 // internal:
Dmitry-Mee5035632017-04-05 18:02:40 +03001607 enum ElementClosingType {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001608 OPEN, // <foo>
1609 CLOSED, // <foo/>
1610 CLOSING // </foo>
1611 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001612 ElementClosingType ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001613 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001614 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001615 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1616 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001617
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001618protected:
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001619 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001620
Lee Thomason50adb4c2012-02-13 15:07:09 -08001621private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001622 XMLElement( XMLDocument* doc );
1623 virtual ~XMLElement();
1624 XMLElement( const XMLElement& ); // not supported
1625 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001626
Dmitry-Me1227d512014-12-05 13:41:45 +03001627 XMLAttribute* FindAttribute( const char* name ) {
1628 return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
1629 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001630 XMLAttribute* FindOrCreateAttribute( const char* name );
1631 //void LinkAttribute( XMLAttribute* attrib );
kezenator4f756162016-11-29 19:46:27 +10001632 char* ParseAttributes( char* p, int* curLineNumPtr );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001633 static void DeleteAttribute( XMLAttribute* attribute );
Dmitry-Mea60caa22016-11-22 18:28:08 +03001634 XMLAttribute* CreateAttribute();
Lee Thomason67d61312012-01-24 16:01:51 -08001635
Lee Thomason5bb2d802014-01-24 10:42:57 -08001636 enum { BUF_SIZE = 200 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001637 ElementClosingType _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001638 // The attribute list is ordered; there is no 'lastAttribute'
1639 // because the list needs to be scanned for dupes before adding
1640 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001641 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001642};
1643
1644
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001645enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001646 PRESERVE_WHITESPACE,
1647 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001648};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001649
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001650
1651/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001652 It can be saved, loaded, and printed to the screen.
1653 All Nodes are connected and allocated to a Document.
1654 If the Document is deleted, all its Nodes are also deleted.
1655*/
PKEuS16ed47d2013-07-06 12:02:43 +02001656class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001657{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001658 friend class XMLElement;
Lee Thomasond946dda2018-04-05 09:11:08 -07001659 // Gives access to SetError and Push/PopDepth, but over-access for everything else.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001660 // Wishing C++ had "internal" scope.
1661 friend class XMLNode;
1662 friend class XMLText;
1663 friend class XMLComment;
1664 friend class XMLDeclaration;
1665 friend class XMLUnknown;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001666public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001667 /// constructor
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001668 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001669 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001670
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001671 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001672 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001673 return this;
1674 }
1675 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001676 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001677 return this;
1678 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001679
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001680 /**
1681 Parse an XML file from a character string.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001682 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001683 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001684
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001685 You may optionally pass in the 'nBytes', which is
1686 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001687 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001688 null terminated string.
1689 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001690 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001691
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001692 /**
1693 Load an XML file from disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001694 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001695 an errorID.
1696 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001697 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001698
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001699 /**
1700 Load an XML file from disk. You are responsible
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001701 for providing and closing the FILE*.
1702
1703 NOTE: The file should be opened as binary ("rb")
1704 not text in order for TinyXML-2 to correctly
1705 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001706
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001707 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001708 an errorID.
1709 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001710 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001711
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001712 /**
1713 Save the XML file to disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001714 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001715 an errorID.
1716 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001717 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001718
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001719 /**
1720 Save the XML file to disk. You are responsible
1721 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001722
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001723 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001724 an errorID.
1725 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001726 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001727
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001728 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001729 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001730 }
1731 Whitespace WhitespaceMode() const {
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001732 return _whitespaceMode;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001733 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001734
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001735 /**
1736 Returns true if this document has a leading Byte Order Mark of UTF8.
1737 */
1738 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001739 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001740 }
1741 /** Sets whether to write the BOM when writing the file.
1742 */
1743 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001744 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001745 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001746
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001747 /** Return the root element of DOM. Equivalent to FirstChildElement().
1748 To get the first node, use FirstChild().
1749 */
1750 XMLElement* RootElement() {
1751 return FirstChildElement();
1752 }
1753 const XMLElement* RootElement() const {
1754 return FirstChildElement();
1755 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001756
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001757 /** Print the Document. If the Printer is not provided, it will
1758 print to stdout. If you provide Printer, this can print to a file:
1759 @verbatim
1760 XMLPrinter printer( fp );
1761 doc.Print( &printer );
1762 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001763
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001764 Or you can use a printer to print to memory:
1765 @verbatim
1766 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001767 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001768 // printer.CStr() has a const char* to the XML
1769 @endverbatim
1770 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001771 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001772 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001773
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001774 /**
1775 Create a new Element associated with
1776 this Document. The memory for the Element
1777 is managed by the Document.
1778 */
1779 XMLElement* NewElement( const char* name );
1780 /**
1781 Create a new Comment associated with
1782 this Document. The memory for the Comment
1783 is managed by the Document.
1784 */
1785 XMLComment* NewComment( const char* comment );
1786 /**
1787 Create a new Text associated with
1788 this Document. The memory for the Text
1789 is managed by the Document.
1790 */
1791 XMLText* NewText( const char* text );
1792 /**
1793 Create a new Declaration associated with
1794 this Document. The memory for the object
1795 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001796
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001797 If the 'text' param is null, the standard
1798 declaration is used.:
1799 @verbatim
1800 <?xml version="1.0" encoding="UTF-8"?>
1801 @endverbatim
1802 */
1803 XMLDeclaration* NewDeclaration( const char* text=0 );
1804 /**
1805 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001806 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001807 is managed by the Document.
1808 */
1809 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001810
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001811 /**
1812 Delete a node associated with this document.
1813 It will be unlinked from the DOM.
1814 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001815 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001816
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001817 void ClearError() {
Lee Thomasonaa188392017-09-19 17:54:31 -07001818 SetError(XML_SUCCESS, 0, 0);
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001819 }
1820
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001821 /// Return true if there was an error parsing the document.
1822 bool Error() const {
Lee Thomason85536252016-06-04 19:10:53 -07001823 return _errorID != XML_SUCCESS;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001824 }
1825 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001826 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001827 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001828 }
Lee Thomason331596e2014-09-11 14:56:43 -07001829 const char* ErrorName() const;
Lee Thomasone90e9012016-12-24 07:34:39 -08001830 static const char* ErrorIDToName(XMLError errorID);
Lee Thomason331596e2014-09-11 14:56:43 -07001831
Lee Thomasonf49b9652017-10-11 10:57:49 -07001832 /** Returns a "long form" error description. A hopefully helpful
1833 diagnostic with location, line number, and/or additional info.
1834 */
1835 const char* ErrorStr() const;
1836
1837 /// A (trivial) utility function that prints the ErrorStr() to stdout.
1838 void PrintError() const;
Lee Thomason8c9e3132017-06-26 16:55:01 -07001839
kezenatorec694152016-11-26 17:21:43 +10001840 /// Return the line where the error occured, or zero if unknown.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001841 int ErrorLineNum() const
kezenatorec694152016-11-26 17:21:43 +10001842 {
1843 return _errorLineNum;
1844 }
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001845
1846 /// Clear the document, resetting it to the initial state.
1847 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001848
Lee Thomason7085f002017-06-01 18:09:43 -07001849 /**
Lee Thomasonb29f5562017-06-01 18:45:32 -07001850 Copies this document to a target document.
Lee Thomason7085f002017-06-01 18:09:43 -07001851 The target will be completely cleared before the copy.
Lee Thomasonb29f5562017-06-01 18:45:32 -07001852 If you want to copy a sub-tree, see XMLNode::DeepClone().
Lee Thomason7085f002017-06-01 18:09:43 -07001853
Lee Thomason1346a172017-06-14 15:14:19 -07001854 NOTE: that the 'target' must be non-null.
Lee Thomason7085f002017-06-01 18:09:43 -07001855 */
Shuichiro Suzuki87cd4e02017-08-24 11:20:26 +09001856 void DeepCopy(XMLDocument* target) const;
Lee Thomason7085f002017-06-01 18:09:43 -07001857
1858 // internal
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001859 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001860
Lee Thomason816d3fa2017-06-05 14:35:55 -07001861 // internal
1862 void MarkInUse(XMLNode*);
1863
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001864 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1865 return 0;
1866 }
1867 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1868 return false;
1869 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001870
Lee Thomason3f57d272012-01-11 15:30:03 -08001871private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001872 XMLDocument( const XMLDocument& ); // not supported
1873 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001874
Lee Thomasone90e9012016-12-24 07:34:39 -08001875 bool _writeBOM;
1876 bool _processEntities;
1877 XMLError _errorID;
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001878 Whitespace _whitespaceMode;
Lee Thomasonaa188392017-09-19 17:54:31 -07001879 mutable StrPair _errorStr;
Lee Thomasone90e9012016-12-24 07:34:39 -08001880 int _errorLineNum;
1881 char* _charBuffer;
1882 int _parseCurLineNum;
Lee Thomasond946dda2018-04-05 09:11:08 -07001883 int _parsingDepth;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001884 // Memory tracking does add some overhead.
1885 // However, the code assumes that you don't
1886 // have a bunch of unlinked nodes around.
1887 // Therefore it takes less memory to track
1888 // in the document vs. a linked list in the XMLNode,
1889 // and the performance is the same.
1890 DynArray<XMLNode*, 10> _unlinked;
Lee Thomasond1983222012-02-06 08:41:24 -08001891
Lee Thomason624d43f2012-10-12 10:58:48 -07001892 MemPoolT< sizeof(XMLElement) > _elementPool;
1893 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1894 MemPoolT< sizeof(XMLText) > _textPool;
1895 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001896
1897 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001898
1899 void Parse();
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001900
Lee Thomasonf49b9652017-10-11 10:57:49 -07001901 void SetError( XMLError error, int lineNum, const char* format, ... );
1902
Lee Thomasond946dda2018-04-05 09:11:08 -07001903 // Something of an obvious security hole, once it was discovered.
1904 // Either an ill-formed XML or an excessively deep one can overflow
1905 // the stack. Track stack depth, and error out if needed.
1906 class DepthTracker {
1907 public:
1908 DepthTracker(XMLDocument * document) {
1909 this->_document = document;
1910 document->PushDepth();
1911 }
1912 ~DepthTracker() {
1913 _document->PopDepth();
1914 }
1915 private:
1916 XMLDocument * _document;
1917 };
1918 bool PushDepth();
1919 bool PopDepth();
1920
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001921 template<class NodeType, int PoolElementSize>
1922 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
Lee Thomason5cae8972012-01-24 18:03:07 -08001923};
1924
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001925template<class NodeType, int PoolElementSize>
1926inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1927{
1928 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1929 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1930 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1931 TIXMLASSERT( returnNode );
1932 returnNode->_memPool = &pool;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001933
1934 _unlinked.Push(returnNode);
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001935 return returnNode;
1936}
Lee Thomason7c913cd2012-01-26 18:32:34 -08001937
Lee Thomason3ffdd392012-03-28 17:27:55 -07001938/**
1939 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001940 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001941 DOM structure. It is a separate utility class.
1942
1943 Take an example:
1944 @verbatim
1945 <Document>
1946 <Element attributeA = "valueA">
1947 <Child attributeB = "value1" />
1948 <Child attributeB = "value2" />
1949 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001950 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001951 @endverbatim
1952
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001953 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001954 easy to write a *lot* of code that looks like:
1955
1956 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001957 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001958 if ( root )
1959 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001960 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001961 if ( element )
1962 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001963 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001964 if ( child )
1965 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001966 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001967 if ( child2 )
1968 {
1969 // Finally do something useful.
1970 @endverbatim
1971
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001972 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001973 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001974 and correct to use:
1975
1976 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001977 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001978 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001979 if ( child2 )
1980 {
1981 // do something useful
1982 @endverbatim
1983
1984 Which is MUCH more concise and useful.
1985
1986 It is also safe to copy handles - internally they are nothing more than node pointers.
1987 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001988 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001989 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001990
1991 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001992*/
PKEuS16ed47d2013-07-06 12:02:43 +02001993class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001994{
1995public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001996 /// 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 +02001997 XMLHandle( XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001998 }
1999 /// Create a handle from a node.
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002000 XMLHandle( XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002001 }
2002 /// Copy constructor
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002003 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002004 }
2005 /// Assignment
2006 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002007 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002008 return *this;
2009 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002010
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002011 /// Get the first child of this handle.
2012 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002013 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002014 }
2015 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002016 XMLHandle FirstChildElement( const char* name = 0 ) {
2017 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002018 }
2019 /// Get the last child of this handle.
2020 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002021 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002022 }
2023 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002024 XMLHandle LastChildElement( const char* name = 0 ) {
2025 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002026 }
2027 /// Get the previous sibling of this handle.
2028 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002029 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002030 }
2031 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002032 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
2033 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002034 }
2035 /// Get the next sibling of this handle.
2036 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002037 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002038 }
2039 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002040 XMLHandle NextSiblingElement( const char* name = 0 ) {
2041 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002042 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002043
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002044 /// Safe cast to XMLNode. This can return null.
2045 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002046 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002047 }
2048 /// Safe cast to XMLElement. This can return null.
2049 XMLElement* ToElement() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002050 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002051 }
2052 /// Safe cast to XMLText. This can return null.
2053 XMLText* ToText() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002054 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002055 }
2056 /// Safe cast to XMLUnknown. This can return null.
2057 XMLUnknown* ToUnknown() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002058 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002059 }
2060 /// Safe cast to XMLDeclaration. This can return null.
2061 XMLDeclaration* ToDeclaration() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002062 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002063 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002064
2065private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002066 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002067};
2068
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08002069
2070/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002071 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
2072 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07002073*/
PKEuS16ed47d2013-07-06 12:02:43 +02002074class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07002075{
2076public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002077 XMLConstHandle( const XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002078 }
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 XMLConstHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002082 }
Lee Thomason8b899812012-04-04 15:58:16 -07002083
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002084 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002085 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002086 return *this;
2087 }
Lee Thomason8b899812012-04-04 15:58:16 -07002088
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002089 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002090 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002091 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002092 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2093 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002094 }
2095 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002096 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002097 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002098 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2099 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002100 }
2101 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002102 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002103 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002104 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2105 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002106 }
2107 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002108 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002109 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002110 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2111 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002112 }
Lee Thomason8b899812012-04-04 15:58:16 -07002113
2114
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002115 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002116 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002117 }
2118 const XMLElement* ToElement() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002119 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002120 }
2121 const XMLText* ToText() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002122 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002123 }
2124 const XMLUnknown* ToUnknown() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002125 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002126 }
2127 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002128 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002129 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08002130
Lee Thomason5cae8972012-01-24 18:03:07 -08002131private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002132 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08002133};
Lee Thomason6f381b72012-03-02 12:59:39 -08002134
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002135
2136/**
2137 Printing functionality. The XMLPrinter gives you more
2138 options than the XMLDocument::Print() method.
2139
2140 It can:
2141 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02002142 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002143 -# Print XML without a XMLDocument.
2144
2145 Print to Memory
2146
2147 @verbatim
2148 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06002149 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02002150 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002151 @endverbatim
2152
2153 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002154
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002155 You provide the file pointer.
2156 @verbatim
2157 XMLPrinter printer( fp );
2158 doc.Print( &printer );
2159 @endverbatim
2160
2161 Print without a XMLDocument
2162
2163 When loading, an XML parser is very useful. However, sometimes
2164 when saving, it just gets in the way. The code is often set up
2165 for streaming, and constructing the DOM is just overhead.
2166
2167 The Printer supports the streaming case. The following code
2168 prints out a trivially simple XML file without ever creating
2169 an XML document.
2170
2171 @verbatim
2172 XMLPrinter printer( fp );
2173 printer.OpenElement( "foo" );
2174 printer.PushAttribute( "foo", "bar" );
2175 printer.CloseElement();
2176 @endverbatim
2177*/
PKEuS16ed47d2013-07-06 12:02:43 +02002178class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002179{
2180public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002181 /** Construct the printer. If the FILE* is specified,
2182 this will print to the FILE. Else it will print
2183 to memory, and the result is available in CStr().
2184 If 'compact' is set to true, then output is created
2185 with only required whitespace and newlines.
2186 */
PKEuS1bfb9542013-08-04 13:51:17 +02002187 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002188 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002189
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002190 /** If streaming, write the BOM and declaration. */
2191 void PushHeader( bool writeBOM, bool writeDeclaration );
2192 /** If streaming, start writing an element.
2193 The element must be closed with CloseElement()
2194 */
Lee Thomason256adb62014-04-06 14:41:46 -07002195 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002196 /// If streaming, add an attribute to an open element.
2197 void PushAttribute( const char* name, const char* value );
2198 void PushAttribute( const char* name, int value );
2199 void PushAttribute( const char* name, unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002200 void PushAttribute(const char* name, int64_t value);
2201 void PushAttribute( const char* name, bool value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002202 void PushAttribute( const char* name, double value );
2203 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002204 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002205
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002206 /// Add a text node.
2207 void PushText( const char* text, bool cdata=false );
2208 /// Add a text node from an integer.
2209 void PushText( int value );
2210 /// Add a text node from an unsigned.
2211 void PushText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002212 /// Add a text node from an unsigned.
2213 void PushText(int64_t value);
2214 /// Add a text node from a bool.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002215 void PushText( bool value );
2216 /// Add a text node from a float.
2217 void PushText( float value );
2218 /// Add a text node from a double.
2219 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002220
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002221 /// Add a comment
2222 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002223
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002224 void PushDeclaration( const char* value );
2225 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002226
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002227 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2228 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2229 return true;
2230 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002231
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002232 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2233 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002234
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002235 virtual bool Visit( const XMLText& text );
2236 virtual bool Visit( const XMLComment& comment );
2237 virtual bool Visit( const XMLDeclaration& declaration );
2238 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002239
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002240 /**
2241 If in print to memory mode, return a pointer to
2242 the XML file in memory.
2243 */
2244 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002245 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002246 }
2247 /**
2248 If in print to memory mode, return the size
2249 of the XML file in memory. (Note the size returned
2250 includes the terminating null.)
2251 */
2252 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002253 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002254 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002255 /**
2256 If in print to memory mode, reset the buffer to the
2257 beginning.
2258 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002259 void ClearBuffer() {
2260 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002261 _buffer.Push(0);
Lee Thomason53858b42017-06-01 19:09:16 -07002262 _firstElement = true;
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002263 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002264
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002265protected:
Alexander Maid740b642014-05-20 22:04:42 +02002266 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002267
Lee Thomasonc18eb232014-02-21 17:31:17 -08002268 /** Prints out the space before an element. You may override to change
2269 the space and tabs used. A PrintSpace() override should call Print().
2270 */
2271 virtual void PrintSpace( int depth );
2272 void Print( const char* format, ... );
Alexander Golubevb2e08e42016-03-28 19:51:59 +03002273 void Write( const char* data, size_t size );
2274 inline void Write( const char* data ) { Write( data, strlen( data ) ); }
2275 void Putc( char ch );
Lee Thomasonc18eb232014-02-21 17:31:17 -08002276
Dmitry-Mea092bc12014-12-23 17:57:05 +03002277 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002278 bool _elementJustOpened;
2279 DynArray< const char*, 10 > _stack;
2280
2281private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002282 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002283
Lee Thomason624d43f2012-10-12 10:58:48 -07002284 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002285 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002286 int _depth;
2287 int _textDepth;
2288 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002289 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002290
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002291 enum {
2292 ENTITY_RANGE = 64,
2293 BUF_SIZE = 200
2294 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002295 bool _entityFlag[ENTITY_RANGE];
2296 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002297
Lee Thomason624d43f2012-10-12 10:58:48 -07002298 DynArray< char, 20 > _buffer;
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002299
2300 // Prohibit cloning, intentionally not implemented
2301 XMLPrinter( const XMLPrinter& );
2302 XMLPrinter& operator=( const XMLPrinter& );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002303};
2304
2305
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002306} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002307
PKEuS95060352013-07-26 10:42:44 +02002308#if defined(_MSC_VER)
2309# pragma warning(pop)
2310#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002311
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002312#endif // TINYXML2_INCLUDED