blob: e4ac9771807a75e82f60f380efd352da31d5739d [file] [log] [blame]
Lee Thomason (grinliz)28129862012-02-25 21:11:20 -08001/*
2Original code by Lee Thomason (www.grinninglizard.com)
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any
6damages arising from the use of this software.
7
8Permission is granted to anyone to use this software for any
9purpose, including commercial applications, and to alter it and
10redistribute it freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must
13not claim that you wrote the original software. If you use this
14software in a product, an acknowledgment in the product documentation
15would be appreciated but is not required.
16
172. Altered source versions must be plainly marked as such, and
18must not be misrepresented as being the original software.
19
203. This notice may not be removed or altered from any source
21distribution.
22*/
Lee Thomason (grinliz)9c38d132012-02-24 21:50:50 -080023
U-Lama\Lee560bd472011-12-28 19:42:49 -080024#include "tinyxml2.h"
25
Lee Thomasonee87c622012-05-14 09:27:47 -070026#include <cstdio>
27#include <cstdlib>
28#include <new>
29#include <cstddef>
U-Lama\Lee560bd472011-12-28 19:42:49 -080030
31using namespace tinyxml2;
32
Lee Thomasone4422302012-01-20 17:59:50 -080033static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
Lee Thomasonfde6a752012-01-14 18:08:12 -080034static const char LF = LINE_FEED;
35static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
36static const char CR = CARRIAGE_RETURN;
Lee Thomasone4422302012-01-20 17:59:50 -080037static const char SINGLE_QUOTE = '\'';
38static const char DOUBLE_QUOTE = '\"';
Lee Thomasonfde6a752012-01-14 18:08:12 -080039
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -080040// Bunch of unicode info at:
41// http://www.unicode.org/faq/utf_bom.html
42// ef bb bf (Microsoft "lead bytes") - designates UTF-8
43
44static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
45static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
46static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -080047
48
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -080049#define DELETE_NODE( node ) { \
50 if ( node ) { \
51 MemPool* pool = node->memPool; \
52 node->~XMLNode(); \
53 pool->Free( node ); \
54 } \
55}
56#define DELETE_ATTRIBUTE( attrib ) { \
57 if ( attrib ) { \
58 MemPool* pool = attrib->memPool; \
59 attrib->~XMLAttribute(); \
60 pool->Free( attrib ); \
61 } \
62}
Lee Thomason43f59302012-02-06 18:18:11 -080063
Lee Thomason8ee79892012-01-25 17:44:30 -080064struct Entity {
65 const char* pattern;
66 int length;
67 char value;
68};
69
70static const int NUM_ENTITIES = 5;
71static const Entity entities[NUM_ENTITIES] =
72{
Lee Thomason18d68bd2012-01-26 18:17:26 -080073 { "quot", 4, DOUBLE_QUOTE },
Lee Thomason8ee79892012-01-25 17:44:30 -080074 { "amp", 3, '&' },
Lee Thomason18d68bd2012-01-26 18:17:26 -080075 { "apos", 4, SINGLE_QUOTE },
Lee Thomason8ee79892012-01-25 17:44:30 -080076 { "lt", 2, '<' },
77 { "gt", 2, '>' }
78};
79
Lee Thomasonfde6a752012-01-14 18:08:12 -080080
Lee Thomason1a1d4a72012-02-15 09:09:25 -080081StrPair::~StrPair()
82{
83 Reset();
84}
85
86
87void StrPair::Reset()
88{
89 if ( flags & NEEDS_DELETE ) {
90 delete [] start;
91 }
92 flags = 0;
93 start = 0;
94 end = 0;
95}
96
97
98void StrPair::SetStr( const char* str, int flags )
99{
100 Reset();
101 size_t len = strlen( str );
102 start = new char[ len+1 ];
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800103 memcpy( start, str, len+1 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800104 end = start + len;
105 this->flags = flags | NEEDS_DELETE;
106}
107
108
109char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
110{
111 TIXMLASSERT( endTag && *endTag );
112
113 char* start = p; // fixme: hides a member
114 char endChar = *endTag;
Thomas Roß7d7a9a32012-05-10 00:23:19 +0200115 size_t length = strlen( endTag );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800116
117 // Inner loop of text parsing.
118 while ( *p ) {
119 if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
120 Set( start, p, strFlags );
121 return p + length;
122 }
123 ++p;
124 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800125 return 0;
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800126}
127
128
129char* StrPair::ParseName( char* p )
130{
131 char* start = p;
132
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800133 if ( !start || !(*start) ) {
134 return 0;
135 }
136
137 if ( !XMLUtil::IsAlpha( *p ) ) {
138 return 0;
139 }
140
141 while( *p && (
142 XMLUtil::IsAlphaNum( (unsigned char) *p )
143 || *p == '_'
144 || *p == '-'
145 || *p == '.'
146 || *p == ':' ))
147 {
148 ++p;
149 }
150
151 if ( p > start ) {
152 Set( start, p, 0 );
153 return p;
154 }
155 return 0;
156}
157
158
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800159
Lee Thomasone4422302012-01-20 17:59:50 -0800160const char* StrPair::GetStr()
161{
162 if ( flags & NEEDS_FLUSH ) {
163 *end = 0;
Lee Thomason8ee79892012-01-25 17:44:30 -0800164 flags ^= NEEDS_FLUSH;
Lee Thomasone4422302012-01-20 17:59:50 -0800165
Lee Thomason8ee79892012-01-25 17:44:30 -0800166 if ( flags ) {
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800167 char* p = start; // the read pointer
168 char* q = start; // the write pointer
Lee Thomasone4422302012-01-20 17:59:50 -0800169
170 while( p < end ) {
Lee Thomason8ee79892012-01-25 17:44:30 -0800171 if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800172 // CR-LF pair becomes LF
173 // CR alone becomes LF
174 // LF-CR becomes LF
175 if ( *(p+1) == LF ) {
176 p += 2;
177 }
178 else {
179 ++p;
180 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800181 *q++ = LF;
Lee Thomasone4422302012-01-20 17:59:50 -0800182 }
Lee Thomason8ee79892012-01-25 17:44:30 -0800183 else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
Lee Thomasone4422302012-01-20 17:59:50 -0800184 if ( *(p+1) == CR ) {
185 p += 2;
186 }
187 else {
188 ++p;
189 }
Lee Thomasone9ecdab2012-02-13 18:11:20 -0800190 *q++ = LF;
Lee Thomasone4422302012-01-20 17:59:50 -0800191 }
Lee Thomason8ee79892012-01-25 17:44:30 -0800192 else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
193 int i=0;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800194
195 // Entities handled by tinyXML2:
196 // - special entities in the entity table [in/out]
197 // - numeric character reference [in]
198 // &#20013; or &#x4e2d;
199
200 if ( *(p+1) == '#' ) {
201 char buf[10] = { 0 };
202 int len;
203 p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
204 for( int i=0; i<len; ++i ) {
205 *q++ = buf[i];
Lee Thomason8ee79892012-01-25 17:44:30 -0800206 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800207 TIXMLASSERT( q <= p );
Lee Thomason8ee79892012-01-25 17:44:30 -0800208 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800209 else {
210 for( i=0; i<NUM_ENTITIES; ++i ) {
211 if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
212 && *(p+entities[i].length+1) == ';' )
213 {
214 // Found an entity convert;
215 *q = entities[i].value;
216 ++q;
217 p += entities[i].length + 2;
218 break;
219 }
220 }
221 if ( i == NUM_ENTITIES ) {
222 // fixme: treat as error?
223 ++p;
224 ++q;
225 }
Lee Thomason8ee79892012-01-25 17:44:30 -0800226 }
227 }
Lee Thomasone4422302012-01-20 17:59:50 -0800228 else {
229 *q = *p;
230 ++p;
Lee Thomasonec975ce2012-01-23 11:42:06 -0800231 ++q;
Lee Thomasone4422302012-01-20 17:59:50 -0800232 }
233 }
Lee Thomason8ee79892012-01-25 17:44:30 -0800234 *q = 0;
Lee Thomasone4422302012-01-20 17:59:50 -0800235 }
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800236 flags = (flags & NEEDS_DELETE);
Lee Thomasone4422302012-01-20 17:59:50 -0800237 }
238 return start;
239}
240
Lee Thomason2c85a712012-01-31 08:24:24 -0800241
Lee Thomasone4422302012-01-20 17:59:50 -0800242
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800243
Lee Thomason56bdd022012-02-09 18:16:58 -0800244// --------- XMLUtil ----------- //
Lee Thomasond1983222012-02-06 08:41:24 -0800245
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800246const char* XMLUtil::ReadBOM( const char* p, bool* bom )
247{
248 *bom = false;
249 const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
250 // Check for BOM:
251 if ( *(pu+0) == TIXML_UTF_LEAD_0
252 && *(pu+1) == TIXML_UTF_LEAD_1
253 && *(pu+2) == TIXML_UTF_LEAD_2 )
254 {
255 *bom = true;
256 p += 3;
257 }
258 return p;
259}
260
261
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800262void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
263{
264 const unsigned long BYTE_MASK = 0xBF;
265 const unsigned long BYTE_MARK = 0x80;
266 const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
267
268 if (input < 0x80)
269 *length = 1;
270 else if ( input < 0x800 )
271 *length = 2;
272 else if ( input < 0x10000 )
273 *length = 3;
274 else if ( input < 0x200000 )
275 *length = 4;
276 else
277 { *length = 0; return; } // This code won't covert this correctly anyway.
278
279 output += *length;
280
281 // Scary scary fall throughs.
282 switch (*length)
283 {
284 case 4:
285 --output;
286 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
287 input >>= 6;
288 case 3:
289 --output;
290 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
291 input >>= 6;
292 case 2:
293 --output;
294 *output = (char)((input | BYTE_MARK) & BYTE_MASK);
295 input >>= 6;
296 case 1:
297 --output;
298 *output = (char)(input | FIRST_BYTE_MARK[*length]);
299 }
300}
301
302
303const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
304{
305 // Presume an entity, and pull it out.
306 *length = 0;
307
308 if ( *(p+1) == '#' && *(p+2) )
309 {
310 unsigned long ucs = 0;
Thomas Roß7d7a9a32012-05-10 00:23:19 +0200311 ptrdiff_t delta = 0;
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800312 unsigned mult = 1;
313
314 if ( *(p+2) == 'x' )
315 {
316 // Hexadecimal.
317 if ( !*(p+3) ) return 0;
318
319 const char* q = p+3;
320 q = strchr( q, ';' );
321
322 if ( !q || !*q ) return 0;
323
Thomas Roß7d7a9a32012-05-10 00:23:19 +0200324 delta = q-p;
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800325 --q;
326
327 while ( *q != 'x' )
328 {
329 if ( *q >= '0' && *q <= '9' )
330 ucs += mult * (*q - '0');
331 else if ( *q >= 'a' && *q <= 'f' )
332 ucs += mult * (*q - 'a' + 10);
333 else if ( *q >= 'A' && *q <= 'F' )
334 ucs += mult * (*q - 'A' + 10 );
335 else
336 return 0;
337 mult *= 16;
338 --q;
339 }
340 }
341 else
342 {
343 // Decimal.
344 if ( !*(p+2) ) return 0;
345
346 const char* q = p+2;
347 q = strchr( q, ';' );
348
349 if ( !q || !*q ) return 0;
350
351 delta = q-p;
352 --q;
353
354 while ( *q != '#' )
355 {
356 if ( *q >= '0' && *q <= '9' )
357 ucs += mult * (*q - '0');
358 else
359 return 0;
360 mult *= 10;
361 --q;
362 }
363 }
364 // convert the UCS to UTF-8
365 ConvertUTF32ToUTF8( ucs, value, length );
366 return p + delta + 1;
367 }
368 return p+1;
369}
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800370
371
Lee Thomasond1983222012-02-06 08:41:24 -0800372char* XMLDocument::Identify( char* p, XMLNode** node )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800373{
374 XMLNode* returnNode = 0;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800375 char* start = p;
Lee Thomason56bdd022012-02-09 18:16:58 -0800376 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason5492a1c2012-01-23 15:32:10 -0800377 if( !p || !*p )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800378 {
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800379 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800380 }
381
382 // What is this thing?
383 // - Elements start with a letter or underscore, but xml is reserved.
384 // - Comments: <!--
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800385 // - Decleration: <?
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800386 // - Everthing else is unknown to tinyxml.
387 //
388
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800389 static const char* xmlHeader = { "<?" };
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800390 static const char* commentHeader = { "<!--" };
391 static const char* dtdHeader = { "<!" };
392 static const char* cdataHeader = { "<![CDATA[" };
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800393 static const char* elementHeader = { "<" }; // and a header for everything else; check last.
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800394
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800395 static const int xmlHeaderLen = 2;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800396 static const int commentHeaderLen = 4;
397 static const int dtdHeaderLen = 2;
398 static const int cdataHeaderLen = 9;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800399 static const int elementHeaderLen = 1;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800400
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800401#if defined(_MSC_VER)
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800402#pragma warning ( push )
403#pragma warning ( disable : 4127 )
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800404#endif
Lee Thomason50f97b22012-02-11 16:33:40 -0800405 TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
406 TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800407#if defined(_MSC_VER)
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -0800408#pragma warning (pop)
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800409#endif
Lee Thomason50f97b22012-02-11 16:33:40 -0800410 if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
411 returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
412 returnNode->memPool = &commentPool;
413 p += xmlHeaderLen;
414 }
415 else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800416 returnNode = new (commentPool.Alloc()) XMLComment( this );
417 returnNode->memPool = &commentPool;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800418 p += commentHeaderLen;
419 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800420 else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
421 XMLText* text = new (textPool.Alloc()) XMLText( this );
422 returnNode = text;
423 returnNode->memPool = &textPool;
424 p += cdataHeaderLen;
425 text->SetCData( true );
426 }
427 else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
428 returnNode = new (commentPool.Alloc()) XMLUnknown( this );
429 returnNode->memPool = &commentPool;
430 p += dtdHeaderLen;
431 }
Lee Thomason56bdd022012-02-09 18:16:58 -0800432 else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
Lee Thomasond1983222012-02-06 08:41:24 -0800433 returnNode = new (elementPool.Alloc()) XMLElement( this );
434 returnNode->memPool = &elementPool;
Lee Thomasondadcdfa2012-01-18 17:55:48 -0800435 p += elementHeaderLen;
436 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800437 else {
Lee Thomasond1983222012-02-06 08:41:24 -0800438 returnNode = new (textPool.Alloc()) XMLText( this );
439 returnNode->memPool = &textPool;
Lee Thomason5492a1c2012-01-23 15:32:10 -0800440 p = start; // Back it up, all the text counts.
441 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800442
443 *node = returnNode;
444 return p;
445}
446
447
Lee Thomason751da522012-02-10 08:50:51 -0800448bool XMLDocument::Accept( XMLVisitor* visitor ) const
449{
450 if ( visitor->VisitEnter( *this ) )
451 {
452 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
453 {
454 if ( !node->Accept( visitor ) )
455 break;
456 }
457 }
458 return visitor->VisitExit( *this );
459}
Lee Thomason56bdd022012-02-09 18:16:58 -0800460
461
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800462// --------- XMLNode ----------- //
463
464XMLNode::XMLNode( XMLDocument* doc ) :
465 document( doc ),
466 parent( 0 ),
467 firstChild( 0 ), lastChild( 0 ),
468 prev( 0 ), next( 0 )
469{
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800470}
471
472
473XMLNode::~XMLNode()
474{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800475 DeleteChildren();
Lee Thomason7c913cd2012-01-26 18:32:34 -0800476 if ( parent ) {
477 parent->Unlink( this );
478 }
Lee Thomason18d68bd2012-01-26 18:17:26 -0800479}
480
481
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800482void XMLNode::SetValue( const char* str, bool staticMem )
483{
484 if ( staticMem )
485 value.SetInternedStr( str );
486 else
487 value.SetStr( str );
488}
489
490
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -0800491void XMLNode::DeleteChildren()
Lee Thomason18d68bd2012-01-26 18:17:26 -0800492{
Lee Thomasond923c672012-01-23 08:44:25 -0800493 while( firstChild ) {
494 XMLNode* node = firstChild;
495 Unlink( node );
Lee Thomasond1983222012-02-06 08:41:24 -0800496
Lee Thomason43f59302012-02-06 18:18:11 -0800497 DELETE_NODE( node );
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800498 }
Lee Thomason18d68bd2012-01-26 18:17:26 -0800499 firstChild = lastChild = 0;
Lee Thomasond923c672012-01-23 08:44:25 -0800500}
501
502
503void XMLNode::Unlink( XMLNode* child )
504{
505 TIXMLASSERT( child->parent == this );
506 if ( child == firstChild )
507 firstChild = firstChild->next;
508 if ( child == lastChild )
509 lastChild = lastChild->prev;
510
511 if ( child->prev ) {
512 child->prev->next = child->next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800513 }
Lee Thomasond923c672012-01-23 08:44:25 -0800514 if ( child->next ) {
515 child->next->prev = child->prev;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800516 }
Lee Thomasond923c672012-01-23 08:44:25 -0800517 child->parent = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800518}
519
520
U-Stream\Leeae25a442012-02-17 17:48:16 -0800521void XMLNode::DeleteChild( XMLNode* node )
522{
523 TIXMLASSERT( node->parent == this );
524 DELETE_NODE( node );
525}
526
527
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800528XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
529{
530 if ( lastChild ) {
531 TIXMLASSERT( firstChild );
532 TIXMLASSERT( lastChild->next == 0 );
533 lastChild->next = addThis;
534 addThis->prev = lastChild;
535 lastChild = addThis;
536
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800537 addThis->next = 0;
538 }
539 else {
540 TIXMLASSERT( firstChild == 0 );
541 firstChild = lastChild = addThis;
542
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800543 addThis->prev = 0;
544 addThis->next = 0;
545 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800546 addThis->parent = this;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800547 return addThis;
548}
549
550
Lee Thomason1ff38e02012-02-14 18:18:16 -0800551XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
552{
553 if ( firstChild ) {
554 TIXMLASSERT( lastChild );
555 TIXMLASSERT( firstChild->prev == 0 );
556
557 firstChild->prev = addThis;
558 addThis->next = firstChild;
559 firstChild = addThis;
560
Lee Thomason1ff38e02012-02-14 18:18:16 -0800561 addThis->prev = 0;
562 }
563 else {
564 TIXMLASSERT( lastChild == 0 );
565 firstChild = lastChild = addThis;
566
Lee Thomason1ff38e02012-02-14 18:18:16 -0800567 addThis->prev = 0;
568 addThis->next = 0;
569 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800570 addThis->parent = this;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800571 return addThis;
572}
573
574
575XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
576{
577 TIXMLASSERT( afterThis->parent == this );
578 if ( afterThis->parent != this )
579 return 0;
580
581 if ( afterThis->next == 0 ) {
582 // The last node or the only node.
583 return InsertEndChild( addThis );
584 }
585 addThis->prev = afterThis;
586 addThis->next = afterThis->next;
587 afterThis->next->prev = addThis;
588 afterThis->next = addThis;
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800589 addThis->parent = this;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800590 return addThis;
591}
592
593
594
595
Lee Thomason56bdd022012-02-09 18:16:58 -0800596const XMLElement* XMLNode::FirstChildElement( const char* value ) const
Lee Thomason2c85a712012-01-31 08:24:24 -0800597{
598 for( XMLNode* node=firstChild; node; node=node->next ) {
599 XMLElement* element = node->ToElement();
600 if ( element ) {
Lee Thomason56bdd022012-02-09 18:16:58 -0800601 if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
Lee Thomason2c85a712012-01-31 08:24:24 -0800602 return element;
603 }
604 }
605 }
606 return 0;
607}
608
609
Lee Thomason56bdd022012-02-09 18:16:58 -0800610const XMLElement* XMLNode::LastChildElement( const char* value ) const
611{
612 for( XMLNode* node=lastChild; node; node=node->prev ) {
613 XMLElement* element = node->ToElement();
614 if ( element ) {
615 if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
616 return element;
617 }
618 }
619 }
620 return 0;
621}
622
623
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800624const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
625{
626 for( XMLNode* element=this->next; element; element = element->next ) {
627 if ( element->ToElement()
628 && (!value || XMLUtil::StringEqual( value, element->Value() )))
629 {
630 return element->ToElement();
631 }
632 }
633 return 0;
634}
635
636
637const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
638{
639 for( XMLNode* element=this->prev; element; element = element->prev ) {
640 if ( element->ToElement()
641 && (!value || XMLUtil::StringEqual( value, element->Value() )))
642 {
643 return element->ToElement();
644 }
645 }
646 return 0;
647}
648
649
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800650char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
Lee Thomason67d61312012-01-24 16:01:51 -0800651{
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800652 // This is a recursive method, but thinking about it "at the current level"
653 // it is a pretty simple flat list:
654 // <foo/>
655 // <!-- comment -->
656 //
657 // With a special case:
658 // <foo>
659 // </foo>
660 // <!-- comment -->
661 //
662 // Where the closing element (/foo) *must* be the next thing after the opening
663 // element, and the names must match. BUT the tricky bit is that the closing
664 // element will be read by the child.
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800665 //
666 // 'endTag' is the end tag for this node, it is returned by a call to a child.
667 // 'parentEnd' is the end tag for the parent, which is filled in and returned.
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800668
Lee Thomason67d61312012-01-24 16:01:51 -0800669 while( p && *p ) {
670 XMLNode* node = 0;
Lee Thomasond6277762012-02-22 16:00:12 -0800671
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800672 p = document->Identify( p, &node );
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800673 if ( p == 0 || node == 0 ) {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800674 break;
675 }
676
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800677 StrPair endTag;
678 p = node->ParseDeep( p, &endTag );
679 if ( !p ) {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800680 DELETE_NODE( node );
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800681 node = 0;
Lee Thomason (grinliz)784607f2012-02-24 16:23:40 -0800682 if ( !document->Error() ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700683 document->SetError( XML_ERROR_PARSING, 0, 0 );
Lee Thomason (grinliz)784607f2012-02-24 16:23:40 -0800684 }
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800685 break;
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800686 }
687
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800688 // We read the end tag. Return it to the parent.
689 if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
690 if ( parentEnd ) {
691 *parentEnd = ((XMLElement*)node)->value;
Lee Thomason67d61312012-01-24 16:01:51 -0800692 }
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800693 DELETE_NODE( node );
694 return p;
695 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800696
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800697 // Handle an end tag returned to this level.
698 // And handle a bunch of annoying errors.
699 XMLElement* ele = node->ToElement();
700 if ( ele ) {
701 if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700702 document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800703 p = 0;
704 }
705 else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700706 document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800707 p = 0;
708 }
709 else if ( !endTag.Empty() ) {
710 if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700711 document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800712 p = 0;
713 }
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800714 }
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800715 }
716 if ( p == 0 ) {
717 DELETE_NODE( node );
718 node = 0;
719 }
720 if ( node ) {
721 this->InsertEndChild( node );
Lee Thomason67d61312012-01-24 16:01:51 -0800722 }
723 }
724 return 0;
725}
726
Lee Thomason5492a1c2012-01-23 15:32:10 -0800727// --------- XMLText ---------- //
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800728char* XMLText::ParseDeep( char* p, StrPair* )
Lee Thomason5492a1c2012-01-23 15:32:10 -0800729{
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800730 const char* start = p;
Lee Thomason50f97b22012-02-11 16:33:40 -0800731 if ( this->CData() ) {
732 p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800733 if ( !p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700734 document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800735 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800736 return p;
737 }
738 else {
Lee Thomason6f381b72012-03-02 12:59:39 -0800739 p = value.ParseText( p, "<", document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800740 if ( !p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700741 document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800742 }
Lee Thomason50f97b22012-02-11 16:33:40 -0800743 if ( p && *p ) {
744 return p-1;
745 }
Lee Thomason5492a1c2012-01-23 15:32:10 -0800746 }
747 return 0;
748}
749
750
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800751XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
752{
753 if ( !doc ) {
754 doc = document;
755 }
756 XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
757 text->SetCData( this->CData() );
758 return text;
759}
760
761
762bool XMLText::ShallowEqual( const XMLNode* compare ) const
763{
764 return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
765}
766
767
Lee Thomason56bdd022012-02-09 18:16:58 -0800768bool XMLText::Accept( XMLVisitor* visitor ) const
769{
770 return visitor->Visit( *this );
771}
772
773
Lee Thomason3f57d272012-01-11 15:30:03 -0800774// --------- XMLComment ---------- //
775
Lee Thomasone4422302012-01-20 17:59:50 -0800776XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
Lee Thomason3f57d272012-01-11 15:30:03 -0800777{
778}
779
780
Lee Thomasonce0763e2012-01-11 15:43:54 -0800781XMLComment::~XMLComment()
Lee Thomason3f57d272012-01-11 15:30:03 -0800782{
Lee Thomasond923c672012-01-23 08:44:25 -0800783 //printf( "~XMLComment\n" );
Lee Thomason3f57d272012-01-11 15:30:03 -0800784}
785
786
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800787char* XMLComment::ParseDeep( char* p, StrPair* )
Lee Thomason3f57d272012-01-11 15:30:03 -0800788{
789 // Comment parses as text.
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800790 const char* start = p;
791 p = value.ParseText( p, "-->", StrPair::COMMENT );
792 if ( p == 0 ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700793 document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800794 }
795 return p;
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800796}
797
798
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800799XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
800{
801 if ( !doc ) {
802 doc = document;
803 }
804 XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
805 return comment;
806}
807
808
809bool XMLComment::ShallowEqual( const XMLNode* compare ) const
810{
811 return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() ));
812}
813
814
Lee Thomason751da522012-02-10 08:50:51 -0800815bool XMLComment::Accept( XMLVisitor* visitor ) const
816{
817 return visitor->Visit( *this );
818}
Lee Thomason56bdd022012-02-09 18:16:58 -0800819
820
Lee Thomason50f97b22012-02-11 16:33:40 -0800821// --------- XMLDeclaration ---------- //
822
823XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
824{
825}
826
827
828XMLDeclaration::~XMLDeclaration()
829{
830 //printf( "~XMLDeclaration\n" );
831}
832
833
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800834char* XMLDeclaration::ParseDeep( char* p, StrPair* )
Lee Thomason50f97b22012-02-11 16:33:40 -0800835{
836 // Declaration parses as text.
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800837 const char* start = p;
838 p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
839 if ( p == 0 ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700840 document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800841 }
842 return p;
Lee Thomason50f97b22012-02-11 16:33:40 -0800843}
844
845
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800846XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
847{
848 if ( !doc ) {
849 doc = document;
850 }
851 XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
852 return dec;
853}
854
855
856bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
857{
858 return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() ));
859}
860
861
862
Lee Thomason50f97b22012-02-11 16:33:40 -0800863bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
864{
865 return visitor->Visit( *this );
866}
867
868// --------- XMLUnknown ---------- //
869
870XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
871{
872}
873
874
875XMLUnknown::~XMLUnknown()
876{
877}
878
879
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -0800880char* XMLUnknown::ParseDeep( char* p, StrPair* )
Lee Thomason50f97b22012-02-11 16:33:40 -0800881{
882 // Unknown parses as text.
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800883 const char* start = p;
884
885 p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
886 if ( !p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700887 document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -0800888 }
889 return p;
Lee Thomason50f97b22012-02-11 16:33:40 -0800890}
891
892
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800893XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
894{
895 if ( !doc ) {
896 doc = document;
897 }
898 XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
899 return text;
900}
901
902
903bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
904{
905 return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() ));
906}
907
908
Lee Thomason50f97b22012-02-11 16:33:40 -0800909bool XMLUnknown::Accept( XMLVisitor* visitor ) const
910{
911 return visitor->Visit( *this );
912}
913
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800914// --------- XMLAttribute ---------- //
Lee Thomason6f381b72012-03-02 12:59:39 -0800915char* XMLAttribute::ParseDeep( char* p, bool processEntities )
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800916{
Lee Thomason56bdd022012-02-09 18:16:58 -0800917 p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
Lee Thomason22aead12012-01-23 13:29:35 -0800918 if ( !p || !*p ) return 0;
919
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800920 char endTag[2] = { *p, 0 };
921 ++p;
Lee Thomason6f381b72012-03-02 12:59:39 -0800922 p = value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -0800923 //if ( value.Empty() ) return 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -0800924 return p;
925}
926
927
U-Stream\Lee09a11c52012-02-17 08:31:16 -0800928void XMLAttribute::SetName( const char* n )
929{
930 name.SetStr( n );
931}
932
933
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800934int XMLAttribute::QueryIntValue( int* value ) const
Lee Thomason1ff38e02012-02-14 18:18:16 -0800935{
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800936 if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800937 return XML_NO_ERROR;
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700938 return XML_WRONG_ATTRIBUTE_TYPE;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800939}
940
941
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800942int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
Lee Thomason1ff38e02012-02-14 18:18:16 -0800943{
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800944 if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800945 return XML_NO_ERROR;
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700946 return XML_WRONG_ATTRIBUTE_TYPE;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800947}
948
949
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800950int XMLAttribute::QueryBoolValue( bool* value ) const
Lee Thomason1ff38e02012-02-14 18:18:16 -0800951{
952 int ival = -1;
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800953 QueryIntValue( &ival );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800954
955 if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
956 *value = true;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800957 return XML_NO_ERROR;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800958 }
959 else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
960 *value = false;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800961 return XML_NO_ERROR;
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800962 }
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700963 return XML_WRONG_ATTRIBUTE_TYPE;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800964}
965
966
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800967int XMLAttribute::QueryDoubleValue( double* value ) const
Lee Thomason1ff38e02012-02-14 18:18:16 -0800968{
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800969 if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800970 return XML_NO_ERROR;
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700971 return XML_WRONG_ATTRIBUTE_TYPE;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800972}
973
974
Lee Thomason7d00b9a2012-02-27 17:54:22 -0800975int XMLAttribute::QueryFloatValue( float* value ) const
Lee Thomason1ff38e02012-02-14 18:18:16 -0800976{
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800977 if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -0800978 return XML_NO_ERROR;
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -0700979 return XML_WRONG_ATTRIBUTE_TYPE;
Lee Thomason1ff38e02012-02-14 18:18:16 -0800980}
981
982
983void XMLAttribute::SetAttribute( const char* v )
984{
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800985 value.SetStr( v );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800986}
987
988
Lee Thomason1ff38e02012-02-14 18:18:16 -0800989void XMLAttribute::SetAttribute( int v )
990{
991 char buf[BUF_SIZE];
Lee Thomason (grinliz)598c13e2012-04-06 21:18:23 -0700992 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v );
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800993 value.SetStr( buf );
Lee Thomason1ff38e02012-02-14 18:18:16 -0800994}
Lee Thomason1a1d4a72012-02-15 09:09:25 -0800995
996
997void XMLAttribute::SetAttribute( unsigned v )
998{
999 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001000 TIXML_SNPRINTF( buf, BUF_SIZE, "%u", v );
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001001 value.SetStr( buf );
1002}
1003
1004
1005void XMLAttribute::SetAttribute( bool v )
1006{
1007 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001008 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ? 1 : 0 );
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001009 value.SetStr( buf );
1010}
1011
1012void XMLAttribute::SetAttribute( double v )
1013{
1014 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001015 TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v );
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001016 value.SetStr( buf );
1017}
1018
1019void XMLAttribute::SetAttribute( float v )
1020{
1021 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001022 TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v );
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001023 value.SetStr( buf );
1024}
1025
Lee Thomasondadcdfa2012-01-18 17:55:48 -08001026
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001027// --------- XMLElement ---------- //
1028XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -08001029 closingType( 0 ),
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001030 rootAttribute( 0 )
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001031{
1032}
1033
1034
1035XMLElement::~XMLElement()
1036{
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001037 while( rootAttribute ) {
1038 XMLAttribute* next = rootAttribute->next;
1039 DELETE_ATTRIBUTE( rootAttribute );
1040 rootAttribute = next;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001041 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001042}
1043
1044
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001045XMLAttribute* XMLElement::FindAttribute( const char* name )
1046{
1047 XMLAttribute* a = 0;
1048 for( a=rootAttribute; a; a = a->next ) {
1049 if ( XMLUtil::StringEqual( a->Name(), name ) )
1050 return a;
1051 }
1052 return 0;
1053}
1054
1055
1056const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
1057{
1058 XMLAttribute* a = 0;
1059 for( a=rootAttribute; a; a = a->next ) {
1060 if ( XMLUtil::StringEqual( a->Name(), name ) )
1061 return a;
1062 }
1063 return 0;
1064}
1065
1066
Lee Thomason8ba7f7d2012-03-24 13:04:04 -07001067const char* XMLElement::Attribute( const char* name, const char* value ) const
1068{
1069 const XMLAttribute* a = FindAttribute( name );
1070 if ( !a )
1071 return 0;
1072 if ( !value || XMLUtil::StringEqual( a->Value(), value ))
1073 return a->Value();
1074 return 0;
1075}
1076
1077
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001078const char* XMLElement::GetText() const
1079{
1080 if ( FirstChild() && FirstChild()->ToText() ) {
1081 return FirstChild()->ToText()->Value();
1082 }
1083 return 0;
1084}
1085
1086
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001087XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
1088{
Lee Thomason5e3803c2012-04-16 08:57:05 -07001089 XMLAttribute* last = 0;
1090 XMLAttribute* attrib = 0;
1091 for( attrib = rootAttribute;
1092 attrib;
1093 last = attrib, attrib = attrib->next )
1094 {
1095 if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
1096 break;
1097 }
1098 }
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001099 if ( !attrib ) {
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001100 attrib = new (document->attributePool.Alloc() ) XMLAttribute();
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001101 attrib->memPool = &document->attributePool;
Lee Thomason5e3803c2012-04-16 08:57:05 -07001102 if ( last ) {
1103 last->next = attrib;
1104 }
1105 else {
1106 rootAttribute = attrib;
1107 }
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001108 attrib->SetName( name );
Lee Thomason1a1d4a72012-02-15 09:09:25 -08001109 }
1110 return attrib;
1111}
1112
1113
U-Stream\Leeae25a442012-02-17 17:48:16 -08001114void XMLElement::DeleteAttribute( const char* name )
1115{
1116 XMLAttribute* prev = 0;
1117 for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
1118 if ( XMLUtil::StringEqual( name, a->Name() ) ) {
1119 if ( prev ) {
1120 prev->next = a->next;
1121 }
1122 else {
1123 rootAttribute = a->next;
1124 }
1125 DELETE_ATTRIBUTE( a );
1126 break;
1127 }
1128 prev = a;
1129 }
1130}
1131
1132
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -08001133char* XMLElement::ParseAttributes( char* p )
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001134{
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001135 const char* start = p;
Lee Thomason5e3803c2012-04-16 08:57:05 -07001136 XMLAttribute* prevAttribute = 0;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001137
1138 // Read the attributes.
1139 while( p ) {
Lee Thomason56bdd022012-02-09 18:16:58 -08001140 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001141 if ( !p || !(*p) ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001142 document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001143 return 0;
1144 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001145
1146 // attribute.
Lee Thomason56bdd022012-02-09 18:16:58 -08001147 if ( XMLUtil::IsAlpha( *p ) ) {
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001148 XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute();
Lee Thomason43f59302012-02-06 18:18:11 -08001149 attrib->memPool = &document->attributePool;
Lee Thomasond1983222012-02-06 08:41:24 -08001150
Lee Thomason6f381b72012-03-02 12:59:39 -08001151 p = attrib->ParseDeep( p, document->ProcessEntities() );
Lee Thomasond6277762012-02-22 16:00:12 -08001152 if ( !p || Attribute( attrib->Name() ) ) {
Lee Thomason43f59302012-02-06 18:18:11 -08001153 DELETE_ATTRIBUTE( attrib );
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001154 document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001155 return 0;
1156 }
Lee Thomason5e3803c2012-04-16 08:57:05 -07001157 // There is a minor bug here: if the attribute in the source xml
1158 // document is duplicated, it will not be detected and the
1159 // attribute will be doubly added. However, tracking the 'prevAttribute'
1160 // avoids re-scanning the attribute list. Preferring performance for
1161 // now, may reconsider in the future.
1162 if ( prevAttribute ) {
1163 prevAttribute->next = attrib;
Lee Thomason5e3803c2012-04-16 08:57:05 -07001164 }
1165 else {
1166 rootAttribute = attrib;
Lee Thomason5e3803c2012-04-16 08:57:05 -07001167 }
Lee Thomason97088852012-04-18 11:39:42 -07001168 prevAttribute = attrib;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001169 }
Lee Thomasone4422302012-01-20 17:59:50 -08001170 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001171 else if ( *p == '/' && *(p+1) == '>' ) {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -08001172 closingType = CLOSED;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001173 return p+2; // done; sealed element.
1174 }
Lee Thomasone4422302012-01-20 17:59:50 -08001175 // end of the tag
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001176 else if ( *p == '>' ) {
1177 ++p;
1178 break;
1179 }
Lee Thomasone4422302012-01-20 17:59:50 -08001180 else {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001181 document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
Lee Thomasone4422302012-01-20 17:59:50 -08001182 return 0;
1183 }
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001184 }
Lee Thomason67d61312012-01-24 16:01:51 -08001185 return p;
1186}
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001187
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001188
Lee Thomason67d61312012-01-24 16:01:51 -08001189//
1190// <ele></ele>
1191// <ele>foo<b>bar</b></ele>
1192//
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001193char* XMLElement::ParseDeep( char* p, StrPair* strPair )
Lee Thomason67d61312012-01-24 16:01:51 -08001194{
1195 // Read the element name.
Lee Thomason56bdd022012-02-09 18:16:58 -08001196 p = XMLUtil::SkipWhiteSpace( p );
Lee Thomason67d61312012-01-24 16:01:51 -08001197 if ( !p ) return 0;
Lee Thomason67d61312012-01-24 16:01:51 -08001198
1199 // The closing element is the </element> form. It is
1200 // parsed just like a regular element then deleted from
1201 // the DOM.
1202 if ( *p == '/' ) {
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -08001203 closingType = CLOSING;
Lee Thomason67d61312012-01-24 16:01:51 -08001204 ++p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001205 }
Lee Thomason67d61312012-01-24 16:01:51 -08001206
Lee Thomason56bdd022012-02-09 18:16:58 -08001207 p = value.ParseName( p );
Lee Thomasond1983222012-02-06 08:41:24 -08001208 if ( value.Empty() ) return 0;
Lee Thomason67d61312012-01-24 16:01:51 -08001209
Lee Thomason (grinliz)46a14cf2012-02-23 22:27:28 -08001210 p = ParseAttributes( p );
1211 if ( !p || !*p || closingType )
Lee Thomason67d61312012-01-24 16:01:51 -08001212 return p;
1213
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001214 p = XMLNode::ParseDeep( p, strPair );
Lee Thomason67d61312012-01-24 16:01:51 -08001215 return p;
Lee Thomason8a5dfee2012-01-18 17:43:40 -08001216}
1217
1218
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001219
1220XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
1221{
1222 if ( !doc ) {
1223 doc = document;
1224 }
1225 XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
1226 for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
1227 element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
1228 }
1229 return element;
1230}
1231
1232
1233bool XMLElement::ShallowEqual( const XMLNode* compare ) const
1234{
1235 const XMLElement* other = compare->ToElement();
1236 if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
1237
1238 const XMLAttribute* a=FirstAttribute();
1239 const XMLAttribute* b=other->FirstAttribute();
1240
1241 while ( a && b ) {
1242 if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
1243 return false;
1244 }
Lee Thomason (grinliz)390e9782012-07-01 21:22:53 -07001245 a = a->Next();
1246 b = b->Next();
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001247 }
1248 if ( a || b ) {
1249 // different count
1250 return false;
1251 }
1252 return true;
1253 }
1254 return false;
1255}
1256
1257
Lee Thomason751da522012-02-10 08:50:51 -08001258bool XMLElement::Accept( XMLVisitor* visitor ) const
1259{
1260 if ( visitor->VisitEnter( *this, rootAttribute ) )
1261 {
1262 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
1263 {
1264 if ( !node->Accept( visitor ) )
1265 break;
1266 }
1267 }
1268 return visitor->VisitExit( *this );
Lee Thomason751da522012-02-10 08:50:51 -08001269}
Lee Thomason56bdd022012-02-09 18:16:58 -08001270
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001271
Lee Thomason3f57d272012-01-11 15:30:03 -08001272// --------- XMLDocument ----------- //
Lee Thomason6f381b72012-03-02 12:59:39 -08001273XMLDocument::XMLDocument( bool _processEntities ) :
Lee Thomason18d68bd2012-01-26 18:17:26 -08001274 XMLNode( 0 ),
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001275 writeBOM( false ),
Lee Thomason6f381b72012-03-02 12:59:39 -08001276 processEntities( _processEntities ),
1277 errorID( 0 ),
1278 errorStr1( 0 ),
1279 errorStr2( 0 ),
U-Lama\Lee560bd472011-12-28 19:42:49 -08001280 charBuffer( 0 )
1281{
Lee Thomason18d68bd2012-01-26 18:17:26 -08001282 document = this; // avoid warning about 'this' in initializer list
U-Lama\Lee560bd472011-12-28 19:42:49 -08001283}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001284
1285
Lee Thomason3f57d272012-01-11 15:30:03 -08001286XMLDocument::~XMLDocument()
1287{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001288 DeleteChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -08001289 delete [] charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -08001290
Lee Thomasonec5a7b42012-02-13 18:16:52 -08001291#if 0
Lee Thomason455c9d42012-02-06 09:14:14 -08001292 textPool.Trace( "text" );
1293 elementPool.Trace( "element" );
1294 commentPool.Trace( "comment" );
1295 attributePool.Trace( "attribute" );
Lee Thomasone9ecdab2012-02-13 18:11:20 -08001296#endif
1297
Lee Thomason455c9d42012-02-06 09:14:14 -08001298 TIXMLASSERT( textPool.CurrentAllocs() == 0 );
1299 TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
1300 TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
1301 TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
Lee Thomason3f57d272012-01-11 15:30:03 -08001302}
1303
1304
Lee Thomason18d68bd2012-01-26 18:17:26 -08001305void XMLDocument::InitDocument()
1306{
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001307 errorID = XML_NO_ERROR;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001308 errorStr1 = 0;
1309 errorStr2 = 0;
1310
1311 delete [] charBuffer;
1312 charBuffer = 0;
1313
1314}
1315
Lee Thomason3f57d272012-01-11 15:30:03 -08001316
Lee Thomason2c85a712012-01-31 08:24:24 -08001317XMLElement* XMLDocument::NewElement( const char* name )
1318{
Lee Thomasond1983222012-02-06 08:41:24 -08001319 XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
1320 ele->memPool = &elementPool;
Lee Thomason2c85a712012-01-31 08:24:24 -08001321 ele->SetName( name );
1322 return ele;
1323}
1324
1325
Lee Thomason1ff38e02012-02-14 18:18:16 -08001326XMLComment* XMLDocument::NewComment( const char* str )
1327{
1328 XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
1329 comment->memPool = &commentPool;
1330 comment->SetValue( str );
1331 return comment;
1332}
1333
1334
1335XMLText* XMLDocument::NewText( const char* str )
1336{
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001337 XMLText* text = new (textPool.Alloc()) XMLText( this );
1338 text->memPool = &textPool;
1339 text->SetValue( str );
1340 return text;
Lee Thomason1ff38e02012-02-14 18:18:16 -08001341}
1342
1343
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001344XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
1345{
1346 XMLDeclaration* dec = new (commentPool.Alloc()) XMLDeclaration( this );
1347 dec->memPool = &commentPool;
Lee Thomasonf68c4382012-04-28 14:37:11 -07001348 dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001349 return dec;
1350}
1351
1352
1353XMLUnknown* XMLDocument::NewUnknown( const char* str )
1354{
1355 XMLUnknown* unk = new (commentPool.Alloc()) XMLUnknown( this );
1356 unk->memPool = &commentPool;
1357 unk->SetValue( str );
1358 return unk;
1359}
1360
1361
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001362int XMLDocument::LoadFile( const char* filename )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001363{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001364 DeleteChildren();
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001365 InitDocument();
1366
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001367#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001368#pragma warning ( push )
1369#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001370#endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001371 FILE* fp = fopen( filename, "rb" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001372#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001373#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001374#endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001375 if ( !fp ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001376 SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001377 return errorID;
1378 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001379 LoadFile( fp );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001380 fclose( fp );
1381 return errorID;
1382}
1383
1384
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001385int XMLDocument::LoadFile( FILE* fp )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001386{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001387 DeleteChildren();
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001388 InitDocument();
1389
1390 fseek( fp, 0, SEEK_END );
1391 unsigned size = ftell( fp );
1392 fseek( fp, 0, SEEK_SET );
1393
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001394 if ( size == 0 ) {
1395 return errorID;
1396 }
1397
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001398 charBuffer = new char[size+1];
Lee Thomasona3efec02012-06-15 14:30:44 -07001399 size_t read = fread( charBuffer, 1, size, fp );
1400 if ( read != size ) {
1401 SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
1402 return errorID;
1403 }
1404
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001405 charBuffer[size] = 0;
1406
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001407 const char* p = charBuffer;
1408 p = XMLUtil::SkipWhiteSpace( p );
1409 p = XMLUtil::ReadBOM( p, &writeBOM );
1410 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001411 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001412 return errorID;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001413 }
1414
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001415 ParseDeep( charBuffer + (p-charBuffer), 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001416 return errorID;
1417}
1418
1419
Ken Miller81da1fb2012-04-09 23:32:26 -05001420int XMLDocument::SaveFile( const char* filename )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001421{
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001422#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001423#pragma warning ( push )
1424#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001425#endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001426 FILE* fp = fopen( filename, "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001427#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001428#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001429#endif
Ken Miller81da1fb2012-04-09 23:32:26 -05001430 if ( !fp ) {
Lee Thomason7f7b1622012-03-24 12:49:03 -07001431 SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
Ken Miller81da1fb2012-04-09 23:32:26 -05001432 return errorID;
Lee Thomason7f7b1622012-03-24 12:49:03 -07001433 }
Ken Miller81da1fb2012-04-09 23:32:26 -05001434 SaveFile(fp);
1435 fclose( fp );
1436 return errorID;
1437}
1438
1439
1440int XMLDocument::SaveFile( FILE* fp )
1441{
1442 XMLPrinter stream( fp );
1443 Print( &stream );
1444 return errorID;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001445}
1446
Lee Thomason1ff38e02012-02-14 18:18:16 -08001447
Lee Thomason7c913cd2012-01-26 18:32:34 -08001448int XMLDocument::Parse( const char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -08001449{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001450 DeleteChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -08001451 InitDocument();
1452
1453 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001454 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001455 return errorID;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001456 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001457 p = XMLUtil::SkipWhiteSpace( p );
1458 p = XMLUtil::ReadBOM( p, &writeBOM );
1459 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001460 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001461 return errorID;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001462 }
1463
Lee Thomason18d68bd2012-01-26 18:17:26 -08001464 size_t len = strlen( p );
1465 charBuffer = new char[ len+1 ];
1466 memcpy( charBuffer, p, len+1 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001467
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001468
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001469 ParseDeep( charBuffer, 0 );
Lee Thomason7c913cd2012-01-26 18:32:34 -08001470 return errorID;
Lee Thomason3f57d272012-01-11 15:30:03 -08001471}
1472
1473
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001474void XMLDocument::Print( XMLPrinter* streamer )
Lee Thomason3f57d272012-01-11 15:30:03 -08001475{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001476 XMLPrinter stdStreamer( stdout );
Lee Thomason5cae8972012-01-24 18:03:07 -08001477 if ( !streamer )
1478 streamer = &stdStreamer;
Lee Thomason751da522012-02-10 08:50:51 -08001479 Accept( streamer );
Lee Thomason3f57d272012-01-11 15:30:03 -08001480}
1481
1482
Lee Thomason67d61312012-01-24 16:01:51 -08001483void XMLDocument::SetError( int error, const char* str1, const char* str2 )
1484{
Lee Thomason18d68bd2012-01-26 18:17:26 -08001485 errorID = error;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001486 errorStr1 = str1;
1487 errorStr2 = str2;
Lee Thomason67d61312012-01-24 16:01:51 -08001488}
1489
Lee Thomason5cae8972012-01-24 18:03:07 -08001490
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001491void XMLDocument::PrintError() const
1492{
1493 if ( errorID ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001494 static const int LEN = 20;
1495 char buf1[LEN] = { 0 };
1496 char buf2[LEN] = { 0 };
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001497
1498 if ( errorStr1 ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001499 TIXML_SNPRINTF( buf1, LEN, "%s", errorStr1 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001500 }
1501 if ( errorStr2 ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001502 TIXML_SNPRINTF( buf2, LEN, "%s", errorStr2 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001503 }
1504
1505 printf( "XMLDocument error id=%d str1=%s str2=%s\n",
1506 errorID, buf1, buf2 );
1507 }
1508}
1509
1510
sniperbat25900882012-05-28 17:22:07 +08001511XMLPrinter::XMLPrinter( FILE* file, bool compact ) :
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001512 elementJustOpened( false ),
1513 firstElement( true ),
1514 fp( file ),
1515 depth( 0 ),
Lee Thomason6f381b72012-03-02 12:59:39 -08001516 textDepth( -1 ),
sniperbat25900882012-05-28 17:22:07 +08001517 processEntities( true ),
1518 compactMode( compact )
Lee Thomason5cae8972012-01-24 18:03:07 -08001519{
Lee Thomason857b8682012-01-25 17:50:25 -08001520 for( int i=0; i<ENTITY_RANGE; ++i ) {
1521 entityFlag[i] = false;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001522 restrictedEntityFlag[i] = false;
Lee Thomason857b8682012-01-25 17:50:25 -08001523 }
1524 for( int i=0; i<NUM_ENTITIES; ++i ) {
1525 TIXMLASSERT( entities[i].value < ENTITY_RANGE );
1526 if ( entities[i].value < ENTITY_RANGE ) {
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001527 entityFlag[ (int)entities[i].value ] = true;
Lee Thomason857b8682012-01-25 17:50:25 -08001528 }
1529 }
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001530 restrictedEntityFlag[(int)'&'] = true;
1531 restrictedEntityFlag[(int)'<'] = true;
1532 restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
U-Stream\Leeae25a442012-02-17 17:48:16 -08001533 buffer.Push( 0 );
1534}
1535
1536
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001537void XMLPrinter::Print( const char* format, ... )
U-Stream\Leeae25a442012-02-17 17:48:16 -08001538{
1539 va_list va;
1540 va_start( va, format );
1541
1542 if ( fp ) {
1543 vfprintf( fp, format, va );
1544 }
1545 else {
1546 // This seems brutally complex. Haven't figured out a better
1547 // way on windows.
1548 #ifdef _MSC_VER
1549 int len = -1;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001550 int expand = 1000;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001551 while ( len < 0 ) {
Lee Thomason (grinliz)598c13e2012-04-06 21:18:23 -07001552 len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), _TRUNCATE, format, va );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001553 if ( len < 0 ) {
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001554 expand *= 3/2;
Lee Thomason (grinliz)598c13e2012-04-06 21:18:23 -07001555 accumulator.PushArr( expand );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001556 }
1557 }
1558 char* p = buffer.PushArr( len ) - 1;
1559 memcpy( p, accumulator.Mem(), len+1 );
1560 #else
1561 int len = vsnprintf( 0, 0, format, va );
Lee Thomason4de93472012-03-13 17:33:35 -07001562 // Close out and re-start the va-args
1563 va_end( va );
1564 va_start( va, format );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001565 char* p = buffer.PushArr( len ) - 1;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001566 vsnprintf( p, len+1, format, va );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001567 #endif
1568 }
1569 va_end( va );
Lee Thomason5cae8972012-01-24 18:03:07 -08001570}
1571
1572
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001573void XMLPrinter::PrintSpace( int depth )
Lee Thomason5cae8972012-01-24 18:03:07 -08001574{
1575 for( int i=0; i<depth; ++i ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001576 Print( " " );
Lee Thomason5cae8972012-01-24 18:03:07 -08001577 }
1578}
1579
1580
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001581void XMLPrinter::PrintString( const char* p, bool restricted )
Lee Thomason857b8682012-01-25 17:50:25 -08001582{
Lee Thomason951d8832012-01-26 08:47:06 -08001583 // Look for runs of bytes between entities to print.
1584 const char* q = p;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001585 const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
Lee Thomason857b8682012-01-25 17:50:25 -08001586
Lee Thomason6f381b72012-03-02 12:59:39 -08001587 if ( processEntities ) {
1588 while ( *q ) {
1589 // Remember, char is sometimes signed. (How many times has that bitten me?)
1590 if ( *q > 0 && *q < ENTITY_RANGE ) {
1591 // Check for entities. If one is found, flush
1592 // the stream up until the entity, write the
1593 // entity, and keep looking.
Lee Thomason (grinliz)8a0975d2012-03-31 20:09:20 -07001594 if ( flag[(unsigned)(*q)] ) {
Lee Thomason6f381b72012-03-02 12:59:39 -08001595 while ( p < q ) {
1596 Print( "%c", *p );
1597 ++p;
1598 }
1599 for( int i=0; i<NUM_ENTITIES; ++i ) {
1600 if ( entities[i].value == *q ) {
1601 Print( "&%s;", entities[i].pattern );
1602 break;
1603 }
1604 }
Lee Thomason951d8832012-01-26 08:47:06 -08001605 ++p;
1606 }
Lee Thomason951d8832012-01-26 08:47:06 -08001607 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001608 ++q;
Lee Thomason951d8832012-01-26 08:47:06 -08001609 }
Lee Thomason951d8832012-01-26 08:47:06 -08001610 }
1611 // Flush the remaining string. This will be the entire
1612 // string if an entity wasn't found.
Lee Thomason6f381b72012-03-02 12:59:39 -08001613 if ( !processEntities || (q-p > 0) ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001614 Print( "%s", p );
Lee Thomason951d8832012-01-26 08:47:06 -08001615 }
Lee Thomason857b8682012-01-25 17:50:25 -08001616}
1617
U-Stream\Leeae25a442012-02-17 17:48:16 -08001618
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001619void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001620{
1621 static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
1622 if ( writeBOM ) {
1623 Print( "%s", bom );
1624 }
1625 if ( writeDec ) {
1626 PushDeclaration( "xml version=\"1.0\"" );
1627 }
1628}
1629
1630
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001631void XMLPrinter::OpenElement( const char* name )
Lee Thomason5cae8972012-01-24 18:03:07 -08001632{
1633 if ( elementJustOpened ) {
1634 SealElement();
1635 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001636 stack.Push( name );
1637
sniperbat25900882012-05-28 17:22:07 +08001638 if ( textDepth < 0 && !firstElement && !compactMode ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001639 Print( "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -08001640 PrintSpace( depth );
1641 }
Lee Thomason5cae8972012-01-24 18:03:07 -08001642
U-Stream\Leeae25a442012-02-17 17:48:16 -08001643 Print( "<%s", name );
Lee Thomason5cae8972012-01-24 18:03:07 -08001644 elementJustOpened = true;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001645 firstElement = false;
Lee Thomason5cae8972012-01-24 18:03:07 -08001646 ++depth;
1647}
1648
1649
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001650void XMLPrinter::PushAttribute( const char* name, const char* value )
Lee Thomason5cae8972012-01-24 18:03:07 -08001651{
1652 TIXMLASSERT( elementJustOpened );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001653 Print( " %s=\"", name );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001654 PrintString( value, false );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001655 Print( "\"" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001656}
1657
1658
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001659void XMLPrinter::PushAttribute( const char* name, int v )
1660{
1661 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001662 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001663 PushAttribute( name, buf );
1664}
1665
1666
1667void XMLPrinter::PushAttribute( const char* name, unsigned v )
1668{
1669 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001670 TIXML_SNPRINTF( buf, BUF_SIZE, "%u", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001671 PushAttribute( name, buf );
1672}
1673
1674
1675void XMLPrinter::PushAttribute( const char* name, bool v )
1676{
1677 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001678 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ? 1 : 0 );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001679 PushAttribute( name, buf );
1680}
1681
1682
1683void XMLPrinter::PushAttribute( const char* name, double v )
1684{
1685 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001686 TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001687 PushAttribute( name, buf );
1688}
1689
1690
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001691void XMLPrinter::CloseElement()
Lee Thomason5cae8972012-01-24 18:03:07 -08001692{
1693 --depth;
1694 const char* name = stack.Pop();
Lee Thomason5cae8972012-01-24 18:03:07 -08001695
1696 if ( elementJustOpened ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001697 Print( "/>" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001698 }
1699 else {
sniperbat25900882012-05-28 17:22:07 +08001700 if ( textDepth < 0 && !compactMode) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001701 Print( "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -08001702 PrintSpace( depth );
1703 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001704 Print( "</%s>", name );
Lee Thomason5cae8972012-01-24 18:03:07 -08001705 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001706
1707 if ( textDepth == depth )
1708 textDepth = -1;
sniperbat25900882012-05-28 17:22:07 +08001709 if ( depth == 0 && !compactMode)
U-Stream\Leeae25a442012-02-17 17:48:16 -08001710 Print( "\n" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001711 elementJustOpened = false;
1712}
1713
1714
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001715void XMLPrinter::SealElement()
Lee Thomason5cae8972012-01-24 18:03:07 -08001716{
1717 elementJustOpened = false;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001718 Print( ">" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001719}
1720
1721
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001722void XMLPrinter::PushText( const char* text, bool cdata )
Lee Thomason5cae8972012-01-24 18:03:07 -08001723{
Lee Thomason56bdd022012-02-09 18:16:58 -08001724 textDepth = depth-1;
1725
Lee Thomason5cae8972012-01-24 18:03:07 -08001726 if ( elementJustOpened ) {
1727 SealElement();
1728 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001729 if ( cdata ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001730 Print( "<![CDATA[" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001731 Print( "%s", text );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001732 Print( "]]>" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001733 }
1734 else {
1735 PrintString( text, true );
1736 }
Lee Thomason5cae8972012-01-24 18:03:07 -08001737}
1738
1739
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001740void XMLPrinter::PushComment( const char* comment )
Lee Thomason5cae8972012-01-24 18:03:07 -08001741{
1742 if ( elementJustOpened ) {
1743 SealElement();
1744 }
sniperbat25900882012-05-28 17:22:07 +08001745 if ( textDepth < 0 && !firstElement && !compactMode) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001746 Print( "\n" );
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001747 PrintSpace( depth );
1748 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001749 firstElement = false;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001750 Print( "<!--%s-->", comment );
Lee Thomason5cae8972012-01-24 18:03:07 -08001751}
Lee Thomason751da522012-02-10 08:50:51 -08001752
1753
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001754void XMLPrinter::PushDeclaration( const char* value )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001755{
1756 if ( elementJustOpened ) {
1757 SealElement();
1758 }
sniperbat25900882012-05-28 17:22:07 +08001759 if ( textDepth < 0 && !firstElement && !compactMode) {
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001760 Print( "\n" );
1761 PrintSpace( depth );
1762 }
1763 firstElement = false;
1764 Print( "<?%s?>", value );
1765}
1766
1767
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001768void XMLPrinter::PushUnknown( const char* value )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001769{
1770 if ( elementJustOpened ) {
1771 SealElement();
1772 }
sniperbat25900882012-05-28 17:22:07 +08001773 if ( textDepth < 0 && !firstElement && !compactMode) {
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001774 Print( "\n" );
1775 PrintSpace( depth );
1776 }
1777 firstElement = false;
1778 Print( "<!%s>", value );
1779}
1780
1781
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001782bool XMLPrinter::VisitEnter( const XMLDocument& doc )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001783{
Lee Thomason6f381b72012-03-02 12:59:39 -08001784 processEntities = doc.ProcessEntities();
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001785 if ( doc.HasBOM() ) {
1786 PushHeader( true, false );
1787 }
1788 return true;
1789}
1790
1791
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001792bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
Lee Thomason751da522012-02-10 08:50:51 -08001793{
1794 OpenElement( element.Name() );
1795 while ( attribute ) {
1796 PushAttribute( attribute->Name(), attribute->Value() );
1797 attribute = attribute->Next();
1798 }
1799 return true;
1800}
1801
1802
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001803bool XMLPrinter::VisitExit( const XMLElement& )
Lee Thomason751da522012-02-10 08:50:51 -08001804{
1805 CloseElement();
1806 return true;
1807}
1808
1809
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001810bool XMLPrinter::Visit( const XMLText& text )
Lee Thomason751da522012-02-10 08:50:51 -08001811{
Lee Thomasond6277762012-02-22 16:00:12 -08001812 PushText( text.Value(), text.CData() );
Lee Thomason751da522012-02-10 08:50:51 -08001813 return true;
1814}
1815
1816
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001817bool XMLPrinter::Visit( const XMLComment& comment )
Lee Thomason751da522012-02-10 08:50:51 -08001818{
1819 PushComment( comment.Value() );
1820 return true;
1821}
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001822
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001823bool XMLPrinter::Visit( const XMLDeclaration& declaration )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001824{
1825 PushDeclaration( declaration.Value() );
1826 return true;
1827}
1828
1829
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001830bool XMLPrinter::Visit( const XMLUnknown& unknown )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001831{
1832 PushUnknown( unknown.Value() );
1833 return true;
1834}