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 |
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 27 | #if 1
|
| 28 | #include <cctype>
|
| 29 | #include <climits>
|
| 30 | #include <cstdio>
|
| 31 | #include <cstring>
|
| 32 | #else
|
| 33 | // Not completely sure all the interesting systems
|
| 34 | // can handle the new headers; can switch this if
|
| 35 | // there is an include problem.
|
| 36 | #include <limits.h>
|
| 37 | #include <ctype.h>
|
| 38 | #include <stdio.h>
|
| 39 | #include <memory.h> // Needed by mac.
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 40 | #endif
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 41 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 42 | /*
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 43 | TODO: add 'lastAttribute' for faster parsing.
|
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 | /*
|
| 47 | gcc: g++ -Wall tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
|
| 48 | */
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 49 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 50 | #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
|
| 51 | #ifndef DEBUG
|
| 52 | #define DEBUG
|
| 53 | #endif
|
| 54 | #endif
|
| 55 |
|
| 56 |
|
| 57 | #if defined(DEBUG)
|
| 58 | #if defined(_MSC_VER)
|
Guillermo A. Amaral | 68b0c87 | 2012-03-24 11:07:19 -0700 | [diff] [blame] | 59 | #define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 60 | #elif defined (ANDROID_NDK)
|
| 61 | #include <android/log.h>
|
| 62 | #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
|
| 63 | #else
|
| 64 | #include <assert.h>
|
| 65 | #define TIXMLASSERT assert
|
| 66 | #endif
|
| 67 | #else
|
| 68 | #define TIXMLASSERT( x ) {}
|
| 69 | #endif
|
| 70 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 71 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 72 | // Deprecated library function hell. Compilers want to use the
|
| 73 | // new safe versions. This probably doesn't fully address the problem,
|
| 74 | // but it gets closer. There are too many compilers for me to fully
|
| 75 | // test. If you get compilation troubles, undefine TIXML_SAFE
|
| 76 |
|
| 77 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
|
| 78 | // Microsoft visual studio, version 2005 and higher.
|
| 79 | #define TIXML_SNPRINTF _snprintf_s
|
| 80 | #define TIXML_SSCANF sscanf_s
|
| 81 | #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
|
| 82 | // Microsoft visual studio, version 6 and higher.
|
| 83 | //#pragma message( "Using _sn* functions." )
|
| 84 | #define TIXML_SNPRINTF _snprintf
|
| 85 | #define TIXML_SSCANF sscanf
|
| 86 | #elif defined(__GNUC__) && (__GNUC__ >= 3 )
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 87 | // GCC version 3 and higher
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 88 | //#warning( "Using sn* functions." )
|
| 89 | #define TIXML_SNPRINTF snprintf
|
| 90 | #define TIXML_SSCANF sscanf
|
| 91 | #else
|
| 92 | #define TIXML_SNPRINTF snprintf
|
| 93 | #define TIXML_SSCANF sscanf
|
| 94 | #endif
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 95 |
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 96 | static const int TIXML2_MAJOR_VERSION = 0;
|
| 97 | static const int TIXML2_MINOR_VERSION = 9;
|
Lee Thomason | e923c5c | 2012-03-24 13:07:06 -0700 | [diff] [blame] | 98 | static const int TIXML2_PATCH_VERSION = 2;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 99 |
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 100 | namespace tinyxml2
|
| 101 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 102 | class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 103 | class XMLElement;
|
| 104 | class XMLAttribute;
|
| 105 | class XMLComment;
|
| 106 | class XMLNode;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 107 | class XMLText;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 108 | class XMLDeclaration;
|
| 109 | class XMLUnknown;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 110 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 111 | class XMLPrinter;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 112 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 113 | /*
|
| 114 | A class that wraps strings. Normally stores the start and end
|
| 115 | pointers into the XML file itself, and will apply normalization
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 116 | and entity translation if actually read. Can also store (and memory
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 117 | manage) a traditional char[]
|
| 118 | */
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 119 | class StrPair
|
| 120 | {
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 121 | public:
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 122 | enum {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 123 | NEEDS_ENTITY_PROCESSING = 0x01,
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 124 | NEEDS_NEWLINE_NORMALIZATION = 0x02,
|
| 125 |
|
| 126 | TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 127 | TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 128 | ATTRIBUTE_NAME = 0,
|
| 129 | ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 130 | ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 131 | COMMENT = NEEDS_NEWLINE_NORMALIZATION
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 132 | };
|
| 133 |
|
| 134 | StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 135 | ~StrPair();
|
| 136 |
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 137 | void Set( char* _start, char* _end, int _flags ) {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 138 | Reset();
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 139 | this->start = _start; this->end = _end; this->flags = _flags | NEEDS_FLUSH;
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 140 | }
|
| 141 | const char* GetStr();
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 142 | bool Empty() const { return start == end; }
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 143 |
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 144 | void SetInternedStr( const char* str ) { Reset(); this->start = const_cast<char*>(str); }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 145 | void SetStr( const char* str, int flags=0 );
|
| 146 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 147 | char* ParseText( char* in, const char* endTag, int strFlags );
|
| 148 | char* ParseName( char* in );
|
| 149 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 150 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 151 | private:
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 152 | void Reset();
|
| 153 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 154 | enum {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 155 | NEEDS_FLUSH = 0x100,
|
| 156 | NEEDS_DELETE = 0x200
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 157 | };
|
| 158 |
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 159 | // After parsing, if *end != 0, it can be set to zero.
|
| 160 | int flags;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 161 | char* start;
|
Lee Thomason | 39ede24 | 2012-01-20 11:27:56 -0800 | [diff] [blame] | 162 | char* end;
|
| 163 | };
|
| 164 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 165 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 166 | /*
|
| 167 | A dynamic array of Plain Old Data. Doesn't support constructors, etc.
|
| 168 | Has a small initial memory pool, so that low or no usage will not
|
| 169 | cause a call to new/delete
|
| 170 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 171 | template <class T, int INIT>
|
| 172 | class DynArray
|
| 173 | {
|
| 174 | public:
|
| 175 | DynArray< T, INIT >()
|
| 176 | {
|
| 177 | mem = pool;
|
| 178 | allocated = INIT;
|
| 179 | size = 0;
|
| 180 | }
|
| 181 | ~DynArray()
|
| 182 | {
|
| 183 | if ( mem != pool ) {
|
| 184 | delete mem;
|
| 185 | }
|
| 186 | }
|
| 187 | void Push( T t )
|
| 188 | {
|
| 189 | EnsureCapacity( size+1 );
|
| 190 | mem[size++] = t;
|
| 191 | }
|
| 192 |
|
| 193 | T* PushArr( int count )
|
| 194 | {
|
| 195 | EnsureCapacity( size+count );
|
| 196 | T* ret = &mem[size];
|
| 197 | size += count;
|
| 198 | return ret;
|
| 199 | }
|
| 200 | T Pop() {
|
| 201 | return mem[--size];
|
| 202 | }
|
| 203 | void PopArr( int count )
|
| 204 | {
|
| 205 | TIXMLASSERT( size >= count );
|
| 206 | size -= count;
|
| 207 | }
|
| 208 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 209 | bool Empty() const { return size == 0; }
|
| 210 | T& operator[](int i) { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
|
| 211 | const T& operator[](int i) const { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
|
| 212 | int Size() const { return size; }
|
| 213 | int Capacity() const { return allocated; }
|
| 214 | const T* Mem() const { return mem; }
|
| 215 | T* Mem() { return mem; }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 216 |
|
| 217 |
|
| 218 | private:
|
| 219 | void EnsureCapacity( int cap ) {
|
| 220 | if ( cap > allocated ) {
|
| 221 | int newAllocated = cap * 2;
|
| 222 | T* newMem = new T[newAllocated];
|
| 223 | memcpy( newMem, mem, sizeof(T)*size ); // warning: not using constructors, only works for PODs
|
| 224 | if ( mem != pool ) delete [] mem;
|
| 225 | mem = newMem;
|
| 226 | allocated = newAllocated;
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | T* mem;
|
| 231 | T pool[INIT];
|
| 232 | int allocated; // objects allocated
|
| 233 | int size; // number objects in use
|
| 234 | };
|
| 235 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 236 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 237 | /*
|
| 238 | Parent virtual class a a pool for fast allocation
|
| 239 | and deallocation of objects.
|
| 240 | */
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 241 | class MemPool
|
| 242 | {
|
| 243 | public:
|
| 244 | MemPool() {}
|
| 245 | virtual ~MemPool() {}
|
| 246 |
|
| 247 | virtual int ItemSize() const = 0;
|
| 248 | virtual void* Alloc() = 0;
|
| 249 | virtual void Free( void* ) = 0;
|
| 250 | };
|
| 251 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 252 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 253 | /*
|
| 254 | Template child class to create pools of the correct type.
|
| 255 | */
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 256 | template< int SIZE >
|
| 257 | class MemPoolT : public MemPool
|
| 258 | {
|
| 259 | public:
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 260 | MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 261 | ~MemPoolT() {
|
| 262 | // Delete the blocks.
|
| 263 | for( int i=0; i<blockPtrs.Size(); ++i ) {
|
| 264 | delete blockPtrs[i];
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | virtual int ItemSize() const { return SIZE; }
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 269 | int CurrentAllocs() const { return currentAllocs; }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 270 |
|
| 271 | virtual void* Alloc() {
|
| 272 | if ( !root ) {
|
| 273 | // Need a new block.
|
| 274 | Block* block = new Block();
|
| 275 | blockPtrs.Push( block );
|
| 276 |
|
| 277 | for( int i=0; i<COUNT-1; ++i ) {
|
| 278 | block->chunk[i].next = &block->chunk[i+1];
|
| 279 | }
|
| 280 | block->chunk[COUNT-1].next = 0;
|
| 281 | root = block->chunk;
|
| 282 | }
|
| 283 | void* result = root;
|
| 284 | root = root->next;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 285 |
|
| 286 | ++currentAllocs;
|
| 287 | if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs;
|
| 288 | nAllocs++;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 289 | return result;
|
| 290 | }
|
| 291 | virtual void Free( void* mem ) {
|
| 292 | if ( !mem ) return;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 293 | --currentAllocs;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 294 | Chunk* chunk = (Chunk*)mem;
|
| 295 | memset( chunk, 0xfe, sizeof(Chunk) );
|
| 296 | chunk->next = root;
|
| 297 | root = chunk;
|
| 298 | }
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 299 | void Trace( const char* name ) {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 300 | printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
|
| 301 | name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() );
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 302 | }
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 303 |
|
| 304 | private:
|
| 305 | enum { COUNT = 1024/SIZE };
|
| 306 | union Chunk {
|
| 307 | Chunk* next;
|
| 308 | char mem[SIZE];
|
| 309 | };
|
| 310 | struct Block {
|
| 311 | Chunk chunk[COUNT];
|
| 312 | };
|
| 313 | DynArray< Block*, 10 > blockPtrs;
|
| 314 | Chunk* root;
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 315 |
|
| 316 | int currentAllocs;
|
| 317 | int nAllocs;
|
| 318 | int maxAllocs;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 319 | };
|
| 320 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 321 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 322 |
|
| 323 | /**
|
| 324 | Implements the interface to the "Visitor pattern" (see the Accept() method.)
|
| 325 | If you call the Accept() method, it requires being passed a XMLVisitor
|
| 326 | class to handle callbacks. For nodes that contain other nodes (Document, Element)
|
| 327 | you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
|
| 328 | are simply called with Visit().
|
| 329 |
|
| 330 | If you return 'true' from a Visit method, recursive parsing will continue. If you return
|
| 331 | false, <b>no children of this node or its sibilings</b> will be Visited.
|
| 332 |
|
| 333 | All flavors of Visit methods have a default implementation that returns 'true' (continue
|
| 334 | visiting). You need to only override methods that are interesting to you.
|
| 335 |
|
| 336 | Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
|
| 337 |
|
| 338 | You should never change the document from a callback.
|
| 339 |
|
| 340 | @sa XMLNode::Accept()
|
| 341 | */
|
| 342 | class XMLVisitor
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 343 | {
|
| 344 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 345 | virtual ~XMLVisitor() {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 346 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 347 | /// Visit a document.
|
| 348 | virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; }
|
| 349 | /// Visit a document.
|
| 350 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
|
| 351 |
|
| 352 | /// Visit an element.
|
| 353 | virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { return true; }
|
| 354 | /// Visit an element.
|
| 355 | virtual bool VisitExit( const XMLElement& /*element*/ ) { return true; }
|
| 356 |
|
| 357 | /// Visit a declaration
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 358 | virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { return true; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 359 | /// Visit a text node
|
| 360 | virtual bool Visit( const XMLText& /*text*/ ) { return true; }
|
| 361 | /// Visit a comment node
|
| 362 | virtual bool Visit( const XMLComment& /*comment*/ ) { return true; }
|
| 363 | /// Visit an unknown node
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 364 | virtual bool Visit( const XMLUnknown& /*unknown*/ ) { return true; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 365 | };
|
| 366 |
|
| 367 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 368 | /*
|
| 369 | Utility functionality.
|
| 370 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 371 | class XMLUtil
|
| 372 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 373 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 374 | // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
|
| 375 | // correct, but simple, and usually works.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 376 | static const char* SkipWhiteSpace( const char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; }
|
| 377 | static char* SkipWhiteSpace( char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 378 |
|
| 379 | inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
|
| 380 | int n = 0;
|
Lee Thomason | d34f52c | 2012-01-20 12:55:24 -0800 | [diff] [blame] | 381 | if ( p == q ) {
|
| 382 | return true;
|
| 383 | }
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 384 | while( *p && *q && *p == *q && n<nChar ) {
|
| 385 | ++p; ++q; ++n;
|
| 386 | }
|
| 387 | if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
|
| 388 | return true;
|
| 389 | }
|
| 390 | return false;
|
| 391 | }
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 392 | inline static int IsUTF8Continuation( const char p ) { return p & 0x80; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 393 | inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalnum( anyByte ) : 1; }
|
| 394 | inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalpha( anyByte ) : 1; }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 395 |
|
| 396 | static const char* ReadBOM( const char* p, bool* hasBOM );
|
| 397 | // p is the starting location,
|
| 398 | // the UTF-8 value of the entity will be placed in value, and length filled in.
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 399 | static const char* GetCharacterRef( const char* p, char* value, int* length );
|
| 400 | static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 401 | };
|
| 402 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 403 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 404 | /** XMLNode is a base class for every object that is in the
|
| 405 | XML Document Object Model (DOM), except XMLAttributes.
|
| 406 | Nodes have siblings, a parent, and children which can
|
| 407 | be navigated. A node is always in a XMLDocument.
|
| 408 | The type of a TiXmlNode can be queried, and it can
|
| 409 | be cast to its more defined type.
|
| 410 |
|
| 411 | An XMLDocument allocates memory for all its Nodes.
|
| 412 | When the XMLDocument gets deleted, all its Nodes
|
| 413 | will also be deleted.
|
| 414 |
|
| 415 | @verbatim
|
| 416 | A Document can contain: Element (container or leaf)
|
| 417 | Comment (leaf)
|
| 418 | Unknown (leaf)
|
| 419 | Declaration( leaf )
|
| 420 |
|
| 421 | An Element can contain: Element (container or leaf)
|
| 422 | Text (leaf)
|
| 423 | Attributes (not on tree)
|
| 424 | Comment (leaf)
|
| 425 | Unknown (leaf)
|
| 426 |
|
| 427 | @endverbatim
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 428 | */
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 429 | class XMLNode
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 430 | {
|
| 431 | friend class XMLDocument;
|
| 432 | friend class XMLElement;
|
| 433 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 434 |
|
| 435 | /// Get the XMLDocument that owns this XMLNode.
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 436 | const XMLDocument* GetDocument() const { return document; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 437 | /// Get the XMLDocument that owns this XMLNode.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 438 | XMLDocument* GetDocument() { return document; }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 439 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 440 | virtual XMLElement* ToElement() { return 0; } ///< Safely cast to an Element, or null.
|
| 441 | virtual XMLText* ToText() { return 0; } ///< Safely cast to Text, or null.
|
| 442 | virtual XMLComment* ToComment() { return 0; } ///< Safely cast to a Comment, or null.
|
| 443 | virtual XMLDocument* ToDocument() { return 0; } ///< Safely cast to a Document, or null.
|
| 444 | virtual XMLDeclaration* ToDeclaration() { return 0; } ///< Safely cast to a Declaration, or null.
|
| 445 | virtual XMLUnknown* ToUnknown() { return 0; } ///< Safely cast to an Unknown, or null.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 446 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 447 | virtual const XMLElement* ToElement() const { return 0; }
|
| 448 | virtual const XMLText* ToText() const { return 0; }
|
| 449 | virtual const XMLComment* ToComment() const { return 0; }
|
| 450 | virtual const XMLDocument* ToDocument() const { return 0; }
|
| 451 | virtual const XMLDeclaration* ToDeclaration() const { return 0; }
|
| 452 | virtual const XMLUnknown* ToUnknown() const { return 0; }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 453 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 454 | /** The meaning of 'value' changes for the specific type.
|
| 455 | @verbatim
|
| 456 | Document: empy
|
| 457 | Element: name of the element
|
| 458 | Comment: the comment text
|
| 459 | Unknown: the tag contents
|
| 460 | Text: the text string
|
| 461 | @endverbatim
|
| 462 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 463 | const char* Value() const { return value.GetStr(); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 464 | /** Set the Value of an XML node.
|
| 465 | @sa Value()
|
| 466 | */
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 467 | void SetValue( const char* val, bool staticMem=false );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 468 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 469 | /// Get the parent of this node on the DOM.
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 470 | const XMLNode* Parent() const { return parent; }
|
| 471 | XMLNode* Parent() { return parent; }
|
| 472 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 473 | /// Returns true if this node has no children.
|
| 474 | bool NoChildren() const { return !firstChild; }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 475 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 476 | /// Get the first child node, or null if none exists.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 477 | const XMLNode* FirstChild() const { return firstChild; }
|
| 478 | XMLNode* FirstChild() { return firstChild; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 479 | /** Get the first child element, or optionally the first child
|
| 480 | element with the specified name.
|
| 481 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 482 | const XMLElement* FirstChildElement( const char* value=0 ) const;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 483 | XMLElement* FirstChildElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( _value )); }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 484 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 485 | /// Get the last child node, or null if none exists.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 486 | const XMLNode* LastChild() const { return lastChild; }
|
| 487 | XMLNode* LastChild() { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() ); }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 488 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 489 | /** Get the last child element or optionally the last child
|
| 490 | element with the specified name.
|
| 491 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 492 | const XMLElement* LastChildElement( const char* value=0 ) const;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 493 | XMLElement* LastChildElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(_value) ); }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 494 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 495 | /// Get the previous (left) sibling node of this node.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 496 | const XMLNode* PreviousSibling() const { return prev; }
|
| 497 | XMLNode* PreviousSibling() { return prev; }
|
| 498 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 499 | /// Get the previous (left) sibling element of this node, with an opitionally supplied name.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 500 | const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 501 | XMLElement* PreviousSiblingElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( _value ) ); }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 502 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 503 | /// Get the next (right) sibling node of this node.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 504 | const XMLNode* NextSibling() const { return next; }
|
| 505 | XMLNode* NextSibling() { return next; }
|
| 506 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 507 | /// Get the next (right) sibling element of this node, with an opitionally supplied name.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 508 | const XMLElement* NextSiblingElement( const char* value=0 ) const;
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 509 | XMLElement* NextSiblingElement( const char* _value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( _value ) ); }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 510 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 511 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 512 | Add a child node as the last (right) child.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 513 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 514 | XMLNode* InsertEndChild( XMLNode* addThis );
|
Lee Thomason | 618dbf8 | 2012-02-28 12:34:27 -0800 | [diff] [blame] | 515 |
|
| 516 | XMLNode* LinkEndChild( XMLNode* addThis ) { return InsertEndChild( addThis ); }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 517 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 518 | Add a child node as the first (left) child.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 519 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 520 | XMLNode* InsertFirstChild( XMLNode* addThis );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 521 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 522 | Add a node after the specified child node.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 523 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 524 | XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
|
| 525 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 526 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 527 | Delete all the children of this node.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 528 | */
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 529 | void DeleteChildren();
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 530 |
|
| 531 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 532 | Delete a child of this node.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 533 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 534 | void DeleteChild( XMLNode* node );
|
| 535 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 536 | /**
|
| 537 | Make a copy of this node, but not its children.
|
| 538 | You may pass in a Document pointer that will be
|
| 539 | the owner of the new Node. If the 'document' is
|
| 540 | null, then the node returned will be allocated
|
| 541 | from the current Document. (this->GetDocument())
|
| 542 |
|
| 543 | Note: if called on a XMLDocument, this will return null.
|
| 544 | */
|
| 545 | virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
|
| 546 |
|
| 547 | /**
|
| 548 | Test if 2 nodes are the same, but don't test children.
|
| 549 | The 2 nodes do not need to be in the same Document.
|
| 550 |
|
| 551 | Note: if called on a XMLDocument, this will return false.
|
| 552 | */
|
| 553 | virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
|
| 554 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 555 | /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the
|
| 556 | XML tree will be conditionally visited and the host will be called back
|
| 557 | via the TiXmlVisitor interface.
|
| 558 |
|
| 559 | This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
|
| 560 | the XML for the callbacks, so the performance of TinyXML is unchanged by using this
|
| 561 | interface versus any other.)
|
| 562 |
|
| 563 | The interface has been based on ideas from:
|
| 564 |
|
| 565 | - http://www.saxproject.org/
|
| 566 | - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
|
| 567 |
|
| 568 | Which are both good references for "visiting".
|
| 569 |
|
| 570 | An example of using Accept():
|
| 571 | @verbatim
|
| 572 | TiXmlPrinter printer;
|
| 573 | tinyxmlDoc.Accept( &printer );
|
| 574 | const char* xmlcstr = printer.CStr();
|
| 575 | @endverbatim
|
| 576 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 577 | virtual bool Accept( XMLVisitor* visitor ) const = 0;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 578 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 579 | // internal
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 580 | virtual char* ParseDeep( char*, StrPair* );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 581 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 582 | protected:
|
| 583 | XMLNode( XMLDocument* );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 584 | virtual ~XMLNode();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 585 | XMLNode( const XMLNode& ); // not supported
|
| 586 | void operator=( const XMLNode& ); // not supported
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 587 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 588 | XMLDocument* document;
|
| 589 | XMLNode* parent;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 590 | mutable StrPair value;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 591 |
|
| 592 | XMLNode* firstChild;
|
| 593 | XMLNode* lastChild;
|
| 594 |
|
| 595 | XMLNode* prev;
|
| 596 | XMLNode* next;
|
| 597 |
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 598 | private:
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 599 | MemPool* memPool;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 600 | void Unlink( XMLNode* child );
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 601 | };
|
| 602 |
|
| 603 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 604 | /** XML text.
|
| 605 |
|
| 606 | Note that a text node can have child element nodes, for example:
|
| 607 | @verbatim
|
| 608 | <root>This is <b>bold</b></root>
|
| 609 | @endverbatim
|
| 610 |
|
| 611 | A text node can have 2 ways to output the next. "normal" output
|
| 612 | and CDATA. It will default to the mode it was parsed from the XML file and
|
| 613 | you generally want to leave it alone, but you can change the output mode with
|
| 614 | SetCDATA() and query it with CDATA().
|
| 615 | */
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 616 | class XMLText : public XMLNode
|
| 617 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 618 | friend class XMLBase;
|
| 619 | friend class XMLDocument;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 620 | public:
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 621 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 622 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 623 | virtual XMLText* ToText() { return this; }
|
| 624 | virtual const XMLText* ToText() const { return this; }
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 625 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 626 | /// Declare whether this should be CDATA or standard text.
|
Lee Thomason | 5ce8941 | 2012-03-20 13:23:44 -0700 | [diff] [blame] | 627 | void SetCData( bool _isCData ) { this->isCData = _isCData; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 628 | /// Returns true if this is a CDATA text element.
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 629 | bool CData() const { return isCData; }
|
| 630 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 631 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 632 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 633 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
| 634 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 635 |
|
| 636 | protected:
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 637 | XMLText( XMLDocument* doc ) : XMLNode( doc ), isCData( false ) {}
|
| 638 | virtual ~XMLText() {}
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 639 | XMLText( const XMLText& ); // not supported
|
| 640 | void operator=( const XMLText& ); // not supported
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 641 |
|
| 642 | private:
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 643 | bool isCData;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 644 | };
|
| 645 |
|
| 646 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 647 | /** An XML Comment. */
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 648 | class XMLComment : public XMLNode
|
| 649 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 650 | friend class XMLDocument;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 651 | public:
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 652 | virtual XMLComment* ToComment() { return this; }
|
| 653 | virtual const XMLComment* ToComment() const { return this; }
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 654 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 655 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 656 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 657 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 658 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 659 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 660 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 661 | protected:
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 662 | XMLComment( XMLDocument* doc );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 663 | virtual ~XMLComment();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 664 | XMLComment( const XMLComment& ); // not supported
|
| 665 | void operator=( const XMLComment& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 666 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 667 | private:
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 668 | };
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 669 |
|
| 670 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 671 | /** In correct XML the declaration is the first entry in the file.
|
| 672 | @verbatim
|
| 673 | <?xml version="1.0" standalone="yes"?>
|
| 674 | @endverbatim
|
| 675 |
|
| 676 | TinyXML2 will happily read or write files without a declaration,
|
| 677 | however.
|
| 678 |
|
| 679 | The text of the declaration isn't interpreted. It is parsed
|
| 680 | and written as a string.
|
| 681 | */
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 682 | class XMLDeclaration : public XMLNode
|
| 683 | {
|
| 684 | friend class XMLDocument;
|
| 685 | public:
|
| 686 | virtual XMLDeclaration* ToDeclaration() { return this; }
|
| 687 | virtual const XMLDeclaration* ToDeclaration() const { return this; }
|
| 688 |
|
| 689 | virtual bool Accept( XMLVisitor* visitor ) const;
|
| 690 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 691 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 692 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 693 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 694 |
|
| 695 | protected:
|
| 696 | XMLDeclaration( XMLDocument* doc );
|
| 697 | virtual ~XMLDeclaration();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 698 | XMLDeclaration( const XMLDeclaration& ); // not supported
|
| 699 | void operator=( const XMLDeclaration& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 700 | };
|
| 701 |
|
| 702 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 703 | /** Any tag that tinyXml doesn't recognize is saved as an
|
| 704 | unknown. It is a tag of text, but should not be modified.
|
| 705 | It will be written back to the XML, unchanged, when the file
|
| 706 | is saved.
|
| 707 |
|
| 708 | DTD tags get thrown into TiXmlUnknowns.
|
| 709 | */
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 710 | class XMLUnknown : public XMLNode
|
| 711 | {
|
| 712 | friend class XMLDocument;
|
| 713 | public:
|
| 714 | virtual XMLUnknown* ToUnknown() { return this; }
|
| 715 | virtual const XMLUnknown* ToUnknown() const { return this; }
|
| 716 |
|
| 717 | virtual bool Accept( XMLVisitor* visitor ) const;
|
| 718 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 719 | char* ParseDeep( char*, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 720 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 721 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 722 |
|
| 723 | protected:
|
| 724 | XMLUnknown( XMLDocument* doc );
|
| 725 | virtual ~XMLUnknown();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 726 | XMLUnknown( const XMLUnknown& ); // not supported
|
| 727 | void operator=( const XMLUnknown& ); // not supported
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 728 | };
|
| 729 |
|
| 730 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 731 | enum {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 732 | XML_NO_ERROR = 0,
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 733 | XML_SUCCESS = 0,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 734 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 735 | XML_NO_ATTRIBUTE,
|
| 736 | XML_WRONG_ATTRIBUTE_TYPE,
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 737 |
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 738 | XML_ERROR_FILE_NOT_FOUND,
|
Lee Thomason | 7f7b162 | 2012-03-24 12:49:03 -0700 | [diff] [blame] | 739 | XML_ERROR_FILE_COULD_NOT_BE_OPENED,
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 740 | XML_ERROR_ELEMENT_MISMATCH,
|
| 741 | XML_ERROR_PARSING_ELEMENT,
|
| 742 | XML_ERROR_PARSING_ATTRIBUTE,
|
| 743 | XML_ERROR_IDENTIFYING_TAG,
|
| 744 | XML_ERROR_PARSING_TEXT,
|
| 745 | XML_ERROR_PARSING_CDATA,
|
| 746 | XML_ERROR_PARSING_COMMENT,
|
| 747 | XML_ERROR_PARSING_DECLARATION,
|
| 748 | XML_ERROR_PARSING_UNKNOWN,
|
| 749 | XML_ERROR_EMPTY_DOCUMENT,
|
| 750 | XML_ERROR_MISMATCHED_ELEMENT,
|
| 751 | XML_ERROR_PARSING
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 752 | };
|
| 753 |
|
| 754 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 755 | /** An attribute is a name-value pair. Elements have an arbitrary
|
| 756 | number of attributes, each with a unique name.
|
| 757 |
|
| 758 | @note The attributes are not XMLNodes. You may only query the
|
| 759 | Next() attribute in a list.
|
| 760 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 761 | class XMLAttribute
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 762 | {
|
| 763 | friend class XMLElement;
|
| 764 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 765 | const char* Name() const { return name.GetStr(); } ///< The name of the attribute.
|
| 766 | const char* Value() const { return value.GetStr(); } ///< The value of the attribute.
|
| 767 | const XMLAttribute* Next() const { return next; } ///< The next attribute in the list.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 768 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 769 | /** IntAttribute interprets the attribute as an integer, and returns the value.
|
| 770 | If the value isn't an integer, 0 will be returned. There is no error checking;
|
| 771 | use QueryIntAttribute() if you need error checking.
|
| 772 | */
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 773 | int IntValue() const { int i=0; QueryIntValue( &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 774 | /// Query as an unsigned integer. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 775 | unsigned UnsignedValue() const { unsigned i=0; QueryUnsignedValue( &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 776 | /// Query as a boolean. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 777 | bool BoolValue() const { bool b=false; QueryBoolValue( &b ); return b; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 778 | /// Query as a double. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 779 | double DoubleValue() const { double d=0; QueryDoubleValue( &d ); return d; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 780 | /// Query as a float. See IntAttribute()
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 781 | float FloatValue() const { float f=0; QueryFloatValue( &f ); return f; }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 782 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 783 | /** QueryIntAttribute interprets the attribute as an integer, and returns the value
|
| 784 | in the provided paremeter. The function will return XML_NO_ERROR on success,
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 785 | and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 786 | */
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 787 | int QueryIntValue( int* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 788 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 789 | int QueryUnsignedValue( unsigned int* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 790 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 791 | int QueryBoolValue( bool* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 792 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 793 | int QueryDoubleValue( double* value ) const;
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 794 | /// See QueryIntAttribute
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 795 | int QueryFloatValue( float* value ) const;
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 796 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 797 | /// Set the attribute to a string value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 798 | void SetAttribute( const char* value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 799 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 800 | void SetAttribute( int value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 801 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 802 | void SetAttribute( unsigned value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 803 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 804 | void SetAttribute( bool value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 805 | /// Set the attribute to value.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 806 | void SetAttribute( double value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 807 | /// Set the attribute to value.
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 808 | void SetAttribute( float value );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 809 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 810 | private:
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 811 | enum { BUF_SIZE = 200 };
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 812 |
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 813 | XMLAttribute() : next( 0 ) {}
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 814 | virtual ~XMLAttribute() {}
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 815 | XMLAttribute( const XMLAttribute& ); // not supported
|
| 816 | void operator=( const XMLAttribute& ); // not supported
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 817 | void SetName( const char* name );
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 818 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 819 | char* ParseDeep( char* p, bool processEntities );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 820 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 821 | mutable StrPair name;
|
| 822 | mutable StrPair value;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 823 | XMLAttribute* next;
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 824 | MemPool* memPool;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 825 | };
|
| 826 |
|
| 827 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 828 | /** The element is a container class. It has a value, the element name,
|
| 829 | and can contain other elements, text, comments, and unknowns.
|
| 830 | Elements also contain an arbitrary number of attributes.
|
| 831 | */
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 832 | class XMLElement : public XMLNode
|
| 833 | {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 834 | friend class XMLBase;
|
| 835 | friend class XMLDocument;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 836 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 837 | /// Get the name of an element (which is the Value() of the node.)
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 838 | const char* Name() const { return Value(); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 839 | /// Set the name of the element.
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 840 | void SetName( const char* str, bool staticMem=false ) { SetValue( str, staticMem ); }
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 841 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 842 | virtual XMLElement* ToElement() { return this; }
|
| 843 | virtual const XMLElement* ToElement() const { return this; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 844 | virtual bool Accept( XMLVisitor* visitor ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 845 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 846 | /** Given an attribute name, Attribute() returns the value
|
Lee Thomason | 9225815 | 2012-03-24 13:05:39 -0700 | [diff] [blame] | 847 | for the attribute of that name, or null if none
|
| 848 | exists. For example:
|
| 849 |
|
| 850 | @verbatim
|
| 851 | const char* value = ele->Attribute( "foo" );
|
| 852 | @endverbatim
|
| 853 |
|
| 854 | The 'value' parameter is normally null. However, if specified,
|
| 855 | the attribute will only be returned if the 'name' and 'value'
|
| 856 | match. This allow you to write code:
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 857 |
|
| 858 | @verbatim
|
| 859 | if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
|
| 860 | @endverbatim
|
| 861 |
|
| 862 | rather than:
|
| 863 | @verbatim
|
| 864 | if ( ele->Attribute( "foo" ) ) {
|
| 865 | if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
|
| 866 | }
|
| 867 | @endverbatim
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 868 | */
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 869 | const char* Attribute( const char* name, const char* value=0 ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 870 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 871 | /** Given an attribute name, IntAttribute() returns the value
|
| 872 | of the attribute interpreted as an integer. 0 will be
|
| 873 | returned if there is an error. For a method with error
|
| 874 | checking, see QueryIntAttribute()
|
| 875 | */
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 876 | int IntAttribute( const char* name ) const { int i=0; QueryIntAttribute( name, &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 877 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 878 | unsigned UnsignedAttribute( const char* name ) const{ unsigned i=0; QueryUnsignedAttribute( name, &i ); return i; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 879 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 880 | bool BoolAttribute( const char* name ) const { bool b=false; QueryBoolAttribute( name, &b ); return b; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 881 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 882 | double DoubleAttribute( const char* name ) const { double d=0; QueryDoubleAttribute( name, &d ); return d; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 883 | /// See IntAttribute()
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 884 | float FloatAttribute( const char* name ) const { float f=0; QueryFloatAttribute( name, &f ); return f; }
|
| 885 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 886 | /** Given an attribute name, QueryIntAttribute() returns
|
Guillermo A. Amaral | 2eb7003 | 2012-03-20 11:26:57 -0700 | [diff] [blame] | 887 | XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
|
| 888 | can't be performed, or XML_NO_ATTRIBUTE if the attribute
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 889 | doesn't exist. If successful, the result of the conversion
|
| 890 | will be written to 'value'. If not successful, nothing will
|
| 891 | be written to 'value'. This allows you to provide default
|
| 892 | value:
|
| 893 |
|
| 894 | @verbatim
|
| 895 | int value = 10;
|
| 896 | QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
|
| 897 | @endverbatim
|
| 898 | */
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 899 | int QueryIntAttribute( const char* name, int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryIntValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 900 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 901 | int QueryUnsignedAttribute( const char* name, unsigned int* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryUnsignedValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 902 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 903 | int QueryBoolAttribute( const char* name, bool* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryBoolValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 904 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 905 | int QueryDoubleAttribute( const char* name, double* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryDoubleValue( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 906 | /// See QueryIntAttribute()
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 907 | int QueryFloatAttribute( const char* name, float* _value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return XML_NO_ATTRIBUTE; return a->QueryFloatValue( _value ); }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 908 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 909 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 910 | void SetAttribute( const char* name, const char* _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 911 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 912 | void SetAttribute( const char* name, int _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 913 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 914 | void SetAttribute( const char* name, unsigned _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 915 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 916 | void SetAttribute( const char* name, bool _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 917 | /// Sets the named attribute to value.
|
Guillermo A. Amaral | 9a6c6b8 | 2012-03-24 17:13:25 -0700 | [diff] [blame] | 918 | void SetAttribute( const char* name, double _value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( _value ); }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 919 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 920 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 921 | Delete an attribute.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 922 | */
|
| 923 | void DeleteAttribute( const char* name );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 924 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 925 | /// Return the first attribute in the list.
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 926 | const XMLAttribute* FirstAttribute() const { return rootAttribute; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 927 | /// Query a specific attribute in the list.
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 928 | const XMLAttribute* FindAttribute( const char* name ) const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 929 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 930 | /** Convenience function for easy access to the text inside an element. Although easy
|
| 931 | and concise, GetText() is limited compared to getting the TiXmlText child
|
| 932 | and accessing it directly.
|
| 933 |
|
| 934 | If the first child of 'this' is a TiXmlText, the GetText()
|
| 935 | returns the character string of the Text node, else null is returned.
|
| 936 |
|
| 937 | This is a convenient method for getting the text of simple contained text:
|
| 938 | @verbatim
|
| 939 | <foo>This is text</foo>
|
| 940 | const char* str = fooElement->GetText();
|
| 941 | @endverbatim
|
| 942 |
|
| 943 | 'str' will be a pointer to "This is text".
|
| 944 |
|
| 945 | Note that this function can be misleading. If the element foo was created from
|
| 946 | this XML:
|
| 947 | @verbatim
|
| 948 | <foo><b>This is text</b></foo>
|
| 949 | @endverbatim
|
| 950 |
|
| 951 | then the value of str would be null. The first child node isn't a text node, it is
|
| 952 | another element. From this XML:
|
| 953 | @verbatim
|
| 954 | <foo>This is <b>text</b></foo>
|
| 955 | @endverbatim
|
| 956 | GetText() will return "This is ".
|
| 957 | */
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 958 | const char* GetText() const;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 959 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 960 | // internal:
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 961 | enum {
|
| 962 | OPEN, // <foo>
|
| 963 | CLOSED, // <foo/>
|
| 964 | CLOSING // </foo>
|
| 965 | };
|
| 966 | int ClosingType() const { return closingType; }
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 967 | char* ParseDeep( char* p, StrPair* endTag );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 968 | virtual XMLNode* ShallowClone( XMLDocument* document ) const;
|
| 969 | virtual bool ShallowEqual( const XMLNode* compare ) const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 970 |
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 971 | private:
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 972 | XMLElement( XMLDocument* doc );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 973 | virtual ~XMLElement();
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 974 | XMLElement( const XMLElement& ); // not supported
|
| 975 | void operator=( const XMLElement& ); // not supported
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 976 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 977 | XMLAttribute* FindAttribute( const char* name );
|
| 978 | XMLAttribute* FindOrCreateAttribute( const char* name );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 979 | void LinkAttribute( XMLAttribute* attrib );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 980 | char* ParseAttributes( char* p );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 981 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 982 | int closingType;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 983 | XMLAttribute* rootAttribute;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 984 | };
|
| 985 |
|
| 986 |
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 987 | /** A document binds together all the functionality.
|
| 988 | It can be saved, loaded, and printed to the screen.
|
| 989 | All Nodes are connected and allocated to a Document.
|
| 990 | If the Document is deleted, all its Nodes are also deleted.
|
| 991 | */
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 992 | class XMLDocument : public XMLNode
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 993 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 994 | friend class XMLElement;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 995 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 996 | /// constructor
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 997 | XMLDocument( bool processEntities = true );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 998 | ~XMLDocument();
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 999 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1000 | virtual XMLDocument* ToDocument() { return this; }
|
| 1001 | virtual const XMLDocument* ToDocument() const { return this; }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1002 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1003 | /**
|
| 1004 | Parse an XML file from a character string.
|
| 1005 | Returns XML_NO_ERROR (0) on success, or
|
| 1006 | an errorID.
|
| 1007 | */
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1008 | int Parse( const char* xml );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1009 | /**
|
| 1010 | Load an XML file from disk.
|
| 1011 | Returns XML_NO_ERROR (0) on success, or
|
| 1012 | an errorID.
|
| 1013 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1014 | int LoadFile( const char* filename );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1015 | /**
|
| 1016 | Load an XML file from disk. You are responsible
|
| 1017 | for providing and closing the FILE*.
|
| 1018 |
|
| 1019 | Returns XML_NO_ERROR (0) on success, or
|
| 1020 | an errorID.
|
| 1021 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1022 | int LoadFile( FILE* );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1023 | /**
|
| 1024 | Save the XML file to disk.
|
| 1025 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1026 | void SaveFile( const char* filename );
|
| 1027 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1028 | bool ProcessEntities() const { return processEntities; }
|
| 1029 |
|
| 1030 | /**
|
| 1031 | Returns true if this document has a leading Byte Order Mark of UTF8.
|
| 1032 | */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1033 | bool HasBOM() const { return writeBOM; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1034 |
|
| 1035 | /** Return the root element of DOM. Equivalent to FirstChildElement().
|
| 1036 | To get the first node, use FirstChild().
|
| 1037 | */
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1038 | XMLElement* RootElement() { return FirstChildElement(); }
|
| 1039 | const XMLElement* RootElement() const { return FirstChildElement(); }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1040 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1041 | /** Print the Document. If the Printer is not provided, it will
|
| 1042 | print to stdout. If you provide Printer, this can print to a file:
|
| 1043 | @verbatim
|
| 1044 | XMLPrinter printer( fp );
|
| 1045 | doc.Print( &printer );
|
| 1046 | @endverbatim
|
| 1047 |
|
| 1048 | Or you can use a printer to print to memory:
|
| 1049 | @verbatim
|
| 1050 | XMLPrinter printer;
|
| 1051 | doc->Print( &printer );
|
Lee Thomason (grinliz) | 2812986 | 2012-02-25 21:11:20 -0800 | [diff] [blame] | 1052 | // printer.CStr() has a const char* to the XML
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1053 | @endverbatim
|
| 1054 | */
|
| 1055 | void Print( XMLPrinter* streamer=0 );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1056 | virtual bool Accept( XMLVisitor* visitor ) const;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1057 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1058 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1059 | Create a new Element associated with
|
| 1060 | this Document. The memory for the Element
|
| 1061 | is managed by the Document.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1062 | */
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1063 | XMLElement* NewElement( const char* name );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1064 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1065 | Create a new Comment associated with
|
| 1066 | this Document. The memory for the Comment
|
| 1067 | is managed by the Document.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1068 | */
|
| 1069 | XMLComment* NewComment( const char* comment );
|
| 1070 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1071 | Create a new Text associated with
|
| 1072 | this Document. The memory for the Text
|
| 1073 | is managed by the Document.
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1074 | */
|
| 1075 | XMLText* NewText( const char* text );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1076 | /**
|
| 1077 | Create a new Declaration associated with
|
| 1078 | this Document. The memory for the object
|
| 1079 | is managed by the Document.
|
| 1080 | */
|
| 1081 | XMLDeclaration* NewDeclaration( const char* text );
|
| 1082 | /**
|
| 1083 | Create a new Unknown associated with
|
| 1084 | this Document. The memory for the object
|
| 1085 | is managed by the Document.
|
| 1086 | */
|
| 1087 | XMLUnknown* NewUnknown( const char* text );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1088 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1089 | /**
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1090 | Delete a node associated with this documented.
|
| 1091 | It will be unlinked from the DOM.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1092 | */
|
| 1093 | void DeleteNode( XMLNode* node ) { node->parent->DeleteChild( node ); }
|
| 1094 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1095 | void SetError( int error, const char* str1, const char* str2 );
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1096 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1097 | /// Return true if there was an error parsing the document.
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1098 | bool Error() const { return errorID != XML_NO_ERROR; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1099 | /// Return the errorID.
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1100 | int ErrorID() const { return errorID; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1101 | /// Return a possibly helpful diagnostic location or string.
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1102 | const char* GetErrorStr1() const { return errorStr1; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1103 | /// Return possibly helpful secondary diagnostic location or string.
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1104 | const char* GetErrorStr2() const { return errorStr2; }
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1105 | /// If there is an error, print it to stdout
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1106 | void PrintError() const;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1107 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1108 | // internal
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1109 | char* Identify( char* p, XMLNode** node );
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1110 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1111 | virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const { return 0; }
|
| 1112 | virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const { return false; }
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1113 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1114 | private:
|
Lee Thomason | 50adb4c | 2012-02-13 15:07:09 -0800 | [diff] [blame] | 1115 | XMLDocument( const XMLDocument& ); // not supported
|
| 1116 | void operator=( const XMLDocument& ); // not supported
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1117 | void InitDocument();
|
| 1118 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1119 | bool writeBOM;
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1120 | bool processEntities;
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1121 | int errorID;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1122 | const char* errorStr1;
|
| 1123 | const char* errorStr2;
|
| 1124 | char* charBuffer;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1125 |
|
| 1126 | MemPoolT< sizeof(XMLElement) > elementPool;
|
| 1127 | MemPoolT< sizeof(XMLAttribute) > attributePool;
|
| 1128 | MemPoolT< sizeof(XMLText) > textPool;
|
| 1129 | MemPoolT< sizeof(XMLComment) > commentPool;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1130 | };
|
| 1131 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1132 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1133 |
|
| 1134 | /**
|
| 1135 | Printing functionality. The XMLPrinter gives you more
|
| 1136 | options than the XMLDocument::Print() method.
|
| 1137 |
|
| 1138 | It can:
|
| 1139 | -# Print to memory.
|
| 1140 | -# Print to a file you provide
|
| 1141 | -# Print XML without a XMLDocument.
|
| 1142 |
|
| 1143 | Print to Memory
|
| 1144 |
|
| 1145 | @verbatim
|
| 1146 | XMLPrinter printer;
|
| 1147 | doc->Print( &printer );
|
| 1148 | SomeFunctior( printer.CStr() );
|
| 1149 | @endverbatim
|
| 1150 |
|
| 1151 | Print to a File
|
| 1152 |
|
| 1153 | You provide the file pointer.
|
| 1154 | @verbatim
|
| 1155 | XMLPrinter printer( fp );
|
| 1156 | doc.Print( &printer );
|
| 1157 | @endverbatim
|
| 1158 |
|
| 1159 | Print without a XMLDocument
|
| 1160 |
|
| 1161 | When loading, an XML parser is very useful. However, sometimes
|
| 1162 | when saving, it just gets in the way. The code is often set up
|
| 1163 | for streaming, and constructing the DOM is just overhead.
|
| 1164 |
|
| 1165 | The Printer supports the streaming case. The following code
|
| 1166 | prints out a trivially simple XML file without ever creating
|
| 1167 | an XML document.
|
| 1168 |
|
| 1169 | @verbatim
|
| 1170 | XMLPrinter printer( fp );
|
| 1171 | printer.OpenElement( "foo" );
|
| 1172 | printer.PushAttribute( "foo", "bar" );
|
| 1173 | printer.CloseElement();
|
| 1174 | @endverbatim
|
| 1175 | */
|
| 1176 | class XMLPrinter : public XMLVisitor
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1177 | {
|
| 1178 | public:
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1179 | /** Construct the printer. If the FILE* is specified,
|
| 1180 | this will print to the FILE. Else it will print
|
| 1181 | to memory, and the result is available in CStr()
|
| 1182 | */
|
| 1183 | XMLPrinter( FILE* file=0 );
|
| 1184 | ~XMLPrinter() {}
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1185 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1186 | /** If streaming, write the BOM and declaration. */
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1187 | void PushHeader( bool writeBOM, bool writeDeclaration );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1188 | /** If streaming, start writing an element.
|
| 1189 | The element must be closed with CloseElement()
|
| 1190 | */
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1191 | void OpenElement( const char* name );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1192 | /// If streaming, add an attribute to an open element.
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1193 | void PushAttribute( const char* name, const char* value );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1194 | void PushAttribute( const char* name, int value );
|
| 1195 | void PushAttribute( const char* name, unsigned value );
|
| 1196 | void PushAttribute( const char* name, bool value );
|
| 1197 | void PushAttribute( const char* name, double value );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1198 | /// If streaming, close the Element.
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1199 | void CloseElement();
|
| 1200 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1201 | /// Add a text node.
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1202 | void PushText( const char* text, bool cdata=false );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1203 | /// Add a comment
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1204 | void PushComment( const char* comment );
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1205 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1206 | void PushDeclaration( const char* value );
|
| 1207 | void PushUnknown( const char* value );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1208 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1209 | virtual bool VisitEnter( const XMLDocument& /*doc*/ );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1210 | virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
|
| 1211 |
|
| 1212 | virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
|
| 1213 | virtual bool VisitExit( const XMLElement& element );
|
| 1214 |
|
| 1215 | virtual bool Visit( const XMLText& text );
|
| 1216 | virtual bool Visit( const XMLComment& comment );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1217 | virtual bool Visit( const XMLDeclaration& declaration );
|
| 1218 | virtual bool Visit( const XMLUnknown& unknown );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1219 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1220 | /**
|
| 1221 | If in print to memory mode, return a pointer to
|
| 1222 | the XML file in memory.
|
| 1223 | */
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1224 | const char* CStr() const { return buffer.Mem(); }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1225 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1226 | private:
|
| 1227 | void SealElement();
|
| 1228 | void PrintSpace( int depth );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1229 | void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1230 | void Print( const char* format, ... );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1231 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1232 | bool elementJustOpened;
|
| 1233 | bool firstElement;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1234 | FILE* fp;
|
| 1235 | int depth;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1236 | int textDepth;
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1237 | bool processEntities;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1238 |
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1239 | enum {
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1240 | ENTITY_RANGE = 64,
|
| 1241 | BUF_SIZE = 200
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1242 | };
|
| 1243 | bool entityFlag[ENTITY_RANGE];
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1244 | bool restrictedEntityFlag[ENTITY_RANGE];
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1245 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1246 | DynArray< const char*, 10 > stack;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1247 | DynArray< char, 20 > buffer, accumulator;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1248 | };
|
| 1249 |
|
| 1250 |
|
Guillermo A. Amaral | b42ba36 | 2012-03-20 00:15:30 -0700 | [diff] [blame] | 1251 | } // tinyxml2
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1252 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1253 |
|
| 1254 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1255 | #endif // TINYXML2_INCLUDED
|