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