blob: 227c7c72783e40cc52010a6e21716bb4f0da1dba [file] [log] [blame]
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001#ifndef TINYXML2_INCLUDED
2#define TINYXML2_INCLUDED
3
U-Lama\Lee4cee6112011-12-31 14:58:18 -08004#include <limits.h>
Lee Thomasonce0763e2012-01-11 15:43:54 -08005#include <ctype.h>
6#include <stdio.h>
U-Lama\Lee4cee6112011-12-31 14:58:18 -08007
8#if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
9 #ifndef DEBUG
10 #define DEBUG
11 #endif
12#endif
13
14
15#if defined(DEBUG)
16 #if defined(_MSC_VER)
17 #define TIXMLASSERT( x ) if ( !(x)) { _asm { int 3 } } //if ( !(x)) WinDebugBreak()
18 #elif defined (ANDROID_NDK)
19 #include <android/log.h>
20 #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
21 #else
22 #include <assert.h>
23 #define TIXMLASSERT assert
24 #endif
25#else
26 #define TIXMLASSERT( x ) {}
27#endif
28
U-Lama\Leee13c3e62011-12-28 14:36:55 -080029
30namespace tinyxml2
31{
Lee Thomasonce0763e2012-01-11 15:43:54 -080032class XMLDocument;
Lee Thomason8a5dfee2012-01-18 17:43:40 -080033class XMLElement;
34class XMLAttribute;
35class XMLComment;
36class XMLNode;
Lee Thomason5492a1c2012-01-23 15:32:10 -080037class XMLText;
U-Lama\Leee13c3e62011-12-28 14:36:55 -080038
U-Lama\Lee560bd472011-12-28 19:42:49 -080039// internal - move to separate namespace
40struct CharBuffer
41{
42 size_t length;
43 char mem[1];
44
45 static CharBuffer* Construct( const char* in );
46 static void Free( CharBuffer* );
47};
48
Lee Thomason39ede242012-01-20 11:27:56 -080049// FIXME: refactor to be the basis for all string handling.
50class StrPair
51{
Lee Thomasond34f52c2012-01-20 12:55:24 -080052public:
Lee Thomason39ede242012-01-20 11:27:56 -080053 enum {
Lee Thomasone4422302012-01-20 17:59:50 -080054 NEEDS_ENTITY_PROCESSING = 0x01,
55 NEEDS_NEWLINE_NORMALIZATION = 0x02
Lee Thomason39ede242012-01-20 11:27:56 -080056 };
57
58 StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
Lee Thomasone4422302012-01-20 17:59:50 -080059 void Set( char* start, char* end, int flags ) {
Lee Thomason39ede242012-01-20 11:27:56 -080060 this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
61 }
62 const char* GetStr();
Lee Thomasone4422302012-01-20 17:59:50 -080063 bool Empty() const { return start == end; }
Lee Thomason39ede242012-01-20 11:27:56 -080064
65private:
Lee Thomasone4422302012-01-20 17:59:50 -080066 enum {
67 NEEDS_FLUSH = 0x100
68 };
69
Lee Thomason39ede242012-01-20 11:27:56 -080070 // After parsing, if *end != 0, it can be set to zero.
71 int flags;
Lee Thomasone4422302012-01-20 17:59:50 -080072 char* start;
Lee Thomason39ede242012-01-20 11:27:56 -080073 char* end;
74};
75
U-Lama\Lee560bd472011-12-28 19:42:49 -080076
Lee Thomason8a5dfee2012-01-18 17:43:40 -080077class XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -080078{
79public:
Lee Thomason8a5dfee2012-01-18 17:43:40 -080080 XMLBase() {}
81 virtual ~XMLBase() {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080082
83protected:
Lee Thomason3f57d272012-01-11 15:30:03 -080084 static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
85 static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080086
87 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
88 int n = 0;
Lee Thomasond34f52c2012-01-20 12:55:24 -080089 if ( p == q ) {
90 return true;
91 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080092 while( *p && *q && *p == *q && n<nChar ) {
93 ++p; ++q; ++n;
94 }
95 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
96 return true;
97 }
98 return false;
99 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800100 inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
101 inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
102 inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800103
Lee Thomasone4422302012-01-20 17:59:50 -0800104 char* ParseText( char* in, StrPair* pair, const char* endTag );
Lee Thomason39ede242012-01-20 11:27:56 -0800105 char* ParseName( char* in, StrPair* pair );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800106 char* Identify( XMLDocument* document, char* p, XMLNode** node );
107};
108
109
110class XMLNode : public XMLBase
111{
112 friend class XMLDocument;
113 friend class XMLElement;
114public:
115 virtual ~XMLNode();
116
117 XMLNode* InsertEndChild( XMLNode* addThis );
118 virtual void Print( FILE* cfile, int depth );
119
Lee Thomason5492a1c2012-01-23 15:32:10 -0800120 virtual XMLElement* ToElement() { return 0; }
121 virtual XMLText* ToText() { return 0; }
122 virtual XMLComment* ToComment() { return 0; }
Lee Thomason3f57d272012-01-11 15:30:03 -0800123
124 virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
125
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800126protected:
127 XMLNode( XMLDocument* );
Lee Thomasond923c672012-01-23 08:44:25 -0800128 void Unlink( XMLNode* child );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800129
Lee Thomason3f57d272012-01-11 15:30:03 -0800130 XMLDocument* document;
131 XMLNode* parent;
132
133 XMLNode* firstChild;
134 XMLNode* lastChild;
135
136 XMLNode* prev;
137 XMLNode* next;
138
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800139private:
Lee Thomason85403d82012-01-11 15:55:05 -0800140 void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800141
142};
143
144
Lee Thomason5492a1c2012-01-23 15:32:10 -0800145class XMLText : public XMLNode
146{
147public:
148 XMLText( XMLDocument* doc ) : XMLNode( doc ) {}
149 virtual ~XMLText() {}
150
151 virtual void Print( FILE* cfile, int depth );
152 const char* Value() { return value.GetStr(); }
153 virtual XMLText* ToText() { return this; }
154
155 char* ParseDeep( char* );
156
157protected:
158
159private:
160 StrPair value;
161};
162
163
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800164class XMLComment : public XMLNode
165{
Lee Thomason3f57d272012-01-11 15:30:03 -0800166public:
167 XMLComment( XMLDocument* doc );
168 virtual ~XMLComment();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800169
Lee Thomason85403d82012-01-11 15:55:05 -0800170 virtual void Print( FILE* cfile, int depth );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800171 virtual XMLComment* ToComment() { return this; }
Lee Thomasonce0763e2012-01-11 15:43:54 -0800172
Lee Thomasond34f52c2012-01-20 12:55:24 -0800173 const char* Value() { return value.GetStr(); }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800174
Lee Thomasonce0763e2012-01-11 15:43:54 -0800175 char* ParseDeep( char* );
176
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800177protected:
178
Lee Thomason3f57d272012-01-11 15:30:03 -0800179private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800180 StrPair value;
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800181};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800182
183
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800184class XMLAttribute : public XMLBase
185{
186 friend class XMLElement;
187public:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800188 XMLAttribute( XMLElement* element ) : next( 0 ) {}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800189 virtual ~XMLAttribute() {}
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800190 virtual void Print( FILE* cfile );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800191
192private:
193 char* ParseDeep( char* p );
194
Lee Thomason22aead12012-01-23 13:29:35 -0800195 StrPair name;
Lee Thomasond34f52c2012-01-20 12:55:24 -0800196 StrPair value;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800197 XMLAttribute* next;
198};
199
200
201class XMLElement : public XMLNode
202{
203public:
204 XMLElement( XMLDocument* doc );
205 virtual ~XMLElement();
206
Lee Thomasond34f52c2012-01-20 12:55:24 -0800207 const char* Name() { return name.GetStr(); }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800208 virtual void Print( FILE* cfile, int depth );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800209
210 virtual XMLElement* ToElement() { return this; }
211 bool Closing() const { return closing; }
212
213 char* ParseDeep( char* p );
214
215protected:
216
217private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800218 StrPair name;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800219 bool closing;
220 XMLAttribute* rootAttribute;
221 XMLAttribute* lastAttribute;
222};
223
224
225class XMLDocument : public XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800226{
227public:
228 XMLDocument();
Lee Thomason3f57d272012-01-11 15:30:03 -0800229 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800230
231 bool Parse( const char* );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800232 void Print( FILE* cfile=stdout, int depth=0 );
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800233
Lee Thomason3f57d272012-01-11 15:30:03 -0800234 XMLNode* Root() { return root; }
235 XMLNode* RootElement();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800236
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800237 enum {
238 ERROR_ELEMENT_MISMATCH,
239 ERROR_PARSING_ELEMENT,
240 ERROR_PARSING_ATTRIBUTE
241 };
242 void SetError( int error, const char* str1, const char* str2 ) {}
243
Lee Thomason3f57d272012-01-11 15:30:03 -0800244private:
245 XMLDocument( const XMLDocument& ); // intentionally not implemented
Lee Thomason3f57d272012-01-11 15:30:03 -0800246
247 XMLNode* root;
U-Lama\Lee560bd472011-12-28 19:42:49 -0800248 CharBuffer* charBuffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800249};
250
251
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800252}; // tinyxml2
253
U-Lama\Lee560bd472011-12-28 19:42:49 -0800254
255
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800256#endif // TINYXML2_INCLUDED