tinyxml2.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. private:
  104. char* ParseDeep( char* p );
  105. const char* value;
  106. XMLAttribute* next;
  107. };
  108. class XMLElement : public XMLNode
  109. {
  110. public:
  111. XMLElement( XMLDocument* doc );
  112. virtual ~XMLElement();
  113. const char* Name() const { return name; }
  114. virtual XMLElement* ToElement() { return this; }
  115. bool Closing() const { return closing; }
  116. char* ParseDeep( char* p );
  117. protected:
  118. private:
  119. const char* name;
  120. bool closing;
  121. XMLAttribute* rootAttribute;
  122. XMLAttribute* lastAttribute;
  123. };
  124. class XMLDocument : public XMLBase
  125. {
  126. public:
  127. XMLDocument();
  128. ~XMLDocument();
  129. bool Parse( const char* );
  130. void Print( FILE* cfile=stdout, int depth=0 );
  131. XMLNode* Root() { return root; }
  132. XMLNode* RootElement();
  133. enum {
  134. ERROR_ELEMENT_MISMATCH,
  135. ERROR_PARSING_ELEMENT,
  136. ERROR_PARSING_ATTRIBUTE
  137. };
  138. void SetError( int error, const char* str1, const char* str2 ) {}
  139. private:
  140. XMLDocument( const XMLDocument& ); // intentionally not implemented
  141. XMLNode* root;
  142. CharBuffer* charBuffer;
  143. };
  144. }; // tinyxml2
  145. #endif // TINYXML2_INCLUDED