blob: 0a02c4bd579600a2d70bb4e0f700ff1be1646fba [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;
Lee Thomasonec975ce2012-01-23 11:42:06 -080070 ++q;
Lee Thomasone4422302012-01-20 17:59:50 -080071 }
72 }
73 }
74 flags = 0;
75 }
76 return start;
77}
78
79
Lee Thomason8a5dfee2012-01-18 17:43:40 -080080// --------- XMLBase ----------- //
Lee Thomasone4422302012-01-20 17:59:50 -080081char* XMLBase::ParseText( char* p, StrPair* pair, const char* endTag )
Lee Thomason3f57d272012-01-11 15:30:03 -080082{
83 TIXMLASSERT( endTag && *endTag );
84
Lee Thomasonfde6a752012-01-14 18:08:12 -080085 char* start = p;
Lee Thomasonfde6a752012-01-14 18:08:12 -080086 char endChar = *endTag;
87 int length = strlen( endTag );
Lee Thomason3f57d272012-01-11 15:30:03 -080088
Lee Thomasonfde6a752012-01-14 18:08:12 -080089 // Inner loop of text parsing.
Lee Thomason3f57d272012-01-11 15:30:03 -080090 while ( *p ) {
Lee Thomasonfde6a752012-01-14 18:08:12 -080091 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
Lee Thomasone4422302012-01-20 17:59:50 -080092 pair->Set( start, p, StrPair::NEEDS_ENTITY_PROCESSING | StrPair::NEEDS_NEWLINE_NORMALIZATION );
Lee Thomasonec975ce2012-01-23 11:42:06 -080093 return p + length;
Lee Thomason3f57d272012-01-11 15:30:03 -080094 }
Lee Thomasonec975ce2012-01-23 11:42:06 -080095 ++p;
Lee Thomason3f57d272012-01-11 15:30:03 -080096 }
Lee Thomasone4422302012-01-20 17:59:50 -080097 return p;
Lee Thomason3f57d272012-01-11 15:30:03 -080098}
99
100
Lee Thomasond34f52c2012-01-20 12:55:24 -0800101char* XMLBase::ParseName( char* p, StrPair* pair )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800102{
103 char* start = p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800104
105 start = p;
106 if ( !start || !(*start) ) {
107 return 0;
108 }
109
110 if ( !IsAlpha( *p ) ) {
111 return 0;
112 }
113
114 while( *p && (
115 IsAlphaNum( (unsigned char) *p )
116 || *p == '_'
117 || *p == '-'
118 || *p == '.'
119 || *p == ':' ))
120 {
121 ++p;
122 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800123
124 if ( p > start ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800125 pair->Set( start, p, 0 );
126 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800127 }
Lee Thomason39ede242012-01-20 11:27:56 -0800128 return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800129}
130
131
132char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
133{
134 XMLNode* returnNode = 0;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800135 char* start = p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800136 p = XMLNode::SkipWhiteSpace( p );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800137 if( !p || !*p )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800138 {
139 return 0;
140 }
141
142 // What is this thing?
143 // - Elements start with a letter or underscore, but xml is reserved.
144 // - Comments: <!--
145 // - Decleration: <?xml
146 // - Everthing else is unknown to tinyxml.
147 //
148
149 static const char* xmlHeader = { "<?xml" };
150 static const char* commentHeader = { "<!--" };
151 static const char* dtdHeader = { "<!" };
152 static const char* cdataHeader = { "<![CDATA[" };
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800153 static const char* elementHeader = { "<" }; // and a header for everything else; check last.
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800154
155 static const int xmlHeaderLen = 5;
156 static const int commentHeaderLen = 4;
157 static const int dtdHeaderLen = 2;
158 static const int cdataHeaderLen = 9;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800159 static const int elementHeaderLen = 1;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800160
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800161 if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800162 returnNode = new XMLComment( document );
163 p += commentHeaderLen;
164 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800165 else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
166 returnNode = new XMLElement( document );
167 p += elementHeaderLen;
168 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800169 // fixme: better text detection
170 else if ( (*p != '<') && IsAlphaNum( *p ) ) {
171 // fixme: this is filtering out empty text...should it?
172 returnNode = new XMLText( document );
173 p = start; // Back it up, all the text counts.
174 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800175 else {
176 TIXMLASSERT( 0 );
177 }
178
179 *node = returnNode;
180 return p;
181}
182
183
184// --------- XMLNode ----------- //
185
186XMLNode::XMLNode( XMLDocument* doc ) :
187 document( doc ),
188 parent( 0 ),
189 firstChild( 0 ), lastChild( 0 ),
190 prev( 0 ), next( 0 )
191{
192
193}
194
195
196XMLNode::~XMLNode()
197{
Lee Thomasond923c672012-01-23 08:44:25 -0800198 //printf( "~XMLNode %x\n", this );
199 while( firstChild ) {
200 XMLNode* node = firstChild;
201 Unlink( node );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800202 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800203 }
Lee Thomasond923c672012-01-23 08:44:25 -0800204}
205
206
207void XMLNode::Unlink( XMLNode* child )
208{
209 TIXMLASSERT( child->parent == this );
210 if ( child == firstChild )
211 firstChild = firstChild->next;
212 if ( child == lastChild )
213 lastChild = lastChild->prev;
214
215 if ( child->prev ) {
216 child->prev->next = child->next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800217 }
Lee Thomasond923c672012-01-23 08:44:25 -0800218 if ( child->next ) {
219 child->next->prev = child->prev;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800220 }
Lee Thomasond923c672012-01-23 08:44:25 -0800221 child->parent = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800222}
223
224
225XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
226{
227 if ( lastChild ) {
228 TIXMLASSERT( firstChild );
229 TIXMLASSERT( lastChild->next == 0 );
230 lastChild->next = addThis;
231 addThis->prev = lastChild;
232 lastChild = addThis;
233
234 addThis->parent = this;
235 addThis->next = 0;
236 }
237 else {
238 TIXMLASSERT( firstChild == 0 );
239 firstChild = lastChild = addThis;
240
241 addThis->parent = this;
242 addThis->prev = 0;
243 addThis->next = 0;
244 }
245 return addThis;
246}
247
248
249void XMLNode::Print( FILE* fp, int depth )
250{
251 for( XMLNode* node = firstChild; node; node=node->next ) {
252 node->Print( fp, depth );
253 }
254}
255
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800256
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800257void XMLNode::PrintSpace( FILE* fp, int depth )
258{
259 for( int i=0; i<depth; ++i ) {
260 fprintf( fp, " " );
261 }
262}
263
264
Lee Thomason5492a1c2012-01-23 15:32:10 -0800265// --------- XMLText ---------- //
266char* XMLText::ParseDeep( char* p )
267{
268 p = ParseText( p, &value, "<" );
269 // consumes the end tag.
270 if ( p && *p ) {
271 return p-1;
272 }
273 return 0;
274}
275
276
277void XMLText::Print( FILE* cfile, int depth )
278{
279 fprintf( cfile, value.GetStr() );
280}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800281
282
Lee Thomason3f57d272012-01-11 15:30:03 -0800283// --------- XMLComment ---------- //
284
Lee Thomasone4422302012-01-20 17:59:50 -0800285XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800286{
287}
288
289
Lee Thomasonce0763e2012-01-11 15:43:54 -0800290XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800291{
Lee Thomasond923c672012-01-23 08:44:25 -0800292 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800293}
294
295
Lee Thomasonce0763e2012-01-11 15:43:54 -0800296void XMLComment::Print( FILE* fp, int depth )
297{
298 XMLNode::Print( fp, depth );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800299 fprintf( fp, "<!--%s-->\n", value.GetStr() );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800300}
301
302
303char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800304{
305 // Comment parses as text.
Lee Thomasone4422302012-01-20 17:59:50 -0800306 return ParseText( p, &value, "-->" );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800307}
308
309
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800310// --------- XMLAttribute ---------- //
311char* XMLAttribute::ParseDeep( char* p )
312{
Lee Thomason22aead12012-01-23 13:29:35 -0800313 p = ParseText( p, &name, "=" );
314 if ( !p || !*p ) return 0;
315
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800316 char endTag[2] = { *p, 0 };
317 ++p;
Lee Thomasone4422302012-01-20 17:59:50 -0800318 p = ParseText( p, &value, endTag );
319 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800320 return p;
321}
322
323
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800324void XMLAttribute::Print( FILE* cfile )
325{
Lee Thomason22aead12012-01-23 13:29:35 -0800326 // fixme: sort out single vs. double quote
327 fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800328}
329
330
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800331// --------- XMLElement ---------- //
332XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800333 closing( false ),
334 rootAttribute( 0 ),
335 lastAttribute( 0 )
336{
337}
338
339
340XMLElement::~XMLElement()
341{
Lee Thomasond923c672012-01-23 08:44:25 -0800342 //printf( "~XMLElemen %x\n",this );
343
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800344 XMLAttribute* attribute = rootAttribute;
345 while( attribute ) {
346 XMLAttribute* next = attribute->next;
347 delete attribute;
348 attribute = next;
349 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800350}
351
352
353char* XMLElement::ParseDeep( char* p )
354{
355 // Read the element name.
356 p = SkipWhiteSpace( p );
357 if ( !p ) return 0;
358 const char* start = p;
359
360 // The closing element is the </element> form. It is
361 // parsed just like a regular element then deleted from
362 // the DOM.
363 if ( *p == '/' ) {
364 closing = true;
365 ++p;
366 }
367
Lee Thomasone4422302012-01-20 17:59:50 -0800368 p = ParseName( p, &name );
369 if ( name.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800370
371 // Read the attributes.
372 while( p ) {
373 p = SkipWhiteSpace( p );
374 if ( !p || !(*p) ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800375 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800376 return 0;
377 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800378
379 // attribute.
Lee Thomason22aead12012-01-23 13:29:35 -0800380 if ( IsAlpha( *p ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800381 XMLAttribute* attrib = new XMLAttribute( this );
382 p = attrib->ParseDeep( p );
383 if ( !p ) {
384 delete attrib;
Lee Thomasone4422302012-01-20 17:59:50 -0800385 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800386 return 0;
387 }
388 if ( rootAttribute ) {
389 TIXMLASSERT( lastAttribute );
390 lastAttribute->next = attrib;
391 lastAttribute = attrib;
392 }
393 else {
394 rootAttribute = lastAttribute = attrib;
395 }
396 }
Lee Thomasone4422302012-01-20 17:59:50 -0800397 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800398 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800399 if ( closing ) {
400 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
401 return 0;
402 }
403 return p+2; // done; sealed element.
404 }
Lee Thomasone4422302012-01-20 17:59:50 -0800405 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800406 else if ( *p == '>' ) {
407 ++p;
408 break;
409 }
Lee Thomasone4422302012-01-20 17:59:50 -0800410 else {
411 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
412 return 0;
413 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800414 }
415
416 while( p && *p ) {
417 XMLNode* node = 0;
418 p = Identify( document, p, &node );
419 if ( p && node ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800420 p = node->ParseDeep( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800421
422 XMLElement* element = node->ToElement();
423 if ( element && element->Closing() ) {
424 if ( StringEqual( element->Name(), this->Name() ) ) {
425 // All good, this is closing tag.
426 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800427 }
428 else {
429 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
430 delete node;
Lee Thomasone4422302012-01-20 17:59:50 -0800431 p = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800432 }
433 return p;
434 }
435 else {
436 this->InsertEndChild( node );
437 }
438 }
439 }
440 return 0;
441}
442
443
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800444void XMLElement::Print( FILE* cfile, int depth )
445{
446 PrintSpace( cfile, depth );
447 fprintf( cfile, "<%s", Name() );
448
449 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
450 fprintf( cfile, " " );
451 attrib->Print( cfile );
452 }
453
454 if ( firstChild ) {
Lee Thomason5492a1c2012-01-23 15:32:10 -0800455 // fixme: once text is on, it should stay on, and not use newlines.
456 bool useNewline = firstChild->ToText() == 0;
457
458 fprintf( cfile, ">", Name() );
459 if ( useNewline ) fprintf( cfile, "\n" );
460
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800461 for( XMLNode* node=firstChild; node; node=node->next ) {
462 node->Print( cfile, depth+1 );
463 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800464
Lee Thomasonec975ce2012-01-23 11:42:06 -0800465 fprintf( cfile, "</%s>\n", Name() );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800466 // fixme: see note above
467 //if ( useNewline ) fprintf( cfile, "\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800468 }
469 else {
470 fprintf( cfile, "/>\n" );
471 }
472}
473
474
Lee Thomason3f57d272012-01-11 15:30:03 -0800475// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800476XMLDocument::XMLDocument() :
477 charBuffer( 0 )
478{
Lee Thomason85403d82012-01-11 15:55:05 -0800479 root = new XMLNode( this );
U-Lama\Lee560bd472011-12-28 19:42:49 -0800480}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800481
482
Lee Thomason3f57d272012-01-11 15:30:03 -0800483XMLDocument::~XMLDocument()
484{
485 delete root;
486 delete charBuffer;
487}
488
489
490
491bool XMLDocument::Parse( const char* p )
492{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800493 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800494 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800495
Lee Thomason22aead12012-01-23 13:29:35 -0800496 // fixme: clean up
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800497 char* q = Identify( this, charBuffer->mem, &node );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800498 while ( node ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800499 root->InsertEndChild( node );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800500 q = node->ParseDeep( q );
501 node = 0;
502 if ( q && *q ) {
503 q = Identify( this, q, &node );
504 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800505 }
506 return false;
Lee Thomason3f57d272012-01-11 15:30:03 -0800507}
508
509
Lee Thomasonce0763e2012-01-11 15:43:54 -0800510void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800511{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800512 for( XMLNode* node = root->firstChild; node; node=node->next ) {
513 node->Print( fp, depth );
514 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800515}
516
517