tinyxml2.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "tinyxml2.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. using namespace tinyxml2;
  7. // --------- CharBuffer ----------- //
  8. /*static*/ CharBuffer* CharBuffer::Construct( const char* in )
  9. {
  10. size_t len = strlen( in );
  11. size_t size = len + sizeof( CharBuffer );
  12. CharBuffer* cb = (CharBuffer*) malloc( size );
  13. cb->length = len;
  14. strcpy( cb->mem, in );
  15. return cb;
  16. }
  17. /*static*/ void CharBuffer::Free( CharBuffer* cb )
  18. {
  19. free( cb );
  20. }
  21. // --------- XMLNode ----------- //
  22. XMLNode::XMLNode( XMLDocument* doc ) :
  23. document( doc ),
  24. parent( 0 ),
  25. firstChild( 0 ), lastChild( 0 ),
  26. prev( 0 ), next( 0 )
  27. {
  28. }
  29. XMLNode::~XMLNode()
  30. {
  31. XMLNode* node=firstChild;
  32. while( node ) {
  33. XMLNode* temp = node->next;
  34. delete node;
  35. node = temp;
  36. }
  37. }
  38. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  39. {
  40. if ( lastChild ) {
  41. TIXMLASSERT( firstChild );
  42. TIXMLASSERT( lastChild->next == 0 );
  43. lastChild->next = addThis;
  44. addThis->prev = lastChild;
  45. lastChild = addThis;
  46. addThis->parent = this;
  47. addThis->next = null;
  48. }
  49. else {
  50. TIXMLASSERT( firstChild == 0 );
  51. firstChild = lastChild = addThis;
  52. addThis->parent = this;
  53. addThis->prev = 0;
  54. addThis->next = null;
  55. }
  56. }
  57. const char* XMLNode::ParseText( char* p, const char* endTag, char** next )
  58. {
  59. TIXMLASSERT( endTag && *endTag );
  60. char* start = SkipWhiteSpace( p );
  61. if ( !start )
  62. return;
  63. char endChar = *endTag;
  64. p = start;
  65. int length = strlen( endTag );
  66. while ( *p ) {
  67. if ( *p == endChar ) {
  68. if ( strncmp( p, endTag, length ) == 0 ) {
  69. *p = 0;
  70. *next = p + length;
  71. return start;
  72. }
  73. }
  74. ++p;
  75. }
  76. return 0;
  77. }
  78. // --------- XMLComment ---------- //
  79. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  80. {
  81. }
  82. virtual XMLComment::~XMLComment()
  83. {
  84. }
  85. virtual char* XMLComment::ParseDeep( char* p )
  86. {
  87. // Comment parses as text.
  88. value = ParseText( p, "-->", &p );
  89. return p;
  90. }
  91. // --------- XMLDocument ----------- //
  92. XMLDocument::XMLDocument() :
  93. charBuffer( 0 )
  94. {
  95. }
  96. XMLDocument::~XMLDocument()
  97. {
  98. delete root;
  99. delete charBuffer;
  100. }
  101. bool XMLDocument::Parse( const char* p )
  102. {
  103. charBuffer = CharBuffer.Construct( p );
  104. XMLNode* node = 0;
  105. Identify( charBuffer., node );
  106. node->Parse( p );
  107. }
  108. XMLComment* XMLDocument::newComment( XMLNode* parent )
  109. {
  110. }
  111. char* XMLDocument::Identify( char* p, XMLNode** node )
  112. {
  113. XMLNode* returnNode = 0;
  114. p = XMLNode::SkipWhiteSpace( p );
  115. if( !p || !*p || *p != '<' )
  116. {
  117. return 0;
  118. }
  119. // What is this thing?
  120. // - Elements start with a letter or underscore, but xml is reserved.
  121. // - Comments: <!--
  122. // - Decleration: <?xml
  123. // - Everthing else is unknown to tinyxml.
  124. //
  125. const char* xmlHeader = { "<?xml" };
  126. const char* commentHeader = { "<!--" };
  127. const char* dtdHeader = { "<!" };
  128. const char* cdataHeader = { "<![CDATA[" };
  129. if ( XMLNode::StringEqual( p, xmlHeader, 5 ) ) {
  130. returnNode = new XMLComment();
  131. }
  132. else {
  133. TIXMLASSERT( 0 );
  134. }
  135. *node = returnNode;
  136. return p;
  137. }