1
0

tinyxml2.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. class XMLText;
  32. // internal - move to separate namespace
  33. struct CharBuffer
  34. {
  35. size_t length;
  36. char mem[1];
  37. static CharBuffer* Construct( const char* in );
  38. static void Free( CharBuffer* );
  39. };
  40. // FIXME: refactor to be the basis for all string handling.
  41. class StrPair
  42. {
  43. public:
  44. enum {
  45. NEEDS_ENTITY_PROCESSING = 0x01,
  46. NEEDS_NEWLINE_NORMALIZATION = 0x02
  47. };
  48. StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
  49. void Set( char* start, char* end, int flags ) {
  50. this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
  51. }
  52. const char* GetStr();
  53. bool Empty() const { return start == end; }
  54. private:
  55. enum {
  56. NEEDS_FLUSH = 0x100
  57. };
  58. // After parsing, if *end != 0, it can be set to zero.
  59. int flags;
  60. char* start;
  61. char* end;
  62. };
  63. class XMLBase
  64. {
  65. public:
  66. XMLBase() {}
  67. virtual ~XMLBase() {}
  68. protected:
  69. static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
  70. static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
  71. inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
  72. int n = 0;
  73. if ( p == q ) {
  74. return true;
  75. }
  76. while( *p && *q && *p == *q && n<nChar ) {
  77. ++p; ++q; ++n;
  78. }
  79. if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
  85. inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
  86. inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
  87. char* ParseText( char* in, StrPair* pair, const char* endTag );
  88. char* ParseName( char* in, StrPair* pair );
  89. char* Identify( XMLDocument* document, char* p, XMLNode** node );
  90. };
  91. class XMLNode : public XMLBase
  92. {
  93. friend class XMLDocument;
  94. friend class XMLElement;
  95. public:
  96. virtual ~XMLNode();
  97. XMLNode* InsertEndChild( XMLNode* addThis );
  98. virtual void Print( FILE* cfile, int depth );
  99. virtual XMLElement* ToElement() { return 0; }
  100. virtual XMLText* ToText() { return 0; }
  101. virtual XMLComment* ToComment() { return 0; }
  102. // fixme: guarentee null terminator to avoid internal checks
  103. virtual char* ParseDeep( char* );
  104. void SetTextParent() { isTextParent = true; }
  105. bool IsTextParent() const { return isTextParent; }
  106. virtual bool IsClosingElement() const { return false; }
  107. protected:
  108. XMLNode( XMLDocument* );
  109. void Unlink( XMLNode* child );
  110. XMLDocument* document;
  111. XMLNode* parent;
  112. bool isTextParent;
  113. XMLNode* firstChild;
  114. XMLNode* lastChild;
  115. XMLNode* prev;
  116. XMLNode* next;
  117. private:
  118. void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
  119. };
  120. class XMLText : public XMLNode
  121. {
  122. public:
  123. XMLText( XMLDocument* doc ) : XMLNode( doc ) {}
  124. virtual ~XMLText() {}
  125. virtual void Print( FILE* cfile, int depth );
  126. const char* Value() { return value.GetStr(); }
  127. virtual XMLText* ToText() { return this; }
  128. char* ParseDeep( char* );
  129. protected:
  130. private:
  131. StrPair value;
  132. };
  133. class XMLComment : public XMLNode
  134. {
  135. public:
  136. XMLComment( XMLDocument* doc );
  137. virtual ~XMLComment();
  138. virtual void Print( FILE* cfile, int depth );
  139. virtual XMLComment* ToComment() { return this; }
  140. const char* Value() { return value.GetStr(); }
  141. char* ParseDeep( char* );
  142. protected:
  143. private:
  144. StrPair value;
  145. };
  146. class XMLAttribute : public XMLBase
  147. {
  148. friend class XMLElement;
  149. public:
  150. XMLAttribute( XMLElement* element ) : next( 0 ) {}
  151. virtual ~XMLAttribute() {}
  152. virtual void Print( FILE* cfile );
  153. private:
  154. char* ParseDeep( char* p );
  155. StrPair name;
  156. StrPair value;
  157. XMLAttribute* next;
  158. };
  159. class XMLElement : public XMLNode
  160. {
  161. public:
  162. XMLElement( XMLDocument* doc );
  163. virtual ~XMLElement();
  164. const char* Name() { return name.GetStr(); }
  165. virtual void Print( FILE* cfile, int depth );
  166. virtual XMLElement* ToElement() { return this; }
  167. virtual bool IsClosingElement() const { return closing; }
  168. char* ParseDeep( char* p );
  169. protected:
  170. private:
  171. char* ParseAttributes( char* p, bool *closedElement );
  172. StrPair name;
  173. bool closing;
  174. XMLAttribute* rootAttribute;
  175. XMLAttribute* lastAttribute;
  176. };
  177. class XMLDocument : public XMLNode
  178. {
  179. public:
  180. XMLDocument();
  181. ~XMLDocument();
  182. bool Parse( const char* );
  183. void Print( FILE* cfile=stdout, int depth=0 );
  184. /*
  185. XMLNode* Root() { return root; }
  186. XMLNode* RootElement();
  187. */
  188. enum {
  189. ERROR_ELEMENT_MISMATCH,
  190. ERROR_PARSING_ELEMENT,
  191. ERROR_PARSING_ATTRIBUTE
  192. };
  193. void SetError( int error, const char* str1, const char* str2 );
  194. private:
  195. XMLDocument( const XMLDocument& ); // intentionally not implemented
  196. CharBuffer* charBuffer;
  197. };
  198. }; // tinyxml2
  199. #endif // TINYXML2_INCLUDED