blob: 72f9db8f6d2258d1293e4c7a1a05ff0cb7b7ce36 [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 Thomason56bdd022012-02-09 18:16:58 -0800213 if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800214 returnNode = new (commentPool.Alloc()) XMLComment( this );
215 returnNode->memPool = &commentPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800216 p += commentHeaderLen;
217 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800218 else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800219 returnNode = new (elementPool.Alloc()) XMLElement( this );
220 returnNode->memPool = &elementPool;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800221 p += elementHeaderLen;
222 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800223 // fixme: better text detection
Lee Thomason56bdd022012-02-09 18:16:58 -0800224 else if ( (*p != '<') && XMLUtil::IsAlphaNum( *p ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800225 returnNode = new (textPool.Alloc()) XMLText( this );
226 returnNode->memPool = &textPool;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800227 p = start; // Back it up, all the text counts.
228 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800229 else {
230 TIXMLASSERT( 0 );
231 }
232
233 *node = returnNode;
234 return p;
235}
236
237
Lee Thomason56bdd022012-02-09 18:16:58 -0800238bool XMLDocument::Accept( XMLVisitor* visitor ) const
239{
240 if ( visitor->VisitEnter( *this ) )
241 {
242 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
243 {
244 if ( !node->Accept( visitor ) )
245 break;
246 }
247 }
248 return visitor->VisitExit( *this );
249}
250
251
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800252// --------- XMLNode ----------- //
253
254XMLNode::XMLNode( XMLDocument* doc ) :
255 document( doc ),
256 parent( 0 ),
Lee Thomason67d61312012-01-24 16:01:51 -0800257 isTextParent( false ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800258 firstChild( 0 ), lastChild( 0 ),
259 prev( 0 ), next( 0 )
260{
261
262}
263
264
265XMLNode::~XMLNode()
266{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800267 ClearChildren();
Lee Thomason7c913cd2012-01-26 18:32:34 -0800268 if ( parent ) {
269 parent->Unlink( this );
270 }
Lee Thomason18d68bd2012-01-26 18:17:26 -0800271}
272
273
274void XMLNode::ClearChildren()
275{
Lee Thomasond923c672012-01-23 08:44:25 -0800276 while( firstChild ) {
277 XMLNode* node = firstChild;
278 Unlink( node );
Lee Thomasond1983222012-02-06 08:41:24 -0800279
Lee Thomason43f59302012-02-06 18:18:11 -0800280 DELETE_NODE( node );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800281 }
Lee Thomason18d68bd2012-01-26 18:17:26 -0800282 firstChild = lastChild = 0;
Lee Thomasond923c672012-01-23 08:44:25 -0800283}
284
285
286void XMLNode::Unlink( XMLNode* child )
287{
288 TIXMLASSERT( child->parent == this );
289 if ( child == firstChild )
290 firstChild = firstChild->next;
291 if ( child == lastChild )
292 lastChild = lastChild->prev;
293
294 if ( child->prev ) {
295 child->prev->next = child->next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800296 }
Lee Thomasond923c672012-01-23 08:44:25 -0800297 if ( child->next ) {
298 child->next->prev = child->prev;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800299 }
Lee Thomasond923c672012-01-23 08:44:25 -0800300 child->parent = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800301}
302
303
304XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
305{
306 if ( lastChild ) {
307 TIXMLASSERT( firstChild );
308 TIXMLASSERT( lastChild->next == 0 );
309 lastChild->next = addThis;
310 addThis->prev = lastChild;
311 lastChild = addThis;
312
313 addThis->parent = this;
314 addThis->next = 0;
315 }
316 else {
317 TIXMLASSERT( firstChild == 0 );
318 firstChild = lastChild = addThis;
319
320 addThis->parent = this;
321 addThis->prev = 0;
322 addThis->next = 0;
323 }
Lee Thomason67d61312012-01-24 16:01:51 -0800324 if ( addThis->ToText() ) {
325 SetTextParent();
326 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800327 return addThis;
328}
329
330
Lee Thomason56bdd022012-02-09 18:16:58 -0800331const XMLElement* XMLNode::FirstChildElement( const char* value ) const
Lee Thomason2c85a712012-01-31 08:24:24 -0800332{
333 for( XMLNode* node=firstChild; node; node=node->next ) {
334 XMLElement* element = node->ToElement();
335 if ( element ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800336 if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
Lee Thomason2c85a712012-01-31 08:24:24 -0800337 return element;
338 }
339 }
340 }
341 return 0;
342}
343
344
Lee Thomason56bdd022012-02-09 18:16:58 -0800345const XMLElement* XMLNode::LastChildElement( const char* value ) const
346{
347 for( XMLNode* node=lastChild; node; node=node->prev ) {
348 XMLElement* element = node->ToElement();
349 if ( element ) {
350 if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
351 return element;
352 }
353 }
354 }
355 return 0;
356}
357
358
359void XMLNode::DeleteChild( XMLNode* node )
360{
361 TIXMLASSERT( node->parent == this );
362 TIXMLASSERT( 0 );
363}
364
365
Lee Thomason5cae8972012-01-24 18:03:07 -0800366void XMLNode::Print( XMLStreamer* streamer )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800367{
368 for( XMLNode* node = firstChild; node; node=node->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800369 node->Print( streamer );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800370 }
371}
372
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800373
Lee Thomason67d61312012-01-24 16:01:51 -0800374char* XMLNode::ParseDeep( char* p )
375{
376 while( p && *p ) {
377 XMLNode* node = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800378 p = document->Identify( p, &node );
Lee Thomason67d61312012-01-24 16:01:51 -0800379 if ( p && node ) {
380 p = node->ParseDeep( p );
381 // FIXME: is it the correct closing element?
382 if ( node->IsClosingElement() ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800383 DELETE_NODE( node );
Lee Thomason67d61312012-01-24 16:01:51 -0800384 return p;
385 }
386 this->InsertEndChild( node );
387 }
388 }
389 return 0;
390}
391
Lee Thomason5492a1c2012-01-23 15:32:10 -0800392// --------- XMLText ---------- //
393char* XMLText::ParseDeep( char* p )
394{
Lee Thomason56bdd022012-02-09 18:16:58 -0800395 p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800396 // consumes the end tag.
397 if ( p && *p ) {
398 return p-1;
399 }
400 return 0;
401}
402
403
Lee Thomason5cae8972012-01-24 18:03:07 -0800404void XMLText::Print( XMLStreamer* streamer )
Lee Thomason5492a1c2012-01-23 15:32:10 -0800405{
Lee Thomason67d61312012-01-24 16:01:51 -0800406 const char* v = value.GetStr();
Lee Thomason5cae8972012-01-24 18:03:07 -0800407 streamer->PushText( v );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800408}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800409
410
Lee Thomason56bdd022012-02-09 18:16:58 -0800411bool XMLText::Accept( XMLVisitor* visitor ) const
412{
413 return visitor->Visit( *this );
414}
415
416
Lee Thomason3f57d272012-01-11 15:30:03 -0800417// --------- XMLComment ---------- //
418
Lee Thomasone4422302012-01-20 17:59:50 -0800419XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800420{
421}
422
423
Lee Thomasonce0763e2012-01-11 15:43:54 -0800424XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800425{
Lee Thomasond923c672012-01-23 08:44:25 -0800426 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800427}
428
429
Lee Thomason5cae8972012-01-24 18:03:07 -0800430void XMLComment::Print( XMLStreamer* streamer )
Lee Thomasonce0763e2012-01-11 15:43:54 -0800431{
Lee Thomason5cae8972012-01-24 18:03:07 -0800432// XMLNode::Print( fp, depth );
433// fprintf( fp, "<!--%s-->\n", value.GetStr() );
434 streamer->PushComment( value.GetStr() );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800435}
436
437
438char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800439{
440 // Comment parses as text.
Lee Thomason56bdd022012-02-09 18:16:58 -0800441 return value.ParseText( p, "-->", StrPair::COMMENT );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800442}
443
444
Lee Thomason56bdd022012-02-09 18:16:58 -0800445bool XMLComment::Accept( XMLVisitor* visitor ) const
446{
447 return visitor->Visit( *this );
448}
449
450
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800451// --------- XMLAttribute ---------- //
452char* XMLAttribute::ParseDeep( char* p )
453{
Lee Thomason56bdd022012-02-09 18:16:58 -0800454 p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
Lee Thomason22aead12012-01-23 13:29:35 -0800455 if ( !p || !*p ) return 0;
456
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800457 char endTag[2] = { *p, 0 };
458 ++p;
Lee Thomason56bdd022012-02-09 18:16:58 -0800459 p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
Lee Thomasone4422302012-01-20 17:59:50 -0800460 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800461 return p;
462}
463
464
Lee Thomason5cae8972012-01-24 18:03:07 -0800465void XMLAttribute::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800466{
Lee Thomason22aead12012-01-23 13:29:35 -0800467 // fixme: sort out single vs. double quote
Lee Thomason5cae8972012-01-24 18:03:07 -0800468 //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
469 streamer->PushAttribute( name.GetStr(), value.GetStr() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800470}
471
472
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800473// --------- XMLElement ---------- //
474XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800475 closing( false ),
476 rootAttribute( 0 ),
477 lastAttribute( 0 )
478{
479}
480
481
482XMLElement::~XMLElement()
483{
Lee Thomasond923c672012-01-23 08:44:25 -0800484 //printf( "~XMLElemen %x\n",this );
485
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800486 XMLAttribute* attribute = rootAttribute;
487 while( attribute ) {
488 XMLAttribute* next = attribute->next;
Lee Thomason43f59302012-02-06 18:18:11 -0800489 DELETE_ATTRIBUTE( attribute );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800490 attribute = next;
491 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800492}
493
494
Lee Thomason67d61312012-01-24 16:01:51 -0800495char* XMLElement::ParseAttributes( char* p, bool* closedElement )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800496{
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800497 const char* start = p;
Lee Thomason67d61312012-01-24 16:01:51 -0800498 *closedElement = false;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800499
500 // Read the attributes.
501 while( p ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800502 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800503 if ( !p || !(*p) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800504 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800505 return 0;
506 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800507
508 // attribute.
Lee Thomason56bdd022012-02-09 18:16:58 -0800509 if ( XMLUtil::IsAlpha( *p ) ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800510 XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
511 attrib->memPool = &document->attributePool;
Lee Thomasond1983222012-02-06 08:41:24 -0800512
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800513 p = attrib->ParseDeep( p );
514 if ( !p ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800515 DELETE_ATTRIBUTE( attrib );
Lee Thomasone4422302012-01-20 17:59:50 -0800516 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800517 return 0;
518 }
519 if ( rootAttribute ) {
520 TIXMLASSERT( lastAttribute );
521 lastAttribute->next = attrib;
522 lastAttribute = attrib;
523 }
524 else {
525 rootAttribute = lastAttribute = attrib;
526 }
527 }
Lee Thomasone4422302012-01-20 17:59:50 -0800528 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800529 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800530 if ( closing ) {
531 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
532 return 0;
533 }
Lee Thomason67d61312012-01-24 16:01:51 -0800534 *closedElement = true;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800535 return p+2; // done; sealed element.
536 }
Lee Thomasone4422302012-01-20 17:59:50 -0800537 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800538 else if ( *p == '>' ) {
539 ++p;
540 break;
541 }
Lee Thomasone4422302012-01-20 17:59:50 -0800542 else {
543 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
544 return 0;
545 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800546 }
Lee Thomason67d61312012-01-24 16:01:51 -0800547 return p;
548}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800549
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800550
Lee Thomason67d61312012-01-24 16:01:51 -0800551//
552// <ele></ele>
553// <ele>foo<b>bar</b></ele>
554//
555char* XMLElement::ParseDeep( char* p )
556{
557 // Read the element name.
Lee Thomason56bdd022012-02-09 18:16:58 -0800558 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason67d61312012-01-24 16:01:51 -0800559 if ( !p ) return 0;
560 const char* start = p;
561
562 // The closing element is the </element> form. It is
563 // parsed just like a regular element then deleted from
564 // the DOM.
565 if ( *p == '/' ) {
566 closing = true;
567 ++p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800568 }
Lee Thomason67d61312012-01-24 16:01:51 -0800569
Lee Thomason56bdd022012-02-09 18:16:58 -0800570 p = value.ParseName( p );
Lee Thomasond1983222012-02-06 08:41:24 -0800571 if ( value.Empty() ) return 0;
Lee Thomason67d61312012-01-24 16:01:51 -0800572
573 bool elementClosed=false;
574 p = ParseAttributes( p, &elementClosed );
575 if ( !p || !*p || elementClosed || closing )
576 return p;
577
578 p = XMLNode::ParseDeep( p );
579 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800580}
581
582
Lee Thomason5cae8972012-01-24 18:03:07 -0800583void XMLElement::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800584{
Lee Thomason5cae8972012-01-24 18:03:07 -0800585 //if ( !parent || !parent->IsTextParent() ) {
586 // PrintSpace( cfile, depth );
587 //}
588 //fprintf( cfile, "<%s", Name() );
Lee Thomason56bdd022012-02-09 18:16:58 -0800589 streamer->OpenElement( Name() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800590
591 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800592 //fprintf( cfile, " " );
593 attrib->Print( streamer );
594
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800595 }
596
Lee Thomason5cae8972012-01-24 18:03:07 -0800597 for( XMLNode* node=firstChild; node; node=node->next ) {
598 node->Print( streamer );
599 }
600 streamer->CloseElement();
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800601}
602
603
Lee Thomason56bdd022012-02-09 18:16:58 -0800604bool XMLElement::Accept( XMLVisitor* visitor ) const
605{
606 if ( visitor->VisitEnter( *this, rootAttribute ) )
607 {
608 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
609 {
610 if ( !node->Accept( visitor ) )
611 break;
612 }
613 }
614 return visitor->VisitExit( *this );
615
616}
617
618
Lee Thomason3f57d272012-01-11 15:30:03 -0800619// --------- XMLDocument ----------- //
Lee Thomason67d61312012-01-24 16:01:51 -0800620XMLDocument::XMLDocument() :
Lee Thomason18d68bd2012-01-26 18:17:26 -0800621 XMLNode( 0 ),
U-Lama\Lee560bd472011-12-28 19:42:49 -0800622 charBuffer( 0 )
623{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800624 document = this; // avoid warning about 'this' in initializer list
U-Lama\Lee560bd472011-12-28 19:42:49 -0800625}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800626
627
Lee Thomason3f57d272012-01-11 15:30:03 -0800628XMLDocument::~XMLDocument()
629{
Lee Thomasond1983222012-02-06 08:41:24 -0800630 ClearChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -0800631 delete [] charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -0800632
Lee Thomason455c9d42012-02-06 09:14:14 -0800633 /*
634 textPool.Trace( "text" );
635 elementPool.Trace( "element" );
636 commentPool.Trace( "comment" );
637 attributePool.Trace( "attribute" );
638 */
639 TIXMLASSERT( textPool.CurrentAllocs() == 0 );
640 TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
641 TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
642 TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
Lee Thomason3f57d272012-01-11 15:30:03 -0800643}
644
645
Lee Thomason18d68bd2012-01-26 18:17:26 -0800646void XMLDocument::InitDocument()
647{
648 errorID = NO_ERROR;
649 errorStr1 = 0;
650 errorStr2 = 0;
651
652 delete [] charBuffer;
653 charBuffer = 0;
654
655}
656
Lee Thomason3f57d272012-01-11 15:30:03 -0800657
Lee Thomason2c85a712012-01-31 08:24:24 -0800658XMLElement* XMLDocument::NewElement( const char* name )
659{
Lee Thomasond1983222012-02-06 08:41:24 -0800660 XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
661 ele->memPool = &elementPool;
Lee Thomason2c85a712012-01-31 08:24:24 -0800662 ele->SetName( name );
663 return ele;
664}
665
666
Lee Thomason7c913cd2012-01-26 18:32:34 -0800667int XMLDocument::Parse( const char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800668{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800669 ClearChildren();
670 InitDocument();
671
672 if ( !p || !*p ) {
673 return true; // correctly parse an empty string?
674 }
675 size_t len = strlen( p );
676 charBuffer = new char[ len+1 ];
677 memcpy( charBuffer, p, len+1 );
Lee Thomason3f57d272012-01-11 15:30:03 -0800678 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800679
Lee Thomason18d68bd2012-01-26 18:17:26 -0800680 char* q = ParseDeep( charBuffer );
Lee Thomason7c913cd2012-01-26 18:32:34 -0800681 return errorID;
Lee Thomason3f57d272012-01-11 15:30:03 -0800682}
683
684
Lee Thomason5cae8972012-01-24 18:03:07 -0800685void XMLDocument::Print( XMLStreamer* streamer )
Lee Thomason3f57d272012-01-11 15:30:03 -0800686{
Lee Thomason5cae8972012-01-24 18:03:07 -0800687 XMLStreamer stdStreamer( stdout );
688 if ( !streamer )
689 streamer = &stdStreamer;
Lee Thomason67d61312012-01-24 16:01:51 -0800690 for( XMLNode* node = firstChild; node; node=node->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800691 node->Print( streamer );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800692 }
Lee Thomason3f57d272012-01-11 15:30:03 -0800693}
694
695
Lee Thomason67d61312012-01-24 16:01:51 -0800696void XMLDocument::SetError( int error, const char* str1, const char* str2 )
697{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800698 errorID = error;
699 printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 ); // fixme: remove
700 errorStr1 = str1;
701 errorStr2 = str2;
Lee Thomason67d61312012-01-24 16:01:51 -0800702}
703
Lee Thomason5cae8972012-01-24 18:03:07 -0800704
Lee Thomason2c85a712012-01-31 08:24:24 -0800705/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800706StringStack::StringStack()
707{
Lee Thomason5cae8972012-01-24 18:03:07 -0800708 nPositive = 0;
Lee Thomason1270ae52012-01-27 17:58:30 -0800709 mem.Push( 0 ); // start with null. makes later code simpler.
Lee Thomason5cae8972012-01-24 18:03:07 -0800710}
711
712
Lee Thomason24767b02012-01-25 17:16:23 -0800713StringStack::~StringStack()
714{
Lee Thomason24767b02012-01-25 17:16:23 -0800715}
716
717
Lee Thomason5cae8972012-01-24 18:03:07 -0800718void StringStack::Push( const char* str ) {
719 int needed = strlen( str ) + 1;
Lee Thomason1270ae52012-01-27 17:58:30 -0800720 char* p = mem.PushArr( needed );
721 strcpy( p, str );
722 if ( needed > 1 )
Lee Thomason5cae8972012-01-24 18:03:07 -0800723 nPositive++;
Lee Thomason5cae8972012-01-24 18:03:07 -0800724}
725
726
727const char* StringStack::Pop() {
Lee Thomason1270ae52012-01-27 17:58:30 -0800728 TIXMLASSERT( mem.Size() > 1 );
729 const char* p = mem.Mem() + mem.Size() - 2; // end of final string.
Lee Thomason5cae8972012-01-24 18:03:07 -0800730 if ( *p ) {
731 nPositive--;
732 }
733 while( *p ) { // stack starts with a null, don't need to check for 'mem'
Lee Thomason1270ae52012-01-27 17:58:30 -0800734 TIXMLASSERT( p > mem.Mem() );
Lee Thomason5cae8972012-01-24 18:03:07 -0800735 --p;
736 }
Lee Thomason1270ae52012-01-27 17:58:30 -0800737 mem.PopArr( strlen(p)+1 );
Lee Thomason5cae8972012-01-24 18:03:07 -0800738 return p+1;
739}
Lee Thomason2c85a712012-01-31 08:24:24 -0800740*/
Lee Thomason5cae8972012-01-24 18:03:07 -0800741
742
Lee Thomason56bdd022012-02-09 18:16:58 -0800743XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false ), textDepth( -1 )
Lee Thomason5cae8972012-01-24 18:03:07 -0800744{
Lee Thomason857b8682012-01-25 17:50:25 -0800745 for( int i=0; i<ENTITY_RANGE; ++i ) {
746 entityFlag[i] = false;
747 }
748 for( int i=0; i<NUM_ENTITIES; ++i ) {
749 TIXMLASSERT( entities[i].value < ENTITY_RANGE );
750 if ( entities[i].value < ENTITY_RANGE ) {
751 entityFlag[ entities[i].value ] = true;
752 }
753 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800754}
755
756
757void XMLStreamer::PrintSpace( int depth )
758{
759 for( int i=0; i<depth; ++i ) {
760 fprintf( fp, " " );
761 }
762}
763
764
Lee Thomason951d8832012-01-26 08:47:06 -0800765void XMLStreamer::PrintString( const char* p )
Lee Thomason857b8682012-01-25 17:50:25 -0800766{
Lee Thomason951d8832012-01-26 08:47:06 -0800767 // Look for runs of bytes between entities to print.
768 const char* q = p;
Lee Thomason857b8682012-01-25 17:50:25 -0800769
Lee Thomason951d8832012-01-26 08:47:06 -0800770 while ( *q ) {
771 if ( *q < ENTITY_RANGE ) {
772 // Check for entities. If one is found, flush
773 // the stream up until the entity, write the
774 // entity, and keep looking.
775 if ( entityFlag[*q] ) {
776 while ( p < q ) {
777 fputc( *p, fp );
778 ++p;
779 }
780 for( int i=0; i<NUM_ENTITIES; ++i ) {
781 if ( entities[i].value == *q ) {
782 fprintf( fp, "&%s;", entities[i].pattern );
783 break;
784 }
785 }
786 ++p;
787 }
788 }
789 ++q;
790 }
791 // Flush the remaining string. This will be the entire
792 // string if an entity wasn't found.
793 if ( q-p > 0 ) {
794 fprintf( fp, "%s", p );
795 }
Lee Thomason857b8682012-01-25 17:50:25 -0800796}
797
Lee Thomason56bdd022012-02-09 18:16:58 -0800798void XMLStreamer::OpenElement( const char* name )
Lee Thomason5cae8972012-01-24 18:03:07 -0800799{
800 if ( elementJustOpened ) {
801 SealElement();
802 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800803 stack.Push( name );
804
805 if ( textDepth < 0 && depth > 0) {
806 fprintf( fp, "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -0800807 PrintSpace( depth );
808 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800809
Lee Thomason5cae8972012-01-24 18:03:07 -0800810 fprintf( fp, "<%s", name );
811 elementJustOpened = true;
812 ++depth;
813}
814
815
816void XMLStreamer::PushAttribute( const char* name, const char* value )
817{
818 TIXMLASSERT( elementJustOpened );
Lee Thomason18d68bd2012-01-26 18:17:26 -0800819 fprintf( fp, " %s=\"", name );
820 PrintString( value );
821 fprintf( fp, "\"" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800822}
823
824
825void XMLStreamer::CloseElement()
826{
827 --depth;
828 const char* name = stack.Pop();
Lee Thomason5cae8972012-01-24 18:03:07 -0800829
830 if ( elementJustOpened ) {
831 fprintf( fp, "/>" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800832 }
833 else {
Lee Thomason56bdd022012-02-09 18:16:58 -0800834 if ( textDepth < 0 ) {
835 fprintf( fp, "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -0800836 PrintSpace( depth );
837 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800838 fprintf( fp, "</%s>", name );
Lee Thomason5cae8972012-01-24 18:03:07 -0800839 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800840
841 if ( textDepth == depth )
842 textDepth = -1;
843 if ( depth == 0 )
844 fprintf( fp, "\n" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800845 elementJustOpened = false;
846}
847
848
849void XMLStreamer::SealElement()
850{
851 elementJustOpened = false;
852 fprintf( fp, ">" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800853}
854
855
856void XMLStreamer::PushText( const char* text )
857{
Lee Thomason56bdd022012-02-09 18:16:58 -0800858 textDepth = depth-1;
859
Lee Thomason5cae8972012-01-24 18:03:07 -0800860 if ( elementJustOpened ) {
861 SealElement();
862 }
Lee Thomason951d8832012-01-26 08:47:06 -0800863 PrintString( text );
Lee Thomason5cae8972012-01-24 18:03:07 -0800864}
865
866
867void XMLStreamer::PushComment( const char* comment )
868{
869 if ( elementJustOpened ) {
870 SealElement();
871 }
872 PrintSpace( depth );
873 fprintf( fp, "<!--%s-->\n", comment );
874}