tinyxml2.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // internal - move to separate namespace
  28. struct CharBuffer
  29. {
  30. size_t length;
  31. char mem[1];
  32. static CharBuffer* Construct( const char* in );
  33. static void Free( CharBuffer* );
  34. };
  35. class XMLNode
  36. {
  37. friend class XMLDocument;
  38. public:
  39. XMLNode* InsertEndChild( XMLNode* addThis );
  40. virtual void Print( FILE* cfile, int depth );
  41. protected:
  42. XMLNode( XMLDocument* );
  43. virtual ~XMLNode();
  44. // Utility
  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. /* Parses text. (Not a text node.)
  58. - [ ] EOL normalization.
  59. - [x] Trim leading whitespace
  60. - [ ] Trim trailing whitespace.
  61. - [ ] Leaves inner whitespace
  62. - [ ] Inserts one space between lines.
  63. */
  64. const char* ParseText( char* in, const char* endTag, char** next );
  65. virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
  66. XMLDocument* document;
  67. XMLNode* parent;
  68. XMLNode* firstChild;
  69. XMLNode* lastChild;
  70. XMLNode* prev;
  71. XMLNode* next;
  72. private:
  73. void PrintSpace( FILE* cfile, int depth ); // prints leading spaces.
  74. };
  75. class XMLComment : public XMLNode
  76. {
  77. public:
  78. XMLComment( XMLDocument* doc );
  79. virtual ~XMLComment();
  80. virtual void Print( FILE* cfile, int depth );
  81. protected:
  82. char* ParseDeep( char* );
  83. private:
  84. const char* value;
  85. };
  86. class XMLDocument
  87. {
  88. public:
  89. XMLDocument();
  90. ~XMLDocument();
  91. bool Parse( const char* );
  92. void Print( FILE* cfile=stdout, int depth=0 );
  93. XMLNode* Root() { return root; }
  94. XMLNode* RootElement();
  95. private:
  96. XMLDocument( const XMLDocument& ); // intentionally not implemented
  97. char* Identify( char* p, XMLNode** node );
  98. XMLNode* root;
  99. CharBuffer* charBuffer;
  100. };
  101. }; // tinyxml2
  102. #endif // TINYXML2_INCLUDED