1
0

tinyxml2.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. // --------- XMLBase ----------- //
  115. char* XMLBase::ParseText( char* p, StrPair* pair, 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. pair->Set( start, p, strFlags );
  125. return p + length;
  126. }
  127. ++p;
  128. }
  129. return p;
  130. }
  131. char* XMLBase::ParseName( char* p, StrPair* pair )
  132. {
  133. char* start = p;
  134. start = p;
  135. if ( !start || !(*start) ) {
  136. return 0;
  137. }
  138. if ( !IsAlpha( *p ) ) {
  139. return 0;
  140. }
  141. while( *p && (
  142. IsAlphaNum( (unsigned char) *p )
  143. || *p == '_'
  144. || *p == '-'
  145. || *p == '.'
  146. || *p == ':' ))
  147. {
  148. ++p;
  149. }
  150. if ( p > start ) {
  151. pair->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 = XMLBase::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 ( XMLBase::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  182. returnNode = new (commentPool.Alloc()) XMLComment( this );
  183. returnNode->memPool = &commentPool;
  184. p += commentHeaderLen;
  185. }
  186. else if ( XMLBase::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 != '<') && XMLBase::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. // --------- XMLNode ----------- //
  204. XMLNode::XMLNode( XMLDocument* doc ) :
  205. document( doc ),
  206. parent( 0 ),
  207. isTextParent( false ),
  208. firstChild( 0 ), lastChild( 0 ),
  209. prev( 0 ), next( 0 )
  210. {
  211. }
  212. XMLNode::~XMLNode()
  213. {
  214. ClearChildren();
  215. if ( parent ) {
  216. parent->Unlink( this );
  217. }
  218. }
  219. void XMLNode::ClearChildren()
  220. {
  221. while( firstChild ) {
  222. XMLNode* node = firstChild;
  223. Unlink( node );
  224. DELETE_NODE( node );
  225. }
  226. firstChild = lastChild = 0;
  227. }
  228. void XMLNode::Unlink( XMLNode* child )
  229. {
  230. TIXMLASSERT( child->parent == this );
  231. if ( child == firstChild )
  232. firstChild = firstChild->next;
  233. if ( child == lastChild )
  234. lastChild = lastChild->prev;
  235. if ( child->prev ) {
  236. child->prev->next = child->next;
  237. }
  238. if ( child->next ) {
  239. child->next->prev = child->prev;
  240. }
  241. child->parent = 0;
  242. }
  243. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  244. {
  245. if ( lastChild ) {
  246. TIXMLASSERT( firstChild );
  247. TIXMLASSERT( lastChild->next == 0 );
  248. lastChild->next = addThis;
  249. addThis->prev = lastChild;
  250. lastChild = addThis;
  251. addThis->parent = this;
  252. addThis->next = 0;
  253. }
  254. else {
  255. TIXMLASSERT( firstChild == 0 );
  256. firstChild = lastChild = addThis;
  257. addThis->parent = this;
  258. addThis->prev = 0;
  259. addThis->next = 0;
  260. }
  261. if ( addThis->ToText() ) {
  262. SetTextParent();
  263. }
  264. return addThis;
  265. }
  266. XMLElement* XMLNode::FirstChildElement( const char* value )
  267. {
  268. for( XMLNode* node=firstChild; node; node=node->next ) {
  269. XMLElement* element = node->ToElement();
  270. if ( element ) {
  271. if ( !value || XMLBase::StringEqual( element->Name(), value ) ) {
  272. return element;
  273. }
  274. }
  275. }
  276. return 0;
  277. }
  278. void XMLNode::Print( XMLStreamer* streamer )
  279. {
  280. for( XMLNode* node = firstChild; node; node=node->next ) {
  281. node->Print( streamer );
  282. }
  283. }
  284. char* XMLNode::ParseDeep( char* p )
  285. {
  286. while( p && *p ) {
  287. XMLNode* node = 0;
  288. p = document->Identify( p, &node );
  289. if ( p && node ) {
  290. p = node->ParseDeep( p );
  291. // FIXME: is it the correct closing element?
  292. if ( node->IsClosingElement() ) {
  293. DELETE_NODE( node );
  294. return p;
  295. }
  296. this->InsertEndChild( node );
  297. }
  298. }
  299. return 0;
  300. }
  301. // --------- XMLText ---------- //
  302. char* XMLText::ParseDeep( char* p )
  303. {
  304. p = XMLBase::ParseText( p, &value, "<", StrPair::TEXT_ELEMENT );
  305. // consumes the end tag.
  306. if ( p && *p ) {
  307. return p-1;
  308. }
  309. return 0;
  310. }
  311. void XMLText::Print( XMLStreamer* streamer )
  312. {
  313. const char* v = value.GetStr();
  314. streamer->PushText( v );
  315. }
  316. // --------- XMLComment ---------- //
  317. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  318. {
  319. }
  320. XMLComment::~XMLComment()
  321. {
  322. //printf( "~XMLComment\n" );
  323. }
  324. void XMLComment::Print( XMLStreamer* streamer )
  325. {
  326. // XMLNode::Print( fp, depth );
  327. // fprintf( fp, "<!--%s-->\n", value.GetStr() );
  328. streamer->PushComment( value.GetStr() );
  329. }
  330. char* XMLComment::ParseDeep( char* p )
  331. {
  332. // Comment parses as text.
  333. return XMLBase::ParseText( p, &value, "-->", StrPair::COMMENT );
  334. }
  335. // --------- XMLAttribute ---------- //
  336. char* XMLAttribute::ParseDeep( char* p )
  337. {
  338. p = XMLBase::ParseText( p, &name, "=", StrPair::ATTRIBUTE_NAME );
  339. if ( !p || !*p ) return 0;
  340. char endTag[2] = { *p, 0 };
  341. ++p;
  342. p = XMLBase::ParseText( p, &value, endTag, StrPair::ATTRIBUTE_VALUE );
  343. if ( value.Empty() ) return 0;
  344. return p;
  345. }
  346. void XMLAttribute::Print( XMLStreamer* streamer )
  347. {
  348. // fixme: sort out single vs. double quote
  349. //fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
  350. streamer->PushAttribute( name.GetStr(), value.GetStr() );
  351. }
  352. // --------- XMLElement ---------- //
  353. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  354. closing( false ),
  355. rootAttribute( 0 ),
  356. lastAttribute( 0 )
  357. {
  358. }
  359. XMLElement::~XMLElement()
  360. {
  361. //printf( "~XMLElemen %x\n",this );
  362. XMLAttribute* attribute = rootAttribute;
  363. while( attribute ) {
  364. XMLAttribute* next = attribute->next;
  365. DELETE_ATTRIBUTE( attribute );
  366. attribute = next;
  367. }
  368. }
  369. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  370. {
  371. const char* start = p;
  372. *closedElement = false;
  373. // Read the attributes.
  374. while( p ) {
  375. p = XMLBase::SkipWhiteSpace( p );
  376. if ( !p || !(*p) ) {
  377. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
  378. return 0;
  379. }
  380. // attribute.
  381. if ( XMLBase::IsAlpha( *p ) ) {
  382. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  383. attrib->memPool = &document->attributePool;
  384. p = attrib->ParseDeep( p );
  385. if ( !p ) {
  386. DELETE_ATTRIBUTE( attrib );
  387. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  388. return 0;
  389. }
  390. if ( rootAttribute ) {
  391. TIXMLASSERT( lastAttribute );
  392. lastAttribute->next = attrib;
  393. lastAttribute = attrib;
  394. }
  395. else {
  396. rootAttribute = lastAttribute = attrib;
  397. }
  398. }
  399. // end of the tag
  400. else if ( *p == '/' && *(p+1) == '>' ) {
  401. if ( closing ) {
  402. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  403. return 0;
  404. }
  405. *closedElement = true;
  406. return p+2; // done; sealed element.
  407. }
  408. // end of the tag
  409. else if ( *p == '>' ) {
  410. ++p;
  411. break;
  412. }
  413. else {
  414. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  415. return 0;
  416. }
  417. }
  418. return p;
  419. }
  420. //
  421. // <ele></ele>
  422. // <ele>foo<b>bar</b></ele>
  423. //
  424. char* XMLElement::ParseDeep( char* p )
  425. {
  426. // Read the element name.
  427. p = XMLBase::SkipWhiteSpace( p );
  428. if ( !p ) return 0;
  429. const char* start = p;
  430. // The closing element is the </element> form. It is
  431. // parsed just like a regular element then deleted from
  432. // the DOM.
  433. if ( *p == '/' ) {
  434. closing = true;
  435. ++p;
  436. }
  437. p = XMLBase::ParseName( p, &value );
  438. if ( value.Empty() ) return 0;
  439. bool elementClosed=false;
  440. p = ParseAttributes( p, &elementClosed );
  441. if ( !p || !*p || elementClosed || closing )
  442. return p;
  443. p = XMLNode::ParseDeep( p );
  444. return p;
  445. }
  446. void XMLElement::Print( XMLStreamer* streamer )
  447. {
  448. //if ( !parent || !parent->IsTextParent() ) {
  449. // PrintSpace( cfile, depth );
  450. //}
  451. //fprintf( cfile, "<%s", Name() );
  452. streamer->OpenElement( Name(), IsTextParent() );
  453. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  454. //fprintf( cfile, " " );
  455. attrib->Print( streamer );
  456. }
  457. for( XMLNode* node=firstChild; node; node=node->next ) {
  458. node->Print( streamer );
  459. }
  460. streamer->CloseElement();
  461. }
  462. // --------- XMLDocument ----------- //
  463. XMLDocument::XMLDocument() :
  464. XMLNode( 0 ),
  465. charBuffer( 0 )
  466. {
  467. document = this; // avoid warning about 'this' in initializer list
  468. }
  469. XMLDocument::~XMLDocument()
  470. {
  471. ClearChildren();
  472. delete [] charBuffer;
  473. /*
  474. textPool.Trace( "text" );
  475. elementPool.Trace( "element" );
  476. commentPool.Trace( "comment" );
  477. attributePool.Trace( "attribute" );
  478. */
  479. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  480. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  481. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  482. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  483. }
  484. void XMLDocument::InitDocument()
  485. {
  486. errorID = NO_ERROR;
  487. errorStr1 = 0;
  488. errorStr2 = 0;
  489. delete [] charBuffer;
  490. charBuffer = 0;
  491. }
  492. XMLElement* XMLDocument::NewElement( const char* name )
  493. {
  494. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  495. ele->memPool = &elementPool;
  496. ele->SetName( name );
  497. return ele;
  498. }
  499. int XMLDocument::Parse( const char* p )
  500. {
  501. ClearChildren();
  502. InitDocument();
  503. if ( !p || !*p ) {
  504. return true; // correctly parse an empty string?
  505. }
  506. size_t len = strlen( p );
  507. charBuffer = new char[ len+1 ];
  508. memcpy( charBuffer, p, len+1 );
  509. XMLNode* node = 0;
  510. char* q = ParseDeep( charBuffer );
  511. return errorID;
  512. }
  513. void XMLDocument::Print( XMLStreamer* streamer )
  514. {
  515. XMLStreamer stdStreamer( stdout );
  516. if ( !streamer )
  517. streamer = &stdStreamer;
  518. for( XMLNode* node = firstChild; node; node=node->next ) {
  519. node->Print( streamer );
  520. }
  521. }
  522. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  523. {
  524. errorID = error;
  525. printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 ); // fixme: remove
  526. errorStr1 = str1;
  527. errorStr2 = str2;
  528. }
  529. /*
  530. StringStack::StringStack()
  531. {
  532. nPositive = 0;
  533. mem.Push( 0 ); // start with null. makes later code simpler.
  534. }
  535. StringStack::~StringStack()
  536. {
  537. }
  538. void StringStack::Push( const char* str ) {
  539. int needed = strlen( str ) + 1;
  540. char* p = mem.PushArr( needed );
  541. strcpy( p, str );
  542. if ( needed > 1 )
  543. nPositive++;
  544. }
  545. const char* StringStack::Pop() {
  546. TIXMLASSERT( mem.Size() > 1 );
  547. const char* p = mem.Mem() + mem.Size() - 2; // end of final string.
  548. if ( *p ) {
  549. nPositive--;
  550. }
  551. while( *p ) { // stack starts with a null, don't need to check for 'mem'
  552. TIXMLASSERT( p > mem.Mem() );
  553. --p;
  554. }
  555. mem.PopArr( strlen(p)+1 );
  556. return p+1;
  557. }
  558. */
  559. XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false )
  560. {
  561. for( int i=0; i<ENTITY_RANGE; ++i ) {
  562. entityFlag[i] = false;
  563. }
  564. for( int i=0; i<NUM_ENTITIES; ++i ) {
  565. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  566. if ( entities[i].value < ENTITY_RANGE ) {
  567. entityFlag[ entities[i].value ] = true;
  568. }
  569. }
  570. }
  571. void XMLStreamer::PrintSpace( int depth )
  572. {
  573. for( int i=0; i<depth; ++i ) {
  574. fprintf( fp, " " );
  575. }
  576. }
  577. void XMLStreamer::PrintString( const char* p )
  578. {
  579. // Look for runs of bytes between entities to print.
  580. const char* q = p;
  581. while ( *q ) {
  582. if ( *q < ENTITY_RANGE ) {
  583. // Check for entities. If one is found, flush
  584. // the stream up until the entity, write the
  585. // entity, and keep looking.
  586. if ( entityFlag[*q] ) {
  587. while ( p < q ) {
  588. fputc( *p, fp );
  589. ++p;
  590. }
  591. for( int i=0; i<NUM_ENTITIES; ++i ) {
  592. if ( entities[i].value == *q ) {
  593. fprintf( fp, "&%s;", entities[i].pattern );
  594. break;
  595. }
  596. }
  597. ++p;
  598. }
  599. }
  600. ++q;
  601. }
  602. // Flush the remaining string. This will be the entire
  603. // string if an entity wasn't found.
  604. if ( q-p > 0 ) {
  605. fprintf( fp, "%s", p );
  606. }
  607. }
  608. void XMLStreamer::OpenElement( const char* name, bool textParent )
  609. {
  610. if ( elementJustOpened ) {
  611. SealElement();
  612. }
  613. if ( !TextOnStack() ) {
  614. PrintSpace( depth );
  615. }
  616. stack.Push( name );
  617. text.Push( textParent ? 'T' : 'e' );
  618. // fixme: can names have entities?
  619. fprintf( fp, "<%s", name );
  620. elementJustOpened = true;
  621. ++depth;
  622. }
  623. void XMLStreamer::PushAttribute( const char* name, const char* value )
  624. {
  625. TIXMLASSERT( elementJustOpened );
  626. fprintf( fp, " %s=\"", name );
  627. PrintString( value );
  628. fprintf( fp, "\"" );
  629. }
  630. void XMLStreamer::CloseElement()
  631. {
  632. --depth;
  633. const char* name = stack.Pop();
  634. bool wasText = TextOnStack();
  635. text.Pop();
  636. if ( elementJustOpened ) {
  637. fprintf( fp, "/>" );
  638. if ( !wasText ) {
  639. fprintf( fp, "\n" );
  640. }
  641. }
  642. else {
  643. if ( !wasText ) {
  644. PrintSpace( depth );
  645. }
  646. // fixme can names have entities?
  647. fprintf( fp, "</%s>", name );
  648. if ( !TextOnStack() ) {
  649. fprintf( fp, "\n" );
  650. }
  651. }
  652. elementJustOpened = false;
  653. }
  654. void XMLStreamer::SealElement()
  655. {
  656. elementJustOpened = false;
  657. fprintf( fp, ">" );
  658. if ( !TextOnStack() ) {
  659. fprintf( fp, "\n" );
  660. }
  661. }
  662. void XMLStreamer::PushText( const char* text )
  663. {
  664. if ( elementJustOpened ) {
  665. SealElement();
  666. }
  667. PrintString( text );
  668. }
  669. void XMLStreamer::PushComment( const char* comment )
  670. {
  671. if ( elementJustOpened ) {
  672. SealElement();
  673. }
  674. PrintSpace( depth );
  675. fprintf( fp, "<!--%s-->\n", comment );
  676. }