blob: 367f05caa4d76f775ed9669b39829b4a8a9cc0bd [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{
Lee Thomason22aead12012-01-23 13:29:35 -0800291 p = ParseText( p, &name, "=" );
292 if ( !p || !*p ) return 0;
293
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800294 char endTag[2] = { *p, 0 };
295 ++p;
Lee Thomasone4422302012-01-20 17:59:50 -0800296 p = ParseText( p, &value, endTag );
297 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800298 return p;
299}
300
301
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800302void XMLAttribute::Print( FILE* cfile )
303{
Lee Thomason22aead12012-01-23 13:29:35 -0800304 // fixme: sort out single vs. double quote
305 fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800306}
307
308
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800309// --------- XMLElement ---------- //
310XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800311 closing( false ),
312 rootAttribute( 0 ),
313 lastAttribute( 0 )
314{
315}
316
317
318XMLElement::~XMLElement()
319{
Lee Thomasond923c672012-01-23 08:44:25 -0800320 //printf( "~XMLElemen %x\n",this );
321
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800322 XMLAttribute* attribute = rootAttribute;
323 while( attribute ) {
324 XMLAttribute* next = attribute->next;
325 delete attribute;
326 attribute = next;
327 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800328}
329
330
331char* XMLElement::ParseDeep( char* p )
332{
333 // Read the element name.
334 p = SkipWhiteSpace( p );
335 if ( !p ) return 0;
336 const char* start = p;
337
338 // The closing element is the </element> form. It is
339 // parsed just like a regular element then deleted from
340 // the DOM.
341 if ( *p == '/' ) {
342 closing = true;
343 ++p;
344 }
345
Lee Thomasone4422302012-01-20 17:59:50 -0800346 p = ParseName( p, &name );
347 if ( name.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800348
349 // Read the attributes.
350 while( p ) {
351 p = SkipWhiteSpace( p );
352 if ( !p || !(*p) ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800353 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800354 return 0;
355 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800356
357 // attribute.
Lee Thomason22aead12012-01-23 13:29:35 -0800358 if ( IsAlpha( *p ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800359 XMLAttribute* attrib = new XMLAttribute( this );
360 p = attrib->ParseDeep( p );
361 if ( !p ) {
362 delete attrib;
Lee Thomasone4422302012-01-20 17:59:50 -0800363 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800364 return 0;
365 }
366 if ( rootAttribute ) {
367 TIXMLASSERT( lastAttribute );
368 lastAttribute->next = attrib;
369 lastAttribute = attrib;
370 }
371 else {
372 rootAttribute = lastAttribute = attrib;
373 }
374 }
Lee Thomasone4422302012-01-20 17:59:50 -0800375 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800376 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800377 if ( closing ) {
378 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
379 return 0;
380 }
381 return p+2; // done; sealed element.
382 }
Lee Thomasone4422302012-01-20 17:59:50 -0800383 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800384 else if ( *p == '>' ) {
385 ++p;
386 break;
387 }
Lee Thomasone4422302012-01-20 17:59:50 -0800388 else {
389 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
390 return 0;
391 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800392 }
393
394 while( p && *p ) {
395 XMLNode* node = 0;
396 p = Identify( document, p, &node );
397 if ( p && node ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800398 p = node->ParseDeep( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800399
400 XMLElement* element = node->ToElement();
401 if ( element && element->Closing() ) {
402 if ( StringEqual( element->Name(), this->Name() ) ) {
403 // All good, this is closing tag.
404 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800405 }
406 else {
407 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
408 delete node;
Lee Thomasone4422302012-01-20 17:59:50 -0800409 p = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800410 }
411 return p;
412 }
413 else {
414 this->InsertEndChild( node );
415 }
416 }
417 }
418 return 0;
419}
420
421
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800422void XMLElement::Print( FILE* cfile, int depth )
423{
424 PrintSpace( cfile, depth );
425 fprintf( cfile, "<%s", Name() );
426
427 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
428 fprintf( cfile, " " );
429 attrib->Print( cfile );
430 }
431
432 if ( firstChild ) {
Lee Thomasonec975ce2012-01-23 11:42:06 -0800433 fprintf( cfile, ">\n" );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800434 for( XMLNode* node=firstChild; node; node=node->next ) {
435 node->Print( cfile, depth+1 );
436 }
Lee Thomasonec975ce2012-01-23 11:42:06 -0800437 fprintf( cfile, "</%s>\n", Name() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800438 }
439 else {
440 fprintf( cfile, "/>\n" );
441 }
442}
443
444
Lee Thomason3f57d272012-01-11 15:30:03 -0800445// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800446XMLDocument::XMLDocument() :
447 charBuffer( 0 )
448{
Lee Thomason85403d82012-01-11 15:55:05 -0800449 root = new XMLNode( this );
U-Lama\Lee560bd472011-12-28 19:42:49 -0800450}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800451
452
Lee Thomason3f57d272012-01-11 15:30:03 -0800453XMLDocument::~XMLDocument()
454{
455 delete root;
456 delete charBuffer;
457}
458
459
460
461bool XMLDocument::Parse( const char* p )
462{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800463 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800464 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800465
Lee Thomason22aead12012-01-23 13:29:35 -0800466 // fixme: clean up
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800467 char* q = Identify( this, charBuffer->mem, &node );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800468 while ( node ) {
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800469 root->InsertEndChild( node );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800470 q = node->ParseDeep( q );
471 node = 0;
472 if ( q && *q ) {
473 q = Identify( this, q, &node );
474 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800475 }
476 return false;
Lee Thomason3f57d272012-01-11 15:30:03 -0800477}
478
479
Lee Thomasonce0763e2012-01-11 15:43:54 -0800480void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800481{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800482 for( XMLNode* node = root->firstChild; node; node=node->next ) {
483 node->Print( fp, depth );
484 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800485}
486
487