1
0

tinyxml2.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. struct Entity {
  14. const char* pattern;
  15. int length;
  16. char value;
  17. };
  18. static const int NUM_ENTITIES = 5;
  19. static const Entity entities[NUM_ENTITIES] =
  20. {
  21. { "quot", 4, '\"' },
  22. { "amp", 3, '&' },
  23. { "apos", 4, '\'' },
  24. { "lt", 2, '<' },
  25. { "gt", 2, '>' }
  26. };
  27. // --------- CharBuffer ----------- //
  28. /*static*/ CharBuffer* CharBuffer::Construct( const char* in )
  29. {
  30. size_t len = strlen( in );
  31. size_t size = len + sizeof( CharBuffer );
  32. CharBuffer* cb = (CharBuffer*) malloc( size );
  33. cb->length = len;
  34. strcpy( cb->mem, in );
  35. return cb;
  36. }
  37. /*static*/ void CharBuffer::Free( CharBuffer* cb )
  38. {
  39. free( cb );
  40. }
  41. const char* StrPair::GetStr()
  42. {
  43. if ( flags & NEEDS_FLUSH ) {
  44. *end = 0;
  45. flags ^= NEEDS_FLUSH;
  46. if ( flags ) {
  47. char* p = start;
  48. char* q = start;
  49. while( p < end ) {
  50. if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  51. // CR-LF pair becomes LF
  52. // CR alone becomes LF
  53. // LF-CR becomes LF
  54. if ( *(p+1) == LF ) {
  55. p += 2;
  56. }
  57. else {
  58. ++p;
  59. }
  60. *q = LF;
  61. }
  62. else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  63. if ( *(p+1) == CR ) {
  64. p += 2;
  65. }
  66. else {
  67. ++p;
  68. }
  69. *q = LF;
  70. }
  71. else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  72. int i=0;
  73. for( i=0; i<NUM_ENTITIES; ++i ) {
  74. if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
  75. && *(p+entities[i].length+1) == ';' )
  76. {
  77. // Found an entity convert;
  78. *q = entities[i].value;
  79. ++q;
  80. p += entities[i].length + 2;
  81. break;
  82. }
  83. }
  84. if ( i == NUM_ENTITIES ) {
  85. // fixme: treat as error?
  86. ++p;
  87. ++q;
  88. }
  89. }
  90. else {
  91. *q = *p;
  92. ++p;
  93. ++q;
  94. }
  95. }
  96. *q = 0;
  97. }
  98. flags = 0;
  99. }
  100. return start;
  101. }
  102. // --------- XMLBase ----------- //
  103. char* XMLBase::ParseText( char* p, StrPair* pair, const char* endTag )
  104. {
  105. TIXMLASSERT( endTag && *endTag );
  106. char* start = p;
  107. char endChar = *endTag;
  108. int length = strlen( endTag );
  109. // Inner loop of text parsing.
  110. while ( *p ) {
  111. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  112. pair->Set( start, p, StrPair::NEEDS_ENTITY_PROCESSING | StrPair::NEEDS_NEWLINE_NORMALIZATION );
  113. return p + length;
  114. }
  115. ++p;
  116. }
  117. return p;
  118. }
  119. char* XMLBase::ParseName( char* p, StrPair* pair )
  120. {
  121. char* start = p;
  122. start = p;
  123. if ( !start || !(*start) ) {
  124. return 0;
  125. }
  126. if ( !IsAlpha( *p ) ) {
  127. return 0;
  128. }
  129. while( *p && (
  130. IsAlphaNum( (unsigned char) *p )
  131. || *p == '_'
  132. || *p == '-'
  133. || *p == '.'
  134. || *p == ':' ))
  135. {
  136. ++p;
  137. }
  138. if ( p > start ) {
  139. pair->Set( start, p, 0 );
  140. return p;
  141. }
  142. return 0;
  143. }
  144. char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
  145. {
  146. XMLNode* returnNode = 0;
  147. char* start = p;
  148. p = XMLNode::SkipWhiteSpace( p );
  149. if( !p || !*p )
  150. {
  151. return 0;
  152. }
  153. // What is this thing?
  154. // - Elements start with a letter or underscore, but xml is reserved.
  155. // - Comments: <!--
  156. // - Decleration: <?xml
  157. // - Everthing else is unknown to tinyxml.
  158. //
  159. static const char* xmlHeader = { "<?xml" };
  160. static const char* commentHeader = { "<!--" };
  161. static const char* dtdHeader = { "<!" };
  162. static const char* cdataHeader = { "<![CDATA[" };
  163. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  164. static const int xmlHeaderLen = 5;
  165. static const int commentHeaderLen = 4;
  166. static const int dtdHeaderLen = 2;
  167. static const int cdataHeaderLen = 9;
  168. static const int elementHeaderLen = 1;
  169. if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
  170. returnNode = new XMLComment( document );
  171. p += commentHeaderLen;
  172. }
  173. else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
  174. returnNode = new XMLElement( document );
  175. p += elementHeaderLen;
  176. }
  177. // fixme: better text detection
  178. else if ( (*p != '<') && IsAlphaNum( *p ) ) {
  179. // fixme: this is filtering out empty text...should it?
  180. returnNode = new XMLText( document );
  181. p = start; // Back it up, all the text counts.
  182. }
  183. else {
  184. TIXMLASSERT( 0 );
  185. }
  186. *node = returnNode;
  187. return p;
  188. }
  189. // --------- XMLNode ----------- //
  190. XMLNode::XMLNode( XMLDocument* doc ) :
  191. document( doc ),
  192. parent( 0 ),
  193. isTextParent( false ),
  194. firstChild( 0 ), lastChild( 0 ),
  195. prev( 0 ), next( 0 )
  196. {
  197. }
  198. XMLNode::~XMLNode()
  199. {
  200. //printf( "~XMLNode %x\n", this );
  201. while( firstChild ) {
  202. XMLNode* node = firstChild;
  203. Unlink( node );
  204. delete node;
  205. }
  206. }
  207. void XMLNode::Unlink( XMLNode* child )
  208. {
  209. TIXMLASSERT( child->parent == this );
  210. if ( child == firstChild )
  211. firstChild = firstChild->next;
  212. if ( child == lastChild )
  213. lastChild = lastChild->prev;
  214. if ( child->prev ) {
  215. child->prev->next = child->next;
  216. }
  217. if ( child->next ) {
  218. child->next->prev = child->prev;
  219. }
  220. child->parent = 0;
  221. }
  222. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  223. {
  224. if ( lastChild ) {
  225. TIXMLASSERT( firstChild );
  226. TIXMLASSERT( lastChild->next == 0 );
  227. lastChild->next = addThis;
  228. addThis->prev = lastChild;
  229. lastChild = addThis;
  230. addThis->parent = this;
  231. addThis->next = 0;
  232. }
  233. else {
  234. TIXMLASSERT( firstChild == 0 );
  235. firstChild = lastChild = addThis;
  236. addThis->parent = this;
  237. addThis->prev = 0;
  238. addThis->next = 0;
  239. }
  240. if ( addThis->ToText() ) {
  241. SetTextParent();
  242. }
  243. return addThis;
  244. }
  245. void XMLNode::Print( XMLStreamer* streamer )
  246. {
  247. for( XMLNode* node = firstChild; node; node=node->next ) {
  248. node->Print( streamer );
  249. }
  250. }
  251. char* XMLNode::ParseDeep( char* p )
  252. {
  253. while( p && *p ) {
  254. XMLNode* node = 0;
  255. p = Identify( document, p, &node );
  256. if ( p && node ) {
  257. p = node->ParseDeep( p );
  258. // FIXME: is it the correct closing element?
  259. if ( node->IsClosingElement() ) {
  260. delete node;
  261. return p;
  262. }
  263. this->InsertEndChild( node );
  264. }
  265. }
  266. return 0;
  267. }
  268. // --------- XMLText ---------- //
  269. char* XMLText::ParseDeep( char* p )
  270. {
  271. p = ParseText( p, &value, "<" );
  272. // consumes the end tag.
  273. if ( p && *p ) {
  274. return p-1;
  275. }
  276. return 0;
  277. }
  278. void XMLText::Print( XMLStreamer* streamer )
  279. {
  280. const char* v = value.GetStr();
  281. streamer->PushText( v );
  282. }
  283. // --------- XMLComment ---------- //
  284. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  285. {
  286. }
  287. XMLComment::~XMLComment()
  288. {
  289. //printf( "~XMLComment\n" );
  290. }
  291. void XMLComment::Print( XMLStreamer* streamer )
  292. {
  293. // XMLNode::Print( fp, depth );
  294. // fprintf( fp, "<!--%s-->\n", value.GetStr() );
  295. streamer->PushComment( value.GetStr() );
  296. }
  297. char* XMLComment::ParseDeep( char* p )
  298. {
  299. // Comment parses as text.
  300. return ParseText( p, &value, "-->" );
  301. }
  302. // --------- XMLAttribute ---------- //
  303. char* XMLAttribute::ParseDeep( char* p )
  304. {
  305. p = ParseText( p, &name, "=" );
  306. if ( !p || !*p ) return 0;
  307. char endTag[2] = { *p, 0 };
  308. ++p;
  309. p = ParseText( p, &value, endTag );
  310. if ( value.Empty() ) return 0;
  311. return p;
  312. }
  313. void XMLAttribute::Print( XMLStreamer* streamer )
  314. {
  315. // fixme: sort out single vs. double quote
  316. //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
  317. streamer->PushAttribute( name.GetStr(), value.GetStr() );
  318. }
  319. // --------- XMLElement ---------- //
  320. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  321. closing( false ),
  322. rootAttribute( 0 ),
  323. lastAttribute( 0 )
  324. {
  325. }
  326. XMLElement::~XMLElement()
  327. {
  328. //printf( "~XMLElemen %x\n",this );
  329. XMLAttribute* attribute = rootAttribute;
  330. while( attribute ) {
  331. XMLAttribute* next = attribute->next;
  332. delete attribute;
  333. attribute = next;
  334. }
  335. }
  336. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  337. {
  338. const char* start = p;
  339. *closedElement = false;
  340. // Read the attributes.
  341. while( p ) {
  342. p = SkipWhiteSpace( p );
  343. if ( !p || !(*p) ) {
  344. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
  345. return 0;
  346. }
  347. // attribute.
  348. if ( IsAlpha( *p ) ) {
  349. XMLAttribute* attrib = new XMLAttribute( this );
  350. p = attrib->ParseDeep( p );
  351. if ( !p ) {
  352. delete attrib;
  353. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  354. return 0;
  355. }
  356. if ( rootAttribute ) {
  357. TIXMLASSERT( lastAttribute );
  358. lastAttribute->next = attrib;
  359. lastAttribute = attrib;
  360. }
  361. else {
  362. rootAttribute = lastAttribute = attrib;
  363. }
  364. }
  365. // end of the tag
  366. else if ( *p == '/' && *(p+1) == '>' ) {
  367. if ( closing ) {
  368. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  369. return 0;
  370. }
  371. *closedElement = true;
  372. return p+2; // done; sealed element.
  373. }
  374. // end of the tag
  375. else if ( *p == '>' ) {
  376. ++p;
  377. break;
  378. }
  379. else {
  380. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  381. return 0;
  382. }
  383. }
  384. return p;
  385. }
  386. //
  387. // <ele></ele>
  388. // <ele>foo<b>bar</b></ele>
  389. //
  390. char* XMLElement::ParseDeep( char* p )
  391. {
  392. // Read the element name.
  393. p = SkipWhiteSpace( p );
  394. if ( !p ) return 0;
  395. const char* start = p;
  396. // The closing element is the </element> form. It is
  397. // parsed just like a regular element then deleted from
  398. // the DOM.
  399. if ( *p == '/' ) {
  400. closing = true;
  401. ++p;
  402. }
  403. p = ParseName( p, &name );
  404. if ( name.Empty() ) return 0;
  405. bool elementClosed=false;
  406. p = ParseAttributes( p, &elementClosed );
  407. if ( !p || !*p || elementClosed || closing )
  408. return p;
  409. p = XMLNode::ParseDeep( p );
  410. return p;
  411. }
  412. void XMLElement::Print( XMLStreamer* streamer )
  413. {
  414. //if ( !parent || !parent->IsTextParent() ) {
  415. // PrintSpace( cfile, depth );
  416. //}
  417. //fprintf( cfile, "<%s", Name() );
  418. streamer->OpenElement( Name(), IsTextParent() );
  419. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  420. //fprintf( cfile, " " );
  421. attrib->Print( streamer );
  422. }
  423. for( XMLNode* node=firstChild; node; node=node->next ) {
  424. node->Print( streamer );
  425. }
  426. streamer->CloseElement();
  427. }
  428. // --------- XMLDocument ----------- //
  429. XMLDocument::XMLDocument() :
  430. XMLNode( this ),
  431. charBuffer( 0 )
  432. {
  433. }
  434. XMLDocument::~XMLDocument()
  435. {
  436. }
  437. bool XMLDocument::Parse( const char* p )
  438. {
  439. charBuffer = CharBuffer::Construct( p );
  440. XMLNode* node = 0;
  441. char* q = ParseDeep( charBuffer->mem );
  442. return true;
  443. }
  444. void XMLDocument::Print( XMLStreamer* streamer )
  445. {
  446. XMLStreamer stdStreamer( stdout );
  447. if ( !streamer )
  448. streamer = &stdStreamer;
  449. for( XMLNode* node = firstChild; node; node=node->next ) {
  450. node->Print( streamer );
  451. }
  452. }
  453. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  454. {
  455. printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 );
  456. }
  457. StringStack::StringStack()
  458. {
  459. *pool = 0;
  460. mem = pool;
  461. inUse = 1; // always has a null
  462. allocated = INIT;
  463. nPositive = 0;
  464. }
  465. StringStack::~StringStack()
  466. {
  467. if ( mem != pool ) {
  468. delete [] mem;
  469. }
  470. }
  471. void StringStack::Push( const char* str ) {
  472. int needed = strlen( str ) + 1;
  473. if ( needed > 1 )
  474. nPositive++;
  475. if ( inUse+needed > allocated ) {
  476. // fixme: power of 2
  477. // less stupid allocation
  478. int more = inUse+needed + 1000;
  479. char* newMem = new char[more];
  480. memcpy( newMem, mem, inUse );
  481. if ( mem != pool ) {
  482. delete [] mem;
  483. }
  484. mem = newMem;
  485. }
  486. strcpy( mem+inUse, str );
  487. inUse += needed;
  488. }
  489. const char* StringStack::Pop() {
  490. TIXMLASSERT( inUse > 1 );
  491. const char* p = mem+inUse-2;
  492. if ( *p ) {
  493. nPositive--;
  494. }
  495. while( *p ) { // stack starts with a null, don't need to check for 'mem'
  496. TIXMLASSERT( p > mem );
  497. --p;
  498. }
  499. inUse = p-mem+1;
  500. return p+1;
  501. }
  502. XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false )
  503. {
  504. }
  505. void XMLStreamer::PrintSpace( int depth )
  506. {
  507. for( int i=0; i<depth; ++i ) {
  508. fprintf( fp, " " );
  509. }
  510. }
  511. void XMLStreamer::OpenElement( const char* name, bool textParent )
  512. {
  513. if ( elementJustOpened ) {
  514. SealElement();
  515. }
  516. if ( text.NumPositive() == 0 ) {
  517. PrintSpace( depth );
  518. }
  519. stack.Push( name );
  520. text.Push( textParent ? "T" : "" );
  521. fprintf( fp, "<%s", name );
  522. elementJustOpened = true;
  523. ++depth;
  524. }
  525. void XMLStreamer::PushAttribute( const char* name, const char* value )
  526. {
  527. TIXMLASSERT( elementJustOpened );
  528. fprintf( fp, " %s=\"%s\"", name, value );
  529. }
  530. void XMLStreamer::CloseElement()
  531. {
  532. --depth;
  533. const char* name = stack.Pop();
  534. int wasPositive = text.NumPositive();
  535. text.Pop();
  536. if ( elementJustOpened ) {
  537. fprintf( fp, "/>" );
  538. if ( text.NumPositive() == 0 ) {
  539. fprintf( fp, "\n" );
  540. }
  541. }
  542. else {
  543. if ( wasPositive == 0 ) {
  544. PrintSpace( depth );
  545. }
  546. fprintf( fp, "</%s>", name );
  547. if ( text.NumPositive() == 0 ) {
  548. fprintf( fp, "\n" );
  549. }
  550. }
  551. elementJustOpened = false;
  552. }
  553. void XMLStreamer::SealElement()
  554. {
  555. elementJustOpened = false;
  556. fprintf( fp, ">" );
  557. if ( text.NumPositive() == 0 ) {
  558. fprintf( fp, "\n" );
  559. }
  560. }
  561. void XMLStreamer::PushText( const char* text )
  562. {
  563. if ( elementJustOpened ) {
  564. SealElement();
  565. }
  566. fprintf( fp, "%s", text );
  567. }
  568. void XMLStreamer::PushComment( const char* comment )
  569. {
  570. if ( elementJustOpened ) {
  571. SealElement();
  572. }
  573. PrintSpace( depth );
  574. fprintf( fp, "<!--%s-->\n", comment );
  575. }