blob: 779fddeb9dc53d82ef951da898a6ebbb19bce2a5 [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* );
125
Lee Thomason3f57d272012-01-11 15:30:03 -0800126 XMLDocument* document;
127 XMLNode* parent;
128
129 XMLNode* firstChild;
130 XMLNode* lastChild;
131
132 XMLNode* prev;
133 XMLNode* next;
134
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800135private:
Lee Thomason85403d82012-01-11 15:55:05 -0800136 void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800137
138};
139
140
141class XMLComment : public XMLNode
142{
Lee Thomason3f57d272012-01-11 15:30:03 -0800143public:
144 XMLComment( XMLDocument* doc );
145 virtual ~XMLComment();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800146
Lee Thomason85403d82012-01-11 15:55:05 -0800147 virtual void Print( FILE* cfile, int depth );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800148
Lee Thomasond34f52c2012-01-20 12:55:24 -0800149 const char* Value() { return value.GetStr(); }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800150
Lee Thomasonce0763e2012-01-11 15:43:54 -0800151 char* ParseDeep( char* );
152
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800153protected:
154
Lee Thomason3f57d272012-01-11 15:30:03 -0800155private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800156 StrPair value;
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800157};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800158
159
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800160class XMLAttribute : public XMLBase
161{
162 friend class XMLElement;
163public:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800164 XMLAttribute( XMLElement* element ) : next( 0 ) {}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800165 virtual ~XMLAttribute() {}
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800166 virtual void Print( FILE* cfile );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800167
168private:
169 char* ParseDeep( char* p );
170
Lee Thomasond34f52c2012-01-20 12:55:24 -0800171 StrPair value;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800172 XMLAttribute* next;
173};
174
175
176class XMLElement : public XMLNode
177{
178public:
179 XMLElement( XMLDocument* doc );
180 virtual ~XMLElement();
181
Lee Thomasond34f52c2012-01-20 12:55:24 -0800182 const char* Name() { return name.GetStr(); }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800183 virtual void Print( FILE* cfile, int depth );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800184
185 virtual XMLElement* ToElement() { return this; }
186 bool Closing() const { return closing; }
187
188 char* ParseDeep( char* p );
189
190protected:
191
192private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800193 StrPair name;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800194 bool closing;
195 XMLAttribute* rootAttribute;
196 XMLAttribute* lastAttribute;
197};
198
199
200class XMLDocument : public XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800201{
202public:
203 XMLDocument();
Lee Thomason3f57d272012-01-11 15:30:03 -0800204 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800205
206 bool Parse( const char* );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800207 void Print( FILE* cfile=stdout, int depth=0 );
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800208
Lee Thomason3f57d272012-01-11 15:30:03 -0800209 XMLNode* Root() { return root; }
210 XMLNode* RootElement();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800211
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800212 enum {
213 ERROR_ELEMENT_MISMATCH,
214 ERROR_PARSING_ELEMENT,
215 ERROR_PARSING_ATTRIBUTE
216 };
217 void SetError( int error, const char* str1, const char* str2 ) {}
218
Lee Thomason3f57d272012-01-11 15:30:03 -0800219private:
220 XMLDocument( const XMLDocument& ); // intentionally not implemented
Lee Thomason3f57d272012-01-11 15:30:03 -0800221
222 XMLNode* root;
U-Lama\Lee560bd472011-12-28 19:42:49 -0800223 CharBuffer* charBuffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800224};
225
226
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800227}; // tinyxml2
228
U-Lama\Lee560bd472011-12-28 19:42:49 -0800229
230
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800231#endif // TINYXML2_INCLUDED