tinyxml2.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. static const char SINGLE_QUOTE = '\'';
  12. static const char DOUBLE_QUOTE = '\"';
  13. // --------- CharBuffer ----------- //
  14. /*static*/ CharBuffer* CharBuffer::Construct( const char* in )
  15. {
  16. size_t len = strlen( in );
  17. size_t size = len + sizeof( CharBuffer );
  18. CharBuffer* cb = (CharBuffer*) malloc( size );
  19. cb->length = len;
  20. strcpy( cb->mem, in );
  21. return cb;
  22. }
  23. /*static*/ void CharBuffer::Free( CharBuffer* cb )
  24. {
  25. free( cb );
  26. }
  27. const char* StrPair::GetStr()
  28. {
  29. if ( flags & NEEDS_FLUSH ) {
  30. *end = 0;
  31. if ( flags & ( NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION ) ) {
  32. char* p = start;
  33. char* q = start;
  34. while( p < end ) {
  35. if ( *p == CR ) {
  36. // CR-LF pair becomes LF
  37. // CR alone becomes LF
  38. // LF-CR becomes LF
  39. if ( *(p+1) == LF ) {
  40. p += 2;
  41. }
  42. else {
  43. ++p;
  44. }
  45. *q = LF;
  46. }
  47. else if ( *p == LF ) {
  48. if ( *(p+1) == CR ) {
  49. p += 2;
  50. }
  51. else {
  52. ++p;
  53. }
  54. *q = LF;
  55. }
  56. else {
  57. *q = *p;
  58. ++p;
  59. ++q;
  60. }
  61. }
  62. }
  63. flags = 0;
  64. }
  65. return start;
  66. }
  67. // --------- XMLBase ----------- //
  68. char* XMLBase::ParseText( char* p, StrPair* pair, const char* endTag )
  69. {
  70. TIXMLASSERT( endTag && *endTag );
  71. char* start = p;
  72. char endChar = *endTag;
  73. int length = strlen( endTag );
  74. // Inner loop of text parsing.
  75. while ( *p ) {
  76. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  77. pair->Set( start, p, StrPair::NEEDS_ENTITY_PROCESSING | StrPair::NEEDS_NEWLINE_NORMALIZATION );
  78. return p + length;
  79. }
  80. ++p;
  81. }
  82. return p;
  83. }
  84. char* XMLBase::ParseName( char* p, StrPair* pair )
  85. {
  86. char* start = p;
  87. start = p;
  88. if ( !start || !(*start) ) {
  89. return 0;
  90. }
  91. if ( !IsAlpha( *p ) ) {
  92. return 0;
  93. }
  94. while( *p && (
  95. IsAlphaNum( (unsigned char) *p )
  96. || *p == '_'
  97. || *p == '-'
  98. || *p == '.'
  99. || *p == ':' ))
  100. {
  101. ++p;
  102. }
  103. if ( p > start ) {
  104. pair->Set( start, p, 0 );
  105. return p;
  106. }
  107. return 0;
  108. }
  109. char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
  110. {
  111. XMLNode* returnNode = 0;
  112. char* start = p;
  113. p = XMLNode::SkipWhiteSpace( p );
  114. if( !p || !*p )
  115. {
  116. return 0;
  117. }
  118. // What is this thing?
  119. // - Elements start with a letter or underscore, but xml is reserved.
  120. // - Comments: <!--
  121. // - Decleration: <?xml
  122. // - Everthing else is unknown to tinyxml.
  123. //
  124. static const char* xmlHeader = { "<?xml" };
  125. static const char* commentHeader = { "<!--" };
  126. static const char* dtdHeader = { "<!" };
  127. static const char* cdataHeader = { "<![CDATA[" };
  128. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  129. static const int xmlHeaderLen = 5;
  130. static const int commentHeaderLen = 4;
  131. static const int dtdHeaderLen = 2;
  132. static const int cdataHeaderLen = 9;
  133. static const int elementHeaderLen = 1;
  134. if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
  135. returnNode = new XMLComment( document );
  136. p += commentHeaderLen;
  137. }
  138. else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
  139. returnNode = new XMLElement( document );
  140. p += elementHeaderLen;
  141. }
  142. // fixme: better text detection
  143. else if ( (*p != '<') && IsAlphaNum( *p ) ) {
  144. // fixme: this is filtering out empty text...should it?
  145. returnNode = new XMLText( document );
  146. p = start; // Back it up, all the text counts.
  147. }
  148. else {
  149. TIXMLASSERT( 0 );
  150. }
  151. *node = returnNode;
  152. return p;
  153. }
  154. // --------- XMLNode ----------- //
  155. XMLNode::XMLNode( XMLDocument* doc ) :
  156. document( doc ),
  157. parent( 0 ),
  158. firstChild( 0 ), lastChild( 0 ),
  159. prev( 0 ), next( 0 )
  160. {
  161. }
  162. XMLNode::~XMLNode()
  163. {
  164. //printf( "~XMLNode %x\n", this );
  165. while( firstChild ) {
  166. XMLNode* node = firstChild;
  167. Unlink( node );
  168. delete node;
  169. }
  170. }
  171. void XMLNode::Unlink( XMLNode* child )
  172. {
  173. TIXMLASSERT( child->parent == this );
  174. if ( child == firstChild )
  175. firstChild = firstChild->next;
  176. if ( child == lastChild )
  177. lastChild = lastChild->prev;
  178. if ( child->prev ) {
  179. child->prev->next = child->next;
  180. }
  181. if ( child->next ) {
  182. child->next->prev = child->prev;
  183. }
  184. child->parent = 0;
  185. }
  186. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  187. {
  188. if ( lastChild ) {
  189. TIXMLASSERT( firstChild );
  190. TIXMLASSERT( lastChild->next == 0 );
  191. lastChild->next = addThis;
  192. addThis->prev = lastChild;
  193. lastChild = addThis;
  194. addThis->parent = this;
  195. addThis->next = 0;
  196. }
  197. else {
  198. TIXMLASSERT( firstChild == 0 );
  199. firstChild = lastChild = addThis;
  200. addThis->parent = this;
  201. addThis->prev = 0;
  202. addThis->next = 0;
  203. }
  204. return addThis;
  205. }
  206. void XMLNode::Print( FILE* fp, int depth )
  207. {
  208. for( XMLNode* node = firstChild; node; node=node->next ) {
  209. node->Print( fp, depth );
  210. }
  211. }
  212. void XMLNode::PrintSpace( FILE* fp, int depth )
  213. {
  214. for( int i=0; i<depth; ++i ) {
  215. fprintf( fp, " " );
  216. }
  217. }
  218. // --------- XMLText ---------- //
  219. char* XMLText::ParseDeep( char* p )
  220. {
  221. p = ParseText( p, &value, "<" );
  222. // consumes the end tag.
  223. if ( p && *p ) {
  224. return p-1;
  225. }
  226. return 0;
  227. }
  228. void XMLText::Print( FILE* cfile, int depth )
  229. {
  230. fprintf( cfile, value.GetStr() );
  231. }
  232. // --------- XMLComment ---------- //
  233. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  234. {
  235. }
  236. XMLComment::~XMLComment()
  237. {
  238. //printf( "~XMLComment\n" );
  239. }
  240. void XMLComment::Print( FILE* fp, int depth )
  241. {
  242. XMLNode::Print( fp, depth );
  243. fprintf( fp, "<!--%s-->\n", value.GetStr() );
  244. }
  245. char* XMLComment::ParseDeep( char* p )
  246. {
  247. // Comment parses as text.
  248. return ParseText( p, &value, "-->" );
  249. }
  250. // --------- XMLAttribute ---------- //
  251. char* XMLAttribute::ParseDeep( char* p )
  252. {
  253. p = ParseText( p, &name, "=" );
  254. if ( !p || !*p ) return 0;
  255. char endTag[2] = { *p, 0 };
  256. ++p;
  257. p = ParseText( p, &value, endTag );
  258. if ( value.Empty() ) return 0;
  259. return p;
  260. }
  261. void XMLAttribute::Print( FILE* cfile )
  262. {
  263. // fixme: sort out single vs. double quote
  264. fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
  265. }
  266. // --------- XMLElement ---------- //
  267. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  268. closing( false ),
  269. rootAttribute( 0 ),
  270. lastAttribute( 0 )
  271. {
  272. }
  273. XMLElement::~XMLElement()
  274. {
  275. //printf( "~XMLElemen %x\n",this );
  276. XMLAttribute* attribute = rootAttribute;
  277. while( attribute ) {
  278. XMLAttribute* next = attribute->next;
  279. delete attribute;
  280. attribute = next;
  281. }
  282. }
  283. char* XMLElement::ParseDeep( char* p )
  284. {
  285. // Read the element name.
  286. p = SkipWhiteSpace( p );
  287. if ( !p ) return 0;
  288. const char* start = p;
  289. // The closing element is the </element> form. It is
  290. // parsed just like a regular element then deleted from
  291. // the DOM.
  292. if ( *p == '/' ) {
  293. closing = true;
  294. ++p;
  295. }
  296. p = ParseName( p, &name );
  297. if ( name.Empty() ) return 0;
  298. // Read the attributes.
  299. while( p ) {
  300. p = SkipWhiteSpace( p );
  301. if ( !p || !(*p) ) {
  302. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
  303. return 0;
  304. }
  305. // attribute.
  306. if ( IsAlpha( *p ) ) {
  307. XMLAttribute* attrib = new XMLAttribute( this );
  308. p = attrib->ParseDeep( p );
  309. if ( !p ) {
  310. delete attrib;
  311. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  312. return 0;
  313. }
  314. if ( rootAttribute ) {
  315. TIXMLASSERT( lastAttribute );
  316. lastAttribute->next = attrib;
  317. lastAttribute = attrib;
  318. }
  319. else {
  320. rootAttribute = lastAttribute = attrib;
  321. }
  322. }
  323. // end of the tag
  324. else if ( *p == '/' && *(p+1) == '>' ) {
  325. if ( closing ) {
  326. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  327. return 0;
  328. }
  329. return p+2; // done; sealed element.
  330. }
  331. // end of the tag
  332. else if ( *p == '>' ) {
  333. ++p;
  334. break;
  335. }
  336. else {
  337. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  338. return 0;
  339. }
  340. }
  341. while( p && *p ) {
  342. XMLNode* node = 0;
  343. p = Identify( document, p, &node );
  344. if ( p && node ) {
  345. p = node->ParseDeep( p );
  346. XMLElement* element = node->ToElement();
  347. if ( element && element->Closing() ) {
  348. if ( StringEqual( element->Name(), this->Name() ) ) {
  349. // All good, this is closing tag.
  350. delete node;
  351. }
  352. else {
  353. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  354. delete node;
  355. p = 0;
  356. }
  357. return p;
  358. }
  359. else {
  360. this->InsertEndChild( node );
  361. }
  362. }
  363. }
  364. return 0;
  365. }
  366. void XMLElement::Print( FILE* cfile, int depth )
  367. {
  368. PrintSpace( cfile, depth );
  369. fprintf( cfile, "<%s", Name() );
  370. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  371. fprintf( cfile, " " );
  372. attrib->Print( cfile );
  373. }
  374. if ( firstChild ) {
  375. // fixme: once text is on, it should stay on, and not use newlines.
  376. bool useNewline = firstChild->ToText() == 0;
  377. fprintf( cfile, ">", Name() );
  378. if ( useNewline ) fprintf( cfile, "\n" );
  379. for( XMLNode* node=firstChild; node; node=node->next ) {
  380. node->Print( cfile, depth+1 );
  381. }
  382. fprintf( cfile, "</%s>\n", Name() );
  383. // fixme: see note above
  384. //if ( useNewline ) fprintf( cfile, "\n" );
  385. }
  386. else {
  387. fprintf( cfile, "/>\n" );
  388. }
  389. }
  390. // --------- XMLDocument ----------- //
  391. XMLDocument::XMLDocument() :
  392. charBuffer( 0 )
  393. {
  394. root = new XMLNode( this );
  395. }
  396. XMLDocument::~XMLDocument()
  397. {
  398. delete root;
  399. delete charBuffer;
  400. }
  401. bool XMLDocument::Parse( const char* p )
  402. {
  403. charBuffer = CharBuffer::Construct( p );
  404. XMLNode* node = 0;
  405. // fixme: clean up
  406. char* q = Identify( this, charBuffer->mem, &node );
  407. while ( node ) {
  408. root->InsertEndChild( node );
  409. q = node->ParseDeep( q );
  410. node = 0;
  411. if ( q && *q ) {
  412. q = Identify( this, q, &node );
  413. }
  414. }
  415. return false;
  416. }
  417. void XMLDocument::Print( FILE* fp, int depth )
  418. {
  419. for( XMLNode* node = root->firstChild; node; node=node->next ) {
  420. node->Print( fp, depth );
  421. }
  422. }