blob: ba5d0311676b7261ed54ce7fbe07dee53309eb48 [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;
U-Lama\Leee13c3e62011-12-28 14:36:55 -080037
U-Lama\Lee560bd472011-12-28 19:42:49 -080038// internal - move to separate namespace
39struct CharBuffer
40{
41 size_t length;
42 char mem[1];
43
44 static CharBuffer* Construct( const char* in );
45 static void Free( CharBuffer* );
46};
47
Lee Thomason39ede242012-01-20 11:27:56 -080048// FIXME: refactor to be the basis for all string handling.
49class StrPair
50{
Lee Thomasond34f52c2012-01-20 12:55:24 -080051public:
Lee Thomason39ede242012-01-20 11:27:56 -080052 enum {
Lee Thomasone4422302012-01-20 17:59:50 -080053 NEEDS_ENTITY_PROCESSING = 0x01,
54 NEEDS_NEWLINE_NORMALIZATION = 0x02
Lee Thomason39ede242012-01-20 11:27:56 -080055 };
56
57 StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
Lee Thomasone4422302012-01-20 17:59:50 -080058 void Set( char* start, char* end, int flags ) {
Lee Thomason39ede242012-01-20 11:27:56 -080059 this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
60 }
61 const char* GetStr();
Lee Thomasone4422302012-01-20 17:59:50 -080062 bool Empty() const { return start == end; }
Lee Thomason39ede242012-01-20 11:27:56 -080063
64private:
Lee Thomasone4422302012-01-20 17:59:50 -080065 enum {
66 NEEDS_FLUSH = 0x100
67 };
68
Lee Thomason39ede242012-01-20 11:27:56 -080069 // After parsing, if *end != 0, it can be set to zero.
70 int flags;
Lee Thomasone4422302012-01-20 17:59:50 -080071 char* start;
Lee Thomason39ede242012-01-20 11:27:56 -080072 char* end;
73};
74
U-Lama\Lee560bd472011-12-28 19:42:49 -080075
Lee Thomason8a5dfee2012-01-18 17:43:40 -080076class XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -080077{
78public:
Lee Thomason8a5dfee2012-01-18 17:43:40 -080079 XMLBase() {}
80 virtual ~XMLBase() {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080081
82protected:
Lee Thomason3f57d272012-01-11 15:30:03 -080083 static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
84 static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080085
86 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
87 int n = 0;
Lee Thomasond34f52c2012-01-20 12:55:24 -080088 if ( p == q ) {
89 return true;
90 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080091 while( *p && *q && *p == *q && n<nChar ) {
92 ++p; ++q; ++n;
93 }
94 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
95 return true;
96 }
97 return false;
98 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -080099 inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
100 inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
101 inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800102
Lee Thomasone4422302012-01-20 17:59:50 -0800103 char* ParseText( char* in, StrPair* pair, const char* endTag );
Lee Thomason39ede242012-01-20 11:27:56 -0800104 char* ParseName( char* in, StrPair* pair );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800105 char* Identify( XMLDocument* document, char* p, XMLNode** node );
106};
107
108
109class XMLNode : public XMLBase
110{
111 friend class XMLDocument;
112 friend class XMLElement;
113public:
114 virtual ~XMLNode();
115
116 XMLNode* InsertEndChild( XMLNode* addThis );
117 virtual void Print( FILE* cfile, int depth );
118
119 virtual XMLElement* ToElement() { return 0; }
Lee Thomason3f57d272012-01-11 15:30:03 -0800120
121 virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
122
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800123protected:
124 XMLNode( XMLDocument* );
Lee Thomasond923c672012-01-23 08:44:25 -0800125 void Unlink( XMLNode* child );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800126
Lee Thomason3f57d272012-01-11 15:30:03 -0800127 XMLDocument* document;
128 XMLNode* parent;
129
130 XMLNode* firstChild;
131 XMLNode* lastChild;
132
133 XMLNode* prev;
134 XMLNode* next;
135
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800136private:
Lee Thomason85403d82012-01-11 15:55:05 -0800137 void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800138
139};
140
141
142class XMLComment : public XMLNode
143{
Lee Thomason3f57d272012-01-11 15:30:03 -0800144public:
145 XMLComment( XMLDocument* doc );
146 virtual ~XMLComment();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800147
Lee Thomason85403d82012-01-11 15:55:05 -0800148 virtual void Print( FILE* cfile, int depth );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800149
Lee Thomasond34f52c2012-01-20 12:55:24 -0800150 const char* Value() { return value.GetStr(); }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800151
Lee Thomasonce0763e2012-01-11 15:43:54 -0800152 char* ParseDeep( char* );
153
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800154protected:
155
Lee Thomason3f57d272012-01-11 15:30:03 -0800156private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800157 StrPair value;
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800158};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800159
160
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800161class XMLAttribute : public XMLBase
162{
163 friend class XMLElement;
164public:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800165 XMLAttribute( XMLElement* element ) : next( 0 ) {}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800166 virtual ~XMLAttribute() {}
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800167 virtual void Print( FILE* cfile );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800168
169private:
170 char* ParseDeep( char* p );
171
Lee Thomason22aead12012-01-23 13:29:35 -0800172 StrPair name;
Lee Thomasond34f52c2012-01-20 12:55:24 -0800173 StrPair value;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800174 XMLAttribute* next;
175};
176
177
178class XMLElement : public XMLNode
179{
180public:
181 XMLElement( XMLDocument* doc );
182 virtual ~XMLElement();
183
Lee Thomasond34f52c2012-01-20 12:55:24 -0800184 const char* Name() { return name.GetStr(); }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800185 virtual void Print( FILE* cfile, int depth );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800186
187 virtual XMLElement* ToElement() { return this; }
188 bool Closing() const { return closing; }
189
190 char* ParseDeep( char* p );
191
192protected:
193
194private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800195 StrPair name;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800196 bool closing;
197 XMLAttribute* rootAttribute;
198 XMLAttribute* lastAttribute;
199};
200
201
202class XMLDocument : public XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800203{
204public:
205 XMLDocument();
Lee Thomason3f57d272012-01-11 15:30:03 -0800206 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800207
208 bool Parse( const char* );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800209 void Print( FILE* cfile=stdout, int depth=0 );
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800210
Lee Thomason3f57d272012-01-11 15:30:03 -0800211 XMLNode* Root() { return root; }
212 XMLNode* RootElement();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800213
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800214 enum {
215 ERROR_ELEMENT_MISMATCH,
216 ERROR_PARSING_ELEMENT,
217 ERROR_PARSING_ATTRIBUTE
218 };
219 void SetError( int error, const char* str1, const char* str2 ) {}
220
Lee Thomason3f57d272012-01-11 15:30:03 -0800221private:
222 XMLDocument( const XMLDocument& ); // intentionally not implemented
Lee Thomason3f57d272012-01-11 15:30:03 -0800223
224 XMLNode* root;
U-Lama\Lee560bd472011-12-28 19:42:49 -0800225 CharBuffer* charBuffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800226};
227
228
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800229}; // tinyxml2
230
U-Lama\Lee560bd472011-12-28 19:42:49 -0800231
232
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800233#endif // TINYXML2_INCLUDED