tinyxml2.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_FLUSH = 0x01,
  45. NEEDS_ENTITY_PROCESSING = 0x02,
  46. NEEDS_NEWLINE_NORMALIZATION = 0x04
  47. };
  48. StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
  49. void Init( const char* start, char* end, int flags ) {
  50. this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
  51. }
  52. const char* GetStr();
  53. private:
  54. // After parsing, if *end != 0, it can be set to zero.
  55. int flags;
  56. const char* start;
  57. char* end;
  58. };
  59. class XMLBase
  60. {
  61. public:
  62. XMLBase() {}
  63. virtual ~XMLBase() {}
  64. protected:
  65. static const char* SkipWhiteSpace( const char* p ) { while( isspace( *p ) ) { ++p; } return p; }
  66. static char* SkipWhiteSpace( char* p ) { while( isspace( *p ) ) { ++p; } return p; }
  67. inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
  68. int n = 0;
  69. if ( p == q ) {
  70. return true;
  71. }
  72. while( *p && *q && *p == *q && n<nChar ) {
  73. ++p; ++q; ++n;
  74. }
  75. if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
  81. inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalnum( anyByte ) : 1; }
  82. inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte <= 127 ) ? isalpha( anyByte ) : 1; }
  83. const char* ParseText( char* in, const char* endTag, char** next );
  84. char* ParseName( char* in, StrPair* pair );
  85. char* Identify( XMLDocument* document, char* p, XMLNode** node );
  86. };
  87. class XMLNode : public XMLBase
  88. {
  89. friend class XMLDocument;
  90. friend class XMLElement;
  91. public:
  92. virtual ~XMLNode();
  93. XMLNode* InsertEndChild( XMLNode* addThis );
  94. virtual void Print( FILE* cfile, int depth );
  95. virtual XMLElement* ToElement() { return 0; }
  96. virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
  97. protected:
  98. XMLNode( XMLDocument* );
  99. XMLDocument* document;
  100. XMLNode* parent;
  101. XMLNode* firstChild;
  102. XMLNode* lastChild;
  103. XMLNode* prev;
  104. XMLNode* next;
  105. private:
  106. void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
  107. };
  108. class XMLComment : public XMLNode
  109. {
  110. public:
  111. XMLComment( XMLDocument* doc );
  112. virtual ~XMLComment();
  113. virtual void Print( FILE* cfile, int depth );
  114. const char* Value() { return value.GetStr(); }
  115. char* ParseDeep( char* );
  116. protected:
  117. private:
  118. StrPair value;
  119. };
  120. class XMLAttribute : public XMLBase
  121. {
  122. friend class XMLElement;
  123. public:
  124. XMLAttribute( XMLElement* element ) : next( 0 ) {}
  125. virtual ~XMLAttribute() {}
  126. virtual void Print( FILE* cfile );
  127. private:
  128. char* ParseDeep( char* p );
  129. StrPair value;
  130. XMLAttribute* next;
  131. };
  132. class XMLElement : public XMLNode
  133. {
  134. public:
  135. XMLElement( XMLDocument* doc );
  136. virtual ~XMLElement();
  137. const char* Name() { return name.GetStr(); }
  138. virtual void Print( FILE* cfile, int depth );
  139. virtual XMLElement* ToElement() { return this; }
  140. bool Closing() const { return closing; }
  141. char* ParseDeep( char* p );
  142. protected:
  143. private:
  144. StrPair name;
  145. bool closing;
  146. XMLAttribute* rootAttribute;
  147. XMLAttribute* lastAttribute;
  148. };
  149. class XMLDocument : public XMLBase
  150. {
  151. public:
  152. XMLDocument();
  153. ~XMLDocument();
  154. bool Parse( const char* );
  155. void Print( FILE* cfile=stdout, int depth=0 );
  156. XMLNode* Root() { return root; }
  157. XMLNode* RootElement();
  158. enum {
  159. ERROR_ELEMENT_MISMATCH,
  160. ERROR_PARSING_ELEMENT,
  161. ERROR_PARSING_ATTRIBUTE
  162. };
  163. void SetError( int error, const char* str1, const char* str2 ) {}
  164. private:
  165. XMLDocument( const XMLDocument& ); // intentionally not implemented
  166. XMLNode* root;
  167. CharBuffer* charBuffer;
  168. };
  169. }; // tinyxml2
  170. #endif // TINYXML2_INCLUDED