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