Game Develop C++ Platform: tinyxml.h Source File

| Game Develop C++ Platform Platform for developing native games based on OpenGL and SFML with Game Develop

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
tinyxml.h
1 /*
2 www.sourceforge.net/projects/tinyxml
3 Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com)
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must
14 not claim that you wrote the original software. If you use this
15 software in a product, an acknowledgment in the product documentation
16 would be appreciated but is not required.
17
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24
31 #if defined(GD_IDE_ONLY)
32 #include "GDCore/TinyXml/tinyxml.h"
33 #else
34
35 #ifndef TINYXML_INCLUDED
36 #define TINYXML_INCLUDED
37
38 #ifdef _MSC_VER
39 #pragma warning( push )
40 #pragma warning( disable : 4530 )
41 #pragma warning( disable : 4786 )
42 #endif
43
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <assert.h>
49
50 // Help out windows:
51 #if defined( _DEBUG ) && !defined( DEBUG )
52 #define DEBUG
53 #endif
54
55 #ifdef TIXML_USE_STL
56 #include <string>
57 #include <iostream>
58 #include <sstream>
59 #define TIXML_STRING std::string
60 #else
61 #include "GDCpp/tinyxml/tinystr.h"
62 #define TIXML_STRING TiXmlString
63 #endif
64
65 // Deprecated library function hell. Compilers want to use the
66 // new safe versions. This probably doesn't fully address the problem,
67 // but it gets closer. There are too many compilers for me to fully
68 // test. If you get compilation troubles, undefine TIXML_SAFE
69 #define TIXML_SAFE
70
71 #ifdef TIXML_SAFE
72 #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
73 // Microsoft visual studio, version 2005 and higher.
74 #define TIXML_SNPRINTF _snprintf_s
75 #define TIXML_SNSCANF _snscanf_s
76 #define TIXML_SSCANF sscanf_s
77 #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
78 // Microsoft visual studio, version 6 and higher.
79 //#pragma message( "Using _sn* functions." )
80 #define TIXML_SNPRINTF _snprintf
81 #define TIXML_SNSCANF _snscanf
82 #define TIXML_SSCANF sscanf
83 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
84 // GCC version 3 and higher.s
85 //#warning( "Using sn* functions." )
86 #define TIXML_SNPRINTF snprintf
87 #define TIXML_SNSCANF snscanf
88 #define TIXML_SSCANF sscanf
89 #else
90 #define TIXML_SSCANF sscanf
91 #endif
92 #endif
93
94 class TiXmlDocument;
95 class TiXmlElement;
96 class TiXmlComment;
97 class TiXmlUnknown;
98 class TiXmlAttribute;
99 class TiXmlText;
100 class TiXmlDeclaration;
101 class TiXmlParsingData;
102
103 const int TIXML_MAJOR_VERSION = 2;
104 const int TIXML_MINOR_VERSION = 5;
105 const int TIXML_PATCH_VERSION = 3;
106
107 /* Internal structure for tracking location of items
108 in the XML file.
109 */
110 struct GD_API TiXmlCursor
111 {
112 TiXmlCursor() { Clear(); }
113 void Clear() { row = col = -1; }
114
115 int row; // 0 based.
116 int col; // 0 based.
117 };
118
119
138 class GD_API TiXmlVisitor
139 {
140 public:
141 virtual ~TiXmlVisitor() {}
142
144 virtual bool VisitEnter( const TiXmlDocument& /*doc*/ ) { return true; }
146 virtual bool VisitExit( const TiXmlDocument& /*doc*/ ) { return true; }
147
149 virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ ) { return true; }
151 virtual bool VisitExit( const TiXmlElement& /*element*/ ) { return true; }
152
154 virtual bool Visit( const TiXmlDeclaration& /*declaration*/ ) { return true; }
156 virtual bool Visit( const TiXmlText& /*text*/ ) { return true; }
158 virtual bool Visit( const TiXmlComment& /*comment*/ ) { return true; }
160 virtual bool Visit( const TiXmlUnknown& /*unknown*/ ) { return true; }
161 };
162
163 // Only used by Attribute::Query functions
164 enum
165 {
166 TIXML_SUCCESS,
167 TIXML_NO_ATTRIBUTE,
168 TIXML_WRONG_TYPE
169 };
170
171
172 // Used by the parsing routines.
173 enum TiXmlEncoding
174 {
175 TIXML_ENCODING_UNKNOWN,
176 TIXML_ENCODING_UTF8,
177 TIXML_ENCODING_LEGACY
178 };
179
180 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
181
204 class GD_API TiXmlBase
205 {
206 friend class TiXmlNode;
207 friend class TiXmlElement;
208 friend class TiXmlDocument;
209
210 public:
211 TiXmlBase() : userData(0) {}
212 virtual ~TiXmlBase() {}
213
223 virtual void Print( FILE* cfile, int depth ) const = 0;
224
231 static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
232
234 static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
235
254 int Row() const { return location.row + 1; }
255 int Column() const { return location.col + 1; }
256
257 void SetUserData( void* user ) { userData = user; }
258 void* GetUserData() { return userData; }
259 const void* GetUserData() const { return userData; }
260
261 // Table that returs, for a given lead byte, the total number of bytes
262 // in the UTF-8 sequence.
263 static const int utf8ByteTable[256];
264
265 virtual const char* Parse( const char* p,
266 TiXmlParsingData* data,
267 TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
268
272 static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out );
273
274 enum
275 {
276 TIXML_NO_ERROR = 0,
277 TIXML_ERROR,
278 TIXML_ERROR_OPENING_FILE,
279 TIXML_ERROR_OUT_OF_MEMORY,
280 TIXML_ERROR_PARSING_ELEMENT,
281 TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
282 TIXML_ERROR_READING_ELEMENT_VALUE,
283 TIXML_ERROR_READING_ATTRIBUTES,
284 TIXML_ERROR_PARSING_EMPTY,
285 TIXML_ERROR_READING_END_TAG,
286 TIXML_ERROR_PARSING_UNKNOWN,
287 TIXML_ERROR_PARSING_COMMENT,
288 TIXML_ERROR_PARSING_DECLARATION,
289 TIXML_ERROR_DOCUMENT_EMPTY,
290 TIXML_ERROR_EMBEDDED_NULL,
291 TIXML_ERROR_PARSING_CDATA,
292 TIXML_ERROR_DOCUMENT_TOP_ONLY,
293
294 TIXML_ERROR_STRING_COUNT
295 };
296
297 protected:
298
299 static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
300 inline static bool IsWhiteSpace( char c )
301 {
302 return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
303 }
304 inline static bool IsWhiteSpace( int c )
305 {
306 if ( c < 256 )
307 return IsWhiteSpace( (char) c );
308 return false; // Again, only truly correct for English/Latin...but usually works.
309 }
310
311 #ifdef TIXML_USE_STL
312 static bool StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
313 static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
314 #endif
315
316 /* Reads an XML name into the string provided. Returns
317 a pointer just past the last character of the name,
318 or 0 if the function has an error.
319 */
320 static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
321
322 /* Reads text. Returns a pointer past the given end tag.
323 Wickedly complex options, but it keeps the (sensitive) code in one place.
324 */
325 static const char* ReadText( const char* in, // where to start
326 TIXML_STRING* text, // the string read
327 bool ignoreWhiteSpace, // whether to keep the white space
328 const char* endTag, // what ends this text
329 bool ignoreCase, // whether to ignore case in the end tag
330 TiXmlEncoding encoding ); // the current encoding
331
332 // If an entity has been found, transform it into a character.
333 static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
334
335 // Get a character, while interpreting entities.
336 // The length can be from 0 to 4 bytes.
337 inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
338 {
339 assert( p );
340 if ( encoding == TIXML_ENCODING_UTF8 )
341 {
342 *length = utf8ByteTable[ *((const unsigned char*)p) ];
343 assert( *length >= 0 && *length < 5 );
344 }
345 else
346 {
347 *length = 1;
348 }
349
350 if ( *length == 1 )
351 {
352 if ( *p == '&' )
353 return GetEntity( p, _value, length, encoding );
354 *_value = *p;
355 return p+1;
356 }
357 else if ( *length )
358 {
359 //strncpy( _value, p, *length ); // lots of compilers don't like this function (unsafe),
360 // and the null terminator isn't needed
361 for( int i=0; p[i] && i<*length; ++i ) {
362 _value[i] = p[i];
363 }
364 return p + (*length);
365 }
366 else
367 {
368 // Not valid text.
369 return 0;
370 }
371 }
372
373 // Return true if the next characters in the stream are any of the endTag sequences.
374 // Ignore case only works for english, and should only be relied on when comparing
375 // to English words: StringEqual( p, "version", true ) is fine.
376 static bool StringEqual( const char* p,
377 const char* endTag,
378 bool ignoreCase,
379 TiXmlEncoding encoding );
380
381 static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
382
383 TiXmlCursor location;
384
386 void* userData;
387
388 // None of these methods are reliable for any language except English.
389 // Good for approximation, not great for accuracy.
390 static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
391 static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
392 inline static int ToLower( int v, TiXmlEncoding encoding )
393 {
394 if ( encoding == TIXML_ENCODING_UTF8 )
395 {
396 if ( v < 128 ) return tolower( v );
397 return v;
398 }
399 else
400 {
401 return tolower( v );
402 }
403 }
404 static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
405
406 private:
407 TiXmlBase( const TiXmlBase& ); // not implemented.
408 void operator=( const TiXmlBase& base ); // not allowed.
409
410 struct Entity
411 {
412 const char* str;
413 unsigned int strLength;
414 char chr;
415 };
416 enum
417 {
418 NUM_ENTITY = 5,
419 MAX_ENTITY_LENGTH = 6
420
421 };
422 static Entity entity[ NUM_ENTITY ];
423 static bool condenseWhiteSpace;
424 };
425
426
433 class GD_API TiXmlNode : public TiXmlBase
434 {
435 friend class TiXmlDocument;
436 friend class TiXmlElement;
437
438 public:
439 #ifdef TIXML_USE_STL
440
444 friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
445
462 friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
463
465 friend std::string& operator<< (std::string& out, const TiXmlNode& base );
466
467 #endif
468
472 enum NodeType
473 {
474 DOCUMENT,
475 ELEMENT,
476 COMMENT,
477 UNKNOWN,
478 TEXT,
479 DECLARATION,
480 TYPECOUNT
481 };
482
483 virtual ~TiXmlNode();
484
497 const char *Value() const { return value.c_str (); }
498
499 #ifdef TIXML_USE_STL
500
504 const std::string& ValueStr() const { return value; }
505 #endif
506
507 const TIXML_STRING& ValueTStr() const { return value; }
508
518 void SetValue(const char * _value) { value = _value;}
519
520 #ifdef TIXML_USE_STL
521 void SetValue( const std::string& _value ) { value = _value; }
523 #endif
524
526 void Clear();
527
529 TiXmlNode* Parent() { return parent; }
530 const TiXmlNode* Parent() const { return parent; }
531
532 const TiXmlNode* FirstChild() const { return firstChild; }
533 TiXmlNode* FirstChild() { return firstChild; }
534 const TiXmlNode* FirstChild( const char * value ) const;
535 TiXmlNode* FirstChild( const char * _value ) {
537 // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
538 // call the method, cast the return back to non-const.
539 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
540 }
541 const TiXmlNode* LastChild() const { return lastChild; }
542 TiXmlNode* LastChild() { return lastChild; }
543
544 const TiXmlNode* LastChild( const char * value ) const;
545 TiXmlNode* LastChild( const char * _value ) {
546 return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
547 }
548
549 #ifdef TIXML_USE_STL
550 const TiXmlNode* FirstChild( const std::string& _value ) const { return FirstChild (_value.c_str ()); }
551 TiXmlNode* FirstChild( const std::string& _value ) { return FirstChild (_value.c_str ()); }
552 const TiXmlNode* LastChild( const std::string& _value ) const { return LastChild (_value.c_str ()); }
553 TiXmlNode* LastChild( const std::string& _value ) { return LastChild (_value.c_str ()); }
554 #endif
555
572 const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
573 TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
574 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
575 }
576
578 const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
579 TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
580 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
581 }
582
583 #ifdef TIXML_USE_STL
584 const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const { return IterateChildren (_value.c_str (), previous); }
585 TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); }
586 #endif
587
591 TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
592
593
603 TiXmlNode* LinkEndChild( TiXmlNode* addThis );
604
608 TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
609
613 TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
614
618 TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
619
621 bool RemoveChild( TiXmlNode* removeThis );
622
624 const TiXmlNode* PreviousSibling() const { return prev; }
625 TiXmlNode* PreviousSibling() { return prev; }
626
628 const TiXmlNode* PreviousSibling( const char * ) const;
629 TiXmlNode* PreviousSibling( const char *_prev ) {
630 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
631 }
632
633 #ifdef TIXML_USE_STL
634 const TiXmlNode* PreviousSibling( const std::string& _value ) const { return PreviousSibling (_value.c_str ()); }
635 TiXmlNode* PreviousSibling( const std::string& _value ) { return PreviousSibling (_value.c_str ()); }
636 const TiXmlNode* NextSibling( const std::string& _value) const { return NextSibling (_value.c_str ()); }
637 TiXmlNode* NextSibling( const std::string& _value) { return NextSibling (_value.c_str ()); }
638 #endif
639
641 const TiXmlNode* NextSibling() const { return next; }
642 TiXmlNode* NextSibling() { return next; }
643
645 const TiXmlNode* NextSibling( const char * ) const;
646 TiXmlNode* NextSibling( const char* _next ) {
647 return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
648 }
649
654 const TiXmlElement* NextSiblingElement() const;
655 TiXmlElement* NextSiblingElement() {
656 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
657 }
658
663 const TiXmlElement* NextSiblingElement( const char * ) const;
664 TiXmlElement* NextSiblingElement( const char *_next ) {
665 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
666 }
667
668 #ifdef TIXML_USE_STL
669 const TiXmlElement* NextSiblingElement( const std::string& _value) const { return NextSiblingElement (_value.c_str ()); }
670 TiXmlElement* NextSiblingElement( const std::string& _value) { return NextSiblingElement (_value.c_str ()); }
671 #endif
672
674 const TiXmlElement* FirstChildElement() const;
675 TiXmlElement* FirstChildElement() {
676 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
677 }
678
680 const TiXmlElement* FirstChildElement( const char * _value ) const;
681 TiXmlElement* FirstChildElement( const char * _value ) {
682 return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
683 }
684
685 #ifdef TIXML_USE_STL
686 const TiXmlElement* FirstChildElement( const std::string& _value ) const { return FirstChildElement (_value.c_str ()); }
687 TiXmlElement* FirstChildElement( const std::string& _value ) { return FirstChildElement (_value.c_str ()); }
688 #endif
689
694 int Type() const { return type; }
695
699 const TiXmlDocument* GetDocument() const;
700 TiXmlDocument* GetDocument() {
701 return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
702 }
703
705 bool NoChildren() const { return !firstChild; }
706
707 virtual const TiXmlDocument* ToDocument() const { return 0; }
708 virtual const TiXmlElement* ToElement() const { return 0; }
709 virtual const TiXmlComment* ToComment() const { return 0; }
710 virtual const TiXmlUnknown* ToUnknown() const { return 0; }
711 virtual const TiXmlText* ToText() const { return 0; }
712 virtual const TiXmlDeclaration* ToDeclaration() const { return 0; }
713
714 virtual TiXmlDocument* ToDocument() { return 0; }
715 virtual TiXmlElement* ToElement() { return 0; }
716 virtual TiXmlComment* ToComment() { return 0; }
717 virtual TiXmlUnknown* ToUnknown() { return 0; }
718 virtual TiXmlText* ToText() { return 0; }
719 virtual TiXmlDeclaration* ToDeclaration() { return 0; }
720
724 virtual TiXmlNode* Clone() const = 0;
725
748 virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
749
750 protected:
751 TiXmlNode( NodeType _type );
752
753 // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
754 // and the assignment operator.
755 void CopyTo( TiXmlNode* target ) const;
756
757 #ifdef TIXML_USE_STL
758 // The real work of the input operator.
759 virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
760 #endif
761
762 // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
763 TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
764
765 TiXmlNode* parent;
766 NodeType type;
767
768 TiXmlNode* firstChild;
769 TiXmlNode* lastChild;
770
771 TIXML_STRING value;
772
773 TiXmlNode* prev;
774 TiXmlNode* next;
775
776 private:
777 TiXmlNode( const TiXmlNode& ); // not implemented.
778 void operator=( const TiXmlNode& base ); // not allowed.
779 };
780
781
789 class GD_API TiXmlAttribute : public TiXmlBase
790 {
791 friend class TiXmlAttributeSet;
792
793 public:
795 TiXmlAttribute() : TiXmlBase()
796 {
797 document = 0;
798 prev = next = 0;
799 }
800
801 #ifdef TIXML_USE_STL
802 TiXmlAttribute( const std::string& _name, const std::string& _value )
804 {
805 name = _name;
806 value = _value;
807 document = 0;
808 prev = next = 0;
809 }
810 #endif
811
813 TiXmlAttribute( const char * _name, const char * _value )
814 {
815 name = _name;
816 value = _value;
817 document = 0;
818 prev = next = 0;
819 }
820
821 const char* Name() const { return name.c_str(); }
822 const char* Value() const { return value.c_str(); }
823 #ifdef TIXML_USE_STL
824 const std::string& ValueStr() const { return value; }
825 #endif
826 int IntValue() const;
827 double DoubleValue() const;
828
829 // Get the tinyxml string representation
830 const TIXML_STRING& NameTStr() const { return name; }
831
841 int QueryIntValue( int* _value ) const;
843 int QueryDoubleValue( double* _value ) const;
844
845 void SetName( const char* _name ) { name = _name; }
846 void SetValue( const char* _value ) { value = _value; }
847
848 void SetIntValue( int _value );
849 void SetDoubleValue( double _value );
850
851 #ifdef TIXML_USE_STL
852 void SetName( const std::string& _name ) { name = _name; }
855 void SetValue( const std::string& _value ) { value = _value; }
856 #endif
857
859 const TiXmlAttribute* Next() const;
860 TiXmlAttribute* Next() {
861 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() );
862 }
863
865 const TiXmlAttribute* Previous() const;
866 TiXmlAttribute* Previous() {
867 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() );
868 }
869
870 bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
871 bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
872 bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
873
874 /* Attribute parsing starts: first letter of the name
875 returns: the next char after the value end quote
876 */
877 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
878
879 // Prints this Attribute to a FILE stream.
880 virtual void Print( FILE* cfile, int depth ) const {
881 Print( cfile, depth, 0 );
882 }
883 void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
884
885 // [internal use]
886 // Set the document pointer so the attribute can report errors.
887 void SetDocument( TiXmlDocument* doc ) { document = doc; }
888
889 private:
890 TiXmlAttribute( const TiXmlAttribute& ); // not implemented.
891 void operator=( const TiXmlAttribute& base ); // not allowed.
892
893 TiXmlDocument* document; // A pointer back to a document, for error reporting.
894 TIXML_STRING name;
895 TIXML_STRING value;
896 TiXmlAttribute* prev;
897 TiXmlAttribute* next;
898 };
899
900
901 /* A class used to manage a group of attributes.
902 It is only used internally, both by the ELEMENT and the DECLARATION.
903
904 The set can be changed transparent to the Element and Declaration
905 classes that use it, but NOT transparent to the Attribute
906 which has to implement a next() and previous() method. Which makes
907 it a bit problematic and prevents the use of STL.
908
909 This version is implemented with circular lists because:
910 - I like circular lists
911 - it demonstrates some independence from the (typical) doubly linked list.
912 */
913 class GD_API TiXmlAttributeSet
914 {
915 public:
916 TiXmlAttributeSet();
917 ~TiXmlAttributeSet();
918
919 void Add( TiXmlAttribute* attribute );
920 void Remove( TiXmlAttribute* attribute );
921
922 const TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
923 TiXmlAttribute* First() { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
924 const TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
925 TiXmlAttribute* Last() { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
926
927 const TiXmlAttribute* Find( const char* _name ) const;
928 TiXmlAttribute* Find( const char* _name ) {
929 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
930 }
931 #ifdef TIXML_USE_STL
932 const TiXmlAttribute* Find( const std::string& _name ) const;
933 TiXmlAttribute* Find( const std::string& _name ) {
934 return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttributeSet* >(this))->Find( _name ) );
935 }
936
937 #endif
938
939 private:
940 //*ME: Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
941 //*ME: this class must be also use a hidden/disabled copy-constructor !!!
942 TiXmlAttributeSet( const TiXmlAttributeSet& ); // not allowed
943 void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
944
945 TiXmlAttribute sentinel;
946 };
947
948
953 class GD_API TiXmlElement : public TiXmlNode
954 {
955 public:
957 TiXmlElement (const char * in_value);
958
959 #ifdef TIXML_USE_STL
960 TiXmlElement( const std::string& _value );
962 #endif
963
964 TiXmlElement( const TiXmlElement& );
965
966 void operator=( const TiXmlElement& base );
967
968 virtual ~TiXmlElement();
969
973 const char* Attribute( const char* name ) const;
974
981 const char* Attribute( const char* name, int* i ) const;
982
989 const char* Attribute( const char* name, double* d ) const;
990
998 int QueryIntAttribute( const char* name, int* _value ) const;
1000 int QueryDoubleAttribute( const char* name, double* _value ) const;
1002 int QueryFloatAttribute( const char* name, float* _value ) const {
1003 double d;
1004 int result = QueryDoubleAttribute( name, &d );
1005 if ( result == TIXML_SUCCESS ) {
1006 *_value = (float)d;
1007 }
1008 return result;
1009 }
1010
1011 #ifdef TIXML_USE_STL
1012
1020 template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
1021 {
1022 const TiXmlAttribute* node = attributeSet.Find( name );
1023 if ( !node )
1024 return TIXML_NO_ATTRIBUTE;
1025
1026 std::stringstream sstream( node->ValueStr() );
1027 sstream >> *outValue;
1028 if ( !sstream.fail() )
1029 return TIXML_SUCCESS;
1030 return TIXML_WRONG_TYPE;
1031 }
1032 /*
1033 This is - in theory - a bug fix for "QueryValueAtribute returns truncated std::string"
1034 but template specialization is hard to get working cross-compiler. Leaving the bug for now.
1035
1036 // The above will fail for std::string because the space character is used as a seperator.
1037 // Specialize for strings. Bug [ 1695429 ] QueryValueAtribute returns truncated std::string
1038 template<> int QueryValueAttribute( const std::string& name, std::string* outValue ) const
1039 {
1040 const TiXmlAttribute* node = attributeSet.Find( name );
1041 if ( !node )
1042 return TIXML_NO_ATTRIBUTE;
1043 *outValue = node->ValueStr();
1044 return TIXML_SUCCESS;
1045 }
1046 */
1047 #endif
1048
1052 void SetAttribute( const char* name, const char * _value );
1053
1054 #ifdef TIXML_USE_STL
1055 const std::string* Attribute( const std::string& name ) const;
1056 const std::string* Attribute( const std::string& name, int* i ) const;
1057 const std::string* Attribute( const std::string& name, double* d ) const;
1058 int QueryIntAttribute( const std::string& name, int* _value ) const;
1059 int QueryDoubleAttribute( const std::string& name, double* _value ) const;
1060
1062 void SetAttribute( const std::string& name, const std::string& _value );
1064 void SetAttribute( const std::string& name, int _value );
1065 #endif
1066
1070 void SetAttribute( const char * name, int value );
1071
1075 void SetDoubleAttribute( const char * name, double value );
1076
1079 void RemoveAttribute( const char * name );
1080 #ifdef TIXML_USE_STL
1081 void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); }
1082 #endif
1083
1084 const TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); }
1085 TiXmlAttribute* FirstAttribute() { return attributeSet.First(); }
1086 const TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); }
1087 TiXmlAttribute* LastAttribute() { return attributeSet.Last(); }
1088
1121 const char* GetText() const;
1122
1124 virtual TiXmlNode* Clone() const;
1125 // Print the Element to a FILE stream.
1126 virtual void Print( FILE* cfile, int depth ) const;
1127
1128 /* Attribtue parsing starts: next char past '<'
1129 returns: next char past '>'
1130 */
1131 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1132
1133 virtual const TiXmlElement* ToElement() const { return this; }
1134 virtual TiXmlElement* ToElement() { return this; }
1135
1138 virtual bool Accept( TiXmlVisitor* visitor ) const;
1139
1140 protected:
1141
1142 void CopyTo( TiXmlElement* target ) const;
1143 void ClearThis(); // like clear, but initializes 'this' object as well
1144
1145 // Used to be public [internal use]
1146 #ifdef TIXML_USE_STL
1147 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1148 #endif
1149 /* [internal use]
1150 Reads the "value" of the element -- another element, or text.
1151 This should terminate with the current end tag.
1152 */
1153 const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
1154
1155 private:
1156
1157 TiXmlAttributeSet attributeSet;
1158 };
1159
1160
1163 class GD_API TiXmlComment : public TiXmlNode
1164 {
1165 public:
1167 TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
1169 TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::COMMENT ) {
1170 SetValue( _value );
1171 }
1172 TiXmlComment( const TiXmlComment& );
1173 void operator=( const TiXmlComment& base );
1174
1175 virtual ~TiXmlComment() {}
1176
1178 virtual TiXmlNode* Clone() const;
1179 // Write this Comment to a FILE stream.
1180 virtual void Print( FILE* cfile, int depth ) const;
1181
1182 /* Attribtue parsing starts: at the ! of the !--
1183 returns: next char past '>'
1184 */
1185 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1186
1187 virtual const TiXmlComment* ToComment() const { return this; }
1188 virtual TiXmlComment* ToComment() { return this; }
1189
1192 virtual bool Accept( TiXmlVisitor* visitor ) const;
1193
1194 protected:
1195 void CopyTo( TiXmlComment* target ) const;
1196
1197 // used to be public
1198 #ifdef TIXML_USE_STL
1199 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1200 #endif
1201 // virtual void StreamOut( TIXML_OSTREAM * out ) const;
1202
1203 private:
1204
1205 };
1206
1207
1213 class GD_API TiXmlText : public TiXmlNode
1214 {
1215 friend class TiXmlElement;
1216 public:
1221 TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TEXT)
1222 {
1223 SetValue( initValue );
1224 cdata = false;
1225 }
1226 virtual ~TiXmlText() {}
1227
1228 #ifdef TIXML_USE_STL
1229 TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TEXT)
1231 {
1232 SetValue( initValue );
1233 cdata = false;
1234 }
1235 #endif
1236
1237 TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TEXT ) { copy.CopyTo( this ); }
1238 void operator=( const TiXmlText& base ) { base.CopyTo( this ); }
1239
1240 // Write this text object to a FILE stream.
1241 virtual void Print( FILE* cfile, int depth ) const;
1242
1244 bool CDATA() const { return cdata; }
1246 void SetCDATA( bool _cdata ) { cdata = _cdata; }
1247
1248 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1249
1250 virtual const TiXmlText* ToText() const { return this; }
1251 virtual TiXmlText* ToText() { return this; }
1252
1255 virtual bool Accept( TiXmlVisitor* content ) const;
1256
1257 protected :
1259 virtual TiXmlNode* Clone() const;
1260 void CopyTo( TiXmlText* target ) const;
1261
1262 bool Blank() const; // returns true if all white space and new lines
1263 // [internal use]
1264 #ifdef TIXML_USE_STL
1265 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1266 #endif
1267
1268 private:
1269 bool cdata; // true if this should be input and output as a CDATA style text element
1270 };
1271
1272
1286 class GD_API TiXmlDeclaration : public TiXmlNode
1287 {
1288 public:
1290 TiXmlDeclaration() : TiXmlNode( TiXmlNode::DECLARATION ) {}
1291
1292 #ifdef TIXML_USE_STL
1293 TiXmlDeclaration( const std::string& _version,
1295 const std::string& _encoding,
1296 const std::string& _standalone );
1297 #endif
1298
1300 TiXmlDeclaration( const char* _version,
1301 const char* _encoding,
1302 const char* _standalone );
1303
1304 TiXmlDeclaration( const TiXmlDeclaration& copy );
1305 void operator=( const TiXmlDeclaration& copy );
1306
1307 virtual ~TiXmlDeclaration() {}
1308
1310 const char *Version() const { return version.c_str (); }
1312 const char *Encoding() const { return encoding.c_str (); }
1314 const char *Standalone() const { return standalone.c_str (); }
1315
1317 virtual TiXmlNode* Clone() const;
1318 // Print this declaration to a FILE stream.
1319 virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
1320 virtual void Print( FILE* cfile, int depth ) const {
1321 Print( cfile, depth, 0 );
1322 }
1323
1324 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1325
1326 virtual const TiXmlDeclaration* ToDeclaration() const { return this; }
1327 virtual TiXmlDeclaration* ToDeclaration() { return this; }
1328
1331 virtual bool Accept( TiXmlVisitor* visitor ) const;
1332
1333 protected:
1334 void CopyTo( TiXmlDeclaration* target ) const;
1335 // used to be public
1336 #ifdef TIXML_USE_STL
1337 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1338 #endif
1339
1340 private:
1341
1342 TIXML_STRING version;
1343 TIXML_STRING encoding;
1344 TIXML_STRING standalone;
1345 };
1346
1347
1355 class GD_API TiXmlUnknown : public TiXmlNode
1356 {
1357 public:
1358 TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
1359 virtual ~TiXmlUnknown() {}
1360
1361 TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::UNKNOWN ) { copy.CopyTo( this ); }
1362 void operator=( const TiXmlUnknown& copy ) { copy.CopyTo( this ); }
1363
1365 virtual TiXmlNode* Clone() const;
1366 // Print this Unknown to a FILE stream.
1367 virtual void Print( FILE* cfile, int depth ) const;
1368
1369 virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1370
1371 virtual const TiXmlUnknown* ToUnknown() const { return this; }
1372 virtual TiXmlUnknown* ToUnknown() { return this; }
1373
1376 virtual bool Accept( TiXmlVisitor* content ) const;
1377
1378 protected:
1379 void CopyTo( TiXmlUnknown* target ) const;
1380
1381 #ifdef TIXML_USE_STL
1382 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1383 #endif
1384
1385 private:
1386
1387 };
1388
1389
1394 class GD_API TiXmlDocument : public TiXmlNode
1395 {
1396 public:
1398 TiXmlDocument();
1400 TiXmlDocument( const char * documentName );
1401
1402 #ifdef TIXML_USE_STL
1403 TiXmlDocument( const std::string& documentName );
1405 #endif
1406
1407 TiXmlDocument( const TiXmlDocument& copy );
1408 void operator=( const TiXmlDocument& copy );
1409
1410 virtual ~TiXmlDocument() {}
1411
1416 bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1418 bool SaveFile() const;
1420 bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1422 bool SaveFile( const char * filename ) const;
1428 bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1430 bool SaveFile( FILE* ) const;
1431
1432 #ifdef TIXML_USE_STL
1433 bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING )
1434 {
1435 // StringToBuffer f( filename );
1436 // return ( f.buffer && LoadFile( f.buffer, encoding ));
1437 return LoadFile( filename.c_str(), encoding );
1438 }
1439 bool SaveFile( const std::string& filename ) const
1440 {
1441 // StringToBuffer f( filename );
1442 // return ( f.buffer && SaveFile( f.buffer ));
1443 return SaveFile( filename.c_str() );
1444 }
1445 #endif
1446
1451 virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1452
1457 const TiXmlElement* RootElement() const { return FirstChildElement(); }
1458 TiXmlElement* RootElement() { return FirstChildElement(); }
1459
1465 bool Error() const { return error; }
1466
1468 const char * ErrorDesc() const { return errorDesc.c_str (); }
1469
1473 int ErrorId() const { return errorId; }
1474
1482 int ErrorRow() const { return errorLocation.row+1; }
1483 int ErrorCol() const { return errorLocation.col+1; }
1484
1509 void SetTabSize( int _tabsize ) { tabsize = _tabsize; }
1510
1511 int TabSize() const { return tabsize; }
1512
1516 void ClearError() { error = false;
1517 errorId = 0;
1518 errorDesc = "";
1519 errorLocation.row = errorLocation.col = 0;
1520 //errorLocation.last = 0;
1521 }
1522
1524 void Print() const { Print( stdout, 0 ); }
1525
1526 /* Write the document to a string using formatted printing ("pretty print"). This
1527 will allocate a character array (new char[]) and return it as a pointer. The
1528 calling code pust call delete[] on the return char* to avoid a memory leak.
1529 */
1530 //char* PrintToMemory() const;
1531
1533 virtual void Print( FILE* cfile, int depth = 0 ) const;
1534 // [internal use]
1535 void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
1536
1537 virtual const TiXmlDocument* ToDocument() const { return this; }
1538 virtual TiXmlDocument* ToDocument() { return this; }
1539
1542 virtual bool Accept( TiXmlVisitor* content ) const;
1543
1544 protected :
1545 // [internal use]
1546 virtual TiXmlNode* Clone() const;
1547 #ifdef TIXML_USE_STL
1548 virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1549 #endif
1550
1551 private:
1552 void CopyTo( TiXmlDocument* target ) const;
1553
1554 bool error;
1555 int errorId;
1556 TIXML_STRING errorDesc;
1557 int tabsize;
1558 TiXmlCursor errorLocation;
1559 bool useMicrosoftBOM; // the UTF-8 BOM were found when read. Note this, and try to write.
1560 };
1561
1562
1643 class GD_API TiXmlHandle
1644 {
1645 public:
1647 TiXmlHandle( TiXmlNode* _node ) { this->node = _node; }
1649 TiXmlHandle( const TiXmlHandle& ref ) { this->node = ref.node; }
1650 TiXmlHandle operator=( const TiXmlHandle& ref ) { this->node = ref.node; return *this; }
1651
1653 TiXmlHandle FirstChild() const;
1655 TiXmlHandle FirstChild( const char * value ) const;
1657 TiXmlHandle FirstChildElement() const;
1659 TiXmlHandle FirstChildElement( const char * value ) const;
1660
1664 TiXmlHandle Child( const char* value, int index ) const;
1668 TiXmlHandle Child( int index ) const;
1673 TiXmlHandle ChildElement( const char* value, int index ) const;
1678 TiXmlHandle ChildElement( int index ) const;
1679
1680 #ifdef TIXML_USE_STL
1681 TiXmlHandle FirstChild( const std::string& _value ) const { return FirstChild( _value.c_str() ); }
1682 TiXmlHandle FirstChildElement( const std::string& _value ) const { return FirstChildElement( _value.c_str() ); }
1683
1684 TiXmlHandle Child( const std::string& _value, int index ) const { return Child( _value.c_str(), index ); }
1685 TiXmlHandle ChildElement( const std::string& _value, int index ) const { return ChildElement( _value.c_str(), index ); }
1686 #endif
1687
1690 TiXmlNode* ToNode() const { return node; }
1693 TiXmlElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
1696 TiXmlText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
1699 TiXmlUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
1700
1704 TiXmlNode* Node() const { return ToNode(); }
1708 TiXmlElement* Element() const { return ToElement(); }
1712 TiXmlText* Text() const { return ToText(); }
1716 TiXmlUnknown* Unknown() const { return ToUnknown(); }
1717
1718 private:
1719 TiXmlNode* node;
1720 };
1721
1722
1742 class GD_API TiXmlPrinter : public TiXmlVisitor
1743 {
1744 public:
1745 TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
1746 buffer(), indent( " " ), lineBreak( "\n" ) {}
1747
1748 virtual bool VisitEnter( const TiXmlDocument& doc );
1749 virtual bool VisitExit( const TiXmlDocument& doc );
1750
1751 virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
1752 virtual bool VisitExit( const TiXmlElement& element );
1753
1754 virtual bool Visit( const TiXmlDeclaration& declaration );
1755 virtual bool Visit( const TiXmlText& text );
1756 virtual bool Visit( const TiXmlComment& comment );
1757 virtual bool Visit( const TiXmlUnknown& unknown );
1758
1762 void SetIndent( const char* _indent ) { indent = _indent ? _indent : "" ; }
1764 const char* Indent() { return indent.c_str(); }
1769 void SetLineBreak( const char* _lineBreak ) { lineBreak = _lineBreak ? _lineBreak : ""; }
1771 const char* LineBreak() { return lineBreak.c_str(); }
1772
1776 void SetStreamPrinting() { indent = "";
1777 lineBreak = "";
1778 }
1780 const char* CStr() { return buffer.c_str(); }
1782 size_t Size() { return buffer.size(); }
1783
1784 #ifdef TIXML_USE_STL
1785 const std::string& Str() { return buffer; }
1787 #endif
1788
1789 private:
1790 void DoIndent() {
1791 for( int i=0; i<depth; ++i )
1792 buffer += indent;
1793 }
1794 void DoLineBreak() {
1795 buffer += lineBreak;
1796 }
1797
1798 int depth;
1799 bool simpleTextPrint;
1800 TIXML_STRING buffer;
1801 TIXML_STRING indent;
1802 TIXML_STRING lineBreak;
1803 };
1804
1805
1806 #ifdef _MSC_VER
1807 #pragma warning( pop )
1808 #endif
1809
1810 #endif
1811
1812
1813 #endif