blob: 69a62897e1230eb6e4895ceada3ce81090d816db [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
Lee Thomason5cae8972012-01-24 18:03:07 -0800253void XMLNode::Print( XMLStreamer* streamer )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800254{
255 for( XMLNode* node = firstChild; node; node=node->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800256 node->Print( streamer );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800257 }
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
Lee Thomason5cae8972012-01-24 18:03:07 -0800279/*
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}
Lee Thomason5cae8972012-01-24 18:03:07 -0800286*/
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800287
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
Lee Thomason5cae8972012-01-24 18:03:07 -0800300void XMLText::Print( XMLStreamer* streamer )
Lee Thomason5492a1c2012-01-23 15:32:10 -0800301{
Lee Thomason67d61312012-01-24 16:01:51 -0800302 const char* v = value.GetStr();
Lee Thomason5cae8972012-01-24 18:03:07 -0800303 streamer->PushText( 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 Thomason5cae8972012-01-24 18:03:07 -0800320void XMLComment::Print( XMLStreamer* streamer )
Lee Thomasonce0763e2012-01-11 15:43:54 -0800321{
Lee Thomason5cae8972012-01-24 18:03:07 -0800322// XMLNode::Print( fp, depth );
323// fprintf( fp, "<!--%s-->\n", value.GetStr() );
324 streamer->PushComment( value.GetStr() );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800325}
326
327
328char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800329{
330 // Comment parses as text.
Lee Thomasone4422302012-01-20 17:59:50 -0800331 return ParseText( p, &value, "-->" );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800332}
333
334
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800335// --------- XMLAttribute ---------- //
336char* XMLAttribute::ParseDeep( char* p )
337{
Lee Thomason22aead12012-01-23 13:29:35 -0800338 p = ParseText( p, &name, "=" );
339 if ( !p || !*p ) return 0;
340
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800341 char endTag[2] = { *p, 0 };
342 ++p;
Lee Thomasone4422302012-01-20 17:59:50 -0800343 p = ParseText( p, &value, endTag );
344 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800345 return p;
346}
347
348
Lee Thomason5cae8972012-01-24 18:03:07 -0800349void XMLAttribute::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800350{
Lee Thomason22aead12012-01-23 13:29:35 -0800351 // fixme: sort out single vs. double quote
Lee Thomason5cae8972012-01-24 18:03:07 -0800352 //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
353 streamer->PushAttribute( name.GetStr(), value.GetStr() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800354}
355
356
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800357// --------- XMLElement ---------- //
358XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800359 closing( false ),
360 rootAttribute( 0 ),
361 lastAttribute( 0 )
362{
363}
364
365
366XMLElement::~XMLElement()
367{
Lee Thomasond923c672012-01-23 08:44:25 -0800368 //printf( "~XMLElemen %x\n",this );
369
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800370 XMLAttribute* attribute = rootAttribute;
371 while( attribute ) {
372 XMLAttribute* next = attribute->next;
373 delete attribute;
374 attribute = next;
375 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800376}
377
378
Lee Thomason67d61312012-01-24 16:01:51 -0800379char* XMLElement::ParseAttributes( char* p, bool* closedElement )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800380{
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800381 const char* start = p;
Lee Thomason67d61312012-01-24 16:01:51 -0800382 *closedElement = false;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800383
384 // Read the attributes.
385 while( p ) {
386 p = SkipWhiteSpace( p );
387 if ( !p || !(*p) ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800388 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800389 return 0;
390 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800391
392 // attribute.
Lee Thomason22aead12012-01-23 13:29:35 -0800393 if ( IsAlpha( *p ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800394 XMLAttribute* attrib = new XMLAttribute( this );
395 p = attrib->ParseDeep( p );
396 if ( !p ) {
397 delete attrib;
Lee Thomasone4422302012-01-20 17:59:50 -0800398 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800399 return 0;
400 }
401 if ( rootAttribute ) {
402 TIXMLASSERT( lastAttribute );
403 lastAttribute->next = attrib;
404 lastAttribute = attrib;
405 }
406 else {
407 rootAttribute = lastAttribute = attrib;
408 }
409 }
Lee Thomasone4422302012-01-20 17:59:50 -0800410 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800411 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800412 if ( closing ) {
413 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
414 return 0;
415 }
Lee Thomason67d61312012-01-24 16:01:51 -0800416 *closedElement = true;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800417 return p+2; // done; sealed element.
418 }
Lee Thomasone4422302012-01-20 17:59:50 -0800419 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800420 else if ( *p == '>' ) {
421 ++p;
422 break;
423 }
Lee Thomasone4422302012-01-20 17:59:50 -0800424 else {
425 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
426 return 0;
427 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800428 }
Lee Thomason67d61312012-01-24 16:01:51 -0800429 return p;
430}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800431
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800432
Lee Thomason67d61312012-01-24 16:01:51 -0800433//
434// <ele></ele>
435// <ele>foo<b>bar</b></ele>
436//
437char* XMLElement::ParseDeep( char* p )
438{
439 // Read the element name.
440 p = SkipWhiteSpace( p );
441 if ( !p ) return 0;
442 const char* start = p;
443
444 // The closing element is the </element> form. It is
445 // parsed just like a regular element then deleted from
446 // the DOM.
447 if ( *p == '/' ) {
448 closing = true;
449 ++p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800450 }
Lee Thomason67d61312012-01-24 16:01:51 -0800451
452 p = ParseName( p, &name );
453 if ( name.Empty() ) return 0;
454
455 bool elementClosed=false;
456 p = ParseAttributes( p, &elementClosed );
457 if ( !p || !*p || elementClosed || closing )
458 return p;
459
460 p = XMLNode::ParseDeep( p );
461 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800462}
463
464
Lee Thomason5cae8972012-01-24 18:03:07 -0800465void XMLElement::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800466{
Lee Thomason5cae8972012-01-24 18:03:07 -0800467 //if ( !parent || !parent->IsTextParent() ) {
468 // PrintSpace( cfile, depth );
469 //}
470 //fprintf( cfile, "<%s", Name() );
471 streamer->OpenElement( Name(), IsTextParent() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800472
473 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800474 //fprintf( cfile, " " );
475 attrib->Print( streamer );
476
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800477 }
478
Lee Thomason5cae8972012-01-24 18:03:07 -0800479 for( XMLNode* node=firstChild; node; node=node->next ) {
480 node->Print( streamer );
481 }
482 streamer->CloseElement();
483
484/* if ( firstChild ) {
Lee Thomason5492a1c2012-01-23 15:32:10 -0800485 fprintf( cfile, ">", Name() );
Lee Thomason67d61312012-01-24 16:01:51 -0800486 if ( !IsTextParent() ) {
487 fprintf( cfile, "\n" );
488 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800489
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800490 for( XMLNode* node=firstChild; node; node=node->next ) {
491 node->Print( cfile, depth+1 );
492 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800493
Lee Thomason67d61312012-01-24 16:01:51 -0800494 fprintf( cfile, "</%s>", Name() );
495 if ( !IsTextParent() ) {
496 fprintf( cfile, "\n" );
497 }
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800498 }
499 else {
Lee Thomason67d61312012-01-24 16:01:51 -0800500 fprintf( cfile, "/>" );
501 if ( !IsTextParent() ) {
502 fprintf( cfile, "\n" );
503 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800504 }*/
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800505}
506
507
Lee Thomason3f57d272012-01-11 15:30:03 -0800508// --------- XMLDocument ----------- //
Lee Thomason67d61312012-01-24 16:01:51 -0800509XMLDocument::XMLDocument() :
510 XMLNode( this ),
U-Lama\Lee560bd472011-12-28 19:42:49 -0800511 charBuffer( 0 )
512{
513}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800514
515
Lee Thomason3f57d272012-01-11 15:30:03 -0800516XMLDocument::~XMLDocument()
517{
Lee Thomason3f57d272012-01-11 15:30:03 -0800518}
519
520
521
522bool XMLDocument::Parse( const char* p )
523{
Lee Thomasonce0763e2012-01-11 15:43:54 -0800524 charBuffer = CharBuffer::Construct( p );
Lee Thomason3f57d272012-01-11 15:30:03 -0800525 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800526
Lee Thomason67d61312012-01-24 16:01:51 -0800527 char* q = ParseDeep( charBuffer->mem );
528 return true;
Lee Thomason3f57d272012-01-11 15:30:03 -0800529}
530
531
Lee Thomason5cae8972012-01-24 18:03:07 -0800532void XMLDocument::Print( XMLStreamer* streamer )
Lee Thomason3f57d272012-01-11 15:30:03 -0800533{
Lee Thomason5cae8972012-01-24 18:03:07 -0800534 XMLStreamer stdStreamer( stdout );
535 if ( !streamer )
536 streamer = &stdStreamer;
Lee Thomason67d61312012-01-24 16:01:51 -0800537 for( XMLNode* node = firstChild; node; node=node->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800538 node->Print( streamer );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800539 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800540}
541
542
Lee Thomason67d61312012-01-24 16:01:51 -0800543void XMLDocument::SetError( int error, const char* str1, const char* str2 )
544{
545 printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 );
546}
547
Lee Thomason5cae8972012-01-24 18:03:07 -0800548
549StringStack::StringStack()
550{
551 mem = new char[INIT];
552 *mem = 0;
553 inUse = 1; // always has a null
554 allocated = INIT;
555 nPositive = 0;
556}
557
558
559void StringStack::Push( const char* str ) {
560 int needed = strlen( str ) + 1;
561 if ( needed > 1 )
562 nPositive++;
563 if ( inUse+needed > allocated ) {
564 // fixme: power of 2
565 // less stupid allocation
566 int more = inUse+needed + 1000;
567
568 char* newMem = new char[more];
569 memcpy( newMem, mem, inUse );
570 delete [] mem;
571 mem = newMem;
572 }
573 strcpy( mem+inUse, str );
574 inUse += needed;
575}
576
577
578const char* StringStack::Pop() {
579 TIXMLASSERT( inUse > 1 );
580 const char* p = mem+inUse-2;
581 if ( *p ) {
582 nPositive--;
583 }
584 while( *p ) { // stack starts with a null, don't need to check for 'mem'
585 TIXMLASSERT( p > mem );
586 --p;
587 }
588 inUse = p-mem+1;
589 return p+1;
590}
591
592
593XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false )
594{
595}
596
597
598void XMLStreamer::PrintSpace( int depth )
599{
600 for( int i=0; i<depth; ++i ) {
601 fprintf( fp, " " );
602 }
603}
604
605
606void XMLStreamer::OpenElement( const char* name, bool textParent )
607{
608 if ( elementJustOpened ) {
609 SealElement();
610 }
611 stack.Push( name );
612 text.Push( textParent ? "T" : "" );
613
614 PrintSpace( depth );
615 fprintf( fp, "<%s", name );
616 elementJustOpened = true;
617 ++depth;
618}
619
620
621void XMLStreamer::PushAttribute( const char* name, const char* value )
622{
623 TIXMLASSERT( elementJustOpened );
624 fprintf( fp, " %s=\"%s\"", name, value );
625}
626
627
628void XMLStreamer::CloseElement()
629{
630 --depth;
631 const char* name = stack.Pop();
632 text.Pop();
633
634 if ( elementJustOpened ) {
635 fprintf( fp, "/>" );
636 if ( text.NumPositive() == 0 ) {
637 fprintf( fp, "\n" );
638 }
639 }
640 else {
641 PrintSpace( depth );
642 fprintf( fp, "</%s>", name );
643 if ( text.NumPositive() == 0 ) {
644 fprintf( fp, "\n" );
645 }
646 }
647 elementJustOpened = false;
648}
649
650
651void XMLStreamer::SealElement()
652{
653 elementJustOpened = false;
654 fprintf( fp, ">" );
655 if ( text.NumPositive() == 0 ) {
656 fprintf( fp, "\n" );
657 }
658}
659
660
661void XMLStreamer::PushText( const char* text )
662{
663 if ( elementJustOpened ) {
664 SealElement();
665 }
666 fprintf( fp, "%s", text );
667}
668
669
670void XMLStreamer::PushComment( const char* comment )
671{
672 if ( elementJustOpened ) {
673 SealElement();
674 }
675 PrintSpace( depth );
676 fprintf( fp, "<!--%s-->\n", comment );
677}