tinyxml2.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  182. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  183. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  184. returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
  185. returnNode->memPool = &commentPool;
  186. p += xmlHeaderLen;
  187. }
  188. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  189. returnNode = new (commentPool.Alloc()) XMLComment( this );
  190. returnNode->memPool = &commentPool;
  191. p += commentHeaderLen;
  192. }
  193. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  194. XMLText* text = new (textPool.Alloc()) XMLText( this );
  195. returnNode = text;
  196. returnNode->memPool = &textPool;
  197. p += cdataHeaderLen;
  198. text->SetCData( true );
  199. }
  200. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  201. returnNode = new (commentPool.Alloc()) XMLUnknown( this );
  202. returnNode->memPool = &commentPool;
  203. p += dtdHeaderLen;
  204. }
  205. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  206. returnNode = new (elementPool.Alloc()) XMLElement( this );
  207. returnNode->memPool = &elementPool;
  208. p += elementHeaderLen;
  209. }
  210. else if ( (*p != '<') && XMLUtil::IsAlphaNum( *p ) ) {
  211. returnNode = new (textPool.Alloc()) XMLText( this );
  212. returnNode->memPool = &textPool;
  213. p = start; // Back it up, all the text counts.
  214. }
  215. else {
  216. this->SetError( ERROR_IDENTIFYING_TAG, p, 0 );
  217. p = 0;
  218. returnNode = 0;
  219. }
  220. *node = returnNode;
  221. return p;
  222. }
  223. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  224. {
  225. if ( visitor->VisitEnter( *this ) )
  226. {
  227. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  228. {
  229. if ( !node->Accept( visitor ) )
  230. break;
  231. }
  232. }
  233. return visitor->VisitExit( *this );
  234. }
  235. // --------- XMLNode ----------- //
  236. XMLNode::XMLNode( XMLDocument* doc ) :
  237. document( doc ),
  238. parent( 0 ),
  239. firstChild( 0 ), lastChild( 0 ),
  240. prev( 0 ), next( 0 )
  241. {
  242. }
  243. XMLNode::~XMLNode()
  244. {
  245. ClearChildren();
  246. if ( parent ) {
  247. parent->Unlink( this );
  248. }
  249. }
  250. void XMLNode::ClearChildren()
  251. {
  252. while( firstChild ) {
  253. XMLNode* node = firstChild;
  254. Unlink( node );
  255. DELETE_NODE( node );
  256. }
  257. firstChild = lastChild = 0;
  258. }
  259. void XMLNode::Unlink( XMLNode* child )
  260. {
  261. TIXMLASSERT( child->parent == this );
  262. if ( child == firstChild )
  263. firstChild = firstChild->next;
  264. if ( child == lastChild )
  265. lastChild = lastChild->prev;
  266. if ( child->prev ) {
  267. child->prev->next = child->next;
  268. }
  269. if ( child->next ) {
  270. child->next->prev = child->prev;
  271. }
  272. child->parent = 0;
  273. }
  274. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  275. {
  276. if ( lastChild ) {
  277. TIXMLASSERT( firstChild );
  278. TIXMLASSERT( lastChild->next == 0 );
  279. lastChild->next = addThis;
  280. addThis->prev = lastChild;
  281. lastChild = addThis;
  282. addThis->parent = this;
  283. addThis->next = 0;
  284. }
  285. else {
  286. TIXMLASSERT( firstChild == 0 );
  287. firstChild = lastChild = addThis;
  288. addThis->parent = this;
  289. addThis->prev = 0;
  290. addThis->next = 0;
  291. }
  292. return addThis;
  293. }
  294. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  295. {
  296. for( XMLNode* node=firstChild; node; node=node->next ) {
  297. XMLElement* element = node->ToElement();
  298. if ( element ) {
  299. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  300. return element;
  301. }
  302. }
  303. }
  304. return 0;
  305. }
  306. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  307. {
  308. for( XMLNode* node=lastChild; node; node=node->prev ) {
  309. XMLElement* element = node->ToElement();
  310. if ( element ) {
  311. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  312. return element;
  313. }
  314. }
  315. }
  316. return 0;
  317. }
  318. void XMLNode::DeleteChild( XMLNode* node )
  319. {
  320. TIXMLASSERT( node->parent == this );
  321. TIXMLASSERT( 0 );
  322. }
  323. char* XMLNode::ParseDeep( char* p )
  324. {
  325. while( p && *p ) {
  326. XMLNode* node = 0;
  327. p = document->Identify( p, &node );
  328. if ( p && node ) {
  329. p = node->ParseDeep( p );
  330. // FIXME: is it the correct closing element?
  331. if ( node->IsClosingElement() ) {
  332. DELETE_NODE( node );
  333. return p;
  334. }
  335. this->InsertEndChild( node );
  336. }
  337. }
  338. return 0;
  339. }
  340. // --------- XMLText ---------- //
  341. char* XMLText::ParseDeep( char* p )
  342. {
  343. if ( this->CData() ) {
  344. p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  345. return p;
  346. }
  347. else {
  348. p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
  349. // consumes the end tag.
  350. if ( p && *p ) {
  351. return p-1;
  352. }
  353. }
  354. return 0;
  355. }
  356. bool XMLText::Accept( XMLVisitor* visitor ) const
  357. {
  358. return visitor->Visit( *this );
  359. }
  360. // --------- XMLComment ---------- //
  361. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  362. {
  363. }
  364. XMLComment::~XMLComment()
  365. {
  366. //printf( "~XMLComment\n" );
  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. // --------- XMLDeclaration ---------- //
  378. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  379. {
  380. }
  381. XMLDeclaration::~XMLDeclaration()
  382. {
  383. //printf( "~XMLDeclaration\n" );
  384. }
  385. char* XMLDeclaration::ParseDeep( char* p )
  386. {
  387. // Declaration parses as text.
  388. return value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  389. }
  390. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  391. {
  392. return visitor->Visit( *this );
  393. }
  394. // --------- XMLUnknown ---------- //
  395. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  396. {
  397. }
  398. XMLUnknown::~XMLUnknown()
  399. {
  400. }
  401. char* XMLUnknown::ParseDeep( char* p )
  402. {
  403. // Unknown parses as text.
  404. return value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  405. }
  406. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  407. {
  408. return visitor->Visit( *this );
  409. }
  410. // --------- XMLAttribute ---------- //
  411. char* XMLAttribute::ParseDeep( char* p )
  412. {
  413. p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
  414. if ( !p || !*p ) return 0;
  415. char endTag[2] = { *p, 0 };
  416. ++p;
  417. p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
  418. if ( value.Empty() ) return 0;
  419. return p;
  420. }
  421. // --------- XMLElement ---------- //
  422. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  423. closing( false ),
  424. rootAttribute( 0 ),
  425. lastAttribute( 0 )
  426. {
  427. }
  428. XMLElement::~XMLElement()
  429. {
  430. XMLAttribute* attribute = rootAttribute;
  431. while( attribute ) {
  432. XMLAttribute* next = attribute->next;
  433. DELETE_ATTRIBUTE( attribute );
  434. attribute = next;
  435. }
  436. }
  437. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  438. {
  439. const char* start = p;
  440. *closedElement = false;
  441. // Read the attributes.
  442. while( p ) {
  443. p = XMLUtil::SkipWhiteSpace( p );
  444. if ( !p || !(*p) ) {
  445. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
  446. return 0;
  447. }
  448. // attribute.
  449. if ( XMLUtil::IsAlpha( *p ) ) {
  450. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  451. attrib->memPool = &document->attributePool;
  452. p = attrib->ParseDeep( p );
  453. if ( !p ) {
  454. DELETE_ATTRIBUTE( attrib );
  455. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  456. return 0;
  457. }
  458. if ( rootAttribute ) {
  459. TIXMLASSERT( lastAttribute );
  460. lastAttribute->next = attrib;
  461. lastAttribute = attrib;
  462. }
  463. else {
  464. rootAttribute = lastAttribute = attrib;
  465. }
  466. }
  467. // end of the tag
  468. else if ( *p == '/' && *(p+1) == '>' ) {
  469. if ( closing ) {
  470. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  471. return 0;
  472. }
  473. *closedElement = true;
  474. return p+2; // done; sealed element.
  475. }
  476. // end of the tag
  477. else if ( *p == '>' ) {
  478. ++p;
  479. break;
  480. }
  481. else {
  482. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  483. return 0;
  484. }
  485. }
  486. return p;
  487. }
  488. //
  489. // <ele></ele>
  490. // <ele>foo<b>bar</b></ele>
  491. //
  492. char* XMLElement::ParseDeep( char* p )
  493. {
  494. // Read the element name.
  495. p = XMLUtil::SkipWhiteSpace( p );
  496. if ( !p ) return 0;
  497. const char* start = p;
  498. // The closing element is the </element> form. It is
  499. // parsed just like a regular element then deleted from
  500. // the DOM.
  501. if ( *p == '/' ) {
  502. closing = true;
  503. ++p;
  504. }
  505. p = value.ParseName( p );
  506. if ( value.Empty() ) return 0;
  507. bool elementClosed=false;
  508. p = ParseAttributes( p, &elementClosed );
  509. if ( !p || !*p || elementClosed || closing )
  510. return p;
  511. p = XMLNode::ParseDeep( p );
  512. return p;
  513. }
  514. bool XMLElement::Accept( XMLVisitor* visitor ) const
  515. {
  516. if ( visitor->VisitEnter( *this, rootAttribute ) )
  517. {
  518. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  519. {
  520. if ( !node->Accept( visitor ) )
  521. break;
  522. }
  523. }
  524. return visitor->VisitExit( *this );
  525. }
  526. // --------- XMLDocument ----------- //
  527. XMLDocument::XMLDocument() :
  528. XMLNode( 0 ),
  529. charBuffer( 0 )
  530. {
  531. document = this; // avoid warning about 'this' in initializer list
  532. }
  533. XMLDocument::~XMLDocument()
  534. {
  535. ClearChildren();
  536. delete [] charBuffer;
  537. #if 1
  538. textPool.Trace( "text" );
  539. elementPool.Trace( "element" );
  540. commentPool.Trace( "comment" );
  541. attributePool.Trace( "attribute" );
  542. #endif
  543. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  544. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  545. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  546. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  547. }
  548. void XMLDocument::InitDocument()
  549. {
  550. errorID = NO_ERROR;
  551. errorStr1 = 0;
  552. errorStr2 = 0;
  553. delete [] charBuffer;
  554. charBuffer = 0;
  555. }
  556. XMLElement* XMLDocument::NewElement( const char* name )
  557. {
  558. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  559. ele->memPool = &elementPool;
  560. ele->SetName( name );
  561. return ele;
  562. }
  563. int XMLDocument::Parse( const char* p )
  564. {
  565. ClearChildren();
  566. InitDocument();
  567. if ( !p || !*p ) {
  568. return true; // correctly parse an empty string?
  569. }
  570. size_t len = strlen( p );
  571. charBuffer = new char[ len+1 ];
  572. memcpy( charBuffer, p, len+1 );
  573. XMLNode* node = 0;
  574. char* q = ParseDeep( charBuffer );
  575. return errorID;
  576. }
  577. void XMLDocument::Print( XMLStreamer* streamer )
  578. {
  579. XMLStreamer stdStreamer( stdout );
  580. if ( !streamer )
  581. streamer = &stdStreamer;
  582. //for( XMLNode* node = firstChild; node; node=node->next ) {
  583. // node->Print( streamer );
  584. //}
  585. Accept( streamer );
  586. }
  587. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  588. {
  589. errorID = error;
  590. printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 ); // fixme: remove
  591. errorStr1 = str1;
  592. errorStr2 = str2;
  593. }
  594. /*
  595. StringStack::StringStack()
  596. {
  597. nPositive = 0;
  598. mem.Push( 0 ); // start with null. makes later code simpler.
  599. }
  600. StringStack::~StringStack()
  601. {
  602. }
  603. void StringStack::Push( const char* str ) {
  604. int needed = strlen( str ) + 1;
  605. char* p = mem.PushArr( needed );
  606. strcpy( p, str );
  607. if ( needed > 1 )
  608. nPositive++;
  609. }
  610. const char* StringStack::Pop() {
  611. TIXMLASSERT( mem.Size() > 1 );
  612. const char* p = mem.Mem() + mem.Size() - 2; // end of final string.
  613. if ( *p ) {
  614. nPositive--;
  615. }
  616. while( *p ) { // stack starts with a null, don't need to check for 'mem'
  617. TIXMLASSERT( p > mem.Mem() );
  618. --p;
  619. }
  620. mem.PopArr( strlen(p)+1 );
  621. return p+1;
  622. }
  623. */
  624. XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false ), textDepth( -1 )
  625. {
  626. for( int i=0; i<ENTITY_RANGE; ++i ) {
  627. entityFlag[i] = false;
  628. }
  629. for( int i=0; i<NUM_ENTITIES; ++i ) {
  630. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  631. if ( entities[i].value < ENTITY_RANGE ) {
  632. entityFlag[ entities[i].value ] = true;
  633. }
  634. }
  635. }
  636. void XMLStreamer::PrintSpace( int depth )
  637. {
  638. for( int i=0; i<depth; ++i ) {
  639. fprintf( fp, " " );
  640. }
  641. }
  642. void XMLStreamer::PrintString( const char* p )
  643. {
  644. // Look for runs of bytes between entities to print.
  645. const char* q = p;
  646. while ( *q ) {
  647. if ( *q < ENTITY_RANGE ) {
  648. // Check for entities. If one is found, flush
  649. // the stream up until the entity, write the
  650. // entity, and keep looking.
  651. if ( entityFlag[*q] ) {
  652. while ( p < q ) {
  653. fputc( *p, fp );
  654. ++p;
  655. }
  656. for( int i=0; i<NUM_ENTITIES; ++i ) {
  657. if ( entities[i].value == *q ) {
  658. fprintf( fp, "&%s;", entities[i].pattern );
  659. break;
  660. }
  661. }
  662. ++p;
  663. }
  664. }
  665. ++q;
  666. }
  667. // Flush the remaining string. This will be the entire
  668. // string if an entity wasn't found.
  669. if ( q-p > 0 ) {
  670. fprintf( fp, "%s", p );
  671. }
  672. }
  673. void XMLStreamer::OpenElement( const char* name )
  674. {
  675. if ( elementJustOpened ) {
  676. SealElement();
  677. }
  678. stack.Push( name );
  679. if ( textDepth < 0 && depth > 0) {
  680. fprintf( fp, "\n" );
  681. PrintSpace( depth );
  682. }
  683. fprintf( fp, "<%s", name );
  684. elementJustOpened = true;
  685. ++depth;
  686. }
  687. void XMLStreamer::PushAttribute( const char* name, const char* value )
  688. {
  689. TIXMLASSERT( elementJustOpened );
  690. fprintf( fp, " %s=\"", name );
  691. PrintString( value );
  692. fprintf( fp, "\"" );
  693. }
  694. void XMLStreamer::CloseElement()
  695. {
  696. --depth;
  697. const char* name = stack.Pop();
  698. if ( elementJustOpened ) {
  699. fprintf( fp, "/>" );
  700. }
  701. else {
  702. if ( textDepth < 0 ) {
  703. fprintf( fp, "\n" );
  704. PrintSpace( depth );
  705. }
  706. fprintf( fp, "</%s>", name );
  707. }
  708. if ( textDepth == depth )
  709. textDepth = -1;
  710. if ( depth == 0 )
  711. fprintf( fp, "\n" );
  712. elementJustOpened = false;
  713. }
  714. void XMLStreamer::SealElement()
  715. {
  716. elementJustOpened = false;
  717. fprintf( fp, ">" );
  718. }
  719. void XMLStreamer::PushText( const char* text, bool cdata )
  720. {
  721. textDepth = depth-1;
  722. if ( elementJustOpened ) {
  723. SealElement();
  724. }
  725. if ( cdata )
  726. fprintf( fp, "<![CDATA[" );
  727. PrintString( text );
  728. if ( cdata )
  729. fprintf( fp, "]]>" );
  730. }
  731. void XMLStreamer::PushComment( const char* comment )
  732. {
  733. if ( elementJustOpened ) {
  734. SealElement();
  735. }
  736. PrintSpace( depth );
  737. fprintf( fp, "<!--%s-->\n", comment );
  738. }
  739. bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  740. {
  741. OpenElement( element.Name() );
  742. while ( attribute ) {
  743. PushAttribute( attribute->Name(), attribute->Value() );
  744. attribute = attribute->Next();
  745. }
  746. return true;
  747. }
  748. bool XMLStreamer::VisitExit( const XMLElement& element )
  749. {
  750. CloseElement();
  751. return true;
  752. }
  753. bool XMLStreamer::Visit( const XMLText& text )
  754. {
  755. PushText( text.Value() );
  756. return true;
  757. }
  758. bool XMLStreamer::Visit( const XMLComment& comment )
  759. {
  760. PushComment( comment.Value() );
  761. return true;
  762. }