Download picasa web albums photos 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 picasa web albums photos automatically.
This application requires mpod mother board.

Picasaウェブアルバムから、自動的に写真をダウンロードして、ディジタルフォトフレームに表示します。
動作させるには mpod マザーボード が必要です。
プログラムの中で、ご自分のアルバムのRSSファイルへのURLを指定してからご利用下さい。

album description edit information description

Committer:
togayan
Date:
Wed Aug 22 16:00:38 2012 +0000
Revision:
0:dfd5cfea7112
1st version of mpod_picasa_photoframe

Who changed what in which revision?

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