tinyxml2.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #ifndef TINYXML2_INCLUDED
  2. #define TINYXML2_INCLUDED
  3. #include <limits.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
  7. #ifndef DEBUG
  8. #define DEBUG
  9. #endif
  10. #endif
  11. #if defined(DEBUG)
  12. #if defined(_MSC_VER)
  13. #define TIXMLASSERT( x ) if ( !(x)) { _asm { int 3 } } //if ( !(x)) WinDebugBreak()
  14. #elif defined (ANDROID_NDK)
  15. #include <android/log.h>
  16. #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
  17. #else
  18. #include <assert.h>
  19. #define TIXMLASSERT assert
  20. #endif
  21. #else
  22. #define TIXMLASSERT( x ) {}
  23. #endif
  24. namespace tinyxml2
  25. {
  26. class XMLDocument;
  27. class XMLElement;
  28. class XMLAttribute;
  29. class XMLComment;
  30. class XMLNode;
  31. // internal - move to separate namespace
  32. struct CharBuffer
  33. {
  34. size_t length;
  35. char mem[1];
  36. static CharBuffer* Construct( const char* in );
  37. static void Free( CharBuffer* );
  38. };
  39. // FIXME: refactor to be the basis for all string handling.
  40. class StrPair
  41. {
  42. public:
  43. enum {
  44. NEEDS_ENTITY_PROCESSING = 0x01,
  45. NEEDS_NEWLINE_NORMALIZATION = 0x02
  46. };
  47. StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
  48. void Set( char* start, char* end, int flags ) {
  49. this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
  50. }
  51. const char* GetStr();
  52. bool Empty() const { return start == end; }
  53. private:
  54. enum {
  55. NEEDS_FLUSH = 0x100
  56. };
  57. // After parsing, if *end != 0, it can be set to zero.
  58. int flags;
  59. char* start;
  60. char* end;
  61. };
  62. class XMLBase
  63. {
  64. public:
  65. XMLBase() {}
  66. virtual ~XMLBase() {}
  67. protected:
  68. static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
  69. static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
  70. inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
  71. int n = 0;
  72. if ( p == q ) {
  73. return true;
  74. }
  75. while( *p && *q && *p == *q && n<nChar ) {
  76. ++p; ++q; ++n;
  77. }
  78. if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
  84. inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
  85. inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
  86. char* ParseText( char* in, StrPair* pair, const char* endTag );
  87. char* ParseName( char* in, StrPair* pair );
  88. char* Identify( XMLDocument* document, char* p, XMLNode** node );
  89. };
  90. class XMLNode : public XMLBase
  91. {
  92. friend class XMLDocument;
  93. friend class XMLElement;
  94. public:
  95. virtual ~XMLNode();
  96. XMLNode* InsertEndChild( XMLNode* addThis );
  97. virtual void Print( FILE* cfile, int depth );
  98. virtual XMLElement* ToElement() { return 0; }
  99. virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
  100. protected:
  101. XMLNode( XMLDocument* );
  102. void Unlink( XMLNode* child );
  103. XMLDocument* document;
  104. XMLNode* parent;
  105. XMLNode* firstChild;
  106. XMLNode* lastChild;
  107. XMLNode* prev;
  108. XMLNode* next;
  109. private:
  110. void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
  111. };
  112. class XMLComment : public XMLNode
  113. {
  114. public:
  115. XMLComment( XMLDocument* doc );
  116. virtual ~XMLComment();
  117. virtual void Print( FILE* cfile, int depth );
  118. const char* Value() { return value.GetStr(); }
  119. char* ParseDeep( char* );
  120. protected:
  121. private:
  122. StrPair value;
  123. };
  124. class XMLAttribute : public XMLBase
  125. {
  126. friend class XMLElement;
  127. public:
  128. XMLAttribute( XMLElement* element ) : next( 0 ) {}
  129. virtual ~XMLAttribute() {}
  130. virtual void Print( FILE* cfile );
  131. private:
  132. char* ParseDeep( char* p );
  133. StrPair name;
  134. StrPair value;
  135. XMLAttribute* next;
  136. };
  137. class XMLElement : public XMLNode
  138. {
  139. public:
  140. XMLElement( XMLDocument* doc );
  141. virtual ~XMLElement();
  142. const char* Name() { return name.GetStr(); }
  143. virtual void Print( FILE* cfile, int depth );
  144. virtual XMLElement* ToElement() { return this; }
  145. bool Closing() const { return closing; }
  146. char* ParseDeep( char* p );
  147. protected:
  148. private:
  149. StrPair name;
  150. bool closing;
  151. XMLAttribute* rootAttribute;
  152. XMLAttribute* lastAttribute;
  153. };
  154. class XMLDocument : public XMLBase
  155. {
  156. public:
  157. XMLDocument();
  158. ~XMLDocument();
  159. bool Parse( const char* );
  160. void Print( FILE* cfile=stdout, int depth=0 );
  161. XMLNode* Root() { return root; }
  162. XMLNode* RootElement();
  163. enum {
  164. ERROR_ELEMENT_MISMATCH,
  165. ERROR_PARSING_ELEMENT,
  166. ERROR_PARSING_ATTRIBUTE
  167. };
  168. void SetError( int error, const char* str1, const char* str2 ) {}
  169. private:
  170. XMLDocument( const XMLDocument& ); // intentionally not implemented
  171. XMLNode* root;
  172. CharBuffer* charBuffer;
  173. };
  174. }; // tinyxml2
  175. #endif // TINYXML2_INCLUDED