blob: f0817581276c1aac7157458e96d94cfc9473375e [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{
51 enum {
52 NEEDS_FLUSH = 0x01,
53 NEEDS_ENTITY_PROCESSING = 0x02,
54 NEEDS_NEWLINE_NORMALIZATION = 0x04
55 };
56
57 StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
58 void Init( const char* start, char* end, int flags; ) {
59 this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
60 }
61 const char* GetStr();
62
63private:
64 // After parsing, if *end != 0, it can be set to zero.
65 int flags;
66 const char* start;
67 char* end;
68};
69
U-Lama\Lee560bd472011-12-28 19:42:49 -080070
Lee Thomason8a5dfee2012-01-18 17:43:40 -080071class XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -080072{
73public:
Lee Thomason8a5dfee2012-01-18 17:43:40 -080074 XMLBase() {}
75 virtual ~XMLBase() {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080076
77protected:
Lee Thomason3f57d272012-01-11 15:30:03 -080078 static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
79 static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080080
81 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
82 int n = 0;
83 while( *p && *q && *p == *q && n<nChar ) {
84 ++p; ++q; ++n;
85 }
86 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
87 return true;
88 }
89 return false;
90 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -080091 inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
92 inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
93 inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080094
Lee Thomason3f57d272012-01-11 15:30:03 -080095 const char* ParseText( char* in, const char* endTag, char** next );
Lee Thomason39ede242012-01-20 11:27:56 -080096 char* ParseName( char* in, StrPair* pair );
Lee Thomason8a5dfee2012-01-18 17:43:40 -080097 char* Identify( XMLDocument* document, char* p, XMLNode** node );
98};
99
100
101class XMLNode : public XMLBase
102{
103 friend class XMLDocument;
104 friend class XMLElement;
105public:
106 virtual ~XMLNode();
107
108 XMLNode* InsertEndChild( XMLNode* addThis );
109 virtual void Print( FILE* cfile, int depth );
110
111 virtual XMLElement* ToElement() { return 0; }
Lee Thomason3f57d272012-01-11 15:30:03 -0800112
113 virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
114
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800115protected:
116 XMLNode( XMLDocument* );
117
Lee Thomason3f57d272012-01-11 15:30:03 -0800118 XMLDocument* document;
119 XMLNode* parent;
120
121 XMLNode* firstChild;
122 XMLNode* lastChild;
123
124 XMLNode* prev;
125 XMLNode* next;
126
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800127private:
Lee Thomason85403d82012-01-11 15:55:05 -0800128 void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800129
130};
131
132
133class XMLComment : public XMLNode
134{
Lee Thomason3f57d272012-01-11 15:30:03 -0800135public:
136 XMLComment( XMLDocument* doc );
137 virtual ~XMLComment();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800138
Lee Thomason85403d82012-01-11 15:55:05 -0800139 virtual void Print( FILE* cfile, int depth );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800140
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800141 const char* Value() const { return value; }
142
Lee Thomasonce0763e2012-01-11 15:43:54 -0800143 char* ParseDeep( char* );
144
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800145protected:
146
Lee Thomason3f57d272012-01-11 15:30:03 -0800147private:
Lee Thomasonce0763e2012-01-11 15:43:54 -0800148 const char* value;
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800149};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800150
151
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800152class XMLAttribute : public XMLBase
153{
154 friend class XMLElement;
155public:
156 XMLAttribute( XMLElement* element ) : value( 0 ), next( 0 ) {}
157 virtual ~XMLAttribute() {}
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800158 virtual void Print( FILE* cfile );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800159
160private:
161 char* ParseDeep( char* p );
162
163 const char* value;
164 XMLAttribute* next;
165};
166
167
168class XMLElement : public XMLNode
169{
170public:
171 XMLElement( XMLDocument* doc );
172 virtual ~XMLElement();
173
174 const char* Name() const { return name; }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800175 virtual void Print( FILE* cfile, int depth );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800176
177 virtual XMLElement* ToElement() { return this; }
178 bool Closing() const { return closing; }
179
180 char* ParseDeep( char* p );
181
182protected:
183
184private:
185 const char* name;
186 bool closing;
187 XMLAttribute* rootAttribute;
188 XMLAttribute* lastAttribute;
189};
190
191
192class XMLDocument : public XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800193{
194public:
195 XMLDocument();
Lee Thomason3f57d272012-01-11 15:30:03 -0800196 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800197
198 bool Parse( const char* );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800199 void Print( FILE* cfile=stdout, int depth=0 );
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800200
Lee Thomason3f57d272012-01-11 15:30:03 -0800201 XMLNode* Root() { return root; }
202 XMLNode* RootElement();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800203
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800204 enum {
205 ERROR_ELEMENT_MISMATCH,
206 ERROR_PARSING_ELEMENT,
207 ERROR_PARSING_ATTRIBUTE
208 };
209 void SetError( int error, const char* str1, const char* str2 ) {}
210
Lee Thomason3f57d272012-01-11 15:30:03 -0800211private:
212 XMLDocument( const XMLDocument& ); // intentionally not implemented
Lee Thomason3f57d272012-01-11 15:30:03 -0800213
214 XMLNode* root;
U-Lama\Lee560bd472011-12-28 19:42:49 -0800215 CharBuffer* charBuffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800216};
217
218
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800219}; // tinyxml2
220
U-Lama\Lee560bd472011-12-28 19:42:49 -0800221
222
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800223#endif // TINYXML2_INCLUDED