Lee Thomason (grinliz) | 2812986 | 2012-02-25 21:11:20 -0800 | [diff] [blame] | 1 | /*
|
| 2 | Original code by Lee Thomason (www.grinninglizard.com)
|
| 3 |
|
| 4 | This software is provided 'as-is', without any express or implied
|
| 5 | warranty. In no event will the authors be held liable for any
|
| 6 | damages arising from the use of this software.
|
| 7 |
|
| 8 | Permission is granted to anyone to use this software for any
|
| 9 | purpose, including commercial applications, and to alter it and
|
| 10 | redistribute it freely, subject to the following restrictions:
|
| 11 |
|
| 12 | 1. The origin of this software must not be misrepresented; you must
|
| 13 | not claim that you wrote the original software. If you use this
|
| 14 | software in a product, an acknowledgment in the product documentation
|
| 15 | would be appreciated but is not required.
|
| 16 |
|
| 17 | 2. Altered source versions must be plainly marked as such, and
|
| 18 | must not be misrepresented as being the original software.
|
| 19 |
|
| 20 | 3. This notice may not be removed or altered from any source
|
| 21 | distribution.
|
| 22 | */
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 23 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 24 | #ifndef TINYXML2_INCLUDED
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 25 | #define TINYXML2_INCLUDED
|
| 26 |
|
Anton Indrawan | f59e2d6 | 2014-11-18 20:50:42 +0100 | [diff] [blame] | 27 | #if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 28 | # include <ctype.h>
|
| 29 | # include <limits.h>
|
| 30 | # include <stdio.h>
|
| 31 | # include <stdlib.h>
|
| 32 | # include <string.h>
|
Lee Thomason | a572db1 | 2016-06-04 19:16:24 -0700 | [diff] [blame] | 33 | # if defined(__PS3__)
|
| 34 | # include <stddef.h>
|
| 35 | # endif
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 36 | #else
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 37 | # include <cctype>
|
| 38 | # include <climits>
|
| 39 | # include <cstdio>
|
| 40 | # include <cstdlib>
|
| 41 | # include <cstring>
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 42 | #endif
|
Lee Thomason | 1889c3e | 2016-06-04 20:22:57 -0700 | [diff] [blame] | 43 | #include <stdint.h>
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 44 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 45 | /*
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 46 | TODO: intern strings instead of allocation.
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 47 | */
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 48 | /*
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 49 | gcc:
|
Peter Matula | 5068991 | 2018-01-09 12:52:26 +0100 | [diff] [blame] | 50 | g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 51 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 52 | Formatting, Artistic Style:
|
| 53 | AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 54 | */
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 55 |
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 56 | #if defined( _DEBUG ) || defined (__DEBUG__)
|
Peter Matula | 5068991 | 2018-01-09 12:52:26 +0100 | [diff] [blame] | 57 | # ifndef TINYXML2_DEBUG
|
| 58 | # define TINYXML2_DEBUG
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 59 | # endif
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 60 | #endif
|
| 61 |
|
PKEuS | 9506035 | 2013-07-26 10:42:44 +0200 | [diff] [blame] | 62 | #ifdef _MSC_VER
|
| 63 | # pragma warning(push)
|
| 64 | # pragma warning(disable: 4251)
|
| 65 | #endif
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 66 |
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 67 | #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 Woehlke | a8e7ea7 | 2016-08-09 13:16:26 -0400 | [diff] [blame] | 75 | #elif __GNUC__ >= 4
|
| 76 | # define TINYXML2_LIB __attribute__((visibility("default")))
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 77 | #else
|
| 78 | # define TINYXML2_LIB
|
| 79 | #endif
|
| 80 |
|
| 81 |
|
Peter Matula | 5068991 | 2018-01-09 12:52:26 +0100 | [diff] [blame] | 82 | #if defined(TINYXML2_DEBUG)
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 83 | # if defined(_MSC_VER)
|
Dmitry-Me | 4bcbf14 | 2014-12-25 19:05:18 +0300 | [diff] [blame] | 84 | # // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
|
Lee Thomason | 598a88d | 2015-10-09 14:42:12 -0700 | [diff] [blame] | 85 | # define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 86 | # 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 Thomason | 598a88d | 2015-10-09 14:42:12 -0700 | [diff] [blame] | 93 | #else
|
| 94 | # define TIXMLASSERT( x ) {}
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 95 | #endif
|
| 96 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 97 |
|
Lee Thomason | c18eb23 | 2014-02-21 17:31:17 -0800 | [diff] [blame] | 98 | /* Versioning, past 1.0.14:
|
Lee Thomason | 85afe9c | 2014-02-23 21:42:16 -0800 | [diff] [blame] | 99 | http://semver.org/
|
Lee Thomason | c18eb23 | 2014-02-21 17:31:17 -0800 | [diff] [blame] | 100 | */
|
Lee Thomason | 8c8293b | 2017-12-10 19:55:35 -0800 | [diff] [blame] | 101 | static const int TIXML2_MAJOR_VERSION = 6;
|
Lee Thomason | c1424ee | 2018-04-05 23:09:23 -0700 | [diff] [blame] | 102 | static const int TIXML2_MINOR_VERSION = 2;
|
Lee Thomason | 8c8293b | 2017-12-10 19:55:35 -0800 | [diff] [blame] | 103 | static const int TIXML2_PATCH_VERSION = 0;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 104 |
|
Lee Thomason | f26a547 | 2017-12-29 09:30:39 -0800 | [diff] [blame] | 105 | #define TINYXML2_MAJOR_VERSION 6
|
Lee Thomason | c1424ee | 2018-04-05 23:09:23 -0700 | [diff] [blame] | 106 | #define TINYXML2_MINOR_VERSION 2
|
Lee Thomason | f26a547 | 2017-12-29 09:30:39 -0800 | [diff] [blame] | 107 | #define TINYXML2_PATCH_VERSION 0
|
Lee Thomason | bd19787 | 2017-12-28 13:31:48 -0800 | [diff] [blame] | 108 |
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 109 | // A fixed element depth limit is problematic. There needs to be a
|
| 110 | // limit to avoid a stack overflow. However, that limit varies per
|
| 111 | // system, and the capacity of the stack. On the other hand, it's a trivial
|
| 112 | // attack that can result from ill, malicious, or even correctly formed XML,
|
Lee Thomason | f928c35 | 2018-04-05 09:24:20 -0700 | [diff] [blame] | 113 | // so there needs to be a limit in place.
|
| 114 | static const int TINYXML2_MAX_ELEMENT_DEPTH = 100;
|
Lee Thomason | d946dda | 2018-04-05 09:11:08 -0700 | [diff] [blame] | 115 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 116 | namespace tinyxml2
|
| 117 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 118 | class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 119 | class XMLElement;
|
| 120 | class XMLAttribute;
|
| 121 | class XMLComment;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 122 | class XMLText;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 123 | class XMLDeclaration;
|
| 124 | class XMLUnknown;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 125 | class XMLPrinter;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 126 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 127 | /*
|
| 128 | A class that wraps strings. Normally stores the start and end
|
| 129 | pointers into the XML file itself, and will apply normalization
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 130 | and entity translation if actually read. Can also store (and memory
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 131 | manage) a traditional char[]
|
| 132 | */
|
PKEuS | 9506035 | 2013-07-26 10:42:44 +0200 | [diff] [blame] | 133 | class StrPair
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 134 | {
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 135 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 136 | enum {
|
| 137 | NEEDS_ENTITY_PROCESSING = 0x01,
|
| 138 | NEEDS_NEWLINE_NORMALIZATION = 0x02,
|
Dmitry-Me | 5420e54 | 2015-05-20 10:51:26 +0300 | [diff] [blame] | 139 | NEEDS_WHITESPACE_COLLAPSING = 0x04,
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 140 |
|
Lee Thomason | 584af57 | 2016-09-05 14:14:16 -0700 | [diff] [blame] | 141 | TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 142 | TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 584af57 | 2016-09-05 14:14:16 -0700 | [diff] [blame] | 143 | ATTRIBUTE_NAME = 0,
|
| 144 | ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
| 145 | ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
| 146 | COMMENT = NEEDS_NEWLINE_NORMALIZATION
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 147 | };
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 148 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 149 | StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 150 | ~StrPair();
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 151 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 152 | void Set( char* start, char* end, int flags ) {
|
Dmitry-Me | 6fc38ec | 2016-09-01 17:59:44 +0300 | [diff] [blame] | 153 | TIXMLASSERT( start );
|
| 154 | TIXMLASSERT( end );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 155 | Reset();
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 156 | _start = start;
|
| 157 | _end = end;
|
| 158 | _flags = flags | NEEDS_FLUSH;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 159 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 160 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 161 | const char* GetStr();
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 162 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 163 | bool Empty() const {
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 164 | return _start == _end;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 165 | }
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 166 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 167 | void SetInternedStr( const char* str ) {
|
| 168 | Reset();
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 169 | _start = const_cast<char*>(str);
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 170 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 171 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 172 | void SetStr( const char* str, int flags=0 );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 173 |
|
kezenator | 4f75616 | 2016-11-29 19:46:27 +1000 | [diff] [blame] | 174 | char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 175 | char* ParseName( char* in );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 176 |
|
Lee Thomason | 2965880 | 2014-11-27 22:31:11 -0800 | [diff] [blame] | 177 | void TransferTo( StrPair* other );
|
Lee Thomason | 584af57 | 2016-09-05 14:14:16 -0700 | [diff] [blame] | 178 | void Reset();
|
Dmitry-Me | 08b40dd | 2014-11-10 11:17:21 +0300 | [diff] [blame] | 179 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 180 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 181 | void CollapseWhitespace();
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 182 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 183 | enum {
|
| 184 | NEEDS_FLUSH = 0x100,
|
| 185 | NEEDS_DELETE = 0x200
|
| 186 | };
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 187 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 188 | int _flags;
|
| 189 | char* _start;
|
| 190 | char* _end;
|
Dmitry-Me | 08b40dd | 2014-11-10 11:17:21 +0300 | [diff] [blame] | 191 |
|
| 192 | StrPair( const StrPair& other ); // not supported
|
| 193 | void operator=( StrPair& other ); // not supported, use TransferTo()
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 194 | };
|
| 195 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 196 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 197 | /*
|
| 198 | A dynamic array of Plain Old Data. Doesn't support constructors, etc.
|
| 199 | Has a small initial memory pool, so that low or no usage will not
|
| 200 | cause a call to new/delete
|
| 201 | */
|
Dmitry-Me | 0400922 | 2015-04-06 18:07:18 +0300 | [diff] [blame] | 202 | template <class T, int INITIAL_SIZE>
|
PKEuS | 9506035 | 2013-07-26 10:42:44 +0200 | [diff] [blame] | 203 | class DynArray
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 204 | {
|
| 205 | public:
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 206 | DynArray() :
|
| 207 | _mem( _pool ),
|
| 208 | _allocated( INITIAL_SIZE ),
|
| 209 | _size( 0 )
|
| 210 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 211 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 212 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 213 | ~DynArray() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 214 | if ( _mem != _pool ) {
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 215 | delete [] _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 216 | }
|
| 217 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 218 |
|
Lee Thomason | ce0510b | 2013-11-26 21:29:37 -0800 | [diff] [blame] | 219 | void Clear() {
|
Reinhard Klambauer | 4e74b13 | 2013-11-22 14:01:58 +0100 | [diff] [blame] | 220 | _size = 0;
|
| 221 | }
|
| 222 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 223 | void Push( T t ) {
|
Dmitry-Me | ed7a7dc | 2015-01-14 08:36:12 +0300 | [diff] [blame] | 224 | TIXMLASSERT( _size < INT_MAX );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 225 | EnsureCapacity( _size+1 );
|
Dmitry-Me | fed5112 | 2016-09-06 18:08:55 +0300 | [diff] [blame] | 226 | _mem[_size] = t;
|
| 227 | ++_size;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 228 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 229 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 230 | T* PushArr( int count ) {
|
Dmitry-Me | 74fb870 | 2015-01-13 10:27:36 +0300 | [diff] [blame] | 231 | TIXMLASSERT( count >= 0 );
|
Dmitry-Me | ed7a7dc | 2015-01-14 08:36:12 +0300 | [diff] [blame] | 232 | TIXMLASSERT( _size <= INT_MAX - count );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 233 | EnsureCapacity( _size+count );
|
| 234 | T* ret = &_mem[_size];
|
| 235 | _size += count;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 236 | return ret;
|
| 237 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 238 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 239 | T Pop() {
|
Dmitry-Me | 74fb870 | 2015-01-13 10:27:36 +0300 | [diff] [blame] | 240 | TIXMLASSERT( _size > 0 );
|
Dmitry-Me | fed5112 | 2016-09-06 18:08:55 +0300 | [diff] [blame] | 241 | --_size;
|
| 242 | return _mem[_size];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 243 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 244 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 245 | void PopArr( int count ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 246 | TIXMLASSERT( _size >= count );
|
| 247 | _size -= count;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 248 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 249 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 250 | bool Empty() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 251 | return _size == 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 252 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 253 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 254 | T& operator[](int i) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 255 | TIXMLASSERT( i>= 0 && i < _size );
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 256 | return _mem[i];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 257 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 258 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 259 | const T& operator[](int i) const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 260 | TIXMLASSERT( i>= 0 && i < _size );
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 261 | return _mem[i];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 262 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 263 |
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 264 | const T& PeekTop() const {
|
Dennis Jenkins | 59c75d3 | 2013-10-08 13:10:07 -0500 | [diff] [blame] | 265 | TIXMLASSERT( _size > 0 );
|
| 266 | return _mem[ _size - 1];
|
| 267 | }
|
| 268 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 269 | int Size() const {
|
Dmitry-Me | 30bdc97 | 2015-01-14 08:32:23 +0300 | [diff] [blame] | 270 | TIXMLASSERT( _size >= 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 271 | return _size;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 272 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 273 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 274 | int Capacity() const {
|
Dmitry-Me | 2f5a103 | 2015-06-18 16:40:09 +0300 | [diff] [blame] | 275 | TIXMLASSERT( _allocated >= INITIAL_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 276 | return _allocated;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 277 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 278 |
|
Lee Thomason | 816d3fa | 2017-06-05 14:35:55 -0700 | [diff] [blame] | 279 | void SwapRemove(int i) {
|
Dmitry-Me | c2f677b | 2017-06-15 12:44:27 +0300 | [diff] [blame] | 280 | TIXMLASSERT(i >= 0 && i < _size);
|
| 281 | TIXMLASSERT(_size > 0);
|
Lee Thomason | 816d3fa | 2017-06-05 14:35:55 -0700 | [diff] [blame] | 282 | _mem[i] = _mem[_size - 1];
|
| 283 | --_size;
|
| 284 | }
|
| 285 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 286 | const T* Mem() const {
|
Dmitry-Me | 2f5a103 | 2015-06-18 16:40:09 +0300 | [diff] [blame] | 287 | TIXMLASSERT( _mem );
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 288 | return _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 289 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 290 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 291 | T* Mem() {
|
Dmitry-Me | 2f5a103 | 2015-06-18 16:40:09 +0300 | [diff] [blame] | 292 | TIXMLASSERT( _mem );
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 293 | return _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 294 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 295 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 296 | private:
|
Dmitry-Me | 3e0af37 | 2015-01-09 15:30:00 +0300 | [diff] [blame] | 297 | DynArray( const DynArray& ); // not supported
|
| 298 | void operator=( const DynArray& ); // not supported
|
| 299 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 300 | void EnsureCapacity( int cap ) {
|
Dmitry-Me | 9fae869 | 2014-12-22 17:59:59 +0300 | [diff] [blame] | 301 | TIXMLASSERT( cap > 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 302 | if ( cap > _allocated ) {
|
Dmitry-Me | 9fae869 | 2014-12-22 17:59:59 +0300 | [diff] [blame] | 303 | TIXMLASSERT( cap <= INT_MAX / 2 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 304 | int newAllocated = cap * 2;
|
| 305 | T* newMem = new T[newAllocated];
|
Dmitry-Me | 243ddf5 | 2017-05-18 17:27:14 +0300 | [diff] [blame] | 306 | TIXMLASSERT( newAllocated >= _size );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 307 | memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
|
| 308 | if ( _mem != _pool ) {
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 309 | delete [] _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 310 | }
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 311 | _mem = newMem;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 312 | _allocated = newAllocated;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 313 | }
|
| 314 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 315 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 316 | T* _mem;
|
Dmitry-Me | 0400922 | 2015-04-06 18:07:18 +0300 | [diff] [blame] | 317 | T _pool[INITIAL_SIZE];
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 318 | int _allocated; // objects allocated
|
| 319 | int _size; // number objects in use
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 320 | };
|
| 321 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 322 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 323 | /*
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 324 | Parent virtual class of a pool for fast allocation
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 325 | and deallocation of objects.
|
| 326 | */
|
PKEuS | 9506035 | 2013-07-26 10:42:44 +0200 | [diff] [blame] | 327 | class MemPool
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 328 | {
|
| 329 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 330 | MemPool() {}
|
| 331 | virtual ~MemPool() {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 332 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 333 | virtual int ItemSize() const = 0;
|
| 334 | virtual void* Alloc() = 0;
|
| 335 | virtual void Free( void* ) = 0;
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 336 | virtual void SetTracked() = 0;
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 337 | virtual void Clear() = 0;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 338 | };
|
| 339 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 340 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 341 | /*
|
| 342 | Template child class to create pools of the correct type.
|
| 343 | */
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 344 | template< int ITEM_SIZE >
|
PKEuS | 9506035 | 2013-07-26 10:42:44 +0200 | [diff] [blame] | 345 | class MemPoolT : public MemPool
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 346 | {
|
| 347 | public:
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 348 | MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 349 | ~MemPoolT() {
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 350 | Clear();
|
| 351 | }
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 352 |
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 353 | void Clear() {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 354 | // Delete the blocks.
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 355 | while( !_blockPtrs.Empty()) {
|
Dmitry-Me | effab6f | 2017-07-26 17:57:50 +0300 | [diff] [blame] | 356 | Block* lastBlock = _blockPtrs.Pop();
|
| 357 | delete lastBlock;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 358 | }
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 359 | _root = 0;
|
| 360 | _currentAllocs = 0;
|
| 361 | _nAllocs = 0;
|
| 362 | _maxAllocs = 0;
|
| 363 | _nUntracked = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 364 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 365 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 366 | virtual int ItemSize() const {
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 367 | return ITEM_SIZE;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 368 | }
|
| 369 | int CurrentAllocs() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 370 | return _currentAllocs;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 371 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 372 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 373 | virtual void* Alloc() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 374 | if ( !_root ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 375 | // Need a new block.
|
| 376 | Block* block = new Block();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 377 | _blockPtrs.Push( block );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 378 |
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 379 | Item* blockItems = block->items;
|
| 380 | for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
|
| 381 | blockItems[i].next = &(blockItems[i + 1]);
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 382 | }
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 383 | blockItems[ITEMS_PER_BLOCK - 1].next = 0;
|
| 384 | _root = blockItems;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 385 | }
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 386 | Item* const result = _root;
|
| 387 | TIXMLASSERT( result != 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 388 | _root = _root->next;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 389 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 390 | ++_currentAllocs;
|
| 391 | if ( _currentAllocs > _maxAllocs ) {
|
| 392 | _maxAllocs = _currentAllocs;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 393 | }
|
Dmitry-Me | 3161a33 | 2016-09-02 16:58:06 +0300 | [diff] [blame] | 394 | ++_nAllocs;
|
| 395 | ++_nUntracked;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 396 | return result;
|
| 397 | }
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 398 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 399 | virtual void Free( void* mem ) {
|
| 400 | if ( !mem ) {
|
| 401 | return;
|
| 402 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 403 | --_currentAllocs;
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 404 | Item* item = static_cast<Item*>( mem );
|
Peter Matula | 5068991 | 2018-01-09 12:52:26 +0100 | [diff] [blame] | 405 | #ifdef TINYXML2_DEBUG
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 406 | memset( item, 0xfe, sizeof( *item ) );
|
Lee Thomason (grinliz) | 6020a01 | 2012-09-08 21:15:09 -0700 | [diff] [blame] | 407 | #endif
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 408 | item->next = _root;
|
| 409 | _root = item;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 410 | }
|
| 411 | void Trace( const char* name ) {
|
| 412 | printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 413 | name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
|
| 414 | ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 415 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 416 |
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 417 | void SetTracked() {
|
Dmitry-Me | 3161a33 | 2016-09-02 16:58:06 +0300 | [diff] [blame] | 418 | --_nUntracked;
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 419 | }
|
| 420 |
|
| 421 | int Untracked() const {
|
| 422 | return _nUntracked;
|
| 423 | }
|
| 424 |
|
Lee Thomason (grinliz) | ac83b4e | 2013-02-01 09:02:34 -0800 | [diff] [blame] | 425 | // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
|
| 426 | // The test file is large, 170k.
|
| 427 | // Release: VS2010 gcc(no opt)
|
| 428 | // 1k: 4000
|
| 429 | // 2k: 4000
|
| 430 | // 4k: 3900 21000
|
| 431 | // 16k: 5200
|
| 432 | // 32k: 4300
|
| 433 | // 64k: 4000 21000
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 434 | // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
|
| 435 | // in private part if ITEMS_PER_BLOCK is private
|
| 436 | enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
|
Jerome Martinez | 7921df1 | 2012-10-24 11:45:44 +0200 | [diff] [blame] | 437 |
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 438 | private:
|
Dmitry-Me | 3e0af37 | 2015-01-09 15:30:00 +0300 | [diff] [blame] | 439 | MemPoolT( const MemPoolT& ); // not supported
|
| 440 | void operator=( const MemPoolT& ); // not supported
|
| 441 |
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 442 | union Item {
|
| 443 | Item* next;
|
| 444 | char itemData[ITEM_SIZE];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 445 | };
|
| 446 | struct Block {
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 447 | Item items[ITEMS_PER_BLOCK];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 448 | };
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 449 | DynArray< Block*, 10 > _blockPtrs;
|
Dmitry-Me | 88145b8 | 2016-08-09 17:59:31 +0300 | [diff] [blame] | 450 | Item* _root;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 451 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 452 | int _currentAllocs;
|
| 453 | int _nAllocs;
|
| 454 | int _maxAllocs;
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 455 | int _nUntracked;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 456 | };
|
| 457 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 458 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 459 |
|
| 460 | /**
|
| 461 | Implements the interface to the "Visitor pattern" (see the Accept() method.)
|
| 462 | If you call the Accept() method, it requires being passed a XMLVisitor
|
| 463 | class to handle callbacks. For nodes that contain other nodes (Document, Element)
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 464 | you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 465 | are simply called with Visit().
|
| 466 |
|
| 467 | If you return 'true' from a Visit method, recursive parsing will continue. If you return
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 468 | false, <b>no children of this node or its siblings</b> will be visited.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 469 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 470 | All flavors of Visit methods have a default implementation that returns 'true' (continue
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 471 | visiting). You need to only override methods that are interesting to you.
|
| 472 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 473 | Generally Accept() is called on the XMLDocument, although all nodes support visiting.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 474 |
|
| 475 | You should never change the document from a callback.
|
| 476 |
|
| 477 | @sa XMLNode::Accept()
|
| 478 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 479 | class TINYXML2_LIB XMLVisitor
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 480 | {
|
| 481 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 482 | virtual ~XMLVisitor() {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 483 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 484 | /// Visit a document.
|
| 485 | virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
|
| 486 | return true;
|
| 487 | }
|
| 488 | /// Visit a document.
|
| 489 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
|
| 490 | return true;
|
| 491 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 492 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 493 | /// Visit an element.
|
| 494 | virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
|
| 495 | return true;
|
| 496 | }
|
| 497 | /// Visit an element.
|
| 498 | virtual bool VisitExit( const XMLElement& /*element*/ ) {
|
| 499 | return true;
|
| 500 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 501 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 502 | /// Visit a declaration.
|
| 503 | virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
|
| 504 | return true;
|
| 505 | }
|
| 506 | /// Visit a text node.
|
| 507 | virtual bool Visit( const XMLText& /*text*/ ) {
|
| 508 | return true;
|
| 509 | }
|
| 510 | /// Visit a comment node.
|
| 511 | virtual bool Visit( const XMLComment& /*comment*/ ) {
|
| 512 | return true;
|
| 513 | }
|
| 514 | /// Visit an unknown node.
|
| 515 | virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
|
| 516 | return true;
|
| 517 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 518 | };
|
| 519 |
|
Dmitry-Me | 66d2a84 | 2014-11-08 15:24:52 +0300 | [diff] [blame] | 520 | // WARNING: must match XMLDocument::_errorNames[]
|
numatrumpet | bb5ffac | 2014-09-06 22:56:46 +0900 | [diff] [blame] | 521 | enum XMLError {
|
numatrumpet | cd8550c | 2014-09-08 16:59:39 +0900 | [diff] [blame] | 522 | XML_SUCCESS = 0,
|
numatrumpet | cd8550c | 2014-09-08 16:59:39 +0900 | [diff] [blame] | 523 | XML_NO_ATTRIBUTE,
|
| 524 | XML_WRONG_ATTRIBUTE_TYPE,
|
| 525 | XML_ERROR_FILE_NOT_FOUND,
|
| 526 | XML_ERROR_FILE_COULD_NOT_BE_OPENED,
|
| 527 | XML_ERROR_FILE_READ_ERROR,
|
numatrumpet | cd8550c | 2014-09-08 16:59:39 +0900 | [diff] [blame] | 528 | XML_ERROR_PARSING_ELEMENT,
|
| 529 | XML_ERROR_PARSING_ATTRIBUTE,
|
numatrumpet | cd8550c | 2014-09-08 16:59:39 +0900 | [diff] [blame] | 530 | XML_ERROR_PARSING_TEXT,
|
| 531 | XML_ERROR_PARSING_CDATA,
|
| 532 | XML_ERROR_PARSING_COMMENT,
|
| 533 | XML_ERROR_PARSING_DECLARATION,
|
| 534 | XML_ERROR_PARSING_UNKNOWN,
|
| 535 | XML_ERROR_EMPTY_DOCUMENT,
|
| 536 | XML_ERROR_MISMATCHED_ELEMENT,
|
| 537 | XML_ERROR_PARSING,
|
| 538 | XML_CAN_NOT_CONVERT_TEXT,
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 539 | XML_NO_TEXT_NODE,
|
Lee Thomason | d946dda | 2018-04-05 09:11:08 -0700 | [diff] [blame] | 540 | XML_ELEMENT_DEPTH_EXCEEDED,
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 541 |
|
| 542 | XML_ERROR_COUNT
|
numatrumpet | bb5ffac | 2014-09-06 22:56:46 +0900 | [diff] [blame] | 543 | };
|
numatrumpet | bb5ffac | 2014-09-06 22:56:46 +0900 | [diff] [blame] | 544 |
|
numatrumpet | cd8550c | 2014-09-08 16:59:39 +0900 | [diff] [blame] | 545 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 546 | /*
|
| 547 | Utility functionality.
|
| 548 | */
|
Lee Thomason | ce667c9 | 2016-12-26 16:45:30 -0800 | [diff] [blame] | 549 | class TINYXML2_LIB XMLUtil
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 550 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 551 | public:
|
kezenator | 4f75616 | 2016-11-29 19:46:27 +1000 | [diff] [blame] | 552 | static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 553 | TIXMLASSERT( p );
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 554 |
|
Dmitry-Me | fa20b22 | 2014-10-31 12:53:04 +0300 | [diff] [blame] | 555 | while( IsWhiteSpace(*p) ) {
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 556 | if (curLineNumPtr && *p == '\n') {
|
kezenator | 4f75616 | 2016-11-29 19:46:27 +1000 | [diff] [blame] | 557 | ++(*curLineNumPtr);
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 558 | }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 559 | ++p;
|
| 560 | }
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 561 | TIXMLASSERT( p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 562 | return p;
|
| 563 | }
|
kezenator | 4f75616 | 2016-11-29 19:46:27 +1000 | [diff] [blame] | 564 | static char* SkipWhiteSpace( char* p, int* curLineNumPtr ) {
|
| 565 | return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 566 | }
|
Dmitry-Me | fa20b22 | 2014-10-31 12:53:04 +0300 | [diff] [blame] | 567 |
|
| 568 | // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
|
| 569 | // correct, but simple, and usually works.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 570 | static bool IsWhiteSpace( char p ) {
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 571 | return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 572 | }
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 573 |
|
Martinsh Shaiters | c6d02f4 | 2013-01-26 21:22:57 +0200 | [diff] [blame] | 574 | inline static bool IsNameStartChar( unsigned char ch ) {
|
Dmitry-Me | ea617f9 | 2015-01-01 16:32:01 +0300 | [diff] [blame] | 575 | if ( ch >= 128 ) {
|
| 576 | // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
|
| 577 | return true;
|
| 578 | }
|
| 579 | if ( isalpha( ch ) ) {
|
| 580 | return true;
|
| 581 | }
|
| 582 | return ch == ':' || ch == '_';
|
Martinsh Shaiters | c6d02f4 | 2013-01-26 21:22:57 +0200 | [diff] [blame] | 583 | }
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 584 |
|
Martinsh Shaiters | c6d02f4 | 2013-01-26 21:22:57 +0200 | [diff] [blame] | 585 | inline static bool IsNameChar( unsigned char ch ) {
|
| 586 | return IsNameStartChar( ch )
|
| 587 | || isdigit( ch )
|
| 588 | || ch == '.'
|
| 589 | || ch == '-';
|
| 590 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 591 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 592 | inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 593 | if ( p == q ) {
|
| 594 | return true;
|
| 595 | }
|
Dmitry-Me | 21f9969 | 2016-10-03 13:01:57 +0300 | [diff] [blame] | 596 | TIXMLASSERT( p );
|
| 597 | TIXMLASSERT( q );
|
| 598 | TIXMLASSERT( nChar >= 0 );
|
Lee Thomason | 598a88d | 2015-10-09 14:42:12 -0700 | [diff] [blame] | 599 | return strncmp( p, q, nChar ) == 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 600 | }
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 601 |
|
Dmitry-Me | 7865aad | 2015-06-19 16:23:35 +0300 | [diff] [blame] | 602 | inline static bool IsUTF8Continuation( char p ) {
|
Dmitry-Me | 72bb0ec | 2014-09-24 16:14:24 +0400 | [diff] [blame] | 603 | return ( p & 0x80 ) != 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 604 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 605 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 606 | static const char* ReadBOM( const char* p, bool* hasBOM );
|
| 607 | // p is the starting location,
|
| 608 | // the UTF-8 value of the entity will be placed in value, and length filled in.
|
| 609 | static const char* GetCharacterRef( const char* p, char* value, int* length );
|
| 610 | static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 611 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 612 | // converts primitive types to strings
|
| 613 | static void ToStr( int v, char* buffer, int bufferSize );
|
| 614 | static void ToStr( unsigned v, char* buffer, int bufferSize );
|
| 615 | static void ToStr( bool v, char* buffer, int bufferSize );
|
| 616 | static void ToStr( float v, char* buffer, int bufferSize );
|
| 617 | static void ToStr( double v, char* buffer, int bufferSize );
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 618 | static void ToStr(int64_t v, char* buffer, int bufferSize);
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 619 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 620 | // converts strings to primitive types
|
| 621 | static bool ToInt( const char* str, int* value );
|
| 622 | static bool ToUnsigned( const char* str, unsigned* value );
|
| 623 | static bool ToBool( const char* str, bool* value );
|
| 624 | static bool ToFloat( const char* str, float* value );
|
| 625 | static bool ToDouble( const char* str, double* value );
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 626 | static bool ToInt64(const char* str, int64_t* value);
|
Lee Thomason | ce667c9 | 2016-12-26 16:45:30 -0800 | [diff] [blame] | 627 |
|
Lee Thomason | c5c99c2 | 2016-12-29 11:19:17 -0800 | [diff] [blame] | 628 | // Changes what is serialized for a boolean value.
|
| 629 | // Default to "true" and "false". Shouldn't be changed
|
| 630 | // unless you have a special testing or compatibility need.
|
Lee Thomason | ce667c9 | 2016-12-26 16:45:30 -0800 | [diff] [blame] | 631 | // Be careful: static, global, & not thread safe.
|
| 632 | // Be sure to set static const memory as parameters.
|
Lee Thomason | c5c99c2 | 2016-12-29 11:19:17 -0800 | [diff] [blame] | 633 | static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
|
Lee Thomason | ce667c9 | 2016-12-26 16:45:30 -0800 | [diff] [blame] | 634 |
|
| 635 | private:
|
Lee Thomason | f458d26 | 2016-12-26 22:47:25 -0800 | [diff] [blame] | 636 | static const char* writeBoolTrue;
|
| 637 | static const char* writeBoolFalse;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 638 | };
|
| 639 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 640 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 641 | /** XMLNode is a base class for every object that is in the
|
| 642 | XML Document Object Model (DOM), except XMLAttributes.
|
| 643 | Nodes have siblings, a parent, and children which can
|
| 644 | be navigated. A node is always in a XMLDocument.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 645 | The type of a XMLNode can be queried, and it can
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 646 | be cast to its more defined type.
|
| 647 |
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 648 | A XMLDocument allocates memory for all its Nodes.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 649 | When the XMLDocument gets deleted, all its Nodes
|
| 650 | will also be deleted.
|
| 651 |
|
| 652 | @verbatim
|
| 653 | A Document can contain: Element (container or leaf)
|
| 654 | Comment (leaf)
|
| 655 | Unknown (leaf)
|
| 656 | Declaration( leaf )
|
| 657 |
|
| 658 | An Element can contain: Element (container or leaf)
|
| 659 | Text (leaf)
|
| 660 | Attributes (not on tree)
|
| 661 | Comment (leaf)
|
| 662 | Unknown (leaf)
|
| 663 |
|
| 664 | @endverbatim
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 665 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 666 | class TINYXML2_LIB XMLNode
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 667 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 668 | friend class XMLDocument;
|
| 669 | friend class XMLElement;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 670 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 671 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 672 | /// Get the XMLDocument that owns this XMLNode.
|
| 673 | const XMLDocument* GetDocument() const {
|
Dmitry-Me | 66487eb | 2015-07-20 18:21:04 +0300 | [diff] [blame] | 674 | TIXMLASSERT( _document );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 675 | return _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 676 | }
|
| 677 | /// Get the XMLDocument that owns this XMLNode.
|
| 678 | XMLDocument* GetDocument() {
|
Dmitry-Me | 66487eb | 2015-07-20 18:21:04 +0300 | [diff] [blame] | 679 | TIXMLASSERT( _document );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 680 | return _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 681 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 682 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 683 | /// Safely cast to an Element, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 684 | virtual XMLElement* ToElement() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 685 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 686 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 687 | /// Safely cast to Text, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 688 | virtual XMLText* ToText() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 689 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 690 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 691 | /// Safely cast to a Comment, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 692 | virtual XMLComment* ToComment() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 693 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 694 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 695 | /// Safely cast to a Document, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 696 | virtual XMLDocument* ToDocument() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 697 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 698 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 699 | /// Safely cast to a Declaration, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 700 | virtual XMLDeclaration* ToDeclaration() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 701 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 702 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 703 | /// Safely cast to an Unknown, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 704 | virtual XMLUnknown* ToUnknown() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 705 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 706 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 707 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 708 | virtual const XMLElement* ToElement() const {
|
| 709 | return 0;
|
| 710 | }
|
| 711 | virtual const XMLText* ToText() const {
|
| 712 | return 0;
|
| 713 | }
|
| 714 | virtual const XMLComment* ToComment() const {
|
| 715 | return 0;
|
| 716 | }
|
| 717 | virtual const XMLDocument* ToDocument() const {
|
| 718 | return 0;
|
| 719 | }
|
| 720 | virtual const XMLDeclaration* ToDeclaration() const {
|
| 721 | return 0;
|
| 722 | }
|
| 723 | virtual const XMLUnknown* ToUnknown() const {
|
| 724 | return 0;
|
| 725 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 726 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 727 | /** The meaning of 'value' changes for the specific type.
|
| 728 | @verbatim
|
Sarat Addepalli | 9afd1d0 | 2015-05-19 12:56:27 +0530 | [diff] [blame] | 729 | Document: empty (NULL is returned, not an empty string)
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 730 | Element: name of the element
|
| 731 | Comment: the comment text
|
| 732 | Unknown: the tag contents
|
| 733 | Text: the text string
|
| 734 | @endverbatim
|
| 735 | */
|
Michael Daumling | 2162688 | 2013-10-22 17:03:37 +0200 | [diff] [blame] | 736 | const char* Value() const;
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 737 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 738 | /** Set the Value of an XML node.
|
| 739 | @sa Value()
|
| 740 | */
|
| 741 | void SetValue( const char* val, bool staticMem=false );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 742 |
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 743 | /// Gets the line number the node is in, if the document was parsed from a file.
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 744 | int GetLineNum() const { return _parseLineNum; }
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 745 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 746 | /// Get the parent of this node on the DOM.
|
| 747 | const XMLNode* Parent() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 748 | return _parent;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 749 | }
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 750 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 751 | XMLNode* Parent() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 752 | return _parent;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 753 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 754 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 755 | /// Returns true if this node has no children.
|
| 756 | bool NoChildren() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 757 | return !_firstChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 758 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 759 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 760 | /// Get the first child node, or null if none exists.
|
| 761 | const XMLNode* FirstChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 762 | return _firstChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 763 | }
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 764 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 765 | XMLNode* FirstChild() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 766 | return _firstChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 767 | }
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 768 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 769 | /** Get the first child element, or optionally the first child
|
| 770 | element with the specified name.
|
| 771 | */
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 772 | const XMLElement* FirstChildElement( const char* name = 0 ) const;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 773 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 774 | XMLElement* FirstChildElement( const char* name = 0 ) {
|
| 775 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 776 | }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 777 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 778 | /// Get the last child node, or null if none exists.
|
| 779 | const XMLNode* LastChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 780 | return _lastChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 781 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 782 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 783 | XMLNode* LastChild() {
|
Dmitry-Me | 8d4e0ec | 2015-03-30 12:58:28 +0300 | [diff] [blame] | 784 | return _lastChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 785 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 786 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 787 | /** Get the last child element or optionally the last child
|
| 788 | element with the specified name.
|
| 789 | */
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 790 | const XMLElement* LastChildElement( const char* name = 0 ) const;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 791 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 792 | XMLElement* LastChildElement( const char* name = 0 ) {
|
| 793 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 794 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 795 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 796 | /// Get the previous (left) sibling node of this node.
|
| 797 | const XMLNode* PreviousSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 798 | return _prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 799 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 800 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 801 | XMLNode* PreviousSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 802 | return _prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 803 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 804 |
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 805 | /// Get the previous (left) sibling element of this node, with an optionally supplied name.
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 806 | const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 807 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 808 | XMLElement* PreviousSiblingElement( const char* name = 0 ) {
|
| 809 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 810 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 811 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 812 | /// Get the next (right) sibling node of this node.
|
| 813 | const XMLNode* NextSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 814 | return _next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 815 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 816 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 817 | XMLNode* NextSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 818 | return _next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 819 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 820 |
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 821 | /// Get the next (right) sibling element of this node, with an optionally supplied name.
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 822 | const XMLElement* NextSiblingElement( const char* name = 0 ) const;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 823 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 824 | XMLElement* NextSiblingElement( const char* name = 0 ) {
|
| 825 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 826 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 827 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 828 | /**
|
| 829 | Add a child node as the last (right) child.
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 830 | If the child node is already part of the document,
|
| 831 | it is moved from its old location to the new location.
|
| 832 | Returns the addThis argument or 0 if the node does not
|
| 833 | belong to the same document.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 834 | */
|
| 835 | XMLNode* InsertEndChild( XMLNode* addThis );
|
Lee Thomason | 618dbf8 | 2012-02-28 12:34:27 -0800 | [diff] [blame] | 836 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 837 | XMLNode* LinkEndChild( XMLNode* addThis ) {
|
| 838 | return InsertEndChild( addThis );
|
| 839 | }
|
| 840 | /**
|
| 841 | Add a child node as the first (left) child.
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 842 | If the child node is already part of the document,
|
| 843 | it is moved from its old location to the new location.
|
| 844 | Returns the addThis argument or 0 if the node does not
|
| 845 | belong to the same document.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 846 | */
|
| 847 | XMLNode* InsertFirstChild( XMLNode* addThis );
|
| 848 | /**
|
| 849 | Add a node after the specified child node.
|
Michael Daumling | ed52328 | 2013-10-23 07:47:29 +0200 | [diff] [blame] | 850 | If the child node is already part of the document,
|
| 851 | it is moved from its old location to the new location.
|
| 852 | Returns the addThis argument or 0 if the afterThis node
|
| 853 | is not a child of this node, or if the node does not
|
| 854 | belong to the same document.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 855 | */
|
| 856 | XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 857 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 858 | /**
|
| 859 | Delete all the children of this node.
|
| 860 | */
|
| 861 | void DeleteChildren();
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 862 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 863 | /**
|
| 864 | Delete a child of this node.
|
| 865 | */
|
| 866 | void DeleteChild( XMLNode* node );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 867 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 868 | /**
|
| 869 | Make a copy of this node, but not its children.
|
| 870 | You may pass in a Document pointer that will be
|
| 871 | the owner of the new Node. If the 'document' is
|
| 872 | null, then the node returned will be allocated
|
| 873 | from the current Document. (this->GetDocument())
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 874 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 875 | Note: if called on a XMLDocument, this will return null.
|
| 876 | */
|
| 877 | virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 878 |
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 879 | /**
|
Lee Thomason | 1346a17 | 2017-06-14 15:14:19 -0700 | [diff] [blame] | 880 | Make a copy of this node and all its children.
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 881 |
|
Dmitry-Me | 3f63f21 | 2017-06-19 18:25:19 +0300 | [diff] [blame] | 882 | If the 'target' is null, then the nodes will
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 883 | be allocated in the current document. If 'target'
|
| 884 | is specified, the memory will be allocated is the
|
Lee Thomason | 1346a17 | 2017-06-14 15:14:19 -0700 | [diff] [blame] | 885 | specified XMLDocument.
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 886 |
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 887 | NOTE: This is probably not the correct tool to
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 888 | copy a document, since XMLDocuments can have multiple
|
Lee Thomason | 1346a17 | 2017-06-14 15:14:19 -0700 | [diff] [blame] | 889 | top level XMLNodes. You probably want to use
|
| 890 | XMLDocument::DeepCopy()
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 891 | */
|
Dmitry-Me | 3f63f21 | 2017-06-19 18:25:19 +0300 | [diff] [blame] | 892 | XMLNode* DeepClone( XMLDocument* target ) const;
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 893 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 894 | /**
|
| 895 | Test if 2 nodes are the same, but don't test children.
|
| 896 | The 2 nodes do not need to be in the same Document.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 897 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 898 | Note: if called on a XMLDocument, this will return false.
|
| 899 | */
|
| 900 | virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 901 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 902 | /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 903 | XML tree will be conditionally visited and the host will be called back
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 904 | via the XMLVisitor interface.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 905 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 906 | This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
|
| 907 | the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 908 | interface versus any other.)
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 909 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 910 | The interface has been based on ideas from:
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 911 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 912 | - http://www.saxproject.org/
|
| 913 | - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 914 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 915 | Which are both good references for "visiting".
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 916 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 917 | An example of using Accept():
|
| 918 | @verbatim
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 919 | XMLPrinter printer;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 920 | tinyxmlDoc.Accept( &printer );
|
| 921 | const char* xmlcstr = printer.CStr();
|
| 922 | @endverbatim
|
| 923 | */
|
| 924 | virtual bool Accept( XMLVisitor* visitor ) const = 0;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 925 |
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 926 | /**
|
| 927 | Set user data into the XMLNode. TinyXML-2 in
|
Lee Thomason | af9bce1 | 2016-07-17 22:35:52 -0700 | [diff] [blame] | 928 | no way processes or interprets user data.
|
| 929 | It is initially 0.
|
| 930 | */
|
| 931 | void SetUserData(void* userData) { _userData = userData; }
|
| 932 |
|
| 933 | /**
|
| 934 | Get user data set into the XMLNode. TinyXML-2 in
|
| 935 | no way processes or interprets user data.
|
| 936 | It is initially 0.
|
| 937 | */
|
| 938 | void* GetUserData() const { return _userData; }
|
| 939 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 940 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 941 | XMLNode( XMLDocument* );
|
| 942 | virtual ~XMLNode();
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 943 |
|
Dmitry-Me | 10b8ecc | 2017-04-12 17:57:44 +0300 | [diff] [blame] | 944 | virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
|
Dmitry-Me | 9b0f177 | 2015-04-03 10:37:31 +0300 | [diff] [blame] | 945 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 946 | XMLDocument* _document;
|
| 947 | XMLNode* _parent;
|
| 948 | mutable StrPair _value;
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 949 | int _parseLineNum;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 950 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 951 | XMLNode* _firstChild;
|
| 952 | XMLNode* _lastChild;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 953 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 954 | XMLNode* _prev;
|
| 955 | XMLNode* _next;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 956 |
|
Lee Thomason | af9bce1 | 2016-07-17 22:35:52 -0700 | [diff] [blame] | 957 | void* _userData;
|
| 958 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 959 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 960 | MemPool* _memPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 961 | void Unlink( XMLNode* child );
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 962 | static void DeleteNode( XMLNode* node );
|
Lee Thomason | 3cebdc4 | 2015-01-05 17:16:28 -0800 | [diff] [blame] | 963 | void InsertChildPreamble( XMLNode* insertThis ) const;
|
Dmitry-Me | ecb9b07 | 2016-10-12 16:44:59 +0300 | [diff] [blame] | 964 | const XMLElement* ToElementWithName( const char* name ) const;
|
Dmitry-Me | f547a99 | 2015-01-09 15:17:09 +0300 | [diff] [blame] | 965 |
|
| 966 | XMLNode( const XMLNode& ); // not supported
|
| 967 | XMLNode& operator=( const XMLNode& ); // not supported
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 968 | };
|
| 969 |
|
| 970 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 971 | /** XML text.
|
| 972 |
|
| 973 | Note that a text node can have child element nodes, for example:
|
| 974 | @verbatim
|
| 975 | <root>This is <b>bold</b></root>
|
| 976 | @endverbatim
|
| 977 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 978 | A text node can have 2 ways to output the next. "normal" output
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 979 | and CDATA. It will default to the mode it was parsed from the XML file and
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 980 | you generally want to leave it alone, but you can change the output mode with
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 981 | SetCData() and query it with CData().
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 982 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 983 | class TINYXML2_LIB XMLText : public XMLNode
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 984 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 985 | friend class XMLDocument;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 986 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 987 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 988 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 989 | virtual XMLText* ToText() {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 990 | return this;
|
| 991 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 992 | virtual const XMLText* ToText() const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 993 | return this;
|
| 994 | }
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 995 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 996 | /// Declare whether this should be CDATA or standard text.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 997 | void SetCData( bool isCData ) {
|
| 998 | _isCData = isCData;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 999 | }
|
| 1000 | /// Returns true if this is a CDATA text element.
|
| 1001 | bool CData() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1002 | return _isCData;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1003 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1004 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1005 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 1006 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1007 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 1008 | protected:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1009 | XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1010 | virtual ~XMLText() {}
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 1011 |
|
Dmitry-Me | 10b8ecc | 2017-04-12 17:57:44 +0300 | [diff] [blame] | 1012 | char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
|
Dmitry-Me | 9b0f177 | 2015-04-03 10:37:31 +0300 | [diff] [blame] | 1013 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 1014 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1015 | bool _isCData;
|
Dmitry-Me | f547a99 | 2015-01-09 15:17:09 +0300 | [diff] [blame] | 1016 |
|
| 1017 | XMLText( const XMLText& ); // not supported
|
| 1018 | XMLText& operator=( const XMLText& ); // not supported
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 1019 | };
|
| 1020 |
|
| 1021 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1022 | /** An XML Comment. */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1023 | class TINYXML2_LIB XMLComment : public XMLNode
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 1024 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1025 | friend class XMLDocument;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1026 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1027 | virtual XMLComment* ToComment() {
|
| 1028 | return this;
|
| 1029 | }
|
| 1030 | virtual const XMLComment* ToComment() const {
|
| 1031 | return this;
|
| 1032 | }
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 1033 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1034 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1035 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1036 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 1037 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 1038 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1039 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1040 | XMLComment( XMLDocument* doc );
|
| 1041 | virtual ~XMLComment();
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1042 |
|
Dmitry-Me | 10b8ecc | 2017-04-12 17:57:44 +0300 | [diff] [blame] | 1043 | char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
|
Dmitry-Me | 9b0f177 | 2015-04-03 10:37:31 +0300 | [diff] [blame] | 1044 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1045 | private:
|
Dmitry-Me | f547a99 | 2015-01-09 15:17:09 +0300 | [diff] [blame] | 1046 | XMLComment( const XMLComment& ); // not supported
|
| 1047 | XMLComment& operator=( const XMLComment& ); // not supported
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 1048 | };
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1049 |
|
| 1050 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1051 | /** In correct XML the declaration is the first entry in the file.
|
| 1052 | @verbatim
|
| 1053 | <?xml version="1.0" standalone="yes"?>
|
| 1054 | @endverbatim
|
| 1055 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1056 | TinyXML-2 will happily read or write files without a declaration,
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1057 | however.
|
| 1058 |
|
| 1059 | The text of the declaration isn't interpreted. It is parsed
|
| 1060 | and written as a string.
|
| 1061 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1062 | class TINYXML2_LIB XMLDeclaration : public XMLNode
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1063 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1064 | friend class XMLDocument;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1065 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1066 | virtual XMLDeclaration* ToDeclaration() {
|
| 1067 | return this;
|
| 1068 | }
|
| 1069 | virtual const XMLDeclaration* ToDeclaration() const {
|
| 1070 | return this;
|
| 1071 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1072 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1073 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1074 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1075 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 1076 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1077 |
|
| 1078 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1079 | XMLDeclaration( XMLDocument* doc );
|
| 1080 | virtual ~XMLDeclaration();
|
Dmitry-Me | f547a99 | 2015-01-09 15:17:09 +0300 | [diff] [blame] | 1081 |
|
Dmitry-Me | 10b8ecc | 2017-04-12 17:57:44 +0300 | [diff] [blame] | 1082 | char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
|
Dmitry-Me | 9b0f177 | 2015-04-03 10:37:31 +0300 | [diff] [blame] | 1083 |
|
Dmitry-Me | f547a99 | 2015-01-09 15:17:09 +0300 | [diff] [blame] | 1084 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1085 | XMLDeclaration( const XMLDeclaration& ); // not supported
|
| 1086 | XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1087 | };
|
| 1088 |
|
| 1089 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1090 | /** Any tag that TinyXML-2 doesn't recognize is saved as an
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1091 | unknown. It is a tag of text, but should not be modified.
|
| 1092 | It will be written back to the XML, unchanged, when the file
|
| 1093 | is saved.
|
| 1094 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1095 | DTD tags get thrown into XMLUnknowns.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1096 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1097 | class TINYXML2_LIB XMLUnknown : public XMLNode
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1098 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1099 | friend class XMLDocument;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1100 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1101 | virtual XMLUnknown* ToUnknown() {
|
| 1102 | return this;
|
| 1103 | }
|
| 1104 | virtual const XMLUnknown* ToUnknown() const {
|
| 1105 | return this;
|
| 1106 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1107 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1108 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1109 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1110 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 1111 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1112 |
|
| 1113 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1114 | XMLUnknown( XMLDocument* doc );
|
| 1115 | virtual ~XMLUnknown();
|
Dmitry-Me | f547a99 | 2015-01-09 15:17:09 +0300 | [diff] [blame] | 1116 |
|
Dmitry-Me | 10b8ecc | 2017-04-12 17:57:44 +0300 | [diff] [blame] | 1117 | char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
|
Dmitry-Me | 9b0f177 | 2015-04-03 10:37:31 +0300 | [diff] [blame] | 1118 |
|
Dmitry-Me | f547a99 | 2015-01-09 15:17:09 +0300 | [diff] [blame] | 1119 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1120 | XMLUnknown( const XMLUnknown& ); // not supported
|
| 1121 | XMLUnknown& operator=( const XMLUnknown& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1122 | };
|
| 1123 |
|
| 1124 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1125 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1126 | /** An attribute is a name-value pair. Elements have an arbitrary
|
| 1127 | number of attributes, each with a unique name.
|
| 1128 |
|
| 1129 | @note The attributes are not XMLNodes. You may only query the
|
| 1130 | Next() attribute in a list.
|
| 1131 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1132 | class TINYXML2_LIB XMLAttribute
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1133 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1134 | friend class XMLElement;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1135 | public:
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1136 | /// The name of the attribute.
|
Michael Daumling | 2162688 | 2013-10-22 17:03:37 +0200 | [diff] [blame] | 1137 | const char* Name() const;
|
| 1138 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1139 | /// The value of the attribute.
|
Michael Daumling | 2162688 | 2013-10-22 17:03:37 +0200 | [diff] [blame] | 1140 | const char* Value() const;
|
| 1141 |
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1142 | /// Gets the line number the attribute is in, if the document was parsed from a file.
|
kezenator | 19d8ea8 | 2016-11-29 19:50:27 +1000 | [diff] [blame] | 1143 | int GetLineNum() const { return _parseLineNum; }
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1144 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1145 | /// The next attribute in the list.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1146 | const XMLAttribute* Next() const {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1147 | return _next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1148 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1149 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1150 | /** IntValue interprets the attribute as an integer, and returns the value.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1151 | If the value isn't an integer, 0 will be returned. There is no error checking;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1152 | use QueryIntValue() if you need error checking.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1153 | */
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1154 | int IntValue() const {
|
| 1155 | int i = 0;
|
| 1156 | QueryIntValue(&i);
|
| 1157 | return i;
|
| 1158 | }
|
| 1159 |
|
| 1160 | int64_t Int64Value() const {
|
| 1161 | int64_t i = 0;
|
| 1162 | QueryInt64Value(&i);
|
| 1163 | return i;
|
| 1164 | }
|
| 1165 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1166 | /// Query as an unsigned integer. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1167 | unsigned UnsignedValue() const {
|
| 1168 | unsigned i=0;
|
| 1169 | QueryUnsignedValue( &i );
|
| 1170 | return i;
|
| 1171 | }
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1172 | /// Query as a boolean. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1173 | bool BoolValue() const {
|
| 1174 | bool b=false;
|
| 1175 | QueryBoolValue( &b );
|
| 1176 | return b;
|
| 1177 | }
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1178 | /// Query as a double. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1179 | double DoubleValue() const {
|
| 1180 | double d=0;
|
| 1181 | QueryDoubleValue( &d );
|
| 1182 | return d;
|
| 1183 | }
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1184 | /// Query as a float. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1185 | float FloatValue() const {
|
| 1186 | float f=0;
|
| 1187 | QueryFloatValue( &f );
|
| 1188 | return f;
|
| 1189 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1190 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1191 | /** QueryIntValue interprets the attribute as an integer, and returns the value
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1192 | in the provided parameter. The function will return XML_SUCCESS on success,
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1193 | and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
|
| 1194 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1195 | XMLError QueryIntValue( int* value ) const;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1196 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1197 | XMLError QueryUnsignedValue( unsigned int* value ) const;
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1198 | /// See QueryIntValue
|
| 1199 | XMLError QueryInt64Value(int64_t* value) const;
|
| 1200 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1201 | XMLError QueryBoolValue( bool* value ) const;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1202 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1203 | XMLError QueryDoubleValue( double* value ) const;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1204 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1205 | XMLError QueryFloatValue( float* value ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1206 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1207 | /// Set the attribute to a string value.
|
| 1208 | void SetAttribute( const char* value );
|
| 1209 | /// Set the attribute to value.
|
| 1210 | void SetAttribute( int value );
|
| 1211 | /// Set the attribute to value.
|
| 1212 | void SetAttribute( unsigned value );
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1213 | /// Set the attribute to value.
|
| 1214 | void SetAttribute(int64_t value);
|
| 1215 | /// Set the attribute to value.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1216 | void SetAttribute( bool value );
|
| 1217 | /// Set the attribute to value.
|
| 1218 | void SetAttribute( double value );
|
| 1219 | /// Set the attribute to value.
|
| 1220 | void SetAttribute( float value );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1221 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1222 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1223 | enum { BUF_SIZE = 200 };
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1224 |
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 1225 | XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1226 | virtual ~XMLAttribute() {}
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1227 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1228 | XMLAttribute( const XMLAttribute& ); // not supported
|
| 1229 | void operator=( const XMLAttribute& ); // not supported
|
| 1230 | void SetName( const char* name );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1231 |
|
kezenator | 4f75616 | 2016-11-29 19:46:27 +1000 | [diff] [blame] | 1232 | char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1233 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1234 | mutable StrPair _name;
|
| 1235 | mutable StrPair _value;
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1236 | int _parseLineNum;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1237 | XMLAttribute* _next;
|
| 1238 | MemPool* _memPool;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1239 | };
|
| 1240 |
|
| 1241 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1242 | /** The element is a container class. It has a value, the element name,
|
| 1243 | and can contain other elements, text, comments, and unknowns.
|
| 1244 | Elements also contain an arbitrary number of attributes.
|
| 1245 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1246 | class TINYXML2_LIB XMLElement : public XMLNode
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1247 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1248 | friend class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1249 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1250 | /// Get the name of an element (which is the Value() of the node.)
|
| 1251 | const char* Name() const {
|
| 1252 | return Value();
|
| 1253 | }
|
| 1254 | /// Set the name of the element.
|
| 1255 | void SetName( const char* str, bool staticMem=false ) {
|
| 1256 | SetValue( str, staticMem );
|
| 1257 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1258 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1259 | virtual XMLElement* ToElement() {
|
| 1260 | return this;
|
| 1261 | }
|
| 1262 | virtual const XMLElement* ToElement() const {
|
| 1263 | return this;
|
| 1264 | }
|
| 1265 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1266 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1267 | /** Given an attribute name, Attribute() returns the value
|
| 1268 | for the attribute of that name, or null if none
|
| 1269 | exists. For example:
|
Lee Thomason | 9225815 | 2012-03-24 13:05:39 -0700 | [diff] [blame] | 1270 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1271 | @verbatim
|
| 1272 | const char* value = ele->Attribute( "foo" );
|
| 1273 | @endverbatim
|
Lee Thomason | 9225815 | 2012-03-24 13:05:39 -0700 | [diff] [blame] | 1274 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1275 | The 'value' parameter is normally null. However, if specified,
|
| 1276 | the attribute will only be returned if the 'name' and 'value'
|
| 1277 | match. This allow you to write code:
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1278 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1279 | @verbatim
|
| 1280 | if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
|
| 1281 | @endverbatim
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1282 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1283 | rather than:
|
| 1284 | @verbatim
|
| 1285 | if ( ele->Attribute( "foo" ) ) {
|
| 1286 | if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
|
| 1287 | }
|
| 1288 | @endverbatim
|
| 1289 | */
|
| 1290 | const char* Attribute( const char* name, const char* value=0 ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1291 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1292 | /** Given an attribute name, IntAttribute() returns the value
|
Lee Thomason | 13cbc9a | 2016-10-27 14:55:07 -0700 | [diff] [blame] | 1293 | of the attribute interpreted as an integer. The default
|
| 1294 | value will be returned if the attribute isn't present,
|
| 1295 | or if there is an error. (For a method with error
|
| 1296 | checking, see QueryIntAttribute()).
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1297 | */
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1298 | int IntAttribute(const char* name, int defaultValue = 0) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1299 | /// See IntAttribute()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1300 | unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1301 | /// See IntAttribute()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1302 | int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1303 | /// See IntAttribute()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1304 | bool BoolAttribute(const char* name, bool defaultValue = false) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1305 | /// See IntAttribute()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1306 | double DoubleAttribute(const char* name, double defaultValue = 0) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1307 | /// See IntAttribute()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1308 | float FloatAttribute(const char* name, float defaultValue = 0) const;
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1309 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1310 | /** Given an attribute name, QueryIntAttribute() returns
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1311 | XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1312 | can't be performed, or XML_NO_ATTRIBUTE if the attribute
|
| 1313 | doesn't exist. If successful, the result of the conversion
|
| 1314 | will be written to 'value'. If not successful, nothing will
|
| 1315 | be written to 'value'. This allows you to provide default
|
| 1316 | value:
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1317 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1318 | @verbatim
|
| 1319 | int value = 10;
|
| 1320 | QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
|
| 1321 | @endverbatim
|
| 1322 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1323 | XMLError QueryIntAttribute( const char* name, int* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1324 | const XMLAttribute* a = FindAttribute( name );
|
| 1325 | if ( !a ) {
|
| 1326 | return XML_NO_ATTRIBUTE;
|
| 1327 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1328 | return a->QueryIntValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1329 | }
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1330 |
|
| 1331 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1332 | XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1333 | const XMLAttribute* a = FindAttribute( name );
|
| 1334 | if ( !a ) {
|
| 1335 | return XML_NO_ATTRIBUTE;
|
| 1336 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1337 | return a->QueryUnsignedValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1338 | }
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1339 |
|
| 1340 | /// See QueryIntAttribute()
|
| 1341 | XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
|
| 1342 | const XMLAttribute* a = FindAttribute(name);
|
| 1343 | if (!a) {
|
| 1344 | return XML_NO_ATTRIBUTE;
|
| 1345 | }
|
| 1346 | return a->QueryInt64Value(value);
|
| 1347 | }
|
| 1348 |
|
| 1349 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1350 | XMLError QueryBoolAttribute( const char* name, bool* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1351 | const XMLAttribute* a = FindAttribute( name );
|
| 1352 | if ( !a ) {
|
| 1353 | return XML_NO_ATTRIBUTE;
|
| 1354 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1355 | return a->QueryBoolValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1356 | }
|
| 1357 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1358 | XMLError QueryDoubleAttribute( const char* name, double* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1359 | const XMLAttribute* a = FindAttribute( name );
|
| 1360 | if ( !a ) {
|
| 1361 | return XML_NO_ATTRIBUTE;
|
| 1362 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1363 | return a->QueryDoubleValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1364 | }
|
| 1365 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1366 | XMLError QueryFloatAttribute( const char* name, float* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1367 | const XMLAttribute* a = FindAttribute( name );
|
| 1368 | if ( !a ) {
|
| 1369 | return XML_NO_ATTRIBUTE;
|
| 1370 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1371 | return a->QueryFloatValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1372 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1373 |
|
Lee Thomason | 5b00e06 | 2017-12-28 14:07:47 -0800 | [diff] [blame] | 1374 | /// See QueryIntAttribute()
|
| 1375 | XMLError QueryStringAttribute(const char* name, const char** value) const {
|
| 1376 | const XMLAttribute* a = FindAttribute(name);
|
| 1377 | if (!a) {
|
| 1378 | return XML_NO_ATTRIBUTE;
|
| 1379 | }
|
| 1380 | *value = a->Value();
|
| 1381 | return XML_SUCCESS;
|
| 1382 | }
|
| 1383 |
|
| 1384 |
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1385 |
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1386 | /** Given an attribute name, QueryAttribute() returns
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1387 | XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1388 | can't be performed, or XML_NO_ATTRIBUTE if the attribute
|
| 1389 | doesn't exist. It is overloaded for the primitive types,
|
| 1390 | and is a generally more convenient replacement of
|
| 1391 | QueryIntAttribute() and related functions.
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1392 |
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1393 | If successful, the result of the conversion
|
| 1394 | will be written to 'value'. If not successful, nothing will
|
| 1395 | be written to 'value'. This allows you to provide default
|
| 1396 | value:
|
| 1397 |
|
| 1398 | @verbatim
|
| 1399 | int value = 10;
|
| 1400 | QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
|
| 1401 | @endverbatim
|
| 1402 | */
|
Lee Thomason | fc80df3 | 2018-06-29 15:37:29 -0700 | [diff] [blame] | 1403 | XMLError QueryAttribute( const char* name, int* value ) const {
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1404 | return QueryIntAttribute( name, value );
|
| 1405 | }
|
| 1406 |
|
Lee Thomason | fc80df3 | 2018-06-29 15:37:29 -0700 | [diff] [blame] | 1407 | XMLError QueryAttribute( const char* name, unsigned int* value ) const {
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1408 | return QueryUnsignedAttribute( name, value );
|
| 1409 | }
|
| 1410 |
|
Lee Thomason | fc80df3 | 2018-06-29 15:37:29 -0700 | [diff] [blame] | 1411 | XMLError QueryAttribute(const char* name, int64_t* value) const {
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1412 | return QueryInt64Attribute(name, value);
|
| 1413 | }
|
| 1414 |
|
Lee Thomason | fc80df3 | 2018-06-29 15:37:29 -0700 | [diff] [blame] | 1415 | XMLError QueryAttribute( const char* name, bool* value ) const {
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1416 | return QueryBoolAttribute( name, value );
|
| 1417 | }
|
| 1418 |
|
Lee Thomason | fc80df3 | 2018-06-29 15:37:29 -0700 | [diff] [blame] | 1419 | XMLError QueryAttribute( const char* name, double* value ) const {
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1420 | return QueryDoubleAttribute( name, value );
|
| 1421 | }
|
| 1422 |
|
Lee Thomason | fc80df3 | 2018-06-29 15:37:29 -0700 | [diff] [blame] | 1423 | XMLError QueryAttribute( const char* name, float* value ) const {
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1424 | return QueryFloatAttribute( name, value );
|
| 1425 | }
|
| 1426 |
|
| 1427 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1428 | void SetAttribute( const char* name, const char* value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1429 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1430 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1431 | }
|
| 1432 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1433 | void SetAttribute( const char* name, int value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1434 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1435 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1436 | }
|
| 1437 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1438 | void SetAttribute( const char* name, unsigned value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1439 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1440 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1441 | }
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1442 |
|
| 1443 | /// Sets the named attribute to value.
|
| 1444 | void SetAttribute(const char* name, int64_t value) {
|
| 1445 | XMLAttribute* a = FindOrCreateAttribute(name);
|
| 1446 | a->SetAttribute(value);
|
| 1447 | }
|
| 1448 |
|
| 1449 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1450 | void SetAttribute( const char* name, bool value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1451 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1452 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1453 | }
|
| 1454 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1455 | void SetAttribute( const char* name, double value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1456 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1457 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1458 | }
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 1459 | /// Sets the named attribute to value.
|
| 1460 | void SetAttribute( const char* name, float value ) {
|
| 1461 | XMLAttribute* a = FindOrCreateAttribute( name );
|
| 1462 | a->SetAttribute( value );
|
| 1463 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1464 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1465 | /**
|
| 1466 | Delete an attribute.
|
| 1467 | */
|
| 1468 | void DeleteAttribute( const char* name );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1469 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1470 | /// Return the first attribute in the list.
|
| 1471 | const XMLAttribute* FirstAttribute() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1472 | return _rootAttribute;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1473 | }
|
| 1474 | /// Query a specific attribute in the list.
|
| 1475 | const XMLAttribute* FindAttribute( const char* name ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1476 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1477 | /** Convenience function for easy access to the text inside an element. Although easy
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1478 | and concise, GetText() is limited compared to getting the XMLText child
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1479 | and accessing it directly.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1480 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1481 | If the first child of 'this' is a XMLText, the GetText()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1482 | returns the character string of the Text node, else null is returned.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1483 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1484 | This is a convenient method for getting the text of simple contained text:
|
| 1485 | @verbatim
|
| 1486 | <foo>This is text</foo>
|
| 1487 | const char* str = fooElement->GetText();
|
| 1488 | @endverbatim
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1489 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1490 | 'str' will be a pointer to "This is text".
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1491 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1492 | Note that this function can be misleading. If the element foo was created from
|
| 1493 | this XML:
|
| 1494 | @verbatim
|
| 1495 | <foo><b>This is text</b></foo>
|
| 1496 | @endverbatim
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1497 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1498 | then the value of str would be null. The first child node isn't a text node, it is
|
| 1499 | another element. From this XML:
|
| 1500 | @verbatim
|
| 1501 | <foo>This is <b>text</b></foo>
|
| 1502 | @endverbatim
|
| 1503 | GetText() will return "This is ".
|
| 1504 | */
|
| 1505 | const char* GetText() const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1506 |
|
Uli Kusterer | 85fff5e | 2014-01-21 01:35:30 +0100 | [diff] [blame] | 1507 | /** Convenience function for easy access to the text inside an element. Although easy
|
| 1508 | and concise, SetText() is limited compared to creating an XMLText child
|
| 1509 | and mutating it directly.
|
| 1510 |
|
| 1511 | If the first child of 'this' is a XMLText, SetText() sets its value to
|
| 1512 | the given string, otherwise it will create a first child that is an XMLText.
|
| 1513 |
|
| 1514 | This is a convenient method for setting the text of simple contained text:
|
| 1515 | @verbatim
|
| 1516 | <foo>This is text</foo>
|
| 1517 | fooElement->SetText( "Hullaballoo!" );
|
| 1518 | <foo>Hullaballoo!</foo>
|
| 1519 | @endverbatim
|
| 1520 |
|
| 1521 | Note that this function can be misleading. If the element foo was created from
|
| 1522 | this XML:
|
| 1523 | @verbatim
|
| 1524 | <foo><b>This is text</b></foo>
|
| 1525 | @endverbatim
|
| 1526 |
|
| 1527 | then it will not change "This is text", but rather prefix it with a text element:
|
| 1528 | @verbatim
|
| 1529 | <foo>Hullaballoo!<b>This is text</b></foo>
|
| 1530 | @endverbatim
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1531 |
|
Uli Kusterer | 85fff5e | 2014-01-21 01:35:30 +0100 | [diff] [blame] | 1532 | For this XML:
|
| 1533 | @verbatim
|
| 1534 | <foo />
|
| 1535 | @endverbatim
|
| 1536 | SetText() will generate
|
| 1537 | @verbatim
|
| 1538 | <foo>Hullaballoo!</foo>
|
| 1539 | @endverbatim
|
| 1540 | */
|
Lee Thomason | 5bb2d80 | 2014-01-24 10:42:57 -0800 | [diff] [blame] | 1541 | void SetText( const char* inText );
|
Dmitry-Me | 9e9c85b | 2015-10-19 18:04:46 +0300 | [diff] [blame] | 1542 | /// Convenience method for setting text inside an element. See SetText() for important limitations.
|
Lee Thomason | 5bb2d80 | 2014-01-24 10:42:57 -0800 | [diff] [blame] | 1543 | void SetText( int value );
|
Dmitry-Me | 9e9c85b | 2015-10-19 18:04:46 +0300 | [diff] [blame] | 1544 | /// Convenience method for setting text inside an element. See SetText() for important limitations.
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1545 | void SetText( unsigned value );
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1546 | /// Convenience method for setting text inside an element. See SetText() for important limitations.
|
| 1547 | void SetText(int64_t value);
|
| 1548 | /// Convenience method for setting text inside an element. See SetText() for important limitations.
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1549 | void SetText( bool value );
|
Dmitry-Me | 9e9c85b | 2015-10-19 18:04:46 +0300 | [diff] [blame] | 1550 | /// Convenience method for setting text inside an element. See SetText() for important limitations.
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1551 | void SetText( double value );
|
Dmitry-Me | 9e9c85b | 2015-10-19 18:04:46 +0300 | [diff] [blame] | 1552 | /// Convenience method for setting text inside an element. See SetText() for important limitations.
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1553 | void SetText( float value );
|
Uli Kusterer | 8fe342a | 2014-01-21 01:12:47 +0100 | [diff] [blame] | 1554 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1555 | /**
|
| 1556 | Convenience method to query the value of a child text node. This is probably best
|
| 1557 | shown by example. Given you have a document is this form:
|
| 1558 | @verbatim
|
| 1559 | <point>
|
| 1560 | <x>1</x>
|
| 1561 | <y>1.4</y>
|
| 1562 | </point>
|
| 1563 | @endverbatim
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1564 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1565 | The QueryIntText() and similar functions provide a safe and easier way to get to the
|
| 1566 | "value" of x and y.
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1567 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1568 | @verbatim
|
| 1569 | int x = 0;
|
| 1570 | float y = 0; // types of x and y are contrived for example
|
| 1571 | const XMLElement* xElement = pointElement->FirstChildElement( "x" );
|
| 1572 | const XMLElement* yElement = pointElement->FirstChildElement( "y" );
|
| 1573 | xElement->QueryIntText( &x );
|
| 1574 | yElement->QueryFloatText( &y );
|
| 1575 | @endverbatim
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1576 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1577 | @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
|
| 1578 | to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1579 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1580 | */
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1581 | XMLError QueryIntText( int* ival ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1582 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1583 | XMLError QueryUnsignedText( unsigned* uval ) const;
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1584 | /// See QueryIntText()
|
| 1585 | XMLError QueryInt64Text(int64_t* uval) const;
|
| 1586 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1587 | XMLError QueryBoolText( bool* bval ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1588 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1589 | XMLError QueryDoubleText( double* dval ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1590 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1591 | XMLError QueryFloatText( float* fval ) const;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1592 |
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1593 | int IntText(int defaultValue = 0) const;
|
| 1594 |
|
Josh Wittner | 3a621f5 | 2016-09-12 19:17:54 -0700 | [diff] [blame] | 1595 | /// See QueryIntText()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1596 | unsigned UnsignedText(unsigned defaultValue = 0) const;
|
Josh Wittner | 3a621f5 | 2016-09-12 19:17:54 -0700 | [diff] [blame] | 1597 | /// See QueryIntText()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1598 | int64_t Int64Text(int64_t defaultValue = 0) const;
|
Josh Wittner | 3a621f5 | 2016-09-12 19:17:54 -0700 | [diff] [blame] | 1599 | /// See QueryIntText()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1600 | bool BoolText(bool defaultValue = false) const;
|
Josh Wittner | 3a621f5 | 2016-09-12 19:17:54 -0700 | [diff] [blame] | 1601 | /// See QueryIntText()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1602 | double DoubleText(double defaultValue = 0) const;
|
Josh Wittner | 3a621f5 | 2016-09-12 19:17:54 -0700 | [diff] [blame] | 1603 | /// See QueryIntText()
|
Josh Wittner | cf3dd09 | 2016-10-11 18:57:17 -0700 | [diff] [blame] | 1604 | float FloatText(float defaultValue = 0) const;
|
Josh Wittner | 3a621f5 | 2016-09-12 19:17:54 -0700 | [diff] [blame] | 1605 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1606 | // internal:
|
Dmitry-Me | e503563 | 2017-04-05 18:02:40 +0300 | [diff] [blame] | 1607 | enum ElementClosingType {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1608 | OPEN, // <foo>
|
| 1609 | CLOSED, // <foo/>
|
| 1610 | CLOSING // </foo>
|
| 1611 | };
|
Dmitry-Me | e503563 | 2017-04-05 18:02:40 +0300 | [diff] [blame] | 1612 | ElementClosingType ClosingType() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1613 | return _closingType;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1614 | }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1615 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 1616 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1617 |
|
Dmitry-Me | 9b0f177 | 2015-04-03 10:37:31 +0300 | [diff] [blame] | 1618 | protected:
|
Dmitry-Me | 10b8ecc | 2017-04-12 17:57:44 +0300 | [diff] [blame] | 1619 | char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr );
|
Dmitry-Me | 9b0f177 | 2015-04-03 10:37:31 +0300 | [diff] [blame] | 1620 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1621 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1622 | XMLElement( XMLDocument* doc );
|
| 1623 | virtual ~XMLElement();
|
| 1624 | XMLElement( const XMLElement& ); // not supported
|
| 1625 | void operator=( const XMLElement& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1626 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1627 | XMLAttribute* FindOrCreateAttribute( const char* name );
|
kezenator | 4f75616 | 2016-11-29 19:46:27 +1000 | [diff] [blame] | 1628 | char* ParseAttributes( char* p, int* curLineNumPtr );
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 1629 | static void DeleteAttribute( XMLAttribute* attribute );
|
Dmitry-Me | a60caa2 | 2016-11-22 18:28:08 +0300 | [diff] [blame] | 1630 | XMLAttribute* CreateAttribute();
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1631 |
|
Lee Thomason | 5bb2d80 | 2014-01-24 10:42:57 -0800 | [diff] [blame] | 1632 | enum { BUF_SIZE = 200 };
|
Dmitry-Me | e503563 | 2017-04-05 18:02:40 +0300 | [diff] [blame] | 1633 | ElementClosingType _closingType;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1634 | // The attribute list is ordered; there is no 'lastAttribute'
|
| 1635 | // because the list needs to be scanned for dupes before adding
|
| 1636 | // a new attribute.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1637 | XMLAttribute* _rootAttribute;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1638 | };
|
| 1639 |
|
| 1640 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 1641 | enum Whitespace {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1642 | PRESERVE_WHITESPACE,
|
| 1643 | COLLAPSE_WHITESPACE
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1644 | };
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 1645 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1646 |
|
| 1647 | /** A Document binds together all the functionality.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1648 | It can be saved, loaded, and printed to the screen.
|
| 1649 | All Nodes are connected and allocated to a Document.
|
| 1650 | If the Document is deleted, all its Nodes are also deleted.
|
| 1651 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1652 | class TINYXML2_LIB XMLDocument : public XMLNode
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1653 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1654 | friend class XMLElement;
|
Lee Thomason | d946dda | 2018-04-05 09:11:08 -0700 | [diff] [blame] | 1655 | // Gives access to SetError and Push/PopDepth, but over-access for everything else.
|
Lee Thomason | f49b965 | 2017-10-11 10:57:49 -0700 | [diff] [blame] | 1656 | // Wishing C++ had "internal" scope.
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1657 | friend class XMLNode;
|
Lee Thomason | f49b965 | 2017-10-11 10:57:49 -0700 | [diff] [blame] | 1658 | friend class XMLText;
|
| 1659 | friend class XMLComment;
|
| 1660 | friend class XMLDeclaration;
|
| 1661 | friend class XMLUnknown;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1662 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1663 | /// constructor
|
Dmitry-Me | ae8a82a | 2017-03-03 15:40:32 +0300 | [diff] [blame] | 1664 | XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1665 | ~XMLDocument();
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1666 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1667 | virtual XMLDocument* ToDocument() {
|
Dmitry-Me | 66487eb | 2015-07-20 18:21:04 +0300 | [diff] [blame] | 1668 | TIXMLASSERT( this == _document );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1669 | return this;
|
| 1670 | }
|
| 1671 | virtual const XMLDocument* ToDocument() const {
|
Dmitry-Me | 66487eb | 2015-07-20 18:21:04 +0300 | [diff] [blame] | 1672 | TIXMLASSERT( this == _document );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1673 | return this;
|
| 1674 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1675 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1676 | /**
|
| 1677 | Parse an XML file from a character string.
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1678 | Returns XML_SUCCESS (0) on success, or
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1679 | an errorID.
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1680 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1681 | You may optionally pass in the 'nBytes', which is
|
| 1682 | the number of bytes which will be parsed. If not
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1683 | specified, TinyXML-2 will assume 'xml' points to a
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1684 | null terminated string.
|
| 1685 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1686 | XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1687 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1688 | /**
|
| 1689 | Load an XML file from disk.
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1690 | Returns XML_SUCCESS (0) on success, or
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1691 | an errorID.
|
| 1692 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1693 | XMLError LoadFile( const char* filename );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1694 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1695 | /**
|
| 1696 | Load an XML file from disk. You are responsible
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1697 | for providing and closing the FILE*.
|
| 1698 |
|
Lee Thomason | cd011bc | 2014-12-17 10:41:34 -0800 | [diff] [blame] | 1699 | NOTE: The file should be opened as binary ("rb")
|
| 1700 | not text in order for TinyXML-2 to correctly
|
| 1701 | do newline normalization.
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1702 |
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1703 | Returns XML_SUCCESS (0) on success, or
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1704 | an errorID.
|
| 1705 | */
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 1706 | XMLError LoadFile( FILE* );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1707 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1708 | /**
|
| 1709 | Save the XML file to disk.
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1710 | Returns XML_SUCCESS (0) on success, or
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1711 | an errorID.
|
| 1712 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1713 | XMLError SaveFile( const char* filename, bool compact = false );
|
Lee Thomason | d11cd16 | 2012-04-12 08:35:36 -0700 | [diff] [blame] | 1714 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1715 | /**
|
| 1716 | Save the XML file to disk. You are responsible
|
| 1717 | for providing and closing the FILE*.
|
Ken Miller | 81da1fb | 2012-04-09 23:32:26 -0500 | [diff] [blame] | 1718 |
|
Benjamin Doherty | 3b9cf99 | 2016-09-23 18:42:23 -0600 | [diff] [blame] | 1719 | Returns XML_SUCCESS (0) on success, or
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1720 | an errorID.
|
| 1721 | */
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 1722 | XMLError SaveFile( FILE* fp, bool compact = false );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1723 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1724 | bool ProcessEntities() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1725 | return _processEntities;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1726 | }
|
| 1727 | Whitespace WhitespaceMode() const {
|
Dmitry-Me | ae8a82a | 2017-03-03 15:40:32 +0300 | [diff] [blame] | 1728 | return _whitespaceMode;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1729 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1730 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1731 | /**
|
| 1732 | Returns true if this document has a leading Byte Order Mark of UTF8.
|
| 1733 | */
|
| 1734 | bool HasBOM() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1735 | return _writeBOM;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1736 | }
|
| 1737 | /** Sets whether to write the BOM when writing the file.
|
| 1738 | */
|
| 1739 | void SetBOM( bool useBOM ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1740 | _writeBOM = useBOM;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1741 | }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1742 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1743 | /** Return the root element of DOM. Equivalent to FirstChildElement().
|
| 1744 | To get the first node, use FirstChild().
|
| 1745 | */
|
| 1746 | XMLElement* RootElement() {
|
| 1747 | return FirstChildElement();
|
| 1748 | }
|
| 1749 | const XMLElement* RootElement() const {
|
| 1750 | return FirstChildElement();
|
| 1751 | }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1752 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1753 | /** Print the Document. If the Printer is not provided, it will
|
| 1754 | print to stdout. If you provide Printer, this can print to a file:
|
| 1755 | @verbatim
|
| 1756 | XMLPrinter printer( fp );
|
| 1757 | doc.Print( &printer );
|
| 1758 | @endverbatim
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1759 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1760 | Or you can use a printer to print to memory:
|
| 1761 | @verbatim
|
| 1762 | XMLPrinter printer;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1763 | doc.Print( &printer );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1764 | // printer.CStr() has a const char* to the XML
|
| 1765 | @endverbatim
|
| 1766 | */
|
PKEuS | 1c5f99e | 2013-07-06 11:28:39 +0200 | [diff] [blame] | 1767 | void Print( XMLPrinter* streamer=0 ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1768 | virtual bool Accept( XMLVisitor* visitor ) const;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1769 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1770 | /**
|
| 1771 | Create a new Element associated with
|
| 1772 | this Document. The memory for the Element
|
| 1773 | is managed by the Document.
|
| 1774 | */
|
| 1775 | XMLElement* NewElement( const char* name );
|
| 1776 | /**
|
| 1777 | Create a new Comment associated with
|
| 1778 | this Document. The memory for the Comment
|
| 1779 | is managed by the Document.
|
| 1780 | */
|
| 1781 | XMLComment* NewComment( const char* comment );
|
| 1782 | /**
|
| 1783 | Create a new Text associated with
|
| 1784 | this Document. The memory for the Text
|
| 1785 | is managed by the Document.
|
| 1786 | */
|
| 1787 | XMLText* NewText( const char* text );
|
| 1788 | /**
|
| 1789 | Create a new Declaration associated with
|
| 1790 | this Document. The memory for the object
|
| 1791 | is managed by the Document.
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1792 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1793 | If the 'text' param is null, the standard
|
| 1794 | declaration is used.:
|
| 1795 | @verbatim
|
| 1796 | <?xml version="1.0" encoding="UTF-8"?>
|
| 1797 | @endverbatim
|
| 1798 | */
|
| 1799 | XMLDeclaration* NewDeclaration( const char* text=0 );
|
| 1800 | /**
|
| 1801 | Create a new Unknown associated with
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 1802 | this Document. The memory for the object
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1803 | is managed by the Document.
|
| 1804 | */
|
| 1805 | XMLUnknown* NewUnknown( const char* text );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1806 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1807 | /**
|
| 1808 | Delete a node associated with this document.
|
| 1809 | It will be unlinked from the DOM.
|
| 1810 | */
|
Lee Thomason | cd011bc | 2014-12-17 10:41:34 -0800 | [diff] [blame] | 1811 | void DeleteNode( XMLNode* node );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1812 |
|
Dmitry-Me | 0d2cef0 | 2016-11-25 18:39:52 +0300 | [diff] [blame] | 1813 | void ClearError() {
|
Lee Thomason | aa18839 | 2017-09-19 17:54:31 -0700 | [diff] [blame] | 1814 | SetError(XML_SUCCESS, 0, 0);
|
Dmitry-Me | 0d2cef0 | 2016-11-25 18:39:52 +0300 | [diff] [blame] | 1815 | }
|
| 1816 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1817 | /// Return true if there was an error parsing the document.
|
| 1818 | bool Error() const {
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1819 | return _errorID != XML_SUCCESS;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1820 | }
|
| 1821 | /// Return the errorID.
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1822 | XMLError ErrorID() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1823 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1824 | }
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 1825 | const char* ErrorName() const;
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1826 | static const char* ErrorIDToName(XMLError errorID);
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 1827 |
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1828 | /** Returns a "long form" error description. A hopefully helpful
|
Lee Thomason | f49b965 | 2017-10-11 10:57:49 -0700 | [diff] [blame] | 1829 | diagnostic with location, line number, and/or additional info.
|
| 1830 | */
|
| 1831 | const char* ErrorStr() const;
|
| 1832 |
|
| 1833 | /// A (trivial) utility function that prints the ErrorStr() to stdout.
|
| 1834 | void PrintError() const;
|
Lee Thomason | 8c9e313 | 2017-06-26 16:55:01 -0700 | [diff] [blame] | 1835 |
|
orbitcowboy | 22b21ec | 2018-07-17 11:52:57 +0200 | [diff] [blame^] | 1836 | /// Return the line where the error occurred, or zero if unknown.
|
Lee Thomason | f49b965 | 2017-10-11 10:57:49 -0700 | [diff] [blame] | 1837 | int ErrorLineNum() const
|
kezenator | ec69415 | 2016-11-26 17:21:43 +1000 | [diff] [blame] | 1838 | {
|
| 1839 | return _errorLineNum;
|
| 1840 | }
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1841 |
|
Martinsh Shaiters | a9d42b0 | 2013-01-30 11:14:27 +0200 | [diff] [blame] | 1842 | /// Clear the document, resetting it to the initial state.
|
| 1843 | void Clear();
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1844 |
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 1845 | /**
|
Lee Thomason | b29f556 | 2017-06-01 18:45:32 -0700 | [diff] [blame] | 1846 | Copies this document to a target document.
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 1847 | The target will be completely cleared before the copy.
|
Lee Thomason | b29f556 | 2017-06-01 18:45:32 -0700 | [diff] [blame] | 1848 | If you want to copy a sub-tree, see XMLNode::DeepClone().
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 1849 |
|
Lee Thomason | 1346a17 | 2017-06-14 15:14:19 -0700 | [diff] [blame] | 1850 | NOTE: that the 'target' must be non-null.
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 1851 | */
|
Shuichiro Suzuki | 87cd4e0 | 2017-08-24 11:20:26 +0900 | [diff] [blame] | 1852 | void DeepCopy(XMLDocument* target) const;
|
Lee Thomason | 7085f00 | 2017-06-01 18:09:43 -0700 | [diff] [blame] | 1853 |
|
| 1854 | // internal
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1855 | char* Identify( char* p, XMLNode** node );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1856 |
|
Lee Thomason | 816d3fa | 2017-06-05 14:35:55 -0700 | [diff] [blame] | 1857 | // internal
|
| 1858 | void MarkInUse(XMLNode*);
|
| 1859 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1860 | virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
|
| 1861 | return 0;
|
| 1862 | }
|
| 1863 | virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
|
| 1864 | return false;
|
| 1865 | }
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1866 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1867 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1868 | XMLDocument( const XMLDocument& ); // not supported
|
| 1869 | void operator=( const XMLDocument& ); // not supported
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1870 |
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1871 | bool _writeBOM;
|
| 1872 | bool _processEntities;
|
| 1873 | XMLError _errorID;
|
Dmitry-Me | ae8a82a | 2017-03-03 15:40:32 +0300 | [diff] [blame] | 1874 | Whitespace _whitespaceMode;
|
Lee Thomason | aa18839 | 2017-09-19 17:54:31 -0700 | [diff] [blame] | 1875 | mutable StrPair _errorStr;
|
Lee Thomason | e90e901 | 2016-12-24 07:34:39 -0800 | [diff] [blame] | 1876 | int _errorLineNum;
|
| 1877 | char* _charBuffer;
|
| 1878 | int _parseCurLineNum;
|
Lee Thomason | d946dda | 2018-04-05 09:11:08 -0700 | [diff] [blame] | 1879 | int _parsingDepth;
|
Lee Thomason | 816d3fa | 2017-06-05 14:35:55 -0700 | [diff] [blame] | 1880 | // Memory tracking does add some overhead.
|
| 1881 | // However, the code assumes that you don't
|
| 1882 | // have a bunch of unlinked nodes around.
|
| 1883 | // Therefore it takes less memory to track
|
| 1884 | // in the document vs. a linked list in the XMLNode,
|
| 1885 | // and the performance is the same.
|
| 1886 | DynArray<XMLNode*, 10> _unlinked;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1887 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1888 | MemPoolT< sizeof(XMLElement) > _elementPool;
|
| 1889 | MemPoolT< sizeof(XMLAttribute) > _attributePool;
|
| 1890 | MemPoolT< sizeof(XMLText) > _textPool;
|
| 1891 | MemPoolT< sizeof(XMLComment) > _commentPool;
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 1892 |
|
| 1893 | static const char* _errorNames[XML_ERROR_COUNT];
|
Dmitry-Me | 97476b7 | 2015-01-01 16:15:57 +0300 | [diff] [blame] | 1894 |
|
| 1895 | void Parse();
|
Dmitry-Me | 2aebfb7 | 2017-02-27 15:53:40 +0300 | [diff] [blame] | 1896 |
|
Lee Thomason | f49b965 | 2017-10-11 10:57:49 -0700 | [diff] [blame] | 1897 | void SetError( XMLError error, int lineNum, const char* format, ... );
|
| 1898 |
|
Lee Thomason | d946dda | 2018-04-05 09:11:08 -0700 | [diff] [blame] | 1899 | // Something of an obvious security hole, once it was discovered.
|
| 1900 | // Either an ill-formed XML or an excessively deep one can overflow
|
| 1901 | // the stack. Track stack depth, and error out if needed.
|
| 1902 | class DepthTracker {
|
| 1903 | public:
|
Lee Thomason | 70d942e | 2018-06-29 15:31:59 -0700 | [diff] [blame] | 1904 | DepthTracker(XMLDocument * document) {
|
| 1905 | this->_document = document;
|
Lee Thomason | d946dda | 2018-04-05 09:11:08 -0700 | [diff] [blame] | 1906 | document->PushDepth();
|
| 1907 | }
|
| 1908 | ~DepthTracker() {
|
| 1909 | _document->PopDepth();
|
| 1910 | }
|
| 1911 | private:
|
| 1912 | XMLDocument * _document;
|
| 1913 | };
|
Lee Thomason | f928c35 | 2018-04-05 09:24:20 -0700 | [diff] [blame] | 1914 | void PushDepth();
|
| 1915 | void PopDepth();
|
Lee Thomason | d946dda | 2018-04-05 09:11:08 -0700 | [diff] [blame] | 1916 |
|
Dmitry-Me | 2aebfb7 | 2017-02-27 15:53:40 +0300 | [diff] [blame] | 1917 | template<class NodeType, int PoolElementSize>
|
| 1918 | NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1919 | };
|
| 1920 |
|
Dmitry-Me | 2aebfb7 | 2017-02-27 15:53:40 +0300 | [diff] [blame] | 1921 | template<class NodeType, int PoolElementSize>
|
| 1922 | inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
|
| 1923 | {
|
| 1924 | TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
|
| 1925 | TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
|
| 1926 | NodeType* returnNode = new (pool.Alloc()) NodeType( this );
|
| 1927 | TIXMLASSERT( returnNode );
|
| 1928 | returnNode->_memPool = &pool;
|
Lee Thomason | 816d3fa | 2017-06-05 14:35:55 -0700 | [diff] [blame] | 1929 |
|
| 1930 | _unlinked.Push(returnNode);
|
Dmitry-Me | 2aebfb7 | 2017-02-27 15:53:40 +0300 | [diff] [blame] | 1931 | return returnNode;
|
| 1932 | }
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1933 |
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1934 | /**
|
| 1935 | A XMLHandle is a class that wraps a node pointer with null checks; this is
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1936 | an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1937 | DOM structure. It is a separate utility class.
|
| 1938 |
|
| 1939 | Take an example:
|
| 1940 | @verbatim
|
| 1941 | <Document>
|
| 1942 | <Element attributeA = "valueA">
|
| 1943 | <Child attributeB = "value1" />
|
| 1944 | <Child attributeB = "value2" />
|
| 1945 | </Element>
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 1946 | </Document>
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1947 | @endverbatim
|
| 1948 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1949 | Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1950 | easy to write a *lot* of code that looks like:
|
| 1951 |
|
| 1952 | @verbatim
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1953 | XMLElement* root = document.FirstChildElement( "Document" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1954 | if ( root )
|
| 1955 | {
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1956 | XMLElement* element = root->FirstChildElement( "Element" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1957 | if ( element )
|
| 1958 | {
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1959 | XMLElement* child = element->FirstChildElement( "Child" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1960 | if ( child )
|
| 1961 | {
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1962 | XMLElement* child2 = child->NextSiblingElement( "Child" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1963 | if ( child2 )
|
| 1964 | {
|
| 1965 | // Finally do something useful.
|
| 1966 | @endverbatim
|
| 1967 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1968 | And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1969 | of such code. A XMLHandle checks for null pointers so it is perfectly safe
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1970 | and correct to use:
|
| 1971 |
|
| 1972 | @verbatim
|
Lee Thomason | db0bbb6 | 2012-04-04 15:47:04 -0700 | [diff] [blame] | 1973 | XMLHandle docHandle( &document );
|
Dmitry-Me | a317bd6 | 2014-12-08 10:35:37 +0300 | [diff] [blame] | 1974 | XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1975 | if ( child2 )
|
| 1976 | {
|
| 1977 | // do something useful
|
| 1978 | @endverbatim
|
| 1979 |
|
| 1980 | Which is MUCH more concise and useful.
|
| 1981 |
|
| 1982 | It is also safe to copy handles - internally they are nothing more than node pointers.
|
| 1983 | @verbatim
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1984 | XMLHandle handleCopy = handle;
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1985 | @endverbatim
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1986 |
|
| 1987 | See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1988 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1989 | class TINYXML2_LIB XMLHandle
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1990 | {
|
| 1991 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1992 | /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 1993 | XMLHandle( XMLNode* node ) : _node( node ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1994 | }
|
| 1995 | /// Create a handle from a node.
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 1996 | XMLHandle( XMLNode& node ) : _node( &node ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1997 | }
|
| 1998 | /// Copy constructor
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 1999 | XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2000 | }
|
| 2001 | /// Assignment
|
| 2002 | XMLHandle& operator=( const XMLHandle& ref ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2003 | _node = ref._node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2004 | return *this;
|
| 2005 | }
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 2006 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2007 | /// Get the first child of this handle.
|
| 2008 | XMLHandle FirstChild() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2009 | return XMLHandle( _node ? _node->FirstChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2010 | }
|
| 2011 | /// Get the first child element of this handle.
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2012 | XMLHandle FirstChildElement( const char* name = 0 ) {
|
| 2013 | return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2014 | }
|
| 2015 | /// Get the last child of this handle.
|
| 2016 | XMLHandle LastChild() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2017 | return XMLHandle( _node ? _node->LastChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2018 | }
|
| 2019 | /// Get the last child element of this handle.
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2020 | XMLHandle LastChildElement( const char* name = 0 ) {
|
| 2021 | return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2022 | }
|
| 2023 | /// Get the previous sibling of this handle.
|
| 2024 | XMLHandle PreviousSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2025 | return XMLHandle( _node ? _node->PreviousSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2026 | }
|
| 2027 | /// Get the previous sibling element of this handle.
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2028 | XMLHandle PreviousSiblingElement( const char* name = 0 ) {
|
| 2029 | return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2030 | }
|
| 2031 | /// Get the next sibling of this handle.
|
| 2032 | XMLHandle NextSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2033 | return XMLHandle( _node ? _node->NextSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2034 | }
|
| 2035 | /// Get the next sibling element of this handle.
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2036 | XMLHandle NextSiblingElement( const char* name = 0 ) {
|
| 2037 | return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2038 | }
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 2039 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2040 | /// Safe cast to XMLNode. This can return null.
|
| 2041 | XMLNode* ToNode() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2042 | return _node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2043 | }
|
| 2044 | /// Safe cast to XMLElement. This can return null.
|
| 2045 | XMLElement* ToElement() {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2046 | return ( _node ? _node->ToElement() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2047 | }
|
| 2048 | /// Safe cast to XMLText. This can return null.
|
| 2049 | XMLText* ToText() {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2050 | return ( _node ? _node->ToText() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2051 | }
|
| 2052 | /// Safe cast to XMLUnknown. This can return null.
|
| 2053 | XMLUnknown* ToUnknown() {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2054 | return ( _node ? _node->ToUnknown() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2055 | }
|
| 2056 | /// Safe cast to XMLDeclaration. This can return null.
|
| 2057 | XMLDeclaration* ToDeclaration() {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2058 | return ( _node ? _node->ToDeclaration() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2059 | }
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 2060 |
|
| 2061 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2062 | XMLNode* _node;
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 2063 | };
|
| 2064 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2065 |
|
| 2066 | /**
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 2067 | A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
|
| 2068 | same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 2069 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 2070 | class TINYXML2_LIB XMLConstHandle
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 2071 | {
|
| 2072 | public:
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 2073 | XMLConstHandle( const XMLNode* node ) : _node( node ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2074 | }
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 2075 | XMLConstHandle( const XMLNode& node ) : _node( &node ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2076 | }
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 2077 | XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2078 | }
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 2079 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2080 | XMLConstHandle& operator=( const XMLConstHandle& ref ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2081 | _node = ref._node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2082 | return *this;
|
| 2083 | }
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 2084 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2085 | const XMLConstHandle FirstChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2086 | return XMLConstHandle( _node ? _node->FirstChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2087 | }
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2088 | const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
|
| 2089 | return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2090 | }
|
| 2091 | const XMLConstHandle LastChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2092 | return XMLConstHandle( _node ? _node->LastChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2093 | }
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2094 | const XMLConstHandle LastChildElement( const char* name = 0 ) const {
|
| 2095 | return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2096 | }
|
| 2097 | const XMLConstHandle PreviousSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2098 | return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2099 | }
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2100 | const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
|
| 2101 | return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2102 | }
|
| 2103 | const XMLConstHandle NextSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2104 | return XMLConstHandle( _node ? _node->NextSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2105 | }
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 2106 | const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
|
| 2107 | return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2108 | }
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 2109 |
|
| 2110 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2111 | const XMLNode* ToNode() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2112 | return _node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2113 | }
|
| 2114 | const XMLElement* ToElement() const {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2115 | return ( _node ? _node->ToElement() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2116 | }
|
| 2117 | const XMLText* ToText() const {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2118 | return ( _node ? _node->ToText() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2119 | }
|
| 2120 | const XMLUnknown* ToUnknown() const {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2121 | return ( _node ? _node->ToUnknown() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2122 | }
|
| 2123 | const XMLDeclaration* ToDeclaration() const {
|
Dmitry-Me | ebb1660 | 2016-11-16 17:22:45 +0300 | [diff] [blame] | 2124 | return ( _node ? _node->ToDeclaration() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2125 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2126 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2127 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2128 | const XMLNode* _node;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 2129 | };
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 2130 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2131 |
|
| 2132 | /**
|
| 2133 | Printing functionality. The XMLPrinter gives you more
|
| 2134 | options than the XMLDocument::Print() method.
|
| 2135 |
|
| 2136 | It can:
|
| 2137 | -# Print to memory.
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 2138 | -# Print to a file you provide.
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2139 | -# Print XML without a XMLDocument.
|
| 2140 |
|
| 2141 | Print to Memory
|
| 2142 |
|
| 2143 | @verbatim
|
| 2144 | XMLPrinter printer;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 2145 | doc.Print( &printer );
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 2146 | SomeFunction( printer.CStr() );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2147 | @endverbatim
|
| 2148 |
|
| 2149 | Print to a File
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2150 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2151 | You provide the file pointer.
|
| 2152 | @verbatim
|
| 2153 | XMLPrinter printer( fp );
|
| 2154 | doc.Print( &printer );
|
| 2155 | @endverbatim
|
| 2156 |
|
| 2157 | Print without a XMLDocument
|
| 2158 |
|
| 2159 | When loading, an XML parser is very useful. However, sometimes
|
| 2160 | when saving, it just gets in the way. The code is often set up
|
| 2161 | for streaming, and constructing the DOM is just overhead.
|
| 2162 |
|
| 2163 | The Printer supports the streaming case. The following code
|
| 2164 | prints out a trivially simple XML file without ever creating
|
| 2165 | an XML document.
|
| 2166 |
|
| 2167 | @verbatim
|
| 2168 | XMLPrinter printer( fp );
|
| 2169 | printer.OpenElement( "foo" );
|
| 2170 | printer.PushAttribute( "foo", "bar" );
|
| 2171 | printer.CloseElement();
|
| 2172 | @endverbatim
|
| 2173 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 2174 | class TINYXML2_LIB XMLPrinter : public XMLVisitor
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2175 | {
|
| 2176 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2177 | /** Construct the printer. If the FILE* is specified,
|
| 2178 | this will print to the FILE. Else it will print
|
| 2179 | to memory, and the result is available in CStr().
|
| 2180 | If 'compact' is set to true, then output is created
|
| 2181 | with only required whitespace and newlines.
|
| 2182 | */
|
PKEuS | 1bfb954 | 2013-08-04 13:51:17 +0200 | [diff] [blame] | 2183 | XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
|
Dennis Jenkins | 59c75d3 | 2013-10-08 13:10:07 -0500 | [diff] [blame] | 2184 | virtual ~XMLPrinter() {}
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2185 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2186 | /** If streaming, write the BOM and declaration. */
|
| 2187 | void PushHeader( bool writeBOM, bool writeDeclaration );
|
| 2188 | /** If streaming, start writing an element.
|
| 2189 | The element must be closed with CloseElement()
|
| 2190 | */
|
Lee Thomason | 256adb6 | 2014-04-06 14:41:46 -0700 | [diff] [blame] | 2191 | void OpenElement( const char* name, bool compactMode=false );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2192 | /// If streaming, add an attribute to an open element.
|
| 2193 | void PushAttribute( const char* name, const char* value );
|
| 2194 | void PushAttribute( const char* name, int value );
|
| 2195 | void PushAttribute( const char* name, unsigned value );
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 2196 | void PushAttribute(const char* name, int64_t value);
|
| 2197 | void PushAttribute( const char* name, bool value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2198 | void PushAttribute( const char* name, double value );
|
| 2199 | /// If streaming, close the Element.
|
Lee Thomason | 256adb6 | 2014-04-06 14:41:46 -0700 | [diff] [blame] | 2200 | virtual void CloseElement( bool compactMode=false );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2201 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2202 | /// Add a text node.
|
| 2203 | void PushText( const char* text, bool cdata=false );
|
| 2204 | /// Add a text node from an integer.
|
| 2205 | void PushText( int value );
|
| 2206 | /// Add a text node from an unsigned.
|
| 2207 | void PushText( unsigned value );
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 2208 | /// Add a text node from an unsigned.
|
| 2209 | void PushText(int64_t value);
|
| 2210 | /// Add a text node from a bool.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2211 | void PushText( bool value );
|
| 2212 | /// Add a text node from a float.
|
| 2213 | void PushText( float value );
|
| 2214 | /// Add a text node from a double.
|
| 2215 | void PushText( double value );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2216 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2217 | /// Add a comment
|
| 2218 | void PushComment( const char* comment );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2219 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2220 | void PushDeclaration( const char* value );
|
| 2221 | void PushUnknown( const char* value );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2222 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2223 | virtual bool VisitEnter( const XMLDocument& /*doc*/ );
|
| 2224 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
|
| 2225 | return true;
|
| 2226 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2227 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2228 | virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
|
| 2229 | virtual bool VisitExit( const XMLElement& element );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2230 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2231 | virtual bool Visit( const XMLText& text );
|
| 2232 | virtual bool Visit( const XMLComment& comment );
|
| 2233 | virtual bool Visit( const XMLDeclaration& declaration );
|
| 2234 | virtual bool Visit( const XMLUnknown& unknown );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2235 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2236 | /**
|
| 2237 | If in print to memory mode, return a pointer to
|
| 2238 | the XML file in memory.
|
| 2239 | */
|
| 2240 | const char* CStr() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2241 | return _buffer.Mem();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2242 | }
|
| 2243 | /**
|
| 2244 | If in print to memory mode, return the size
|
| 2245 | of the XML file in memory. (Note the size returned
|
| 2246 | includes the terminating null.)
|
| 2247 | */
|
| 2248 | int CStrSize() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2249 | return _buffer.Size();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2250 | }
|
Reinhard Klambauer | 3bc3d4e | 2013-11-22 14:05:21 +0100 | [diff] [blame] | 2251 | /**
|
| 2252 | If in print to memory mode, reset the buffer to the
|
| 2253 | beginning.
|
| 2254 | */
|
Lee Thomason | ce0510b | 2013-11-26 21:29:37 -0800 | [diff] [blame] | 2255 | void ClearBuffer() {
|
| 2256 | _buffer.Clear();
|
Reinhard Klambauer | 3bc3d4e | 2013-11-22 14:05:21 +0100 | [diff] [blame] | 2257 | _buffer.Push(0);
|
Lee Thomason | 53858b4 | 2017-06-01 19:09:16 -0700 | [diff] [blame] | 2258 | _firstElement = true;
|
Reinhard Klambauer | 3bc3d4e | 2013-11-22 14:05:21 +0100 | [diff] [blame] | 2259 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2260 |
|
Dennis Jenkins | 59c75d3 | 2013-10-08 13:10:07 -0500 | [diff] [blame] | 2261 | protected:
|
Alexander Mai | d740b64 | 2014-05-20 22:04:42 +0200 | [diff] [blame] | 2262 | virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
|
Uli Kusterer | 5d1d27e | 2014-02-20 11:50:22 +0100 | [diff] [blame] | 2263 |
|
Lee Thomason | c18eb23 | 2014-02-21 17:31:17 -0800 | [diff] [blame] | 2264 | /** Prints out the space before an element. You may override to change
|
| 2265 | the space and tabs used. A PrintSpace() override should call Print().
|
| 2266 | */
|
| 2267 | virtual void PrintSpace( int depth );
|
| 2268 | void Print( const char* format, ... );
|
Alexander Golubev | b2e08e4 | 2016-03-28 19:51:59 +0300 | [diff] [blame] | 2269 | void Write( const char* data, size_t size );
|
| 2270 | inline void Write( const char* data ) { Write( data, strlen( data ) ); }
|
| 2271 | void Putc( char ch );
|
Lee Thomason | c18eb23 | 2014-02-21 17:31:17 -0800 | [diff] [blame] | 2272 |
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2273 | void SealElementIfJustOpened();
|
Dennis Jenkins | 59c75d3 | 2013-10-08 13:10:07 -0500 | [diff] [blame] | 2274 | bool _elementJustOpened;
|
| 2275 | DynArray< const char*, 10 > _stack;
|
| 2276 |
|
| 2277 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2278 | void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2279 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2280 | bool _firstElement;
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 2281 | FILE* _fp;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2282 | int _depth;
|
| 2283 | int _textDepth;
|
| 2284 | bool _processEntities;
|
Lee Thomason | c18eb23 | 2014-02-21 17:31:17 -0800 | [diff] [blame] | 2285 | bool _compactMode;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2286 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2287 | enum {
|
| 2288 | ENTITY_RANGE = 64,
|
| 2289 | BUF_SIZE = 200
|
| 2290 | };
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2291 | bool _entityFlag[ENTITY_RANGE];
|
| 2292 | bool _restrictedEntityFlag[ENTITY_RANGE];
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2293 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2294 | DynArray< char, 20 > _buffer;
|
Thierry Lelegard | 7f0f754 | 2017-09-01 10:14:16 +0200 | [diff] [blame] | 2295 |
|
| 2296 | // Prohibit cloning, intentionally not implemented
|
| 2297 | XMLPrinter( const XMLPrinter& );
|
| 2298 | XMLPrinter& operator=( const XMLPrinter& );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2299 | };
|
| 2300 |
|
| 2301 |
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 2302 | } // tinyxml2
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2303 |
|
PKEuS | 9506035 | 2013-07-26 10:42:44 +0200 | [diff] [blame] | 2304 | #if defined(_MSC_VER)
|
| 2305 | # pragma warning(pop)
|
| 2306 | #endif
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2307 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 2308 | #endif // TINYXML2_INCLUDED
|