tinyxml2.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. XMLDocument* document;
  103. XMLNode* parent;
  104. XMLNode* firstChild;
  105. XMLNode* lastChild;
  106. XMLNode* prev;
  107. XMLNode* next;
  108. private:
  109. void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
  110. };
  111. class XMLComment : public XMLNode
  112. {
  113. public:
  114. XMLComment( XMLDocument* doc );
  115. virtual ~XMLComment();
  116. virtual void Print( FILE* cfile, int depth );
  117. const char* Value() { return value.GetStr(); }
  118. char* ParseDeep( char* );
  119. protected:
  120. private:
  121. StrPair value;
  122. };
  123. class XMLAttribute : public XMLBase
  124. {
  125. friend class XMLElement;
  126. public:
  127. XMLAttribute( XMLElement* element ) : next( 0 ) {}
  128. virtual ~XMLAttribute() {}
  129. virtual void Print( FILE* cfile );
  130. private:
  131. char* ParseDeep( char* p );
  132. StrPair value;
  133. XMLAttribute* next;
  134. };
  135. class XMLElement : public XMLNode
  136. {
  137. public:
  138. XMLElement( XMLDocument* doc );
  139. virtual ~XMLElement();
  140. const char* Name() { return name.GetStr(); }
  141. virtual void Print( FILE* cfile, int depth );
  142. virtual XMLElement* ToElement() { return this; }
  143. bool Closing() const { return closing; }
  144. char* ParseDeep( char* p );
  145. protected:
  146. private:
  147. StrPair name;
  148. bool closing;
  149. XMLAttribute* rootAttribute;
  150. XMLAttribute* lastAttribute;
  151. };
  152. class XMLDocument : public XMLBase
  153. {
  154. public:
  155. XMLDocument();
  156. ~XMLDocument();
  157. bool Parse( const char* );
  158. void Print( FILE* cfile=stdout, int depth=0 );
  159. XMLNode* Root() { return root; }
  160. XMLNode* RootElement();
  161. enum {
  162. ERROR_ELEMENT_MISMATCH,
  163. ERROR_PARSING_ELEMENT,
  164. ERROR_PARSING_ATTRIBUTE
  165. };
  166. void SetError( int error, const char* str1, const char* str2 ) {}
  167. private:
  168. XMLDocument( const XMLDocument& ); // intentionally not implemented
  169. XMLNode* root;
  170. CharBuffer* charBuffer;
  171. };
  172. }; // tinyxml2
  173. #endif // TINYXML2_INCLUDED