blob: 9df038f87144c0c672d64907b584e34b3495ece2 [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 ),
Lee Thomason67d61312012-01-24 16:01:51 -0800189 isTextParent( false ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800190 firstChild( 0 ), lastChild( 0 ),
191 prev( 0 ), next( 0 )
192{
193
194}
195
196
197XMLNode::~XMLNode()
198{
Lee Thomasond923c672012-01-23 08:44:25 -0800199 //printf( "~XMLNode %x\n", this );
200 while( firstChild ) {
201 XMLNode* node = firstChild;
202 Unlink( node );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800203 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800204 }
Lee Thomasond923c672012-01-23 08:44:25 -0800205}
206
207
208void XMLNode::Unlink( XMLNode* child )
209{
210 TIXMLASSERT( child->parent == this );
211 if ( child == firstChild )
212 firstChild = firstChild->next;
213 if ( child == lastChild )
214 lastChild = lastChild->prev;
215
216 if ( child->prev ) {
217 child->prev->next = child->next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800218 }
Lee Thomasond923c672012-01-23 08:44:25 -0800219 if ( child->next ) {
220 child->next->prev = child->prev;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800221 }
Lee Thomasond923c672012-01-23 08:44:25 -0800222 child->parent = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800223}
224
225
226XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
227{
228 if ( lastChild ) {
229 TIXMLASSERT( firstChild );
230 TIXMLASSERT( lastChild->next == 0 );
231 lastChild->next = addThis;
232 addThis->prev = lastChild;
233 lastChild = addThis;
234
235 addThis->parent = this;
236 addThis->next = 0;
237 }
238 else {
239 TIXMLASSERT( firstChild == 0 );
240 firstChild = lastChild = addThis;
241
242 addThis->parent = this;
243 addThis->prev = 0;
244 addThis->next = 0;
245 }
Lee Thomason67d61312012-01-24 16:01:51 -0800246 if ( addThis->ToText() ) {
247 SetTextParent();
248 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800249 return addThis;
250}
251
252
253void XMLNode::Print( FILE* fp, int depth )
254{
255 for( XMLNode* node = firstChild; node; node=node->next ) {
256 node->Print( fp, depth );
257 }
258}
259
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800260
Lee Thomason67d61312012-01-24 16:01:51 -0800261char* XMLNode::ParseDeep( char* p )
262{
263 while( p && *p ) {
264 XMLNode* node = 0;
265 p = Identify( document, p, &node );
266 if ( p && node ) {
267 p = node->ParseDeep( p );
268 // FIXME: is it the correct closing element?
269 if ( node->IsClosingElement() ) {
270 delete node;
271 return p;
272 }
273 this->InsertEndChild( node );
274 }
275 }
276 return 0;
277}
278
279
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800280void XMLNode::PrintSpace( FILE* fp, int depth )
281{
282 for( int i=0; i<depth; ++i ) {
283 fprintf( fp, " " );
284 }
285}
286
287
Lee Thomason5492a1c2012-01-23 15:32:10 -0800288// --------- XMLText ---------- //
289char* XMLText::ParseDeep( char* p )
290{
291 p = ParseText( p, &value, "<" );
292 // consumes the end tag.
293 if ( p && *p ) {
294 return p-1;
295 }
296 return 0;
297}
298
299
300void XMLText::Print( FILE* cfile, int depth )
301{
Lee Thomason67d61312012-01-24 16:01:51 -0800302 const char* v = value.GetStr();
303 fprintf( cfile, v );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800304}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800305
306
Lee Thomason3f57d272012-01-11 15:30:03 -0800307// --------- XMLComment ---------- //
308
Lee Thomasone4422302012-01-20 17:59:50 -0800309XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800310{
311}
312
313
Lee Thomasonce0763e2012-01-11 15:43:54 -0800314XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800315{
Lee Thomasond923c672012-01-23 08:44:25 -0800316 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800317}
318
319
Lee Thomasonce0763e2012-01-11 15:43:54 -0800320void XMLComment::Print( FILE* fp, int depth )
321{
322 XMLNode::Print( fp, depth );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800323 fprintf( fp, "<!--%s-->\n", value.GetStr() );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800324}
325
326
327char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800328{
329 // Comment parses as text.
Lee Thomasone4422302012-01-20 17:59:50 -0800330 return ParseText( p, &value, "-->" );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800331}
332
333
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800334// --------- XMLAttribute ---------- //
335char* XMLAttribute::ParseDeep( char* p )
336{
Lee Thomason22aead12012-01-23 13:29:35 -0800337 p = ParseText( p, &name, "=" );
338 if ( !p || !*p ) return 0;
339
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800340 char endTag[2] = { *p, 0 };
341 ++p;
Lee Thomasone4422302012-01-20 17:59:50 -0800342 p = ParseText( p, &value, endTag );
343 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800344 return p;
345}
346
347
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800348void XMLAttribute::Print( FILE* cfile )
349{
Lee Thomason22aead12012-01-23 13:29:35 -0800350 // fixme: sort out single vs. double quote
351 fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800352}
353
354
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800355// --------- XMLElement ---------- //
356XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800357 closing( false ),
358 rootAttribute( 0 ),
359 lastAttribute( 0 )
360{
361}
362
363
364XMLElement::~XMLElement()
365{
Lee Thomasond923c672012-01-23 08:44:25 -0800366 //printf( "~XMLElemen %x\n",this );
367
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800368 XMLAttribute* attribute = rootAttribute;
369 while( attribute ) {
370 XMLAttribute* next = attribute->next;
371 delete attribute;
372 attribute = next;
373 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800374}
375
376
Lee Thomason67d61312012-01-24 16:01:51 -0800377char* XMLElement::ParseAttributes( char* p, bool* closedElement )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800378{
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800379 const char* start = p;
Lee Thomason67d61312012-01-24 16:01:51 -0800380 *closedElement = false;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800381
382 // Read the attributes.
383 while( p ) {
384 p = SkipWhiteSpace( p );
385 if ( !p || !(*p) ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800386 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800387 return 0;
388 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800389
390 // attribute.
Lee Thomason22aead12012-01-23 13:29:35 -0800391 if ( IsAlpha( *p ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800392 XMLAttribute* attrib = new XMLAttribute( this );
393 p = attrib->ParseDeep( p );
394 if ( !p ) {
395 delete attrib;
Lee Thomasone4422302012-01-20 17:59:50 -0800396 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800397 return 0;
398 }
399 if ( rootAttribute ) {
400 TIXMLASSERT( lastAttribute );
401 lastAttribute->next = attrib;
402 lastAttribute = attrib;
403 }
404 else {
405 rootAttribute = lastAttribute = attrib;
406 }
407 }
Lee Thomasone4422302012-01-20 17:59:50 -0800408 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800409 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800410 if ( closing ) {
411 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
412 return 0;
413 }
Lee Thomason67d61312012-01-24 16:01:51 -0800414 *closedElement = true;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800415 return p+2; // done; sealed element.
416 }
Lee Thomasone4422302012-01-20 17:59:50 -0800417 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800418 else if ( *p == '>' ) {
419 ++p;
420 break;
421 }
Lee Thomasone4422302012-01-20 17:59:50 -0800422 else {
423 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
424 return 0;
425 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800426 }
Lee Thomason67d61312012-01-24 16:01:51 -0800427 return p;
428}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800429
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800430
Lee Thomason67d61312012-01-24 16:01:51 -0800431//
432// <ele></ele>
433// <ele>foo<b>bar</b></ele>
434//
435char* XMLElement::ParseDeep( char* p )
436{
437 // Read the element name.
438 p = SkipWhiteSpace( p );
439 if ( !p ) return 0;
440 const char* start = p;
441
442 // The closing element is the </element> form. It is
443 // parsed just like a regular element then deleted from
444 // the DOM.
445 if ( *p == '/' ) {
446 closing = true;
447 ++p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800448 }
Lee Thomason67d61312012-01-24 16:01:51 -0800449
450 p = ParseName( p, &name );
451 if ( name.Empty() ) return 0;
452
453 bool elementClosed=false;
454 p = ParseAttributes( p, &elementClosed );
455 if ( !p || !*p || elementClosed || closing )
456 return p;
457
458 p = XMLNode::ParseDeep( p );
459 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800460}
461
462
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800463void XMLElement::Print( FILE* cfile, int depth )
464{
Lee Thomason67d61312012-01-24 16:01:51 -0800465 if ( !parent || !parent->IsTextParent() ) {
466 PrintSpace( cfile, depth );
467 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800468 fprintf( cfile, "<%s", Name() );
469
470 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
471 fprintf( cfile, " " );
472 attrib->Print( cfile );
473 }
474
475 if ( firstChild ) {
Lee Thomason5492a1c2012-01-23 15:32:10 -0800476 fprintf( cfile, ">", Name() );
Lee Thomason67d61312012-01-24 16:01:51 -0800477 if ( !IsTextParent() ) {
478 fprintf( cfile, "\n" );
479 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800480
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800481 for( XMLNode* node=firstChild; node; node=node->next ) {
482 node->Print( cfile, depth+1 );
483 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800484
Lee Thomason67d61312012-01-24 16:01:51 -0800485 fprintf( cfile, "</%s>", Name() );
486 if ( !IsTextParent() ) {
487 fprintf( cfile, "\n" );
488 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800489 }
490 else {
Lee Thomason67d61312012-01-24 16:01:51 -0800491 fprintf( cfile, "/>" );
492 if ( !IsTextParent() ) {
493 fprintf( cfile, "\n" );
494 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800495 }
496}
497
498
Lee Thomason3f57d272012-01-11 15:30:03 -0800499// --------- XMLDocument ----------- //
Lee Thomason67d61312012-01-24 16:01:51 -0800500XMLDocument::XMLDocument() :
501 XMLNode( this ),
U-Lama\Lee560bd472011-12-28 19:42:49 -0800502 charBuffer( 0 )
503{
504}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800505
506
Lee Thomason3f57d272012-01-11 15:30:03 -0800507XMLDocument::~XMLDocument()
508{
Lee Thomason3f57d272012-01-11 15:30:03 -0800509}
510
511
512
513bool XMLDocument::Parse( const char* p )
514{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800515 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800516 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800517
Lee Thomason67d61312012-01-24 16:01:51 -0800518 char* q = ParseDeep( charBuffer->mem );
519 return true;
Lee Thomason3f57d272012-01-11 15:30:03 -0800520}
521
522
Lee Thomasonce0763e2012-01-11 15:43:54 -0800523void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800524{
Lee Thomason67d61312012-01-24 16:01:51 -0800525 for( XMLNode* node = firstChild; node; node=node->next ) {
Lee Thomasonce0763e2012-01-11 15:43:54 -0800526 node->Print( fp, depth );
527 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800528}
529
530
Lee Thomason67d61312012-01-24 16:01:51 -0800531void XMLDocument::SetError( int error, const char* str1, const char* str2 )
532{
533 printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 );
534}
535