tinyxml2.cpp 18 KB

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