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