tinyxml2.h 4.3 KB

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