forked from leethomason/tinyxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyxml2.cpp
1834 lines (1511 loc) · 38.9 KB
/
tinyxml2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Original code by Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinyxml2.h"
#if 1
#include <cstdio>
#include <cstdlib>
#include <new>
#else
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <new>
#include <stdarg.h>
#endif
using namespace tinyxml2;
static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
static const char LF = LINE_FEED;
static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
static const char CR = CARRIAGE_RETURN;
static const char SINGLE_QUOTE = '\'';
static const char DOUBLE_QUOTE = '\"';
// Bunch of unicode info at:
// http://www.unicode.org/faq/utf_bom.html
// ef bb bf (Microsoft "lead bytes") - designates UTF-8
static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
#define DELETE_NODE( node ) { \
if ( node ) { \
MemPool* pool = node->memPool; \
node->~XMLNode(); \
pool->Free( node ); \
} \
}
#define DELETE_ATTRIBUTE( attrib ) { \
if ( attrib ) { \
MemPool* pool = attrib->memPool; \
attrib->~XMLAttribute(); \
pool->Free( attrib ); \
} \
}
struct Entity {
const char* pattern;
int length;
char value;
};
static const int NUM_ENTITIES = 5;
static const Entity entities[NUM_ENTITIES] =
{
{ "quot", 4, DOUBLE_QUOTE },
{ "amp", 3, '&' },
{ "apos", 4, SINGLE_QUOTE },
{ "lt", 2, '<' },
{ "gt", 2, '>' }
};
StrPair::~StrPair()
{
Reset();
}
void StrPair::Reset()
{
if ( flags & NEEDS_DELETE ) {
delete [] start;
}
flags = 0;
start = 0;
end = 0;
}
void StrPair::SetStr( const char* str, int flags )
{
Reset();
size_t len = strlen( str );
start = new char[ len+1 ];
memcpy( start, str, len+1 );
end = start + len;
this->flags = flags | NEEDS_DELETE;
}
char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
{
TIXMLASSERT( endTag && *endTag );
char* start = p; // fixme: hides a member
char endChar = *endTag;
int length = strlen( endTag );
// Inner loop of text parsing.
while ( *p ) {
if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
Set( start, p, strFlags );
return p + length;
}
++p;
}
return 0;
}
char* StrPair::ParseName( char* p )
{
char* start = p;
if ( !start || !(*start) ) {
return 0;
}
if ( !XMLUtil::IsAlpha( *p ) ) {
return 0;
}
while( *p && (
XMLUtil::IsAlphaNum( (unsigned char) *p )
|| *p == '_'
|| *p == '-'
|| *p == '.'
|| *p == ':' ))
{
++p;
}
if ( p > start ) {
Set( start, p, 0 );
return p;
}
return 0;
}
const char* StrPair::GetStr()
{
if ( flags & NEEDS_FLUSH ) {
*end = 0;
flags ^= NEEDS_FLUSH;
if ( flags ) {
char* p = start; // the read pointer
char* q = start; // the write pointer
while( p < end ) {
if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
// CR-LF pair becomes LF
// CR alone becomes LF
// LF-CR becomes LF
if ( *(p+1) == LF ) {
p += 2;
}
else {
++p;
}
*q++ = LF;
}
else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
if ( *(p+1) == CR ) {
p += 2;
}
else {
++p;
}
*q++ = LF;
}
else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
int i=0;
// Entities handled by tinyXML2:
// - special entities in the entity table [in/out]
// - numeric character reference [in]
// 中 or 中
if ( *(p+1) == '#' ) {
char buf[10] = { 0 };
int len;
p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
for( int i=0; i<len; ++i ) {
*q++ = buf[i];
}
TIXMLASSERT( q <= p );
}
else {
for( i=0; i<NUM_ENTITIES; ++i ) {
if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
&& *(p+entities[i].length+1) == ';' )
{
// Found an entity convert;
*q = entities[i].value;
++q;
p += entities[i].length + 2;
break;
}
}
if ( i == NUM_ENTITIES ) {
// fixme: treat as error?
++p;
++q;
}
}
}
else {
*q = *p;
++p;
++q;
}
}
*q = 0;
}
flags = (flags & NEEDS_DELETE);
}
return start;
}
// --------- XMLUtil ----------- //
const char* XMLUtil::ReadBOM( const char* p, bool* bom )
{
*bom = false;
const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
// Check for BOM:
if ( *(pu+0) == TIXML_UTF_LEAD_0
&& *(pu+1) == TIXML_UTF_LEAD_1
&& *(pu+2) == TIXML_UTF_LEAD_2 )
{
*bom = true;
p += 3;
}
return p;
}
void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
{
const unsigned long BYTE_MASK = 0xBF;
const unsigned long BYTE_MARK = 0x80;
const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
if (input < 0x80)
*length = 1;
else if ( input < 0x800 )
*length = 2;
else if ( input < 0x10000 )
*length = 3;
else if ( input < 0x200000 )
*length = 4;
else
{ *length = 0; return; } // This code won't covert this correctly anyway.
output += *length;
// Scary scary fall throughs.
switch (*length)
{
case 4:
--output;
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
case 3:
--output;
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
case 2:
--output;
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
case 1:
--output;
*output = (char)(input | FIRST_BYTE_MARK[*length]);
}
}
const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
{
// Presume an entity, and pull it out.
*length = 0;
if ( *(p+1) == '#' && *(p+2) )
{
unsigned long ucs = 0;
int delta = 0;
unsigned mult = 1;
if ( *(p+2) == 'x' )
{
// Hexadecimal.
if ( !*(p+3) ) return 0;
const char* q = p+3;
q = strchr( q, ';' );
if ( !q || !*q ) return 0;
delta = (q-p);
--q;
while ( *q != 'x' )
{
if ( *q >= '0' && *q <= '9' )
ucs += mult * (*q - '0');
else if ( *q >= 'a' && *q <= 'f' )
ucs += mult * (*q - 'a' + 10);
else if ( *q >= 'A' && *q <= 'F' )
ucs += mult * (*q - 'A' + 10 );
else
return 0;
mult *= 16;
--q;
}
}
else
{
// Decimal.
if ( !*(p+2) ) return 0;
const char* q = p+2;
q = strchr( q, ';' );
if ( !q || !*q ) return 0;
delta = q-p;
--q;
while ( *q != '#' )
{
if ( *q >= '0' && *q <= '9' )
ucs += mult * (*q - '0');
else
return 0;
mult *= 10;
--q;
}
}
// convert the UCS to UTF-8
ConvertUTF32ToUTF8( ucs, value, length );
return p + delta + 1;
}
return p+1;
}
char* XMLDocument::Identify( char* p, XMLNode** node )
{
XMLNode* returnNode = 0;
char* start = p;
p = XMLUtil::SkipWhiteSpace( p );
if( !p || !*p )
{
return p;
}
// What is this thing?
// - Elements start with a letter or underscore, but xml is reserved.
// - Comments: <!--
// - Decleration: <?
// - Everthing else is unknown to tinyxml.
//
static const char* xmlHeader = { "<?" };
static const char* commentHeader = { "<!--" };
static const char* dtdHeader = { "<!" };
static const char* cdataHeader = { "<![CDATA[" };
static const char* elementHeader = { "<" }; // and a header for everything else; check last.
static const int xmlHeaderLen = 2;
static const int commentHeaderLen = 4;
static const int dtdHeaderLen = 2;
static const int cdataHeaderLen = 9;
static const int elementHeaderLen = 1;
#if defined(_MSC_VER)
#pragma warning ( push )
#pragma warning ( disable : 4127 )
#endif
TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
#if defined(_MSC_VER)
#pragma warning (pop)
#endif
if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
returnNode->memPool = &commentPool;
p += xmlHeaderLen;
}
else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
returnNode = new (commentPool.Alloc()) XMLComment( this );
returnNode->memPool = &commentPool;
p += commentHeaderLen;
}
else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
XMLText* text = new (textPool.Alloc()) XMLText( this );
returnNode = text;
returnNode->memPool = &textPool;
p += cdataHeaderLen;
text->SetCData( true );
}
else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
returnNode = new (commentPool.Alloc()) XMLUnknown( this );
returnNode->memPool = &commentPool;
p += dtdHeaderLen;
}
else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
returnNode = new (elementPool.Alloc()) XMLElement( this );
returnNode->memPool = &elementPool;
p += elementHeaderLen;
}
else {
returnNode = new (textPool.Alloc()) XMLText( this );
returnNode->memPool = &textPool;
p = start; // Back it up, all the text counts.
}
*node = returnNode;
return p;
}
bool XMLDocument::Accept( XMLVisitor* visitor ) const
{
if ( visitor->VisitEnter( *this ) )
{
for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
{
if ( !node->Accept( visitor ) )
break;
}
}
return visitor->VisitExit( *this );
}
// --------- XMLNode ----------- //
XMLNode::XMLNode( XMLDocument* doc ) :
document( doc ),
parent( 0 ),
firstChild( 0 ), lastChild( 0 ),
prev( 0 ), next( 0 )
{
}
XMLNode::~XMLNode()
{
DeleteChildren();
if ( parent ) {
parent->Unlink( this );
}
}
void XMLNode::SetValue( const char* str, bool staticMem )
{
if ( staticMem )
value.SetInternedStr( str );
else
value.SetStr( str );
}
void XMLNode::DeleteChildren()
{
while( firstChild ) {
XMLNode* node = firstChild;
Unlink( node );
DELETE_NODE( node );
}
firstChild = lastChild = 0;
}
void XMLNode::Unlink( XMLNode* child )
{
TIXMLASSERT( child->parent == this );
if ( child == firstChild )
firstChild = firstChild->next;
if ( child == lastChild )
lastChild = lastChild->prev;
if ( child->prev ) {
child->prev->next = child->next;
}
if ( child->next ) {
child->next->prev = child->prev;
}
child->parent = 0;
}
void XMLNode::DeleteChild( XMLNode* node )
{
TIXMLASSERT( node->parent == this );
DELETE_NODE( node );
}
XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
{
if ( lastChild ) {
TIXMLASSERT( firstChild );
TIXMLASSERT( lastChild->next == 0 );
lastChild->next = addThis;
addThis->prev = lastChild;
lastChild = addThis;
addThis->next = 0;
}
else {
TIXMLASSERT( firstChild == 0 );
firstChild = lastChild = addThis;
addThis->prev = 0;
addThis->next = 0;
}
addThis->parent = this;
return addThis;
}
XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
{
if ( firstChild ) {
TIXMLASSERT( lastChild );
TIXMLASSERT( firstChild->prev == 0 );
firstChild->prev = addThis;
addThis->next = firstChild;
firstChild = addThis;
addThis->prev = 0;
}
else {
TIXMLASSERT( lastChild == 0 );
firstChild = lastChild = addThis;
addThis->prev = 0;
addThis->next = 0;
}
addThis->parent = this;
return addThis;
}
XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
{
TIXMLASSERT( afterThis->parent == this );
if ( afterThis->parent != this )
return 0;
if ( afterThis->next == 0 ) {
// The last node or the only node.
return InsertEndChild( addThis );
}
addThis->prev = afterThis;
addThis->next = afterThis->next;
afterThis->next->prev = addThis;
afterThis->next = addThis;
addThis->parent = this;
return addThis;
}
const XMLElement* XMLNode::FirstChildElement( const char* value ) const
{
for( XMLNode* node=firstChild; node; node=node->next ) {
XMLElement* element = node->ToElement();
if ( element ) {
if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
return element;
}
}
}
return 0;
}
const XMLElement* XMLNode::LastChildElement( const char* value ) const
{
for( XMLNode* node=lastChild; node; node=node->prev ) {
XMLElement* element = node->ToElement();
if ( element ) {
if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
return element;
}
}
}
return 0;
}
const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
{
for( XMLNode* element=this->next; element; element = element->next ) {
if ( element->ToElement()
&& (!value || XMLUtil::StringEqual( value, element->Value() )))
{
return element->ToElement();
}
}
return 0;
}
const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
{
for( XMLNode* element=this->prev; element; element = element->prev ) {
if ( element->ToElement()
&& (!value || XMLUtil::StringEqual( value, element->Value() )))
{
return element->ToElement();
}
}
return 0;
}
char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
{
// This is a recursive method, but thinking about it "at the current level"
// it is a pretty simple flat list:
// <foo/>
// <!-- comment -->
//
// With a special case:
// <foo>
// </foo>
// <!-- comment -->
//
// Where the closing element (/foo) *must* be the next thing after the opening
// element, and the names must match. BUT the tricky bit is that the closing
// element will be read by the child.
//
// 'endTag' is the end tag for this node, it is returned by a call to a child.
// 'parentEnd' is the end tag for the parent, which is filled in and returned.
while( p && *p ) {
XMLNode* node = 0;
p = document->Identify( p, &node );
if ( p == 0 || node == 0 ) {
break;
}
StrPair endTag;
p = node->ParseDeep( p, &endTag );
if ( !p ) {
DELETE_NODE( node );
node = 0;
if ( !document->Error() ) {
document->SetError( XML_ERROR_PARSING, 0, 0 );
}
break;
}
// We read the end tag. Return it to the parent.
if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
if ( parentEnd ) {
*parentEnd = ((XMLElement*)node)->value;
}
DELETE_NODE( node );
return p;
}
// Handle an end tag returned to this level.
// And handle a bunch of annoying errors.
XMLElement* ele = node->ToElement();
if ( ele ) {
if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
p = 0;
}
else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
p = 0;
}
else if ( !endTag.Empty() ) {
if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
p = 0;
}
}
}
if ( p == 0 ) {
DELETE_NODE( node );
node = 0;
}
if ( node ) {
this->InsertEndChild( node );
}
}
return 0;
}
// --------- XMLText ---------- //
char* XMLText::ParseDeep( char* p, StrPair* )
{
const char* start = p;
if ( this->CData() ) {
p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
if ( !p ) {
document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
}
return p;
}
else {
p = value.ParseText( p, "<", document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES );
if ( !p ) {
document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
}
if ( p && *p ) {
return p-1;
}
}
return 0;
}
XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = document;
}
XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
text->SetCData( this->CData() );
return text;
}
bool XMLText::ShallowEqual( const XMLNode* compare ) const
{
return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
}
bool XMLText::Accept( XMLVisitor* visitor ) const
{
return visitor->Visit( *this );
}
// --------- XMLComment ---------- //
XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
{
}
XMLComment::~XMLComment()
{
//printf( "~XMLComment\n" );
}
char* XMLComment::ParseDeep( char* p, StrPair* )
{
// Comment parses as text.
const char* start = p;
p = value.ParseText( p, "-->", StrPair::COMMENT );
if ( p == 0 ) {
document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
}
return p;
}
XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = document;
}
XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
return comment;
}
bool XMLComment::ShallowEqual( const XMLNode* compare ) const
{
return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() ));
}
bool XMLComment::Accept( XMLVisitor* visitor ) const
{
return visitor->Visit( *this );
}
// --------- XMLDeclaration ---------- //
XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
{
}
XMLDeclaration::~XMLDeclaration()
{
//printf( "~XMLDeclaration\n" );
}
char* XMLDeclaration::ParseDeep( char* p, StrPair* )
{
// Declaration parses as text.
const char* start = p;
p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
if ( p == 0 ) {
document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
}
return p;
}
XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = document;
}
XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
return dec;
}
bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
{
return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() ));
}
bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
{
return visitor->Visit( *this );
}
// --------- XMLUnknown ---------- //
XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
{
}
XMLUnknown::~XMLUnknown()
{
}
char* XMLUnknown::ParseDeep( char* p, StrPair* )
{
// Unknown parses as text.
const char* start = p;
p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
if ( !p ) {
document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
}
return p;
}
XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = document;
}
XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
return text;
}
bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
{
return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() ));
}
bool XMLUnknown::Accept( XMLVisitor* visitor ) const
{
return visitor->Visit( *this );
}
// --------- XMLAttribute ---------- //
char* XMLAttribute::ParseDeep( char* p, bool processEntities )
{
p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
if ( !p || !*p ) return 0;
char endTag[2] = { *p, 0 };
++p;
p = value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
//if ( value.Empty() ) return 0;
return p;
}
void XMLAttribute::SetName( const char* n )
{
name.SetStr( n );
}
int XMLAttribute::QueryIntValue( int* value ) const
{
if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
return XML_NO_ERROR;
return XML_WRONG_ATTRIBUTE_TYPE;
}
int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
{
if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
return XML_NO_ERROR;
return XML_WRONG_ATTRIBUTE_TYPE;
}
int XMLAttribute::QueryBoolValue( bool* value ) const
{
int ival = -1;
QueryIntValue( &ival );
if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
*value = true;
return XML_NO_ERROR;
}
else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
*value = false;
return XML_NO_ERROR;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
int XMLAttribute::QueryDoubleValue( double* value ) const
{
if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
return XML_NO_ERROR;
return XML_WRONG_ATTRIBUTE_TYPE;
}
int XMLAttribute::QueryFloatValue( float* value ) const
{
if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
return XML_NO_ERROR;
return XML_WRONG_ATTRIBUTE_TYPE;
}
void XMLAttribute::SetAttribute( const char* v )
{
value.SetStr( v );
}
void XMLAttribute::SetAttribute( int v )
{
char buf[BUF_SIZE];
TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v );