GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
TinyXml.hpp
Go to the documentation of this file.
1 /*
2  www.sourceforge.net/projects/tinyxml
3  Original code by 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 
25 
26 // ----------------------------
27 // - Customization for granoo -
28 // ----------------------------
29 // Remove 'TIXML_USE_STL' #define macro because GranOO uses STL !
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 
33 #ifndef TINYXML_INCLUDED
34 #define TINYXML_INCLUDED
35 
36 #ifdef _MSC_VER
37 #pragma warning( push )
38 #pragma warning( disable : 4530 )
39 #pragma warning( disable : 4786 )
40 #endif
41 
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <assert.h>
47 
48 // Help out windows:
49 #if defined( _DEBUG ) && !defined( DEBUG )
50 #define DEBUG
51 #endif
52 
53 #include <string>
54 #include <iostream>
55 #include <sstream>
56 #define TIXML_STRING std::string
57 
58 // Deprecated library function hell. Compilers want to use the
59 // new safe versions. This probably doesn't fully address the problem,
60 // but it gets closer. There are too many compilers for me to fully
61 // test. If you get compilation troubles, undefine TIXML_SAFE
62 #define TIXML_SAFE
63 
64 #ifdef TIXML_SAFE
65  #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
66 // Microsoft visual studio, version 2005 and higher.
67  #define TIXML_SNPRINTF _snprintf_s
68  #define TIXML_SSCANF sscanf_s
69  #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
70 // Microsoft visual studio, version 6 and higher.
71 //#pragma message( "Using _sn* functions." )
72  #define TIXML_SNPRINTF _snprintf
73  #define TIXML_SSCANF sscanf
74  #elif defined(__GNUC__) && (__GNUC__ >= 3 )
75 // GCC version 3 and higher.s
76 //#warning( "Using sn* functions." )
77  #define TIXML_SNPRINTF snprintf
78  #define TIXML_SSCANF sscanf
79  #else
80  #define TIXML_SNPRINTF snprintf
81  #define TIXML_SSCANF sscanf
82  #endif
83 #endif
84 
85 class TiXmlDocument;
86 class TiXmlElement;
87 class TiXmlComment;
88 class TiXmlUnknown;
89 class TiXmlAttribute;
90 class TiXmlText;
91 class TiXmlDeclaration;
92 class TiXmlParsingData;
93 
94 const int TIXML_MAJOR_VERSION = 2;
95 const int TIXML_MINOR_VERSION = 6;
96 const int TIXML_PATCH_VERSION = 2;
97 
98 /* Internal structure for tracking location of items
99  in the XML file.
100  */
101 struct TiXmlCursor
102 {
103  TiXmlCursor() {
104  clear();
105  }
106 
107  void clear() {
108  row = col = -1;
109  }
110 
111  int row; // 0 based.
112  int col; // 0 based.
113 };
114 
115 
135 class TiXmlVisitor
136 {
137 public:
138  virtual ~TiXmlVisitor() {
139  }
140 
142  virtual bool VisitEnter( const TiXmlDocument& /*doc*/ ) {
143  return true;
144  }
145 
147  virtual bool VisitExit( const TiXmlDocument& /*doc*/ ) {
148  return true;
149  }
150 
152  virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ ) {
153  return true;
154  }
155 
157  virtual bool VisitExit( const TiXmlElement& /*element*/ ) {
158  return true;
159  }
160 
162  virtual bool Visit( const TiXmlDeclaration& /*declaration*/ ) {
163  return true;
164  }
165 
167  virtual bool Visit( const TiXmlText& /*text*/ ) {
168  return true;
169  }
170 
172  virtual bool Visit( const TiXmlComment& /*comment*/ ) {
173  return true;
174  }
175 
177  virtual bool Visit( const TiXmlUnknown& /*unknown*/ ) {
178  return true;
179  }
180 
181 };
182 
183 // Only used by Attribute::Query functions
184 enum
185 {
186  TIXML_SUCCESS,
187  TIXML_NO_ATTRIBUTE,
188  TIXML_WRONG_TYPE
189 };
190 
191 
192 // Used by the parsing routines.
193 enum TiXmlEncoding
194 {
195  TIXML_ENCODING_UNKNOWN,
196  TIXML_ENCODING_UTF8,
197  TIXML_ENCODING_LEGACY
198 };
199 
200 const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
201 
224 class TiXmlBase
225 {
226  friend class TiXmlNode;
227  friend class TiXmlElement;
228  friend class TiXmlDocument;
229 
230 public:
231  TiXmlBase() : userData(0) {
232  }
233 
234  virtual ~TiXmlBase() {
235  }
236 
246  virtual void Print( FILE* cfile, int depth ) const = 0;
247 
254  static void SetCondenseWhiteSpace( bool condense ) {
255  condenseWhiteSpace = condense;
256  }
257 
259  static bool IsWhiteSpaceCondensed() {
260  return condenseWhiteSpace;
261  }
262 
281  int Row() const {
282  return location.row + 1;
283  }
284 
285  int Column() const {
286  return location.col + 1;
287  }
288 
289  void SetUserData( void* user ) {
290  userData = user;
291  }
292 
293  void* GetUserData() {
294  return userData;
295  }
296 
297  const void* GetUserData() const {
298  return userData;
299  }
300 
301  // Table that returs, for a given lead byte, the total number of bytes
302  // in the UTF-8 sequence.
303  static const int utf8ByteTable[256];
304 
305  virtual const char* Parse( const char* p,
306  TiXmlParsingData* data,
307  TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
308 
312  static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out );
313 
314  enum
315  {
316  TIXML_NO_ERROR = 0,
317  TIXML_ERROR,
318  TIXML_ERROR_OPENING_FILE,
319  TIXML_ERROR_PARSING_ELEMENT,
320  TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
321  TIXML_ERROR_READING_ELEMENT_VALUE,
322  TIXML_ERROR_READING_ATTRIBUTES,
323  TIXML_ERROR_PARSING_EMPTY,
324  TIXML_ERROR_READING_END_TAG,
325  TIXML_ERROR_PARSING_UNKNOWN,
326  TIXML_ERROR_PARSING_COMMENT,
327  TIXML_ERROR_PARSING_DECLARATION,
328  TIXML_ERROR_DOCUMENT_EMPTY,
329  TIXML_ERROR_EMBEDDED_NULL,
330  TIXML_ERROR_PARSING_CDATA,
331  TIXML_ERROR_DOCUMENT_TOP_ONLY,
332 
333  TIXML_ERROR_STRING_COUNT
334  };
335 
336 protected:
337  static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
338 
339  inline static bool IsWhiteSpace( char c ) {
340  return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
341  }
342 
343  inline static bool IsWhiteSpace( int c ) {
344  if ( c < 256 )
345  return IsWhiteSpace( (char) c );
346  return false; // Again, only truly correct for English/Latin...but usually works.
347  }
348 
349  static bool StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
350  static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
351 
352  /* Reads an XML name into the string provided. Returns
353  a pointer just past the last character of the name,
354  or 0 if the function has an error.
355  */
356  static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
357 
358  /* Reads text. Returns a pointer past the given end tag.
359  Wickedly complex options, but it keeps the (sensitive) code in one place.
360  */
361  static const char* ReadText( const char* in, // where to start
362  TIXML_STRING* text, // the string read
363  bool ignoreWhiteSpace, // whether to keep the white space
364  const char* endTag, // what ends this text
365  bool ignoreCase, // whether to ignore case in the end tag
366  TiXmlEncoding encoding ); // the current encoding
367 
368  // If an entity has been found, transform it into a character.
369  static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
370 
371  // Get a character, while interpreting entities.
372  // The length can be from 0 to 4 bytes.
373  inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding ) {
374  assert( p );
375  if ( encoding == TIXML_ENCODING_UTF8 ) {
376  *length = utf8ByteTable[ *((const unsigned char*)p) ];
377  assert( *length >= 0 && *length < 5 );
378  } else {
379  *length = 1;
380  }
381 
382  if ( *length == 1 ) {
383  if ( *p == '&' )
384  return GetEntity( p, _value, length, encoding );
385  *_value = *p;
386  return p+1;
387  } else if ( *length ) {
388  //strncpy( _value, p, *length ); // lots of compilers don't like this function (unsafe),
389  // and the null terminator isn't needed
390  for ( int i = 0; p[i] && i<*length; ++i ) {
391  _value[i] = p[i];
392  }
393  return p + (*length);
394  } else {
395  // Not valid text.
396  return 0;
397  }
398  }
399 
400  // Return true if the next characters in the stream are any of the endTag sequences.
401  // Ignore case only works for english, and should only be relied on when comparing
402  // to English words: StringEqual( p, "version", true ) is fine.
403  static bool StringEqual( const char* p,
404  const char* endTag,
405  bool ignoreCase,
406  TiXmlEncoding encoding );
407 
408  static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
409 
410  TiXmlCursor location;
411 
413  void* userData;
414 
415  // None of these methods are reliable for any language except English.
416  // Good for approximation, not great for accuracy.
417  static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
418  static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
419  inline static int ToLower( int v, TiXmlEncoding encoding ) {
420  if ( encoding == TIXML_ENCODING_UTF8 ) {
421  if ( v < 128 )
422  return tolower( v );
423  return v;
424  } else {
425  return tolower( v );
426  }
427  }
428 
429  static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
430 
431 private:
432  TiXmlBase( const TiXmlBase& ); // not implemented.
433  void operator=( const TiXmlBase& base ); // not allowed.
434 
435  struct Entity
436  {
437  const char* str;
438  unsigned int strLength;
439  char chr;
440  };
441  enum
442  {
443  NUM_ENTITY = 5,
444  MAX_ENTITY_LENGTH = 6
445 
446  };
447  static Entity entity[ NUM_ENTITY ];
448  static bool condenseWhiteSpace;
449 };
450 
451 
458 class TiXmlNode : public TiXmlBase
459 {
460  friend class TiXmlDocument;
461  friend class TiXmlElement;
462 
463 public:
467  friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
468 
485  friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
486 
488  friend std::string& operator<< (std::string& out, const TiXmlNode& base );
489 
493  enum NodeType
494  {
495  TINYXML_DOCUMENT,
496  TINYXML_ELEMENT,
497  TINYXML_COMMENT,
498  TINYXML_UNKNOWN,
499  TINYXML_TEXT,
500  TINYXML_DECLARATION,
501  TINYXML_TYPECOUNT
502  };
503 
504  virtual ~TiXmlNode();
505 
518  const char *Value() const {
519  return value.c_str ();
520  }
521 
526  const std::string& ValueStr() const {
527  return value;
528  }
529 
530  const TIXML_STRING& ValueTStr() const {
531  return value;
532  }
533 
543  void SetValue(const char * _value) {
544  value = _value;
545  }
546 
548  void SetValue( const std::string& _value ) {
549  value = _value;
550  }
551 
553  void clear();
554 
556  TiXmlNode* Parent() {
557  return parent;
558  }
559 
560  const TiXmlNode* Parent() const {
561  return parent;
562  }
563 
564  const TiXmlNode* FirstChild() const {
565  return firstChild;
566  }
567 
568  TiXmlNode* FirstChild() {
569  return firstChild;
570  }
571 
572  const TiXmlNode* FirstChild( const char * value ) const;
574  TiXmlNode* FirstChild( const char * _value ) {
575  // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
576  // call the method, cast the return back to non-const.
577  return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
578  }
579 
580  const TiXmlNode* LastChild() const {
581  return lastChild;
582  }
583 
584  TiXmlNode* LastChild() {
585  return lastChild;
586  }
587 
588  const TiXmlNode* LastChild( const char * value ) const;
589  TiXmlNode* LastChild( const char * _value ) {
590  return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
591  }
592 
593  const TiXmlNode* FirstChild( const std::string& _value ) const {
594  return FirstChild (_value.c_str ());
595  }
596 
597  TiXmlNode* FirstChild( const std::string& _value ) {
598  return FirstChild (_value.c_str ());
599  }
600 
601  const TiXmlNode* LastChild( const std::string& _value ) const {
602  return LastChild (_value.c_str ());
603  }
604 
605  TiXmlNode* LastChild( const std::string& _value ) {
606  return LastChild (_value.c_str ());
607  }
608 
625  const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
626  TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
627  return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
628  }
629 
631  const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
632  TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
633  return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
634  }
635 
636  const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const {
637  return IterateChildren (_value.c_str (), previous);
638  }
639 
640  TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) {
641  return IterateChildren (_value.c_str (), previous);
642  }
643 
647  TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
648 
649 
659  TiXmlNode* LinkEndChild( TiXmlNode* addThis );
660 
664  TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
665 
669  TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
670 
674  TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
675 
677  bool RemoveChild( TiXmlNode* removeThis );
678 
680  const TiXmlNode* PreviousSibling() const {
681  return prev;
682  }
683 
684  TiXmlNode* PreviousSibling() {
685  return prev;
686  }
687 
689  const TiXmlNode* PreviousSibling( const char * ) const;
690  TiXmlNode* PreviousSibling( const char *_prev ) {
691  return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
692  }
693 
694  const TiXmlNode* PreviousSibling( const std::string& _value ) const {
695  return PreviousSibling (_value.c_str ());
696  }
697 
698  TiXmlNode* PreviousSibling( const std::string& _value ) {
699  return PreviousSibling (_value.c_str ());
700  }
701 
702  const TiXmlNode* NextSibling( const std::string& _value) const {
703  return NextSibling (_value.c_str ());
704  }
705 
706  TiXmlNode* NextSibling( const std::string& _value) {
707  return NextSibling (_value.c_str ());
708  }
709 
711  const TiXmlNode* NextSibling() const {
712  return next;
713  }
714 
715  TiXmlNode* NextSibling() {
716  return next;
717  }
718 
720  const TiXmlNode* NextSibling( const char * ) const;
721  TiXmlNode* NextSibling( const char* _next ) {
722  return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
723  }
724 
729  const TiXmlElement* NextSiblingElement() const;
730  TiXmlElement* NextSiblingElement() {
731  return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
732  }
733 
738  const TiXmlElement* NextSiblingElement( const char * ) const;
739  TiXmlElement* NextSiblingElement( const char *_next ) {
740  return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
741  }
742 
743  const TiXmlElement* NextSiblingElement( const std::string& _value) const {
744  return NextSiblingElement (_value.c_str ());
745  }
746 
747  TiXmlElement* NextSiblingElement( const std::string& _value) {
748  return NextSiblingElement (_value.c_str ());
749  }
750 
752  const TiXmlElement* FirstChildElement() const;
753  TiXmlElement* FirstChildElement() {
754  return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
755  }
756 
758  const TiXmlElement* FirstChildElement( const char * _value ) const;
759  TiXmlElement* FirstChildElement( const char * _value ) {
760  return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
761  }
762 
763  const TiXmlElement* FirstChildElement( const std::string& _value ) const {
764  return FirstChildElement (_value.c_str ());
765  }
766 
767  TiXmlElement* FirstChildElement( const std::string& _value ) {
768  return FirstChildElement (_value.c_str ());
769  }
770 
775  int Type() const {
776  return type;
777  }
778 
782  const TiXmlDocument* GetDocument() const;
783  TiXmlDocument* GetDocument() {
784  return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
785  }
786 
788  bool NoChildren() const {
789  return !firstChild;
790  }
791 
792  virtual const TiXmlDocument* ToDocument() const {
793  return 0;
794  }
795 
796  virtual const TiXmlElement* ToElement() const {
797  return 0;
798  }
799 
800  virtual const TiXmlComment* ToComment() const {
801  return 0;
802  }
803 
804  virtual const TiXmlUnknown* ToUnknown() const {
805  return 0;
806  }
807 
808  virtual const TiXmlText* ToText() const {
809  return 0;
810  }
811 
812  virtual const TiXmlDeclaration* ToDeclaration() const {
813  return 0;
814  }
815 
816  virtual TiXmlDocument* ToDocument() {
817  return 0;
818  }
819 
820  virtual TiXmlElement* ToElement() {
821  return 0;
822  }
823 
824  virtual TiXmlComment* ToComment() {
825  return 0;
826  }
827 
828  virtual TiXmlUnknown* ToUnknown() {
829  return 0;
830  }
831 
832  virtual TiXmlText* ToText() {
833  return 0;
834  }
835 
836  virtual TiXmlDeclaration* ToDeclaration() {
837  return 0;
838  }
839 
843  virtual TiXmlNode* Clone() const = 0;
844 
867  virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
868 
869 protected:
870  TiXmlNode( NodeType _type );
871 
872  // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
873  // and the assignment operator.
874  void CopyTo( TiXmlNode* target ) const;
875 
876  // The real work of the input operator.
877  virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
878 
879  // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
880  TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
881 
882  TiXmlNode* parent;
883  NodeType type;
884 
885  TiXmlNode* firstChild;
886  TiXmlNode* lastChild;
887 
888  TIXML_STRING value;
889 
890  TiXmlNode* prev;
891  TiXmlNode* next;
892 
893 private:
894  TiXmlNode( const TiXmlNode& ); // not implemented.
895  void operator=( const TiXmlNode& base ); // not allowed.
896 };
897 
898 
906 class TiXmlAttribute : public TiXmlBase
907 {
908  friend class TiXmlAttributeSet;
909 
910 public:
912  TiXmlAttribute() : TiXmlBase() {
913  document = 0;
914  prev = next = 0;
915  }
916 
918  TiXmlAttribute( const std::string& _name, const std::string& _value ) {
919  name = _name;
920  value = _value;
921  document = 0;
922  prev = next = 0;
923  }
924 
926  TiXmlAttribute( const char * _name, const char * _value ) {
927  name = _name;
928  value = _value;
929  document = 0;
930  prev = next = 0;
931  }
932 
933  const char* Name() const {
934  return name.c_str();
935  }
936 
937  const char* Value() const {
938  return value.c_str();
939  }
940 
941  const std::string& ValueStr() const {
942  return value;
943  }
944 
945  int IntValue() const;
946  double DoubleValue() const;
947 
948  // Get the tinyxml string representation
949  const TIXML_STRING& NameTStr() const {
950  return name;
951  }
952 
962  int QueryIntValue( int* _value ) const;
964  int QueryDoubleValue( double* _value ) const;
965 
966  void SetName( const char* _name ) {
967  name = _name;
968  }
969 
970  void SetValue( const char* _value ) {
971  value = _value;
972  }
973 
974  void SetIntValue( int _value );
975  void SetDoubleValue( double _value );
976 
978  void SetName( const std::string& _name ) {
979  name = _name;
980  }
981 
983  void SetValue( const std::string& _value ) {
984  value = _value;
985  }
986 
988  const TiXmlAttribute* Next() const;
989  TiXmlAttribute* Next() {
990  return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() );
991  }
992 
994  const TiXmlAttribute* Previous() const;
995  TiXmlAttribute* Previous() {
996  return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() );
997  }
998 
999  bool operator==( const TiXmlAttribute& rhs ) const {
1000  return rhs.name == name;
1001  }
1002 
1003  bool operator<( const TiXmlAttribute& rhs ) const {
1004  return name < rhs.name;
1005  }
1006 
1007  bool operator>( const TiXmlAttribute& rhs ) const {
1008  return name > rhs.name;
1009  }
1010 
1011  /* Attribute parsing starts: first letter of the name
1012  returns: the next char after the value end quote
1013  */
1014  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1015 
1016  // Prints this Attribute to a FILE stream.
1017  virtual void Print( FILE* cfile, int depth ) const {
1018  Print( cfile, depth, 0 );
1019  }
1020 
1021  void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
1022 
1023  // [internal use]
1024  // Set the document pointer so the attribute can report errors.
1025  void SetDocument( TiXmlDocument* doc ) {
1026  document = doc;
1027  }
1028 
1029 private:
1030  TiXmlAttribute( const TiXmlAttribute& ); // not implemented.
1031  void operator=( const TiXmlAttribute& base ); // not allowed.
1032 
1033  TiXmlDocument* document; // A pointer back to a document, for error reporting.
1034  TIXML_STRING name;
1035  TIXML_STRING value;
1036  TiXmlAttribute* prev;
1037  TiXmlAttribute* next;
1038 };
1039 
1040 
1041 /* A class used to manage a group of attributes.
1042  It is only used internally, both by the ELEMENT and the DECLARATION.
1043 
1044  The set can be changed transparent to the Element and Declaration
1045  classes that use it, but NOT transparent to the Attribute
1046  which has to implement a next() and previous() method. Which makes
1047  it a bit problematic and prevents the use of STL.
1048 
1049  This version is implemented with circular lists because:
1050  - I like circular lists
1051  - it demonstrates some independence from the (typical) doubly linked list.
1052  */
1053 class TiXmlAttributeSet
1054 {
1055 public:
1056  TiXmlAttributeSet();
1057  ~TiXmlAttributeSet();
1058 
1059  void add( TiXmlAttribute* attribute );
1060  void Remove( TiXmlAttribute* attribute );
1061 
1062  const TiXmlAttribute* First() const {
1063  return ( sentinel.next == &sentinel ) ? 0 : sentinel.next;
1064  }
1065 
1066  TiXmlAttribute* First() {
1067  return ( sentinel.next == &sentinel ) ? 0 : sentinel.next;
1068  }
1069 
1070  const TiXmlAttribute* Last() const {
1071  return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev;
1072  }
1073 
1074  TiXmlAttribute* Last() {
1075  return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev;
1076  }
1077 
1078  TiXmlAttribute* Find( const char* _name ) const;
1079  TiXmlAttribute* FindOrCreate( const char* _name );
1080 
1081  TiXmlAttribute* Find( const std::string& _name ) const;
1082  TiXmlAttribute* FindOrCreate( const std::string& _name );
1083 
1084 private:
1085  //*ME: Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
1086  //*ME: this class must be also use a hidden/disabled copy-constructor !!!
1087  TiXmlAttributeSet( const TiXmlAttributeSet& ); // not allowed
1088  void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
1089 
1090  TiXmlAttribute sentinel;
1091 };
1092 
1093 
1098 class TiXmlElement : public TiXmlNode
1099 {
1100 public:
1102  TiXmlElement (const char * in_value);
1103 
1105  TiXmlElement( const std::string& _value );
1106 
1107  TiXmlElement( const TiXmlElement& );
1108 
1109  TiXmlElement& operator=( const TiXmlElement& base );
1110 
1111  virtual ~TiXmlElement();
1112 
1116  const char* Attribute( const char* name ) const;
1117 
1124  const char* Attribute( const char* name, int* i ) const;
1125 
1132  const char* Attribute( const char* name, double* d ) const;
1133 
1141  int QueryIntAttribute( const char* name, int* _value ) const;
1143  int QueryUnsignedAttribute( const char* name, unsigned* _value ) const;
1148  int QueryBoolAttribute( const char* name, bool* _value ) const;
1150  int QueryDoubleAttribute( const char* name, double* _value ) const;
1152  int QueryFloatAttribute( const char* name, float* _value ) const {
1153  double d;
1154  int result = QueryDoubleAttribute( name, &d );
1155  if ( result == TIXML_SUCCESS ) {
1156  *_value = (float)d;
1157  }
1158  return result;
1159  }
1160 
1162  int QueryStringAttribute( const char* name, std::string* _value ) const {
1163  const char* cstr = Attribute( name );
1164  if ( cstr ) {
1165  *_value = std::string( cstr );
1166  return TIXML_SUCCESS;
1167  }
1168  return TIXML_NO_ATTRIBUTE;
1169  }
1170 
1179  template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const {
1180  const TiXmlAttribute* node = attributeSet.Find( name );
1181  if ( !node )
1182  return TIXML_NO_ATTRIBUTE;
1183 
1184  std::stringstream sstream( node->ValueStr() );
1185  sstream >> *outValue;
1186  if ( !sstream.fail() )
1187  return TIXML_SUCCESS;
1188  return TIXML_WRONG_TYPE;
1189  }
1190 
1191  int QueryValueAttribute( const std::string& name, std::string* outValue ) const {
1192  const TiXmlAttribute* node = attributeSet.Find( name );
1193  if ( !node )
1194  return TIXML_NO_ATTRIBUTE;
1195  *outValue = node->ValueStr();
1196  return TIXML_SUCCESS;
1197  }
1198 
1202  void SetAttribute( const char* name, const char * _value );
1203 
1204  const std::string* Attribute( const std::string& name ) const;
1205  const std::string* Attribute( const std::string& name, int* i ) const;
1206  const std::string* Attribute( const std::string& name, double* d ) const;
1207  int QueryIntAttribute( const std::string& name, int* _value ) const;
1208  int QueryDoubleAttribute( const std::string& name, double* _value ) const;
1209 
1211  void SetAttribute( const std::string& name, const std::string& _value );
1213  void SetAttribute( const std::string& name, int _value );
1215  void SetDoubleAttribute( const std::string& name, double value );
1216 
1220  void SetAttribute( const char * name, int value );
1221 
1225  void SetDoubleAttribute( const char * name, double value );
1226 
1229  void RemoveAttribute( const char * name );
1230  void RemoveAttribute( const std::string& name ) {
1231  RemoveAttribute (name.c_str ());
1232  }
1233 
1234  const TiXmlAttribute* FirstAttribute() const {
1235  return attributeSet.First();
1236  }
1237 
1238  TiXmlAttribute* FirstAttribute() {
1239  return attributeSet.First();
1240  }
1241 
1242  const TiXmlAttribute* LastAttribute() const {
1243  return attributeSet.Last();
1244  }
1245 
1246  TiXmlAttribute* LastAttribute() {
1247  return attributeSet.Last();
1248  }
1249 
1282  const char* GetText() const;
1283 
1285  virtual TiXmlNode* Clone() const;
1286  // Print the Element to a FILE stream.
1287  virtual void Print( FILE* cfile, int depth ) const;
1288 
1289  /* Attribtue parsing starts: next char past '<'
1290  returns: next char past '>'
1291  */
1292  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1293 
1294  virtual const TiXmlElement* ToElement() const {
1295  return this;
1296  }
1297 
1298  virtual TiXmlElement* ToElement() {
1299  return this;
1300  }
1301 
1304  virtual bool Accept( TiXmlVisitor* visitor ) const;
1305 
1306 protected:
1307  void CopyTo( TiXmlElement* target ) const;
1308  void ClearThis(); // like clear, but initializes 'this' object as well
1309 
1310  // Used to be public [internal use]
1311  virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1312  /* [internal use]
1313  Reads the "value" of the element -- another element, or text.
1314  This should terminate with the current end tag.
1315  */
1316  const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
1317 
1318 private:
1319  TiXmlAttributeSet attributeSet;
1320 };
1321 
1322 
1325 class TiXmlComment : public TiXmlNode
1326 {
1327 public:
1329  TiXmlComment() : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {
1330  }
1331 
1333  TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {
1334  SetValue( _value );
1335  }
1336 
1337  TiXmlComment( const TiXmlComment& );
1338  TiXmlComment& operator=( const TiXmlComment& base );
1339 
1340  virtual ~TiXmlComment() {
1341  }
1342 
1344  virtual TiXmlNode* Clone() const;
1345  // Write this Comment to a FILE stream.
1346  virtual void Print( FILE* cfile, int depth ) const;
1347 
1348  /* Attribtue parsing starts: at the ! of the !--
1349  returns: next char past '>'
1350  */
1351  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1352 
1353  virtual const TiXmlComment* ToComment() const {
1354  return this;
1355  }
1356 
1357  virtual TiXmlComment* ToComment() {
1358  return this;
1359  }
1360 
1363  virtual bool Accept( TiXmlVisitor* visitor ) const;
1364 
1365 protected:
1366  void CopyTo( TiXmlComment* target ) const;
1367 
1368  // used to be public
1369  virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1370 // virtual void StreamOut( TIXML_OSTREAM * out ) const;
1371 
1372 private:
1373 };
1374 
1375 
1381 class TiXmlText : public TiXmlNode
1382 {
1383  friend class TiXmlElement;
1384 
1385 public:
1390  TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT) {
1391  SetValue( initValue );
1392  cdata = false;
1393  }
1394 
1395  virtual ~TiXmlText() {
1396  }
1397 
1399  TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT) {
1400  SetValue( initValue );
1401  cdata = false;
1402  }
1403 
1404  TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TINYXML_TEXT ) {
1405  copy.CopyTo( this );
1406  }
1407 
1408  TiXmlText& operator=( const TiXmlText& base ) {
1409  base.CopyTo( this ); return *this;
1410  }
1411 
1412  // Write this text object to a FILE stream.
1413  virtual void Print( FILE* cfile, int depth ) const;
1414 
1416  bool CDATA() const {
1417  return cdata;
1418  }
1419 
1421  void SetCDATA( bool _cdata ) {
1422  cdata = _cdata;
1423  }
1424 
1425  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1426 
1427  virtual const TiXmlText* ToText() const {
1428  return this;
1429  }
1430 
1431  virtual TiXmlText* ToText() {
1432  return this;
1433  }
1434 
1437  virtual bool Accept( TiXmlVisitor* content ) const;
1438 
1439 protected:
1441  virtual TiXmlNode* Clone() const;
1442  void CopyTo( TiXmlText* target ) const;
1443 
1444  bool Blank() const; // returns true if all white space and new lines
1445  // [internal use]
1446  virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1447 
1448 private:
1449  bool cdata; // true if this should be input and output as a CDATA style text element
1450 };
1451 
1452 
1466 class TiXmlDeclaration : public TiXmlNode
1467 {
1468 public:
1470  TiXmlDeclaration() : TiXmlNode( TiXmlNode::TINYXML_DECLARATION ) {
1471  }
1472 
1474  TiXmlDeclaration( const std::string& _version,
1475  const std::string& _encoding,
1476  const std::string& _standalone );
1478  TiXmlDeclaration( const char* _version,
1479  const char* _encoding,
1480  const char* _standalone );
1481 
1482  TiXmlDeclaration( const TiXmlDeclaration& copy );
1483  TiXmlDeclaration& operator=( const TiXmlDeclaration& copy );
1484 
1485  virtual ~TiXmlDeclaration() {
1486  }
1487 
1489  const char *Version() const {
1490  return version.c_str ();
1491  }
1492 
1494  const char *Encoding() const {
1495  return encoding.c_str ();
1496  }
1497 
1499  const char *Standalone() const {
1500  return standalone.c_str ();
1501  }
1502 
1504  virtual TiXmlNode* Clone() const;
1505  // Print this declaration to a FILE stream.
1506  virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
1507  virtual void Print( FILE* cfile, int depth ) const {
1508  Print( cfile, depth, 0 );
1509  }
1510 
1511  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1512 
1513  virtual const TiXmlDeclaration* ToDeclaration() const {
1514  return this;
1515  }
1516 
1517  virtual TiXmlDeclaration* ToDeclaration() {
1518  return this;
1519  }
1520 
1523  virtual bool Accept( TiXmlVisitor* visitor ) const;
1524 
1525 protected:
1526  void CopyTo( TiXmlDeclaration* target ) const;
1527  // used to be public
1528  virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1529 
1530 private:
1531  TIXML_STRING version;
1532  TIXML_STRING encoding;
1533  TIXML_STRING standalone;
1534 };
1535 
1536 
1544 class TiXmlUnknown : public TiXmlNode
1545 {
1546 public:
1547  TiXmlUnknown() : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) {
1548  }
1549 
1550  virtual ~TiXmlUnknown() {
1551  }
1552 
1553  TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) {
1554  copy.CopyTo( this );
1555  }
1556 
1557  TiXmlUnknown& operator=( const TiXmlUnknown& copy ) {
1558  copy.CopyTo( this ); return *this;
1559  }
1560 
1562  virtual TiXmlNode* Clone() const;
1563  // Print this Unknown to a FILE stream.
1564  virtual void Print( FILE* cfile, int depth ) const;
1565 
1566  virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
1567 
1568  virtual const TiXmlUnknown* ToUnknown() const {
1569  return this;
1570  }
1571 
1572  virtual TiXmlUnknown* ToUnknown() {
1573  return this;
1574  }
1575 
1578  virtual bool Accept( TiXmlVisitor* content ) const;
1579 
1580 protected:
1581  void CopyTo( TiXmlUnknown* target ) const;
1582 
1583  virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1584 
1585 private:
1586 };
1587 
1588 
1593 class TiXmlDocument : public TiXmlNode
1594 {
1595 public:
1597  TiXmlDocument();
1599  TiXmlDocument( const char * documentName );
1600 
1602  TiXmlDocument( const std::string& documentName );
1603 
1604  TiXmlDocument( const TiXmlDocument& copy );
1605  TiXmlDocument& operator=( const TiXmlDocument& copy );
1606 
1607  virtual ~TiXmlDocument() {
1608  }
1609 
1614  bool load_file( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1616  bool SaveFile() const;
1618  bool load_file( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1620  bool SaveFile( const char * filename ) const;
1626  bool load_file( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1628  bool SaveFile( FILE* ) const;
1629 
1630  bool load_file( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ) {
1631  return load_file( filename.c_str(), encoding );
1632  }
1633 
1634  bool SaveFile( const std::string& filename ) const
1635  {
1636  return SaveFile( filename.c_str() );
1637  }
1638 
1643  virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
1644 
1649  const TiXmlElement* RootElement() const {
1650  return FirstChildElement();
1651  }
1652 
1653  TiXmlElement* RootElement() {
1654  return FirstChildElement();
1655  }
1656 
1662  bool Error() const {
1663  return error;
1664  }
1665 
1667  const char * Errordesc() const {
1668  return errorDesc.c_str ();
1669  }
1670 
1674  int ErrorId() const {
1675  return errorId;
1676  }
1677 
1685  int ErrorRow() const {
1686  return errorLocation.row+1;
1687  }
1688 
1689  int ErrorCol() const {
1690  return errorLocation.col+1;
1691  }
1692 
1717  void SetTabSize( int _tabsize ) {
1718  tabsize = _tabsize;
1719  }
1720 
1721  int TabSize() const {
1722  return tabsize;
1723  }
1724 
1728  void ClearError() {
1729  error = false;
1730  errorId = 0;
1731  errorDesc = "";
1732  errorLocation.row = errorLocation.col = 0;
1733  //errorLocation.last = 0;
1734  }
1735 
1737  void Print() const {
1738  Print( stdout, 0 );
1739  }
1740 
1741  /* Write the document to a string using formatted printing ("pretty print"). This
1742  will allocate a character array (new char[]) and return it as a pointer. The
1743  calling code pust call delete[] on the return char* to avoid a memory leak.
1744  */
1745  //char* PrintToMemory() const;
1746 
1748  virtual void Print( FILE* cfile, int depth = 0 ) const;
1749  // [internal use]
1750  void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
1751 
1752  virtual const TiXmlDocument* ToDocument() const {
1753  return this;
1754  }
1755 
1756  virtual TiXmlDocument* ToDocument() {
1757  return this;
1758  }
1759 
1762  virtual bool Accept( TiXmlVisitor* content ) const;
1763 
1764 protected:
1765  // [internal use]
1766  virtual TiXmlNode* Clone() const;
1767  virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
1768 
1769 private:
1770  void CopyTo( TiXmlDocument* target ) const;
1771 
1772  bool error;
1773  int errorId;
1774  TIXML_STRING errorDesc;
1775  int tabsize;
1776  TiXmlCursor errorLocation;
1777  bool useMicrosoftBOM; // the UTF-8 BOM were found when read. Note this, and try to write.
1778 };
1779 
1780 
1861 class TiXmlHandle
1862 {
1863 public:
1865  TiXmlHandle( TiXmlNode* _node ) {
1866  this->node = _node;
1867  }
1868 
1870  TiXmlHandle( const TiXmlHandle& ref ) {
1871  this->node = ref.node;
1872  }
1873 
1874  TiXmlHandle operator=( const TiXmlHandle& ref ) {
1875  if ( &ref != this )
1876  this->node = ref.node;
1877  return *this;
1878  }
1879 
1881  TiXmlHandle FirstChild() const;
1883  TiXmlHandle FirstChild( const char * value ) const;
1885  TiXmlHandle FirstChildElement() const;
1887  TiXmlHandle FirstChildElement( const char * value ) const;
1888 
1892  TiXmlHandle Child( const char* value, int index ) const;
1896  TiXmlHandle Child( int index ) const;
1901  TiXmlHandle ChildElement( const char* value, int index ) const;
1906  TiXmlHandle ChildElement( int index ) const;
1907 
1908  TiXmlHandle FirstChild( const std::string& _value ) const {
1909  return FirstChild( _value.c_str() );
1910  }
1911 
1912  TiXmlHandle FirstChildElement( const std::string& _value ) const {
1913  return FirstChildElement( _value.c_str() );
1914  }
1915 
1916  TiXmlHandle Child( const std::string& _value, int index ) const {
1917  return Child( _value.c_str(), index );
1918  }
1919 
1920  TiXmlHandle ChildElement( const std::string& _value, int index ) const {
1921  return ChildElement( _value.c_str(), index );
1922  }
1923 
1926  TiXmlNode* ToNode() const {
1927  return node;
1928  }
1929 
1932  TiXmlElement* ToElement() const {
1933  return ( ( node && node->ToElement() ) ? node->ToElement() : 0 );
1934  }
1935 
1938  TiXmlText* ToText() const {
1939  return ( ( node && node->ToText() ) ? node->ToText() : 0 );
1940  }
1941 
1944  TiXmlUnknown* ToUnknown() const {
1945  return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 );
1946  }
1947 
1951  TiXmlNode* Node() const {
1952  return ToNode();
1953  }
1954 
1958  TiXmlElement* Element() const {
1959  return ToElement();
1960  }
1961 
1965  TiXmlText* Text() const {
1966  return ToText();
1967  }
1968 
1972  TiXmlUnknown* Unknown() const {
1973  return ToUnknown();
1974  }
1975 
1976 private:
1977  TiXmlNode* node;
1978 };
1979 
1980 
2000 class TiXmlPrinter : public TiXmlVisitor
2001 {
2002 public:
2003  TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
2004  buffer(), indent( " " ), lineBreak( "\n" ) {
2005  }
2006 
2007  virtual bool VisitEnter( const TiXmlDocument& doc );
2008  virtual bool VisitExit( const TiXmlDocument& doc );
2009 
2010  virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
2011  virtual bool VisitExit( const TiXmlElement& element );
2012 
2013  virtual bool Visit( const TiXmlDeclaration& declaration );
2014  virtual bool Visit( const TiXmlText& text );
2015  virtual bool Visit( const TiXmlComment& comment );
2016  virtual bool Visit( const TiXmlUnknown& unknown );
2017 
2021  void SetIndent( const char* _indent ) {
2022  indent = _indent ? _indent : "";
2023  }
2024 
2026  const char* Indent() {
2027  return indent.c_str();
2028  }
2029 
2034  void SetLineBreak( const char* _lineBreak ) {
2035  lineBreak = _lineBreak ? _lineBreak : "";
2036  }
2037 
2039  const char* LineBreak() {
2040  return lineBreak.c_str();
2041  }
2042 
2046  void SetStreamPrinting() {
2047  indent = "";
2048  lineBreak = "";
2049  }
2050 
2052  const char* CStr() {
2053  return buffer.c_str();
2054  }
2055 
2057  size_t Size() {
2058  return buffer.size();
2059  }
2060 
2062  const std::string& Str() {
2063  return buffer;
2064  }
2065 
2066 private:
2067  void DoIndent() {
2068  for ( int i = 0; i<depth; ++i )
2069  buffer += indent;
2070  }
2071 
2072  void DoLineBreak() {
2073  buffer += lineBreak;
2074  }
2075 
2076  int depth;
2077  bool simpleTextPrint;
2078  TIXML_STRING buffer;
2079  TIXML_STRING indent;
2080  TIXML_STRING lineBreak;
2081 };
2082 
2083 
2084 #ifdef _MSC_VER
2085 #pragma warning( pop )
2086 #endif
2087 
2088 #endif
2089 
2090 #endif
std::ostream & operator<<(std::ostream &os, const Color &color)
Definition: Color.cpp:38
std::istream & operator>>(std::istream &in, Vector &v)
bool operator==(const EulerAngle &, const EulerAngle &)
T value(details::expression_node< T > *n)
Definition: Exprtk.hpp:15070
static std::string data()
Definition: Exprtk.hpp:44228
static char_cptr version
Definition: Exprtk.hpp:44221