tinyxml2.cpp 16 KB

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