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