blob: 4a3152a9f6b3a0b25df316630685e27921c6926e [file] [log] [blame]
U-Lama\Lee560bd472011-12-28 19:42:49 -08001#include "tinyxml2.h"
2
3#include <string.h>
4#include <stdlib.h>
5#include <stdio.h>
U-Lama\Lee4cee6112011-12-31 14:58:18 -08006#include <ctype.h>
U-Lama\Lee560bd472011-12-28 19:42:49 -08007
8using namespace tinyxml2;
9
Lee Thomasonfde6a752012-01-14 18:08:12 -080010static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
11static const char LF = LINE_FEED;
12static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
13static const char CR = CARRIAGE_RETURN;
14
15
Lee Thomason3f57d272012-01-11 15:30:03 -080016// --------- CharBuffer ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -080017/*static*/ CharBuffer* CharBuffer::Construct( const char* in )
18{
19 size_t len = strlen( in );
20 size_t size = len + sizeof( CharBuffer );
21 CharBuffer* cb = (CharBuffer*) malloc( size );
22 cb->length = len;
23 strcpy( cb->mem, in );
24 return cb;
25}
26
27
28/*static*/ void CharBuffer::Free( CharBuffer* cb )
29{
30 free( cb );
31}
32
33
Lee Thomason8a5dfee2012-01-18 17:43:40 -080034// --------- XMLBase ----------- //
35const char* XMLBase::ParseText( char* p, const char* endTag, char** next )
Lee Thomason3f57d272012-01-11 15:30:03 -080036{
37 TIXMLASSERT( endTag && *endTag );
38
Lee Thomasonfde6a752012-01-14 18:08:12 -080039 char* start = p;
40 char* q = p; // q (target) <= p (src) in same buffer.
41 char endChar = *endTag;
42 int length = strlen( endTag );
43 char* nextTag = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -080044 *next = 0;
Lee Thomason3f57d272012-01-11 15:30:03 -080045
Lee Thomasonfde6a752012-01-14 18:08:12 -080046 // Inner loop of text parsing.
Lee Thomason3f57d272012-01-11 15:30:03 -080047 while ( *p ) {
Lee Thomasonfde6a752012-01-14 18:08:12 -080048 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
49 *q = 0;
50 nextTag = p + length;
51 break;
Lee Thomason3f57d272012-01-11 15:30:03 -080052 }
Lee Thomasonfde6a752012-01-14 18:08:12 -080053 else if ( *p == CR ) {
54 // CR-LF pair becomes LF
55 // CR alone becomes LF
56 // LF-CR becomes LF
57 if ( *(p+1) == LF ) {
58 p += 2;
59 }
60 else {
61 ++p;
62 }
63 *q = LF;
64 }
65 else if ( *p == LF ) {
66 if ( *(p+1) == CR ) {
67 p += 2;
68 }
69 else {
70 ++p;
71 }
72 *q = LF;
73 }
74 else {
75 *q = *p;
76 ++p;
77 }
78 ++q;
Lee Thomason3f57d272012-01-11 15:30:03 -080079 }
Lee Thomasonfde6a752012-01-14 18:08:12 -080080
81 // Error? If we don't have a text tag, something went wrong. (Although
82 // what the nextTag points at may be null.)
83 if ( nextTag == 0 ) {
84 return 0;
85 }
86 *next = nextTag;
87 return start;
Lee Thomason3f57d272012-01-11 15:30:03 -080088}
89
90
Lee Thomason39ede242012-01-20 11:27:56 -080091char* XMLBase::ParseName( char* in, StrPair* pair );
92
Lee Thomason8a5dfee2012-01-18 17:43:40 -080093const char* XMLBase::ParseName( char* p, char** next )
94{
95 char* start = p;
96 char* nextTag = 0;
97 *next = 0;
98
99 start = p;
100 if ( !start || !(*start) ) {
101 return 0;
102 }
103
104 if ( !IsAlpha( *p ) ) {
105 return 0;
106 }
107
108 while( *p && (
109 IsAlphaNum( (unsigned char) *p )
110 || *p == '_'
111 || *p == '-'
112 || *p == '.'
113 || *p == ':' ))
114 {
115 ++p;
116 }
117 *p = 0;
118
119 if ( p > start ) {
Lee Thomason39ede242012-01-20 11:27:56 -0800120 *next = p+1;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800121 return start;
122 }
Lee Thomason39ede242012-01-20 11:27:56 -0800123 return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800124}
125
126
127char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
128{
129 XMLNode* returnNode = 0;
130
131 p = XMLNode::SkipWhiteSpace( p );
132 if( !p || !*p || *p != '<' )
133 {
134 return 0;
135 }
136
137 // What is this thing?
138 // - Elements start with a letter or underscore, but xml is reserved.
139 // - Comments: <!--
140 // - Decleration: <?xml
141 // - Everthing else is unknown to tinyxml.
142 //
143
144 static const char* xmlHeader = { "<?xml" };
145 static const char* commentHeader = { "<!--" };
146 static const char* dtdHeader = { "<!" };
147 static const char* cdataHeader = { "<![CDATA[" };
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800148 static const char* elementHeader = { "<" }; // and a header for everything else; check last.
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800149
150 static const int xmlHeaderLen = 5;
151 static const int commentHeaderLen = 4;
152 static const int dtdHeaderLen = 2;
153 static const int cdataHeaderLen = 9;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800154 static const int elementHeaderLen = 1;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800155
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800156 if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800157 returnNode = new XMLComment( document );
158 p += commentHeaderLen;
159 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800160 else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
161 returnNode = new XMLElement( document );
162 p += elementHeaderLen;
163 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800164 else {
165 TIXMLASSERT( 0 );
166 }
167
168 *node = returnNode;
169 return p;
170}
171
172
173// --------- XMLNode ----------- //
174
175XMLNode::XMLNode( XMLDocument* doc ) :
176 document( doc ),
177 parent( 0 ),
178 firstChild( 0 ), lastChild( 0 ),
179 prev( 0 ), next( 0 )
180{
181
182}
183
184
185XMLNode::~XMLNode()
186{
187 XMLNode* node=firstChild;
188 while( node ) {
189 XMLNode* temp = node->next;
190 delete node;
191 node = temp;
192 }
193 if ( prev ) {
194 prev->next = next;
195 }
196 if ( next ) {
197 next->prev = prev;
198 }
199}
200
201
202XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
203{
204 if ( lastChild ) {
205 TIXMLASSERT( firstChild );
206 TIXMLASSERT( lastChild->next == 0 );
207 lastChild->next = addThis;
208 addThis->prev = lastChild;
209 lastChild = addThis;
210
211 addThis->parent = this;
212 addThis->next = 0;
213 }
214 else {
215 TIXMLASSERT( firstChild == 0 );
216 firstChild = lastChild = addThis;
217
218 addThis->parent = this;
219 addThis->prev = 0;
220 addThis->next = 0;
221 }
222 return addThis;
223}
224
225
226void XMLNode::Print( FILE* fp, int depth )
227{
228 for( XMLNode* node = firstChild; node; node=node->next ) {
229 node->Print( fp, depth );
230 }
231}
232
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800233
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800234void XMLNode::PrintSpace( FILE* fp, int depth )
235{
236 for( int i=0; i<depth; ++i ) {
237 fprintf( fp, " " );
238 }
239}
240
241
242
243
Lee Thomason3f57d272012-01-11 15:30:03 -0800244// --------- XMLComment ---------- //
245
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800246XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ), value( 0 )
Lee Thomason3f57d272012-01-11 15:30:03 -0800247{
248}
249
250
Lee Thomasonce0763e2012-01-11 15:43:54 -0800251XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800252{
Lee Thomason3f57d272012-01-11 15:30:03 -0800253}
254
255
Lee Thomasonce0763e2012-01-11 15:43:54 -0800256void XMLComment::Print( FILE* fp, int depth )
257{
258 XMLNode::Print( fp, depth );
Lee Thomasonfde6a752012-01-14 18:08:12 -0800259 fprintf( fp, "<!--%s-->\n", value );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800260}
261
262
263char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800264{
265 // Comment parses as text.
266 value = ParseText( p, "-->", &p );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800267 return p;
268}
269
270
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800271// --------- XMLAttribute ---------- //
272char* XMLAttribute::ParseDeep( char* p )
273{
274 char endTag[2] = { *p, 0 };
275 ++p;
276 value = ParseText( p, endTag, &p );
277 if ( !value ) return 0;
278 return p;
279}
280
281
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800282void XMLAttribute::Print( FILE* cfile )
283{
284 fprintf( cfile, "\"%s\"", value );
285}
286
287
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800288// --------- XMLElement ---------- //
289XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
290 name( 0 ),
291 closing( false ),
292 rootAttribute( 0 ),
293 lastAttribute( 0 )
294{
295}
296
297
298XMLElement::~XMLElement()
299{
300 XMLAttribute* attribute = rootAttribute;
301 while( attribute ) {
302 XMLAttribute* next = attribute->next;
303 delete attribute;
304 attribute = next;
305 }
306
307 XMLNode* child = firstChild;
308 while( child ) {
309 XMLNode* next = child->next;
310 delete child;
311 child = next;
312 }
313}
314
315
316char* XMLElement::ParseDeep( char* p )
317{
318 // Read the element name.
319 p = SkipWhiteSpace( p );
320 if ( !p ) return 0;
321 const char* start = p;
322
323 // The closing element is the </element> form. It is
324 // parsed just like a regular element then deleted from
325 // the DOM.
326 if ( *p == '/' ) {
327 closing = true;
328 ++p;
329 }
330
331 name = ParseName( p, &p );
332 if ( !name ) return 0;
333
334 // Read the attributes.
335 while( p ) {
336 p = SkipWhiteSpace( p );
337 if ( !p || !(*p) ) {
338 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name );
339 return 0;
340 }
341 const char* saveP = p;
342
343 // attribute.
344 if ( *p == '\'' || *p == '\"' ) {
345 XMLAttribute* attrib = new XMLAttribute( this );
346 p = attrib->ParseDeep( p );
347 if ( !p ) {
348 delete attrib;
349 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, saveP );
350 return 0;
351 }
352 if ( rootAttribute ) {
353 TIXMLASSERT( lastAttribute );
354 lastAttribute->next = attrib;
355 lastAttribute = attrib;
356 }
357 else {
358 rootAttribute = lastAttribute = attrib;
359 }
360 }
361 else if ( *p == '/' && *(p+1) == '>' ) {
362 // end tag.
363 if ( closing ) {
364 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
365 return 0;
366 }
367 return p+2; // done; sealed element.
368 }
369 else if ( *p == '>' ) {
370 ++p;
371 break;
372 }
373 }
374
375 while( p && *p ) {
376 XMLNode* node = 0;
377 p = Identify( document, p, &node );
378 if ( p && node ) {
379 node->ParseDeep( p );
380
381 XMLElement* element = node->ToElement();
382 if ( element && element->Closing() ) {
383 if ( StringEqual( element->Name(), this->Name() ) ) {
384 // All good, this is closing tag.
385 delete node;
386 p = 0;
387 }
388 else {
389 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
390 delete node;
391 }
392 return p;
393 }
394 else {
395 this->InsertEndChild( node );
396 }
397 }
398 }
399 return 0;
400}
401
402
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800403void XMLElement::Print( FILE* cfile, int depth )
404{
405 PrintSpace( cfile, depth );
406 fprintf( cfile, "<%s", Name() );
407
408 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
409 fprintf( cfile, " " );
410 attrib->Print( cfile );
411 }
412
413 if ( firstChild ) {
414 fprintf( cfile, ">/n" );
415 for( XMLNode* node=firstChild; node; node=node->next ) {
416 node->Print( cfile, depth+1 );
417 }
418 fprintf( cfile, "</%s>", Name() );
419 }
420 else {
421 fprintf( cfile, "/>\n" );
422 }
423}
424
425
Lee Thomason3f57d272012-01-11 15:30:03 -0800426// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800427XMLDocument::XMLDocument() :
428 charBuffer( 0 )
429{
Lee Thomason85403d82012-01-11 15:55:05 -0800430 root = new XMLNode( this );
U-Lama\Lee560bd472011-12-28 19:42:49 -0800431}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800432
433
Lee Thomason3f57d272012-01-11 15:30:03 -0800434XMLDocument::~XMLDocument()
435{
436 delete root;
437 delete charBuffer;
438}
439
440
441
442bool XMLDocument::Parse( const char* p )
443{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800444 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800445 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800446
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800447 char* q = Identify( this, charBuffer->mem, &node );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800448 if ( node ) {
449 root->InsertEndChild( node );
450 node->ParseDeep( q );
451 return true;
452 }
453 return false;
Lee Thomason3f57d272012-01-11 15:30:03 -0800454}
455
456
Lee Thomasonce0763e2012-01-11 15:43:54 -0800457void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800458{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800459 for( XMLNode* node = root->firstChild; node; node=node->next ) {
460 node->Print( fp, depth );
461 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800462}
463
464