tinyxml2.h 4.8 KB

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