blob: 80dfc5c06df99d1e53a9bb6e101f27d62a877f50 [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 }
1245 }
1246 if ( a || b ) {
1247 // different count
1248 return false;
1249 }
1250 return true;
1251 }
1252 return false;
1253}
1254
1255
Lee Thomason751da522012-02-10 08:50:51 -08001256bool XMLElement::Accept( XMLVisitor* visitor ) const
1257{
1258 if ( visitor->VisitEnter( *this, rootAttribute ) )
1259 {
1260 for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
1261 {
1262 if ( !node->Accept( visitor ) )
1263 break;
1264 }
1265 }
1266 return visitor->VisitExit( *this );
Lee Thomason751da522012-02-10 08:50:51 -08001267}
Lee Thomason56bdd022012-02-09 18:16:58 -08001268
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001269
Lee Thomason3f57d272012-01-11 15:30:03 -08001270// --------- XMLDocument ----------- //
Lee Thomason6f381b72012-03-02 12:59:39 -08001271XMLDocument::XMLDocument( bool _processEntities ) :
Lee Thomason18d68bd2012-01-26 18:17:26 -08001272 XMLNode( 0 ),
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001273 writeBOM( false ),
Lee Thomason6f381b72012-03-02 12:59:39 -08001274 processEntities( _processEntities ),
1275 errorID( 0 ),
1276 errorStr1( 0 ),
1277 errorStr2( 0 ),
U-Lama\Lee560bd472011-12-28 19:42:49 -08001278 charBuffer( 0 )
1279{
Lee Thomason18d68bd2012-01-26 18:17:26 -08001280 document = this; // avoid warning about 'this' in initializer list
U-Lama\Lee560bd472011-12-28 19:42:49 -08001281}
U-Lama\Leee13c3e62011-12-28 14:36:55 -08001282
1283
Lee Thomason3f57d272012-01-11 15:30:03 -08001284XMLDocument::~XMLDocument()
1285{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001286 DeleteChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -08001287 delete [] charBuffer;
Lee Thomasond1983222012-02-06 08:41:24 -08001288
Lee Thomasonec5a7b42012-02-13 18:16:52 -08001289#if 0
Lee Thomason455c9d42012-02-06 09:14:14 -08001290 textPool.Trace( "text" );
1291 elementPool.Trace( "element" );
1292 commentPool.Trace( "comment" );
1293 attributePool.Trace( "attribute" );
Lee Thomasone9ecdab2012-02-13 18:11:20 -08001294#endif
1295
Lee Thomason455c9d42012-02-06 09:14:14 -08001296 TIXMLASSERT( textPool.CurrentAllocs() == 0 );
1297 TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
1298 TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
1299 TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
Lee Thomason3f57d272012-01-11 15:30:03 -08001300}
1301
1302
Lee Thomason18d68bd2012-01-26 18:17:26 -08001303void XMLDocument::InitDocument()
1304{
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001305 errorID = XML_NO_ERROR;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001306 errorStr1 = 0;
1307 errorStr2 = 0;
1308
1309 delete [] charBuffer;
1310 charBuffer = 0;
1311
1312}
1313
Lee Thomason3f57d272012-01-11 15:30:03 -08001314
Lee Thomason2c85a712012-01-31 08:24:24 -08001315XMLElement* XMLDocument::NewElement( const char* name )
1316{
Lee Thomasond1983222012-02-06 08:41:24 -08001317 XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
1318 ele->memPool = &elementPool;
Lee Thomason2c85a712012-01-31 08:24:24 -08001319 ele->SetName( name );
1320 return ele;
1321}
1322
1323
Lee Thomason1ff38e02012-02-14 18:18:16 -08001324XMLComment* XMLDocument::NewComment( const char* str )
1325{
1326 XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
1327 comment->memPool = &commentPool;
1328 comment->SetValue( str );
1329 return comment;
1330}
1331
1332
1333XMLText* XMLDocument::NewText( const char* str )
1334{
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001335 XMLText* text = new (textPool.Alloc()) XMLText( this );
1336 text->memPool = &textPool;
1337 text->SetValue( str );
1338 return text;
Lee Thomason1ff38e02012-02-14 18:18:16 -08001339}
1340
1341
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001342XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
1343{
1344 XMLDeclaration* dec = new (commentPool.Alloc()) XMLDeclaration( this );
1345 dec->memPool = &commentPool;
Lee Thomasonf68c4382012-04-28 14:37:11 -07001346 dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001347 return dec;
1348}
1349
1350
1351XMLUnknown* XMLDocument::NewUnknown( const char* str )
1352{
1353 XMLUnknown* unk = new (commentPool.Alloc()) XMLUnknown( this );
1354 unk->memPool = &commentPool;
1355 unk->SetValue( str );
1356 return unk;
1357}
1358
1359
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001360int XMLDocument::LoadFile( const char* filename )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001361{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001362 DeleteChildren();
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001363 InitDocument();
1364
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001365#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001366#pragma warning ( push )
1367#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001368#endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001369 FILE* fp = fopen( filename, "rb" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001370#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001371#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001372#endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001373 if ( !fp ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001374 SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001375 return errorID;
1376 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001377 LoadFile( fp );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001378 fclose( fp );
1379 return errorID;
1380}
1381
1382
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001383int XMLDocument::LoadFile( FILE* fp )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001384{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001385 DeleteChildren();
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001386 InitDocument();
1387
1388 fseek( fp, 0, SEEK_END );
1389 unsigned size = ftell( fp );
1390 fseek( fp, 0, SEEK_SET );
1391
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001392 if ( size == 0 ) {
1393 return errorID;
1394 }
1395
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001396 charBuffer = new char[size+1];
1397 fread( charBuffer, size, 1, fp );
1398 charBuffer[size] = 0;
1399
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001400 const char* p = charBuffer;
1401 p = XMLUtil::SkipWhiteSpace( p );
1402 p = XMLUtil::ReadBOM( p, &writeBOM );
1403 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001404 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001405 return errorID;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001406 }
1407
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001408 ParseDeep( charBuffer + (p-charBuffer), 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001409 return errorID;
1410}
1411
1412
Ken Miller81da1fb2012-04-09 23:32:26 -05001413int XMLDocument::SaveFile( const char* filename )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001414{
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001415#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001416#pragma warning ( push )
1417#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001418#endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001419 FILE* fp = fopen( filename, "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001420#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001421#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001422#endif
Ken Miller81da1fb2012-04-09 23:32:26 -05001423 if ( !fp ) {
Lee Thomason7f7b1622012-03-24 12:49:03 -07001424 SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
Ken Miller81da1fb2012-04-09 23:32:26 -05001425 return errorID;
Lee Thomason7f7b1622012-03-24 12:49:03 -07001426 }
Ken Miller81da1fb2012-04-09 23:32:26 -05001427 SaveFile(fp);
1428 fclose( fp );
1429 return errorID;
1430}
1431
1432
1433int XMLDocument::SaveFile( FILE* fp )
1434{
1435 XMLPrinter stream( fp );
1436 Print( &stream );
1437 return errorID;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001438}
1439
Lee Thomason1ff38e02012-02-14 18:18:16 -08001440
Lee Thomason7c913cd2012-01-26 18:32:34 -08001441int XMLDocument::Parse( const char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -08001442{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001443 DeleteChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -08001444 InitDocument();
1445
1446 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001447 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001448 return errorID;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001449 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001450 p = XMLUtil::SkipWhiteSpace( p );
1451 p = XMLUtil::ReadBOM( p, &writeBOM );
1452 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001453 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001454 return errorID;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001455 }
1456
Lee Thomason18d68bd2012-01-26 18:17:26 -08001457 size_t len = strlen( p );
1458 charBuffer = new char[ len+1 ];
1459 memcpy( charBuffer, p, len+1 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001460
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001461
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001462 ParseDeep( charBuffer, 0 );
Lee Thomason7c913cd2012-01-26 18:32:34 -08001463 return errorID;
Lee Thomason3f57d272012-01-11 15:30:03 -08001464}
1465
1466
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001467void XMLDocument::Print( XMLPrinter* streamer )
Lee Thomason3f57d272012-01-11 15:30:03 -08001468{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001469 XMLPrinter stdStreamer( stdout );
Lee Thomason5cae8972012-01-24 18:03:07 -08001470 if ( !streamer )
1471 streamer = &stdStreamer;
Lee Thomason751da522012-02-10 08:50:51 -08001472 Accept( streamer );
Lee Thomason3f57d272012-01-11 15:30:03 -08001473}
1474
1475
Lee Thomason67d61312012-01-24 16:01:51 -08001476void XMLDocument::SetError( int error, const char* str1, const char* str2 )
1477{
Lee Thomason18d68bd2012-01-26 18:17:26 -08001478 errorID = error;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001479 errorStr1 = str1;
1480 errorStr2 = str2;
Lee Thomason67d61312012-01-24 16:01:51 -08001481}
1482
Lee Thomason5cae8972012-01-24 18:03:07 -08001483
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001484void XMLDocument::PrintError() const
1485{
1486 if ( errorID ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001487 static const int LEN = 20;
1488 char buf1[LEN] = { 0 };
1489 char buf2[LEN] = { 0 };
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001490
1491 if ( errorStr1 ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001492 TIXML_SNPRINTF( buf1, LEN, "%s", errorStr1 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001493 }
1494 if ( errorStr2 ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001495 TIXML_SNPRINTF( buf2, LEN, "%s", errorStr2 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001496 }
1497
1498 printf( "XMLDocument error id=%d str1=%s str2=%s\n",
1499 errorID, buf1, buf2 );
1500 }
1501}
1502
1503
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001504XMLPrinter::XMLPrinter( FILE* file ) :
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001505 elementJustOpened( false ),
1506 firstElement( true ),
1507 fp( file ),
1508 depth( 0 ),
Lee Thomason6f381b72012-03-02 12:59:39 -08001509 textDepth( -1 ),
1510 processEntities( true )
Lee Thomason5cae8972012-01-24 18:03:07 -08001511{
Lee Thomason857b8682012-01-25 17:50:25 -08001512 for( int i=0; i<ENTITY_RANGE; ++i ) {
1513 entityFlag[i] = false;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001514 restrictedEntityFlag[i] = false;
Lee Thomason857b8682012-01-25 17:50:25 -08001515 }
1516 for( int i=0; i<NUM_ENTITIES; ++i ) {
1517 TIXMLASSERT( entities[i].value < ENTITY_RANGE );
1518 if ( entities[i].value < ENTITY_RANGE ) {
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001519 entityFlag[ (int)entities[i].value ] = true;
Lee Thomason857b8682012-01-25 17:50:25 -08001520 }
1521 }
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001522 restrictedEntityFlag[(int)'&'] = true;
1523 restrictedEntityFlag[(int)'<'] = true;
1524 restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
U-Stream\Leeae25a442012-02-17 17:48:16 -08001525 buffer.Push( 0 );
1526}
1527
1528
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001529void XMLPrinter::Print( const char* format, ... )
U-Stream\Leeae25a442012-02-17 17:48:16 -08001530{
1531 va_list va;
1532 va_start( va, format );
1533
1534 if ( fp ) {
1535 vfprintf( fp, format, va );
1536 }
1537 else {
1538 // This seems brutally complex. Haven't figured out a better
1539 // way on windows.
1540 #ifdef _MSC_VER
1541 int len = -1;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001542 int expand = 1000;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001543 while ( len < 0 ) {
Lee Thomason (grinliz)598c13e2012-04-06 21:18:23 -07001544 len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), _TRUNCATE, format, va );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001545 if ( len < 0 ) {
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001546 expand *= 3/2;
Lee Thomason (grinliz)598c13e2012-04-06 21:18:23 -07001547 accumulator.PushArr( expand );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001548 }
1549 }
1550 char* p = buffer.PushArr( len ) - 1;
1551 memcpy( p, accumulator.Mem(), len+1 );
1552 #else
1553 int len = vsnprintf( 0, 0, format, va );
Lee Thomason4de93472012-03-13 17:33:35 -07001554 // Close out and re-start the va-args
1555 va_end( va );
1556 va_start( va, format );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001557 char* p = buffer.PushArr( len ) - 1;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001558 vsnprintf( p, len+1, format, va );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001559 #endif
1560 }
1561 va_end( va );
Lee Thomason5cae8972012-01-24 18:03:07 -08001562}
1563
1564
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001565void XMLPrinter::PrintSpace( int depth )
Lee Thomason5cae8972012-01-24 18:03:07 -08001566{
1567 for( int i=0; i<depth; ++i ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001568 Print( " " );
Lee Thomason5cae8972012-01-24 18:03:07 -08001569 }
1570}
1571
1572
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001573void XMLPrinter::PrintString( const char* p, bool restricted )
Lee Thomason857b8682012-01-25 17:50:25 -08001574{
Lee Thomason951d8832012-01-26 08:47:06 -08001575 // Look for runs of bytes between entities to print.
1576 const char* q = p;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001577 const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
Lee Thomason857b8682012-01-25 17:50:25 -08001578
Lee Thomason6f381b72012-03-02 12:59:39 -08001579 if ( processEntities ) {
1580 while ( *q ) {
1581 // Remember, char is sometimes signed. (How many times has that bitten me?)
1582 if ( *q > 0 && *q < ENTITY_RANGE ) {
1583 // Check for entities. If one is found, flush
1584 // the stream up until the entity, write the
1585 // entity, and keep looking.
Lee Thomason (grinliz)8a0975d2012-03-31 20:09:20 -07001586 if ( flag[(unsigned)(*q)] ) {
Lee Thomason6f381b72012-03-02 12:59:39 -08001587 while ( p < q ) {
1588 Print( "%c", *p );
1589 ++p;
1590 }
1591 for( int i=0; i<NUM_ENTITIES; ++i ) {
1592 if ( entities[i].value == *q ) {
1593 Print( "&%s;", entities[i].pattern );
1594 break;
1595 }
1596 }
Lee Thomason951d8832012-01-26 08:47:06 -08001597 ++p;
1598 }
Lee Thomason951d8832012-01-26 08:47:06 -08001599 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001600 ++q;
Lee Thomason951d8832012-01-26 08:47:06 -08001601 }
Lee Thomason951d8832012-01-26 08:47:06 -08001602 }
1603 // Flush the remaining string. This will be the entire
1604 // string if an entity wasn't found.
Lee Thomason6f381b72012-03-02 12:59:39 -08001605 if ( !processEntities || (q-p > 0) ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001606 Print( "%s", p );
Lee Thomason951d8832012-01-26 08:47:06 -08001607 }
Lee Thomason857b8682012-01-25 17:50:25 -08001608}
1609
U-Stream\Leeae25a442012-02-17 17:48:16 -08001610
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001611void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001612{
1613 static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
1614 if ( writeBOM ) {
1615 Print( "%s", bom );
1616 }
1617 if ( writeDec ) {
1618 PushDeclaration( "xml version=\"1.0\"" );
1619 }
1620}
1621
1622
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001623void XMLPrinter::OpenElement( const char* name )
Lee Thomason5cae8972012-01-24 18:03:07 -08001624{
1625 if ( elementJustOpened ) {
1626 SealElement();
1627 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001628 stack.Push( name );
1629
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001630 if ( textDepth < 0 && !firstElement ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001631 Print( "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -08001632 PrintSpace( depth );
1633 }
Lee Thomason5cae8972012-01-24 18:03:07 -08001634
U-Stream\Leeae25a442012-02-17 17:48:16 -08001635 Print( "<%s", name );
Lee Thomason5cae8972012-01-24 18:03:07 -08001636 elementJustOpened = true;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001637 firstElement = false;
Lee Thomason5cae8972012-01-24 18:03:07 -08001638 ++depth;
1639}
1640
1641
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001642void XMLPrinter::PushAttribute( const char* name, const char* value )
Lee Thomason5cae8972012-01-24 18:03:07 -08001643{
1644 TIXMLASSERT( elementJustOpened );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001645 Print( " %s=\"", name );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001646 PrintString( value, false );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001647 Print( "\"" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001648}
1649
1650
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001651void XMLPrinter::PushAttribute( const char* name, int v )
1652{
1653 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001654 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001655 PushAttribute( name, buf );
1656}
1657
1658
1659void XMLPrinter::PushAttribute( const char* name, unsigned v )
1660{
1661 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001662 TIXML_SNPRINTF( buf, BUF_SIZE, "%u", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001663 PushAttribute( name, buf );
1664}
1665
1666
1667void XMLPrinter::PushAttribute( const char* name, bool v )
1668{
1669 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001670 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ? 1 : 0 );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001671 PushAttribute( name, buf );
1672}
1673
1674
1675void XMLPrinter::PushAttribute( const char* name, double v )
1676{
1677 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001678 TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001679 PushAttribute( name, buf );
1680}
1681
1682
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001683void XMLPrinter::CloseElement()
Lee Thomason5cae8972012-01-24 18:03:07 -08001684{
1685 --depth;
1686 const char* name = stack.Pop();
Lee Thomason5cae8972012-01-24 18:03:07 -08001687
1688 if ( elementJustOpened ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001689 Print( "/>" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001690 }
1691 else {
Lee Thomason56bdd022012-02-09 18:16:58 -08001692 if ( textDepth < 0 ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001693 Print( "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -08001694 PrintSpace( depth );
1695 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001696 Print( "</%s>", name );
Lee Thomason5cae8972012-01-24 18:03:07 -08001697 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001698
1699 if ( textDepth == depth )
1700 textDepth = -1;
1701 if ( depth == 0 )
U-Stream\Leeae25a442012-02-17 17:48:16 -08001702 Print( "\n" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001703 elementJustOpened = false;
1704}
1705
1706
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001707void XMLPrinter::SealElement()
Lee Thomason5cae8972012-01-24 18:03:07 -08001708{
1709 elementJustOpened = false;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001710 Print( ">" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001711}
1712
1713
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001714void XMLPrinter::PushText( const char* text, bool cdata )
Lee Thomason5cae8972012-01-24 18:03:07 -08001715{
Lee Thomason56bdd022012-02-09 18:16:58 -08001716 textDepth = depth-1;
1717
Lee Thomason5cae8972012-01-24 18:03:07 -08001718 if ( elementJustOpened ) {
1719 SealElement();
1720 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001721 if ( cdata ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001722 Print( "<![CDATA[" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001723 Print( "%s", text );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001724 Print( "]]>" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001725 }
1726 else {
1727 PrintString( text, true );
1728 }
Lee Thomason5cae8972012-01-24 18:03:07 -08001729}
1730
1731
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001732void XMLPrinter::PushComment( const char* comment )
Lee Thomason5cae8972012-01-24 18:03:07 -08001733{
1734 if ( elementJustOpened ) {
1735 SealElement();
1736 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001737 if ( textDepth < 0 && !firstElement ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001738 Print( "\n" );
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001739 PrintSpace( depth );
1740 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001741 firstElement = false;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001742 Print( "<!--%s-->", comment );
Lee Thomason5cae8972012-01-24 18:03:07 -08001743}
Lee Thomason751da522012-02-10 08:50:51 -08001744
1745
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001746void XMLPrinter::PushDeclaration( const char* value )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001747{
1748 if ( elementJustOpened ) {
1749 SealElement();
1750 }
1751 if ( textDepth < 0 && !firstElement) {
1752 Print( "\n" );
1753 PrintSpace( depth );
1754 }
1755 firstElement = false;
1756 Print( "<?%s?>", value );
1757}
1758
1759
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001760void XMLPrinter::PushUnknown( const char* value )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001761{
1762 if ( elementJustOpened ) {
1763 SealElement();
1764 }
1765 if ( textDepth < 0 && !firstElement ) {
1766 Print( "\n" );
1767 PrintSpace( depth );
1768 }
1769 firstElement = false;
1770 Print( "<!%s>", value );
1771}
1772
1773
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001774bool XMLPrinter::VisitEnter( const XMLDocument& doc )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001775{
Lee Thomason6f381b72012-03-02 12:59:39 -08001776 processEntities = doc.ProcessEntities();
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001777 if ( doc.HasBOM() ) {
1778 PushHeader( true, false );
1779 }
1780 return true;
1781}
1782
1783
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001784bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
Lee Thomason751da522012-02-10 08:50:51 -08001785{
1786 OpenElement( element.Name() );
1787 while ( attribute ) {
1788 PushAttribute( attribute->Name(), attribute->Value() );
1789 attribute = attribute->Next();
1790 }
1791 return true;
1792}
1793
1794
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001795bool XMLPrinter::VisitExit( const XMLElement& )
Lee Thomason751da522012-02-10 08:50:51 -08001796{
1797 CloseElement();
1798 return true;
1799}
1800
1801
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001802bool XMLPrinter::Visit( const XMLText& text )
Lee Thomason751da522012-02-10 08:50:51 -08001803{
Lee Thomasond6277762012-02-22 16:00:12 -08001804 PushText( text.Value(), text.CData() );
Lee Thomason751da522012-02-10 08:50:51 -08001805 return true;
1806}
1807
1808
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001809bool XMLPrinter::Visit( const XMLComment& comment )
Lee Thomason751da522012-02-10 08:50:51 -08001810{
1811 PushComment( comment.Value() );
1812 return true;
1813}
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001814
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001815bool XMLPrinter::Visit( const XMLDeclaration& declaration )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001816{
1817 PushDeclaration( declaration.Value() );
1818 return true;
1819}
1820
1821
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001822bool XMLPrinter::Visit( const XMLUnknown& unknown )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001823{
1824 PushUnknown( unknown.Value() );
1825 return true;
1826}