1
0

tinyxml2.cpp 21 KB

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