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