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