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