tinyxml2.cpp 20 KB

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