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 |
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 27 | #if defined(ANDROID_NDK) || defined(__BORLANDC__)
|
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>
|
| 33 | # include <stdarg.h>
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 34 | #else
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 35 | # include <cctype>
|
| 36 | # include <climits>
|
| 37 | # include <cstdio>
|
| 38 | # include <cstdlib>
|
| 39 | # include <cstring>
|
| 40 | # include <cstdarg>
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 41 | #endif
|
Lee Thomason (grinliz) | 6a22be2 | 2012-04-04 12:39:05 -0700 | [diff] [blame] | 42 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 43 | /*
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 44 | TODO: intern strings instead of allocation.
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 45 | */
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 46 | /*
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 47 | gcc:
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 48 | g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 49 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 50 | Formatting, Artistic Style:
|
| 51 | 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] | 52 | */
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 53 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 54 | #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 55 | # ifndef DEBUG
|
| 56 | # define DEBUG
|
| 57 | # endif
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 58 | #endif
|
| 59 |
|
| 60 |
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 61 | #ifdef _WIN32
|
| 62 | # ifdef TINYXML2_EXPORT
|
| 63 | # define TINYXML2_LIB __declspec(dllexport)
|
| 64 | # elif defined(TINYXML2_IMPORT)
|
| 65 | # define TINYXML2_LIB __declspec(dllimport)
|
| 66 | # else
|
| 67 | # define TINYXML2_LIB
|
| 68 | # endif
|
| 69 | #else
|
| 70 | # define TINYXML2_LIB
|
| 71 | #endif
|
| 72 |
|
| 73 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 74 | #if defined(DEBUG)
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 75 | # if defined(_MSC_VER)
|
| 76 | # define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
|
| 77 | # elif defined (ANDROID_NDK)
|
| 78 | # include <android/log.h>
|
| 79 | # define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
|
| 80 | # else
|
| 81 | # include <assert.h>
|
| 82 | # define TIXMLASSERT assert
|
| 83 | # endif
|
| 84 | # else
|
| 85 | # define TIXMLASSERT( x ) {}
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 86 | #endif
|
| 87 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 88 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 89 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 90 | // Microsoft visual studio, version 2005 and higher.
|
| 91 | /*int _snprintf_s(
|
| 92 | char *buffer,
|
| 93 | size_t sizeOfBuffer,
|
| 94 | size_t count,
|
| 95 | const char *format [,
|
| 96 | argument] ...
|
| 97 | );*/
|
| 98 | inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
|
| 99 | {
|
| 100 | va_list va;
|
| 101 | va_start( va, format );
|
| 102 | int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
|
| 103 | va_end( va );
|
| 104 | return result;
|
| 105 | }
|
| 106 | #define TIXML_SSCANF sscanf_s
|
Lee Thomason (grinliz) | b9e791f | 2012-04-06 21:27:10 -0700 | [diff] [blame] | 107 | #else
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 108 | // GCC version 3 and higher
|
| 109 | //#warning( "Using sn* functions." )
|
| 110 | #define TIXML_SNPRINTF snprintf
|
| 111 | #define TIXML_SSCANF sscanf
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 112 | #endif
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 113 |
|
selfpoised | e77e195 | 2013-03-13 14:08:29 +0800 | [diff] [blame] | 114 | static const int TIXML2_MAJOR_VERSION = 1;
|
| 115 | static const int TIXML2_MINOR_VERSION = 0;
|
| 116 | static const int TIXML2_PATCH_VERSION = 11;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 117 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 118 | namespace tinyxml2
|
| 119 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 120 | class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 121 | class XMLElement;
|
| 122 | class XMLAttribute;
|
| 123 | class XMLComment;
|
| 124 | class XMLNode;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 125 | class XMLText;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 126 | class XMLDeclaration;
|
| 127 | class XMLUnknown;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 128 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 129 | class XMLPrinter;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 130 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 131 | /*
|
| 132 | A class that wraps strings. Normally stores the start and end
|
| 133 | pointers into the XML file itself, and will apply normalization
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 134 | and entity translation if actually read. Can also store (and memory
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 135 | manage) a traditional char[]
|
| 136 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 137 | class TINYXML2_LIB StrPair
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 138 | {
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 139 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 140 | enum {
|
| 141 | NEEDS_ENTITY_PROCESSING = 0x01,
|
| 142 | NEEDS_NEWLINE_NORMALIZATION = 0x02,
|
selfpoised | e77e195 | 2013-03-13 14:08:29 +0800 | [diff] [blame] | 143 | COLLAPSE_WHITESPACE = 0x04,
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 144 |
|
selfpoised | e77e195 | 2013-03-13 14:08:29 +0800 | [diff] [blame] | 145 | TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 146 | TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
selfpoised | e77e195 | 2013-03-13 14:08:29 +0800 | [diff] [blame] | 147 | ATTRIBUTE_NAME = 0,
|
| 148 | ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
| 149 | ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
| 150 | COMMENT = NEEDS_NEWLINE_NORMALIZATION
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 151 | };
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 152 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 153 | StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 154 | ~StrPair();
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 155 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 156 | void Set( char* start, char* end, int flags ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 157 | Reset();
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 158 | _start = start;
|
| 159 | _end = end;
|
| 160 | _flags = flags | NEEDS_FLUSH;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 161 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 162 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 163 | const char* GetStr();
|
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 | bool Empty() const {
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 166 | return _start == _end;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 167 | }
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 168 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 169 | void SetInternedStr( const char* str ) {
|
| 170 | Reset();
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 171 | _start = const_cast<char*>(str);
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 172 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 173 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 174 | void SetStr( const char* str, int flags=0 );
|
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 | char* ParseText( char* in, const char* endTag, int strFlags );
|
| 177 | char* ParseName( char* in );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 178 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 179 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 180 | void Reset();
|
| 181 | void CollapseWhitespace();
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 182 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 183 | enum {
|
| 184 | NEEDS_FLUSH = 0x100,
|
| 185 | NEEDS_DELETE = 0x200
|
| 186 | };
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 187 |
|
selfpoised | 4dd59bc | 2013-03-13 16:54:15 +0800 | [diff] [blame] | 188 | // After parsing, if *_end != 0, it can be set to zero.
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 189 | int _flags;
|
| 190 | char* _start;
|
| 191 | char* _end;
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 192 | };
|
| 193 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 194 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 195 | /*
|
| 196 | A dynamic array of Plain Old Data. Doesn't support constructors, etc.
|
| 197 | Has a small initial memory pool, so that low or no usage will not
|
| 198 | cause a call to new/delete
|
| 199 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 200 | template <class T, int INIT>
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 201 | class TINYXML2_LIB DynArray
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 202 | {
|
| 203 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 204 | DynArray< T, INIT >() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 205 | _mem = _pool;
|
| 206 | _allocated = INIT;
|
| 207 | _size = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 208 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 209 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 210 | ~DynArray() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 211 | if ( _mem != _pool ) {
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 212 | delete [] _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 213 | }
|
| 214 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 215 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 216 | void Push( T t ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 217 | EnsureCapacity( _size+1 );
|
| 218 | _mem[_size++] = t;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 219 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 220 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 221 | T* PushArr( int count ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 222 | EnsureCapacity( _size+count );
|
| 223 | T* ret = &_mem[_size];
|
| 224 | _size += count;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 225 | return ret;
|
| 226 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 227 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 228 | T Pop() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 229 | return _mem[--_size];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 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 | void PopArr( int count ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 233 | TIXMLASSERT( _size >= count );
|
| 234 | _size -= count;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 235 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 236 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 237 | bool Empty() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 238 | return _size == 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 239 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 240 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 241 | T& operator[](int i) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 242 | TIXMLASSERT( i>= 0 && i < _size );
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 243 | return _mem[i];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 244 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 245 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 246 | const T& operator[](int i) const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 247 | TIXMLASSERT( i>= 0 && i < _size );
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 248 | return _mem[i];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 249 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 250 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 251 | int Size() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 252 | return _size;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 253 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 254 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 255 | int Capacity() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 256 | return _allocated;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 257 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 258 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 259 | const T* Mem() const {
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 260 | return _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 261 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 262 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 263 | T* Mem() {
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 264 | return _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 265 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 266 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 267 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 268 | void EnsureCapacity( int cap ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 269 | if ( cap > _allocated ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 270 | int newAllocated = cap * 2;
|
| 271 | T* newMem = new T[newAllocated];
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 272 | memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
|
| 273 | if ( _mem != _pool ) {
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 274 | delete [] _mem;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 275 | }
|
Lee Thomason | ed5c879 | 2012-10-12 10:09:48 -0700 | [diff] [blame] | 276 | _mem = newMem;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 277 | _allocated = newAllocated;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 278 | }
|
| 279 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 280 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 281 | T* _mem;
|
| 282 | T _pool[INIT];
|
| 283 | int _allocated; // objects allocated
|
| 284 | int _size; // number objects in use
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 285 | };
|
| 286 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 287 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 288 | /*
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 289 | Parent virtual class of a pool for fast allocation
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 290 | and deallocation of objects.
|
| 291 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 292 | class TINYXML2_LIB MemPool
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 293 | {
|
| 294 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 295 | MemPool() {}
|
| 296 | virtual ~MemPool() {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 297 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 298 | virtual int ItemSize() const = 0;
|
| 299 | virtual void* Alloc() = 0;
|
| 300 | virtual void Free( void* ) = 0;
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 301 | virtual void SetTracked() = 0;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 302 | };
|
| 303 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 304 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 305 | /*
|
| 306 | Template child class to create pools of the correct type.
|
| 307 | */
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 308 | template< int SIZE >
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 309 | class TINYXML2_LIB MemPoolT : public MemPool
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 310 | {
|
| 311 | public:
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 312 | MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 313 | ~MemPoolT() {
|
| 314 | // Delete the blocks.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 315 | for( int i=0; i<_blockPtrs.Size(); ++i ) {
|
| 316 | delete _blockPtrs[i];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 317 | }
|
| 318 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 319 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 320 | virtual int ItemSize() const {
|
| 321 | return SIZE;
|
| 322 | }
|
| 323 | int CurrentAllocs() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 324 | return _currentAllocs;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 325 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 326 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 327 | virtual void* Alloc() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 328 | if ( !_root ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 329 | // Need a new block.
|
| 330 | Block* block = new Block();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 331 | _blockPtrs.Push( block );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 332 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 333 | for( int i=0; i<COUNT-1; ++i ) {
|
| 334 | block->chunk[i].next = &block->chunk[i+1];
|
| 335 | }
|
| 336 | block->chunk[COUNT-1].next = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 337 | _root = block->chunk;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 338 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 339 | void* result = _root;
|
| 340 | _root = _root->next;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 341 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 342 | ++_currentAllocs;
|
| 343 | if ( _currentAllocs > _maxAllocs ) {
|
| 344 | _maxAllocs = _currentAllocs;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 345 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 346 | _nAllocs++;
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 347 | _nUntracked++;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 348 | return result;
|
| 349 | }
|
| 350 | virtual void Free( void* mem ) {
|
| 351 | if ( !mem ) {
|
| 352 | return;
|
| 353 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 354 | --_currentAllocs;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 355 | Chunk* chunk = (Chunk*)mem;
|
Lee Thomason (grinliz) | 6020a01 | 2012-09-08 21:15:09 -0700 | [diff] [blame] | 356 | #ifdef DEBUG
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 357 | memset( chunk, 0xfe, sizeof(Chunk) );
|
Lee Thomason (grinliz) | 6020a01 | 2012-09-08 21:15:09 -0700 | [diff] [blame] | 358 | #endif
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 359 | chunk->next = _root;
|
| 360 | _root = chunk;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 361 | }
|
| 362 | void Trace( const char* name ) {
|
| 363 | printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 364 | name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 365 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 366 |
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 367 | void SetTracked() {
|
| 368 | _nUntracked--;
|
| 369 | }
|
| 370 |
|
| 371 | int Untracked() const {
|
| 372 | return _nUntracked;
|
| 373 | }
|
| 374 |
|
Lee Thomason (grinliz) | ac83b4e | 2013-02-01 09:02:34 -0800 | [diff] [blame] | 375 | // This number is perf sensitive. 4k seems like a good tradeoff on my machine.
|
| 376 | // The test file is large, 170k.
|
| 377 | // Release: VS2010 gcc(no opt)
|
| 378 | // 1k: 4000
|
| 379 | // 2k: 4000
|
| 380 | // 4k: 3900 21000
|
| 381 | // 16k: 5200
|
| 382 | // 32k: 4300
|
| 383 | // 64k: 4000 21000
|
| 384 | enum { COUNT = (4*1024)/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private
|
Jerome Martinez | 7921df1 | 2012-10-24 11:45:44 +0200 | [diff] [blame] | 385 |
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 386 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 387 | union Chunk {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 388 | Chunk* next;
|
| 389 | char mem[SIZE];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 390 | };
|
| 391 | struct Block {
|
Lee Thomason (grinliz) | 856da21 | 2012-10-19 09:08:15 -0700 | [diff] [blame] | 392 | Chunk chunk[COUNT];
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 393 | };
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 394 | DynArray< Block*, 10 > _blockPtrs;
|
| 395 | Chunk* _root;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 396 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 397 | int _currentAllocs;
|
| 398 | int _nAllocs;
|
| 399 | int _maxAllocs;
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 400 | int _nUntracked;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 401 | };
|
| 402 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 403 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 404 |
|
| 405 | /**
|
| 406 | Implements the interface to the "Visitor pattern" (see the Accept() method.)
|
| 407 | If you call the Accept() method, it requires being passed a XMLVisitor
|
| 408 | class to handle callbacks. For nodes that contain other nodes (Document, Element)
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 409 | 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] | 410 | are simply called with Visit().
|
| 411 |
|
| 412 | 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] | 413 | 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] | 414 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 415 | 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] | 416 | visiting). You need to only override methods that are interesting to you.
|
| 417 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 418 | Generally Accept() is called on the XMLDocument, although all nodes support visiting.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 419 |
|
| 420 | You should never change the document from a callback.
|
| 421 |
|
| 422 | @sa XMLNode::Accept()
|
| 423 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 424 | class TINYXML2_LIB XMLVisitor
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 425 | {
|
| 426 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 427 | virtual ~XMLVisitor() {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 428 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 429 | /// Visit a document.
|
| 430 | virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
|
| 431 | return true;
|
| 432 | }
|
| 433 | /// Visit a document.
|
| 434 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
|
| 435 | return true;
|
| 436 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 437 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 438 | /// Visit an element.
|
| 439 | virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
|
| 440 | return true;
|
| 441 | }
|
| 442 | /// Visit an element.
|
| 443 | virtual bool VisitExit( const XMLElement& /*element*/ ) {
|
| 444 | return true;
|
| 445 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 446 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 447 | /// Visit a declaration.
|
| 448 | virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
|
| 449 | return true;
|
| 450 | }
|
| 451 | /// Visit a text node.
|
| 452 | virtual bool Visit( const XMLText& /*text*/ ) {
|
| 453 | return true;
|
| 454 | }
|
| 455 | /// Visit a comment node.
|
| 456 | virtual bool Visit( const XMLComment& /*comment*/ ) {
|
| 457 | return true;
|
| 458 | }
|
| 459 | /// Visit an unknown node.
|
| 460 | virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
|
| 461 | return true;
|
| 462 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 463 | };
|
| 464 |
|
| 465 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 466 | /*
|
| 467 | Utility functionality.
|
| 468 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 469 | class XMLUtil
|
| 470 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 471 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 472 | // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
|
| 473 | // correct, but simple, and usually works.
|
| 474 | static const char* SkipWhiteSpace( const char* p ) {
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 475 | while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<const unsigned char*>(p) ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 476 | ++p;
|
| 477 | }
|
| 478 | return p;
|
| 479 | }
|
| 480 | static char* SkipWhiteSpace( char* p ) {
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 481 | while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<unsigned char*>(p) ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 482 | ++p;
|
| 483 | }
|
| 484 | return p;
|
| 485 | }
|
| 486 | static bool IsWhiteSpace( char p ) {
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 487 | return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 488 | }
|
Martinsh Shaiters | c6d02f4 | 2013-01-26 21:22:57 +0200 | [diff] [blame] | 489 |
|
| 490 | inline static bool IsNameStartChar( unsigned char ch ) {
|
| 491 | return ( ( ch < 128 ) ? isalpha( ch ) : 1 )
|
| 492 | || ch == ':'
|
| 493 | || ch == '_';
|
| 494 | }
|
| 495 |
|
| 496 | inline static bool IsNameChar( unsigned char ch ) {
|
| 497 | return IsNameStartChar( ch )
|
| 498 | || isdigit( ch )
|
| 499 | || ch == '.'
|
| 500 | || ch == '-';
|
| 501 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 502 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 503 | inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
|
| 504 | int n = 0;
|
| 505 | if ( p == q ) {
|
| 506 | return true;
|
| 507 | }
|
| 508 | while( *p && *q && *p == *q && n<nChar ) {
|
| 509 | ++p;
|
| 510 | ++q;
|
| 511 | ++n;
|
| 512 | }
|
| 513 | if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
|
| 514 | return true;
|
| 515 | }
|
| 516 | return false;
|
| 517 | }
|
Martinsh Shaiters | c6d02f4 | 2013-01-26 21:22:57 +0200 | [diff] [blame] | 518 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 519 | inline static int IsUTF8Continuation( const char p ) {
|
| 520 | return p & 0x80;
|
| 521 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 522 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 523 | static const char* ReadBOM( const char* p, bool* hasBOM );
|
| 524 | // p is the starting location,
|
| 525 | // the UTF-8 value of the entity will be placed in value, and length filled in.
|
| 526 | static const char* GetCharacterRef( const char* p, char* value, int* length );
|
| 527 | static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 528 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 529 | // converts primitive types to strings
|
| 530 | static void ToStr( int v, char* buffer, int bufferSize );
|
| 531 | static void ToStr( unsigned v, char* buffer, int bufferSize );
|
| 532 | static void ToStr( bool v, char* buffer, int bufferSize );
|
| 533 | static void ToStr( float v, char* buffer, int bufferSize );
|
| 534 | static void ToStr( double v, char* buffer, int bufferSize );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 535 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 536 | // converts strings to primitive types
|
| 537 | static bool ToInt( const char* str, int* value );
|
| 538 | static bool ToUnsigned( const char* str, unsigned* value );
|
| 539 | static bool ToBool( const char* str, bool* value );
|
| 540 | static bool ToFloat( const char* str, float* value );
|
| 541 | static bool ToDouble( const char* str, double* value );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 542 | };
|
| 543 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 544 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 545 | /** XMLNode is a base class for every object that is in the
|
| 546 | XML Document Object Model (DOM), except XMLAttributes.
|
| 547 | Nodes have siblings, a parent, and children which can
|
| 548 | be navigated. A node is always in a XMLDocument.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 549 | The type of a XMLNode can be queried, and it can
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 550 | be cast to its more defined type.
|
| 551 |
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 552 | A XMLDocument allocates memory for all its Nodes.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 553 | When the XMLDocument gets deleted, all its Nodes
|
| 554 | will also be deleted.
|
| 555 |
|
| 556 | @verbatim
|
| 557 | A Document can contain: Element (container or leaf)
|
| 558 | Comment (leaf)
|
| 559 | Unknown (leaf)
|
| 560 | Declaration( leaf )
|
| 561 |
|
| 562 | An Element can contain: Element (container or leaf)
|
| 563 | Text (leaf)
|
| 564 | Attributes (not on tree)
|
| 565 | Comment (leaf)
|
| 566 | Unknown (leaf)
|
| 567 |
|
| 568 | @endverbatim
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 569 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 570 | class TINYXML2_LIB XMLNode
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 571 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 572 | friend class XMLDocument;
|
| 573 | friend class XMLElement;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 574 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 575 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 576 | /// Get the XMLDocument that owns this XMLNode.
|
| 577 | const XMLDocument* GetDocument() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 578 | return _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 579 | }
|
| 580 | /// Get the XMLDocument that owns this XMLNode.
|
| 581 | XMLDocument* GetDocument() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 582 | return _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 583 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 584 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 585 | /// Safely cast to an Element, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 586 | virtual XMLElement* ToElement() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 587 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 588 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 589 | /// Safely cast to Text, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 590 | virtual XMLText* ToText() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 591 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 592 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 593 | /// Safely cast to a Comment, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 594 | virtual XMLComment* ToComment() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 595 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 596 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 597 | /// Safely cast to a Document, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 598 | virtual XMLDocument* ToDocument() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 599 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 600 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 601 | /// Safely cast to a Declaration, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 602 | virtual XMLDeclaration* ToDeclaration() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 603 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 604 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 605 | /// Safely cast to an Unknown, or null.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 606 | virtual XMLUnknown* ToUnknown() {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 607 | return 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 608 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 609 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 610 | virtual const XMLElement* ToElement() const {
|
| 611 | return 0;
|
| 612 | }
|
| 613 | virtual const XMLText* ToText() const {
|
| 614 | return 0;
|
| 615 | }
|
| 616 | virtual const XMLComment* ToComment() const {
|
| 617 | return 0;
|
| 618 | }
|
| 619 | virtual const XMLDocument* ToDocument() const {
|
| 620 | return 0;
|
| 621 | }
|
| 622 | virtual const XMLDeclaration* ToDeclaration() const {
|
| 623 | return 0;
|
| 624 | }
|
| 625 | virtual const XMLUnknown* ToUnknown() const {
|
| 626 | return 0;
|
| 627 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 628 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 629 | /** The meaning of 'value' changes for the specific type.
|
| 630 | @verbatim
|
| 631 | Document: empty
|
| 632 | Element: name of the element
|
| 633 | Comment: the comment text
|
| 634 | Unknown: the tag contents
|
| 635 | Text: the text string
|
| 636 | @endverbatim
|
| 637 | */
|
| 638 | const char* Value() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 639 | return _value.GetStr();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 640 | }
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 641 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 642 | /** Set the Value of an XML node.
|
| 643 | @sa Value()
|
| 644 | */
|
| 645 | void SetValue( const char* val, bool staticMem=false );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 646 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 647 | /// Get the parent of this node on the DOM.
|
| 648 | const XMLNode* Parent() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 649 | return _parent;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 650 | }
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 651 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 652 | XMLNode* Parent() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 653 | return _parent;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 654 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 655 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 656 | /// Returns true if this node has no children.
|
| 657 | bool NoChildren() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 658 | return !_firstChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 659 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 660 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 661 | /// Get the first child node, or null if none exists.
|
| 662 | const XMLNode* FirstChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 663 | return _firstChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 664 | }
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 665 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 666 | XMLNode* FirstChild() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 667 | return _firstChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 668 | }
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 669 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 670 | /** Get the first child element, or optionally the first child
|
| 671 | element with the specified name.
|
| 672 | */
|
| 673 | const XMLElement* FirstChildElement( const char* value=0 ) const;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 674 |
|
| 675 | XMLElement* FirstChildElement( const char* value=0 ) {
|
| 676 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value ));
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 677 | }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 678 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 679 | /// Get the last child node, or null if none exists.
|
| 680 | const XMLNode* LastChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 681 | return _lastChild;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 682 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 683 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 684 | XMLNode* LastChild() {
|
| 685 | return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() );
|
| 686 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 687 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 688 | /** Get the last child element or optionally the last child
|
| 689 | element with the specified name.
|
| 690 | */
|
| 691 | const XMLElement* LastChildElement( const char* value=0 ) const;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 692 |
|
| 693 | XMLElement* LastChildElement( const char* value=0 ) {
|
| 694 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 695 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 696 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 697 | /// Get the previous (left) sibling node of this node.
|
| 698 | const XMLNode* PreviousSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 699 | return _prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 700 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 701 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 702 | XMLNode* PreviousSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 703 | return _prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 704 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 705 |
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 706 | /// Get the previous (left) sibling element of this node, with an optionally supplied name.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 707 | const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 708 |
|
| 709 | XMLElement* PreviousSiblingElement( const char* value=0 ) {
|
| 710 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 711 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 712 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 713 | /// Get the next (right) sibling node of this node.
|
| 714 | const XMLNode* NextSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 715 | return _next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 716 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 717 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 718 | XMLNode* NextSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 719 | return _next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 720 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 721 |
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 722 | /// Get the next (right) sibling element of this node, with an optionally supplied name.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 723 | const XMLElement* NextSiblingElement( const char* value=0 ) const;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 724 |
|
| 725 | XMLElement* NextSiblingElement( const char* value=0 ) {
|
| 726 | return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 727 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 728 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 729 | /**
|
| 730 | Add a child node as the last (right) child.
|
| 731 | */
|
| 732 | XMLNode* InsertEndChild( XMLNode* addThis );
|
Lee Thomason | 618dbf8 | 2012-02-28 12:34:27 -0800 | [diff] [blame] | 733 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 734 | XMLNode* LinkEndChild( XMLNode* addThis ) {
|
| 735 | return InsertEndChild( addThis );
|
| 736 | }
|
| 737 | /**
|
| 738 | Add a child node as the first (left) child.
|
| 739 | */
|
| 740 | XMLNode* InsertFirstChild( XMLNode* addThis );
|
| 741 | /**
|
| 742 | Add a node after the specified child node.
|
| 743 | */
|
| 744 | XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 745 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 746 | /**
|
| 747 | Delete all the children of this node.
|
| 748 | */
|
| 749 | void DeleteChildren();
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 750 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 751 | /**
|
| 752 | Delete a child of this node.
|
| 753 | */
|
| 754 | void DeleteChild( XMLNode* node );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 755 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 756 | /**
|
| 757 | Make a copy of this node, but not its children.
|
| 758 | You may pass in a Document pointer that will be
|
| 759 | the owner of the new Node. If the 'document' is
|
| 760 | null, then the node returned will be allocated
|
| 761 | from the current Document. (this->GetDocument())
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 762 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 763 | Note: if called on a XMLDocument, this will return null.
|
| 764 | */
|
| 765 | virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 766 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 767 | /**
|
| 768 | Test if 2 nodes are the same, but don't test children.
|
| 769 | The 2 nodes do not need to be in the same Document.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 770 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 771 | Note: if called on a XMLDocument, this will return false.
|
| 772 | */
|
| 773 | virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 774 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 775 | /** 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] | 776 | 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] | 777 | via the XMLVisitor interface.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 778 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 779 | This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
|
| 780 | 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] | 781 | interface versus any other.)
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 782 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 783 | The interface has been based on ideas from:
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 784 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 785 | - http://www.saxproject.org/
|
| 786 | - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 787 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 788 | Which are both good references for "visiting".
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 789 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 790 | An example of using Accept():
|
| 791 | @verbatim
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 792 | XMLPrinter printer;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 793 | tinyxmlDoc.Accept( &printer );
|
| 794 | const char* xmlcstr = printer.CStr();
|
| 795 | @endverbatim
|
| 796 | */
|
| 797 | virtual bool Accept( XMLVisitor* visitor ) const = 0;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 798 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 799 | // internal
|
| 800 | virtual char* ParseDeep( char*, StrPair* );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 801 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 802 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 803 | XMLNode( XMLDocument* );
|
| 804 | virtual ~XMLNode();
|
| 805 | XMLNode( const XMLNode& ); // not supported
|
| 806 | XMLNode& operator=( const XMLNode& ); // not supported
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 807 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 808 | XMLDocument* _document;
|
| 809 | XMLNode* _parent;
|
| 810 | mutable StrPair _value;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 811 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 812 | XMLNode* _firstChild;
|
| 813 | XMLNode* _lastChild;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 814 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 815 | XMLNode* _prev;
|
| 816 | XMLNode* _next;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 817 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 818 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 819 | MemPool* _memPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 820 | void Unlink( XMLNode* child );
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 821 | };
|
| 822 |
|
| 823 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 824 | /** XML text.
|
| 825 |
|
| 826 | Note that a text node can have child element nodes, for example:
|
| 827 | @verbatim
|
| 828 | <root>This is <b>bold</b></root>
|
| 829 | @endverbatim
|
| 830 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 831 | 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] | 832 | 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] | 833 | 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] | 834 | SetCData() and query it with CData().
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 835 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 836 | class TINYXML2_LIB XMLText : public XMLNode
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 837 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 838 | friend class XMLBase;
|
| 839 | friend class XMLDocument;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 840 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 841 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 842 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 843 | virtual XMLText* ToText() {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 844 | return this;
|
| 845 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 846 | virtual const XMLText* ToText() const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 847 | return this;
|
| 848 | }
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 849 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 850 | /// Declare whether this should be CDATA or standard text.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 851 | void SetCData( bool isCData ) {
|
| 852 | _isCData = isCData;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 853 | }
|
| 854 | /// Returns true if this is a CDATA text element.
|
| 855 | bool CData() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 856 | return _isCData;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 857 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 858 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 859 | char* ParseDeep( char*, StrPair* endTag );
|
| 860 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 861 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 862 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 863 | protected:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 864 | XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 865 | virtual ~XMLText() {}
|
| 866 | XMLText( const XMLText& ); // not supported
|
| 867 | XMLText& operator=( const XMLText& ); // not supported
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 868 |
|
| 869 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 870 | bool _isCData;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 871 | };
|
| 872 |
|
| 873 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 874 | /** An XML Comment. */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 875 | class TINYXML2_LIB XMLComment : public XMLNode
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 876 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 877 | friend class XMLDocument;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 878 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 879 | virtual XMLComment* ToComment() {
|
| 880 | return this;
|
| 881 | }
|
| 882 | virtual const XMLComment* ToComment() const {
|
| 883 | return this;
|
| 884 | }
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 885 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 886 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 887 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 888 | char* ParseDeep( char*, StrPair* endTag );
|
| 889 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 890 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 891 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 892 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 893 | XMLComment( XMLDocument* doc );
|
| 894 | virtual ~XMLComment();
|
| 895 | XMLComment( const XMLComment& ); // not supported
|
| 896 | XMLComment& operator=( const XMLComment& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 897 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 898 | private:
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 899 | };
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 900 |
|
| 901 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 902 | /** In correct XML the declaration is the first entry in the file.
|
| 903 | @verbatim
|
| 904 | <?xml version="1.0" standalone="yes"?>
|
| 905 | @endverbatim
|
| 906 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 907 | TinyXML-2 will happily read or write files without a declaration,
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 908 | however.
|
| 909 |
|
| 910 | The text of the declaration isn't interpreted. It is parsed
|
| 911 | and written as a string.
|
| 912 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 913 | class TINYXML2_LIB XMLDeclaration : public XMLNode
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 914 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 915 | friend class XMLDocument;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 916 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 917 | virtual XMLDeclaration* ToDeclaration() {
|
| 918 | return this;
|
| 919 | }
|
| 920 | virtual const XMLDeclaration* ToDeclaration() const {
|
| 921 | return this;
|
| 922 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 923 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 924 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 925 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 926 | char* ParseDeep( char*, StrPair* endTag );
|
| 927 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 928 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 929 |
|
| 930 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 931 | XMLDeclaration( XMLDocument* doc );
|
| 932 | virtual ~XMLDeclaration();
|
| 933 | XMLDeclaration( const XMLDeclaration& ); // not supported
|
| 934 | XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 935 | };
|
| 936 |
|
| 937 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 938 | /** 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] | 939 | unknown. It is a tag of text, but should not be modified.
|
| 940 | It will be written back to the XML, unchanged, when the file
|
| 941 | is saved.
|
| 942 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 943 | DTD tags get thrown into XMLUnknowns.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 944 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 945 | class TINYXML2_LIB XMLUnknown : public XMLNode
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 946 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 947 | friend class XMLDocument;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 948 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 949 | virtual XMLUnknown* ToUnknown() {
|
| 950 | return this;
|
| 951 | }
|
| 952 | virtual const XMLUnknown* ToUnknown() const {
|
| 953 | return this;
|
| 954 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 955 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 956 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 957 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 958 | char* ParseDeep( char*, StrPair* endTag );
|
| 959 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 960 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 961 |
|
| 962 | protected:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 963 | XMLUnknown( XMLDocument* doc );
|
| 964 | virtual ~XMLUnknown();
|
| 965 | XMLUnknown( const XMLUnknown& ); // not supported
|
| 966 | XMLUnknown& operator=( const XMLUnknown& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 967 | };
|
| 968 |
|
| 969 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 970 | enum XMLError {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 971 | XML_NO_ERROR = 0,
|
| 972 | XML_SUCCESS = 0,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 973 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 974 | XML_NO_ATTRIBUTE,
|
| 975 | XML_WRONG_ATTRIBUTE_TYPE,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 976 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 977 | XML_ERROR_FILE_NOT_FOUND,
|
| 978 | XML_ERROR_FILE_COULD_NOT_BE_OPENED,
|
| 979 | XML_ERROR_FILE_READ_ERROR,
|
| 980 | XML_ERROR_ELEMENT_MISMATCH,
|
| 981 | XML_ERROR_PARSING_ELEMENT,
|
| 982 | XML_ERROR_PARSING_ATTRIBUTE,
|
| 983 | XML_ERROR_IDENTIFYING_TAG,
|
| 984 | XML_ERROR_PARSING_TEXT,
|
| 985 | XML_ERROR_PARSING_CDATA,
|
| 986 | XML_ERROR_PARSING_COMMENT,
|
| 987 | XML_ERROR_PARSING_DECLARATION,
|
| 988 | XML_ERROR_PARSING_UNKNOWN,
|
| 989 | XML_ERROR_EMPTY_DOCUMENT,
|
| 990 | XML_ERROR_MISMATCHED_ELEMENT,
|
| 991 | XML_ERROR_PARSING,
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 992 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 993 | XML_CAN_NOT_CONVERT_TEXT,
|
| 994 | XML_NO_TEXT_NODE
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 995 | };
|
| 996 |
|
| 997 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 998 | /** An attribute is a name-value pair. Elements have an arbitrary
|
| 999 | number of attributes, each with a unique name.
|
| 1000 |
|
| 1001 | @note The attributes are not XMLNodes. You may only query the
|
| 1002 | Next() attribute in a list.
|
| 1003 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1004 | class TINYXML2_LIB XMLAttribute
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1005 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1006 | friend class XMLElement;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1007 | public:
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1008 | /// The name of the attribute.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1009 | const char* Name() const {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1010 | return _name.GetStr();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1011 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1012 | /// The value of the attribute.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1013 | const char* Value() const {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1014 | return _value.GetStr();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1015 | }
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1016 | /// The next attribute in the list.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1017 | const XMLAttribute* Next() const {
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1018 | return _next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1019 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1020 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1021 | /** IntValue interprets the attribute as an integer, and returns the value.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1022 | 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] | 1023 | use QueryIntValue() if you need error checking.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1024 | */
|
| 1025 | int IntValue() const {
|
| 1026 | int i=0;
|
| 1027 | QueryIntValue( &i );
|
| 1028 | return i;
|
| 1029 | }
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1030 | /// Query as an unsigned integer. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1031 | unsigned UnsignedValue() const {
|
| 1032 | unsigned i=0;
|
| 1033 | QueryUnsignedValue( &i );
|
| 1034 | return i;
|
| 1035 | }
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1036 | /// Query as a boolean. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1037 | bool BoolValue() const {
|
| 1038 | bool b=false;
|
| 1039 | QueryBoolValue( &b );
|
| 1040 | return b;
|
| 1041 | }
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1042 | /// Query as a double. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1043 | double DoubleValue() const {
|
| 1044 | double d=0;
|
| 1045 | QueryDoubleValue( &d );
|
| 1046 | return d;
|
| 1047 | }
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1048 | /// Query as a float. See IntValue()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1049 | float FloatValue() const {
|
| 1050 | float f=0;
|
| 1051 | QueryFloatValue( &f );
|
| 1052 | return f;
|
| 1053 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1054 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1055 | /** QueryIntValue interprets the attribute as an integer, and returns the value
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 1056 | in the provided parameter. The function will return XML_NO_ERROR on success,
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1057 | and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
|
| 1058 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1059 | XMLError QueryIntValue( int* value ) const;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1060 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1061 | XMLError QueryUnsignedValue( unsigned int* value ) const;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1062 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1063 | XMLError QueryBoolValue( bool* value ) const;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1064 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1065 | XMLError QueryDoubleValue( double* value ) const;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1066 | /// See QueryIntValue
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1067 | XMLError QueryFloatValue( float* value ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1068 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1069 | /// Set the attribute to a string value.
|
| 1070 | void SetAttribute( const char* value );
|
| 1071 | /// Set the attribute to value.
|
| 1072 | void SetAttribute( int value );
|
| 1073 | /// Set the attribute to value.
|
| 1074 | void SetAttribute( unsigned value );
|
| 1075 | /// Set the attribute to value.
|
| 1076 | void SetAttribute( bool value );
|
| 1077 | /// Set the attribute to value.
|
| 1078 | void SetAttribute( double value );
|
| 1079 | /// Set the attribute to value.
|
| 1080 | void SetAttribute( float value );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1081 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1082 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1083 | enum { BUF_SIZE = 200 };
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1084 |
|
Thomas Roß | 6189231 | 2013-05-12 14:07:38 +0200 | [diff] [blame] | 1085 | XMLAttribute() : _next( 0 ), _memPool( 0 ) {}
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1086 | virtual ~XMLAttribute() {}
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1087 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1088 | XMLAttribute( const XMLAttribute& ); // not supported
|
| 1089 | void operator=( const XMLAttribute& ); // not supported
|
| 1090 | void SetName( const char* name );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1091 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1092 | char* ParseDeep( char* p, bool processEntities );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1093 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1094 | mutable StrPair _name;
|
| 1095 | mutable StrPair _value;
|
| 1096 | XMLAttribute* _next;
|
| 1097 | MemPool* _memPool;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1098 | };
|
| 1099 |
|
| 1100 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1101 | /** The element is a container class. It has a value, the element name,
|
| 1102 | and can contain other elements, text, comments, and unknowns.
|
| 1103 | Elements also contain an arbitrary number of attributes.
|
| 1104 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1105 | class TINYXML2_LIB XMLElement : public XMLNode
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1106 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1107 | friend class XMLBase;
|
| 1108 | friend class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1109 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1110 | /// Get the name of an element (which is the Value() of the node.)
|
| 1111 | const char* Name() const {
|
| 1112 | return Value();
|
| 1113 | }
|
| 1114 | /// Set the name of the element.
|
| 1115 | void SetName( const char* str, bool staticMem=false ) {
|
| 1116 | SetValue( str, staticMem );
|
| 1117 | }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1118 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1119 | virtual XMLElement* ToElement() {
|
| 1120 | return this;
|
| 1121 | }
|
| 1122 | virtual const XMLElement* ToElement() const {
|
| 1123 | return this;
|
| 1124 | }
|
| 1125 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1126 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1127 | /** Given an attribute name, Attribute() returns the value
|
| 1128 | for the attribute of that name, or null if none
|
| 1129 | exists. For example:
|
Lee Thomason | 9225815 | 2012-03-24 13:05:39 -0700 | [diff] [blame] | 1130 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1131 | @verbatim
|
| 1132 | const char* value = ele->Attribute( "foo" );
|
| 1133 | @endverbatim
|
Lee Thomason | 9225815 | 2012-03-24 13:05:39 -0700 | [diff] [blame] | 1134 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1135 | The 'value' parameter is normally null. However, if specified,
|
| 1136 | the attribute will only be returned if the 'name' and 'value'
|
| 1137 | match. This allow you to write code:
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1138 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1139 | @verbatim
|
| 1140 | if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
|
| 1141 | @endverbatim
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1142 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1143 | rather than:
|
| 1144 | @verbatim
|
| 1145 | if ( ele->Attribute( "foo" ) ) {
|
| 1146 | if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
|
| 1147 | }
|
| 1148 | @endverbatim
|
| 1149 | */
|
| 1150 | const char* Attribute( const char* name, const char* value=0 ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1151 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1152 | /** Given an attribute name, IntAttribute() returns the value
|
| 1153 | of the attribute interpreted as an integer. 0 will be
|
| 1154 | returned if there is an error. For a method with error
|
| 1155 | checking, see QueryIntAttribute()
|
| 1156 | */
|
| 1157 | int IntAttribute( const char* name ) const {
|
| 1158 | int i=0;
|
| 1159 | QueryIntAttribute( name, &i );
|
| 1160 | return i;
|
| 1161 | }
|
| 1162 | /// See IntAttribute()
|
| 1163 | unsigned UnsignedAttribute( const char* name ) const {
|
| 1164 | unsigned i=0;
|
| 1165 | QueryUnsignedAttribute( name, &i );
|
| 1166 | return i;
|
| 1167 | }
|
| 1168 | /// See IntAttribute()
|
| 1169 | bool BoolAttribute( const char* name ) const {
|
| 1170 | bool b=false;
|
| 1171 | QueryBoolAttribute( name, &b );
|
| 1172 | return b;
|
| 1173 | }
|
| 1174 | /// See IntAttribute()
|
| 1175 | double DoubleAttribute( const char* name ) const {
|
| 1176 | double d=0;
|
| 1177 | QueryDoubleAttribute( name, &d );
|
| 1178 | return d;
|
| 1179 | }
|
| 1180 | /// See IntAttribute()
|
| 1181 | float FloatAttribute( const char* name ) const {
|
| 1182 | float f=0;
|
| 1183 | QueryFloatAttribute( name, &f );
|
| 1184 | return f;
|
| 1185 | }
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1186 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1187 | /** Given an attribute name, QueryIntAttribute() returns
|
| 1188 | XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
|
| 1189 | can't be performed, or XML_NO_ATTRIBUTE if the attribute
|
| 1190 | doesn't exist. If successful, the result of the conversion
|
| 1191 | will be written to 'value'. If not successful, nothing will
|
| 1192 | be written to 'value'. This allows you to provide default
|
| 1193 | value:
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1194 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1195 | @verbatim
|
| 1196 | int value = 10;
|
| 1197 | QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
|
| 1198 | @endverbatim
|
| 1199 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1200 | XMLError QueryIntAttribute( const char* name, int* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1201 | const XMLAttribute* a = FindAttribute( name );
|
| 1202 | if ( !a ) {
|
| 1203 | return XML_NO_ATTRIBUTE;
|
| 1204 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1205 | return a->QueryIntValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1206 | }
|
| 1207 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1208 | XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1209 | const XMLAttribute* a = FindAttribute( name );
|
| 1210 | if ( !a ) {
|
| 1211 | return XML_NO_ATTRIBUTE;
|
| 1212 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1213 | return a->QueryUnsignedValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1214 | }
|
| 1215 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1216 | XMLError QueryBoolAttribute( const char* name, bool* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1217 | const XMLAttribute* a = FindAttribute( name );
|
| 1218 | if ( !a ) {
|
| 1219 | return XML_NO_ATTRIBUTE;
|
| 1220 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1221 | return a->QueryBoolValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1222 | }
|
| 1223 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1224 | XMLError QueryDoubleAttribute( const char* name, double* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1225 | const XMLAttribute* a = FindAttribute( name );
|
| 1226 | if ( !a ) {
|
| 1227 | return XML_NO_ATTRIBUTE;
|
| 1228 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1229 | return a->QueryDoubleValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1230 | }
|
| 1231 | /// See QueryIntAttribute()
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1232 | XMLError QueryFloatAttribute( const char* name, float* value ) const {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1233 | const XMLAttribute* a = FindAttribute( name );
|
| 1234 | if ( !a ) {
|
| 1235 | return XML_NO_ATTRIBUTE;
|
| 1236 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1237 | return a->QueryFloatValue( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1238 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1239 |
|
Lee Thomason (grinliz) | 5efaa5f | 2013-02-01 19:26:30 -0800 | [diff] [blame] | 1240 |
|
| 1241 | /** Given an attribute name, QueryAttribute() returns
|
| 1242 | XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
|
| 1243 | can't be performed, or XML_NO_ATTRIBUTE if the attribute
|
| 1244 | doesn't exist. It is overloaded for the primitive types,
|
| 1245 | and is a generally more convenient replacement of
|
| 1246 | QueryIntAttribute() and related functions.
|
| 1247 |
|
| 1248 | If successful, the result of the conversion
|
| 1249 | will be written to 'value'. If not successful, nothing will
|
| 1250 | be written to 'value'. This allows you to provide default
|
| 1251 | value:
|
| 1252 |
|
| 1253 | @verbatim
|
| 1254 | int value = 10;
|
| 1255 | QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
|
| 1256 | @endverbatim
|
| 1257 | */
|
| 1258 | int QueryAttribute( const char* name, int* value ) const {
|
| 1259 | return QueryIntAttribute( name, value );
|
| 1260 | }
|
| 1261 |
|
| 1262 | int QueryAttribute( const char* name, unsigned int* value ) const {
|
| 1263 | return QueryUnsignedAttribute( name, value );
|
| 1264 | }
|
| 1265 |
|
| 1266 | int QueryAttribute( const char* name, bool* value ) const {
|
| 1267 | return QueryBoolAttribute( name, value );
|
| 1268 | }
|
| 1269 |
|
| 1270 | int QueryAttribute( const char* name, double* value ) const {
|
| 1271 | return QueryDoubleAttribute( name, value );
|
| 1272 | }
|
| 1273 |
|
| 1274 | int QueryAttribute( const char* name, float* value ) const {
|
| 1275 | return QueryFloatAttribute( name, value );
|
| 1276 | }
|
| 1277 |
|
| 1278 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1279 | void SetAttribute( const char* name, const char* value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1280 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1281 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1282 | }
|
| 1283 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1284 | void SetAttribute( const char* name, int value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1285 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1286 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1287 | }
|
| 1288 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1289 | void SetAttribute( const char* name, unsigned value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1290 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1291 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1292 | }
|
| 1293 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1294 | void SetAttribute( const char* name, bool value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1295 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1296 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1297 | }
|
| 1298 | /// Sets the named attribute to value.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1299 | void SetAttribute( const char* name, double value ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1300 | XMLAttribute* a = FindOrCreateAttribute( name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1301 | a->SetAttribute( value );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1302 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1303 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1304 | /**
|
| 1305 | Delete an attribute.
|
| 1306 | */
|
| 1307 | void DeleteAttribute( const char* name );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1308 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1309 | /// Return the first attribute in the list.
|
| 1310 | const XMLAttribute* FirstAttribute() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1311 | return _rootAttribute;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1312 | }
|
| 1313 | /// Query a specific attribute in the list.
|
| 1314 | const XMLAttribute* FindAttribute( const char* name ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1315 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1316 | /** 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] | 1317 | and concise, GetText() is limited compared to getting the XMLText child
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1318 | and accessing it directly.
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1319 |
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1320 | If the first child of 'this' is a XMLText, the GetText()
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1321 | 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] | 1322 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1323 | This is a convenient method for getting the text of simple contained text:
|
| 1324 | @verbatim
|
| 1325 | <foo>This is text</foo>
|
| 1326 | const char* str = fooElement->GetText();
|
| 1327 | @endverbatim
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1328 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1329 | 'str' will be a pointer to "This is text".
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1330 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1331 | Note that this function can be misleading. If the element foo was created from
|
| 1332 | this XML:
|
| 1333 | @verbatim
|
| 1334 | <foo><b>This is text</b></foo>
|
| 1335 | @endverbatim
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1336 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1337 | then the value of str would be null. The first child node isn't a text node, it is
|
| 1338 | another element. From this XML:
|
| 1339 | @verbatim
|
| 1340 | <foo>This is <b>text</b></foo>
|
| 1341 | @endverbatim
|
| 1342 | GetText() will return "This is ".
|
| 1343 | */
|
| 1344 | const char* GetText() const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1345 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1346 | /**
|
| 1347 | Convenience method to query the value of a child text node. This is probably best
|
| 1348 | shown by example. Given you have a document is this form:
|
| 1349 | @verbatim
|
| 1350 | <point>
|
| 1351 | <x>1</x>
|
| 1352 | <y>1.4</y>
|
| 1353 | </point>
|
| 1354 | @endverbatim
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1355 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1356 | The QueryIntText() and similar functions provide a safe and easier way to get to the
|
| 1357 | "value" of x and y.
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1358 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1359 | @verbatim
|
| 1360 | int x = 0;
|
| 1361 | float y = 0; // types of x and y are contrived for example
|
| 1362 | const XMLElement* xElement = pointElement->FirstChildElement( "x" );
|
| 1363 | const XMLElement* yElement = pointElement->FirstChildElement( "y" );
|
| 1364 | xElement->QueryIntText( &x );
|
| 1365 | yElement->QueryFloatText( &y );
|
| 1366 | @endverbatim
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1367 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1368 | @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
|
| 1369 | 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] | 1370 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1371 | */
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1372 | XMLError QueryIntText( int* ival ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1373 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1374 | XMLError QueryUnsignedText( unsigned* uval ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1375 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1376 | XMLError QueryBoolText( bool* bval ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1377 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1378 | XMLError QueryDoubleText( double* dval ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1379 | /// See QueryIntText()
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1380 | XMLError QueryFloatText( float* fval ) const;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1381 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1382 | // internal:
|
| 1383 | enum {
|
| 1384 | OPEN, // <foo>
|
| 1385 | CLOSED, // <foo/>
|
| 1386 | CLOSING // </foo>
|
| 1387 | };
|
| 1388 | int ClosingType() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1389 | return _closingType;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1390 | }
|
| 1391 | char* ParseDeep( char* p, StrPair* endTag );
|
| 1392 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 1393 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1394 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1395 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1396 | XMLElement( XMLDocument* doc );
|
| 1397 | virtual ~XMLElement();
|
| 1398 | XMLElement( const XMLElement& ); // not supported
|
| 1399 | void operator=( const XMLElement& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1400 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1401 | XMLAttribute* FindAttribute( const char* name );
|
| 1402 | XMLAttribute* FindOrCreateAttribute( const char* name );
|
| 1403 | //void LinkAttribute( XMLAttribute* attrib );
|
| 1404 | char* ParseAttributes( char* p );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1405 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1406 | int _closingType;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1407 | // The attribute list is ordered; there is no 'lastAttribute'
|
| 1408 | // because the list needs to be scanned for dupes before adding
|
| 1409 | // a new attribute.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1410 | XMLAttribute* _rootAttribute;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1411 | };
|
| 1412 |
|
| 1413 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 1414 | enum Whitespace {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1415 | PRESERVE_WHITESPACE,
|
| 1416 | COLLAPSE_WHITESPACE
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1417 | };
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 1418 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1419 |
|
| 1420 | /** A Document binds together all the functionality.
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 1421 | It can be saved, loaded, and printed to the screen.
|
| 1422 | All Nodes are connected and allocated to a Document.
|
| 1423 | If the Document is deleted, all its Nodes are also deleted.
|
| 1424 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1425 | class TINYXML2_LIB XMLDocument : public XMLNode
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1426 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1427 | friend class XMLElement;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1428 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1429 | /// constructor
|
| 1430 | XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
|
| 1431 | ~XMLDocument();
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1432 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1433 | virtual XMLDocument* ToDocument() {
|
| 1434 | return this;
|
| 1435 | }
|
| 1436 | virtual const XMLDocument* ToDocument() const {
|
| 1437 | return this;
|
| 1438 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1439 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1440 | /**
|
| 1441 | Parse an XML file from a character string.
|
| 1442 | Returns XML_NO_ERROR (0) on success, or
|
| 1443 | an errorID.
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 1444 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1445 | You may optionally pass in the 'nBytes', which is
|
| 1446 | the number of bytes which will be parsed. If not
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1447 | specified, TinyXML-2 will assume 'xml' points to a
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1448 | null terminated string.
|
| 1449 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1450 | XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1451 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1452 | /**
|
| 1453 | Load an XML file from disk.
|
| 1454 | Returns XML_NO_ERROR (0) on success, or
|
| 1455 | an errorID.
|
| 1456 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1457 | XMLError LoadFile( const char* filename );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1458 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1459 | /**
|
| 1460 | Load an XML file from disk. You are responsible
|
| 1461 | for providing and closing the FILE*.
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1462 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1463 | Returns XML_NO_ERROR (0) on success, or
|
| 1464 | an errorID.
|
| 1465 | */
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 1466 | XMLError LoadFile( FILE* );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1467 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1468 | /**
|
| 1469 | Save the XML file to disk.
|
| 1470 | Returns XML_NO_ERROR (0) on success, or
|
| 1471 | an errorID.
|
| 1472 | */
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1473 | XMLError SaveFile( const char* filename, bool compact = false );
|
Lee Thomason | d11cd16 | 2012-04-12 08:35:36 -0700 | [diff] [blame] | 1474 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1475 | /**
|
| 1476 | Save the XML file to disk. You are responsible
|
| 1477 | for providing and closing the FILE*.
|
Ken Miller | 81da1fb | 2012-04-09 23:32:26 -0500 | [diff] [blame] | 1478 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1479 | Returns XML_NO_ERROR (0) on success, or
|
| 1480 | an errorID.
|
| 1481 | */
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 1482 | XMLError SaveFile( FILE* fp, bool compact = false );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1483 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1484 | bool ProcessEntities() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1485 | return _processEntities;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1486 | }
|
| 1487 | Whitespace WhitespaceMode() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1488 | return _whitespace;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1489 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1490 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1491 | /**
|
| 1492 | Returns true if this document has a leading Byte Order Mark of UTF8.
|
| 1493 | */
|
| 1494 | bool HasBOM() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1495 | return _writeBOM;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1496 | }
|
| 1497 | /** Sets whether to write the BOM when writing the file.
|
| 1498 | */
|
| 1499 | void SetBOM( bool useBOM ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1500 | _writeBOM = useBOM;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1501 | }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1502 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1503 | /** Return the root element of DOM. Equivalent to FirstChildElement().
|
| 1504 | To get the first node, use FirstChild().
|
| 1505 | */
|
| 1506 | XMLElement* RootElement() {
|
| 1507 | return FirstChildElement();
|
| 1508 | }
|
| 1509 | const XMLElement* RootElement() const {
|
| 1510 | return FirstChildElement();
|
| 1511 | }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1512 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1513 | /** Print the Document. If the Printer is not provided, it will
|
| 1514 | print to stdout. If you provide Printer, this can print to a file:
|
| 1515 | @verbatim
|
| 1516 | XMLPrinter printer( fp );
|
| 1517 | doc.Print( &printer );
|
| 1518 | @endverbatim
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1519 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1520 | Or you can use a printer to print to memory:
|
| 1521 | @verbatim
|
| 1522 | XMLPrinter printer;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1523 | doc.Print( &printer );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1524 | // printer.CStr() has a const char* to the XML
|
| 1525 | @endverbatim
|
| 1526 | */
|
PKEuS | 1c5f99e | 2013-07-06 11:28:39 +0200 | [diff] [blame] | 1527 | void Print( XMLPrinter* streamer=0 ) const;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1528 | virtual bool Accept( XMLVisitor* visitor ) const;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1529 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1530 | /**
|
| 1531 | Create a new Element associated with
|
| 1532 | this Document. The memory for the Element
|
| 1533 | is managed by the Document.
|
| 1534 | */
|
| 1535 | XMLElement* NewElement( const char* name );
|
| 1536 | /**
|
| 1537 | Create a new Comment associated with
|
| 1538 | this Document. The memory for the Comment
|
| 1539 | is managed by the Document.
|
| 1540 | */
|
| 1541 | XMLComment* NewComment( const char* comment );
|
| 1542 | /**
|
| 1543 | Create a new Text associated with
|
| 1544 | this Document. The memory for the Text
|
| 1545 | is managed by the Document.
|
| 1546 | */
|
| 1547 | XMLText* NewText( const char* text );
|
| 1548 | /**
|
| 1549 | Create a new Declaration associated with
|
| 1550 | this Document. The memory for the object
|
| 1551 | is managed by the Document.
|
Lee Thomason | f68c438 | 2012-04-28 14:37:11 -0700 | [diff] [blame] | 1552 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1553 | If the 'text' param is null, the standard
|
| 1554 | declaration is used.:
|
| 1555 | @verbatim
|
| 1556 | <?xml version="1.0" encoding="UTF-8"?>
|
| 1557 | @endverbatim
|
| 1558 | */
|
| 1559 | XMLDeclaration* NewDeclaration( const char* text=0 );
|
| 1560 | /**
|
| 1561 | Create a new Unknown associated with
|
Andrew C. Martin | 0fd8746 | 2013-03-09 20:09:45 -0700 | [diff] [blame] | 1562 | this Document. The memory for the object
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1563 | is managed by the Document.
|
| 1564 | */
|
| 1565 | XMLUnknown* NewUnknown( const char* text );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1566 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1567 | /**
|
| 1568 | Delete a node associated with this document.
|
| 1569 | It will be unlinked from the DOM.
|
| 1570 | */
|
| 1571 | void DeleteNode( XMLNode* node ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1572 | node->_parent->DeleteChild( node );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1573 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1574 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1575 | void SetError( XMLError error, const char* str1, const char* str2 );
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1576 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1577 | /// Return true if there was an error parsing the document.
|
| 1578 | bool Error() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1579 | return _errorID != XML_NO_ERROR;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1580 | }
|
| 1581 | /// Return the errorID.
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1582 | XMLError ErrorID() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1583 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1584 | }
|
| 1585 | /// Return a possibly helpful diagnostic location or string.
|
| 1586 | const char* GetErrorStr1() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1587 | return _errorStr1;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1588 | }
|
| 1589 | /// Return a possibly helpful secondary diagnostic location or string.
|
| 1590 | const char* GetErrorStr2() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1591 | return _errorStr2;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1592 | }
|
| 1593 | /// If there is an error, print it to stdout.
|
| 1594 | void PrintError() const;
|
Martinsh Shaiters | a9d42b0 | 2013-01-30 11:14:27 +0200 | [diff] [blame] | 1595 |
|
| 1596 | /// Clear the document, resetting it to the initial state.
|
| 1597 | void Clear();
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1598 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1599 | // internal
|
| 1600 | char* Identify( char* p, XMLNode** node );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1601 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1602 | virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
|
| 1603 | return 0;
|
| 1604 | }
|
| 1605 | virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
|
| 1606 | return false;
|
| 1607 | }
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1608 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1609 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1610 | XMLDocument( const XMLDocument& ); // not supported
|
| 1611 | void operator=( const XMLDocument& ); // not supported
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1612 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1613 | bool _writeBOM;
|
| 1614 | bool _processEntities;
|
| 1615 | XMLError _errorID;
|
| 1616 | Whitespace _whitespace;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1617 | const char* _errorStr1;
|
| 1618 | const char* _errorStr2;
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1619 | char* _charBuffer;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1620 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1621 | MemPoolT< sizeof(XMLElement) > _elementPool;
|
| 1622 | MemPoolT< sizeof(XMLAttribute) > _attributePool;
|
| 1623 | MemPoolT< sizeof(XMLText) > _textPool;
|
| 1624 | MemPoolT< sizeof(XMLComment) > _commentPool;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1625 | };
|
| 1626 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1627 |
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1628 | /**
|
| 1629 | 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] | 1630 | 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] | 1631 | DOM structure. It is a separate utility class.
|
| 1632 |
|
| 1633 | Take an example:
|
| 1634 | @verbatim
|
| 1635 | <Document>
|
| 1636 | <Element attributeA = "valueA">
|
| 1637 | <Child attributeB = "value1" />
|
| 1638 | <Child attributeB = "value2" />
|
| 1639 | </Element>
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 1640 | </Document>
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1641 | @endverbatim
|
| 1642 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1643 | 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] | 1644 | easy to write a *lot* of code that looks like:
|
| 1645 |
|
| 1646 | @verbatim
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1647 | XMLElement* root = document.FirstChildElement( "Document" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1648 | if ( root )
|
| 1649 | {
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1650 | XMLElement* element = root->FirstChildElement( "Element" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1651 | if ( element )
|
| 1652 | {
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1653 | XMLElement* child = element->FirstChildElement( "Child" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1654 | if ( child )
|
| 1655 | {
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1656 | XMLElement* child2 = child->NextSiblingElement( "Child" );
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1657 | if ( child2 )
|
| 1658 | {
|
| 1659 | // Finally do something useful.
|
| 1660 | @endverbatim
|
| 1661 |
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1662 | 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] | 1663 | 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] | 1664 | and correct to use:
|
| 1665 |
|
| 1666 | @verbatim
|
Lee Thomason | db0bbb6 | 2012-04-04 15:47:04 -0700 | [diff] [blame] | 1667 | XMLHandle docHandle( &document );
|
| 1668 | XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement();
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1669 | if ( child2 )
|
| 1670 | {
|
| 1671 | // do something useful
|
| 1672 | @endverbatim
|
| 1673 |
|
| 1674 | Which is MUCH more concise and useful.
|
| 1675 |
|
| 1676 | It is also safe to copy handles - internally they are nothing more than node pointers.
|
| 1677 | @verbatim
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1678 | XMLHandle handleCopy = handle;
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1679 | @endverbatim
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1680 |
|
| 1681 | 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] | 1682 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1683 | class TINYXML2_LIB XMLHandle
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1684 | {
|
| 1685 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1686 | /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1687 | XMLHandle( XMLNode* node ) {
|
| 1688 | _node = node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1689 | }
|
| 1690 | /// Create a handle from a node.
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1691 | XMLHandle( XMLNode& node ) {
|
| 1692 | _node = &node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1693 | }
|
| 1694 | /// Copy constructor
|
| 1695 | XMLHandle( const XMLHandle& ref ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1696 | _node = ref._node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1697 | }
|
| 1698 | /// Assignment
|
| 1699 | XMLHandle& operator=( const XMLHandle& ref ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1700 | _node = ref._node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1701 | return *this;
|
| 1702 | }
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1703 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1704 | /// Get the first child of this handle.
|
| 1705 | XMLHandle FirstChild() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1706 | return XMLHandle( _node ? _node->FirstChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1707 | }
|
| 1708 | /// Get the first child element of this handle.
|
| 1709 | XMLHandle FirstChildElement( const char* value=0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1710 | return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1711 | }
|
| 1712 | /// Get the last child of this handle.
|
| 1713 | XMLHandle LastChild() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1714 | return XMLHandle( _node ? _node->LastChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1715 | }
|
| 1716 | /// Get the last child element of this handle.
|
| 1717 | XMLHandle LastChildElement( const char* _value=0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1718 | return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1719 | }
|
| 1720 | /// Get the previous sibling of this handle.
|
| 1721 | XMLHandle PreviousSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1722 | return XMLHandle( _node ? _node->PreviousSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1723 | }
|
| 1724 | /// Get the previous sibling element of this handle.
|
| 1725 | XMLHandle PreviousSiblingElement( const char* _value=0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1726 | return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1727 | }
|
| 1728 | /// Get the next sibling of this handle.
|
| 1729 | XMLHandle NextSibling() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1730 | return XMLHandle( _node ? _node->NextSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1731 | }
|
| 1732 | /// Get the next sibling element of this handle.
|
| 1733 | XMLHandle NextSiblingElement( const char* _value=0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1734 | return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1735 | }
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1736 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1737 | /// Safe cast to XMLNode. This can return null.
|
| 1738 | XMLNode* ToNode() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1739 | return _node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1740 | }
|
| 1741 | /// Safe cast to XMLElement. This can return null.
|
| 1742 | XMLElement* ToElement() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1743 | return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1744 | }
|
| 1745 | /// Safe cast to XMLText. This can return null.
|
| 1746 | XMLText* ToText() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1747 | return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1748 | }
|
| 1749 | /// Safe cast to XMLUnknown. This can return null.
|
| 1750 | XMLUnknown* ToUnknown() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1751 | return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1752 | }
|
| 1753 | /// Safe cast to XMLDeclaration. This can return null.
|
| 1754 | XMLDeclaration* ToDeclaration() {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1755 | return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1756 | }
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1757 |
|
| 1758 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1759 | XMLNode* _node;
|
Lee Thomason | 3ffdd39 | 2012-03-28 17:27:55 -0700 | [diff] [blame] | 1760 | };
|
| 1761 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1762 |
|
| 1763 | /**
|
Lee Thomason (grinliz) | ae209f6 | 2012-04-04 22:00:07 -0700 | [diff] [blame] | 1764 | A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
|
| 1765 | 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] | 1766 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1767 | class TINYXML2_LIB XMLConstHandle
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 1768 | {
|
| 1769 | public:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1770 | XMLConstHandle( const XMLNode* node ) {
|
| 1771 | _node = node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1772 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1773 | XMLConstHandle( const XMLNode& node ) {
|
| 1774 | _node = &node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1775 | }
|
| 1776 | XMLConstHandle( const XMLConstHandle& ref ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1777 | _node = ref._node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1778 | }
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 1779 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1780 | XMLConstHandle& operator=( const XMLConstHandle& ref ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1781 | _node = ref._node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1782 | return *this;
|
| 1783 | }
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 1784 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1785 | const XMLConstHandle FirstChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1786 | return XMLConstHandle( _node ? _node->FirstChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1787 | }
|
| 1788 | const XMLConstHandle FirstChildElement( const char* value=0 ) const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1789 | return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1790 | }
|
| 1791 | const XMLConstHandle LastChild() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1792 | return XMLConstHandle( _node ? _node->LastChild() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1793 | }
|
| 1794 | const XMLConstHandle LastChildElement( const char* _value=0 ) const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1795 | return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1796 | }
|
| 1797 | const XMLConstHandle PreviousSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1798 | return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1799 | }
|
| 1800 | const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1801 | return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1802 | }
|
| 1803 | const XMLConstHandle NextSibling() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1804 | return XMLConstHandle( _node ? _node->NextSibling() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1805 | }
|
| 1806 | const XMLConstHandle NextSiblingElement( const char* _value=0 ) const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1807 | return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1808 | }
|
Lee Thomason | 8b89981 | 2012-04-04 15:58:16 -0700 | [diff] [blame] | 1809 |
|
| 1810 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1811 | const XMLNode* ToNode() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1812 | return _node;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1813 | }
|
| 1814 | const XMLElement* ToElement() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1815 | return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1816 | }
|
| 1817 | const XMLText* ToText() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1818 | return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1819 | }
|
| 1820 | const XMLUnknown* ToUnknown() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1821 | return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1822 | }
|
| 1823 | const XMLDeclaration* ToDeclaration() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1824 | return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1825 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1826 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1827 | private:
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1828 | const XMLNode* _node;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1829 | };
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1830 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1831 |
|
| 1832 | /**
|
| 1833 | Printing functionality. The XMLPrinter gives you more
|
| 1834 | options than the XMLDocument::Print() method.
|
| 1835 |
|
| 1836 | It can:
|
| 1837 | -# Print to memory.
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 1838 | -# Print to a file you provide.
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1839 | -# Print XML without a XMLDocument.
|
| 1840 |
|
| 1841 | Print to Memory
|
| 1842 |
|
| 1843 | @verbatim
|
| 1844 | XMLPrinter printer;
|
Vasily Biryukov | 9a975b7 | 2013-05-11 21:41:42 +0600 | [diff] [blame] | 1845 | doc.Print( &printer );
|
Thomas Roß | 08bdf50 | 2012-05-12 14:21:23 +0200 | [diff] [blame] | 1846 | SomeFunction( printer.CStr() );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1847 | @endverbatim
|
| 1848 |
|
| 1849 | Print to a File
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1850 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1851 | You provide the file pointer.
|
| 1852 | @verbatim
|
| 1853 | XMLPrinter printer( fp );
|
| 1854 | doc.Print( &printer );
|
| 1855 | @endverbatim
|
| 1856 |
|
| 1857 | Print without a XMLDocument
|
| 1858 |
|
| 1859 | When loading, an XML parser is very useful. However, sometimes
|
| 1860 | when saving, it just gets in the way. The code is often set up
|
| 1861 | for streaming, and constructing the DOM is just overhead.
|
| 1862 |
|
| 1863 | The Printer supports the streaming case. The following code
|
| 1864 | prints out a trivially simple XML file without ever creating
|
| 1865 | an XML document.
|
| 1866 |
|
| 1867 | @verbatim
|
| 1868 | XMLPrinter printer( fp );
|
| 1869 | printer.OpenElement( "foo" );
|
| 1870 | printer.PushAttribute( "foo", "bar" );
|
| 1871 | printer.CloseElement();
|
| 1872 | @endverbatim
|
| 1873 | */
|
PKEuS | 16ed47d | 2013-07-06 12:02:43 +0200 | [diff] [blame] | 1874 | class TINYXML2_LIB XMLPrinter : public XMLVisitor
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1875 | {
|
| 1876 | public:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1877 | /** Construct the printer. If the FILE* is specified,
|
| 1878 | this will print to the FILE. Else it will print
|
| 1879 | to memory, and the result is available in CStr().
|
| 1880 | If 'compact' is set to true, then output is created
|
| 1881 | with only required whitespace and newlines.
|
| 1882 | */
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 1883 | XMLPrinter( FILE* file=0, bool compact = false );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1884 | ~XMLPrinter() {}
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1885 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1886 | /** If streaming, write the BOM and declaration. */
|
| 1887 | void PushHeader( bool writeBOM, bool writeDeclaration );
|
| 1888 | /** If streaming, start writing an element.
|
| 1889 | The element must be closed with CloseElement()
|
| 1890 | */
|
| 1891 | void OpenElement( const char* name );
|
| 1892 | /// If streaming, add an attribute to an open element.
|
| 1893 | void PushAttribute( const char* name, const char* value );
|
| 1894 | void PushAttribute( const char* name, int value );
|
| 1895 | void PushAttribute( const char* name, unsigned value );
|
| 1896 | void PushAttribute( const char* name, bool value );
|
| 1897 | void PushAttribute( const char* name, double value );
|
| 1898 | /// If streaming, close the Element.
|
| 1899 | void CloseElement();
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1900 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1901 | /// Add a text node.
|
| 1902 | void PushText( const char* text, bool cdata=false );
|
| 1903 | /// Add a text node from an integer.
|
| 1904 | void PushText( int value );
|
| 1905 | /// Add a text node from an unsigned.
|
| 1906 | void PushText( unsigned value );
|
| 1907 | /// Add a text node from a bool.
|
| 1908 | void PushText( bool value );
|
| 1909 | /// Add a text node from a float.
|
| 1910 | void PushText( float value );
|
| 1911 | /// Add a text node from a double.
|
| 1912 | void PushText( double value );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1913 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1914 | /// Add a comment
|
| 1915 | void PushComment( const char* comment );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1916 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1917 | void PushDeclaration( const char* value );
|
| 1918 | void PushUnknown( const char* value );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1919 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1920 | virtual bool VisitEnter( const XMLDocument& /*doc*/ );
|
| 1921 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
|
| 1922 | return true;
|
| 1923 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1924 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1925 | virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
|
| 1926 | virtual bool VisitExit( const XMLElement& element );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1927 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1928 | virtual bool Visit( const XMLText& text );
|
| 1929 | virtual bool Visit( const XMLComment& comment );
|
| 1930 | virtual bool Visit( const XMLDeclaration& declaration );
|
| 1931 | virtual bool Visit( const XMLUnknown& unknown );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1932 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1933 | /**
|
| 1934 | If in print to memory mode, return a pointer to
|
| 1935 | the XML file in memory.
|
| 1936 | */
|
| 1937 | const char* CStr() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1938 | return _buffer.Mem();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1939 | }
|
| 1940 | /**
|
| 1941 | If in print to memory mode, return the size
|
| 1942 | of the XML file in memory. (Note the size returned
|
| 1943 | includes the terminating null.)
|
| 1944 | */
|
| 1945 | int CStrSize() const {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1946 | return _buffer.Size();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1947 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1948 |
|
| 1949 | private:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1950 | void SealElement();
|
| 1951 | void PrintSpace( int depth );
|
| 1952 | void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
|
| 1953 | void Print( const char* format, ... );
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1954 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1955 | bool _elementJustOpened;
|
| 1956 | bool _firstElement;
|
Jerome Martinez | 242c3ea | 2013-01-06 12:20:04 +0100 | [diff] [blame] | 1957 | FILE* _fp;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1958 | int _depth;
|
| 1959 | int _textDepth;
|
| 1960 | bool _processEntities;
|
| 1961 | bool _compactMode;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1962 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1963 | enum {
|
| 1964 | ENTITY_RANGE = 64,
|
| 1965 | BUF_SIZE = 200
|
| 1966 | };
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1967 | bool _entityFlag[ENTITY_RANGE];
|
| 1968 | bool _restrictedEntityFlag[ENTITY_RANGE];
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1969 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1970 | DynArray< const char*, 10 > _stack;
|
| 1971 | DynArray< char, 20 > _buffer;
|
PKEuS | e736f29 | 2012-07-16 03:27:55 -0700 | [diff] [blame] | 1972 | #ifdef _MSC_VER
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1973 | DynArray< char, 20 > _accumulator;
|
PKEuS | e736f29 | 2012-07-16 03:27:55 -0700 | [diff] [blame] | 1974 | #endif
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1975 | };
|
| 1976 |
|
| 1977 |
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 1978 | } // tinyxml2
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1979 |
|
| 1980 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1981 | #endif // TINYXML2_INCLUDED
|