blob: b08734957886fde926d45027f5b2e10160361b80 [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
48
Lee Thomason8a5dfee2012-01-18 17:43:40 -080049class XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -080050{
51public:
Lee Thomason8a5dfee2012-01-18 17:43:40 -080052 XMLBase() {}
53 virtual ~XMLBase() {}
U-Lama\Lee4cee6112011-12-31 14:58:18 -080054
55protected:
Lee Thomason3f57d272012-01-11 15:30:03 -080056 static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
57 static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080058
59 inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
60 int n = 0;
61 while( *p && *q && *p == *q && n<nChar ) {
62 ++p; ++q; ++n;
63 }
64 if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
65 return true;
66 }
67 return false;
68 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -080069 inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
70 inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
71 inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
U-Lama\Lee4cee6112011-12-31 14:58:18 -080072
Lee Thomason3f57d272012-01-11 15:30:03 -080073 const char* ParseText( char* in, const char* endTag, char** next );
Lee Thomason8a5dfee2012-01-18 17:43:40 -080074 const char* ParseName( char* in, char** next );
75 char* Identify( XMLDocument* document, char* p, XMLNode** node );
76};
77
78
79class XMLNode : public XMLBase
80{
81 friend class XMLDocument;
82 friend class XMLElement;
83public:
84 virtual ~XMLNode();
85
86 XMLNode* InsertEndChild( XMLNode* addThis );
87 virtual void Print( FILE* cfile, int depth );
88
89 virtual XMLElement* ToElement() { return 0; }
Lee Thomason3f57d272012-01-11 15:30:03 -080090
91 virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
92
Lee Thomason8a5dfee2012-01-18 17:43:40 -080093protected:
94 XMLNode( XMLDocument* );
95
Lee Thomason3f57d272012-01-11 15:30:03 -080096 XMLDocument* document;
97 XMLNode* parent;
98
99 XMLNode* firstChild;
100 XMLNode* lastChild;
101
102 XMLNode* prev;
103 XMLNode* next;
104
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800105private:
Lee Thomason85403d82012-01-11 15:55:05 -0800106 void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800107
108};
109
110
111class XMLComment : public XMLNode
112{
Lee Thomason3f57d272012-01-11 15:30:03 -0800113public:
114 XMLComment( XMLDocument* doc );
115 virtual ~XMLComment();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800116
Lee Thomason85403d82012-01-11 15:55:05 -0800117 virtual void Print( FILE* cfile, int depth );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800118
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800119 const char* Value() const { return value; }
120
Lee Thomasonce0763e2012-01-11 15:43:54 -0800121 char* ParseDeep( char* );
122
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800123protected:
124
Lee Thomason3f57d272012-01-11 15:30:03 -0800125private:
Lee Thomasonce0763e2012-01-11 15:43:54 -0800126 const char* value;
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800127};
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800128
129
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800130class XMLAttribute : public XMLBase
131{
132 friend class XMLElement;
133public:
134 XMLAttribute( XMLElement* element ) : value( 0 ), next( 0 ) {}
135 virtual ~XMLAttribute() {}
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800136 virtual void Print( FILE* cfile );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800137
138private:
139 char* ParseDeep( char* p );
140
141 const char* value;
142 XMLAttribute* next;
143};
144
145
146class XMLElement : public XMLNode
147{
148public:
149 XMLElement( XMLDocument* doc );
150 virtual ~XMLElement();
151
152 const char* Name() const { return name; }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800153 virtual void Print( FILE* cfile, int depth );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800154
155 virtual XMLElement* ToElement() { return this; }
156 bool Closing() const { return closing; }
157
158 char* ParseDeep( char* p );
159
160protected:
161
162private:
163 const char* name;
164 bool closing;
165 XMLAttribute* rootAttribute;
166 XMLAttribute* lastAttribute;
167};
168
169
170class XMLDocument : public XMLBase
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800171{
172public:
173 XMLDocument();
Lee Thomason3f57d272012-01-11 15:30:03 -0800174 ~XMLDocument();
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800175
176 bool Parse( const char* );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800177 void Print( FILE* cfile=stdout, int depth=0 );
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800178
Lee Thomason3f57d272012-01-11 15:30:03 -0800179 XMLNode* Root() { return root; }
180 XMLNode* RootElement();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800181
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800182 enum {
183 ERROR_ELEMENT_MISMATCH,
184 ERROR_PARSING_ELEMENT,
185 ERROR_PARSING_ATTRIBUTE
186 };
187 void SetError( int error, const char* str1, const char* str2 ) {}
188
Lee Thomason3f57d272012-01-11 15:30:03 -0800189private:
190 XMLDocument( const XMLDocument& ); // intentionally not implemented
Lee Thomason3f57d272012-01-11 15:30:03 -0800191
192 XMLNode* root;
U-Lama\Lee560bd472011-12-28 19:42:49 -0800193 CharBuffer* charBuffer;
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800194};
195
196
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800197}; // tinyxml2
198
U-Lama\Lee560bd472011-12-28 19:42:49 -0800199
200
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800201#endif // TINYXML2_INCLUDED