tinyxml2.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  8. static const char LF = LINE_FEED;
  9. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  10. static const char CR = CARRIAGE_RETURN;
  11. // --------- CharBuffer ----------- //
  12. /*static*/ CharBuffer* CharBuffer::Construct( const char* in )
  13. {
  14. size_t len = strlen( in );
  15. size_t size = len + sizeof( CharBuffer );
  16. CharBuffer* cb = (CharBuffer*) malloc( size );
  17. cb->length = len;
  18. strcpy( cb->mem, in );
  19. return cb;
  20. }
  21. /*static*/ void CharBuffer::Free( CharBuffer* cb )
  22. {
  23. free( cb );
  24. }
  25. // --------- XMLNode ----------- //
  26. XMLNode::XMLNode( XMLDocument* doc ) :
  27. document( doc ),
  28. parent( 0 ),
  29. firstChild( 0 ), lastChild( 0 ),
  30. prev( 0 ), next( 0 )
  31. {
  32. }
  33. XMLNode::~XMLNode()
  34. {
  35. XMLNode* node=firstChild;
  36. while( node ) {
  37. XMLNode* temp = node->next;
  38. delete node;
  39. node = temp;
  40. }
  41. }
  42. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  43. {
  44. if ( lastChild ) {
  45. TIXMLASSERT( firstChild );
  46. TIXMLASSERT( lastChild->next == 0 );
  47. lastChild->next = addThis;
  48. addThis->prev = lastChild;
  49. lastChild = addThis;
  50. addThis->parent = this;
  51. addThis->next = 0;
  52. }
  53. else {
  54. TIXMLASSERT( firstChild == 0 );
  55. firstChild = lastChild = addThis;
  56. addThis->parent = this;
  57. addThis->prev = 0;
  58. addThis->next = 0;
  59. }
  60. return addThis;
  61. }
  62. void XMLNode::Print( FILE* fp, int depth )
  63. {
  64. for( XMLNode* node = firstChild; node; node=node->next ) {
  65. node->Print( fp, depth );
  66. }
  67. }
  68. void XMLNode::PrintSpace( FILE* fp, int depth )
  69. {
  70. for( int i=0; i<depth; ++i ) {
  71. fprintf( fp, " " );
  72. }
  73. }
  74. const char* XMLNode::ParseText( char* p, const char* endTag, char** next )
  75. {
  76. TIXMLASSERT( endTag && *endTag );
  77. char* start = p;
  78. char* q = p; // q (target) <= p (src) in same buffer.
  79. char endChar = *endTag;
  80. int length = strlen( endTag );
  81. char* nextTag = 0;
  82. // Inner loop of text parsing.
  83. while ( *p ) {
  84. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  85. *q = 0;
  86. nextTag = p + length;
  87. break;
  88. }
  89. else if ( *p == CR ) {
  90. // CR-LF pair becomes LF
  91. // CR alone becomes LF
  92. // LF-CR becomes LF
  93. if ( *(p+1) == LF ) {
  94. p += 2;
  95. }
  96. else {
  97. ++p;
  98. }
  99. *q = LF;
  100. }
  101. else if ( *p == LF ) {
  102. if ( *(p+1) == CR ) {
  103. p += 2;
  104. }
  105. else {
  106. ++p;
  107. }
  108. *q = LF;
  109. }
  110. else {
  111. *q = *p;
  112. ++p;
  113. }
  114. ++q;
  115. }
  116. // Error? If we don't have a text tag, something went wrong. (Although
  117. // what the nextTag points at may be null.)
  118. if ( nextTag == 0 ) {
  119. return 0;
  120. }
  121. *next = nextTag;
  122. return start;
  123. }
  124. // --------- XMLComment ---------- //
  125. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  126. {
  127. }
  128. XMLComment::~XMLComment()
  129. {
  130. }
  131. void XMLComment::Print( FILE* fp, int depth )
  132. {
  133. XMLNode::Print( fp, depth );
  134. fprintf( fp, "<!--%s-->\n", value );
  135. }
  136. char* XMLComment::ParseDeep( char* p )
  137. {
  138. // Comment parses as text.
  139. value = ParseText( p, "-->", &p );
  140. return p;
  141. }
  142. // --------- XMLDocument ----------- //
  143. XMLDocument::XMLDocument() :
  144. charBuffer( 0 )
  145. {
  146. root = new XMLNode( this );
  147. }
  148. XMLDocument::~XMLDocument()
  149. {
  150. delete root;
  151. delete charBuffer;
  152. }
  153. bool XMLDocument::Parse( const char* p )
  154. {
  155. charBuffer = CharBuffer::Construct( p );
  156. XMLNode* node = 0;
  157. char* q = Identify( charBuffer->mem, &node );
  158. root->InsertEndChild( node );
  159. node->ParseDeep( q );
  160. return true;
  161. }
  162. void XMLDocument::Print( FILE* fp, int depth )
  163. {
  164. for( XMLNode* node = root->firstChild; node; node=node->next ) {
  165. node->Print( fp, depth );
  166. }
  167. }
  168. char* XMLDocument::Identify( char* p, XMLNode** node )
  169. {
  170. XMLNode* returnNode = 0;
  171. p = XMLNode::SkipWhiteSpace( p );
  172. if( !p || !*p || *p != '<' )
  173. {
  174. return 0;
  175. }
  176. // What is this thing?
  177. // - Elements start with a letter or underscore, but xml is reserved.
  178. // - Comments: <!--
  179. // - Decleration: <?xml
  180. // - Everthing else is unknown to tinyxml.
  181. //
  182. static const char* xmlHeader = { "<?xml" };
  183. static const char* commentHeader = { "<!--" };
  184. static const char* dtdHeader = { "<!" };
  185. static const char* cdataHeader = { "<![CDATA[" };
  186. static const int xmlHeaderLen = 5;
  187. static const int commentHeaderLen = 4;
  188. static const int dtdHeaderLen = 2;
  189. static const int cdataHeaderLen = 9;
  190. if ( XMLNode::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  191. returnNode = new XMLComment( this );
  192. p += commentHeaderLen;
  193. }
  194. else {
  195. TIXMLASSERT( 0 );
  196. }
  197. *node = returnNode;
  198. return p;
  199. }