blob: fe1665b4b20c2ca793d9f09c6960902a403a3474 [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{
191 XMLNode* node=firstChild;
192 while( node ) {
193 XMLNode* temp = node->next;
194 delete node;
195 node = temp;
196 }
197 if ( prev ) {
198 prev->next = next;
199 }
200 if ( next ) {
201 next->prev = prev;
202 }
203}
204
205
206XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
207{
208 if ( lastChild ) {
209 TIXMLASSERT( firstChild );
210 TIXMLASSERT( lastChild->next == 0 );
211 lastChild->next = addThis;
212 addThis->prev = lastChild;
213 lastChild = addThis;
214
215 addThis->parent = this;
216 addThis->next = 0;
217 }
218 else {
219 TIXMLASSERT( firstChild == 0 );
220 firstChild = lastChild = addThis;
221
222 addThis->parent = this;
223 addThis->prev = 0;
224 addThis->next = 0;
225 }
226 return addThis;
227}
228
229
230void XMLNode::Print( FILE* fp, int depth )
231{
232 for( XMLNode* node = firstChild; node; node=node->next ) {
233 node->Print( fp, depth );
234 }
235}
236
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800237
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800238void XMLNode::PrintSpace( FILE* fp, int depth )
239{
240 for( int i=0; i<depth; ++i ) {
241 fprintf( fp, " " );
242 }
243}
244
245
246
247
Lee Thomason3f57d272012-01-11 15:30:03 -0800248// --------- XMLComment ---------- //
249
Lee Thomasone4422302012-01-20 17:59:50 -0800250XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800251{
252}
253
254
Lee Thomasonce0763e2012-01-11 15:43:54 -0800255XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800256{
Lee Thomason3f57d272012-01-11 15:30:03 -0800257}
258
259
Lee Thomasonce0763e2012-01-11 15:43:54 -0800260void XMLComment::Print( FILE* fp, int depth )
261{
262 XMLNode::Print( fp, depth );
Lee Thomasonfde6a752012-01-14 18:08:12 -0800263 fprintf( fp, "<!--%s-->\n", value );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800264}
265
266
267char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800268{
269 // Comment parses as text.
Lee Thomasone4422302012-01-20 17:59:50 -0800270 return ParseText( p, &value, "-->" );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800271}
272
273
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800274// --------- XMLAttribute ---------- //
275char* XMLAttribute::ParseDeep( char* p )
276{
277 char endTag[2] = { *p, 0 };
278 ++p;
Lee Thomasone4422302012-01-20 17:59:50 -0800279 p = ParseText( p, &value, endTag );
280 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800281 return p;
282}
283
284
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800285void XMLAttribute::Print( FILE* cfile )
286{
287 fprintf( cfile, "\"%s\"", value );
288}
289
290
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800291// --------- XMLElement ---------- //
292XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800293 closing( false ),
294 rootAttribute( 0 ),
295 lastAttribute( 0 )
296{
297}
298
299
300XMLElement::~XMLElement()
301{
302 XMLAttribute* attribute = rootAttribute;
303 while( attribute ) {
304 XMLAttribute* next = attribute->next;
305 delete attribute;
306 attribute = next;
307 }
308
309 XMLNode* child = firstChild;
310 while( child ) {
311 XMLNode* next = child->next;
312 delete child;
313 child = next;
314 }
315}
316
317
318char* XMLElement::ParseDeep( char* p )
319{
320 // Read the element name.
321 p = SkipWhiteSpace( p );
322 if ( !p ) return 0;
323 const char* start = p;
324
325 // The closing element is the </element> form. It is
326 // parsed just like a regular element then deleted from
327 // the DOM.
328 if ( *p == '/' ) {
329 closing = true;
330 ++p;
331 }
332
Lee Thomasone4422302012-01-20 17:59:50 -0800333 p = ParseName( p, &name );
334 if ( name.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800335
336 // Read the attributes.
337 while( p ) {
338 p = SkipWhiteSpace( p );
339 if ( !p || !(*p) ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800340 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800341 return 0;
342 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800343
344 // attribute.
Lee Thomasone4422302012-01-20 17:59:50 -0800345 if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800346 XMLAttribute* attrib = new XMLAttribute( this );
347 p = attrib->ParseDeep( p );
348 if ( !p ) {
349 delete attrib;
Lee Thomasone4422302012-01-20 17:59:50 -0800350 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800351 return 0;
352 }
353 if ( rootAttribute ) {
354 TIXMLASSERT( lastAttribute );
355 lastAttribute->next = attrib;
356 lastAttribute = attrib;
357 }
358 else {
359 rootAttribute = lastAttribute = attrib;
360 }
361 }
Lee Thomasone4422302012-01-20 17:59:50 -0800362 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800363 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800364 if ( closing ) {
365 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
366 return 0;
367 }
368 return p+2; // done; sealed element.
369 }
Lee Thomasone4422302012-01-20 17:59:50 -0800370 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800371 else if ( *p == '>' ) {
372 ++p;
373 break;
374 }
Lee Thomasone4422302012-01-20 17:59:50 -0800375 else {
376 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
377 return 0;
378 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800379 }
380
381 while( p && *p ) {
382 XMLNode* node = 0;
383 p = Identify( document, p, &node );
384 if ( p && node ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800385 p = node->ParseDeep( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800386
387 XMLElement* element = node->ToElement();
388 if ( element && element->Closing() ) {
389 if ( StringEqual( element->Name(), this->Name() ) ) {
390 // All good, this is closing tag.
391 delete node;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800392 }
393 else {
394 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
395 delete node;
Lee Thomasone4422302012-01-20 17:59:50 -0800396 p = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800397 }
398 return p;
399 }
400 else {
401 this->InsertEndChild( node );
402 }
403 }
404 }
405 return 0;
406}
407
408
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800409void XMLElement::Print( FILE* cfile, int depth )
410{
411 PrintSpace( cfile, depth );
412 fprintf( cfile, "<%s", Name() );
413
414 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
415 fprintf( cfile, " " );
416 attrib->Print( cfile );
417 }
418
419 if ( firstChild ) {
420 fprintf( cfile, ">/n" );
421 for( XMLNode* node=firstChild; node; node=node->next ) {
422 node->Print( cfile, depth+1 );
423 }
424 fprintf( cfile, "</%s>", Name() );
425 }
426 else {
427 fprintf( cfile, "/>\n" );
428 }
429}
430
431
Lee Thomason3f57d272012-01-11 15:30:03 -0800432// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800433XMLDocument::XMLDocument() :
434 charBuffer( 0 )
435{
Lee Thomason85403d82012-01-11 15:55:05 -0800436 root = new XMLNode( this );
U-Lama\Lee560bd472011-12-28 19:42:49 -0800437}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800438
439
Lee Thomason3f57d272012-01-11 15:30:03 -0800440XMLDocument::~XMLDocument()
441{
442 delete root;
443 delete charBuffer;
444}
445
446
447
448bool XMLDocument::Parse( const char* p )
449{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800450 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800451 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800452
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800453 char* q = Identify( this, charBuffer->mem, &node );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800454 if ( node ) {
455 root->InsertEndChild( node );
456 node->ParseDeep( q );
457 return true;
458 }
459 return false;
Lee Thomason3f57d272012-01-11 15:30:03 -0800460}
461
462
Lee Thomasonce0763e2012-01-11 15:43:54 -0800463void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800464{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800465 for( XMLNode* node = root->firstChild; node; node=node->next ) {
466 node->Print( fp, depth );
467 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800468}
469
470