blob: eae8f26c56b33900366a37617ae93f512a8932cf [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 Thomasone4422302012-01-20 17:59:50 -080010static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
Lee Thomasonfde6a752012-01-14 18:08:12 -080011static const char LF = LINE_FEED;
12static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
13static const char CR = CARRIAGE_RETURN;
Lee Thomasone4422302012-01-20 17:59:50 -080014static const char SINGLE_QUOTE = '\'';
15static const char DOUBLE_QUOTE = '\"';
Lee Thomasonfde6a752012-01-14 18:08:12 -080016
17
Lee Thomason3f57d272012-01-11 15:30:03 -080018// --------- CharBuffer ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -080019/*static*/ CharBuffer* CharBuffer::Construct( const char* in )
20{
21 size_t len = strlen( in );
22 size_t size = len + sizeof( CharBuffer );
23 CharBuffer* cb = (CharBuffer*) malloc( size );
24 cb->length = len;
25 strcpy( cb->mem, in );
26 return cb;
27}
28
29
30/*static*/ void CharBuffer::Free( CharBuffer* cb )
31{
32 free( cb );
33}
34
35
Lee Thomasone4422302012-01-20 17:59:50 -080036const char* StrPair::GetStr()
37{
38 if ( flags & NEEDS_FLUSH ) {
39 *end = 0;
40
41 if ( flags & ( NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION ) ) {
42 char* p = start;
43 char* q = start;
44
45 while( p < end ) {
46 if ( *p == CR ) {
47 // CR-LF pair becomes LF
48 // CR alone becomes LF
49 // LF-CR becomes LF
50 if ( *(p+1) == LF ) {
51 p += 2;
52 }
53 else {
54 ++p;
55 }
56 *q = LF;
57 }
58 else if ( *p == LF ) {
59 if ( *(p+1) == CR ) {
60 p += 2;
61 }
62 else {
63 ++p;
64 }
65 *q = LF;
66 }
67 else {
68 *q = *p;
69 ++p;
70 }
71 }
72 }
73 flags = 0;
74 }
75 return start;
76}
77
78
Lee Thomason8a5dfee2012-01-18 17:43:40 -080079// --------- XMLBase ----------- //
Lee Thomasone4422302012-01-20 17:59:50 -080080char* XMLBase::ParseText( char* p, StrPair* pair, const char* endTag )
Lee Thomason3f57d272012-01-11 15:30:03 -080081{
82 TIXMLASSERT( endTag && *endTag );
83
Lee Thomasonfde6a752012-01-14 18:08:12 -080084 char* start = p;
Lee Thomasonfde6a752012-01-14 18:08:12 -080085 char endChar = *endTag;
86 int length = strlen( endTag );
Lee Thomason3f57d272012-01-11 15:30:03 -080087
Lee Thomasonfde6a752012-01-14 18:08:12 -080088 // Inner loop of text parsing.
Lee Thomason3f57d272012-01-11 15:30:03 -080089 while ( *p ) {
Lee Thomasonfde6a752012-01-14 18:08:12 -080090 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
Lee Thomasone4422302012-01-20 17:59:50 -080091 pair->Set( start, p, StrPair::NEEDS_ENTITY_PROCESSING | StrPair::NEEDS_NEWLINE_NORMALIZATION );
Lee Thomasonfde6a752012-01-14 18:08:12 -080092 break;
Lee Thomason3f57d272012-01-11 15:30:03 -080093 }
Lee Thomason3f57d272012-01-11 15:30:03 -080094 }
Lee Thomasone4422302012-01-20 17:59:50 -080095 return p;
Lee Thomason3f57d272012-01-11 15:30:03 -080096}
97
98
Lee Thomasond34f52c2012-01-20 12:55:24 -080099char* XMLBase::ParseName( char* p, StrPair* pair )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800100{
101 char* start = p;
102 char* nextTag = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800103
104 start = p;
105 if ( !start || !(*start) ) {
106 return 0;
107 }
108
109 if ( !IsAlpha( *p ) ) {
110 return 0;
111 }
112
113 while( *p && (
114 IsAlphaNum( (unsigned char) *p )
115 || *p == '_'
116 || *p == '-'
117 || *p == '.'
118 || *p == ':' ))
119 {
120 ++p;
121 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800122
123 if ( p > start ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800124 pair->Set( start, p, 0 );
125 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800126 }
Lee Thomason39ede242012-01-20 11:27:56 -0800127 return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800128}
129
130
131char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
132{
133 XMLNode* returnNode = 0;
134
135 p = XMLNode::SkipWhiteSpace( p );
136 if( !p || !*p || *p != '<' )
137 {
138 return 0;
139 }
140
141 // What is this thing?
142 // - Elements start with a letter or underscore, but xml is reserved.
143 // - Comments: <!--
144 // - Decleration: <?xml
145 // - Everthing else is unknown to tinyxml.
146 //
147
148 static const char* xmlHeader = { "<?xml" };
149 static const char* commentHeader = { "<!--" };
150 static const char* dtdHeader = { "<!" };
151 static const char* cdataHeader = { "<![CDATA[" };
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800152 static const char* elementHeader = { "<" }; // and a header for everything else; check last.
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800153
154 static const int xmlHeaderLen = 5;
155 static const int commentHeaderLen = 4;
156 static const int dtdHeaderLen = 2;
157 static const int cdataHeaderLen = 9;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800158 static const int elementHeaderLen = 1;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800159
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800160 if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800161 returnNode = new XMLComment( document );
162 p += commentHeaderLen;
163 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800164 else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
165 returnNode = new XMLElement( document );
166 p += elementHeaderLen;
167 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800168 else {
169 TIXMLASSERT( 0 );
170 }
171
172 *node = returnNode;
173 return p;
174}
175
176
177// --------- XMLNode ----------- //
178
179XMLNode::XMLNode( XMLDocument* doc ) :
180 document( doc ),
181 parent( 0 ),
182 firstChild( 0 ), lastChild( 0 ),
183 prev( 0 ), next( 0 )
184{
185
186}
187
188
189XMLNode::~XMLNode()
190{
Lee Thomasond923c672012-01-23 08:44:25 -0800191 //printf( "~XMLNode %x\n", this );
192 while( firstChild ) {
193 XMLNode* node = firstChild;
194 Unlink( node );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800195 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800196 }
Lee Thomasond923c672012-01-23 08:44:25 -0800197}
198
199
200void XMLNode::Unlink( XMLNode* child )
201{
202 TIXMLASSERT( child->parent == this );
203 if ( child == firstChild )
204 firstChild = firstChild->next;
205 if ( child == lastChild )
206 lastChild = lastChild->prev;
207
208 if ( child->prev ) {
209 child->prev->next = child->next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800210 }
Lee Thomasond923c672012-01-23 08:44:25 -0800211 if ( child->next ) {
212 child->next->prev = child->prev;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800213 }
Lee Thomasond923c672012-01-23 08:44:25 -0800214 child->parent = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800215}
216
217
218XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
219{
220 if ( lastChild ) {
221 TIXMLASSERT( firstChild );
222 TIXMLASSERT( lastChild->next == 0 );
223 lastChild->next = addThis;
224 addThis->prev = lastChild;
225 lastChild = addThis;
226
227 addThis->parent = this;
228 addThis->next = 0;
229 }
230 else {
231 TIXMLASSERT( firstChild == 0 );
232 firstChild = lastChild = addThis;
233
234 addThis->parent = this;
235 addThis->prev = 0;
236 addThis->next = 0;
237 }
238 return addThis;
239}
240
241
242void XMLNode::Print( FILE* fp, int depth )
243{
244 for( XMLNode* node = firstChild; node; node=node->next ) {
245 node->Print( fp, depth );
246 }
247}
248
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800249
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800250void XMLNode::PrintSpace( FILE* fp, int depth )
251{
252 for( int i=0; i<depth; ++i ) {
253 fprintf( fp, " " );
254 }
255}
256
257
258
259
Lee Thomason3f57d272012-01-11 15:30:03 -0800260// --------- XMLComment ---------- //
261
Lee Thomasone4422302012-01-20 17:59:50 -0800262XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800263{
264}
265
266
Lee Thomasonce0763e2012-01-11 15:43:54 -0800267XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800268{
Lee Thomasond923c672012-01-23 08:44:25 -0800269 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800270}
271
272
Lee Thomasonce0763e2012-01-11 15:43:54 -0800273void XMLComment::Print( FILE* fp, int depth )
274{
275 XMLNode::Print( fp, depth );
Lee Thomasonfde6a752012-01-14 18:08:12 -0800276 fprintf( fp, "<!--%s-->\n", value );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800277}
278
279
280char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800281{
282 // Comment parses as text.
Lee Thomasone4422302012-01-20 17:59:50 -0800283 return ParseText( p, &value, "-->" );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800284}
285
286
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800287// --------- XMLAttribute ---------- //
288char* XMLAttribute::ParseDeep( char* p )
289{
290 char endTag[2] = { *p, 0 };
291 ++p;
Lee Thomasone4422302012-01-20 17:59:50 -0800292 p = ParseText( p, &value, endTag );
293 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800294 return p;
295}
296
297
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800298void XMLAttribute::Print( FILE* cfile )
299{
300 fprintf( cfile, "\"%s\"", value );
301}
302
303
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800304// --------- XMLElement ---------- //
305XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800306 closing( false ),
307 rootAttribute( 0 ),
308 lastAttribute( 0 )
309{
310}
311
312
313XMLElement::~XMLElement()
314{
Lee Thomasond923c672012-01-23 08:44:25 -0800315 //printf( "~XMLElemen %x\n",this );
316
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800317 XMLAttribute* attribute = rootAttribute;
318 while( attribute ) {
319 XMLAttribute* next = attribute->next;
320 delete attribute;
321 attribute = next;
322 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800323}
324
325
326char* XMLElement::ParseDeep( char* p )
327{
328 // Read the element name.
329 p = SkipWhiteSpace( p );
330 if ( !p ) return 0;
331 const char* start = p;
332
333 // The closing element is the </element> form. It is
334 // parsed just like a regular element then deleted from
335 // the DOM.
336 if ( *p == '/' ) {
337 closing = true;
338 ++p;
339 }
340
Lee Thomasone4422302012-01-20 17:59:50 -0800341 p = ParseName( p, &name );
342 if ( name.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800343
344 // Read the attributes.
345 while( p ) {
346 p = SkipWhiteSpace( p );
347 if ( !p || !(*p) ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800348 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800349 return 0;
350 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800351
352 // attribute.
Lee Thomasone4422302012-01-20 17:59:50 -0800353 if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800354 XMLAttribute* attrib = new XMLAttribute( this );
355 p = attrib->ParseDeep( p );
356 if ( !p ) {
357 delete attrib;
Lee Thomasone4422302012-01-20 17:59:50 -0800358 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800359 return 0;
360 }
361 if ( rootAttribute ) {
362 TIXMLASSERT( lastAttribute );
363 lastAttribute->next = attrib;
364 lastAttribute = attrib;
365 }
366 else {
367 rootAttribute = lastAttribute = attrib;
368 }
369 }
Lee Thomasone4422302012-01-20 17:59:50 -0800370 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800371 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800372 if ( closing ) {
373 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
374 return 0;
375 }
376 return p+2; // done; sealed element.
377 }
Lee Thomasone4422302012-01-20 17:59:50 -0800378 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800379 else if ( *p == '>' ) {
380 ++p;
381 break;
382 }
Lee Thomasone4422302012-01-20 17:59:50 -0800383 else {
384 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
385 return 0;
386 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800387 }
388
389 while( p && *p ) {
390 XMLNode* node = 0;
391 p = Identify( document, p, &node );
392 if ( p && node ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800393 p = node->ParseDeep( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800394
395 XMLElement* element = node->ToElement();
396 if ( element && element->Closing() ) {
397 if ( StringEqual( element->Name(), this->Name() ) ) {
398 // All good, this is closing tag.
399 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800400 }
401 else {
402 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
403 delete node;
Lee Thomasone4422302012-01-20 17:59:50 -0800404 p = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800405 }
406 return p;
407 }
408 else {
409 this->InsertEndChild( node );
410 }
411 }
412 }
413 return 0;
414}
415
416
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800417void XMLElement::Print( FILE* cfile, int depth )
418{
419 PrintSpace( cfile, depth );
420 fprintf( cfile, "<%s", Name() );
421
422 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
423 fprintf( cfile, " " );
424 attrib->Print( cfile );
425 }
426
427 if ( firstChild ) {
428 fprintf( cfile, ">/n" );
429 for( XMLNode* node=firstChild; node; node=node->next ) {
430 node->Print( cfile, depth+1 );
431 }
432 fprintf( cfile, "</%s>", Name() );
433 }
434 else {
435 fprintf( cfile, "/>\n" );
436 }
437}
438
439
Lee Thomason3f57d272012-01-11 15:30:03 -0800440// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800441XMLDocument::XMLDocument() :
442 charBuffer( 0 )
443{
Lee Thomason85403d82012-01-11 15:55:05 -0800444 root = new XMLNode( this );
U-Lama\Lee560bd472011-12-28 19:42:49 -0800445}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800446
447
Lee Thomason3f57d272012-01-11 15:30:03 -0800448XMLDocument::~XMLDocument()
449{
450 delete root;
451 delete charBuffer;
452}
453
454
455
456bool XMLDocument::Parse( const char* p )
457{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800458 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800459 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800460
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800461 char* q = Identify( this, charBuffer->mem, &node );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800462 if ( node ) {
463 root->InsertEndChild( node );
464 node->ParseDeep( q );
465 return true;
466 }
467 return false;
Lee Thomason3f57d272012-01-11 15:30:03 -0800468}
469
470
Lee Thomasonce0763e2012-01-11 15:43:54 -0800471void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800472{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800473 for( XMLNode* node = root->firstChild; node; node=node->next ) {
474 node->Print( fp, depth );
475 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800476}
477
478