blob: de1f173eac20fb7f8c8e49aedbb519ddcdf9c94b [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];
Lee Thomasona3efec02012-06-15 14:30:44 -07001397 size_t read = fread( charBuffer, 1, size, fp );
1398 if ( read != size ) {
1399 SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
1400 return errorID;
1401 }
1402
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001403 charBuffer[size] = 0;
1404
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001405 const char* p = charBuffer;
1406 p = XMLUtil::SkipWhiteSpace( p );
1407 p = XMLUtil::ReadBOM( p, &writeBOM );
1408 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001409 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001410 return errorID;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001411 }
1412
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001413 ParseDeep( charBuffer + (p-charBuffer), 0 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001414 return errorID;
1415}
1416
1417
Ken Miller81da1fb2012-04-09 23:32:26 -05001418int XMLDocument::SaveFile( const char* filename )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001419{
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001420#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001421#pragma warning ( push )
1422#pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001423#endif
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001424 FILE* fp = fopen( filename, "w" );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001425#if defined(_MSC_VER)
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001426#pragma warning ( pop )
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001427#endif
Ken Miller81da1fb2012-04-09 23:32:26 -05001428 if ( !fp ) {
Lee Thomason7f7b1622012-03-24 12:49:03 -07001429 SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
Ken Miller81da1fb2012-04-09 23:32:26 -05001430 return errorID;
Lee Thomason7f7b1622012-03-24 12:49:03 -07001431 }
Ken Miller81da1fb2012-04-09 23:32:26 -05001432 SaveFile(fp);
1433 fclose( fp );
1434 return errorID;
1435}
1436
1437
1438int XMLDocument::SaveFile( FILE* fp )
1439{
1440 XMLPrinter stream( fp );
1441 Print( &stream );
1442 return errorID;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001443}
1444
Lee Thomason1ff38e02012-02-14 18:18:16 -08001445
Lee Thomason7c913cd2012-01-26 18:32:34 -08001446int XMLDocument::Parse( const char* p )
Lee Thomason3f57d272012-01-11 15:30:03 -08001447{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001448 DeleteChildren();
Lee Thomason18d68bd2012-01-26 18:17:26 -08001449 InitDocument();
1450
1451 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001452 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001453 return errorID;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001454 }
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001455 p = XMLUtil::SkipWhiteSpace( p );
1456 p = XMLUtil::ReadBOM( p, &writeBOM );
1457 if ( !p || !*p ) {
Guillermo A. Amaral2eb70032012-03-20 11:26:57 -07001458 SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
Lee Thomasond6277762012-02-22 16:00:12 -08001459 return errorID;
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001460 }
1461
Lee Thomason18d68bd2012-01-26 18:17:26 -08001462 size_t len = strlen( p );
1463 charBuffer = new char[ len+1 ];
1464 memcpy( charBuffer, p, len+1 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001465
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001466
Lee Thomason (grinliz)7468f112012-02-24 08:56:50 -08001467 ParseDeep( charBuffer, 0 );
Lee Thomason7c913cd2012-01-26 18:32:34 -08001468 return errorID;
Lee Thomason3f57d272012-01-11 15:30:03 -08001469}
1470
1471
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001472void XMLDocument::Print( XMLPrinter* streamer )
Lee Thomason3f57d272012-01-11 15:30:03 -08001473{
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001474 XMLPrinter stdStreamer( stdout );
Lee Thomason5cae8972012-01-24 18:03:07 -08001475 if ( !streamer )
1476 streamer = &stdStreamer;
Lee Thomason751da522012-02-10 08:50:51 -08001477 Accept( streamer );
Lee Thomason3f57d272012-01-11 15:30:03 -08001478}
1479
1480
Lee Thomason67d61312012-01-24 16:01:51 -08001481void XMLDocument::SetError( int error, const char* str1, const char* str2 )
1482{
Lee Thomason18d68bd2012-01-26 18:17:26 -08001483 errorID = error;
Lee Thomason18d68bd2012-01-26 18:17:26 -08001484 errorStr1 = str1;
1485 errorStr2 = str2;
Lee Thomason67d61312012-01-24 16:01:51 -08001486}
1487
Lee Thomason5cae8972012-01-24 18:03:07 -08001488
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001489void XMLDocument::PrintError() const
1490{
1491 if ( errorID ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001492 static const int LEN = 20;
1493 char buf1[LEN] = { 0 };
1494 char buf2[LEN] = { 0 };
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001495
1496 if ( errorStr1 ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001497 TIXML_SNPRINTF( buf1, LEN, "%s", errorStr1 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001498 }
1499 if ( errorStr2 ) {
Lee Thomason (grinliz)5ce4d972012-02-26 21:14:23 -08001500 TIXML_SNPRINTF( buf2, LEN, "%s", errorStr2 );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001501 }
1502
1503 printf( "XMLDocument error id=%d str1=%s str2=%s\n",
1504 errorID, buf1, buf2 );
1505 }
1506}
1507
1508
sniperbat25900882012-05-28 17:22:07 +08001509XMLPrinter::XMLPrinter( FILE* file, bool compact ) :
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001510 elementJustOpened( false ),
1511 firstElement( true ),
1512 fp( file ),
1513 depth( 0 ),
Lee Thomason6f381b72012-03-02 12:59:39 -08001514 textDepth( -1 ),
sniperbat25900882012-05-28 17:22:07 +08001515 processEntities( true ),
1516 compactMode( compact )
Lee Thomason5cae8972012-01-24 18:03:07 -08001517{
Lee Thomason857b8682012-01-25 17:50:25 -08001518 for( int i=0; i<ENTITY_RANGE; ++i ) {
1519 entityFlag[i] = false;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001520 restrictedEntityFlag[i] = false;
Lee Thomason857b8682012-01-25 17:50:25 -08001521 }
1522 for( int i=0; i<NUM_ENTITIES; ++i ) {
1523 TIXMLASSERT( entities[i].value < ENTITY_RANGE );
1524 if ( entities[i].value < ENTITY_RANGE ) {
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001525 entityFlag[ (int)entities[i].value ] = true;
Lee Thomason857b8682012-01-25 17:50:25 -08001526 }
1527 }
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001528 restrictedEntityFlag[(int)'&'] = true;
1529 restrictedEntityFlag[(int)'<'] = true;
1530 restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
U-Stream\Leeae25a442012-02-17 17:48:16 -08001531 buffer.Push( 0 );
1532}
1533
1534
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001535void XMLPrinter::Print( const char* format, ... )
U-Stream\Leeae25a442012-02-17 17:48:16 -08001536{
1537 va_list va;
1538 va_start( va, format );
1539
1540 if ( fp ) {
1541 vfprintf( fp, format, va );
1542 }
1543 else {
1544 // This seems brutally complex. Haven't figured out a better
1545 // way on windows.
1546 #ifdef _MSC_VER
1547 int len = -1;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001548 int expand = 1000;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001549 while ( len < 0 ) {
Lee Thomason (grinliz)598c13e2012-04-06 21:18:23 -07001550 len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), _TRUNCATE, format, va );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001551 if ( len < 0 ) {
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001552 expand *= 3/2;
Lee Thomason (grinliz)598c13e2012-04-06 21:18:23 -07001553 accumulator.PushArr( expand );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001554 }
1555 }
1556 char* p = buffer.PushArr( len ) - 1;
1557 memcpy( p, accumulator.Mem(), len+1 );
1558 #else
1559 int len = vsnprintf( 0, 0, format, va );
Lee Thomason4de93472012-03-13 17:33:35 -07001560 // Close out and re-start the va-args
1561 va_end( va );
1562 va_start( va, format );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001563 char* p = buffer.PushArr( len ) - 1;
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001564 vsnprintf( p, len+1, format, va );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001565 #endif
1566 }
1567 va_end( va );
Lee Thomason5cae8972012-01-24 18:03:07 -08001568}
1569
1570
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001571void XMLPrinter::PrintSpace( int depth )
Lee Thomason5cae8972012-01-24 18:03:07 -08001572{
1573 for( int i=0; i<depth; ++i ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001574 Print( " " );
Lee Thomason5cae8972012-01-24 18:03:07 -08001575 }
1576}
1577
1578
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001579void XMLPrinter::PrintString( const char* p, bool restricted )
Lee Thomason857b8682012-01-25 17:50:25 -08001580{
Lee Thomason951d8832012-01-26 08:47:06 -08001581 // Look for runs of bytes between entities to print.
1582 const char* q = p;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001583 const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
Lee Thomason857b8682012-01-25 17:50:25 -08001584
Lee Thomason6f381b72012-03-02 12:59:39 -08001585 if ( processEntities ) {
1586 while ( *q ) {
1587 // Remember, char is sometimes signed. (How many times has that bitten me?)
1588 if ( *q > 0 && *q < ENTITY_RANGE ) {
1589 // Check for entities. If one is found, flush
1590 // the stream up until the entity, write the
1591 // entity, and keep looking.
Lee Thomason (grinliz)8a0975d2012-03-31 20:09:20 -07001592 if ( flag[(unsigned)(*q)] ) {
Lee Thomason6f381b72012-03-02 12:59:39 -08001593 while ( p < q ) {
1594 Print( "%c", *p );
1595 ++p;
1596 }
1597 for( int i=0; i<NUM_ENTITIES; ++i ) {
1598 if ( entities[i].value == *q ) {
1599 Print( "&%s;", entities[i].pattern );
1600 break;
1601 }
1602 }
Lee Thomason951d8832012-01-26 08:47:06 -08001603 ++p;
1604 }
Lee Thomason951d8832012-01-26 08:47:06 -08001605 }
Lee Thomason6f381b72012-03-02 12:59:39 -08001606 ++q;
Lee Thomason951d8832012-01-26 08:47:06 -08001607 }
Lee Thomason951d8832012-01-26 08:47:06 -08001608 }
1609 // Flush the remaining string. This will be the entire
1610 // string if an entity wasn't found.
Lee Thomason6f381b72012-03-02 12:59:39 -08001611 if ( !processEntities || (q-p > 0) ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001612 Print( "%s", p );
Lee Thomason951d8832012-01-26 08:47:06 -08001613 }
Lee Thomason857b8682012-01-25 17:50:25 -08001614}
1615
U-Stream\Leeae25a442012-02-17 17:48:16 -08001616
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001617void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001618{
1619 static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
1620 if ( writeBOM ) {
1621 Print( "%s", bom );
1622 }
1623 if ( writeDec ) {
1624 PushDeclaration( "xml version=\"1.0\"" );
1625 }
1626}
1627
1628
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001629void XMLPrinter::OpenElement( const char* name )
Lee Thomason5cae8972012-01-24 18:03:07 -08001630{
1631 if ( elementJustOpened ) {
1632 SealElement();
1633 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001634 stack.Push( name );
1635
sniperbat25900882012-05-28 17:22:07 +08001636 if ( textDepth < 0 && !firstElement && !compactMode ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001637 Print( "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -08001638 PrintSpace( depth );
1639 }
Lee Thomason5cae8972012-01-24 18:03:07 -08001640
U-Stream\Leeae25a442012-02-17 17:48:16 -08001641 Print( "<%s", name );
Lee Thomason5cae8972012-01-24 18:03:07 -08001642 elementJustOpened = true;
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001643 firstElement = false;
Lee Thomason5cae8972012-01-24 18:03:07 -08001644 ++depth;
1645}
1646
1647
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001648void XMLPrinter::PushAttribute( const char* name, const char* value )
Lee Thomason5cae8972012-01-24 18:03:07 -08001649{
1650 TIXMLASSERT( elementJustOpened );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001651 Print( " %s=\"", name );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001652 PrintString( value, false );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001653 Print( "\"" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001654}
1655
1656
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001657void XMLPrinter::PushAttribute( const char* name, int v )
1658{
1659 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001660 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001661 PushAttribute( name, buf );
1662}
1663
1664
1665void XMLPrinter::PushAttribute( const char* name, unsigned v )
1666{
1667 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001668 TIXML_SNPRINTF( buf, BUF_SIZE, "%u", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001669 PushAttribute( name, buf );
1670}
1671
1672
1673void XMLPrinter::PushAttribute( const char* name, bool v )
1674{
1675 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001676 TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v ? 1 : 0 );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001677 PushAttribute( name, buf );
1678}
1679
1680
1681void XMLPrinter::PushAttribute( const char* name, double v )
1682{
1683 char buf[BUF_SIZE];
Lee Thomason (grinliz)a4a36ba2012-04-06 21:24:29 -07001684 TIXML_SNPRINTF( buf, BUF_SIZE, "%f", v );
Lee Thomason7d00b9a2012-02-27 17:54:22 -08001685 PushAttribute( name, buf );
1686}
1687
1688
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001689void XMLPrinter::CloseElement()
Lee Thomason5cae8972012-01-24 18:03:07 -08001690{
1691 --depth;
1692 const char* name = stack.Pop();
Lee Thomason5cae8972012-01-24 18:03:07 -08001693
1694 if ( elementJustOpened ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001695 Print( "/>" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001696 }
1697 else {
sniperbat25900882012-05-28 17:22:07 +08001698 if ( textDepth < 0 && !compactMode) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001699 Print( "\n" );
Lee Thomason24767b02012-01-25 17:16:23 -08001700 PrintSpace( depth );
1701 }
U-Stream\Leeae25a442012-02-17 17:48:16 -08001702 Print( "</%s>", name );
Lee Thomason5cae8972012-01-24 18:03:07 -08001703 }
Lee Thomason56bdd022012-02-09 18:16:58 -08001704
1705 if ( textDepth == depth )
1706 textDepth = -1;
sniperbat25900882012-05-28 17:22:07 +08001707 if ( depth == 0 && !compactMode)
U-Stream\Leeae25a442012-02-17 17:48:16 -08001708 Print( "\n" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001709 elementJustOpened = false;
1710}
1711
1712
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001713void XMLPrinter::SealElement()
Lee Thomason5cae8972012-01-24 18:03:07 -08001714{
1715 elementJustOpened = false;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001716 Print( ">" );
Lee Thomason5cae8972012-01-24 18:03:07 -08001717}
1718
1719
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001720void XMLPrinter::PushText( const char* text, bool cdata )
Lee Thomason5cae8972012-01-24 18:03:07 -08001721{
Lee Thomason56bdd022012-02-09 18:16:58 -08001722 textDepth = depth-1;
1723
Lee Thomason5cae8972012-01-24 18:03:07 -08001724 if ( elementJustOpened ) {
1725 SealElement();
1726 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001727 if ( cdata ) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001728 Print( "<![CDATA[" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001729 Print( "%s", text );
U-Stream\Leeae25a442012-02-17 17:48:16 -08001730 Print( "]]>" );
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001731 }
1732 else {
1733 PrintString( text, true );
1734 }
Lee Thomason5cae8972012-01-24 18:03:07 -08001735}
1736
1737
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001738void XMLPrinter::PushComment( const char* comment )
Lee Thomason5cae8972012-01-24 18:03:07 -08001739{
1740 if ( elementJustOpened ) {
1741 SealElement();
1742 }
sniperbat25900882012-05-28 17:22:07 +08001743 if ( textDepth < 0 && !firstElement && !compactMode) {
U-Stream\Leeae25a442012-02-17 17:48:16 -08001744 Print( "\n" );
U-Stream\Lee09a11c52012-02-17 08:31:16 -08001745 PrintSpace( depth );
1746 }
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001747 firstElement = false;
U-Stream\Leeae25a442012-02-17 17:48:16 -08001748 Print( "<!--%s-->", comment );
Lee Thomason5cae8972012-01-24 18:03:07 -08001749}
Lee Thomason751da522012-02-10 08:50:51 -08001750
1751
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001752void XMLPrinter::PushDeclaration( const char* value )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001753{
1754 if ( elementJustOpened ) {
1755 SealElement();
1756 }
sniperbat25900882012-05-28 17:22:07 +08001757 if ( textDepth < 0 && !firstElement && !compactMode) {
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001758 Print( "\n" );
1759 PrintSpace( depth );
1760 }
1761 firstElement = false;
1762 Print( "<?%s?>", value );
1763}
1764
1765
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001766void XMLPrinter::PushUnknown( const char* value )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001767{
1768 if ( elementJustOpened ) {
1769 SealElement();
1770 }
sniperbat25900882012-05-28 17:22:07 +08001771 if ( textDepth < 0 && !firstElement && !compactMode) {
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001772 Print( "\n" );
1773 PrintSpace( depth );
1774 }
1775 firstElement = false;
1776 Print( "<!%s>", value );
1777}
1778
1779
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001780bool XMLPrinter::VisitEnter( const XMLDocument& doc )
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001781{
Lee Thomason6f381b72012-03-02 12:59:39 -08001782 processEntities = doc.ProcessEntities();
Lee Thomason (grinliz)68db57e2012-02-21 09:08:12 -08001783 if ( doc.HasBOM() ) {
1784 PushHeader( true, false );
1785 }
1786 return true;
1787}
1788
1789
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001790bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
Lee Thomason751da522012-02-10 08:50:51 -08001791{
1792 OpenElement( element.Name() );
1793 while ( attribute ) {
1794 PushAttribute( attribute->Name(), attribute->Value() );
1795 attribute = attribute->Next();
1796 }
1797 return true;
1798}
1799
1800
Lee Thomason (grinliz)9b093cc2012-02-25 21:30:18 -08001801bool XMLPrinter::VisitExit( const XMLElement& )
Lee Thomason751da522012-02-10 08:50:51 -08001802{
1803 CloseElement();
1804 return true;
1805}
1806
1807
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001808bool XMLPrinter::Visit( const XMLText& text )
Lee Thomason751da522012-02-10 08:50:51 -08001809{
Lee Thomasond6277762012-02-22 16:00:12 -08001810 PushText( text.Value(), text.CData() );
Lee Thomason751da522012-02-10 08:50:51 -08001811 return true;
1812}
1813
1814
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001815bool XMLPrinter::Visit( const XMLComment& comment )
Lee Thomason751da522012-02-10 08:50:51 -08001816{
1817 PushComment( comment.Value() );
1818 return true;
1819}
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001820
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001821bool XMLPrinter::Visit( const XMLDeclaration& declaration )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001822{
1823 PushDeclaration( declaration.Value() );
1824 return true;
1825}
1826
1827
Lee Thomason (grinliz)2a1cd272012-02-24 17:37:53 -08001828bool XMLPrinter::Visit( const XMLUnknown& unknown )
Lee Thomason (grinliz)bd0a8ac2012-02-20 20:14:33 -08001829{
1830 PushUnknown( unknown.Value() );
1831 return true;
1832}