blob: 85cb497393d57001b02a7177d12da09e34f2631d [file] [log] [blame]
Lee Thomason (grinliz)28129862012-02-25 21:11:20 -08001/*
2Original code by Lee Thomason (www.grinninglizard.com)
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any
6damages arising from the use of this software.
7
8Permission is granted to anyone to use this software for any
9purpose, including commercial applications, and to alter it and
10redistribute it freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must
13not claim that you wrote the original software. If you use this
14software in a product, an acknowledgment in the product documentation
15would be appreciated but is not required.
16
172. Altered source versions must be plainly marked as such, and
18must not be misrepresented as being the original software.
19
203. This notice may not be removed or altered from any source
21distribution.
22*/
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -080023
Lee Thomason7d00b9a2012-02-27 17:54:22 -080024#ifndef TINYXML2_INCLUDED
U-Lama\Leee13c3e62011-12-28 14:36:55 -080025#define TINYXML2_INCLUDED
26
Anton Indrawanf59e2d62014-11-18 20:50:42 +010027#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070028# include <ctype.h>
29# include <limits.h>
30# include <stdio.h>
31# include <stdlib.h>
32# include <string.h>
Lee Thomasona572db12016-06-04 19:16:24 -070033# if defined(__PS3__)
34# include <stddef.h>
35# endif
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070036#else
Lee Thomasona9cf3f92012-10-11 16:56:51 -070037# include <cctype>
38# include <climits>
39# include <cstdio>
40# include <cstdlib>
41# include <cstring>
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -070042#endif
Lee Thomason1889c3e2016-06-04 20:22:57 -070043#include <stdint.h>
Lee Thomason (grinliz)6a22be22012-04-04 12:39:05 -070044
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -070045/*
Lee Thomason7d00b9a2012-02-27 17:54:22 -080046 TODO: intern strings instead of allocation.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080047*/
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080048/*
Lee Thomasona9cf3f92012-10-11 16:56:51 -070049 gcc:
Peter Matula50689912018-01-09 12:52:26 +010050 g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
MortenMacFly4ee49f12013-01-14 20:03:14 +010051
Lee Thomasona9cf3f92012-10-11 16:56:51 -070052 Formatting, Artistic Style:
53 AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -080054*/
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -080055
Lee Thomason7085f002017-06-01 18:09:43 -070056#if defined( _DEBUG ) || defined (__DEBUG__)
Peter Matula50689912018-01-09 12:52:26 +010057# ifndef TINYXML2_DEBUG
58# define TINYXML2_DEBUG
Lee Thomasona9cf3f92012-10-11 16:56:51 -070059# endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080060#endif
61
PKEuS95060352013-07-26 10:42:44 +020062#ifdef _MSC_VER
63# pragma warning(push)
64# pragma warning(disable: 4251)
65#endif
U-Lama\Lee4cee6112011-12-31 14:58:18 -080066
PKEuS16ed47d2013-07-06 12:02:43 +020067#ifdef _WIN32
68# ifdef TINYXML2_EXPORT
69# define TINYXML2_LIB __declspec(dllexport)
70# elif defined(TINYXML2_IMPORT)
71# define TINYXML2_LIB __declspec(dllimport)
72# else
73# define TINYXML2_LIB
74# endif
Matthew Woehlkea8e7ea72016-08-09 13:16:26 -040075#elif __GNUC__ >= 4
76# define TINYXML2_LIB __attribute__((visibility("default")))
PKEuS16ed47d2013-07-06 12:02:43 +020077#else
78# define TINYXML2_LIB
79#endif
80
81
Peter Matula50689912018-01-09 12:52:26 +010082#if defined(TINYXML2_DEBUG)
Lee Thomasona9cf3f92012-10-11 16:56:51 -070083# if defined(_MSC_VER)
Dmitry-Me4bcbf142014-12-25 19:05:18 +030084# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
Lee Thomason598a88d2015-10-09 14:42:12 -070085# define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
Lee Thomasona9cf3f92012-10-11 16:56:51 -070086# elif defined (ANDROID_NDK)
87# include <android/log.h>
88# define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
89# else
90# include <assert.h>
91# define TIXMLASSERT assert
92# endif
Lee Thomason598a88d2015-10-09 14:42:12 -070093#else
94# define TIXMLASSERT( x ) {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080095#endif
96
U-Lama\Leee13c3e62011-12-28 14:36:55 -080097
Lee Thomasonc18eb232014-02-21 17:31:17 -080098/* Versioning, past 1.0.14:
Lee Thomason85afe9c2014-02-23 21:42:16 -080099 http://semver.org/
Lee Thomasonc18eb232014-02-21 17:31:17 -0800100*/
Lee Thomason8c8293b2017-12-10 19:55:35 -0800101static const int TIXML2_MAJOR_VERSION = 6;
Lee Thomasonf26a5472017-12-29 09:30:39 -0800102static const int TIXML2_MINOR_VERSION = 1;
Lee Thomason8c8293b2017-12-10 19:55:35 -0800103static const int TIXML2_PATCH_VERSION = 0;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800104
Lee Thomasonf26a5472017-12-29 09:30:39 -0800105#define TINYXML2_MAJOR_VERSION 6
106#define TINYXML2_MINOR_VERSION 1
107#define TINYXML2_PATCH_VERSION 0
Lee Thomasonbd197872017-12-28 13:31:48 -0800108
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 );
Peter Matula50689912018-01-09 12:52:26 +0100398#ifdef TINYXML2_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 Thomason5b00e062017-12-28 14:07:47 -08001368 /// See QueryIntAttribute()
1369 XMLError QueryStringAttribute(const char* name, const char** value) const {
1370 const XMLAttribute* a = FindAttribute(name);
1371 if (!a) {
1372 return XML_NO_ATTRIBUTE;
1373 }
1374 *value = a->Value();
1375 return XML_SUCCESS;
1376 }
1377
1378
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001379
1380 /** Given an attribute name, QueryAttribute() returns
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001381 XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001382 can't be performed, or XML_NO_ATTRIBUTE if the attribute
1383 doesn't exist. It is overloaded for the primitive types,
1384 and is a generally more convenient replacement of
1385 QueryIntAttribute() and related functions.
1386
1387 If successful, the result of the conversion
1388 will be written to 'value'. If not successful, nothing will
1389 be written to 'value'. This allows you to provide default
1390 value:
1391
1392 @verbatim
1393 int value = 10;
1394 QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
1395 @endverbatim
1396 */
1397 int QueryAttribute( const char* name, int* value ) const {
1398 return QueryIntAttribute( name, value );
1399 }
1400
1401 int QueryAttribute( const char* name, unsigned int* value ) const {
1402 return QueryUnsignedAttribute( name, value );
1403 }
1404
Lee Thomason51c12712016-06-04 20:18:49 -07001405 int QueryAttribute(const char* name, int64_t* value) const {
1406 return QueryInt64Attribute(name, value);
1407 }
1408
Lee Thomason (grinliz)5efaa5f2013-02-01 19:26:30 -08001409 int QueryAttribute( const char* name, bool* value ) const {
1410 return QueryBoolAttribute( name, value );
1411 }
1412
1413 int QueryAttribute( const char* name, double* value ) const {
1414 return QueryDoubleAttribute( name, value );
1415 }
1416
1417 int QueryAttribute( const char* name, float* value ) const {
1418 return QueryFloatAttribute( name, value );
1419 }
1420
1421 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001422 void SetAttribute( const char* name, const char* value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001423 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001424 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001425 }
1426 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001427 void SetAttribute( const char* name, int value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001428 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001429 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001430 }
1431 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001432 void SetAttribute( const char* name, unsigned value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001433 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001434 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001435 }
Lee Thomason51c12712016-06-04 20:18:49 -07001436
1437 /// Sets the named attribute to value.
1438 void SetAttribute(const char* name, int64_t value) {
1439 XMLAttribute* a = FindOrCreateAttribute(name);
1440 a->SetAttribute(value);
1441 }
1442
1443 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001444 void SetAttribute( const char* name, bool value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001445 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001446 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001447 }
1448 /// Sets the named attribute to value.
Lee Thomason624d43f2012-10-12 10:58:48 -07001449 void SetAttribute( const char* name, double value ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001450 XMLAttribute* a = FindOrCreateAttribute( name );
Lee Thomason624d43f2012-10-12 10:58:48 -07001451 a->SetAttribute( value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001452 }
Lee Thomasonc3708cc2014-01-14 12:30:03 -08001453 /// Sets the named attribute to value.
1454 void SetAttribute( const char* name, float value ) {
1455 XMLAttribute* a = FindOrCreateAttribute( name );
1456 a->SetAttribute( value );
1457 }
Lee Thomason50f97b22012-02-11 16:33:40 -08001458
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001459 /**
1460 Delete an attribute.
1461 */
1462 void DeleteAttribute( const char* name );
Lee Thomason751da522012-02-10 08:50:51 -08001463
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001464 /// Return the first attribute in the list.
1465 const XMLAttribute* FirstAttribute() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001466 return _rootAttribute;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001467 }
1468 /// Query a specific attribute in the list.
1469 const XMLAttribute* FindAttribute( const char* name ) const;
Lee Thomason751da522012-02-10 08:50:51 -08001470
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001471 /** Convenience function for easy access to the text inside an element. Although easy
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001472 and concise, GetText() is limited compared to getting the XMLText child
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001473 and accessing it directly.
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001474
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001475 If the first child of 'this' is a XMLText, the GetText()
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001476 returns the character string of the Text node, else null is returned.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001477
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001478 This is a convenient method for getting the text of simple contained text:
1479 @verbatim
1480 <foo>This is text</foo>
1481 const char* str = fooElement->GetText();
1482 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001483
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001484 'str' will be a pointer to "This is text".
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001485
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001486 Note that this function can be misleading. If the element foo was created from
1487 this XML:
1488 @verbatim
1489 <foo><b>This is text</b></foo>
1490 @endverbatim
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001491
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001492 then the value of str would be null. The first child node isn't a text node, it is
1493 another element. From this XML:
1494 @verbatim
1495 <foo>This is <b>text</b></foo>
1496 @endverbatim
1497 GetText() will return "This is ".
1498 */
1499 const char* GetText() const;
Lee Thomason751da522012-02-10 08:50:51 -08001500
Uli Kusterer85fff5e2014-01-21 01:35:30 +01001501 /** Convenience function for easy access to the text inside an element. Although easy
1502 and concise, SetText() is limited compared to creating an XMLText child
1503 and mutating it directly.
1504
1505 If the first child of 'this' is a XMLText, SetText() sets its value to
1506 the given string, otherwise it will create a first child that is an XMLText.
1507
1508 This is a convenient method for setting the text of simple contained text:
1509 @verbatim
1510 <foo>This is text</foo>
1511 fooElement->SetText( "Hullaballoo!" );
1512 <foo>Hullaballoo!</foo>
1513 @endverbatim
1514
1515 Note that this function can be misleading. If the element foo was created from
1516 this XML:
1517 @verbatim
1518 <foo><b>This is text</b></foo>
1519 @endverbatim
1520
1521 then it will not change "This is text", but rather prefix it with a text element:
1522 @verbatim
1523 <foo>Hullaballoo!<b>This is text</b></foo>
1524 @endverbatim
1525
1526 For this XML:
1527 @verbatim
1528 <foo />
1529 @endverbatim
1530 SetText() will generate
1531 @verbatim
1532 <foo>Hullaballoo!</foo>
1533 @endverbatim
1534 */
Lee Thomason5bb2d802014-01-24 10:42:57 -08001535 void SetText( const char* inText );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001536 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001537 void SetText( int value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001538 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001539 void SetText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07001540 /// Convenience method for setting text inside an element. See SetText() for important limitations.
1541 void SetText(int64_t value);
1542 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001543 void SetText( bool value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001544 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001545 void SetText( double value );
Dmitry-Me9e9c85b2015-10-19 18:04:46 +03001546 /// Convenience method for setting text inside an element. See SetText() for important limitations.
Lee Thomason5bb2d802014-01-24 10:42:57 -08001547 void SetText( float value );
Uli Kusterer8fe342a2014-01-21 01:12:47 +01001548
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001549 /**
1550 Convenience method to query the value of a child text node. This is probably best
1551 shown by example. Given you have a document is this form:
1552 @verbatim
1553 <point>
1554 <x>1</x>
1555 <y>1.4</y>
1556 </point>
1557 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001558
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001559 The QueryIntText() and similar functions provide a safe and easier way to get to the
1560 "value" of x and y.
Lee Thomason21be8822012-07-15 17:27:22 -07001561
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001562 @verbatim
1563 int x = 0;
1564 float y = 0; // types of x and y are contrived for example
1565 const XMLElement* xElement = pointElement->FirstChildElement( "x" );
1566 const XMLElement* yElement = pointElement->FirstChildElement( "y" );
1567 xElement->QueryIntText( &x );
1568 yElement->QueryFloatText( &y );
1569 @endverbatim
Lee Thomason21be8822012-07-15 17:27:22 -07001570
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001571 @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
1572 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 -07001573
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001574 */
MortenMacFly4ee49f12013-01-14 20:03:14 +01001575 XMLError QueryIntText( int* ival ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001576 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001577 XMLError QueryUnsignedText( unsigned* uval ) const;
Lee Thomason51c12712016-06-04 20:18:49 -07001578 /// See QueryIntText()
1579 XMLError QueryInt64Text(int64_t* uval) const;
1580 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001581 XMLError QueryBoolText( bool* bval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001582 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001583 XMLError QueryDoubleText( double* dval ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001584 /// See QueryIntText()
MortenMacFly4ee49f12013-01-14 20:03:14 +01001585 XMLError QueryFloatText( float* fval ) const;
Lee Thomason21be8822012-07-15 17:27:22 -07001586
Josh Wittnercf3dd092016-10-11 18:57:17 -07001587 int IntText(int defaultValue = 0) const;
1588
Josh Wittner3a621f52016-09-12 19:17:54 -07001589 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001590 unsigned UnsignedText(unsigned defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001591 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001592 int64_t Int64Text(int64_t defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001593 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001594 bool BoolText(bool defaultValue = false) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001595 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001596 double DoubleText(double defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001597 /// See QueryIntText()
Josh Wittnercf3dd092016-10-11 18:57:17 -07001598 float FloatText(float defaultValue = 0) const;
Josh Wittner3a621f52016-09-12 19:17:54 -07001599
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001600 // internal:
Dmitry-Mee5035632017-04-05 18:02:40 +03001601 enum ElementClosingType {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001602 OPEN, // <foo>
1603 CLOSED, // <foo/>
1604 CLOSING // </foo>
1605 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001606 ElementClosingType ClosingType() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001607 return _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001608 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001609 virtual XMLNode* ShallowClone( XMLDocument* document ) const;
1610 virtual bool ShallowEqual( const XMLNode* compare ) const;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001611
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001612protected:
Dmitry-Me10b8ecc2017-04-12 17:57:44 +03001613 char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
Dmitry-Me9b0f1772015-04-03 10:37:31 +03001614
Lee Thomason50adb4c2012-02-13 15:07:09 -08001615private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001616 XMLElement( XMLDocument* doc );
1617 virtual ~XMLElement();
1618 XMLElement( const XMLElement& ); // not supported
1619 void operator=( const XMLElement& ); // not supported
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001620
Dmitry-Me1227d512014-12-05 13:41:45 +03001621 XMLAttribute* FindAttribute( const char* name ) {
1622 return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
1623 }
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001624 XMLAttribute* FindOrCreateAttribute( const char* name );
1625 //void LinkAttribute( XMLAttribute* attrib );
kezenator4f756162016-11-29 19:46:27 +10001626 char* ParseAttributes( char* p, int* curLineNumPtr );
Dmitry-Mee3225b12014-09-03 11:03:11 +04001627 static void DeleteAttribute( XMLAttribute* attribute );
Dmitry-Mea60caa22016-11-22 18:28:08 +03001628 XMLAttribute* CreateAttribute();
Lee Thomason67d61312012-01-24 16:01:51 -08001629
Lee Thomason5bb2d802014-01-24 10:42:57 -08001630 enum { BUF_SIZE = 200 };
Dmitry-Mee5035632017-04-05 18:02:40 +03001631 ElementClosingType _closingType;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001632 // The attribute list is ordered; there is no 'lastAttribute'
1633 // because the list needs to be scanned for dupes before adding
1634 // a new attribute.
Lee Thomason624d43f2012-10-12 10:58:48 -07001635 XMLAttribute* _rootAttribute;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001636};
1637
1638
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001639enum Whitespace {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001640 PRESERVE_WHITESPACE,
1641 COLLAPSE_WHITESPACE
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001642};
Lee Thomason (grinliz)bc1bfb72012-08-20 22:00:38 -07001643
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001644
1645/** A Document binds together all the functionality.
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -08001646 It can be saved, loaded, and printed to the screen.
1647 All Nodes are connected and allocated to a Document.
1648 If the Document is deleted, all its Nodes are also deleted.
1649*/
PKEuS16ed47d2013-07-06 12:02:43 +02001650class TINYXML2_LIB XMLDocument : public XMLNode
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001651{
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001652 friend class XMLElement;
Lee Thomasonf49b9652017-10-11 10:57:49 -07001653 // Gives access to SetError, but over-access for everything else.
1654 // Wishing C++ had "internal" scope.
1655 friend class XMLNode;
1656 friend class XMLText;
1657 friend class XMLComment;
1658 friend class XMLDeclaration;
1659 friend class XMLUnknown;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001660public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001661 /// constructor
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001662 XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001663 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001664
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001665 virtual XMLDocument* ToDocument() {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001666 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001667 return this;
1668 }
1669 virtual const XMLDocument* ToDocument() const {
Dmitry-Me66487eb2015-07-20 18:21:04 +03001670 TIXMLASSERT( this == _document );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001671 return this;
1672 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001673
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001674 /**
1675 Parse an XML file from a character string.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001676 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001677 an errorID.
Lee Thomason (grinliz)e2bcb322012-09-17 17:58:25 -07001678
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001679 You may optionally pass in the 'nBytes', which is
1680 the number of bytes which will be parsed. If not
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001681 specified, TinyXML-2 will assume 'xml' points to a
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001682 null terminated string.
1683 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001684 XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001685
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001686 /**
1687 Load an XML file from disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001688 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001689 an errorID.
1690 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001691 XMLError LoadFile( const char* filename );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001692
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001693 /**
1694 Load an XML file from disk. You are responsible
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001695 for providing and closing the FILE*.
1696
1697 NOTE: The file should be opened as binary ("rb")
1698 not text in order for TinyXML-2 to correctly
1699 do newline normalization.
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001700
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001701 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001702 an errorID.
1703 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001704 XMLError LoadFile( FILE* );
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001705
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001706 /**
1707 Save the XML file to disk.
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001708 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001709 an errorID.
1710 */
Lee Thomason2fa81722012-11-09 12:37:46 -08001711 XMLError SaveFile( const char* filename, bool compact = false );
Lee Thomasond11cd162012-04-12 08:35:36 -07001712
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001713 /**
1714 Save the XML file to disk. You are responsible
1715 for providing and closing the FILE*.
Ken Miller81da1fb2012-04-09 23:32:26 -05001716
Benjamin Doherty3b9cf992016-09-23 18:42:23 -06001717 Returns XML_SUCCESS (0) on success, or
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001718 an errorID.
1719 */
Jerome Martinez242c3ea2013-01-06 12:20:04 +01001720 XMLError SaveFile( FILE* fp, bool compact = false );
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001721
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001722 bool ProcessEntities() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001723 return _processEntities;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001724 }
1725 Whitespace WhitespaceMode() const {
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001726 return _whitespaceMode;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001727 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001728
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001729 /**
1730 Returns true if this document has a leading Byte Order Mark of UTF8.
1731 */
1732 bool HasBOM() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001733 return _writeBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001734 }
1735 /** Sets whether to write the BOM when writing the file.
1736 */
1737 void SetBOM( bool useBOM ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001738 _writeBOM = useBOM;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001739 }
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001740
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001741 /** Return the root element of DOM. Equivalent to FirstChildElement().
1742 To get the first node, use FirstChild().
1743 */
1744 XMLElement* RootElement() {
1745 return FirstChildElement();
1746 }
1747 const XMLElement* RootElement() const {
1748 return FirstChildElement();
1749 }
Lee Thomason18d68bd2012-01-26 18:17:26 -08001750
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001751 /** Print the Document. If the Printer is not provided, it will
1752 print to stdout. If you provide Printer, this can print to a file:
1753 @verbatim
1754 XMLPrinter printer( fp );
1755 doc.Print( &printer );
1756 @endverbatim
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001757
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001758 Or you can use a printer to print to memory:
1759 @verbatim
1760 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001761 doc.Print( &printer );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001762 // printer.CStr() has a const char* to the XML
1763 @endverbatim
1764 */
PKEuS1c5f99e2013-07-06 11:28:39 +02001765 void Print( XMLPrinter* streamer=0 ) const;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001766 virtual bool Accept( XMLVisitor* visitor ) const;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001767
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001768 /**
1769 Create a new Element associated with
1770 this Document. The memory for the Element
1771 is managed by the Document.
1772 */
1773 XMLElement* NewElement( const char* name );
1774 /**
1775 Create a new Comment associated with
1776 this Document. The memory for the Comment
1777 is managed by the Document.
1778 */
1779 XMLComment* NewComment( const char* comment );
1780 /**
1781 Create a new Text associated with
1782 this Document. The memory for the Text
1783 is managed by the Document.
1784 */
1785 XMLText* NewText( const char* text );
1786 /**
1787 Create a new Declaration associated with
1788 this Document. The memory for the object
1789 is managed by the Document.
Lee Thomasonf68c4382012-04-28 14:37:11 -07001790
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001791 If the 'text' param is null, the standard
1792 declaration is used.:
1793 @verbatim
1794 <?xml version="1.0" encoding="UTF-8"?>
1795 @endverbatim
1796 */
1797 XMLDeclaration* NewDeclaration( const char* text=0 );
1798 /**
1799 Create a new Unknown associated with
Andrew C. Martin0fd87462013-03-09 20:09:45 -07001800 this Document. The memory for the object
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001801 is managed by the Document.
1802 */
1803 XMLUnknown* NewUnknown( const char* text );
Lee Thomason2c85a712012-01-31 08:24:24 -08001804
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001805 /**
1806 Delete a node associated with this document.
1807 It will be unlinked from the DOM.
1808 */
Lee Thomasoncd011bc2014-12-17 10:41:34 -08001809 void DeleteNode( XMLNode* node );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001810
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001811 void ClearError() {
Lee Thomasonaa188392017-09-19 17:54:31 -07001812 SetError(XML_SUCCESS, 0, 0);
Dmitry-Me0d2cef02016-11-25 18:39:52 +03001813 }
1814
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001815 /// Return true if there was an error parsing the document.
1816 bool Error() const {
Lee Thomason85536252016-06-04 19:10:53 -07001817 return _errorID != XML_SUCCESS;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001818 }
1819 /// Return the errorID.
Lee Thomason2fa81722012-11-09 12:37:46 -08001820 XMLError ErrorID() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07001821 return _errorID;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001822 }
Lee Thomason331596e2014-09-11 14:56:43 -07001823 const char* ErrorName() const;
Lee Thomasone90e9012016-12-24 07:34:39 -08001824 static const char* ErrorIDToName(XMLError errorID);
Lee Thomason331596e2014-09-11 14:56:43 -07001825
Lee Thomasonf49b9652017-10-11 10:57:49 -07001826 /** Returns a "long form" error description. A hopefully helpful
1827 diagnostic with location, line number, and/or additional info.
1828 */
1829 const char* ErrorStr() const;
1830
1831 /// A (trivial) utility function that prints the ErrorStr() to stdout.
1832 void PrintError() const;
Lee Thomason8c9e3132017-06-26 16:55:01 -07001833
kezenatorec694152016-11-26 17:21:43 +10001834 /// Return the line where the error occured, or zero if unknown.
Lee Thomasonf49b9652017-10-11 10:57:49 -07001835 int ErrorLineNum() const
kezenatorec694152016-11-26 17:21:43 +10001836 {
1837 return _errorLineNum;
1838 }
Martinsh Shaitersa9d42b02013-01-30 11:14:27 +02001839
1840 /// Clear the document, resetting it to the initial state.
1841 void Clear();
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001842
Lee Thomason7085f002017-06-01 18:09:43 -07001843 /**
Lee Thomasonb29f5562017-06-01 18:45:32 -07001844 Copies this document to a target document.
Lee Thomason7085f002017-06-01 18:09:43 -07001845 The target will be completely cleared before the copy.
Lee Thomasonb29f5562017-06-01 18:45:32 -07001846 If you want to copy a sub-tree, see XMLNode::DeepClone().
Lee Thomason7085f002017-06-01 18:09:43 -07001847
Lee Thomason1346a172017-06-14 15:14:19 -07001848 NOTE: that the 'target' must be non-null.
Lee Thomason7085f002017-06-01 18:09:43 -07001849 */
Shuichiro Suzuki87cd4e02017-08-24 11:20:26 +09001850 void DeepCopy(XMLDocument* target) const;
Lee Thomason7085f002017-06-01 18:09:43 -07001851
1852 // internal
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001853 char* Identify( char* p, XMLNode** node );
Lee Thomason2c85a712012-01-31 08:24:24 -08001854
Lee Thomason816d3fa2017-06-05 14:35:55 -07001855 // internal
1856 void MarkInUse(XMLNode*);
1857
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001858 virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
1859 return 0;
1860 }
1861 virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
1862 return false;
1863 }
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001864
Lee Thomason3f57d272012-01-11 15:30:03 -08001865private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001866 XMLDocument( const XMLDocument& ); // not supported
1867 void operator=( const XMLDocument& ); // not supported
Lee Thomason18d68bd2012-01-26 18:17:26 -08001868
Lee Thomasone90e9012016-12-24 07:34:39 -08001869 bool _writeBOM;
1870 bool _processEntities;
1871 XMLError _errorID;
Dmitry-Meae8a82a2017-03-03 15:40:32 +03001872 Whitespace _whitespaceMode;
Lee Thomasonaa188392017-09-19 17:54:31 -07001873 mutable StrPair _errorStr;
Lee Thomasone90e9012016-12-24 07:34:39 -08001874 int _errorLineNum;
1875 char* _charBuffer;
1876 int _parseCurLineNum;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001877 // Memory tracking does add some overhead.
1878 // However, the code assumes that you don't
1879 // have a bunch of unlinked nodes around.
1880 // Therefore it takes less memory to track
1881 // in the document vs. a linked list in the XMLNode,
1882 // and the performance is the same.
1883 DynArray<XMLNode*, 10> _unlinked;
Lee Thomasond1983222012-02-06 08:41:24 -08001884
Lee Thomason624d43f2012-10-12 10:58:48 -07001885 MemPoolT< sizeof(XMLElement) > _elementPool;
1886 MemPoolT< sizeof(XMLAttribute) > _attributePool;
1887 MemPoolT< sizeof(XMLText) > _textPool;
1888 MemPoolT< sizeof(XMLComment) > _commentPool;
Lee Thomason331596e2014-09-11 14:56:43 -07001889
1890 static const char* _errorNames[XML_ERROR_COUNT];
Dmitry-Me97476b72015-01-01 16:15:57 +03001891
1892 void Parse();
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001893
Lee Thomasonf49b9652017-10-11 10:57:49 -07001894 void SetError( XMLError error, int lineNum, const char* format, ... );
1895
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001896 template<class NodeType, int PoolElementSize>
1897 NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
Lee Thomason5cae8972012-01-24 18:03:07 -08001898};
1899
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001900template<class NodeType, int PoolElementSize>
1901inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
1902{
1903 TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
1904 TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
1905 NodeType* returnNode = new (pool.Alloc()) NodeType( this );
1906 TIXMLASSERT( returnNode );
1907 returnNode->_memPool = &pool;
Lee Thomason816d3fa2017-06-05 14:35:55 -07001908
1909 _unlinked.Push(returnNode);
Dmitry-Me2aebfb72017-02-27 15:53:40 +03001910 return returnNode;
1911}
Lee Thomason7c913cd2012-01-26 18:32:34 -08001912
Lee Thomason3ffdd392012-03-28 17:27:55 -07001913/**
1914 A XMLHandle is a class that wraps a node pointer with null checks; this is
Vasily Biryukov9a975b72013-05-11 21:41:42 +06001915 an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
Lee Thomason3ffdd392012-03-28 17:27:55 -07001916 DOM structure. It is a separate utility class.
1917
1918 Take an example:
1919 @verbatim
1920 <Document>
1921 <Element attributeA = "valueA">
1922 <Child attributeB = "value1" />
1923 <Child attributeB = "value2" />
1924 </Element>
Thomas Roß08bdf502012-05-12 14:21:23 +02001925 </Document>
Lee Thomason3ffdd392012-03-28 17:27:55 -07001926 @endverbatim
1927
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001928 Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
Lee Thomason3ffdd392012-03-28 17:27:55 -07001929 easy to write a *lot* of code that looks like:
1930
1931 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001932 XMLElement* root = document.FirstChildElement( "Document" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001933 if ( root )
1934 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001935 XMLElement* element = root->FirstChildElement( "Element" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001936 if ( element )
1937 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001938 XMLElement* child = element->FirstChildElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001939 if ( child )
1940 {
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001941 XMLElement* child2 = child->NextSiblingElement( "Child" );
Lee Thomason3ffdd392012-03-28 17:27:55 -07001942 if ( child2 )
1943 {
1944 // Finally do something useful.
1945 @endverbatim
1946
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001947 And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07001948 of such code. A XMLHandle checks for null pointers so it is perfectly safe
Lee Thomason3ffdd392012-03-28 17:27:55 -07001949 and correct to use:
1950
1951 @verbatim
Lee Thomasondb0bbb62012-04-04 15:47:04 -07001952 XMLHandle docHandle( &document );
Dmitry-Mea317bd62014-12-08 10:35:37 +03001953 XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
Lee Thomason3ffdd392012-03-28 17:27:55 -07001954 if ( child2 )
1955 {
1956 // do something useful
1957 @endverbatim
1958
1959 Which is MUCH more concise and useful.
1960
1961 It is also safe to copy handles - internally they are nothing more than node pointers.
1962 @verbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001963 XMLHandle handleCopy = handle;
Lee Thomason3ffdd392012-03-28 17:27:55 -07001964 @endverbatim
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07001965
1966 See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
Lee Thomason3ffdd392012-03-28 17:27:55 -07001967*/
PKEuS16ed47d2013-07-06 12:02:43 +02001968class TINYXML2_LIB XMLHandle
Lee Thomason3ffdd392012-03-28 17:27:55 -07001969{
1970public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001971 /// 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 +02001972 XMLHandle( XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001973 }
1974 /// Create a handle from a node.
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001975 XMLHandle( XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001976 }
1977 /// Copy constructor
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02001978 XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001979 }
1980 /// Assignment
1981 XMLHandle& operator=( const XMLHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07001982 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001983 return *this;
1984 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07001985
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001986 /// Get the first child of this handle.
1987 XMLHandle FirstChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001988 return XMLHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001989 }
1990 /// Get the first child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001991 XMLHandle FirstChildElement( const char* name = 0 ) {
1992 return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001993 }
1994 /// Get the last child of this handle.
1995 XMLHandle LastChild() {
Lee Thomason624d43f2012-10-12 10:58:48 -07001996 return XMLHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07001997 }
1998 /// Get the last child element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03001999 XMLHandle LastChildElement( const char* name = 0 ) {
2000 return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002001 }
2002 /// Get the previous sibling of this handle.
2003 XMLHandle PreviousSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002004 return XMLHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002005 }
2006 /// Get the previous sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002007 XMLHandle PreviousSiblingElement( const char* name = 0 ) {
2008 return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002009 }
2010 /// Get the next sibling of this handle.
2011 XMLHandle NextSibling() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002012 return XMLHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002013 }
2014 /// Get the next sibling element of this handle.
Dmitry-Me886ad972015-07-22 11:00:51 +03002015 XMLHandle NextSiblingElement( const char* name = 0 ) {
2016 return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002017 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002018
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002019 /// Safe cast to XMLNode. This can return null.
2020 XMLNode* ToNode() {
Lee Thomason624d43f2012-10-12 10:58:48 -07002021 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002022 }
2023 /// Safe cast to XMLElement. This can return null.
2024 XMLElement* ToElement() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002025 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002026 }
2027 /// Safe cast to XMLText. This can return null.
2028 XMLText* ToText() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002029 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002030 }
2031 /// Safe cast to XMLUnknown. This can return null.
2032 XMLUnknown* ToUnknown() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002033 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002034 }
2035 /// Safe cast to XMLDeclaration. This can return null.
2036 XMLDeclaration* ToDeclaration() {
Dmitry-Meebb16602016-11-16 17:22:45 +03002037 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002038 }
Lee Thomason3ffdd392012-03-28 17:27:55 -07002039
2040private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002041 XMLNode* _node;
Lee Thomason3ffdd392012-03-28 17:27:55 -07002042};
2043
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08002044
2045/**
Lee Thomason (grinliz)ae209f62012-04-04 22:00:07 -07002046 A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
2047 same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
Lee Thomason8b899812012-04-04 15:58:16 -07002048*/
PKEuS16ed47d2013-07-06 12:02:43 +02002049class TINYXML2_LIB XMLConstHandle
Lee Thomason8b899812012-04-04 15:58:16 -07002050{
2051public:
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002052 XMLConstHandle( const XMLNode* node ) : _node( node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002053 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002054 XMLConstHandle( const XMLNode& node ) : _node( &node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002055 }
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002056 XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002057 }
Lee Thomason8b899812012-04-04 15:58:16 -07002058
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002059 XMLConstHandle& operator=( const XMLConstHandle& ref ) {
Lee Thomason624d43f2012-10-12 10:58:48 -07002060 _node = ref._node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002061 return *this;
2062 }
Lee Thomason8b899812012-04-04 15:58:16 -07002063
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002064 const XMLConstHandle FirstChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002065 return XMLConstHandle( _node ? _node->FirstChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002066 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002067 const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
2068 return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002069 }
2070 const XMLConstHandle LastChild() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002071 return XMLConstHandle( _node ? _node->LastChild() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002072 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002073 const XMLConstHandle LastChildElement( const char* name = 0 ) const {
2074 return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002075 }
2076 const XMLConstHandle PreviousSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002077 return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002078 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002079 const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
2080 return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002081 }
2082 const XMLConstHandle NextSibling() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002083 return XMLConstHandle( _node ? _node->NextSibling() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002084 }
Dmitry-Me886ad972015-07-22 11:00:51 +03002085 const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
2086 return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002087 }
Lee Thomason8b899812012-04-04 15:58:16 -07002088
2089
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002090 const XMLNode* ToNode() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002091 return _node;
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002092 }
2093 const XMLElement* ToElement() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002094 return ( _node ? _node->ToElement() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002095 }
2096 const XMLText* ToText() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002097 return ( _node ? _node->ToText() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002098 }
2099 const XMLUnknown* ToUnknown() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002100 return ( _node ? _node->ToUnknown() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002101 }
2102 const XMLDeclaration* ToDeclaration() const {
Dmitry-Meebb16602016-11-16 17:22:45 +03002103 return ( _node ? _node->ToDeclaration() : 0 );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002104 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08002105
Lee Thomason5cae8972012-01-24 18:03:07 -08002106private:
Lee Thomason624d43f2012-10-12 10:58:48 -07002107 const XMLNode* _node;
Lee Thomason56bdd022012-02-09 18:16:58 -08002108};
Lee Thomason6f381b72012-03-02 12:59:39 -08002109
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002110
2111/**
2112 Printing functionality. The XMLPrinter gives you more
2113 options than the XMLDocument::Print() method.
2114
2115 It can:
2116 -# Print to memory.
Thomas Roß08bdf502012-05-12 14:21:23 +02002117 -# Print to a file you provide.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002118 -# Print XML without a XMLDocument.
2119
2120 Print to Memory
2121
2122 @verbatim
2123 XMLPrinter printer;
Vasily Biryukov9a975b72013-05-11 21:41:42 +06002124 doc.Print( &printer );
Thomas Roß08bdf502012-05-12 14:21:23 +02002125 SomeFunction( printer.CStr() );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002126 @endverbatim
2127
2128 Print to a File
Lee Thomason (grinliz)2f1f6242012-09-16 11:32:34 -07002129
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002130 You provide the file pointer.
2131 @verbatim
2132 XMLPrinter printer( fp );
2133 doc.Print( &printer );
2134 @endverbatim
2135
2136 Print without a XMLDocument
2137
2138 When loading, an XML parser is very useful. However, sometimes
2139 when saving, it just gets in the way. The code is often set up
2140 for streaming, and constructing the DOM is just overhead.
2141
2142 The Printer supports the streaming case. The following code
2143 prints out a trivially simple XML file without ever creating
2144 an XML document.
2145
2146 @verbatim
2147 XMLPrinter printer( fp );
2148 printer.OpenElement( "foo" );
2149 printer.PushAttribute( "foo", "bar" );
2150 printer.CloseElement();
2151 @endverbatim
2152*/
PKEuS16ed47d2013-07-06 12:02:43 +02002153class TINYXML2_LIB XMLPrinter : public XMLVisitor
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002154{
2155public:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002156 /** Construct the printer. If the FILE* is specified,
2157 this will print to the FILE. Else it will print
2158 to memory, and the result is available in CStr().
2159 If 'compact' is set to true, then output is created
2160 with only required whitespace and newlines.
2161 */
PKEuS1bfb9542013-08-04 13:51:17 +02002162 XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002163 virtual ~XMLPrinter() {}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002164
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002165 /** If streaming, write the BOM and declaration. */
2166 void PushHeader( bool writeBOM, bool writeDeclaration );
2167 /** If streaming, start writing an element.
2168 The element must be closed with CloseElement()
2169 */
Lee Thomason256adb62014-04-06 14:41:46 -07002170 void OpenElement( const char* name, bool compactMode=false );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002171 /// If streaming, add an attribute to an open element.
2172 void PushAttribute( const char* name, const char* value );
2173 void PushAttribute( const char* name, int value );
2174 void PushAttribute( const char* name, unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002175 void PushAttribute(const char* name, int64_t value);
2176 void PushAttribute( const char* name, bool value );
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002177 void PushAttribute( const char* name, double value );
2178 /// If streaming, close the Element.
Lee Thomason256adb62014-04-06 14:41:46 -07002179 virtual void CloseElement( bool compactMode=false );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002180
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002181 /// Add a text node.
2182 void PushText( const char* text, bool cdata=false );
2183 /// Add a text node from an integer.
2184 void PushText( int value );
2185 /// Add a text node from an unsigned.
2186 void PushText( unsigned value );
Lee Thomason51c12712016-06-04 20:18:49 -07002187 /// Add a text node from an unsigned.
2188 void PushText(int64_t value);
2189 /// Add a text node from a bool.
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002190 void PushText( bool value );
2191 /// Add a text node from a float.
2192 void PushText( float value );
2193 /// Add a text node from a double.
2194 void PushText( double value );
Lee Thomason21be8822012-07-15 17:27:22 -07002195
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002196 /// Add a comment
2197 void PushComment( const char* comment );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002198
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002199 void PushDeclaration( const char* value );
2200 void PushUnknown( const char* value );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002201
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002202 virtual bool VisitEnter( const XMLDocument& /*doc*/ );
2203 virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
2204 return true;
2205 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002206
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002207 virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
2208 virtual bool VisitExit( const XMLElement& element );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002209
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002210 virtual bool Visit( const XMLText& text );
2211 virtual bool Visit( const XMLComment& comment );
2212 virtual bool Visit( const XMLDeclaration& declaration );
2213 virtual bool Visit( const XMLUnknown& unknown );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002214
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002215 /**
2216 If in print to memory mode, return a pointer to
2217 the XML file in memory.
2218 */
2219 const char* CStr() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002220 return _buffer.Mem();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002221 }
2222 /**
2223 If in print to memory mode, return the size
2224 of the XML file in memory. (Note the size returned
2225 includes the terminating null.)
2226 */
2227 int CStrSize() const {
Lee Thomason624d43f2012-10-12 10:58:48 -07002228 return _buffer.Size();
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002229 }
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002230 /**
2231 If in print to memory mode, reset the buffer to the
2232 beginning.
2233 */
Lee Thomasonce0510b2013-11-26 21:29:37 -08002234 void ClearBuffer() {
2235 _buffer.Clear();
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002236 _buffer.Push(0);
Lee Thomason53858b42017-06-01 19:09:16 -07002237 _firstElement = true;
Reinhard Klambauer3bc3d4e2013-11-22 14:05:21 +01002238 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002239
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002240protected:
Alexander Maid740b642014-05-20 22:04:42 +02002241 virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
Uli Kusterer5d1d27e2014-02-20 11:50:22 +01002242
Lee Thomasonc18eb232014-02-21 17:31:17 -08002243 /** Prints out the space before an element. You may override to change
2244 the space and tabs used. A PrintSpace() override should call Print().
2245 */
2246 virtual void PrintSpace( int depth );
2247 void Print( const char* format, ... );
Alexander Golubevb2e08e42016-03-28 19:51:59 +03002248 void Write( const char* data, size_t size );
2249 inline void Write( const char* data ) { Write( data, strlen( data ) ); }
2250 void Putc( char ch );
Lee Thomasonc18eb232014-02-21 17:31:17 -08002251
Dmitry-Mea092bc12014-12-23 17:57:05 +03002252 void SealElementIfJustOpened();
Dennis Jenkins59c75d32013-10-08 13:10:07 -05002253 bool _elementJustOpened;
2254 DynArray< const char*, 10 > _stack;
2255
2256private:
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002257 void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002258
Lee Thomason624d43f2012-10-12 10:58:48 -07002259 bool _firstElement;
Jerome Martinez242c3ea2013-01-06 12:20:04 +01002260 FILE* _fp;
Lee Thomason624d43f2012-10-12 10:58:48 -07002261 int _depth;
2262 int _textDepth;
2263 bool _processEntities;
Lee Thomasonc18eb232014-02-21 17:31:17 -08002264 bool _compactMode;
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002265
Lee Thomasona9cf3f92012-10-11 16:56:51 -07002266 enum {
2267 ENTITY_RANGE = 64,
2268 BUF_SIZE = 200
2269 };
Lee Thomason624d43f2012-10-12 10:58:48 -07002270 bool _entityFlag[ENTITY_RANGE];
2271 bool _restrictedEntityFlag[ENTITY_RANGE];
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002272
Lee Thomason624d43f2012-10-12 10:58:48 -07002273 DynArray< char, 20 > _buffer;
Thierry Lelegard7f0f7542017-09-01 10:14:16 +02002274
2275 // Prohibit cloning, intentionally not implemented
2276 XMLPrinter( const XMLPrinter& );
2277 XMLPrinter& operator=( const XMLPrinter& );
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002278};
2279
2280
Guillermo A. Amaralb42ba362012-03-20 00:15:30 -07002281} // tinyxml2
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002282
PKEuS95060352013-07-26 10:42:44 +02002283#if defined(_MSC_VER)
2284# pragma warning(pop)
2285#endif
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002286
U-Lama\Leee13c3e62011-12-28 14:36:55 -08002287#endif // TINYXML2_INCLUDED