blob: fe3979e250dfc73050a07e7a1ef30cf57b755352 [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 {
53 NEEDS_FLUSH = 0x01,
54 NEEDS_ENTITY_PROCESSING = 0x02,
55 NEEDS_NEWLINE_NORMALIZATION = 0x04
56 };
57
58 StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
Lee Thomasond34f52c2012-01-20 12:55:24 -080059 void Init( const 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();
63
64private:
65 // After parsing, if *end != 0, it can be set to zero.
66 int flags;
67 const char* start;
68 char* end;
69};
70
U-Lama\Lee560bd472011-12-28 19:42:49 -080071
Lee Thomason8a5dfee2012-01-18 17:43:40 -080072class XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -080073{
74public:
Lee Thomason8a5dfee2012-01-18 17:43:40 -080075 XMLBase() {}
76 virtual ~XMLBase() {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080077
78protected:
Lee Thomason3f57d272012-01-11 15:30:03 -080079 static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
80 static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080081
82 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
83 int n = 0;
Lee Thomasond34f52c2012-01-20 12:55:24 -080084 if ( p == q ) {
85 return true;
86 }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080087 while( *p && *q && *p == *q && n<nChar ) {
88 ++p; ++q; ++n;
89 }
90 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
91 return true;
92 }
93 return false;
94 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -080095 inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
96 inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
97 inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080098
Lee Thomason3f57d272012-01-11 15:30:03 -080099 const char* ParseText( char* in, const char* endTag, char** next );
Lee Thomason39ede242012-01-20 11:27:56 -0800100 char* ParseName( char* in, StrPair* pair );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800101 char* Identify( XMLDocument* document, char* p, XMLNode** node );
102};
103
104
105class XMLNode : public XMLBase
106{
107 friend class XMLDocument;
108 friend class XMLElement;
109public:
110 virtual ~XMLNode();
111
112 XMLNode* InsertEndChild( XMLNode* addThis );
113 virtual void Print( FILE* cfile, int depth );
114
115 virtual XMLElement* ToElement() { return 0; }
Lee Thomason3f57d272012-01-11 15:30:03 -0800116
117 virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
118
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800119protected:
120 XMLNode( XMLDocument* );
121
Lee Thomason3f57d272012-01-11 15:30:03 -0800122 XMLDocument* document;
123 XMLNode* parent;
124
125 XMLNode* firstChild;
126 XMLNode* lastChild;
127
128 XMLNode* prev;
129 XMLNode* next;
130
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800131private:
Lee Thomason85403d82012-01-11 15:55:05 -0800132 void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800133
134};
135
136
137class XMLComment : public XMLNode
138{
Lee Thomason3f57d272012-01-11 15:30:03 -0800139public:
140 XMLComment( XMLDocument* doc );
141 virtual ~XMLComment();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800142
Lee Thomason85403d82012-01-11 15:55:05 -0800143 virtual void Print( FILE* cfile, int depth );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800144
Lee Thomasond34f52c2012-01-20 12:55:24 -0800145 const char* Value() { return value.GetStr(); }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800146
Lee Thomasonce0763e2012-01-11 15:43:54 -0800147 char* ParseDeep( char* );
148
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800149protected:
150
Lee Thomason3f57d272012-01-11 15:30:03 -0800151private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800152 StrPair value;
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800153};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800154
155
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800156class XMLAttribute : public XMLBase
157{
158 friend class XMLElement;
159public:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800160 XMLAttribute( XMLElement* element ) : next( 0 ) {}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800161 virtual ~XMLAttribute() {}
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800162 virtual void Print( FILE* cfile );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800163
164private:
165 char* ParseDeep( char* p );
166
Lee Thomasond34f52c2012-01-20 12:55:24 -0800167 StrPair value;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800168 XMLAttribute* next;
169};
170
171
172class XMLElement : public XMLNode
173{
174public:
175 XMLElement( XMLDocument* doc );
176 virtual ~XMLElement();
177
Lee Thomasond34f52c2012-01-20 12:55:24 -0800178 const char* Name() { return name.GetStr(); }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800179 virtual void Print( FILE* cfile, int depth );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800180
181 virtual XMLElement* ToElement() { return this; }
182 bool Closing() const { return closing; }
183
184 char* ParseDeep( char* p );
185
186protected:
187
188private:
Lee Thomasond34f52c2012-01-20 12:55:24 -0800189 StrPair name;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800190 bool closing;
191 XMLAttribute* rootAttribute;
192 XMLAttribute* lastAttribute;
193};
194
195
196class XMLDocument : public XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800197{
198public:
199 XMLDocument();
Lee Thomason3f57d272012-01-11 15:30:03 -0800200 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800201
202 bool Parse( const char* );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800203 void Print( FILE* cfile=stdout, int depth=0 );
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800204
Lee Thomason3f57d272012-01-11 15:30:03 -0800205 XMLNode* Root() { return root; }
206 XMLNode* RootElement();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800207
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800208 enum {
209 ERROR_ELEMENT_MISMATCH,
210 ERROR_PARSING_ELEMENT,
211 ERROR_PARSING_ATTRIBUTE
212 };
213 void SetError( int error, const char* str1, const char* str2 ) {}
214
Lee Thomason3f57d272012-01-11 15:30:03 -0800215private:
216 XMLDocument( const XMLDocument& ); // intentionally not implemented
Lee Thomason3f57d272012-01-11 15:30:03 -0800217
218 XMLNode* root;
U-Lama\Lee560bd472011-12-28 19:42:49 -0800219 CharBuffer* charBuffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800220};
221
222
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800223}; // tinyxml2
224
U-Lama\Lee560bd472011-12-28 19:42:49 -0800225
226
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800227#endif // TINYXML2_INCLUDED