blob: 505a892f586ac28e6109c90b8b1ddfd779b3db55 [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:
Lee Thomason5b0a6772012-11-19 13:54:42 -080050 g++ -Wall -DDEBUG 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__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070057# ifndef DEBUG
58# define DEBUG
59# 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
U-Lama\Lee4cee6112011-12-31 14:58:18 -080082#if defined(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 Thomason37bc3ac2017-06-26 16:56:16 -0700101static const int TIXML2_MAJOR_VERSION = 5;
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530102static const int TIXML2_MINOR_VERSION = 0;
Lee Thomason74d44ac2016-07-17 22:57:36 -0700103static const int TIXML2_PATCH_VERSION = 1;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800104
Lee Thomasonbd197872017-12-28 13:31:48 -0800105#define TINYXML2_MAJOR_VERSION 5
106#define TINYXML2_MINOR_VERSION 0
107#define TINYXML2_PATCH_VERSION 1
108
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800109namespace tinyxml2
110{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800111class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800112class XMLElement;
113class XMLAttribute;
114class XMLComment;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800115class XMLText;
Lee Thomason50f97b22012-02-11 16:33:40 -0800116class XMLDeclaration;
117class XMLUnknown;
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800118class XMLPrinter;
Lee Thomason5cae8972012-01-24 18:03:07 -0800119
U-Stream\Leeae25a442012-02-17 17:48:16 -0800120/*
121 A class that wraps strings. Normally stores the start and end
122 pointers into the XML file itself, and will apply normalization
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800123 and entity translation if actually read. Can also store (and memory
U-Stream\Leeae25a442012-02-17 17:48:16 -0800124 manage) a traditional char[]
125*/
PKEuS95060352013-07-26 10:42:44 +0200126class StrPair
Lee Thomason39ede242012-01-20 11:27:56 -0800127{
Lee Thomasond34f52c2012-01-20 12:55:24 -0800128public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700129 enum {
130 NEEDS_ENTITY_PROCESSING = 0x01,
131 NEEDS_NEWLINE_NORMALIZATION = 0x02,
Dmitry-Me5420e542015-05-20 10:51:26 +0300132 NEEDS_WHITESPACE_COLLAPSING = 0x04,
Lee Thomason18d68bd2012-01-26 18:17:26 -0800133
Lee Thomason584af572016-09-05 14:14:16 -0700134 TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700135 TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
Lee Thomason584af572016-09-05 14:14:16 -0700136 ATTRIBUTE_NAME = 0,
137 ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
138 ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
139 COMMENT = NEEDS_NEWLINE_NORMALIZATION
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700140 };
Lee Thomason39ede242012-01-20 11:27:56 -0800141
Lee Thomason120b3a62012-10-12 10:06:59 -0700142 StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700143 ~StrPair();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800144
Lee Thomason120b3a62012-10-12 10:06:59 -0700145 void Set( char* start, char* end, int flags ) {
Dmitry-Me6fc38ec2016-09-01 17:59:44 +0300146 TIXMLASSERT( start );
147 TIXMLASSERT( end );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700148 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700149 _start = start;
150 _end = end;
151 _flags = flags | NEEDS_FLUSH;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700152 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700153
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700154 const char* GetStr();
Lee Thomason120b3a62012-10-12 10:06:59 -0700155
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700156 bool Empty() const {
Lee Thomason120b3a62012-10-12 10:06:59 -0700157 return _start == _end;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700158 }
Lee Thomason39ede242012-01-20 11:27:56 -0800159
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700160 void SetInternedStr( const char* str ) {
161 Reset();
Lee Thomason120b3a62012-10-12 10:06:59 -0700162 _start = const_cast<char*>(str);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700163 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700164
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700165 void SetStr( const char* str, int flags=0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800166
kezenator4f756162016-11-29 19:46:27 +1000167 char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700168 char* ParseName( char* in );
Lee Thomason56bdd022012-02-09 18:16:58 -0800169
Lee Thomason29658802014-11-27 22:31:11 -0800170 void TransferTo( StrPair* other );
Lee Thomason584af572016-09-05 14:14:16 -0700171 void Reset();
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300172
Lee Thomason39ede242012-01-20 11:27:56 -0800173private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700174 void CollapseWhitespace();
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800175
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700176 enum {
177 NEEDS_FLUSH = 0x100,
178 NEEDS_DELETE = 0x200
179 };
Lee Thomasone4422302012-01-20 17:59:50 -0800180
Lee Thomason120b3a62012-10-12 10:06:59 -0700181 int _flags;
182 char* _start;
183 char* _end;
Dmitry-Me08b40dd2014-11-10 11:17:21 +0300184
185 StrPair( const StrPair& other ); // not supported
186 void operator=( StrPair& other ); // not supported, use TransferTo()
Lee Thomason39ede242012-01-20 11:27:56 -0800187};
188
U-Lama\Lee560bd472011-12-28 19:42:49 -0800189
U-Stream\Leeae25a442012-02-17 17:48:16 -0800190/*
191 A dynamic array of Plain Old Data. Doesn't support constructors, etc.
192 Has a small initial memory pool, so that low or no usage will not
193 cause a call to new/delete
194*/
Dmitry-Me04009222015-04-06 18:07:18 +0300195template <class T, int INITIAL_SIZE>
PKEuS95060352013-07-26 10:42:44 +0200196class DynArray
Lee Thomason2c85a712012-01-31 08:24:24 -0800197{
198public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200199 DynArray() :
200 _mem( _pool ),
201 _allocated( INITIAL_SIZE ),
202 _size( 0 )
203 {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700204 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700205
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700206 ~DynArray() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700207 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700208 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700209 }
210 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700211
Lee Thomasonce0510b2013-11-26 21:29:37 -0800212 void Clear() {
Reinhard Klambauer4e74b132013-11-22 14:01:58 +0100213 _size = 0;
214 }
215
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700216 void Push( T t ) {
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300217 TIXMLASSERT( _size < INT_MAX );
Lee Thomason624d43f2012-10-12 10:58:48 -0700218 EnsureCapacity( _size+1 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300219 _mem[_size] = t;
220 ++_size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700221 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800222
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700223 T* PushArr( int count ) {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300224 TIXMLASSERT( count >= 0 );
Dmitry-Meed7a7dc2015-01-14 08:36:12 +0300225 TIXMLASSERT( _size <= INT_MAX - count );
Lee Thomason624d43f2012-10-12 10:58:48 -0700226 EnsureCapacity( _size+count );
227 T* ret = &_mem[_size];
228 _size += count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700229 return ret;
230 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700231
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700232 T Pop() {
Dmitry-Me74fb8702015-01-13 10:27:36 +0300233 TIXMLASSERT( _size > 0 );
Dmitry-Mefed51122016-09-06 18:08:55 +0300234 --_size;
235 return _mem[_size];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700236 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700237
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700238 void PopArr( int count ) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700239 TIXMLASSERT( _size >= count );
240 _size -= count;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700241 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800242
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700243 bool Empty() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700244 return _size == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700245 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700246
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700247 T& operator[](int i) {
Lee Thomason624d43f2012-10-12 10:58:48 -0700248 TIXMLASSERT( i>= 0 && i < _size );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700249 return _mem[i];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700250 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700251
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700252 const T& operator[](int i) const {
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 Thomasonf07b9522014-10-30 13:25:12 -0700257 const T& PeekTop() const {
Dennis Jenkins59c75d32013-10-08 13:10:07 -0500258 TIXMLASSERT( _size > 0 );
259 return _mem[ _size - 1];
260 }
261
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700262 int Size() const {
Dmitry-Me30bdc972015-01-14 08:32:23 +0300263 TIXMLASSERT( _size >= 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700264 return _size;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700265 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700266
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700267 int Capacity() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300268 TIXMLASSERT( _allocated >= INITIAL_SIZE );
Lee Thomason624d43f2012-10-12 10:58:48 -0700269 return _allocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700270 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700271
Lee Thomason816d3fa2017-06-05 14:35:55 -0700272 void SwapRemove(int i) {
Dmitry-Mec2f677b2017-06-15 12:44:27 +0300273 TIXMLASSERT(i >= 0 && i < _size);
274 TIXMLASSERT(_size > 0);
Lee Thomason816d3fa2017-06-05 14:35:55 -0700275 _mem[i] = _mem[_size - 1];
276 --_size;
277 }
278
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700279 const T* Mem() const {
Dmitry-Me2f5a1032015-06-18 16:40:09 +0300280 TIXMLASSERT( _mem );
Lee Thomasoned5c8792012-10-12 10:09:48 -0700281 return _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700282 }
Lee Thomason120b3a62012-10-12 10:06:59 -0700283
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700284 T* Mem() {
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 Thomason2c85a712012-01-31 08:24:24 -0800288
Lee Thomason2c85a712012-01-31 08:24:24 -0800289private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300290 DynArray( const DynArray& ); // not supported
291 void operator=( const DynArray& ); // not supported
292
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700293 void EnsureCapacity( int cap ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300294 TIXMLASSERT( cap > 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700295 if ( cap > _allocated ) {
Dmitry-Me9fae8692014-12-22 17:59:59 +0300296 TIXMLASSERT( cap <= INT_MAX / 2 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700297 int newAllocated = cap * 2;
298 T* newMem = new T[newAllocated];
Dmitry-Me243ddf52017-05-18 17:27:14 +0300299 TIXMLASSERT( newAllocated >= _size );
Lee Thomason624d43f2012-10-12 10:58:48 -0700300 memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
301 if ( _mem != _pool ) {
Lee Thomasoned5c8792012-10-12 10:09:48 -0700302 delete [] _mem;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700303 }
Lee Thomasoned5c8792012-10-12 10:09:48 -0700304 _mem = newMem;
Lee Thomason624d43f2012-10-12 10:58:48 -0700305 _allocated = newAllocated;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700306 }
307 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800308
Lee Thomason624d43f2012-10-12 10:58:48 -0700309 T* _mem;
Dmitry-Me04009222015-04-06 18:07:18 +0300310 T _pool[INITIAL_SIZE];
Lee Thomason624d43f2012-10-12 10:58:48 -0700311 int _allocated; // objects allocated
312 int _size; // number objects in use
Lee Thomason2c85a712012-01-31 08:24:24 -0800313};
314
Lee Thomason50adb4c2012-02-13 15:07:09 -0800315
U-Stream\Leeae25a442012-02-17 17:48:16 -0800316/*
Thomas Roß08bdf502012-05-12 14:21:23 +0200317 Parent virtual class of a pool for fast allocation
U-Stream\Leeae25a442012-02-17 17:48:16 -0800318 and deallocation of objects.
319*/
PKEuS95060352013-07-26 10:42:44 +0200320class MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800321{
322public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700323 MemPool() {}
324 virtual ~MemPool() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800325
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700326 virtual int ItemSize() const = 0;
327 virtual void* Alloc() = 0;
328 virtual void Free( void* ) = 0;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800329 virtual void SetTracked() = 0;
Lee Thomasonf07b9522014-10-30 13:25:12 -0700330 virtual void Clear() = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800331};
332
Lee Thomason50adb4c2012-02-13 15:07:09 -0800333
U-Stream\Leeae25a442012-02-17 17:48:16 -0800334/*
335 Template child class to create pools of the correct type.
336*/
Dmitry-Me88145b82016-08-09 17:59:31 +0300337template< int ITEM_SIZE >
PKEuS95060352013-07-26 10:42:44 +0200338class MemPoolT : public MemPool
Lee Thomasond1983222012-02-06 08:41:24 -0800339{
340public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +0200341 MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700342 ~MemPoolT() {
Lee Thomasonf07b9522014-10-30 13:25:12 -0700343 Clear();
344 }
345
346 void Clear() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700347 // Delete the blocks.
Lee Thomasonf07b9522014-10-30 13:25:12 -0700348 while( !_blockPtrs.Empty()) {
Dmitry-Meeffab6f2017-07-26 17:57:50 +0300349 Block* lastBlock = _blockPtrs.Pop();
350 delete lastBlock;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700351 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700352 _root = 0;
353 _currentAllocs = 0;
354 _nAllocs = 0;
355 _maxAllocs = 0;
356 _nUntracked = 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700357 }
Lee Thomasond1983222012-02-06 08:41:24 -0800358
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700359 virtual int ItemSize() const {
Dmitry-Me88145b82016-08-09 17:59:31 +0300360 return ITEM_SIZE;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700361 }
362 int CurrentAllocs() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700363 return _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700364 }
Lee Thomasond1983222012-02-06 08:41:24 -0800365
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700366 virtual void* Alloc() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700367 if ( !_root ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700368 // Need a new block.
369 Block* block = new Block();
Lee Thomason624d43f2012-10-12 10:58:48 -0700370 _blockPtrs.Push( block );
Lee Thomasond1983222012-02-06 08:41:24 -0800371
Dmitry-Me88145b82016-08-09 17:59:31 +0300372 Item* blockItems = block->items;
373 for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
374 blockItems[i].next = &(blockItems[i + 1]);
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700375 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300376 blockItems[ITEMS_PER_BLOCK - 1].next = 0;
377 _root = blockItems;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700378 }
Dmitry-Me88145b82016-08-09 17:59:31 +0300379 Item* const result = _root;
380 TIXMLASSERT( result != 0 );
Lee Thomason624d43f2012-10-12 10:58:48 -0700381 _root = _root->next;
Lee Thomason455c9d42012-02-06 09:14:14 -0800382
Lee Thomason624d43f2012-10-12 10:58:48 -0700383 ++_currentAllocs;
384 if ( _currentAllocs > _maxAllocs ) {
385 _maxAllocs = _currentAllocs;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700386 }
Dmitry-Me3161a332016-09-02 16:58:06 +0300387 ++_nAllocs;
388 ++_nUntracked;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700389 return result;
390 }
Lee Thomasonf07b9522014-10-30 13:25:12 -0700391
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700392 virtual void Free( void* mem ) {
393 if ( !mem ) {
394 return;
395 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700396 --_currentAllocs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300397 Item* item = static_cast<Item*>( mem );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700398#ifdef DEBUG
Dmitry-Me88145b82016-08-09 17:59:31 +0300399 memset( item, 0xfe, sizeof( *item ) );
Lee Thomason (grinliz)6020a012012-09-08 21:15:09 -0700400#endif
Dmitry-Me88145b82016-08-09 17:59:31 +0300401 item->next = _root;
402 _root = item;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700403 }
404 void Trace( const char* name ) {
405 printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
Dmitry-Me88145b82016-08-09 17:59:31 +0300406 name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
407 ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700408 }
Lee Thomasond1983222012-02-06 08:41:24 -0800409
Lee Thomason5b0a6772012-11-19 13:54:42 -0800410 void SetTracked() {
Dmitry-Me3161a332016-09-02 16:58:06 +0300411 --_nUntracked;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800412 }
413
414 int Untracked() const {
415 return _nUntracked;
416 }
417
Lee Thomason (grinliz)ac83b4e2013-02-01 09:02:34 -0800418 // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
419 // The test file is large, 170k.
420 // Release: VS2010 gcc(no opt)
421 // 1k: 4000
422 // 2k: 4000
423 // 4k: 3900 21000
424 // 16k: 5200
425 // 32k: 4300
426 // 64k: 4000 21000
Dmitry-Me88145b82016-08-09 17:59:31 +0300427 // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
428 // in private part if ITEMS_PER_BLOCK is private
429 enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
Jerome Martinez7921df12012-10-24 11:45:44 +0200430
Lee Thomasond1983222012-02-06 08:41:24 -0800431private:
Dmitry-Me3e0af372015-01-09 15:30:00 +0300432 MemPoolT( const MemPoolT& ); // not supported
433 void operator=( const MemPoolT& ); // not supported
434
Dmitry-Me88145b82016-08-09 17:59:31 +0300435 union Item {
436 Item* next;
437 char itemData[ITEM_SIZE];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700438 };
439 struct Block {
Dmitry-Me88145b82016-08-09 17:59:31 +0300440 Item items[ITEMS_PER_BLOCK];
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700441 };
Lee Thomason624d43f2012-10-12 10:58:48 -0700442 DynArray< Block*, 10 > _blockPtrs;
Dmitry-Me88145b82016-08-09 17:59:31 +0300443 Item* _root;
Lee Thomason455c9d42012-02-06 09:14:14 -0800444
Lee Thomason624d43f2012-10-12 10:58:48 -0700445 int _currentAllocs;
446 int _nAllocs;
447 int _maxAllocs;
Lee Thomason5b0a6772012-11-19 13:54:42 -0800448 int _nUntracked;
Lee Thomasond1983222012-02-06 08:41:24 -0800449};
450
Lee Thomason2c85a712012-01-31 08:24:24 -0800451
Lee Thomason56bdd022012-02-09 18:16:58 -0800452
453/**
454 Implements the interface to the "Visitor pattern" (see the Accept() method.)
455 If you call the Accept() method, it requires being passed a XMLVisitor
456 class to handle callbacks. For nodes that contain other nodes (Document, Element)
Thomas Roß08bdf502012-05-12 14:21:23 +0200457 you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
Lee Thomason56bdd022012-02-09 18:16:58 -0800458 are simply called with Visit().
459
460 If you return 'true' from a Visit method, recursive parsing will continue. If you return
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700461 false, <b>no children of this node or its siblings</b> will be visited.
Lee Thomason56bdd022012-02-09 18:16:58 -0800462
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700463 All flavors of Visit methods have a default implementation that returns 'true' (continue
Lee Thomason56bdd022012-02-09 18:16:58 -0800464 visiting). You need to only override methods that are interesting to you.
465
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600466 Generally Accept() is called on the XMLDocument, although all nodes support visiting.
Lee Thomason56bdd022012-02-09 18:16:58 -0800467
468 You should never change the document from a callback.
469
470 @sa XMLNode::Accept()
471*/
PKEuS16ed47d2013-07-06 12:02:43 +0200472class TINYXML2_LIB XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800473{
474public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700475 virtual ~XMLVisitor() {}
Lee Thomasond1983222012-02-06 08:41:24 -0800476
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700477 /// Visit a document.
478 virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
479 return true;
480 }
481 /// Visit a document.
482 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
483 return true;
484 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800485
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700486 /// Visit an element.
487 virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
488 return true;
489 }
490 /// Visit an element.
491 virtual bool VisitExit( const XMLElement& /*element*/ ) {
492 return true;
493 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800494
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700495 /// Visit a declaration.
496 virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
497 return true;
498 }
499 /// Visit a text node.
500 virtual bool Visit( const XMLText& /*text*/ ) {
501 return true;
502 }
503 /// Visit a comment node.
504 virtual bool Visit( const XMLComment& /*comment*/ ) {
505 return true;
506 }
507 /// Visit an unknown node.
508 virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
509 return true;
510 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800511};
512
Dmitry-Me66d2a842014-11-08 15:24:52 +0300513// WARNING: must match XMLDocument::_errorNames[]
numatrumpetbb5ffac2014-09-06 22:56:46 +0900514enum XMLError {
numatrumpetcd8550c2014-09-08 16:59:39 +0900515 XML_SUCCESS = 0,
numatrumpetcd8550c2014-09-08 16:59:39 +0900516 XML_NO_ATTRIBUTE,
517 XML_WRONG_ATTRIBUTE_TYPE,
518 XML_ERROR_FILE_NOT_FOUND,
519 XML_ERROR_FILE_COULD_NOT_BE_OPENED,
520 XML_ERROR_FILE_READ_ERROR,
Lee Thomason8bba8b42017-06-20 09:18:41 -0700521 UNUSED_XML_ERROR_ELEMENT_MISMATCH, // remove at next major version
numatrumpetcd8550c2014-09-08 16:59:39 +0900522 XML_ERROR_PARSING_ELEMENT,
523 XML_ERROR_PARSING_ATTRIBUTE,
Lee Thomason8bba8b42017-06-20 09:18:41 -0700524 UNUSED_XML_ERROR_IDENTIFYING_TAG, // remove at next major version
numatrumpetcd8550c2014-09-08 16:59:39 +0900525 XML_ERROR_PARSING_TEXT,
526 XML_ERROR_PARSING_CDATA,
527 XML_ERROR_PARSING_COMMENT,
528 XML_ERROR_PARSING_DECLARATION,
529 XML_ERROR_PARSING_UNKNOWN,
530 XML_ERROR_EMPTY_DOCUMENT,
531 XML_ERROR_MISMATCHED_ELEMENT,
532 XML_ERROR_PARSING,
533 XML_CAN_NOT_CONVERT_TEXT,
Lee Thomason331596e2014-09-11 14:56:43 -0700534 XML_NO_TEXT_NODE,
535
536 XML_ERROR_COUNT
numatrumpetbb5ffac2014-09-06 22:56:46 +0900537};
numatrumpetbb5ffac2014-09-06 22:56:46 +0900538
numatrumpetcd8550c2014-09-08 16:59:39 +0900539
U-Stream\Leeae25a442012-02-17 17:48:16 -0800540/*
541 Utility functionality.
542*/
Lee Thomasonce667c92016-12-26 16:45:30 -0800543class TINYXML2_LIB XMLUtil
Lee Thomason56bdd022012-02-09 18:16:58 -0800544{
Lee Thomasond1983222012-02-06 08:41:24 -0800545public:
kezenator4f756162016-11-29 19:46:27 +1000546 static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300547 TIXMLASSERT( p );
Lee Thomasone90e9012016-12-24 07:34:39 -0800548
Dmitry-Mefa20b222014-10-31 12:53:04 +0300549 while( IsWhiteSpace(*p) ) {
Lee Thomasone90e9012016-12-24 07:34:39 -0800550 if (curLineNumPtr && *p == '\n') {
kezenator4f756162016-11-29 19:46:27 +1000551 ++(*curLineNumPtr);
kezenatorec694152016-11-26 17:21:43 +1000552 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700553 ++p;
554 }
Dmitry-Mebb836dc2014-12-24 11:54:05 +0300555 TIXMLASSERT( p );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700556 return p;
557 }
kezenator4f756162016-11-29 19:46:27 +1000558 static char* SkipWhiteSpace( char* p, int* curLineNumPtr ) {
559 return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700560 }
Dmitry-Mefa20b222014-10-31 12:53:04 +0300561
562 // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
563 // correct, but simple, and usually works.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700564 static bool IsWhiteSpace( char p ) {
Jerome Martinez242c3ea2013-01-06 12:20:04 +0100565 return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700566 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200567
568 inline static bool IsNameStartChar( unsigned char ch ) {
Dmitry-Meea617f92015-01-01 16:32:01 +0300569 if ( ch >= 128 ) {
570 // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
571 return true;
572 }
573 if ( isalpha( ch ) ) {
574 return true;
575 }
576 return ch == ':' || ch == '_';
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200577 }
578
579 inline static bool IsNameChar( unsigned char ch ) {
580 return IsNameStartChar( ch )
581 || isdigit( ch )
582 || ch == '.'
583 || ch == '-';
584 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800585
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700586 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700587 if ( p == q ) {
588 return true;
589 }
Dmitry-Me21f99692016-10-03 13:01:57 +0300590 TIXMLASSERT( p );
591 TIXMLASSERT( q );
592 TIXMLASSERT( nChar >= 0 );
Lee Thomason598a88d2015-10-09 14:42:12 -0700593 return strncmp( p, q, nChar ) == 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700594 }
Martinsh Shaitersc6d02f42013-01-26 21:22:57 +0200595
Dmitry-Me7865aad2015-06-19 16:23:35 +0300596 inline static bool IsUTF8Continuation( char p ) {
Dmitry-Me72bb0ec2014-09-24 16:14:24 +0400597 return ( p & 0x80 ) != 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700598 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800599
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700600 static const char* ReadBOM( const char* p, bool* hasBOM );
601 // p is the starting location,
602 // the UTF-8 value of the entity will be placed in value, and length filled in.
603 static const char* GetCharacterRef( const char* p, char* value, int* length );
604 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
Lee Thomason21be8822012-07-15 17:27:22 -0700605
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700606 // converts primitive types to strings
607 static void ToStr( int v, char* buffer, int bufferSize );
608 static void ToStr( unsigned v, char* buffer, int bufferSize );
609 static void ToStr( bool v, char* buffer, int bufferSize );
610 static void ToStr( float v, char* buffer, int bufferSize );
611 static void ToStr( double v, char* buffer, int bufferSize );
Lee Thomason51c12712016-06-04 20:18:49 -0700612 static void ToStr(int64_t v, char* buffer, int bufferSize);
Lee Thomason21be8822012-07-15 17:27:22 -0700613
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700614 // converts strings to primitive types
615 static bool ToInt( const char* str, int* value );
616 static bool ToUnsigned( const char* str, unsigned* value );
617 static bool ToBool( const char* str, bool* value );
618 static bool ToFloat( const char* str, float* value );
619 static bool ToDouble( const char* str, double* value );
Lee Thomason51c12712016-06-04 20:18:49 -0700620 static bool ToInt64(const char* str, int64_t* value);
Lee Thomasonce667c92016-12-26 16:45:30 -0800621
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800622 // Changes what is serialized for a boolean value.
623 // Default to "true" and "false". Shouldn't be changed
624 // unless you have a special testing or compatibility need.
Lee Thomasonce667c92016-12-26 16:45:30 -0800625 // Be careful: static, global, & not thread safe.
626 // Be sure to set static const memory as parameters.
Lee Thomasonc5c99c22016-12-29 11:19:17 -0800627 static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
Lee Thomasonce667c92016-12-26 16:45:30 -0800628
629private:
Lee Thomasonf458d262016-12-26 22:47:25 -0800630 static const char* writeBoolTrue;
631 static const char* writeBoolFalse;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800632};
633
Lee Thomason5cae8972012-01-24 18:03:07 -0800634
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800635/** XMLNode is a base class for every object that is in the
636 XML Document Object Model (DOM), except XMLAttributes.
637 Nodes have siblings, a parent, and children which can
638 be navigated. A node is always in a XMLDocument.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700639 The type of a XMLNode can be queried, and it can
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800640 be cast to its more defined type.
641
Thomas Roß08bdf502012-05-12 14:21:23 +0200642 A XMLDocument allocates memory for all its Nodes.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800643 When the XMLDocument gets deleted, all its Nodes
644 will also be deleted.
645
646 @verbatim
647 A Document can contain: Element (container or leaf)
648 Comment (leaf)
649 Unknown (leaf)
650 Declaration( leaf )
651
652 An Element can contain: Element (container or leaf)
653 Text (leaf)
654 Attributes (not on tree)
655 Comment (leaf)
656 Unknown (leaf)
657
658 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800659*/
PKEuS16ed47d2013-07-06 12:02:43 +0200660class TINYXML2_LIB XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800661{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700662 friend class XMLDocument;
663 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800664public:
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800665
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700666 /// Get the XMLDocument that owns this XMLNode.
667 const XMLDocument* GetDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300668 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700669 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700670 }
671 /// Get the XMLDocument that owns this XMLNode.
672 XMLDocument* GetDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +0300673 TIXMLASSERT( _document );
Lee Thomason624d43f2012-10-12 10:58:48 -0700674 return _document;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700675 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800676
Lee Thomason2fa81722012-11-09 12:37:46 -0800677 /// Safely cast to an Element, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700678 virtual XMLElement* ToElement() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100679 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700680 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800681 /// Safely cast to Text, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700682 virtual XMLText* ToText() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100683 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700684 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800685 /// Safely cast to a Comment, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700686 virtual XMLComment* ToComment() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100687 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700688 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800689 /// Safely cast to a Document, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700690 virtual XMLDocument* ToDocument() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100691 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700692 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800693 /// Safely cast to a Declaration, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700694 virtual XMLDeclaration* ToDeclaration() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100695 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700696 }
Lee Thomason2fa81722012-11-09 12:37:46 -0800697 /// Safely cast to an Unknown, or null.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700698 virtual XMLUnknown* ToUnknown() {
MortenMacFly4ee49f12013-01-14 20:03:14 +0100699 return 0;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700700 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800701
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700702 virtual const XMLElement* ToElement() const {
703 return 0;
704 }
705 virtual const XMLText* ToText() const {
706 return 0;
707 }
708 virtual const XMLComment* ToComment() const {
709 return 0;
710 }
711 virtual const XMLDocument* ToDocument() const {
712 return 0;
713 }
714 virtual const XMLDeclaration* ToDeclaration() const {
715 return 0;
716 }
717 virtual const XMLUnknown* ToUnknown() const {
718 return 0;
719 }
Lee Thomason751da522012-02-10 08:50:51 -0800720
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700721 /** The meaning of 'value' changes for the specific type.
722 @verbatim
Sarat Addepalli9afd1d02015-05-19 12:56:27 +0530723 Document: empty (NULL is returned, not an empty string)
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700724 Element: name of the element
725 Comment: the comment text
726 Unknown: the tag contents
727 Text: the text string
728 @endverbatim
729 */
Michael Daumling21626882013-10-22 17:03:37 +0200730 const char* Value() const;
MortenMacFly4ee49f12013-01-14 20:03:14 +0100731
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700732 /** Set the Value of an XML node.
733 @sa Value()
734 */
735 void SetValue( const char* val, bool staticMem=false );
Lee Thomason2c85a712012-01-31 08:24:24 -0800736
kezenatorec694152016-11-26 17:21:43 +1000737 /// Gets the line number the node is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +1000738 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +1000739
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700740 /// Get the parent of this node on the DOM.
741 const XMLNode* Parent() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700742 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700743 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100744
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700745 XMLNode* Parent() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700746 return _parent;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700747 }
Lee Thomason751da522012-02-10 08:50:51 -0800748
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700749 /// Returns true if this node has no children.
750 bool NoChildren() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700751 return !_firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700752 }
Lee Thomason751da522012-02-10 08:50:51 -0800753
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700754 /// Get the first child node, or null if none exists.
755 const XMLNode* FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700756 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700757 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100758
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700759 XMLNode* FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700760 return _firstChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700761 }
MortenMacFly4ee49f12013-01-14 20:03:14 +0100762
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700763 /** Get the first child element, or optionally the first child
764 element with the specified name.
765 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300766 const XMLElement* FirstChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700767
Dmitry-Me886ad972015-07-22 11:00:51 +0300768 XMLElement* FirstChildElement( const char* name = 0 ) {
769 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700770 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800771
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700772 /// Get the last child node, or null if none exists.
773 const XMLNode* LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700774 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700775 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700776
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700777 XMLNode* LastChild() {
Dmitry-Me8d4e0ec2015-03-30 12:58:28 +0300778 return _lastChild;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700779 }
Lee Thomason2c85a712012-01-31 08:24:24 -0800780
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700781 /** Get the last child element or optionally the last child
782 element with the specified name.
783 */
Dmitry-Me886ad972015-07-22 11:00:51 +0300784 const XMLElement* LastChildElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700785
Dmitry-Me886ad972015-07-22 11:00:51 +0300786 XMLElement* LastChildElement( const char* name = 0 ) {
787 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700788 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700789
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700790 /// Get the previous (left) sibling node of this node.
791 const XMLNode* PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700792 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700793 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700794
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700795 XMLNode* PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700796 return _prev;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700797 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800798
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700799 /// Get the previous (left) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300800 const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
Lee Thomason624d43f2012-10-12 10:58:48 -0700801
Dmitry-Me886ad972015-07-22 11:00:51 +0300802 XMLElement* PreviousSiblingElement( const char* name = 0 ) {
803 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700804 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700805
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700806 /// Get the next (right) sibling node of this node.
807 const XMLNode* NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700808 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700809 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700810
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700811 XMLNode* NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -0700812 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700813 }
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700814
Andrew C. Martin0fd87462013-03-09 20:09:45 -0700815 /// Get the next (right) sibling element of this node, with an optionally supplied name.
Dmitry-Me886ad972015-07-22 11:00:51 +0300816 const XMLElement* NextSiblingElement( const char* name = 0 ) const;
Lee Thomason624d43f2012-10-12 10:58:48 -0700817
Dmitry-Me886ad972015-07-22 11:00:51 +0300818 XMLElement* NextSiblingElement( const char* name = 0 ) {
819 return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700820 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800821
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700822 /**
823 Add a child node as the last (right) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200824 If the child node is already part of the document,
825 it is moved from its old location to the new location.
826 Returns the addThis argument or 0 if the node does not
827 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700828 */
829 XMLNode* InsertEndChild( XMLNode* addThis );
Lee Thomason618dbf82012-02-28 12:34:27 -0800830
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700831 XMLNode* LinkEndChild( XMLNode* addThis ) {
832 return InsertEndChild( addThis );
833 }
834 /**
835 Add a child node as the first (left) child.
Michael Daumlinged523282013-10-23 07:47:29 +0200836 If the child node is already part of the document,
837 it is moved from its old location to the new location.
838 Returns the addThis argument or 0 if the node does not
839 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700840 */
841 XMLNode* InsertFirstChild( XMLNode* addThis );
842 /**
843 Add a node after the specified child node.
Michael Daumlinged523282013-10-23 07:47:29 +0200844 If the child node is already part of the document,
845 it is moved from its old location to the new location.
846 Returns the addThis argument or 0 if the afterThis node
847 is not a child of this node, or if the node does not
848 belong to the same document.
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700849 */
850 XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700851
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700852 /**
853 Delete all the children of this node.
854 */
855 void DeleteChildren();
U-Stream\Leeae25a442012-02-17 17:48:16 -0800856
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700857 /**
858 Delete a child of this node.
859 */
860 void DeleteChild( XMLNode* node );
Lee Thomason56bdd022012-02-09 18:16:58 -0800861
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700862 /**
863 Make a copy of this node, but not its children.
864 You may pass in a Document pointer that will be
865 the owner of the new Node. If the 'document' is
866 null, then the node returned will be allocated
867 from the current Document. (this->GetDocument())
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800868
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700869 Note: if called on a XMLDocument, this will return null.
870 */
871 virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800872
Lee Thomason7085f002017-06-01 18:09:43 -0700873 /**
Lee Thomason1346a172017-06-14 15:14:19 -0700874 Make a copy of this node and all its children.
Lee Thomason7085f002017-06-01 18:09:43 -0700875
Dmitry-Me3f63f212017-06-19 18:25:19 +0300876 If the 'target' is null, then the nodes will
877 be allocated in the current document. If 'target'
Lee Thomason1346a172017-06-14 15:14:19 -0700878 is specified, the memory will be allocated is the
879 specified XMLDocument.
Lee Thomason7085f002017-06-01 18:09:43 -0700880
881 NOTE: This is probably not the correct tool to
882 copy a document, since XMLDocuments can have multiple
Lee Thomason1346a172017-06-14 15:14:19 -0700883 top level XMLNodes. You probably want to use
884 XMLDocument::DeepCopy()
Lee Thomason7085f002017-06-01 18:09:43 -0700885 */
Dmitry-Me3f63f212017-06-19 18:25:19 +0300886 XMLNode* DeepClone( XMLDocument* target ) const;
Lee Thomason7085f002017-06-01 18:09:43 -0700887
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700888 /**
889 Test if 2 nodes are the same, but don't test children.
890 The 2 nodes do not need to be in the same Document.
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800891
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700892 Note: if called on a XMLDocument, this will return false.
893 */
894 virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800895
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600896 /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700897 XML tree will be conditionally visited and the host will be called back
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600898 via the XMLVisitor interface.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800899
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600900 This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
901 the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700902 interface versus any other.)
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800903
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700904 The interface has been based on ideas from:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800905
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700906 - http://www.saxproject.org/
907 - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800908
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700909 Which are both good references for "visiting".
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800910
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700911 An example of using Accept():
912 @verbatim
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600913 XMLPrinter printer;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700914 tinyxmlDoc.Accept( &printer );
915 const char* xmlcstr = printer.CStr();
916 @endverbatim
917 */
918 virtual bool Accept( XMLVisitor* visitor ) const = 0;
Lee Thomason56bdd022012-02-09 18:16:58 -0800919
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700920 /**
921 Set user data into the XMLNode. TinyXML-2 in
922 no way processes or interprets user data.
923 It is initially 0.
924 */
925 void SetUserData(void* userData) { _userData = userData; }
926
927 /**
928 Get user data set into the XMLNode. TinyXML-2 in
929 no way processes or interprets user data.
930 It is initially 0.
931 */
932 void* GetUserData() const { return _userData; }
933
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800934protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700935 XMLNode( XMLDocument* );
936 virtual ~XMLNode();
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700937
Dmitry-Me10b8ecc2017-04-12 17:57:44 +0300938 virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +0300939
Lee Thomason624d43f2012-10-12 10:58:48 -0700940 XMLDocument* _document;
941 XMLNode* _parent;
942 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +1000943 int _parseLineNum;
Lee Thomason3f57d272012-01-11 15:30:03 -0800944
Lee Thomason624d43f2012-10-12 10:58:48 -0700945 XMLNode* _firstChild;
946 XMLNode* _lastChild;
Lee Thomason3f57d272012-01-11 15:30:03 -0800947
Lee Thomason624d43f2012-10-12 10:58:48 -0700948 XMLNode* _prev;
949 XMLNode* _next;
Lee Thomason3f57d272012-01-11 15:30:03 -0800950
Lee Thomasonaf9bce12016-07-17 22:35:52 -0700951 void* _userData;
952
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800953private:
Lee Thomason624d43f2012-10-12 10:58:48 -0700954 MemPool* _memPool;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700955 void Unlink( XMLNode* child );
Dmitry-Mee3225b12014-09-03 11:03:11 +0400956 static void DeleteNode( XMLNode* node );
Lee Thomason3cebdc42015-01-05 17:16:28 -0800957 void InsertChildPreamble( XMLNode* insertThis ) const;
Dmitry-Meecb9b072016-10-12 16:44:59 +0300958 const XMLElement* ToElementWithName( const char* name ) const;
Dmitry-Mef547a992015-01-09 15:17:09 +0300959
960 XMLNode( const XMLNode& ); // not supported
961 XMLNode& operator=( const XMLNode& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800962};
963
964
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800965/** XML text.
966
967 Note that a text node can have child element nodes, for example:
968 @verbatim
969 <root>This is <b>bold</b></root>
970 @endverbatim
971
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -0700972 A text node can have 2 ways to output the next. "normal" output
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800973 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 -0700974 you generally want to leave it alone, but you can change the output mode with
Vasily Biryukov9a975b72013-05-11 21:41:42 +0600975 SetCData() and query it with CData().
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -0800976*/
PKEuS16ed47d2013-07-06 12:02:43 +0200977class TINYXML2_LIB XMLText : public XMLNode
Lee Thomason5492a1c2012-01-23 15:32:10 -0800978{
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700979 friend class XMLDocument;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800980public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700981 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -0800982
Lee Thomason624d43f2012-10-12 10:58:48 -0700983 virtual XMLText* ToText() {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700984 return this;
985 }
Lee Thomason624d43f2012-10-12 10:58:48 -0700986 virtual const XMLText* ToText() const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700987 return this;
988 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800989
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700990 /// Declare whether this should be CDATA or standard text.
Lee Thomason624d43f2012-10-12 10:58:48 -0700991 void SetCData( bool isCData ) {
992 _isCData = isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700993 }
994 /// Returns true if this is a CDATA text element.
995 bool CData() const {
Lee Thomason624d43f2012-10-12 10:58:48 -0700996 return _isCData;
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700997 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800998
Lee Thomasona9cf3f92012-10-11 16:56:51 -0700999 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1000 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001001
Lee Thomason5492a1c2012-01-23 15:32:10 -08001002protected:
Lee Thomason624d43f2012-10-12 10:58:48 -07001003 XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001004 virtual ~XMLText() {}
Lee Thomason5492a1c2012-01-23 15:32:10 -08001005
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001006 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001007
Lee Thomason5492a1c2012-01-23 15:32:10 -08001008private:
Lee Thomason624d43f2012-10-12 10:58:48 -07001009 bool _isCData;
Dmitry-Mef547a992015-01-09 15:17:09 +03001010
1011 XMLText( const XMLText& ); // not supported
1012 XMLText& operator=( const XMLText& ); // not supported
Lee Thomason5492a1c2012-01-23 15:32:10 -08001013};
1014
1015
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001016/** An XML Comment. */
PKEuS16ed47d2013-07-06 12:02:43 +02001017class TINYXML2_LIB XMLComment : public XMLNode
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001018{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001019 friend class XMLDocument;
Lee Thomason3f57d272012-01-11 15:30:03 -08001020public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001021 virtual XMLComment* ToComment() {
1022 return this;
1023 }
1024 virtual const XMLComment* ToComment() const {
1025 return this;
1026 }
Lee Thomasonce0763e2012-01-11 15:43:54 -08001027
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001028 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001029
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001030 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1031 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomasonce0763e2012-01-11 15:43:54 -08001032
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001033protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001034 XMLComment( XMLDocument* doc );
1035 virtual ~XMLComment();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001036
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001037 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001038
Lee Thomason3f57d272012-01-11 15:30:03 -08001039private:
Dmitry-Mef547a992015-01-09 15:17:09 +03001040 XMLComment( const XMLComment& ); // not supported
1041 XMLComment& operator=( const XMLComment& ); // not supported
U-Lama\Lee4cee6112011-12-31 14:58:18 -08001042};
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001043
1044
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001045/** In correct XML the declaration is the first entry in the file.
1046 @verbatim
1047 <?xml version="1.0" standalone="yes"?>
1048 @endverbatim
1049
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001050 TinyXML-2 will happily read or write files without a declaration,
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001051 however.
1052
1053 The text of the declaration isn't interpreted. It is parsed
1054 and written as a string.
1055*/
PKEuS16ed47d2013-07-06 12:02:43 +02001056class TINYXML2_LIB XMLDeclaration : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001057{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001058 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001059public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001060 virtual XMLDeclaration* ToDeclaration() {
1061 return this;
1062 }
1063 virtual const XMLDeclaration* ToDeclaration() const {
1064 return this;
1065 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001066
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001067 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001068
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001069 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1070 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001071
1072protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001073 XMLDeclaration( XMLDocument* doc );
1074 virtual ~XMLDeclaration();
Dmitry-Mef547a992015-01-09 15:17:09 +03001075
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001076 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001077
Dmitry-Mef547a992015-01-09 15:17:09 +03001078private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001079 XMLDeclaration( const XMLDeclaration& ); // not supported
1080 XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001081};
1082
1083
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001084/** Any tag that TinyXML-2 doesn't recognize is saved as an
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001085 unknown. It is a tag of text, but should not be modified.
1086 It will be written back to the XML, unchanged, when the file
1087 is saved.
1088
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001089 DTD tags get thrown into XMLUnknowns.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001090*/
PKEuS16ed47d2013-07-06 12:02:43 +02001091class TINYXML2_LIB XMLUnknown : public XMLNode
Lee Thomason50f97b22012-02-11 16:33:40 -08001092{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001093 friend class XMLDocument;
Lee Thomason50f97b22012-02-11 16:33:40 -08001094public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001095 virtual XMLUnknown* ToUnknown() {
1096 return this;
1097 }
1098 virtual const XMLUnknown* ToUnknown() const {
1099 return this;
1100 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001101
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001102 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001103
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001104 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1105 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason50f97b22012-02-11 16:33:40 -08001106
1107protected:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001108 XMLUnknown( XMLDocument* doc );
1109 virtual ~XMLUnknown();
Dmitry-Mef547a992015-01-09 15:17:09 +03001110
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001111 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001112
Dmitry-Mef547a992015-01-09 15:17:09 +03001113private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001114 XMLUnknown( const XMLUnknown& ); // not supported
1115 XMLUnknown& operator=( const XMLUnknown& ); // not supported
Lee Thomason50f97b22012-02-11 16:33:40 -08001116};
1117
1118
Lee Thomason1ff38e02012-02-14 18:18:16 -08001119
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001120/** An attribute is a name-value pair. Elements have an arbitrary
1121 number of attributes, each with a unique name.
1122
1123 @note The attributes are not XMLNodes. You may only query the
1124 Next() attribute in a list.
1125*/
PKEuS16ed47d2013-07-06 12:02:43 +02001126class TINYXML2_LIB XMLAttribute
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001127{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001128 friend class XMLElement;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001129public:
Lee Thomason2fa81722012-11-09 12:37:46 -08001130 /// The name of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001131 const char* Name() const;
1132
Lee Thomason2fa81722012-11-09 12:37:46 -08001133 /// The value of the attribute.
Michael Daumling21626882013-10-22 17:03:37 +02001134 const char* Value() const;
1135
kezenatorec694152016-11-26 17:21:43 +10001136 /// Gets the line number the attribute is in, if the document was parsed from a file.
kezenator19d8ea82016-11-29 19:50:27 +10001137 int GetLineNum() const { return _parseLineNum; }
kezenatorec694152016-11-26 17:21:43 +10001138
Lee Thomason2fa81722012-11-09 12:37:46 -08001139 /// The next attribute in the list.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001140 const XMLAttribute* Next() const {
MortenMacFly4ee49f12013-01-14 20:03:14 +01001141 return _next;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001142 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001143
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001144 /** IntValue interprets the attribute as an integer, and returns the value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001145 If the value isn't an integer, 0 will be returned. There is no error checking;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001146 use QueryIntValue() if you need error checking.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001147 */
Lee Thomason51c12712016-06-04 20:18:49 -07001148 int IntValue() const {
1149 int i = 0;
1150 QueryIntValue(&i);
1151 return i;
1152 }
1153
1154 int64_t Int64Value() const {
1155 int64_t i = 0;
1156 QueryInt64Value(&i);
1157 return i;
1158 }
1159
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001160 /// Query as an unsigned integer. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001161 unsigned UnsignedValue() const {
1162 unsigned i=0;
1163 QueryUnsignedValue( &i );
1164 return i;
1165 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001166 /// Query as a boolean. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001167 bool BoolValue() const {
1168 bool b=false;
1169 QueryBoolValue( &b );
1170 return b;
1171 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001172 /// Query as a double. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001173 double DoubleValue() const {
1174 double d=0;
1175 QueryDoubleValue( &d );
1176 return d;
1177 }
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001178 /// Query as a float. See IntValue()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001179 float FloatValue() const {
1180 float f=0;
1181 QueryFloatValue( &f );
1182 return f;
1183 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001184
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001185 /** QueryIntValue interprets the attribute as an integer, and returns the value
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001186 in the provided parameter. The function will return XML_SUCCESS on success,
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001187 and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
1188 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001189 XMLError QueryIntValue( int* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001190 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001191 XMLError QueryUnsignedValue( unsigned int* value ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001192 /// See QueryIntValue
1193 XMLError QueryInt64Value(int64_t* value) const;
1194 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001195 XMLError QueryBoolValue( bool* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001196 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001197 XMLError QueryDoubleValue( double* value ) const;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001198 /// See QueryIntValue
Lee Thomason2fa81722012-11-09 12:37:46 -08001199 XMLError QueryFloatValue( float* value ) const;
Lee Thomason50adb4c2012-02-13 15:07:09 -08001200
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001201 /// Set the attribute to a string value.
1202 void SetAttribute( const char* value );
1203 /// Set the attribute to value.
1204 void SetAttribute( int value );
1205 /// Set the attribute to value.
1206 void SetAttribute( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001207 /// Set the attribute to value.
1208 void SetAttribute(int64_t value);
1209 /// Set the attribute to value.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001210 void SetAttribute( bool value );
1211 /// Set the attribute to value.
1212 void SetAttribute( double value );
1213 /// Set the attribute to value.
1214 void SetAttribute( float value );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001215
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001216private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001217 enum { BUF_SIZE = 200 };
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001218
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001219 XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001220 virtual ~XMLAttribute() {}
Lee Thomason624d43f2012-10-12 10:58:48 -07001221
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001222 XMLAttribute( const XMLAttribute& ); // not supported
1223 void operator=( const XMLAttribute& ); // not supported
1224 void SetName( const char* name );
Lee Thomason50adb4c2012-02-13 15:07:09 -08001225
kezenator4f756162016-11-29 19:46:27 +10001226 char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001227
Lee Thomason624d43f2012-10-12 10:58:48 -07001228 mutable StrPair _name;
1229 mutable StrPair _value;
kezenatorec694152016-11-26 17:21:43 +10001230 int _parseLineNum;
Lee Thomason624d43f2012-10-12 10:58:48 -07001231 XMLAttribute* _next;
1232 MemPool* _memPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001233};
1234
1235
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001236/** The element is a container class. It has a value, the element name,
1237 and can contain other elements, text, comments, and unknowns.
1238 Elements also contain an arbitrary number of attributes.
1239*/
PKEuS16ed47d2013-07-06 12:02:43 +02001240class TINYXML2_LIB XMLElement : public XMLNode
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001241{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001242 friend class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001243public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001244 /// Get the name of an element (which is the Value() of the node.)
1245 const char* Name() const {
1246 return Value();
1247 }
1248 /// Set the name of the element.
1249 void SetName( const char* str, bool staticMem=false ) {
1250 SetValue( str, staticMem );
1251 }
Lee Thomason2c85a712012-01-31 08:24:24 -08001252
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001253 virtual XMLElement* ToElement() {
1254 return this;
1255 }
1256 virtual const XMLElement* ToElement() const {
1257 return this;
1258 }
1259 virtual bool Accept( XMLVisitor* visitor ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001260
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001261 /** Given an attribute name, Attribute() returns the value
1262 for the attribute of that name, or null if none
1263 exists. For example:
Lee Thomason92258152012-03-24 13:05:39 -07001264
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001265 @verbatim
1266 const char* value = ele->Attribute( "foo" );
1267 @endverbatim
Lee Thomason92258152012-03-24 13:05:39 -07001268
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001269 The 'value' parameter is normally null. However, if specified,
1270 the attribute will only be returned if the 'name' and 'value'
1271 match. This allow you to write code:
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001272
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001273 @verbatim
1274 if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
1275 @endverbatim
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001276
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001277 rather than:
1278 @verbatim
1279 if ( ele->Attribute( "foo" ) ) {
1280 if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
1281 }
1282 @endverbatim
1283 */
1284 const char* Attribute( const char* name, const char* value=0 ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001285
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001286 /** Given an attribute name, IntAttribute() returns the value
Lee Thomason13cbc9a2016-10-27 14:55:07 -07001287 of the attribute interpreted as an integer. The default
1288 value will be returned if the attribute isn't present,
1289 or if there is an error. (For a method with error
1290 checking, see QueryIntAttribute()).
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001291 */
Josh Wittnercf3dd092016-10-11 18:57:17 -07001292 int IntAttribute(const char* name, int defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001293 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001294 unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001295 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001296 int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001297 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001298 bool BoolAttribute(const char* name, bool defaultValue = false) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001299 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001300 double DoubleAttribute(const char* name, double defaultValue = 0) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001301 /// See IntAttribute()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001302 float FloatAttribute(const char* name, float defaultValue = 0) const;
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001303
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001304 /** Given an attribute name, QueryIntAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001305 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001306 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1307 doesn't exist. If successful, the result of the conversion
1308 will be written to 'value'. If not successful, nothing will
1309 be written to 'value'. This allows you to provide default
1310 value:
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001311
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001312 @verbatim
1313 int value = 10;
1314 QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1315 @endverbatim
1316 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001317 XMLError QueryIntAttribute( const char* name, int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001318 const XMLAttribute* a = FindAttribute( name );
1319 if ( !a ) {
1320 return XML_NO_ATTRIBUTE;
1321 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001322 return a->QueryIntValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001323 }
Lee Thomason51c12712016-06-04 20:18:49 -07001324
1325 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001326 XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001327 const XMLAttribute* a = FindAttribute( name );
1328 if ( !a ) {
1329 return XML_NO_ATTRIBUTE;
1330 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001331 return a->QueryUnsignedValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001332 }
Lee Thomason51c12712016-06-04 20:18:49 -07001333
1334 /// See QueryIntAttribute()
1335 XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
1336 const XMLAttribute* a = FindAttribute(name);
1337 if (!a) {
1338 return XML_NO_ATTRIBUTE;
1339 }
1340 return a->QueryInt64Value(value);
1341 }
1342
1343 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001344 XMLError QueryBoolAttribute( const char* name, bool* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001345 const XMLAttribute* a = FindAttribute( name );
1346 if ( !a ) {
1347 return XML_NO_ATTRIBUTE;
1348 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001349 return a->QueryBoolValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001350 }
1351 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001352 XMLError QueryDoubleAttribute( const char* name, double* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001353 const XMLAttribute* a = FindAttribute( name );
1354 if ( !a ) {
1355 return XML_NO_ATTRIBUTE;
1356 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001357 return a->QueryDoubleValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001358 }
1359 /// See QueryIntAttribute()
Lee Thomason2fa81722012-11-09 12:37:46 -08001360 XMLError QueryFloatAttribute( const char* name, float* value ) const {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001361 const XMLAttribute* a = FindAttribute( name );
1362 if ( !a ) {
1363 return XML_NO_ATTRIBUTE;
1364 }
Lee Thomason624d43f2012-10-12 10:58:48 -07001365 return a->QueryFloatValue( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001366 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001367
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001368
1369 /** Given an attribute name, QueryAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001370 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001371 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1372 doesn't exist. It is overloaded for the primitive types,
1373 and is a generally more convenient replacement of
1374 QueryIntAttribute() and related functions.
1375
1376 If successful, the result of the conversion
1377 will be written to 'value'. If not successful, nothing will
1378 be written to 'value'. This allows you to provide default
1379 value:
1380
1381 @verbatim
1382 int value = 10;
1383 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1384 @endverbatim
1385 */
1386 int QueryAttribute( const char* name, int* value ) const {
1387 return QueryIntAttribute( name, value );
1388 }
1389
1390 int QueryAttribute( const char* name, unsigned int* value ) const {
1391 return QueryUnsignedAttribute( name, value );
1392 }
1393
Lee Thomason51c12712016-06-04 20:18:49 -07001394 int QueryAttribute(const char* name, int64_t* value) const {
1395 return QueryInt64Attribute(name, value);
1396 }
1397
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001398 int QueryAttribute( const char* name, bool* value ) const {
1399 return QueryBoolAttribute( name, value );
1400 }
1401
1402 int QueryAttribute( const char* name, double* value ) const {
1403 return QueryDoubleAttribute( name, value );
1404 }
1405
1406 int QueryAttribute( const char* name, float* value ) const {
1407 return QueryFloatAttribute( name, value );
1408 }
1409
1410 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001411 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001412 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001413 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001414 }
1415 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001416 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001417 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001418 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001419 }
1420 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001421 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001422 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001423 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001424 }
Lee Thomason51c12712016-06-04 20:18:49 -07001425
1426 /// Sets the named attribute to value.
1427 void SetAttribute(const char* name, int64_t value) {
1428 XMLAttribute* a = FindOrCreateAttribute(name);
1429 a->SetAttribute(value);
1430 }
1431
1432 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001433 void SetAttribute( const char* name, bool 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, double 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 Thomasonc3708cc2014-01-14 12:30:03 -08001442 /// Sets the named attribute to value.
1443 void SetAttribute( const char* name, float value ) {
1444 XMLAttribute* a = FindOrCreateAttribute( name );
1445 a->SetAttribute( value );
1446 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001447
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001448 /**
1449 Delete an attribute.
1450 */
1451 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001452
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001453 /// Return the first attribute in the list.
1454 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001455 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001456 }
1457 /// Query a specific attribute in the list.
1458 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001459
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001460 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001461 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001462 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001463
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001464 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001465 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001466
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001467 This is a convenient method for getting the text of simple contained text:
1468 @verbatim
1469 <foo>This is text</foo>
1470 const char* str = fooElement->GetText();
1471 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001472
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001473 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001474
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001475 Note that this function can be misleading. If the element foo was created from
1476 this XML:
1477 @verbatim
1478 <foo><b>This is text</b></foo>
1479 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001480
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001481 then the value of str would be null. The first child node isn't a text node, it is
1482 another element. From this XML:
1483 @verbatim
1484 <foo>This is <b>text</b></foo>
1485 @endverbatim
1486 GetText() will return "This is ".
1487 */
1488 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001489
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001490 /** Convenience function for easy access to the text inside an element. Although easy
1491 and concise, SetText() is limited compared to creating an XMLText child
1492 and mutating it directly.
1493
1494 If the first child of 'this' is a XMLText, SetText() sets its value to
1495 the given string, otherwise it will create a first child that is an XMLText.
1496
1497 This is a convenient method for setting the text of simple contained text:
1498 @verbatim
1499 <foo>This is text</foo>
1500 fooElement->SetText( "Hullaballoo!" );
1501 <foo>Hullaballoo!</foo>
1502 @endverbatim
1503
1504 Note that this function can be misleading. If the element foo was created from
1505 this XML:
1506 @verbatim
1507 <foo><b>This is text</b></foo>
1508 @endverbatim
1509
1510 then it will not change "This is text", but rather prefix it with a text element:
1511 @verbatim
1512 <foo>Hullaballoo!<b>This is text</b></foo>
1513 @endverbatim
1514
1515 For this XML:
1516 @verbatim
1517 <foo />
1518 @endverbatim
1519 SetText() will generate
1520 @verbatim
1521 <foo>Hullaballoo!</foo>
1522 @endverbatim
1523 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001524 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001525 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001526 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001527 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001528 void SetText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001529 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1530 void SetText(int64_t value);
1531 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001532 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001533 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001534 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001535 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001536 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001537
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001538 /**
1539 Convenience method to query the value of a child text node. This is probably best
1540 shown by example. Given you have a document is this form:
1541 @verbatim
1542 <point>
1543 <x>1</x>
1544 <y>1.4</y>
1545 </point>
1546 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001547
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001548 The QueryIntText() and similar functions provide a safe and easier way to get to the
1549 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001550
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001551 @verbatim
1552 int x = 0;
1553 float y = 0; // types of x and y are contrived for example
1554 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1555 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1556 xElement->QueryIntText( &x );
1557 yElement->QueryFloatText( &y );
1558 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001559
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001560 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1561 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 -07001562
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001563 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001564 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001565 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001566 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001567 /// See QueryIntText()
1568 XMLError QueryInt64Text(int64_t* uval) const;
1569 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001570 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001571 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001572 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001573 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001574 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001575
Josh Wittnercf3dd092016-10-11 18:57:17 -07001576 int IntText(int defaultValue = 0) const;
1577
Josh Wittner3a621f52016-09-12 19:17:54 -07001578 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001579 unsigned UnsignedText(unsigned defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001580 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001581 int64_t Int64Text(int64_t defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001582 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001583 bool BoolText(bool defaultValue = false) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001584 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001585 double DoubleText(double defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001586 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001587 float FloatText(float defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001588
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001589 // internal:
Dmitry-Mee5035632017-04-05 18:02:40 +03001590 enum ElementClosingType {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001591 OPEN, // <foo>
1592 CLOSED, // <foo/>
1593 CLOSING // </foo>
1594 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001595 ElementClosingType ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001596 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001597 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001598 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1599 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001600
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001601protected:
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001602 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001603
Lee Thomason50adb4c2012-02-13 15:07:09 -08001604private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001605 XMLElement( XMLDocument* doc );
1606 virtual ~XMLElement();
1607 XMLElement( const XMLElement& ); // not supported
1608 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001609
Dmitry-Me1227d512014-12-05 13:41:45 +03001610 XMLAttribute* FindAttribute( const char* name ) {
1611 return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
1612 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001613 XMLAttribute* FindOrCreateAttribute( const char* name );
1614 //void LinkAttribute( XMLAttribute* attrib );
kezenator4f756162016-11-29 19:46:27 +10001615 char* ParseAttributes( char* p, int* curLineNumPtr );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001616 static void DeleteAttribute( XMLAttribute* attribute );
Dmitry-Mea60caa22016-11-22 18:28:08 +03001617 XMLAttribute* CreateAttribute();
Lee Thomason67d61312012-01-24 16:01:51 -08001618
Lee Thomason5bb2d802014-01-24 10:42:57 -08001619 enum { BUF_SIZE = 200 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001620 ElementClosingType _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001621 // The attribute list is ordered; there is no 'lastAttribute'
1622 // because the list needs to be scanned for dupes before adding
1623 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001624 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001625};
1626
1627
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001628enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001629 PRESERVE_WHITESPACE,
1630 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001631};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001632
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001633
1634/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001635 It can be saved, loaded, and printed to the screen.
1636 All Nodes are connected and allocated to a Document.
1637 If the Document is deleted, all its Nodes are also deleted.
1638*/
PKEuS16ed47d2013-07-06 12:02:43 +02001639class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001640{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001641 friend class XMLElement;
Lee Thomasonf49b9652017-10-11 10:57:49 -07001642 // Gives access to SetError, but over-access for everything else.
1643 // Wishing C++ had "internal" scope.
1644 friend class XMLNode;
1645 friend class XMLText;
1646 friend class XMLComment;
1647 friend class XMLDeclaration;
1648 friend class XMLUnknown;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001649public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001650 /// constructor
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001651 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001652 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001653
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001654 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001655 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001656 return this;
1657 }
1658 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001659 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001660 return this;
1661 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001662
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001663 /**
1664 Parse an XML file from a character string.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001665 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001666 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001667
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001668 You may optionally pass in the 'nBytes', which is
1669 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001670 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001671 null terminated string.
1672 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001673 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001674
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001675 /**
1676 Load an XML file from disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001677 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001678 an errorID.
1679 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001680 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001681
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001682 /**
1683 Load an XML file from disk. You are responsible
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001684 for providing and closing the FILE*.
1685
1686 NOTE: The file should be opened as binary ("rb")
1687 not text in order for TinyXML-2 to correctly
1688 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001689
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001690 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001691 an errorID.
1692 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001693 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001694
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001695 /**
1696 Save the XML file to disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001697 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001698 an errorID.
1699 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001700 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001701
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001702 /**
1703 Save the XML file to disk. You are responsible
1704 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001705
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001706 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001707 an errorID.
1708 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001709 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001710
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001711 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001712 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001713 }
1714 Whitespace WhitespaceMode() const {
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001715 return _whitespaceMode;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001716 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001717
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001718 /**
1719 Returns true if this document has a leading Byte Order Mark of UTF8.
1720 */
1721 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001722 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001723 }
1724 /** Sets whether to write the BOM when writing the file.
1725 */
1726 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001727 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001728 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001729
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001730 /** Return the root element of DOM. Equivalent to FirstChildElement().
1731 To get the first node, use FirstChild().
1732 */
1733 XMLElement* RootElement() {
1734 return FirstChildElement();
1735 }
1736 const XMLElement* RootElement() const {
1737 return FirstChildElement();
1738 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001739
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001740 /** Print the Document. If the Printer is not provided, it will
1741 print to stdout. If you provide Printer, this can print to a file:
1742 @verbatim
1743 XMLPrinter printer( fp );
1744 doc.Print( &printer );
1745 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001746
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001747 Or you can use a printer to print to memory:
1748 @verbatim
1749 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001750 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001751 // printer.CStr() has a const char* to the XML
1752 @endverbatim
1753 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001754 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001755 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001756
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001757 /**
1758 Create a new Element associated with
1759 this Document. The memory for the Element
1760 is managed by the Document.
1761 */
1762 XMLElement* NewElement( const char* name );
1763 /**
1764 Create a new Comment associated with
1765 this Document. The memory for the Comment
1766 is managed by the Document.
1767 */
1768 XMLComment* NewComment( const char* comment );
1769 /**
1770 Create a new Text associated with
1771 this Document. The memory for the Text
1772 is managed by the Document.
1773 */
1774 XMLText* NewText( const char* text );
1775 /**
1776 Create a new Declaration associated with
1777 this Document. The memory for the object
1778 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001779
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001780 If the 'text' param is null, the standard
1781 declaration is used.:
1782 @verbatim
1783 <?xml version="1.0" encoding="UTF-8"?>
1784 @endverbatim
1785 */
1786 XMLDeclaration* NewDeclaration( const char* text=0 );
1787 /**
1788 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001789 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001790 is managed by the Document.
1791 */
1792 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001793
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001794 /**
1795 Delete a node associated with this document.
1796 It will be unlinked from the DOM.
1797 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001798 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001799
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001800 void ClearError() {
Lee Thomasonaa188392017-09-19 17:54:31 -07001801 SetError(XML_SUCCESS, 0, 0);
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001802 }
1803
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001804 /// Return true if there was an error parsing the document.
1805 bool Error() const {
Lee Thomason85536252016-06-04 19:10:53 -07001806 return _errorID != XML_SUCCESS;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001807 }
1808 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001809 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001810 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001811 }
Lee Thomason331596e2014-09-11 14:56:43 -07001812 const char* ErrorName() const;
Lee Thomasone90e9012016-12-24 07:34:39 -08001813 static const char* ErrorIDToName(XMLError errorID);
Lee Thomason331596e2014-09-11 14:56:43 -07001814
Lee Thomasonf49b9652017-10-11 10:57:49 -07001815 /** Returns a "long form" error description. A hopefully helpful
1816 diagnostic with location, line number, and/or additional info.
1817 */
1818 const char* ErrorStr() const;
1819
1820 /// A (trivial) utility function that prints the ErrorStr() to stdout.
1821 void PrintError() const;
Lee Thomason8c9e3132017-06-26 16:55:01 -07001822
kezenatorec694152016-11-26 17:21:43 +10001823 /// Return the line where the error occured, or zero if unknown.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001824 int ErrorLineNum() const
kezenatorec694152016-11-26 17:21:43 +10001825 {
1826 return _errorLineNum;
1827 }
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001828
1829 /// Clear the document, resetting it to the initial state.
1830 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001831
Lee Thomason7085f002017-06-01 18:09:43 -07001832 /**
Lee Thomasonb29f5562017-06-01 18:45:32 -07001833 Copies this document to a target document.
Lee Thomason7085f002017-06-01 18:09:43 -07001834 The target will be completely cleared before the copy.
Lee Thomasonb29f5562017-06-01 18:45:32 -07001835 If you want to copy a sub-tree, see XMLNode::DeepClone().
Lee Thomason7085f002017-06-01 18:09:43 -07001836
Lee Thomason1346a172017-06-14 15:14:19 -07001837 NOTE: that the 'target' must be non-null.
Lee Thomason7085f002017-06-01 18:09:43 -07001838 */
Shuichiro Suzuki87cd4e02017-08-24 11:20:26 +09001839 void DeepCopy(XMLDocument* target) const;
Lee Thomason7085f002017-06-01 18:09:43 -07001840
1841 // internal
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001842 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001843
Lee Thomason816d3fa2017-06-05 14:35:55 -07001844 // internal
1845 void MarkInUse(XMLNode*);
1846
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001847 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1848 return 0;
1849 }
1850 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1851 return false;
1852 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001853
Lee Thomason3f57d272012-01-11 15:30:03 -08001854private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001855 XMLDocument( const XMLDocument& ); // not supported
1856 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001857
Lee Thomasone90e9012016-12-24 07:34:39 -08001858 bool _writeBOM;
1859 bool _processEntities;
1860 XMLError _errorID;
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001861 Whitespace _whitespaceMode;
Lee Thomasonaa188392017-09-19 17:54:31 -07001862 mutable StrPair _errorStr;
Lee Thomasone90e9012016-12-24 07:34:39 -08001863 int _errorLineNum;
1864 char* _charBuffer;
1865 int _parseCurLineNum;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001866 // Memory tracking does add some overhead.
1867 // However, the code assumes that you don't
1868 // have a bunch of unlinked nodes around.
1869 // Therefore it takes less memory to track
1870 // in the document vs. a linked list in the XMLNode,
1871 // and the performance is the same.
1872 DynArray<XMLNode*, 10> _unlinked;
Lee Thomasond1983222012-02-06 08:41:24 -08001873
Lee Thomason624d43f2012-10-12 10:58:48 -07001874 MemPoolT< sizeof(XMLElement) > _elementPool;
1875 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1876 MemPoolT< sizeof(XMLText) > _textPool;
1877 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001878
1879 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001880
1881 void Parse();
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001882
Lee Thomasonf49b9652017-10-11 10:57:49 -07001883 void SetError( XMLError error, int lineNum, const char* format, ... );
1884
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001885 template<class NodeType, int PoolElementSize>
1886 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
Lee Thomason5cae8972012-01-24 18:03:07 -08001887};
1888
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001889template<class NodeType, int PoolElementSize>
1890inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1891{
1892 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1893 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1894 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1895 TIXMLASSERT( returnNode );
1896 returnNode->_memPool = &pool;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001897
1898 _unlinked.Push(returnNode);
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001899 return returnNode;
1900}
Lee Thomason7c913cd2012-01-26 18:32:34 -08001901
Lee Thomason3ffdd392012-03-28 17:27:55 -07001902/**
1903 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001904 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001905 DOM structure. It is a separate utility class.
1906
1907 Take an example:
1908 @verbatim
1909 <Document>
1910 <Element attributeA = "valueA">
1911 <Child attributeB = "value1" />
1912 <Child attributeB = "value2" />
1913 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001914 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001915 @endverbatim
1916
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001917 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001918 easy to write a *lot* of code that looks like:
1919
1920 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001921 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001922 if ( root )
1923 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001924 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001925 if ( element )
1926 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001927 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001928 if ( child )
1929 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001930 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001931 if ( child2 )
1932 {
1933 // Finally do something useful.
1934 @endverbatim
1935
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001936 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001937 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001938 and correct to use:
1939
1940 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001941 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001942 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001943 if ( child2 )
1944 {
1945 // do something useful
1946 @endverbatim
1947
1948 Which is MUCH more concise and useful.
1949
1950 It is also safe to copy handles - internally they are nothing more than node pointers.
1951 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001952 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001953 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001954
1955 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001956*/
PKEuS16ed47d2013-07-06 12:02:43 +02001957class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001958{
1959public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001960 /// 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 +02001961 XMLHandle( XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001962 }
1963 /// Create a handle from a node.
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001964 XMLHandle( XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001965 }
1966 /// Copy constructor
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001967 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001968 }
1969 /// Assignment
1970 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001971 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001972 return *this;
1973 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001974
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001975 /// Get the first child of this handle.
1976 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001977 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001978 }
1979 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001980 XMLHandle FirstChildElement( const char* name = 0 ) {
1981 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001982 }
1983 /// Get the last child of this handle.
1984 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001985 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001986 }
1987 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001988 XMLHandle LastChildElement( const char* name = 0 ) {
1989 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001990 }
1991 /// Get the previous sibling of this handle.
1992 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001993 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001994 }
1995 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001996 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
1997 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001998 }
1999 /// Get the next sibling of this handle.
2000 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002001 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002002 }
2003 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002004 XMLHandle NextSiblingElement( const char* name = 0 ) {
2005 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002006 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002007
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002008 /// Safe cast to XMLNode. This can return null.
2009 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002010 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002011 }
2012 /// Safe cast to XMLElement. This can return null.
2013 XMLElement* ToElement() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002014 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002015 }
2016 /// Safe cast to XMLText. This can return null.
2017 XMLText* ToText() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002018 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002019 }
2020 /// Safe cast to XMLUnknown. This can return null.
2021 XMLUnknown* ToUnknown() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002022 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002023 }
2024 /// Safe cast to XMLDeclaration. This can return null.
2025 XMLDeclaration* ToDeclaration() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002026 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002027 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002028
2029private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002030 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002031};
2032
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08002033
2034/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002035 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
2036 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07002037*/
PKEuS16ed47d2013-07-06 12:02:43 +02002038class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07002039{
2040public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002041 XMLConstHandle( const XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002042 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002043 XMLConstHandle( const XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002044 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002045 XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002046 }
Lee Thomason8b899812012-04-04 15:58:16 -07002047
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002048 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002049 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002050 return *this;
2051 }
Lee Thomason8b899812012-04-04 15:58:16 -07002052
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002053 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002054 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002055 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002056 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2057 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002058 }
2059 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002060 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002061 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002062 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2063 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002064 }
2065 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002066 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002067 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002068 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2069 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002070 }
2071 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002072 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002073 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002074 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2075 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002076 }
Lee Thomason8b899812012-04-04 15:58:16 -07002077
2078
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002079 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002080 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002081 }
2082 const XMLElement* ToElement() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002083 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002084 }
2085 const XMLText* ToText() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002086 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002087 }
2088 const XMLUnknown* ToUnknown() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002089 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002090 }
2091 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002092 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002093 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08002094
Lee Thomason5cae8972012-01-24 18:03:07 -08002095private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002096 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08002097};
Lee Thomason6f381b72012-03-02 12:59:39 -08002098
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002099
2100/**
2101 Printing functionality. The XMLPrinter gives you more
2102 options than the XMLDocument::Print() method.
2103
2104 It can:
2105 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02002106 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002107 -# Print XML without a XMLDocument.
2108
2109 Print to Memory
2110
2111 @verbatim
2112 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06002113 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02002114 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002115 @endverbatim
2116
2117 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002118
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002119 You provide the file pointer.
2120 @verbatim
2121 XMLPrinter printer( fp );
2122 doc.Print( &printer );
2123 @endverbatim
2124
2125 Print without a XMLDocument
2126
2127 When loading, an XML parser is very useful. However, sometimes
2128 when saving, it just gets in the way. The code is often set up
2129 for streaming, and constructing the DOM is just overhead.
2130
2131 The Printer supports the streaming case. The following code
2132 prints out a trivially simple XML file without ever creating
2133 an XML document.
2134
2135 @verbatim
2136 XMLPrinter printer( fp );
2137 printer.OpenElement( "foo" );
2138 printer.PushAttribute( "foo", "bar" );
2139 printer.CloseElement();
2140 @endverbatim
2141*/
PKEuS16ed47d2013-07-06 12:02:43 +02002142class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002143{
2144public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002145 /** Construct the printer. If the FILE* is specified,
2146 this will print to the FILE. Else it will print
2147 to memory, and the result is available in CStr().
2148 If 'compact' is set to true, then output is created
2149 with only required whitespace and newlines.
2150 */
PKEuS1bfb9542013-08-04 13:51:17 +02002151 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002152 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002153
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002154 /** If streaming, write the BOM and declaration. */
2155 void PushHeader( bool writeBOM, bool writeDeclaration );
2156 /** If streaming, start writing an element.
2157 The element must be closed with CloseElement()
2158 */
Lee Thomason256adb62014-04-06 14:41:46 -07002159 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002160 /// If streaming, add an attribute to an open element.
2161 void PushAttribute( const char* name, const char* value );
2162 void PushAttribute( const char* name, int value );
2163 void PushAttribute( const char* name, unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002164 void PushAttribute(const char* name, int64_t value);
2165 void PushAttribute( const char* name, bool value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002166 void PushAttribute( const char* name, double value );
2167 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002168 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002169
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002170 /// Add a text node.
2171 void PushText( const char* text, bool cdata=false );
2172 /// Add a text node from an integer.
2173 void PushText( int value );
2174 /// Add a text node from an unsigned.
2175 void PushText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002176 /// Add a text node from an unsigned.
2177 void PushText(int64_t value);
2178 /// Add a text node from a bool.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002179 void PushText( bool value );
2180 /// Add a text node from a float.
2181 void PushText( float value );
2182 /// Add a text node from a double.
2183 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002184
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002185 /// Add a comment
2186 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002187
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002188 void PushDeclaration( const char* value );
2189 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002190
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002191 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2192 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2193 return true;
2194 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002195
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002196 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2197 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002198
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002199 virtual bool Visit( const XMLText& text );
2200 virtual bool Visit( const XMLComment& comment );
2201 virtual bool Visit( const XMLDeclaration& declaration );
2202 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002203
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002204 /**
2205 If in print to memory mode, return a pointer to
2206 the XML file in memory.
2207 */
2208 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002209 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002210 }
2211 /**
2212 If in print to memory mode, return the size
2213 of the XML file in memory. (Note the size returned
2214 includes the terminating null.)
2215 */
2216 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002217 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002218 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002219 /**
2220 If in print to memory mode, reset the buffer to the
2221 beginning.
2222 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002223 void ClearBuffer() {
2224 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002225 _buffer.Push(0);
Lee Thomason53858b42017-06-01 19:09:16 -07002226 _firstElement = true;
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002227 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002228
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002229protected:
Alexander Maid740b642014-05-20 22:04:42 +02002230 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002231
Lee Thomasonc18eb232014-02-21 17:31:17 -08002232 /** Prints out the space before an element. You may override to change
2233 the space and tabs used. A PrintSpace() override should call Print().
2234 */
2235 virtual void PrintSpace( int depth );
2236 void Print( const char* format, ... );
Alexander Golubevb2e08e42016-03-28 19:51:59 +03002237 void Write( const char* data, size_t size );
2238 inline void Write( const char* data ) { Write( data, strlen( data ) ); }
2239 void Putc( char ch );
Lee Thomasonc18eb232014-02-21 17:31:17 -08002240
Dmitry-Mea092bc12014-12-23 17:57:05 +03002241 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002242 bool _elementJustOpened;
2243 DynArray< const char*, 10 > _stack;
2244
2245private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002246 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002247
Lee Thomason624d43f2012-10-12 10:58:48 -07002248 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002249 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002250 int _depth;
2251 int _textDepth;
2252 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002253 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002254
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002255 enum {
2256 ENTITY_RANGE = 64,
2257 BUF_SIZE = 200
2258 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002259 bool _entityFlag[ENTITY_RANGE];
2260 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002261
Lee Thomason624d43f2012-10-12 10:58:48 -07002262 DynArray< char, 20 > _buffer;
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002263
2264 // Prohibit cloning, intentionally not implemented
2265 XMLPrinter( const XMLPrinter& );
2266 XMLPrinter& operator=( const XMLPrinter& );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002267};
2268
2269
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002270} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002271
PKEuS95060352013-07-26 10:42:44 +02002272#if defined(_MSC_VER)
2273# pragma warning(pop)
2274#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002275
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002276#endif // TINYXML2_INCLUDED