blob: 5e1b1d808e9edbedce2ddb8d0c6fa568c0908903 [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 Thomasonfde6a752012-01-14 18:08:12 -080010static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
11static const char LF = LINE_FEED;
12static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
13static const char CR = CARRIAGE_RETURN;
14
15
Lee Thomason3f57d272012-01-11 15:30:03 -080016// --------- CharBuffer ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -080017/*static*/ CharBuffer* CharBuffer::Construct( const char* in )
18{
19 size_t len = strlen( in );
20 size_t size = len + sizeof( CharBuffer );
21 CharBuffer* cb = (CharBuffer*) malloc( size );
22 cb->length = len;
23 strcpy( cb->mem, in );
24 return cb;
25}
26
27
28/*static*/ void CharBuffer::Free( CharBuffer* cb )
29{
30 free( cb );
31}
32
33
Lee Thomason8a5dfee2012-01-18 17:43:40 -080034// --------- XMLBase ----------- //
35const char* XMLBase::ParseText( char* p, const char* endTag, char** next )
Lee Thomason3f57d272012-01-11 15:30:03 -080036{
37 TIXMLASSERT( endTag && *endTag );
38
Lee Thomasonfde6a752012-01-14 18:08:12 -080039 char* start = p;
40 char* q = p; // q (target) <= p (src) in same buffer.
41 char endChar = *endTag;
42 int length = strlen( endTag );
43 char* nextTag = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -080044 *next = 0;
Lee Thomason3f57d272012-01-11 15:30:03 -080045
Lee Thomasonfde6a752012-01-14 18:08:12 -080046 // Inner loop of text parsing.
Lee Thomason3f57d272012-01-11 15:30:03 -080047 while ( *p ) {
Lee Thomasonfde6a752012-01-14 18:08:12 -080048 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
49 *q = 0;
50 nextTag = p + length;
51 break;
Lee Thomason3f57d272012-01-11 15:30:03 -080052 }
Lee Thomasonfde6a752012-01-14 18:08:12 -080053 else if ( *p == CR ) {
54 // CR-LF pair becomes LF
55 // CR alone becomes LF
56 // LF-CR becomes LF
57 if ( *(p+1) == LF ) {
58 p += 2;
59 }
60 else {
61 ++p;
62 }
63 *q = LF;
64 }
65 else if ( *p == LF ) {
66 if ( *(p+1) == CR ) {
67 p += 2;
68 }
69 else {
70 ++p;
71 }
72 *q = LF;
73 }
74 else {
75 *q = *p;
76 ++p;
77 }
78 ++q;
Lee Thomason3f57d272012-01-11 15:30:03 -080079 }
Lee Thomasonfde6a752012-01-14 18:08:12 -080080
81 // Error? If we don't have a text tag, something went wrong. (Although
82 // what the nextTag points at may be null.)
83 if ( nextTag == 0 ) {
84 return 0;
85 }
86 *next = nextTag;
87 return start;
Lee Thomason3f57d272012-01-11 15:30:03 -080088}
89
90
Lee Thomason8a5dfee2012-01-18 17:43:40 -080091const char* XMLBase::ParseName( char* p, char** next )
92{
93 char* start = p;
94 char* nextTag = 0;
95 *next = 0;
96
97 start = p;
98 if ( !start || !(*start) ) {
99 return 0;
100 }
101
102 if ( !IsAlpha( *p ) ) {
103 return 0;
104 }
105
106 while( *p && (
107 IsAlphaNum( (unsigned char) *p )
108 || *p == '_'
109 || *p == '-'
110 || *p == '.'
111 || *p == ':' ))
112 {
113 ++p;
114 }
115 *p = 0;
116
117 if ( p > start ) {
118 *next = p;
119 return start;
120 }
121 return p+1;
122}
123
124
125char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
126{
127 XMLNode* returnNode = 0;
128
129 p = XMLNode::SkipWhiteSpace( p );
130 if( !p || !*p || *p != '<' )
131 {
132 return 0;
133 }
134
135 // What is this thing?
136 // - Elements start with a letter or underscore, but xml is reserved.
137 // - Comments: <!--
138 // - Decleration: <?xml
139 // - Everthing else is unknown to tinyxml.
140 //
141
142 static const char* xmlHeader = { "<?xml" };
143 static const char* commentHeader = { "<!--" };
144 static const char* dtdHeader = { "<!" };
145 static const char* cdataHeader = { "<![CDATA[" };
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800146 static const char* elementHeader = { "<" }; // and a header for everything else; check last.
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800147
148 static const int xmlHeaderLen = 5;
149 static const int commentHeaderLen = 4;
150 static const int dtdHeaderLen = 2;
151 static const int cdataHeaderLen = 9;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800152 static const int elementHeaderLen = 1;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800153
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800154 if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800155 returnNode = new XMLComment( document );
156 p += commentHeaderLen;
157 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800158 else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
159 returnNode = new XMLElement( document );
160 p += elementHeaderLen;
161 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800162 else {
163 TIXMLASSERT( 0 );
164 }
165
166 *node = returnNode;
167 return p;
168}
169
170
171// --------- XMLNode ----------- //
172
173XMLNode::XMLNode( XMLDocument* doc ) :
174 document( doc ),
175 parent( 0 ),
176 firstChild( 0 ), lastChild( 0 ),
177 prev( 0 ), next( 0 )
178{
179
180}
181
182
183XMLNode::~XMLNode()
184{
185 XMLNode* node=firstChild;
186 while( node ) {
187 XMLNode* temp = node->next;
188 delete node;
189 node = temp;
190 }
191 if ( prev ) {
192 prev->next = next;
193 }
194 if ( next ) {
195 next->prev = prev;
196 }
197}
198
199
200XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
201{
202 if ( lastChild ) {
203 TIXMLASSERT( firstChild );
204 TIXMLASSERT( lastChild->next == 0 );
205 lastChild->next = addThis;
206 addThis->prev = lastChild;
207 lastChild = addThis;
208
209 addThis->parent = this;
210 addThis->next = 0;
211 }
212 else {
213 TIXMLASSERT( firstChild == 0 );
214 firstChild = lastChild = addThis;
215
216 addThis->parent = this;
217 addThis->prev = 0;
218 addThis->next = 0;
219 }
220 return addThis;
221}
222
223
224void XMLNode::Print( FILE* fp, int depth )
225{
226 for( XMLNode* node = firstChild; node; node=node->next ) {
227 node->Print( fp, depth );
228 }
229}
230
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800231
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800232void XMLNode::PrintSpace( FILE* fp, int depth )
233{
234 for( int i=0; i<depth; ++i ) {
235 fprintf( fp, " " );
236 }
237}
238
239
240
241
Lee Thomason3f57d272012-01-11 15:30:03 -0800242// --------- XMLComment ---------- //
243
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800244XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ), value( 0 )
Lee Thomason3f57d272012-01-11 15:30:03 -0800245{
246}
247
248
Lee Thomasonce0763e2012-01-11 15:43:54 -0800249XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800250{
Lee Thomason3f57d272012-01-11 15:30:03 -0800251}
252
253
Lee Thomasonce0763e2012-01-11 15:43:54 -0800254void XMLComment::Print( FILE* fp, int depth )
255{
256 XMLNode::Print( fp, depth );
Lee Thomasonfde6a752012-01-14 18:08:12 -0800257 fprintf( fp, "<!--%s-->\n", value );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800258}
259
260
261char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800262{
263 // Comment parses as text.
264 value = ParseText( p, "-->", &p );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800265 return p;
266}
267
268
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800269// --------- XMLAttribute ---------- //
270char* XMLAttribute::ParseDeep( char* p )
271{
272 char endTag[2] = { *p, 0 };
273 ++p;
274 value = ParseText( p, endTag, &p );
275 if ( !value ) return 0;
276 return p;
277}
278
279
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800280void XMLAttribute::Print( FILE* cfile )
281{
282 fprintf( cfile, "\"%s\"", value );
283}
284
285
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800286// --------- XMLElement ---------- //
287XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
288 name( 0 ),
289 closing( false ),
290 rootAttribute( 0 ),
291 lastAttribute( 0 )
292{
293}
294
295
296XMLElement::~XMLElement()
297{
298 XMLAttribute* attribute = rootAttribute;
299 while( attribute ) {
300 XMLAttribute* next = attribute->next;
301 delete attribute;
302 attribute = next;
303 }
304
305 XMLNode* child = firstChild;
306 while( child ) {
307 XMLNode* next = child->next;
308 delete child;
309 child = next;
310 }
311}
312
313
314char* XMLElement::ParseDeep( char* p )
315{
316 // Read the element name.
317 p = SkipWhiteSpace( p );
318 if ( !p ) return 0;
319 const char* start = p;
320
321 // The closing element is the </element> form. It is
322 // parsed just like a regular element then deleted from
323 // the DOM.
324 if ( *p == '/' ) {
325 closing = true;
326 ++p;
327 }
328
329 name = ParseName( p, &p );
330 if ( !name ) return 0;
331
332 // Read the attributes.
333 while( p ) {
334 p = SkipWhiteSpace( p );
335 if ( !p || !(*p) ) {
336 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name );
337 return 0;
338 }
339 const char* saveP = p;
340
341 // attribute.
342 if ( *p == '\'' || *p == '\"' ) {
343 XMLAttribute* attrib = new XMLAttribute( this );
344 p = attrib->ParseDeep( p );
345 if ( !p ) {
346 delete attrib;
347 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, saveP );
348 return 0;
349 }
350 if ( rootAttribute ) {
351 TIXMLASSERT( lastAttribute );
352 lastAttribute->next = attrib;
353 lastAttribute = attrib;
354 }
355 else {
356 rootAttribute = lastAttribute = attrib;
357 }
358 }
359 else if ( *p == '/' && *(p+1) == '>' ) {
360 // end tag.
361 if ( closing ) {
362 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
363 return 0;
364 }
365 return p+2; // done; sealed element.
366 }
367 else if ( *p == '>' ) {
368 ++p;
369 break;
370 }
371 }
372
373 while( p && *p ) {
374 XMLNode* node = 0;
375 p = Identify( document, p, &node );
376 if ( p && node ) {
377 node->ParseDeep( p );
378
379 XMLElement* element = node->ToElement();
380 if ( element && element->Closing() ) {
381 if ( StringEqual( element->Name(), this->Name() ) ) {
382 // All good, this is closing tag.
383 delete node;
384 p = 0;
385 }
386 else {
387 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
388 delete node;
389 }
390 return p;
391 }
392 else {
393 this->InsertEndChild( node );
394 }
395 }
396 }
397 return 0;
398}
399
400
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800401void XMLElement::Print( FILE* cfile, int depth )
402{
403 PrintSpace( cfile, depth );
404 fprintf( cfile, "<%s", Name() );
405
406 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
407 fprintf( cfile, " " );
408 attrib->Print( cfile );
409 }
410
411 if ( firstChild ) {
412 fprintf( cfile, ">/n" );
413 for( XMLNode* node=firstChild; node; node=node->next ) {
414 node->Print( cfile, depth+1 );
415 }
416 fprintf( cfile, "</%s>", Name() );
417 }
418 else {
419 fprintf( cfile, "/>\n" );
420 }
421}
422
423
Lee Thomason3f57d272012-01-11 15:30:03 -0800424// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800425XMLDocument::XMLDocument() :
426 charBuffer( 0 )
427{
Lee Thomason85403d82012-01-11 15:55:05 -0800428 root = new XMLNode( this );
U-Lama\Lee560bd472011-12-28 19:42:49 -0800429}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800430
431
Lee Thomason3f57d272012-01-11 15:30:03 -0800432XMLDocument::~XMLDocument()
433{
434 delete root;
435 delete charBuffer;
436}
437
438
439
440bool XMLDocument::Parse( const char* p )
441{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800442 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800443 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800444
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800445 char* q = Identify( this, charBuffer->mem, &node );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800446 if ( node ) {
447 root->InsertEndChild( node );
448 node->ParseDeep( q );
449 return true;
450 }
451 return false;
Lee Thomason3f57d272012-01-11 15:30:03 -0800452}
453
454
Lee Thomasonce0763e2012-01-11 15:43:54 -0800455void XMLDocument::Print( FILE* fp, int depth )
Lee Thomason3f57d272012-01-11 15:30:03 -0800456{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800457 for( XMLNode* node = root->firstChild; node; node=node->next ) {
458 node->Print( fp, depth );
459 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800460}
461
462