1
0

tinyxml2.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. // --------- XMLBase ----------- //
  26. const char* XMLBase::ParseText( char* p, const char* endTag, char** next )
  27. {
  28. TIXMLASSERT( endTag && *endTag );
  29. char* start = p;
  30. char* q = p; // q (target) <= p (src) in same buffer.
  31. char endChar = *endTag;
  32. int length = strlen( endTag );
  33. char* nextTag = 0;
  34. *next = 0;
  35. // Inner loop of text parsing.
  36. while ( *p ) {
  37. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  38. *q = 0;
  39. nextTag = p + length;
  40. break;
  41. }
  42. else if ( *p == CR ) {
  43. // CR-LF pair becomes LF
  44. // CR alone becomes LF
  45. // LF-CR becomes LF
  46. if ( *(p+1) == LF ) {
  47. p += 2;
  48. }
  49. else {
  50. ++p;
  51. }
  52. *q = LF;
  53. }
  54. else if ( *p == LF ) {
  55. if ( *(p+1) == CR ) {
  56. p += 2;
  57. }
  58. else {
  59. ++p;
  60. }
  61. *q = LF;
  62. }
  63. else {
  64. *q = *p;
  65. ++p;
  66. }
  67. ++q;
  68. }
  69. // Error? If we don't have a text tag, something went wrong. (Although
  70. // what the nextTag points at may be null.)
  71. if ( nextTag == 0 ) {
  72. return 0;
  73. }
  74. *next = nextTag;
  75. return start;
  76. }
  77. const char* XMLBase::ParseName( char* p, char** next )
  78. {
  79. char* start = p;
  80. char* nextTag = 0;
  81. *next = 0;
  82. start = p;
  83. if ( !start || !(*start) ) {
  84. return 0;
  85. }
  86. if ( !IsAlpha( *p ) ) {
  87. return 0;
  88. }
  89. while( *p && (
  90. IsAlphaNum( (unsigned char) *p )
  91. || *p == '_'
  92. || *p == '-'
  93. || *p == '.'
  94. || *p == ':' ))
  95. {
  96. ++p;
  97. }
  98. *p = 0;
  99. if ( p > start ) {
  100. *next = p;
  101. return start;
  102. }
  103. return p+1;
  104. }
  105. char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
  106. {
  107. XMLNode* returnNode = 0;
  108. p = XMLNode::SkipWhiteSpace( p );
  109. if( !p || !*p || *p != '<' )
  110. {
  111. return 0;
  112. }
  113. // What is this thing?
  114. // - Elements start with a letter or underscore, but xml is reserved.
  115. // - Comments: <!--
  116. // - Decleration: <?xml
  117. // - Everthing else is unknown to tinyxml.
  118. //
  119. static const char* xmlHeader = { "<?xml" };
  120. static const char* commentHeader = { "<!--" };
  121. static const char* dtdHeader = { "<!" };
  122. static const char* cdataHeader = { "<![CDATA[" };
  123. static const int xmlHeaderLen = 5;
  124. static const int commentHeaderLen = 4;
  125. static const int dtdHeaderLen = 2;
  126. static const int cdataHeaderLen = 9;
  127. if ( XMLNode::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  128. returnNode = new XMLComment( document );
  129. p += commentHeaderLen;
  130. }
  131. else {
  132. TIXMLASSERT( 0 );
  133. }
  134. *node = returnNode;
  135. return p;
  136. }
  137. // --------- XMLNode ----------- //
  138. XMLNode::XMLNode( XMLDocument* doc ) :
  139. document( doc ),
  140. parent( 0 ),
  141. firstChild( 0 ), lastChild( 0 ),
  142. prev( 0 ), next( 0 )
  143. {
  144. }
  145. XMLNode::~XMLNode()
  146. {
  147. XMLNode* node=firstChild;
  148. while( node ) {
  149. XMLNode* temp = node->next;
  150. delete node;
  151. node = temp;
  152. }
  153. if ( prev ) {
  154. prev->next = next;
  155. }
  156. if ( next ) {
  157. next->prev = prev;
  158. }
  159. }
  160. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  161. {
  162. if ( lastChild ) {
  163. TIXMLASSERT( firstChild );
  164. TIXMLASSERT( lastChild->next == 0 );
  165. lastChild->next = addThis;
  166. addThis->prev = lastChild;
  167. lastChild = addThis;
  168. addThis->parent = this;
  169. addThis->next = 0;
  170. }
  171. else {
  172. TIXMLASSERT( firstChild == 0 );
  173. firstChild = lastChild = addThis;
  174. addThis->parent = this;
  175. addThis->prev = 0;
  176. addThis->next = 0;
  177. }
  178. return addThis;
  179. }
  180. void XMLNode::Print( FILE* fp, int depth )
  181. {
  182. for( XMLNode* node = firstChild; node; node=node->next ) {
  183. node->Print( fp, depth );
  184. }
  185. }
  186. void XMLNode::PrintSpace( FILE* fp, int depth )
  187. {
  188. for( int i=0; i<depth; ++i ) {
  189. fprintf( fp, " " );
  190. }
  191. }
  192. // --------- XMLComment ---------- //
  193. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ), value( 0 )
  194. {
  195. }
  196. XMLComment::~XMLComment()
  197. {
  198. }
  199. void XMLComment::Print( FILE* fp, int depth )
  200. {
  201. XMLNode::Print( fp, depth );
  202. fprintf( fp, "<!--%s-->\n", value );
  203. }
  204. char* XMLComment::ParseDeep( char* p )
  205. {
  206. // Comment parses as text.
  207. value = ParseText( p, "-->", &p );
  208. return p;
  209. }
  210. // --------- XMLAttribute ---------- //
  211. char* XMLAttribute::ParseDeep( char* p )
  212. {
  213. char endTag[2] = { *p, 0 };
  214. ++p;
  215. value = ParseText( p, endTag, &p );
  216. if ( !value ) return 0;
  217. return p;
  218. }
  219. // --------- XMLElement ---------- //
  220. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  221. name( 0 ),
  222. closing( false ),
  223. rootAttribute( 0 ),
  224. lastAttribute( 0 )
  225. {
  226. }
  227. XMLElement::~XMLElement()
  228. {
  229. XMLAttribute* attribute = rootAttribute;
  230. while( attribute ) {
  231. XMLAttribute* next = attribute->next;
  232. delete attribute;
  233. attribute = next;
  234. }
  235. XMLNode* child = firstChild;
  236. while( child ) {
  237. XMLNode* next = child->next;
  238. delete child;
  239. child = next;
  240. }
  241. }
  242. char* XMLElement::ParseDeep( char* p )
  243. {
  244. // Read the element name.
  245. p = SkipWhiteSpace( p );
  246. if ( !p ) return 0;
  247. const char* start = p;
  248. // The closing element is the </element> form. It is
  249. // parsed just like a regular element then deleted from
  250. // the DOM.
  251. if ( *p == '/' ) {
  252. closing = true;
  253. ++p;
  254. }
  255. name = ParseName( p, &p );
  256. if ( !name ) return 0;
  257. // Read the attributes.
  258. while( p ) {
  259. p = SkipWhiteSpace( p );
  260. if ( !p || !(*p) ) {
  261. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name );
  262. return 0;
  263. }
  264. const char* saveP = p;
  265. // attribute.
  266. if ( *p == '\'' || *p == '\"' ) {
  267. XMLAttribute* attrib = new XMLAttribute( this );
  268. p = attrib->ParseDeep( p );
  269. if ( !p ) {
  270. delete attrib;
  271. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, saveP );
  272. return 0;
  273. }
  274. if ( rootAttribute ) {
  275. TIXMLASSERT( lastAttribute );
  276. lastAttribute->next = attrib;
  277. lastAttribute = attrib;
  278. }
  279. else {
  280. rootAttribute = lastAttribute = attrib;
  281. }
  282. }
  283. else if ( *p == '/' && *(p+1) == '>' ) {
  284. // end tag.
  285. if ( closing ) {
  286. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  287. return 0;
  288. }
  289. return p+2; // done; sealed element.
  290. }
  291. else if ( *p == '>' ) {
  292. ++p;
  293. break;
  294. }
  295. }
  296. while( p && *p ) {
  297. XMLNode* node = 0;
  298. p = Identify( document, p, &node );
  299. if ( p && node ) {
  300. node->ParseDeep( p );
  301. XMLElement* element = node->ToElement();
  302. if ( element && element->Closing() ) {
  303. if ( StringEqual( element->Name(), this->Name() ) ) {
  304. // All good, this is closing tag.
  305. delete node;
  306. p = 0;
  307. }
  308. else {
  309. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  310. delete node;
  311. }
  312. return p;
  313. }
  314. else {
  315. this->InsertEndChild( node );
  316. }
  317. }
  318. }
  319. return 0;
  320. }
  321. // --------- XMLDocument ----------- //
  322. XMLDocument::XMLDocument() :
  323. charBuffer( 0 )
  324. {
  325. root = new XMLNode( this );
  326. }
  327. XMLDocument::~XMLDocument()
  328. {
  329. delete root;
  330. delete charBuffer;
  331. }
  332. bool XMLDocument::Parse( const char* p )
  333. {
  334. charBuffer = CharBuffer::Construct( p );
  335. XMLNode* node = 0;
  336. char* q = Identify( this, charBuffer->mem, &node );
  337. root->InsertEndChild( node );
  338. node->ParseDeep( q );
  339. return true;
  340. }
  341. void XMLDocument::Print( FILE* fp, int depth )
  342. {
  343. for( XMLNode* node = root->firstChild; node; node=node->next ) {
  344. node->Print( fp, depth );
  345. }
  346. }