tinyxml2.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. isTextParent( false ),
  159. firstChild( 0 ), lastChild( 0 ),
  160. prev( 0 ), next( 0 )
  161. {
  162. }
  163. XMLNode::~XMLNode()
  164. {
  165. //printf( "~XMLNode %x\n", this );
  166. while( firstChild ) {
  167. XMLNode* node = firstChild;
  168. Unlink( node );
  169. delete node;
  170. }
  171. }
  172. void XMLNode::Unlink( XMLNode* child )
  173. {
  174. TIXMLASSERT( child->parent == this );
  175. if ( child == firstChild )
  176. firstChild = firstChild->next;
  177. if ( child == lastChild )
  178. lastChild = lastChild->prev;
  179. if ( child->prev ) {
  180. child->prev->next = child->next;
  181. }
  182. if ( child->next ) {
  183. child->next->prev = child->prev;
  184. }
  185. child->parent = 0;
  186. }
  187. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  188. {
  189. if ( lastChild ) {
  190. TIXMLASSERT( firstChild );
  191. TIXMLASSERT( lastChild->next == 0 );
  192. lastChild->next = addThis;
  193. addThis->prev = lastChild;
  194. lastChild = addThis;
  195. addThis->parent = this;
  196. addThis->next = 0;
  197. }
  198. else {
  199. TIXMLASSERT( firstChild == 0 );
  200. firstChild = lastChild = addThis;
  201. addThis->parent = this;
  202. addThis->prev = 0;
  203. addThis->next = 0;
  204. }
  205. if ( addThis->ToText() ) {
  206. SetTextParent();
  207. }
  208. return addThis;
  209. }
  210. void XMLNode::Print( XMLStreamer* streamer )
  211. {
  212. for( XMLNode* node = firstChild; node; node=node->next ) {
  213. node->Print( streamer );
  214. }
  215. }
  216. char* XMLNode::ParseDeep( char* p )
  217. {
  218. while( p && *p ) {
  219. XMLNode* node = 0;
  220. p = Identify( document, p, &node );
  221. if ( p && node ) {
  222. p = node->ParseDeep( p );
  223. // FIXME: is it the correct closing element?
  224. if ( node->IsClosingElement() ) {
  225. delete node;
  226. return p;
  227. }
  228. this->InsertEndChild( node );
  229. }
  230. }
  231. return 0;
  232. }
  233. /*
  234. void XMLNode::PrintSpace( FILE* fp, int depth )
  235. {
  236. for( int i=0; i<depth; ++i ) {
  237. fprintf( fp, " " );
  238. }
  239. }
  240. */
  241. // --------- XMLText ---------- //
  242. char* XMLText::ParseDeep( char* p )
  243. {
  244. p = ParseText( p, &value, "<" );
  245. // consumes the end tag.
  246. if ( p && *p ) {
  247. return p-1;
  248. }
  249. return 0;
  250. }
  251. void XMLText::Print( XMLStreamer* streamer )
  252. {
  253. const char* v = value.GetStr();
  254. streamer->PushText( v );
  255. }
  256. // --------- XMLComment ---------- //
  257. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  258. {
  259. }
  260. XMLComment::~XMLComment()
  261. {
  262. //printf( "~XMLComment\n" );
  263. }
  264. void XMLComment::Print( XMLStreamer* streamer )
  265. {
  266. // XMLNode::Print( fp, depth );
  267. // fprintf( fp, "<!--%s-->\n", value.GetStr() );
  268. streamer->PushComment( value.GetStr() );
  269. }
  270. char* XMLComment::ParseDeep( char* p )
  271. {
  272. // Comment parses as text.
  273. return ParseText( p, &value, "-->" );
  274. }
  275. // --------- XMLAttribute ---------- //
  276. char* XMLAttribute::ParseDeep( char* p )
  277. {
  278. p = ParseText( p, &name, "=" );
  279. if ( !p || !*p ) return 0;
  280. char endTag[2] = { *p, 0 };
  281. ++p;
  282. p = ParseText( p, &value, endTag );
  283. if ( value.Empty() ) return 0;
  284. return p;
  285. }
  286. void XMLAttribute::Print( XMLStreamer* streamer )
  287. {
  288. // fixme: sort out single vs. double quote
  289. //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
  290. streamer->PushAttribute( name.GetStr(), value.GetStr() );
  291. }
  292. // --------- XMLElement ---------- //
  293. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  294. closing( false ),
  295. rootAttribute( 0 ),
  296. lastAttribute( 0 )
  297. {
  298. }
  299. XMLElement::~XMLElement()
  300. {
  301. //printf( "~XMLElemen %x\n",this );
  302. XMLAttribute* attribute = rootAttribute;
  303. while( attribute ) {
  304. XMLAttribute* next = attribute->next;
  305. delete attribute;
  306. attribute = next;
  307. }
  308. }
  309. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  310. {
  311. const char* start = p;
  312. *closedElement = false;
  313. // Read the attributes.
  314. while( p ) {
  315. p = SkipWhiteSpace( p );
  316. if ( !p || !(*p) ) {
  317. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
  318. return 0;
  319. }
  320. // attribute.
  321. if ( IsAlpha( *p ) ) {
  322. XMLAttribute* attrib = new XMLAttribute( this );
  323. p = attrib->ParseDeep( p );
  324. if ( !p ) {
  325. delete attrib;
  326. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  327. return 0;
  328. }
  329. if ( rootAttribute ) {
  330. TIXMLASSERT( lastAttribute );
  331. lastAttribute->next = attrib;
  332. lastAttribute = attrib;
  333. }
  334. else {
  335. rootAttribute = lastAttribute = attrib;
  336. }
  337. }
  338. // end of the tag
  339. else if ( *p == '/' && *(p+1) == '>' ) {
  340. if ( closing ) {
  341. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  342. return 0;
  343. }
  344. *closedElement = true;
  345. return p+2; // done; sealed element.
  346. }
  347. // end of the tag
  348. else if ( *p == '>' ) {
  349. ++p;
  350. break;
  351. }
  352. else {
  353. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  354. return 0;
  355. }
  356. }
  357. return p;
  358. }
  359. //
  360. // <ele></ele>
  361. // <ele>foo<b>bar</b></ele>
  362. //
  363. char* XMLElement::ParseDeep( char* p )
  364. {
  365. // Read the element name.
  366. p = SkipWhiteSpace( p );
  367. if ( !p ) return 0;
  368. const char* start = p;
  369. // The closing element is the </element> form. It is
  370. // parsed just like a regular element then deleted from
  371. // the DOM.
  372. if ( *p == '/' ) {
  373. closing = true;
  374. ++p;
  375. }
  376. p = ParseName( p, &name );
  377. if ( name.Empty() ) return 0;
  378. bool elementClosed=false;
  379. p = ParseAttributes( p, &elementClosed );
  380. if ( !p || !*p || elementClosed || closing )
  381. return p;
  382. p = XMLNode::ParseDeep( p );
  383. return p;
  384. }
  385. void XMLElement::Print( XMLStreamer* streamer )
  386. {
  387. //if ( !parent || !parent->IsTextParent() ) {
  388. // PrintSpace( cfile, depth );
  389. //}
  390. //fprintf( cfile, "<%s", Name() );
  391. streamer->OpenElement( Name(), IsTextParent() );
  392. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  393. //fprintf( cfile, " " );
  394. attrib->Print( streamer );
  395. }
  396. for( XMLNode* node=firstChild; node; node=node->next ) {
  397. node->Print( streamer );
  398. }
  399. streamer->CloseElement();
  400. /* if ( firstChild ) {
  401. fprintf( cfile, ">", Name() );
  402. if ( !IsTextParent() ) {
  403. fprintf( cfile, "\n" );
  404. }
  405. for( XMLNode* node=firstChild; node; node=node->next ) {
  406. node->Print( cfile, depth+1 );
  407. }
  408. fprintf( cfile, "</%s>", Name() );
  409. if ( !IsTextParent() ) {
  410. fprintf( cfile, "\n" );
  411. }
  412. }
  413. else {
  414. fprintf( cfile, "/>" );
  415. if ( !IsTextParent() ) {
  416. fprintf( cfile, "\n" );
  417. }
  418. }*/
  419. }
  420. // --------- XMLDocument ----------- //
  421. XMLDocument::XMLDocument() :
  422. XMLNode( this ),
  423. charBuffer( 0 )
  424. {
  425. }
  426. XMLDocument::~XMLDocument()
  427. {
  428. }
  429. bool XMLDocument::Parse( const char* p )
  430. {
  431. charBuffer = CharBuffer::Construct( p );
  432. XMLNode* node = 0;
  433. char* q = ParseDeep( charBuffer->mem );
  434. return true;
  435. }
  436. void XMLDocument::Print( XMLStreamer* streamer )
  437. {
  438. XMLStreamer stdStreamer( stdout );
  439. if ( !streamer )
  440. streamer = &stdStreamer;
  441. for( XMLNode* node = firstChild; node; node=node->next ) {
  442. node->Print( streamer );
  443. }
  444. }
  445. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  446. {
  447. printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 );
  448. }
  449. StringStack::StringStack()
  450. {
  451. *pool = 0;
  452. mem = pool;
  453. inUse = 1; // always has a null
  454. allocated = INIT;
  455. nPositive = 0;
  456. }
  457. StringStack::~StringStack()
  458. {
  459. if ( mem != pool ) {
  460. delete [] mem;
  461. }
  462. }
  463. void StringStack::Push( const char* str ) {
  464. int needed = strlen( str ) + 1;
  465. if ( needed > 1 )
  466. nPositive++;
  467. if ( inUse+needed > allocated ) {
  468. // fixme: power of 2
  469. // less stupid allocation
  470. int more = inUse+needed + 1000;
  471. char* newMem = new char[more];
  472. memcpy( newMem, mem, inUse );
  473. if ( mem != pool ) {
  474. delete [] mem;
  475. }
  476. mem = newMem;
  477. }
  478. strcpy( mem+inUse, str );
  479. inUse += needed;
  480. }
  481. const char* StringStack::Pop() {
  482. TIXMLASSERT( inUse > 1 );
  483. const char* p = mem+inUse-2;
  484. if ( *p ) {
  485. nPositive--;
  486. }
  487. while( *p ) { // stack starts with a null, don't need to check for 'mem'
  488. TIXMLASSERT( p > mem );
  489. --p;
  490. }
  491. inUse = p-mem+1;
  492. return p+1;
  493. }
  494. XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false )
  495. {
  496. }
  497. void XMLStreamer::PrintSpace( int depth )
  498. {
  499. for( int i=0; i<depth; ++i ) {
  500. fprintf( fp, " " );
  501. }
  502. }
  503. void XMLStreamer::OpenElement( const char* name, bool textParent )
  504. {
  505. if ( elementJustOpened ) {
  506. SealElement();
  507. }
  508. if ( text.NumPositive() == 0 ) {
  509. PrintSpace( depth );
  510. }
  511. stack.Push( name );
  512. text.Push( textParent ? "T" : "" );
  513. fprintf( fp, "<%s", name );
  514. elementJustOpened = true;
  515. ++depth;
  516. }
  517. void XMLStreamer::PushAttribute( const char* name, const char* value )
  518. {
  519. TIXMLASSERT( elementJustOpened );
  520. fprintf( fp, " %s=\"%s\"", name, value );
  521. }
  522. void XMLStreamer::CloseElement()
  523. {
  524. --depth;
  525. const char* name = stack.Pop();
  526. int wasPositive = text.NumPositive();
  527. text.Pop();
  528. if ( elementJustOpened ) {
  529. fprintf( fp, "/>" );
  530. if ( text.NumPositive() == 0 ) {
  531. fprintf( fp, "\n" );
  532. }
  533. }
  534. else {
  535. if ( wasPositive == 0 ) {
  536. PrintSpace( depth );
  537. }
  538. fprintf( fp, "</%s>", name );
  539. if ( text.NumPositive() == 0 ) {
  540. fprintf( fp, "\n" );
  541. }
  542. }
  543. elementJustOpened = false;
  544. }
  545. void XMLStreamer::SealElement()
  546. {
  547. elementJustOpened = false;
  548. fprintf( fp, ">" );
  549. if ( text.NumPositive() == 0 ) {
  550. fprintf( fp, "\n" );
  551. }
  552. }
  553. void XMLStreamer::PushText( const char* text )
  554. {
  555. if ( elementJustOpened ) {
  556. SealElement();
  557. }
  558. fprintf( fp, "%s", text );
  559. }
  560. void XMLStreamer::PushComment( const char* comment )
  561. {
  562. if ( elementJustOpened ) {
  563. SealElement();
  564. }
  565. PrintSpace( depth );
  566. fprintf( fp, "<!--%s-->\n", comment );
  567. }