blob: 07fa458c29481e36f066b260ccd01d6a176af250 [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;
135
136 p = XMLNode::SkipWhiteSpace( p );
137 if( !p || !*p || *p != '<' )
138 {
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 Thomason8a5dfee2012-01-18 17:43:40 -0800169 else {
170 TIXMLASSERT( 0 );
171 }
172
173 *node = returnNode;
174 return p;
175}
176
177
178// --------- XMLNode ----------- //
179
180XMLNode::XMLNode( XMLDocument* doc ) :
181 document( doc ),
182 parent( 0 ),
183 firstChild( 0 ), lastChild( 0 ),
184 prev( 0 ), next( 0 )
185{
186
187}
188
189
190XMLNode::~XMLNode()
191{
Lee Thomasond923c672012-01-23 08:44:25 -0800192 //printf( "~XMLNode %x\n", this );
193 while( firstChild ) {
194 XMLNode* node = firstChild;
195 Unlink( node );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800196 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800197 }
Lee Thomasond923c672012-01-23 08:44:25 -0800198}
199
200
201void XMLNode::Unlink( XMLNode* child )
202{
203 TIXMLASSERT( child->parent == this );
204 if ( child == firstChild )
205 firstChild = firstChild->next;
206 if ( child == lastChild )
207 lastChild = lastChild->prev;
208
209 if ( child->prev ) {
210 child->prev->next = child->next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800211 }
Lee Thomasond923c672012-01-23 08:44:25 -0800212 if ( child->next ) {
213 child->next->prev = child->prev;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800214 }
Lee Thomasond923c672012-01-23 08:44:25 -0800215 child->parent = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800216}
217
218
219XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
220{
221 if ( lastChild ) {
222 TIXMLASSERT( firstChild );
223 TIXMLASSERT( lastChild->next == 0 );
224 lastChild->next = addThis;
225 addThis->prev = lastChild;
226 lastChild = addThis;
227
228 addThis->parent = this;
229 addThis->next = 0;
230 }
231 else {
232 TIXMLASSERT( firstChild == 0 );
233 firstChild = lastChild = addThis;
234
235 addThis->parent = this;
236 addThis->prev = 0;
237 addThis->next = 0;
238 }
239 return addThis;
240}
241
242
243void XMLNode::Print( FILE* fp, int depth )
244{
245 for( XMLNode* node = firstChild; node; node=node->next ) {
246 node->Print( fp, depth );
247 }
248}
249
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800250
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800251void XMLNode::PrintSpace( FILE* fp, int depth )
252{
253 for( int i=0; i<depth; ++i ) {
254 fprintf( fp, " " );
255 }
256}
257
258
259
260
Lee Thomason3f57d272012-01-11 15:30:03 -0800261// --------- XMLComment ---------- //
262
Lee Thomasone4422302012-01-20 17:59:50 -0800263XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800264{
265}
266
267
Lee Thomasonce0763e2012-01-11 15:43:54 -0800268XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800269{
Lee Thomasond923c672012-01-23 08:44:25 -0800270 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800271}
272
273
Lee Thomasonce0763e2012-01-11 15:43:54 -0800274void XMLComment::Print( FILE* fp, int depth )
275{
276 XMLNode::Print( fp, depth );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800277 fprintf( fp, "<!--%s-->\n", value.GetStr() );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800278}
279
280
281char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800282{
283 // Comment parses as text.
Lee Thomasone4422302012-01-20 17:59:50 -0800284 return ParseText( p, &value, "-->" );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800285}
286
287
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800288// --------- XMLAttribute ---------- //
289char* XMLAttribute::ParseDeep( char* p )
290{
291 char endTag[2] = { *p, 0 };
292 ++p;
Lee Thomasone4422302012-01-20 17:59:50 -0800293 p = ParseText( p, &value, endTag );
294 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800295 return p;
296}
297
298
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800299void XMLAttribute::Print( FILE* cfile )
300{
301 fprintf( cfile, "\"%s\"", value );
302}
303
304
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800305// --------- XMLElement ---------- //
306XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800307 closing( false ),
308 rootAttribute( 0 ),
309 lastAttribute( 0 )
310{
311}
312
313
314XMLElement::~XMLElement()
315{
Lee Thomasond923c672012-01-23 08:44:25 -0800316 //printf( "~XMLElemen %x\n",this );
317
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800318 XMLAttribute* attribute = rootAttribute;
319 while( attribute ) {
320 XMLAttribute* next = attribute->next;
321 delete attribute;
322 attribute = next;
323 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800324}
325
326
327char* XMLElement::ParseDeep( char* p )
328{
329 // Read the element name.
330 p = SkipWhiteSpace( p );
331 if ( !p ) return 0;
332 const char* start = p;
333
334 // The closing element is the </element> form. It is
335 // parsed just like a regular element then deleted from
336 // the DOM.
337 if ( *p == '/' ) {
338 closing = true;
339 ++p;
340 }
341
Lee Thomasone4422302012-01-20 17:59:50 -0800342 p = ParseName( p, &name );
343 if ( name.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800344
345 // Read the attributes.
346 while( p ) {
347 p = SkipWhiteSpace( p );
348 if ( !p || !(*p) ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800349 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800350 return 0;
351 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800352
353 // attribute.
Lee Thomasone4422302012-01-20 17:59:50 -0800354 if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800355 XMLAttribute* attrib = new XMLAttribute( this );
356 p = attrib->ParseDeep( p );
357 if ( !p ) {
358 delete attrib;
Lee Thomasone4422302012-01-20 17:59:50 -0800359 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800360 return 0;
361 }
362 if ( rootAttribute ) {
363 TIXMLASSERT( lastAttribute );
364 lastAttribute->next = attrib;
365 lastAttribute = attrib;
366 }
367 else {
368 rootAttribute = lastAttribute = attrib;
369 }
370 }
Lee Thomasone4422302012-01-20 17:59:50 -0800371 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800372 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800373 if ( closing ) {
374 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
375 return 0;
376 }
377 return p+2; // done; sealed element.
378 }
Lee Thomasone4422302012-01-20 17:59:50 -0800379 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800380 else if ( *p == '>' ) {
381 ++p;
382 break;
383 }
Lee Thomasone4422302012-01-20 17:59:50 -0800384 else {
385 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
386 return 0;
387 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800388 }
389
390 while( p && *p ) {
391 XMLNode* node = 0;
392 p = Identify( document, p, &node );
393 if ( p && node ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800394 p = node->ParseDeep( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800395
396 XMLElement* element = node->ToElement();
397 if ( element && element->Closing() ) {
398 if ( StringEqual( element->Name(), this->Name() ) ) {
399 // All good, this is closing tag.
400 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800401 }
402 else {
403 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
404 delete node;
Lee Thomasone4422302012-01-20 17:59:50 -0800405 p = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800406 }
407 return p;
408 }
409 else {
410 this->InsertEndChild( node );
411 }
412 }
413 }
414 return 0;
415}
416
417
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800418void XMLElement::Print( FILE* cfile, int depth )
419{
420 PrintSpace( cfile, depth );
421 fprintf( cfile, "<%s", Name() );
422
423 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
424 fprintf( cfile, " " );
425 attrib->Print( cfile );
426 }
427
428 if ( firstChild ) {
Lee Thomasonec975ce2012-01-23 11:42:06 -0800429 fprintf( cfile, ">\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800430 for( XMLNode* node=firstChild; node; node=node->next ) {
431 node->Print( cfile, depth+1 );
432 }
Lee Thomasonec975ce2012-01-23 11:42:06 -0800433 fprintf( cfile, "</%s>\n", Name() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800434 }
435 else {
436 fprintf( cfile, "/>\n" );
437 }
438}
439
440
Lee Thomason3f57d272012-01-11 15:30:03 -0800441// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800442XMLDocument::XMLDocument() :
443 charBuffer( 0 )
444{
Lee Thomason85403d82012-01-11 15:55:05 -0800445 root = new XMLNode( this );
U-Lama\Lee560bd472011-12-28 19:42:49 -0800446}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800447
448
Lee Thomason3f57d272012-01-11 15:30:03 -0800449XMLDocument::~XMLDocument()
450{
451 delete root;
452 delete charBuffer;
453}
454
455
456
457bool XMLDocument::Parse( const char* p )
458{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800459 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800460 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800461
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800462 char* q = Identify( this, charBuffer->mem, &node );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800463 while ( node ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800464 root->InsertEndChild( node );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800465 q = node->ParseDeep( q );
466 node = 0;
467 if ( q && *q ) {
468 q = Identify( this, q, &node );
469 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800470 }
471 return false;
Lee Thomason3f57d272012-01-11 15:30:03 -0800472}
473
474
Lee Thomasonce0763e2012-01-11 15:43:54 -0800475void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800476{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800477 for( XMLNode* node = root->firstChild; node; node=node->next ) {
478 node->Print( fp, depth );
479 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800480}
481
482