blob: c860c07a71a3d3849d746f10813f55476f71c44b [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 Thomason751da522012-02-10 08:50:51 -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}
Lee Thomason56bdd022012-02-09 18:16:58 -0800250
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 Thomason751da522012-02-10 08:50:51 -0800366/*void 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}
Lee Thomason751da522012-02-10 08:50:51 -0800372*/
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800373
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800374
Lee Thomason67d61312012-01-24 16:01:51 -0800375char* XMLNode::ParseDeep( char* p )
376{
377 while( p && *p ) {
378 XMLNode* node = 0;
Lee Thomasond1983222012-02-06 08:41:24 -0800379 p = document->Identify( p, &node );
Lee Thomason67d61312012-01-24 16:01:51 -0800380 if ( p && node ) {
381 p = node->ParseDeep( p );
382 // FIXME: is it the correct closing element?
383 if ( node->IsClosingElement() ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800384 DELETE_NODE( node );
Lee Thomason67d61312012-01-24 16:01:51 -0800385 return p;
386 }
387 this->InsertEndChild( node );
388 }
389 }
390 return 0;
391}
392
Lee Thomason5492a1c2012-01-23 15:32:10 -0800393// --------- XMLText ---------- //
394char* XMLText::ParseDeep( char* p )
395{
Lee Thomason56bdd022012-02-09 18:16:58 -0800396 p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800397 // consumes the end tag.
398 if ( p && *p ) {
399 return p-1;
400 }
401 return 0;
402}
403
404
Lee Thomason751da522012-02-10 08:50:51 -0800405/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800406void XMLText::Print( XMLStreamer* streamer )
Lee Thomason5492a1c2012-01-23 15:32:10 -0800407{
Lee Thomason67d61312012-01-24 16:01:51 -0800408 const char* v = value.GetStr();
Lee Thomason5cae8972012-01-24 18:03:07 -0800409 streamer->PushText( v );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800410}
Lee Thomason751da522012-02-10 08:50:51 -0800411*/
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800412
413
Lee Thomason56bdd022012-02-09 18:16:58 -0800414bool XMLText::Accept( XMLVisitor* visitor ) const
415{
416 return visitor->Visit( *this );
417}
418
419
Lee Thomason3f57d272012-01-11 15:30:03 -0800420// --------- XMLComment ---------- //
421
Lee Thomasone4422302012-01-20 17:59:50 -0800422XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800423{
424}
425
426
Lee Thomasonce0763e2012-01-11 15:43:54 -0800427XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800428{
Lee Thomasond923c672012-01-23 08:44:25 -0800429 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800430}
431
432
Lee Thomason751da522012-02-10 08:50:51 -0800433/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800434void XMLComment::Print( XMLStreamer* streamer )
Lee Thomasonce0763e2012-01-11 15:43:54 -0800435{
Lee Thomason5cae8972012-01-24 18:03:07 -0800436// XMLNode::Print( fp, depth );
437// fprintf( fp, "<!--%s-->\n", value.GetStr() );
438 streamer->PushComment( value.GetStr() );
Lee Thomasonce0763e2012-01-11 15:43:54 -0800439}
Lee Thomason751da522012-02-10 08:50:51 -0800440*/
Lee Thomasonce0763e2012-01-11 15:43:54 -0800441
442
443char* XMLComment::ParseDeep( char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800444{
445 // Comment parses as text.
Lee Thomason56bdd022012-02-09 18:16:58 -0800446 return value.ParseText( p, "-->", StrPair::COMMENT );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800447}
448
449
Lee Thomason751da522012-02-10 08:50:51 -0800450bool XMLComment::Accept( XMLVisitor* visitor ) const
451{
452 return visitor->Visit( *this );
453}
Lee Thomason56bdd022012-02-09 18:16:58 -0800454
455
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800456// --------- XMLAttribute ---------- //
457char* XMLAttribute::ParseDeep( char* p )
458{
Lee Thomason56bdd022012-02-09 18:16:58 -0800459 p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
Lee Thomason22aead12012-01-23 13:29:35 -0800460 if ( !p || !*p ) return 0;
461
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800462 char endTag[2] = { *p, 0 };
463 ++p;
Lee Thomason56bdd022012-02-09 18:16:58 -0800464 p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
Lee Thomasone4422302012-01-20 17:59:50 -0800465 if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800466 return p;
467}
468
469
Lee Thomason751da522012-02-10 08:50:51 -0800470/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800471void XMLAttribute::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800472{
Lee Thomason22aead12012-01-23 13:29:35 -0800473 // fixme: sort out single vs. double quote
Lee Thomason5cae8972012-01-24 18:03:07 -0800474 //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
475 streamer->PushAttribute( name.GetStr(), value.GetStr() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800476}
Lee Thomason751da522012-02-10 08:50:51 -0800477*/
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800478
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800479// --------- XMLElement ---------- //
480XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800481 closing( false ),
482 rootAttribute( 0 ),
483 lastAttribute( 0 )
484{
485}
486
487
488XMLElement::~XMLElement()
489{
Lee Thomasond923c672012-01-23 08:44:25 -0800490 //printf( "~XMLElemen %x\n",this );
491
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800492 XMLAttribute* attribute = rootAttribute;
493 while( attribute ) {
494 XMLAttribute* next = attribute->next;
Lee Thomason43f59302012-02-06 18:18:11 -0800495 DELETE_ATTRIBUTE( attribute );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800496 attribute = next;
497 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800498}
499
500
Lee Thomason67d61312012-01-24 16:01:51 -0800501char* XMLElement::ParseAttributes( char* p, bool* closedElement )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800502{
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800503 const char* start = p;
Lee Thomason67d61312012-01-24 16:01:51 -0800504 *closedElement = false;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800505
506 // Read the attributes.
507 while( p ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800508 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800509 if ( !p || !(*p) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800510 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800511 return 0;
512 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800513
514 // attribute.
Lee Thomason56bdd022012-02-09 18:16:58 -0800515 if ( XMLUtil::IsAlpha( *p ) ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800516 XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
517 attrib->memPool = &document->attributePool;
Lee Thomasond1983222012-02-06 08:41:24 -0800518
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800519 p = attrib->ParseDeep( p );
520 if ( !p ) {
Lee Thomason43f59302012-02-06 18:18:11 -0800521 DELETE_ATTRIBUTE( attrib );
Lee Thomasone4422302012-01-20 17:59:50 -0800522 document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800523 return 0;
524 }
525 if ( rootAttribute ) {
526 TIXMLASSERT( lastAttribute );
527 lastAttribute->next = attrib;
528 lastAttribute = attrib;
529 }
530 else {
531 rootAttribute = lastAttribute = attrib;
532 }
533 }
Lee Thomasone4422302012-01-20 17:59:50 -0800534 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800535 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800536 if ( closing ) {
537 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
538 return 0;
539 }
Lee Thomason67d61312012-01-24 16:01:51 -0800540 *closedElement = true;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800541 return p+2; // done; sealed element.
542 }
Lee Thomasone4422302012-01-20 17:59:50 -0800543 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800544 else if ( *p == '>' ) {
545 ++p;
546 break;
547 }
Lee Thomasone4422302012-01-20 17:59:50 -0800548 else {
549 document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
550 return 0;
551 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800552 }
Lee Thomason67d61312012-01-24 16:01:51 -0800553 return p;
554}
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800555
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800556
Lee Thomason67d61312012-01-24 16:01:51 -0800557//
558// <ele></ele>
559// <ele>foo<b>bar</b></ele>
560//
561char* XMLElement::ParseDeep( char* p )
562{
563 // Read the element name.
Lee Thomason56bdd022012-02-09 18:16:58 -0800564 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason67d61312012-01-24 16:01:51 -0800565 if ( !p ) return 0;
566 const char* start = p;
567
568 // The closing element is the </element> form. It is
569 // parsed just like a regular element then deleted from
570 // the DOM.
571 if ( *p == '/' ) {
572 closing = true;
573 ++p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800574 }
Lee Thomason67d61312012-01-24 16:01:51 -0800575
Lee Thomason56bdd022012-02-09 18:16:58 -0800576 p = value.ParseName( p );
Lee Thomasond1983222012-02-06 08:41:24 -0800577 if ( value.Empty() ) return 0;
Lee Thomason67d61312012-01-24 16:01:51 -0800578
579 bool elementClosed=false;
580 p = ParseAttributes( p, &elementClosed );
581 if ( !p || !*p || elementClosed || closing )
582 return p;
583
584 p = XMLNode::ParseDeep( p );
585 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800586}
587
588
Lee Thomason751da522012-02-10 08:50:51 -0800589/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800590void XMLElement::Print( XMLStreamer* streamer )
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800591{
Lee Thomason5cae8972012-01-24 18:03:07 -0800592 //if ( !parent || !parent->IsTextParent() ) {
593 // PrintSpace( cfile, depth );
594 //}
595 //fprintf( cfile, "<%s", Name() );
Lee Thomason56bdd022012-02-09 18:16:58 -0800596 streamer->OpenElement( Name() );
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800597
598 for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
Lee Thomason5cae8972012-01-24 18:03:07 -0800599 //fprintf( cfile, " " );
600 attrib->Print( streamer );
601
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800602 }
603
Lee Thomason5cae8972012-01-24 18:03:07 -0800604 for( XMLNode* node=firstChild; node; node=node->next ) {
605 node->Print( streamer );
606 }
607 streamer->CloseElement();
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800608}
Lee Thomason751da522012-02-10 08:50:51 -0800609*/
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800610
611
Lee Thomason751da522012-02-10 08:50:51 -0800612bool XMLElement::Accept( XMLVisitor* visitor ) const
613{
614 if ( visitor->VisitEnter( *this, rootAttribute ) )
615 {
616 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
617 {
618 if ( !node->Accept( visitor ) )
619 break;
620 }
621 }
622 return visitor->VisitExit( *this );
623
624}
Lee Thomason56bdd022012-02-09 18:16:58 -0800625
626
Lee Thomason3f57d272012-01-11 15:30:03 -0800627// --------- XMLDocument ----------- //
Lee Thomason67d61312012-01-24 16:01:51 -0800628XMLDocument::XMLDocument() :
Lee Thomason18d68bd2012-01-26 18:17:26 -0800629 XMLNode( 0 ),
U-Lama\Lee560bd472011-12-28 19:42:49 -0800630 charBuffer( 0 )
631{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800632 document = this; // avoid warning about 'this' in initializer list
U-Lama\Lee560bd472011-12-28 19:42:49 -0800633}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800634
635
Lee Thomason3f57d272012-01-11 15:30:03 -0800636XMLDocument::~XMLDocument()
637{
Lee Thomasond1983222012-02-06 08:41:24 -0800638 ClearChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -0800639 delete [] charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -0800640
Lee Thomason455c9d42012-02-06 09:14:14 -0800641 /*
642 textPool.Trace( "text" );
643 elementPool.Trace( "element" );
644 commentPool.Trace( "comment" );
645 attributePool.Trace( "attribute" );
646 */
647 TIXMLASSERT( textPool.CurrentAllocs() == 0 );
648 TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
649 TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
650 TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
Lee Thomason3f57d272012-01-11 15:30:03 -0800651}
652
653
Lee Thomason18d68bd2012-01-26 18:17:26 -0800654void XMLDocument::InitDocument()
655{
656 errorID = NO_ERROR;
657 errorStr1 = 0;
658 errorStr2 = 0;
659
660 delete [] charBuffer;
661 charBuffer = 0;
662
663}
664
Lee Thomason3f57d272012-01-11 15:30:03 -0800665
Lee Thomason2c85a712012-01-31 08:24:24 -0800666XMLElement* XMLDocument::NewElement( const char* name )
667{
Lee Thomasond1983222012-02-06 08:41:24 -0800668 XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
669 ele->memPool = &elementPool;
Lee Thomason2c85a712012-01-31 08:24:24 -0800670 ele->SetName( name );
671 return ele;
672}
673
674
Lee Thomason7c913cd2012-01-26 18:32:34 -0800675int XMLDocument::Parse( const char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -0800676{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800677 ClearChildren();
678 InitDocument();
679
680 if ( !p || !*p ) {
681 return true; // correctly parse an empty string?
682 }
683 size_t len = strlen( p );
684 charBuffer = new char[ len+1 ];
685 memcpy( charBuffer, p, len+1 );
Lee Thomason3f57d272012-01-11 15:30:03 -0800686 XMLNode* node = 0;
Lee Thomason85403d82012-01-11 15:55:05 -0800687
Lee Thomason18d68bd2012-01-26 18:17:26 -0800688 char* q = ParseDeep( charBuffer );
Lee Thomason7c913cd2012-01-26 18:32:34 -0800689 return errorID;
Lee Thomason3f57d272012-01-11 15:30:03 -0800690}
691
692
Lee Thomason5cae8972012-01-24 18:03:07 -0800693void XMLDocument::Print( XMLStreamer* streamer )
Lee Thomason3f57d272012-01-11 15:30:03 -0800694{
Lee Thomason5cae8972012-01-24 18:03:07 -0800695 XMLStreamer stdStreamer( stdout );
696 if ( !streamer )
697 streamer = &stdStreamer;
Lee Thomason751da522012-02-10 08:50:51 -0800698 //for( XMLNode* node = firstChild; node; node=node->next ) {
699 // node->Print( streamer );
700 //}
701 Accept( streamer );
Lee Thomason3f57d272012-01-11 15:30:03 -0800702}
703
704
Lee Thomason67d61312012-01-24 16:01:51 -0800705void XMLDocument::SetError( int error, const char* str1, const char* str2 )
706{
Lee Thomason18d68bd2012-01-26 18:17:26 -0800707 errorID = error;
708 printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 ); // fixme: remove
709 errorStr1 = str1;
710 errorStr2 = str2;
Lee Thomason67d61312012-01-24 16:01:51 -0800711}
712
Lee Thomason5cae8972012-01-24 18:03:07 -0800713
Lee Thomason2c85a712012-01-31 08:24:24 -0800714/*
Lee Thomason5cae8972012-01-24 18:03:07 -0800715StringStack::StringStack()
716{
Lee Thomason5cae8972012-01-24 18:03:07 -0800717 nPositive = 0;
Lee Thomason1270ae52012-01-27 17:58:30 -0800718 mem.Push( 0 ); // start with null. makes later code simpler.
Lee Thomason5cae8972012-01-24 18:03:07 -0800719}
720
721
Lee Thomason24767b02012-01-25 17:16:23 -0800722StringStack::~StringStack()
723{
Lee Thomason24767b02012-01-25 17:16:23 -0800724}
725
726
Lee Thomason5cae8972012-01-24 18:03:07 -0800727void StringStack::Push( const char* str ) {
728 int needed = strlen( str ) + 1;
Lee Thomason1270ae52012-01-27 17:58:30 -0800729 char* p = mem.PushArr( needed );
730 strcpy( p, str );
731 if ( needed > 1 )
Lee Thomason5cae8972012-01-24 18:03:07 -0800732 nPositive++;
Lee Thomason5cae8972012-01-24 18:03:07 -0800733}
734
735
736const char* StringStack::Pop() {
Lee Thomason1270ae52012-01-27 17:58:30 -0800737 TIXMLASSERT( mem.Size() > 1 );
738 const char* p = mem.Mem() + mem.Size() - 2; // end of final string.
Lee Thomason5cae8972012-01-24 18:03:07 -0800739 if ( *p ) {
740 nPositive--;
741 }
742 while( *p ) { // stack starts with a null, don't need to check for 'mem'
Lee Thomason1270ae52012-01-27 17:58:30 -0800743 TIXMLASSERT( p > mem.Mem() );
Lee Thomason5cae8972012-01-24 18:03:07 -0800744 --p;
745 }
Lee Thomason1270ae52012-01-27 17:58:30 -0800746 mem.PopArr( strlen(p)+1 );
Lee Thomason5cae8972012-01-24 18:03:07 -0800747 return p+1;
748}
Lee Thomason2c85a712012-01-31 08:24:24 -0800749*/
Lee Thomason5cae8972012-01-24 18:03:07 -0800750
751
Lee Thomason56bdd022012-02-09 18:16:58 -0800752XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false ), textDepth( -1 )
Lee Thomason5cae8972012-01-24 18:03:07 -0800753{
Lee Thomason857b8682012-01-25 17:50:25 -0800754 for( int i=0; i<ENTITY_RANGE; ++i ) {
755 entityFlag[i] = false;
756 }
757 for( int i=0; i<NUM_ENTITIES; ++i ) {
758 TIXMLASSERT( entities[i].value < ENTITY_RANGE );
759 if ( entities[i].value < ENTITY_RANGE ) {
760 entityFlag[ entities[i].value ] = true;
761 }
762 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800763}
764
765
766void XMLStreamer::PrintSpace( int depth )
767{
768 for( int i=0; i<depth; ++i ) {
769 fprintf( fp, " " );
770 }
771}
772
773
Lee Thomason951d8832012-01-26 08:47:06 -0800774void XMLStreamer::PrintString( const char* p )
Lee Thomason857b8682012-01-25 17:50:25 -0800775{
Lee Thomason951d8832012-01-26 08:47:06 -0800776 // Look for runs of bytes between entities to print.
777 const char* q = p;
Lee Thomason857b8682012-01-25 17:50:25 -0800778
Lee Thomason951d8832012-01-26 08:47:06 -0800779 while ( *q ) {
780 if ( *q < ENTITY_RANGE ) {
781 // Check for entities. If one is found, flush
782 // the stream up until the entity, write the
783 // entity, and keep looking.
784 if ( entityFlag[*q] ) {
785 while ( p < q ) {
786 fputc( *p, fp );
787 ++p;
788 }
789 for( int i=0; i<NUM_ENTITIES; ++i ) {
790 if ( entities[i].value == *q ) {
791 fprintf( fp, "&%s;", entities[i].pattern );
792 break;
793 }
794 }
795 ++p;
796 }
797 }
798 ++q;
799 }
800 // Flush the remaining string. This will be the entire
801 // string if an entity wasn't found.
802 if ( q-p > 0 ) {
803 fprintf( fp, "%s", p );
804 }
Lee Thomason857b8682012-01-25 17:50:25 -0800805}
806
Lee Thomason56bdd022012-02-09 18:16:58 -0800807void XMLStreamer::OpenElement( const char* name )
Lee Thomason5cae8972012-01-24 18:03:07 -0800808{
809 if ( elementJustOpened ) {
810 SealElement();
811 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800812 stack.Push( name );
813
814 if ( textDepth < 0 && depth > 0) {
815 fprintf( fp, "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -0800816 PrintSpace( depth );
817 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800818
Lee Thomason5cae8972012-01-24 18:03:07 -0800819 fprintf( fp, "<%s", name );
820 elementJustOpened = true;
821 ++depth;
822}
823
824
825void XMLStreamer::PushAttribute( const char* name, const char* value )
826{
827 TIXMLASSERT( elementJustOpened );
Lee Thomason18d68bd2012-01-26 18:17:26 -0800828 fprintf( fp, " %s=\"", name );
829 PrintString( value );
830 fprintf( fp, "\"" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800831}
832
833
834void XMLStreamer::CloseElement()
835{
836 --depth;
837 const char* name = stack.Pop();
Lee Thomason5cae8972012-01-24 18:03:07 -0800838
839 if ( elementJustOpened ) {
840 fprintf( fp, "/>" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800841 }
842 else {
Lee Thomason56bdd022012-02-09 18:16:58 -0800843 if ( textDepth < 0 ) {
844 fprintf( fp, "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -0800845 PrintSpace( depth );
846 }
Lee Thomason5cae8972012-01-24 18:03:07 -0800847 fprintf( fp, "</%s>", name );
Lee Thomason5cae8972012-01-24 18:03:07 -0800848 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800849
850 if ( textDepth == depth )
851 textDepth = -1;
852 if ( depth == 0 )
853 fprintf( fp, "\n" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800854 elementJustOpened = false;
855}
856
857
858void XMLStreamer::SealElement()
859{
860 elementJustOpened = false;
861 fprintf( fp, ">" );
Lee Thomason5cae8972012-01-24 18:03:07 -0800862}
863
864
865void XMLStreamer::PushText( const char* text )
866{
Lee Thomason56bdd022012-02-09 18:16:58 -0800867 textDepth = depth-1;
868
Lee Thomason5cae8972012-01-24 18:03:07 -0800869 if ( elementJustOpened ) {
870 SealElement();
871 }
Lee Thomason951d8832012-01-26 08:47:06 -0800872 PrintString( text );
Lee Thomason5cae8972012-01-24 18:03:07 -0800873}
874
875
876void XMLStreamer::PushComment( const char* comment )
877{
878 if ( elementJustOpened ) {
879 SealElement();
880 }
881 PrintSpace( depth );
882 fprintf( fp, "<!--%s-->\n", comment );
883}
Lee Thomason751da522012-02-10 08:50:51 -0800884
885
886bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
887{
888 OpenElement( element.Name() );
889 while ( attribute ) {
890 PushAttribute( attribute->Name(), attribute->Value() );
891 attribute = attribute->Next();
892 }
893 return true;
894}
895
896
897bool XMLStreamer::VisitExit( const XMLElement& element )
898{
899 CloseElement();
900 return true;
901}
902
903
904bool XMLStreamer::Visit( const XMLText& text )
905{
906 PushText( text.Value() );
907 return true;
908}
909
910
911bool XMLStreamer::Visit( const XMLComment& comment )
912{
913 PushComment( comment.Value() );
914 return true;
915}