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