tinyxml2.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. char* XMLBase::ParseName( char* in, StrPair* pair );
  78. const char* XMLBase::ParseName( char* p, char** next )
  79. {
  80. char* start = p;
  81. char* nextTag = 0;
  82. *next = 0;
  83. start = p;
  84. if ( !start || !(*start) ) {
  85. return 0;
  86. }
  87. if ( !IsAlpha( *p ) ) {
  88. return 0;
  89. }
  90. while( *p && (
  91. IsAlphaNum( (unsigned char) *p )
  92. || *p == '_'
  93. || *p == '-'
  94. || *p == '.'
  95. || *p == ':' ))
  96. {
  97. ++p;
  98. }
  99. *p = 0;
  100. if ( p > start ) {
  101. *next = p+1;
  102. return start;
  103. }
  104. return 0;
  105. }
  106. char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
  107. {
  108. XMLNode* returnNode = 0;
  109. p = XMLNode::SkipWhiteSpace( p );
  110. if( !p || !*p || *p != '<' )
  111. {
  112. return 0;
  113. }
  114. // What is this thing?
  115. // - Elements start with a letter or underscore, but xml is reserved.
  116. // - Comments: <!--
  117. // - Decleration: <?xml
  118. // - Everthing else is unknown to tinyxml.
  119. //
  120. static const char* xmlHeader = { "<?xml" };
  121. static const char* commentHeader = { "<!--" };
  122. static const char* dtdHeader = { "<!" };
  123. static const char* cdataHeader = { "<![CDATA[" };
  124. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  125. static const int xmlHeaderLen = 5;
  126. static const int commentHeaderLen = 4;
  127. static const int dtdHeaderLen = 2;
  128. static const int cdataHeaderLen = 9;
  129. static const int elementHeaderLen = 1;
  130. if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
  131. returnNode = new XMLComment( document );
  132. p += commentHeaderLen;
  133. }
  134. else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
  135. returnNode = new XMLElement( document );
  136. p += elementHeaderLen;
  137. }
  138. else {
  139. TIXMLASSERT( 0 );
  140. }
  141. *node = returnNode;
  142. return p;
  143. }
  144. // --------- XMLNode ----------- //
  145. XMLNode::XMLNode( XMLDocument* doc ) :
  146. document( doc ),
  147. parent( 0 ),
  148. firstChild( 0 ), lastChild( 0 ),
  149. prev( 0 ), next( 0 )
  150. {
  151. }
  152. XMLNode::~XMLNode()
  153. {
  154. XMLNode* node=firstChild;
  155. while( node ) {
  156. XMLNode* temp = node->next;
  157. delete node;
  158. node = temp;
  159. }
  160. if ( prev ) {
  161. prev->next = next;
  162. }
  163. if ( next ) {
  164. next->prev = prev;
  165. }
  166. }
  167. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  168. {
  169. if ( lastChild ) {
  170. TIXMLASSERT( firstChild );
  171. TIXMLASSERT( lastChild->next == 0 );
  172. lastChild->next = addThis;
  173. addThis->prev = lastChild;
  174. lastChild = addThis;
  175. addThis->parent = this;
  176. addThis->next = 0;
  177. }
  178. else {
  179. TIXMLASSERT( firstChild == 0 );
  180. firstChild = lastChild = addThis;
  181. addThis->parent = this;
  182. addThis->prev = 0;
  183. addThis->next = 0;
  184. }
  185. return addThis;
  186. }
  187. void XMLNode::Print( FILE* fp, int depth )
  188. {
  189. for( XMLNode* node = firstChild; node; node=node->next ) {
  190. node->Print( fp, depth );
  191. }
  192. }
  193. void XMLNode::PrintSpace( FILE* fp, int depth )
  194. {
  195. for( int i=0; i<depth; ++i ) {
  196. fprintf( fp, " " );
  197. }
  198. }
  199. // --------- XMLComment ---------- //
  200. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ), value( 0 )
  201. {
  202. }
  203. XMLComment::~XMLComment()
  204. {
  205. }
  206. void XMLComment::Print( FILE* fp, int depth )
  207. {
  208. XMLNode::Print( fp, depth );
  209. fprintf( fp, "<!--%s-->\n", value );
  210. }
  211. char* XMLComment::ParseDeep( char* p )
  212. {
  213. // Comment parses as text.
  214. value = ParseText( p, "-->", &p );
  215. return p;
  216. }
  217. // --------- XMLAttribute ---------- //
  218. char* XMLAttribute::ParseDeep( char* p )
  219. {
  220. char endTag[2] = { *p, 0 };
  221. ++p;
  222. value = ParseText( p, endTag, &p );
  223. if ( !value ) return 0;
  224. return p;
  225. }
  226. void XMLAttribute::Print( FILE* cfile )
  227. {
  228. fprintf( cfile, "\"%s\"", value );
  229. }
  230. // --------- XMLElement ---------- //
  231. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  232. name( 0 ),
  233. closing( false ),
  234. rootAttribute( 0 ),
  235. lastAttribute( 0 )
  236. {
  237. }
  238. XMLElement::~XMLElement()
  239. {
  240. XMLAttribute* attribute = rootAttribute;
  241. while( attribute ) {
  242. XMLAttribute* next = attribute->next;
  243. delete attribute;
  244. attribute = next;
  245. }
  246. XMLNode* child = firstChild;
  247. while( child ) {
  248. XMLNode* next = child->next;
  249. delete child;
  250. child = next;
  251. }
  252. }
  253. char* XMLElement::ParseDeep( char* p )
  254. {
  255. // Read the element name.
  256. p = SkipWhiteSpace( p );
  257. if ( !p ) return 0;
  258. const char* start = p;
  259. // The closing element is the </element> form. It is
  260. // parsed just like a regular element then deleted from
  261. // the DOM.
  262. if ( *p == '/' ) {
  263. closing = true;
  264. ++p;
  265. }
  266. name = ParseName( p, &p );
  267. if ( !name ) return 0;
  268. // Read the attributes.
  269. while( p ) {
  270. p = SkipWhiteSpace( p );
  271. if ( !p || !(*p) ) {
  272. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name );
  273. return 0;
  274. }
  275. const char* saveP = p;
  276. // attribute.
  277. if ( *p == '\'' || *p == '\"' ) {
  278. XMLAttribute* attrib = new XMLAttribute( this );
  279. p = attrib->ParseDeep( p );
  280. if ( !p ) {
  281. delete attrib;
  282. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, saveP );
  283. return 0;
  284. }
  285. if ( rootAttribute ) {
  286. TIXMLASSERT( lastAttribute );
  287. lastAttribute->next = attrib;
  288. lastAttribute = attrib;
  289. }
  290. else {
  291. rootAttribute = lastAttribute = attrib;
  292. }
  293. }
  294. else if ( *p == '/' && *(p+1) == '>' ) {
  295. // end tag.
  296. if ( closing ) {
  297. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  298. return 0;
  299. }
  300. return p+2; // done; sealed element.
  301. }
  302. else if ( *p == '>' ) {
  303. ++p;
  304. break;
  305. }
  306. }
  307. while( p && *p ) {
  308. XMLNode* node = 0;
  309. p = Identify( document, p, &node );
  310. if ( p && node ) {
  311. node->ParseDeep( p );
  312. XMLElement* element = node->ToElement();
  313. if ( element && element->Closing() ) {
  314. if ( StringEqual( element->Name(), this->Name() ) ) {
  315. // All good, this is closing tag.
  316. delete node;
  317. p = 0;
  318. }
  319. else {
  320. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  321. delete node;
  322. }
  323. return p;
  324. }
  325. else {
  326. this->InsertEndChild( node );
  327. }
  328. }
  329. }
  330. return 0;
  331. }
  332. void XMLElement::Print( FILE* cfile, int depth )
  333. {
  334. PrintSpace( cfile, depth );
  335. fprintf( cfile, "<%s", Name() );
  336. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  337. fprintf( cfile, " " );
  338. attrib->Print( cfile );
  339. }
  340. if ( firstChild ) {
  341. fprintf( cfile, ">/n" );
  342. for( XMLNode* node=firstChild; node; node=node->next ) {
  343. node->Print( cfile, depth+1 );
  344. }
  345. fprintf( cfile, "</%s>", Name() );
  346. }
  347. else {
  348. fprintf( cfile, "/>\n" );
  349. }
  350. }
  351. // --------- XMLDocument ----------- //
  352. XMLDocument::XMLDocument() :
  353. charBuffer( 0 )
  354. {
  355. root = new XMLNode( this );
  356. }
  357. XMLDocument::~XMLDocument()
  358. {
  359. delete root;
  360. delete charBuffer;
  361. }
  362. bool XMLDocument::Parse( const char* p )
  363. {
  364. charBuffer = CharBuffer::Construct( p );
  365. XMLNode* node = 0;
  366. char* q = Identify( this, charBuffer->mem, &node );
  367. if ( node ) {
  368. root->InsertEndChild( node );
  369. node->ParseDeep( q );
  370. return true;
  371. }
  372. return false;
  373. }
  374. void XMLDocument::Print( FILE* fp, int depth )
  375. {
  376. for( XMLNode* node = root->firstChild; node; node=node->next ) {
  377. node->Print( fp, depth );
  378. }
  379. }