blob: 3614b468922373634737b392c35792dddb8398d5 [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>
Lee Thomasond1983222012-02-06 08:41:24 -08007#include <new.h>
8
9//#pragma warning ( disable : 4291 )
U-Lama\Lee560bd472011-12-28 19:42:49 -080010
11using namespace tinyxml2;
12
Lee Thomasone4422302012-01-20 17:59:50 -080013static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
Lee Thomasonfde6a752012-01-14 18:08:12 -080014static const char LF = LINE_FEED;
15static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
16static const char CR = CARRIAGE_RETURN;
Lee Thomasone4422302012-01-20 17:59:50 -080017static const char SINGLE_QUOTE = '\'';
18static const char DOUBLE_QUOTE = '\"';
Lee Thomasonfde6a752012-01-14 18:08:12 -080019
Lee Thomason43f59302012-02-06 18:18:11 -080020#define DELETE_NODE( node ) { MemPool* pool = node->memPool; node->~XMLNode(); pool->Free( node ); }
21#define DELETE_ATTRIBUTE( attrib ) { MemPool* pool = attrib->memPool; attrib->~XMLAttribute(); pool->Free( attrib ); }
22
Lee Thomason8ee79892012-01-25 17:44:30 -080023struct Entity {
24 const char* pattern;
25 int length;
26 char value;
27};
28
29static const int NUM_ENTITIES = 5;
30static const Entity entities[NUM_ENTITIES] =
31{
Lee Thomason18d68bd2012-01-26 18:17:26 -080032 { "quot", 4, DOUBLE_QUOTE },
Lee Thomason8ee79892012-01-25 17:44:30 -080033 { "amp", 3, '&' },
Lee Thomason18d68bd2012-01-26 18:17:26 -080034 { "apos", 4, SINGLE_QUOTE },
Lee Thomason8ee79892012-01-25 17:44:30 -080035 { "lt", 2, '<' },
36 { "gt", 2, '>' }
37};
38
Lee Thomasonfde6a752012-01-14 18:08:12 -080039
Lee Thomasone4422302012-01-20 17:59:50 -080040const char* StrPair::GetStr()
41{
42 if ( flags & NEEDS_FLUSH ) {
43 *end = 0;
Lee Thomason8ee79892012-01-25 17:44:30 -080044 flags ^= NEEDS_FLUSH;
Lee Thomasone4422302012-01-20 17:59:50 -080045
Lee Thomason8ee79892012-01-25 17:44:30 -080046 if ( flags ) {
Lee Thomasone4422302012-01-20 17:59:50 -080047 char* p = start;
48 char* q = start;
49
50 while( p < end ) {
Lee Thomason8ee79892012-01-25 17:44:30 -080051 if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
Lee Thomasone4422302012-01-20 17:59:50 -080052 // CR-LF pair becomes LF
53 // CR alone becomes LF
54 // LF-CR becomes LF
55 if ( *(p+1) == LF ) {
56 p += 2;
57 }
58 else {
59 ++p;
60 }
61 *q = LF;
62 }
Lee Thomason8ee79892012-01-25 17:44:30 -080063 else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
Lee Thomasone4422302012-01-20 17:59:50 -080064 if ( *(p+1) == CR ) {
65 p += 2;
66 }
67 else {
68 ++p;
69 }
70 *q = LF;
71 }
Lee Thomason8ee79892012-01-25 17:44:30 -080072 else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
73 int i=0;
74 for( i=0; i<NUM_ENTITIES; ++i ) {
75 if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
76 && *(p+entities[i].length+1) == ';' )
77 {
78 // Found an entity convert;
79 *q = entities[i].value;
80 ++q;
81 p += entities[i].length + 2;
82 break;
83 }
84 }
85 if ( i == NUM_ENTITIES ) {
86 // fixme: treat as error?
87 ++p;
88 ++q;
89 }
90 }
Lee Thomasone4422302012-01-20 17:59:50 -080091 else {
92 *q = *p;
93 ++p;
Lee Thomasonec975ce2012-01-23 11:42:06 -080094 ++q;
Lee Thomasone4422302012-01-20 17:59:50 -080095 }
96 }
Lee Thomason8ee79892012-01-25 17:44:30 -080097 *q = 0;
Lee Thomasone4422302012-01-20 17:59:50 -080098 }
99 flags = 0;
100 }
101 return start;
102}
103
Lee Thomason2c85a712012-01-31 08:24:24 -0800104/*
105const char* StringPool::Intern( const char* str )
106{
107 // Treat the array as a linear, inplace hash table.
108 // Nothing can get deleted, so that's handy.
109 if ( size > pool.Size()*3/4 ) {
110 DynArray< const char*, 20 > store;
111 for( int i=0; i<pool.Size(); ++i ) {
112 if ( pool[i] != 0 ) {
113 store.Push( pool[i] );
114 }
115 }
116 int newSize = pool.Size() * 2;
117 pool.PopArr( pool.Size() );
118
119 const char** mem = pool.PushArr( newSize );
120 memset( (void*)mem, 0, sizeof(char)*newSize );
121
122 while ( !store.Empty() ) {
123 Intern( store.Pop() );
124 }
125 }
126
127}
128*/
129
Lee Thomasone4422302012-01-20 17:59:50 -0800130
Lee Thomason56bdd022012-02-09 18:16:58 -0800131// --------- XMLUtil ----------- //
Lee Thomasond1983222012-02-06 08:41:24 -0800132
Lee Thomason56bdd022012-02-09 18:16:58 -0800133char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
Lee Thomason3f57d272012-01-11 15:30:03 -0800134{
135 TIXMLASSERT( endTag && *endTag );
136
Lee Thomasonfde6a752012-01-14 18:08:12 -0800137 char* start = p;
Lee Thomasonfde6a752012-01-14 18:08:12 -0800138 char endChar = *endTag;
139 int length = strlen( endTag );
Lee Thomason3f57d272012-01-11 15:30:03 -0800140
Lee Thomasonfde6a752012-01-14 18:08:12 -0800141 // Inner loop of text parsing.
Lee Thomason3f57d272012-01-11 15:30:03 -0800142 while ( *p ) {
Lee Thomasonfde6a752012-01-14 18:08:12 -0800143 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800144 Set( start, p, strFlags );
Lee Thomasonec975ce2012-01-23 11:42:06 -0800145 return p + length;
Lee Thomason3f57d272012-01-11 15:30:03 -0800146 }
Lee Thomasonec975ce2012-01-23 11:42:06 -0800147 ++p;
Lee Thomason3f57d272012-01-11 15:30:03 -0800148 }
Lee Thomasone4422302012-01-20 17:59:50 -0800149 return p;
Lee Thomason3f57d272012-01-11 15:30:03 -0800150}
151
152
Lee Thomason56bdd022012-02-09 18:16:58 -0800153char* StrPair::ParseName( char* p )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800154{
155 char* start = p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800156
157 start = p;
158 if ( !start || !(*start) ) {
159 return 0;
160 }
161
Lee Thomason56bdd022012-02-09 18:16:58 -0800162 if ( !XMLUtil::IsAlpha( *p ) ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800163 return 0;
164 }
165
166 while( *p && (
Lee Thomason56bdd022012-02-09 18:16:58 -0800167 XMLUtil::IsAlphaNum( (unsigned char) *p )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800168 || *p == '_'
169 || *p == '-'
170 || *p == '.'
171 || *p == ':' ))
172 {
173 ++p;
174 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800175
176 if ( p > start ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800177 Set( start, p, 0 );
Lee Thomasone4422302012-01-20 17:59:50 -0800178 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800179 }
Lee Thomason39ede242012-01-20 11:27:56 -0800180 return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800181}
182
183
Lee Thomasond1983222012-02-06 08:41:24 -0800184char* XMLDocument::Identify( char* p, XMLNode** node )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800185{
186 XMLNode* returnNode = 0;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800187 char* start = p;
Lee Thomason56bdd022012-02-09 18:16:58 -0800188 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800189 if( !p || !*p )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800190 {
191 return 0;
192 }
193
194 // What is this thing?
195 // - Elements start with a letter or underscore, but xml is reserved.
196 // - Comments: <!--
197 // - Decleration: <?xml
198 // - Everthing else is unknown to tinyxml.
199 //
200
201 static const char* xmlHeader = { "<?xml" };
202 static const char* commentHeader = { "<!--" };
203 static const char* dtdHeader = { "<!" };
204 static const char* cdataHeader = { "<![CDATA[" };
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800205 static const char* elementHeader = { "<" }; // and a header for everything else; check last.
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800206
207 static const int xmlHeaderLen = 5;
208 static const int commentHeaderLen = 4;
209 static const int dtdHeaderLen = 2;
210 static const int cdataHeaderLen = 9;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800211 static const int elementHeaderLen = 1;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800212
Lee Thomason50f97b22012-02-11 16:33:40 -0800213 TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
214 TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
215
216 if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
217 returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
218 returnNode->memPool = &commentPool;
219 p += xmlHeaderLen;
220 }
221 else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800222 returnNode = new (commentPool.Alloc()) XMLComment( this );
223 returnNode->memPool = &commentPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800224 p += commentHeaderLen;
225 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800226 else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
227 XMLText* text = new (textPool.Alloc()) XMLText( this );
228 returnNode = text;
229 returnNode->memPool = &textPool;
230 p += cdataHeaderLen;
231 text->SetCData( true );
232 }
233 else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
234 returnNode = new (commentPool.Alloc()) XMLUnknown( this );
235 returnNode->memPool = &commentPool;
236 p += dtdHeaderLen;
237 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800238 else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800239 returnNode = new (elementPool.Alloc()) XMLElement( this );
240 returnNode->memPool = &elementPool;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800241 p += elementHeaderLen;
242 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800243 else if ( (*p != '<') && XMLUtil::IsAlphaNum( *p ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800244 returnNode = new (textPool.Alloc()) XMLText( this );
245 returnNode->memPool = &textPool;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800246 p = start; // Back it up, all the text counts.
247 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800248 else {
Lee Thomason50f97b22012-02-11 16:33:40 -0800249 this->SetError( ERROR_IDENTIFYING_TAG, p, 0 );
250 p = 0;
251 returnNode = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800252 }
253
254 *node = returnNode;
255 return p;
256}
257
258
Lee Thomason751da522012-02-10 08:50:51 -0800259bool XMLDocument::Accept( XMLVisitor* visitor ) const
260{
261 if ( visitor->VisitEnter( *this ) )
262 {
263 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
264 {
265 if ( !node->Accept( visitor ) )
266 break;
267 }
268 }
269 return visitor->VisitExit( *this );
270}
Lee Thomason56bdd022012-02-09 18:16:58 -0800271
272
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800273// --------- XMLNode ----------- //
274
275XMLNode::XMLNode( XMLDocument* doc ) :
276 document( doc ),
277 parent( 0 ),
Lee Thomason50adb4c2012-02-13 15:07:09 -0800278// isTextParent( false ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800279 firstChild( 0 ), lastChild( 0 ),
280 prev( 0 ), next( 0 )
281{
282
283}
284
285
286XMLNode::~XMLNode()
287{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800288 ClearChildren();
Lee Thomason7c913cd2012-01-26 18:32:34 -0800289 if ( parent ) {
290 parent->Unlink( this );
291 }
Lee Thomason18d68bd2012-01-26 18:17:26 -0800292}
293
294
295void XMLNode::ClearChildren()
296{
Lee Thomasond923c672012-01-23 08:44:25 -0800297 while( firstChild ) {
298 XMLNode* node = firstChild;
299 Unlink( node );
Lee Thomasond1983222012-02-06 08:41:24 -0800300
Lee Thomason43f59302012-02-06 18:18:11 -0800301 DELETE_NODE( node );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800302 }
Lee Thomason18d68bd2012-01-26 18:17:26 -0800303 firstChild = lastChild = 0;
Lee Thomasond923c672012-01-23 08:44:25 -0800304}
305
306
307void XMLNode::Unlink( XMLNode* child )
308{
309 TIXMLASSERT( child->parent == this );
310 if ( child == firstChild )
311 firstChild = firstChild->next;
312 if ( child == lastChild )
313 lastChild = lastChild->prev;
314
315 if ( child->prev ) {
316 child->prev->next = child->next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800317 }
Lee Thomasond923c672012-01-23 08:44:25 -0800318 if ( child->next ) {
319 child->next->prev = child->prev;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800320 }
Lee Thomasond923c672012-01-23 08:44:25 -0800321 child->parent = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800322}
323
324
325XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
326{
327 if ( lastChild ) {
328 TIXMLASSERT( firstChild );
329 TIXMLASSERT( lastChild->next == 0 );
330 lastChild->next = addThis;
331 addThis->prev = lastChild;
332 lastChild = addThis;
333
334 addThis->parent = this;
335 addThis->next = 0;
336 }
337 else {
338 TIXMLASSERT( firstChild == 0 );
339 firstChild = lastChild = addThis;
340
341 addThis->parent = this;
342 addThis->prev = 0;
343 addThis->next = 0;
344 }
Lee Thomason50adb4c2012-02-13 15:07:09 -0800345// if ( addThis->ToText() ) {
346// SetTextParent();
347// }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800348 return addThis;
349}
350
351
Lee Thomason56bdd022012-02-09 18:16:58 -0800352const XMLElement* XMLNode::FirstChildElement( const char* value ) const
Lee Thomason2c85a712012-01-31 08:24:24 -0800353{
354 for( XMLNode* node=firstChild; node; node=node->next ) {
355 XMLElement* element = node->ToElement();
356 if ( element ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800357 if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
Lee Thomason2c85a712012-01-31 08:24:24 -0800358 return element;
359 }
360 }
361 }
362 return 0;
363}
364
365
Lee Thomason56bdd022012-02-09 18:16:58 -0800366const XMLElement* XMLNode::LastChildElement( const char* value ) const
367{
368 for( XMLNode* node=lastChild; node; node=node->prev ) {
369 XMLElement* element = node->ToElement();
370 if ( element ) {
371 if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
372 return element;
373 }
374 }
375 }
376 return 0;
377}
378
379
380void XMLNode::DeleteChild( XMLNode* node )
381{
382 TIXMLASSERT( node->parent == this );
383 TIXMLASSERT( 0 );
384}
385
386
Lee Thomason751da522012-02-10 08:50:51 -0800387/*void XMLNode::Print( XMLStreamer* streamer )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800388{
389 for( XMLNode* node = firstChild; node; node=node->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800390 node->Print( streamer );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800391 }
392}
Lee Thomason751da522012-02-10 08:50:51 -0800393*/
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800394
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800395
Lee Thomason67d61312012-01-24 16:01:51 -0800396char* XMLNode::ParseDeep( char* p )
397{
398 while( p && *p ) {
399 XMLNode* node = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800400 p = document->Identify( p, &node );
Lee Thomason67d61312012-01-24 16:01:51 -0800401 if ( p && node ) {
402 p = node->ParseDeep( p );
403 // FIXME: is it the correct closing element?
404 if ( node->IsClosingElement() ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800405 DELETE_NODE( node );
Lee Thomason67d61312012-01-24 16:01:51 -0800406 return p;
407 }
408 this->InsertEndChild( node );
409 }
410 }
411 return 0;
412}
413
Lee Thomason5492a1c2012-01-23 15:32:10 -0800414// --------- XMLText ---------- //
415char* XMLText::ParseDeep( char* p )
416{
Lee Thomason50f97b22012-02-11 16:33:40 -0800417 if ( this->CData() ) {
418 p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
419 return p;
420 }
421 else {
422 p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
423 // consumes the end tag.
424 if ( p && *p ) {
425 return p-1;
426 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800427 }
428 return 0;
429}
430
431
Lee Thomason56bdd022012-02-09 18:16:58 -0800432bool XMLText::Accept( XMLVisitor* visitor ) const
433{
434 return visitor->Visit( *this );
435}
436
437
Lee Thomason3f57d272012-01-11 15:30:03 -0800438// --------- XMLComment ---------- //
439
Lee Thomasone4422302012-01-20 17:59:50 -0800440XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800441{
442}
443
444
Lee Thomasonce0763e2012-01-11 15:43:54 -0800445XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800446{
Lee Thomasond923c672012-01-23 08:44:25 -0800447 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800448}
449
450
Lee Thomasonce0763e2012-01-11 15:43:54 -0800451char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800452{
453 // Comment parses as text.
Lee Thomason56bdd022012-02-09 18:16:58 -0800454 return value.ParseText( p, "-->", StrPair::COMMENT );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800455}
456
457
Lee Thomason751da522012-02-10 08:50:51 -0800458bool XMLComment::Accept( XMLVisitor* visitor ) const
459{
460 return visitor->Visit( *this );
461}
Lee Thomason56bdd022012-02-09 18:16:58 -0800462
463
Lee Thomason50f97b22012-02-11 16:33:40 -0800464// --------- XMLDeclaration ---------- //
465
466XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
467{
468}
469
470
471XMLDeclaration::~XMLDeclaration()
472{
473 //printf( "~XMLDeclaration\n" );
474}
475
476
477char* XMLDeclaration::ParseDeep( char* p )
478{
479 // Declaration parses as text.
480 return value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
481}
482
483
484bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
485{
486 return visitor->Visit( *this );
487}
488
489// --------- XMLUnknown ---------- //
490
491XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
492{
493}
494
495
496XMLUnknown::~XMLUnknown()
497{
498}
499
500
501char* XMLUnknown::ParseDeep( char* p )
502{
503 // Unknown parses as text.
504 return value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
505}
506
507
508bool XMLUnknown::Accept( XMLVisitor* visitor ) const
509{
510 return visitor->Visit( *this );
511}
512
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800513// --------- XMLAttribute ---------- //
514char* XMLAttribute::ParseDeep( char* p )
515{
Lee Thomason56bdd022012-02-09 18:16:58 -0800516 p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
Lee Thomason22aead12012-01-23 13:29:35 -0800517 if ( !p || !*p ) return 0;
518
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800519 char endTag[2] = { *p, 0 };
520 ++p;
Lee Thomason56bdd022012-02-09 18:16:58 -0800521 p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
Lee Thomasone4422302012-01-20 17:59:50 -0800522 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800523 return p;
524}
525
526
Lee Thomason751da522012-02-10 08:50:51 -0800527/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800528void XMLAttribute::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800529{
Lee Thomason22aead12012-01-23 13:29:35 -0800530 // fixme: sort out single vs. double quote
Lee Thomason5cae8972012-01-24 18:03:07 -0800531 //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
532 streamer->PushAttribute( name.GetStr(), value.GetStr() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800533}
Lee Thomason751da522012-02-10 08:50:51 -0800534*/
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800535
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800536// --------- XMLElement ---------- //
537XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800538 closing( false ),
539 rootAttribute( 0 ),
540 lastAttribute( 0 )
541{
542}
543
544
545XMLElement::~XMLElement()
546{
Lee Thomasond923c672012-01-23 08:44:25 -0800547 //printf( "~XMLElemen %x\n",this );
548
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800549 XMLAttribute* attribute = rootAttribute;
550 while( attribute ) {
551 XMLAttribute* next = attribute->next;
Lee Thomason43f59302012-02-06 18:18:11 -0800552 DELETE_ATTRIBUTE( attribute );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800553 attribute = next;
554 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800555}
556
557
Lee Thomason67d61312012-01-24 16:01:51 -0800558char* XMLElement::ParseAttributes( char* p, bool* closedElement )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800559{
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800560 const char* start = p;
Lee Thomason67d61312012-01-24 16:01:51 -0800561 *closedElement = false;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800562
563 // Read the attributes.
564 while( p ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800565 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800566 if ( !p || !(*p) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800567 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800568 return 0;
569 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800570
571 // attribute.
Lee Thomason56bdd022012-02-09 18:16:58 -0800572 if ( XMLUtil::IsAlpha( *p ) ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800573 XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
574 attrib->memPool = &document->attributePool;
Lee Thomasond1983222012-02-06 08:41:24 -0800575
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800576 p = attrib->ParseDeep( p );
577 if ( !p ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800578 DELETE_ATTRIBUTE( attrib );
Lee Thomasone4422302012-01-20 17:59:50 -0800579 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800580 return 0;
581 }
582 if ( rootAttribute ) {
583 TIXMLASSERT( lastAttribute );
584 lastAttribute->next = attrib;
585 lastAttribute = attrib;
586 }
587 else {
588 rootAttribute = lastAttribute = attrib;
589 }
590 }
Lee Thomasone4422302012-01-20 17:59:50 -0800591 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800592 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800593 if ( closing ) {
594 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
595 return 0;
596 }
Lee Thomason67d61312012-01-24 16:01:51 -0800597 *closedElement = true;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800598 return p+2; // done; sealed element.
599 }
Lee Thomasone4422302012-01-20 17:59:50 -0800600 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800601 else if ( *p == '>' ) {
602 ++p;
603 break;
604 }
Lee Thomasone4422302012-01-20 17:59:50 -0800605 else {
606 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
607 return 0;
608 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800609 }
Lee Thomason67d61312012-01-24 16:01:51 -0800610 return p;
611}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800612
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800613
Lee Thomason67d61312012-01-24 16:01:51 -0800614//
615// <ele></ele>
616// <ele>foo<b>bar</b></ele>
617//
618char* XMLElement::ParseDeep( char* p )
619{
620 // Read the element name.
Lee Thomason56bdd022012-02-09 18:16:58 -0800621 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason67d61312012-01-24 16:01:51 -0800622 if ( !p ) return 0;
623 const char* start = p;
624
625 // The closing element is the </element> form. It is
626 // parsed just like a regular element then deleted from
627 // the DOM.
628 if ( *p == '/' ) {
629 closing = true;
630 ++p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800631 }
Lee Thomason67d61312012-01-24 16:01:51 -0800632
Lee Thomason56bdd022012-02-09 18:16:58 -0800633 p = value.ParseName( p );
Lee Thomasond1983222012-02-06 08:41:24 -0800634 if ( value.Empty() ) return 0;
Lee Thomason67d61312012-01-24 16:01:51 -0800635
636 bool elementClosed=false;
637 p = ParseAttributes( p, &elementClosed );
638 if ( !p || !*p || elementClosed || closing )
639 return p;
640
641 p = XMLNode::ParseDeep( p );
642 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800643}
644
645
Lee Thomason751da522012-02-10 08:50:51 -0800646/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800647void XMLElement::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800648{
Lee Thomason5cae8972012-01-24 18:03:07 -0800649 //if ( !parent || !parent->IsTextParent() ) {
650 // PrintSpace( cfile, depth );
651 //}
652 //fprintf( cfile, "<%s", Name() );
Lee Thomason56bdd022012-02-09 18:16:58 -0800653 streamer->OpenElement( Name() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800654
655 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800656 //fprintf( cfile, " " );
657 attrib->Print( streamer );
658
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800659 }
660
Lee Thomason5cae8972012-01-24 18:03:07 -0800661 for( XMLNode* node=firstChild; node; node=node->next ) {
662 node->Print( streamer );
663 }
664 streamer->CloseElement();
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800665}
Lee Thomason751da522012-02-10 08:50:51 -0800666*/
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800667
668
Lee Thomason751da522012-02-10 08:50:51 -0800669bool XMLElement::Accept( XMLVisitor* visitor ) const
670{
671 if ( visitor->VisitEnter( *this, rootAttribute ) )
672 {
673 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
674 {
675 if ( !node->Accept( visitor ) )
676 break;
677 }
678 }
679 return visitor->VisitExit( *this );
680
681}
Lee Thomason56bdd022012-02-09 18:16:58 -0800682
683
Lee Thomason3f57d272012-01-11 15:30:03 -0800684// --------- XMLDocument ----------- //
Lee Thomason67d61312012-01-24 16:01:51 -0800685XMLDocument::XMLDocument() :
Lee Thomason18d68bd2012-01-26 18:17:26 -0800686 XMLNode( 0 ),
U-Lama\Lee560bd472011-12-28 19:42:49 -0800687 charBuffer( 0 )
688{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800689 document = this; // avoid warning about 'this' in initializer list
U-Lama\Lee560bd472011-12-28 19:42:49 -0800690}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800691
692
Lee Thomason3f57d272012-01-11 15:30:03 -0800693XMLDocument::~XMLDocument()
694{
Lee Thomasond1983222012-02-06 08:41:24 -0800695 ClearChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -0800696 delete [] charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -0800697
Lee Thomason455c9d42012-02-06 09:14:14 -0800698 /*
699 textPool.Trace( "text" );
700 elementPool.Trace( "element" );
701 commentPool.Trace( "comment" );
702 attributePool.Trace( "attribute" );
703 */
704 TIXMLASSERT( textPool.CurrentAllocs() == 0 );
705 TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
706 TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
707 TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
Lee Thomason3f57d272012-01-11 15:30:03 -0800708}
709
710
Lee Thomason18d68bd2012-01-26 18:17:26 -0800711void XMLDocument::InitDocument()
712{
713 errorID = NO_ERROR;
714 errorStr1 = 0;
715 errorStr2 = 0;
716
717 delete [] charBuffer;
718 charBuffer = 0;
719
720}
721
Lee Thomason3f57d272012-01-11 15:30:03 -0800722
Lee Thomason2c85a712012-01-31 08:24:24 -0800723XMLElement* XMLDocument::NewElement( const char* name )
724{
Lee Thomasond1983222012-02-06 08:41:24 -0800725 XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
726 ele->memPool = &elementPool;
Lee Thomason2c85a712012-01-31 08:24:24 -0800727 ele->SetName( name );
728 return ele;
729}
730
731
Lee Thomason7c913cd2012-01-26 18:32:34 -0800732int XMLDocument::Parse( const char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800733{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800734 ClearChildren();
735 InitDocument();
736
737 if ( !p || !*p ) {
738 return true; // correctly parse an empty string?
739 }
740 size_t len = strlen( p );
741 charBuffer = new char[ len+1 ];
742 memcpy( charBuffer, p, len+1 );
Lee Thomason3f57d272012-01-11 15:30:03 -0800743 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800744
Lee Thomason18d68bd2012-01-26 18:17:26 -0800745 char* q = ParseDeep( charBuffer );
Lee Thomason7c913cd2012-01-26 18:32:34 -0800746 return errorID;
Lee Thomason3f57d272012-01-11 15:30:03 -0800747}
748
749
Lee Thomason5cae8972012-01-24 18:03:07 -0800750void XMLDocument::Print( XMLStreamer* streamer )
Lee Thomason3f57d272012-01-11 15:30:03 -0800751{
Lee Thomason5cae8972012-01-24 18:03:07 -0800752 XMLStreamer stdStreamer( stdout );
753 if ( !streamer )
754 streamer = &stdStreamer;
Lee Thomason751da522012-02-10 08:50:51 -0800755 //for( XMLNode* node = firstChild; node; node=node->next ) {
756 // node->Print( streamer );
757 //}
758 Accept( streamer );
Lee Thomason3f57d272012-01-11 15:30:03 -0800759}
760
761
Lee Thomason67d61312012-01-24 16:01:51 -0800762void XMLDocument::SetError( int error, const char* str1, const char* str2 )
763{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800764 errorID = error;
765 printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 ); // fixme: remove
766 errorStr1 = str1;
767 errorStr2 = str2;
Lee Thomason67d61312012-01-24 16:01:51 -0800768}
769
Lee Thomason5cae8972012-01-24 18:03:07 -0800770
Lee Thomason2c85a712012-01-31 08:24:24 -0800771/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800772StringStack::StringStack()
773{
Lee Thomason5cae8972012-01-24 18:03:07 -0800774 nPositive = 0;
Lee Thomason1270ae52012-01-27 17:58:30 -0800775 mem.Push( 0 ); // start with null. makes later code simpler.
Lee Thomason5cae8972012-01-24 18:03:07 -0800776}
777
778
Lee Thomason24767b02012-01-25 17:16:23 -0800779StringStack::~StringStack()
780{
Lee Thomason24767b02012-01-25 17:16:23 -0800781}
782
783
Lee Thomason5cae8972012-01-24 18:03:07 -0800784void StringStack::Push( const char* str ) {
785 int needed = strlen( str ) + 1;
Lee Thomason1270ae52012-01-27 17:58:30 -0800786 char* p = mem.PushArr( needed );
787 strcpy( p, str );
788 if ( needed > 1 )
Lee Thomason5cae8972012-01-24 18:03:07 -0800789 nPositive++;
Lee Thomason5cae8972012-01-24 18:03:07 -0800790}
791
792
793const char* StringStack::Pop() {
Lee Thomason1270ae52012-01-27 17:58:30 -0800794 TIXMLASSERT( mem.Size() > 1 );
795 const char* p = mem.Mem() + mem.Size() - 2; // end of final string.
Lee Thomason5cae8972012-01-24 18:03:07 -0800796 if ( *p ) {
797 nPositive--;
798 }
799 while( *p ) { // stack starts with a null, don't need to check for 'mem'
Lee Thomason1270ae52012-01-27 17:58:30 -0800800 TIXMLASSERT( p > mem.Mem() );
Lee Thomason5cae8972012-01-24 18:03:07 -0800801 --p;
802 }
Lee Thomason1270ae52012-01-27 17:58:30 -0800803 mem.PopArr( strlen(p)+1 );
Lee Thomason5cae8972012-01-24 18:03:07 -0800804 return p+1;
805}
Lee Thomason2c85a712012-01-31 08:24:24 -0800806*/
Lee Thomason5cae8972012-01-24 18:03:07 -0800807
808
Lee Thomason56bdd022012-02-09 18:16:58 -0800809XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false ), textDepth( -1 )
Lee Thomason5cae8972012-01-24 18:03:07 -0800810{
Lee Thomason857b8682012-01-25 17:50:25 -0800811 for( int i=0; i<ENTITY_RANGE; ++i ) {
812 entityFlag[i] = false;
813 }
814 for( int i=0; i<NUM_ENTITIES; ++i ) {
815 TIXMLASSERT( entities[i].value < ENTITY_RANGE );
816 if ( entities[i].value < ENTITY_RANGE ) {
817 entityFlag[ entities[i].value ] = true;
818 }
819 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800820}
821
822
823void XMLStreamer::PrintSpace( int depth )
824{
825 for( int i=0; i<depth; ++i ) {
826 fprintf( fp, " " );
827 }
828}
829
830
Lee Thomason951d8832012-01-26 08:47:06 -0800831void XMLStreamer::PrintString( const char* p )
Lee Thomason857b8682012-01-25 17:50:25 -0800832{
Lee Thomason951d8832012-01-26 08:47:06 -0800833 // Look for runs of bytes between entities to print.
834 const char* q = p;
Lee Thomason857b8682012-01-25 17:50:25 -0800835
Lee Thomason951d8832012-01-26 08:47:06 -0800836 while ( *q ) {
837 if ( *q < ENTITY_RANGE ) {
838 // Check for entities. If one is found, flush
839 // the stream up until the entity, write the
840 // entity, and keep looking.
841 if ( entityFlag[*q] ) {
842 while ( p < q ) {
843 fputc( *p, fp );
844 ++p;
845 }
846 for( int i=0; i<NUM_ENTITIES; ++i ) {
847 if ( entities[i].value == *q ) {
848 fprintf( fp, "&%s;", entities[i].pattern );
849 break;
850 }
851 }
852 ++p;
853 }
854 }
855 ++q;
856 }
857 // Flush the remaining string. This will be the entire
858 // string if an entity wasn't found.
859 if ( q-p > 0 ) {
860 fprintf( fp, "%s", p );
861 }
Lee Thomason857b8682012-01-25 17:50:25 -0800862}
863
Lee Thomason56bdd022012-02-09 18:16:58 -0800864void XMLStreamer::OpenElement( const char* name )
Lee Thomason5cae8972012-01-24 18:03:07 -0800865{
866 if ( elementJustOpened ) {
867 SealElement();
868 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800869 stack.Push( name );
870
871 if ( textDepth < 0 && depth > 0) {
872 fprintf( fp, "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -0800873 PrintSpace( depth );
874 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800875
Lee Thomason5cae8972012-01-24 18:03:07 -0800876 fprintf( fp, "<%s", name );
877 elementJustOpened = true;
878 ++depth;
879}
880
881
882void XMLStreamer::PushAttribute( const char* name, const char* value )
883{
884 TIXMLASSERT( elementJustOpened );
Lee Thomason18d68bd2012-01-26 18:17:26 -0800885 fprintf( fp, " %s=\"", name );
886 PrintString( value );
887 fprintf( fp, "\"" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800888}
889
890
891void XMLStreamer::CloseElement()
892{
893 --depth;
894 const char* name = stack.Pop();
Lee Thomason5cae8972012-01-24 18:03:07 -0800895
896 if ( elementJustOpened ) {
897 fprintf( fp, "/>" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800898 }
899 else {
Lee Thomason56bdd022012-02-09 18:16:58 -0800900 if ( textDepth < 0 ) {
901 fprintf( fp, "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -0800902 PrintSpace( depth );
903 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800904 fprintf( fp, "</%s>", name );
Lee Thomason5cae8972012-01-24 18:03:07 -0800905 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800906
907 if ( textDepth == depth )
908 textDepth = -1;
909 if ( depth == 0 )
910 fprintf( fp, "\n" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800911 elementJustOpened = false;
912}
913
914
915void XMLStreamer::SealElement()
916{
917 elementJustOpened = false;
918 fprintf( fp, ">" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800919}
920
921
Lee Thomason50f97b22012-02-11 16:33:40 -0800922void XMLStreamer::PushText( const char* text, bool cdata )
Lee Thomason5cae8972012-01-24 18:03:07 -0800923{
Lee Thomason56bdd022012-02-09 18:16:58 -0800924 textDepth = depth-1;
925
Lee Thomason5cae8972012-01-24 18:03:07 -0800926 if ( elementJustOpened ) {
927 SealElement();
928 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800929 if ( cdata )
930 fprintf( fp, "<![CDATA[" );
Lee Thomason951d8832012-01-26 08:47:06 -0800931 PrintString( text );
Lee Thomason50f97b22012-02-11 16:33:40 -0800932 if ( cdata )
933 fprintf( fp, "]]>" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800934}
935
936
937void XMLStreamer::PushComment( const char* comment )
938{
939 if ( elementJustOpened ) {
940 SealElement();
941 }
942 PrintSpace( depth );
943 fprintf( fp, "<!--%s-->\n", comment );
944}
Lee Thomason751da522012-02-10 08:50:51 -0800945
946
947bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
948{
949 OpenElement( element.Name() );
950 while ( attribute ) {
951 PushAttribute( attribute->Name(), attribute->Value() );
952 attribute = attribute->Next();
953 }
954 return true;
955}
956
957
958bool XMLStreamer::VisitExit( const XMLElement& element )
959{
960 CloseElement();
961 return true;
962}
963
964
965bool XMLStreamer::Visit( const XMLText& text )
966{
967 PushText( text.Value() );
968 return true;
969}
970
971
972bool XMLStreamer::Visit( const XMLComment& comment )
973{
974 PushComment( comment.Value() );
975 return true;
976}