tinyxml2.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. StrPair::~StrPair()
  32. {
  33. Reset();
  34. }
  35. void StrPair::Reset()
  36. {
  37. if ( flags & NEEDS_DELETE ) {
  38. delete [] start;
  39. }
  40. flags = 0;
  41. start = 0;
  42. end = 0;
  43. }
  44. void StrPair::SetStr( const char* str, int flags )
  45. {
  46. Reset();
  47. size_t len = strlen( str );
  48. start = new char[ len+1 ];
  49. strncpy( start, str, len );
  50. end = start + len;
  51. this->flags = flags | NEEDS_DELETE;
  52. }
  53. char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
  54. {
  55. TIXMLASSERT( endTag && *endTag );
  56. char* start = p; // fixme: hides a member
  57. char endChar = *endTag;
  58. int length = strlen( endTag );
  59. // Inner loop of text parsing.
  60. while ( *p ) {
  61. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  62. Set( start, p, strFlags );
  63. return p + length;
  64. }
  65. ++p;
  66. }
  67. return p;
  68. }
  69. char* StrPair::ParseName( char* p )
  70. {
  71. char* start = p;
  72. start = p;
  73. if ( !start || !(*start) ) {
  74. return 0;
  75. }
  76. if ( !XMLUtil::IsAlpha( *p ) ) {
  77. return 0;
  78. }
  79. while( *p && (
  80. XMLUtil::IsAlphaNum( (unsigned char) *p )
  81. || *p == '_'
  82. || *p == '-'
  83. || *p == '.'
  84. || *p == ':' ))
  85. {
  86. ++p;
  87. }
  88. if ( p > start ) {
  89. Set( start, p, 0 );
  90. return p;
  91. }
  92. return 0;
  93. }
  94. const char* StrPair::GetStr()
  95. {
  96. if ( flags & NEEDS_FLUSH ) {
  97. *end = 0;
  98. flags ^= NEEDS_FLUSH;
  99. if ( flags ) {
  100. char* p = start;
  101. char* q = start;
  102. while( p < end ) {
  103. if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  104. // CR-LF pair becomes LF
  105. // CR alone becomes LF
  106. // LF-CR becomes LF
  107. if ( *(p+1) == LF ) {
  108. p += 2;
  109. }
  110. else {
  111. ++p;
  112. }
  113. *q++ = LF;
  114. }
  115. else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  116. if ( *(p+1) == CR ) {
  117. p += 2;
  118. }
  119. else {
  120. ++p;
  121. }
  122. *q++ = LF;
  123. }
  124. else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  125. int i=0;
  126. for( i=0; i<NUM_ENTITIES; ++i ) {
  127. if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
  128. && *(p+entities[i].length+1) == ';' )
  129. {
  130. // Found an entity convert;
  131. *q = entities[i].value;
  132. ++q;
  133. p += entities[i].length + 2;
  134. break;
  135. }
  136. }
  137. if ( i == NUM_ENTITIES ) {
  138. // fixme: treat as error?
  139. ++p;
  140. ++q;
  141. }
  142. }
  143. else {
  144. *q = *p;
  145. ++p;
  146. ++q;
  147. }
  148. }
  149. *q = 0;
  150. }
  151. flags = (flags & NEEDS_DELETE);
  152. }
  153. return start;
  154. }
  155. // --------- XMLUtil ----------- //
  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::SetValue( const char* str, bool staticMem )
  251. {
  252. if ( staticMem )
  253. value.SetInternedStr( str );
  254. else
  255. value.SetStr( str );
  256. }
  257. void XMLNode::ClearChildren()
  258. {
  259. while( firstChild ) {
  260. XMLNode* node = firstChild;
  261. Unlink( node );
  262. DELETE_NODE( node );
  263. }
  264. firstChild = lastChild = 0;
  265. }
  266. void XMLNode::Unlink( XMLNode* child )
  267. {
  268. TIXMLASSERT( child->parent == this );
  269. if ( child == firstChild )
  270. firstChild = firstChild->next;
  271. if ( child == lastChild )
  272. lastChild = lastChild->prev;
  273. if ( child->prev ) {
  274. child->prev->next = child->next;
  275. }
  276. if ( child->next ) {
  277. child->next->prev = child->prev;
  278. }
  279. child->parent = 0;
  280. }
  281. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  282. {
  283. if ( lastChild ) {
  284. TIXMLASSERT( firstChild );
  285. TIXMLASSERT( lastChild->next == 0 );
  286. lastChild->next = addThis;
  287. addThis->prev = lastChild;
  288. lastChild = addThis;
  289. addThis->parent = this;
  290. addThis->next = 0;
  291. }
  292. else {
  293. TIXMLASSERT( firstChild == 0 );
  294. firstChild = lastChild = addThis;
  295. addThis->parent = this;
  296. addThis->prev = 0;
  297. addThis->next = 0;
  298. }
  299. return addThis;
  300. }
  301. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  302. {
  303. if ( firstChild ) {
  304. TIXMLASSERT( lastChild );
  305. TIXMLASSERT( firstChild->prev == 0 );
  306. firstChild->prev = addThis;
  307. addThis->next = firstChild;
  308. firstChild = addThis;
  309. addThis->parent = this;
  310. addThis->prev = 0;
  311. }
  312. else {
  313. TIXMLASSERT( lastChild == 0 );
  314. firstChild = lastChild = addThis;
  315. addThis->parent = this;
  316. addThis->prev = 0;
  317. addThis->next = 0;
  318. }
  319. return addThis;
  320. }
  321. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  322. {
  323. TIXMLASSERT( afterThis->parent == this );
  324. if ( afterThis->parent != this )
  325. return 0;
  326. if ( afterThis->next == 0 ) {
  327. // The last node or the only node.
  328. return InsertEndChild( addThis );
  329. }
  330. addThis->prev = afterThis;
  331. addThis->next = afterThis->next;
  332. afterThis->next->prev = addThis;
  333. afterThis->next = addThis;
  334. return addThis;
  335. }
  336. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  337. {
  338. for( XMLNode* node=firstChild; node; node=node->next ) {
  339. XMLElement* element = node->ToElement();
  340. if ( element ) {
  341. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  342. return element;
  343. }
  344. }
  345. }
  346. return 0;
  347. }
  348. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  349. {
  350. for( XMLNode* node=lastChild; node; node=node->prev ) {
  351. XMLElement* element = node->ToElement();
  352. if ( element ) {
  353. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  354. return element;
  355. }
  356. }
  357. }
  358. return 0;
  359. }
  360. void XMLNode::DeleteChild( XMLNode* node )
  361. {
  362. TIXMLASSERT( node->parent == this );
  363. TIXMLASSERT( 0 );
  364. }
  365. char* XMLNode::ParseDeep( char* p )
  366. {
  367. while( p && *p ) {
  368. XMLNode* node = 0;
  369. p = document->Identify( p, &node );
  370. if ( p && node ) {
  371. p = node->ParseDeep( p );
  372. // FIXME: is it the correct closing element?
  373. if ( node->IsClosingElement() ) {
  374. DELETE_NODE( node );
  375. return p;
  376. }
  377. this->InsertEndChild( node );
  378. }
  379. }
  380. return 0;
  381. }
  382. // --------- XMLText ---------- //
  383. char* XMLText::ParseDeep( char* p )
  384. {
  385. if ( this->CData() ) {
  386. p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  387. return p;
  388. }
  389. else {
  390. p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
  391. // consumes the end tag.
  392. if ( p && *p ) {
  393. return p-1;
  394. }
  395. }
  396. return 0;
  397. }
  398. bool XMLText::Accept( XMLVisitor* visitor ) const
  399. {
  400. return visitor->Visit( *this );
  401. }
  402. // --------- XMLComment ---------- //
  403. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  404. {
  405. }
  406. XMLComment::~XMLComment()
  407. {
  408. //printf( "~XMLComment\n" );
  409. }
  410. char* XMLComment::ParseDeep( char* p )
  411. {
  412. // Comment parses as text.
  413. return value.ParseText( p, "-->", StrPair::COMMENT );
  414. }
  415. bool XMLComment::Accept( XMLVisitor* visitor ) const
  416. {
  417. return visitor->Visit( *this );
  418. }
  419. // --------- XMLDeclaration ---------- //
  420. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  421. {
  422. }
  423. XMLDeclaration::~XMLDeclaration()
  424. {
  425. //printf( "~XMLDeclaration\n" );
  426. }
  427. char* XMLDeclaration::ParseDeep( char* p )
  428. {
  429. // Declaration parses as text.
  430. return value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  431. }
  432. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  433. {
  434. return visitor->Visit( *this );
  435. }
  436. // --------- XMLUnknown ---------- //
  437. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  438. {
  439. }
  440. XMLUnknown::~XMLUnknown()
  441. {
  442. }
  443. char* XMLUnknown::ParseDeep( char* p )
  444. {
  445. // Unknown parses as text.
  446. return value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  447. }
  448. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  449. {
  450. return visitor->Visit( *this );
  451. }
  452. // --------- XMLAttribute ---------- //
  453. char* XMLAttribute::ParseDeep( char* p )
  454. {
  455. p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
  456. if ( !p || !*p ) return 0;
  457. char endTag[2] = { *p, 0 };
  458. ++p;
  459. p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
  460. if ( value.Empty() ) return 0;
  461. return p;
  462. }
  463. int XMLAttribute::QueryIntAttribute( int* value ) const
  464. {
  465. if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
  466. return ATTRIBUTE_SUCCESS;
  467. return WRONG_ATTRIBUTE_TYPE;
  468. }
  469. int XMLAttribute::QueryUnsignedAttribute( unsigned int* value ) const
  470. {
  471. if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
  472. return ATTRIBUTE_SUCCESS;
  473. return WRONG_ATTRIBUTE_TYPE;
  474. }
  475. int XMLAttribute::QueryBoolAttribute( bool* value ) const
  476. {
  477. int ival = -1;
  478. QueryIntAttribute( &ival );
  479. if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
  480. *value = true;
  481. return ATTRIBUTE_SUCCESS;
  482. }
  483. else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
  484. *value = false;
  485. return ATTRIBUTE_SUCCESS;
  486. }
  487. return WRONG_ATTRIBUTE_TYPE;
  488. }
  489. int XMLAttribute::QueryDoubleAttribute( double* value ) const
  490. {
  491. if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
  492. return ATTRIBUTE_SUCCESS;
  493. return WRONG_ATTRIBUTE_TYPE;
  494. }
  495. int XMLAttribute::QueryFloatAttribute( float* value ) const
  496. {
  497. if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
  498. return ATTRIBUTE_SUCCESS;
  499. return WRONG_ATTRIBUTE_TYPE;
  500. }
  501. void XMLAttribute::SetAttribute( const char* v )
  502. {
  503. value.SetStr( v );
  504. }
  505. void XMLAttribute::SetAttribute( int v )
  506. {
  507. char buf[BUF_SIZE];
  508. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
  509. value.SetStr( buf );
  510. }
  511. void XMLAttribute::SetAttribute( unsigned v )
  512. {
  513. char buf[BUF_SIZE];
  514. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
  515. value.SetStr( buf );
  516. }
  517. void XMLAttribute::SetAttribute( bool v )
  518. {
  519. char buf[BUF_SIZE];
  520. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
  521. value.SetStr( buf );
  522. }
  523. void XMLAttribute::SetAttribute( double v )
  524. {
  525. char buf[BUF_SIZE];
  526. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  527. value.SetStr( buf );
  528. }
  529. void XMLAttribute::SetAttribute( float v )
  530. {
  531. char buf[BUF_SIZE];
  532. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  533. value.SetStr( buf );
  534. }
  535. // --------- XMLElement ---------- //
  536. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  537. closing( false ),
  538. rootAttribute( 0 ),
  539. lastAttribute( 0 )
  540. {
  541. }
  542. XMLElement::~XMLElement()
  543. {
  544. XMLAttribute* attribute = rootAttribute;
  545. while( attribute ) {
  546. XMLAttribute* next = attribute->next;
  547. DELETE_ATTRIBUTE( attribute );
  548. attribute = next;
  549. }
  550. }
  551. XMLAttribute* XMLElement::FindAttribute( const char* name )
  552. {
  553. XMLAttribute* a = 0;
  554. for( a=rootAttribute; a; a = a->next ) {
  555. if ( XMLUtil::StringEqual( a->Name(), name ) )
  556. return a;
  557. }
  558. return 0;
  559. }
  560. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  561. {
  562. XMLAttribute* a = 0;
  563. for( a=rootAttribute; a; a = a->next ) {
  564. if ( XMLUtil::StringEqual( a->Name(), name ) )
  565. return a;
  566. }
  567. return 0;
  568. }
  569. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  570. {
  571. XMLAttribute* attrib = FindAttribute( name );
  572. if ( !attrib ) {
  573. attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  574. attrib->memPool = &document->attributePool;
  575. }
  576. return attrib;
  577. }
  578. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  579. {
  580. const char* start = p;
  581. *closedElement = false;
  582. // Read the attributes.
  583. while( p ) {
  584. p = XMLUtil::SkipWhiteSpace( p );
  585. if ( !p || !(*p) ) {
  586. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
  587. return 0;
  588. }
  589. // attribute.
  590. if ( XMLUtil::IsAlpha( *p ) ) {
  591. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  592. attrib->memPool = &document->attributePool;
  593. p = attrib->ParseDeep( p );
  594. if ( !p ) {
  595. DELETE_ATTRIBUTE( attrib );
  596. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  597. return 0;
  598. }
  599. if ( rootAttribute ) {
  600. TIXMLASSERT( lastAttribute );
  601. lastAttribute->next = attrib;
  602. lastAttribute = attrib;
  603. }
  604. else {
  605. rootAttribute = lastAttribute = attrib;
  606. }
  607. }
  608. // end of the tag
  609. else if ( *p == '/' && *(p+1) == '>' ) {
  610. if ( closing ) {
  611. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  612. return 0;
  613. }
  614. *closedElement = true;
  615. return p+2; // done; sealed element.
  616. }
  617. // end of the tag
  618. else if ( *p == '>' ) {
  619. ++p;
  620. break;
  621. }
  622. else {
  623. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  624. return 0;
  625. }
  626. }
  627. return p;
  628. }
  629. //
  630. // <ele></ele>
  631. // <ele>foo<b>bar</b></ele>
  632. //
  633. char* XMLElement::ParseDeep( char* p )
  634. {
  635. // Read the element name.
  636. p = XMLUtil::SkipWhiteSpace( p );
  637. if ( !p ) return 0;
  638. const char* start = p;
  639. // The closing element is the </element> form. It is
  640. // parsed just like a regular element then deleted from
  641. // the DOM.
  642. if ( *p == '/' ) {
  643. closing = true;
  644. ++p;
  645. }
  646. p = value.ParseName( p );
  647. if ( value.Empty() ) return 0;
  648. bool elementClosed=false;
  649. p = ParseAttributes( p, &elementClosed );
  650. if ( !p || !*p || elementClosed || closing )
  651. return p;
  652. p = XMLNode::ParseDeep( p );
  653. return p;
  654. }
  655. bool XMLElement::Accept( XMLVisitor* visitor ) const
  656. {
  657. if ( visitor->VisitEnter( *this, rootAttribute ) )
  658. {
  659. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  660. {
  661. if ( !node->Accept( visitor ) )
  662. break;
  663. }
  664. }
  665. return visitor->VisitExit( *this );
  666. }
  667. // --------- XMLDocument ----------- //
  668. XMLDocument::XMLDocument() :
  669. XMLNode( 0 ),
  670. charBuffer( 0 )
  671. {
  672. document = this; // avoid warning about 'this' in initializer list
  673. }
  674. XMLDocument::~XMLDocument()
  675. {
  676. ClearChildren();
  677. delete [] charBuffer;
  678. #if 0
  679. textPool.Trace( "text" );
  680. elementPool.Trace( "element" );
  681. commentPool.Trace( "comment" );
  682. attributePool.Trace( "attribute" );
  683. #endif
  684. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  685. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  686. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  687. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  688. }
  689. void XMLDocument::InitDocument()
  690. {
  691. errorID = NO_ERROR;
  692. errorStr1 = 0;
  693. errorStr2 = 0;
  694. delete [] charBuffer;
  695. charBuffer = 0;
  696. }
  697. XMLElement* XMLDocument::NewElement( const char* name )
  698. {
  699. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  700. ele->memPool = &elementPool;
  701. ele->SetName( name );
  702. return ele;
  703. }
  704. XMLComment* XMLDocument::NewComment( const char* str )
  705. {
  706. XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
  707. comment->memPool = &commentPool;
  708. comment->SetValue( str );
  709. return comment;
  710. }
  711. XMLText* XMLDocument::NewText( const char* str )
  712. {
  713. XMLText* Text = new (textPool.Alloc()) XMLText( this );
  714. Text->memPool = &textPool;
  715. Text->SetValue( str );
  716. return Text;
  717. }
  718. int XMLDocument::Parse( const char* p )
  719. {
  720. ClearChildren();
  721. InitDocument();
  722. if ( !p || !*p ) {
  723. return true; // correctly parse an empty string?
  724. }
  725. size_t len = strlen( p );
  726. charBuffer = new char[ len+1 ];
  727. memcpy( charBuffer, p, len+1 );
  728. XMLNode* node = 0;
  729. char* q = ParseDeep( charBuffer );
  730. return errorID;
  731. }
  732. void XMLDocument::Print( XMLStreamer* streamer )
  733. {
  734. XMLStreamer stdStreamer( stdout );
  735. if ( !streamer )
  736. streamer = &stdStreamer;
  737. //for( XMLNode* node = firstChild; node; node=node->next ) {
  738. // node->Print( streamer );
  739. //}
  740. Accept( streamer );
  741. }
  742. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  743. {
  744. errorID = error;
  745. printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 ); // fixme: remove
  746. errorStr1 = str1;
  747. errorStr2 = str2;
  748. }
  749. /*
  750. StringStack::StringStack()
  751. {
  752. nPositive = 0;
  753. mem.Push( 0 ); // start with null. makes later code simpler.
  754. }
  755. StringStack::~StringStack()
  756. {
  757. }
  758. void StringStack::Push( const char* str ) {
  759. int needed = strlen( str ) + 1;
  760. char* p = mem.PushArr( needed );
  761. strcpy( p, str );
  762. if ( needed > 1 )
  763. nPositive++;
  764. }
  765. const char* StringStack::Pop() {
  766. TIXMLASSERT( mem.Size() > 1 );
  767. const char* p = mem.Mem() + mem.Size() - 2; // end of final string.
  768. if ( *p ) {
  769. nPositive--;
  770. }
  771. while( *p ) { // stack starts with a null, don't need to check for 'mem'
  772. TIXMLASSERT( p > mem.Mem() );
  773. --p;
  774. }
  775. mem.PopArr( strlen(p)+1 );
  776. return p+1;
  777. }
  778. */
  779. XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false ), textDepth( -1 )
  780. {
  781. for( int i=0; i<ENTITY_RANGE; ++i ) {
  782. entityFlag[i] = false;
  783. }
  784. for( int i=0; i<NUM_ENTITIES; ++i ) {
  785. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  786. if ( entities[i].value < ENTITY_RANGE ) {
  787. entityFlag[ entities[i].value ] = true;
  788. }
  789. }
  790. }
  791. void XMLStreamer::PrintSpace( int depth )
  792. {
  793. for( int i=0; i<depth; ++i ) {
  794. fprintf( fp, " " );
  795. }
  796. }
  797. void XMLStreamer::PrintString( const char* p )
  798. {
  799. // Look for runs of bytes between entities to print.
  800. const char* q = p;
  801. while ( *q ) {
  802. if ( *q < ENTITY_RANGE ) {
  803. // Check for entities. If one is found, flush
  804. // the stream up until the entity, write the
  805. // entity, and keep looking.
  806. if ( entityFlag[*q] ) {
  807. while ( p < q ) {
  808. fputc( *p, fp );
  809. ++p;
  810. }
  811. for( int i=0; i<NUM_ENTITIES; ++i ) {
  812. if ( entities[i].value == *q ) {
  813. fprintf( fp, "&%s;", entities[i].pattern );
  814. break;
  815. }
  816. }
  817. ++p;
  818. }
  819. }
  820. ++q;
  821. }
  822. // Flush the remaining string. This will be the entire
  823. // string if an entity wasn't found.
  824. if ( q-p > 0 ) {
  825. fprintf( fp, "%s", p );
  826. }
  827. }
  828. void XMLStreamer::OpenElement( const char* name )
  829. {
  830. if ( elementJustOpened ) {
  831. SealElement();
  832. }
  833. stack.Push( name );
  834. if ( textDepth < 0 && depth > 0) {
  835. fprintf( fp, "\n" );
  836. PrintSpace( depth );
  837. }
  838. fprintf( fp, "<%s", name );
  839. elementJustOpened = true;
  840. ++depth;
  841. }
  842. void XMLStreamer::PushAttribute( const char* name, const char* value )
  843. {
  844. TIXMLASSERT( elementJustOpened );
  845. fprintf( fp, " %s=\"", name );
  846. PrintString( value );
  847. fprintf( fp, "\"" );
  848. }
  849. void XMLStreamer::CloseElement()
  850. {
  851. --depth;
  852. const char* name = stack.Pop();
  853. if ( elementJustOpened ) {
  854. fprintf( fp, "/>" );
  855. }
  856. else {
  857. if ( textDepth < 0 ) {
  858. fprintf( fp, "\n" );
  859. PrintSpace( depth );
  860. }
  861. fprintf( fp, "</%s>", name );
  862. }
  863. if ( textDepth == depth )
  864. textDepth = -1;
  865. if ( depth == 0 )
  866. fprintf( fp, "\n" );
  867. elementJustOpened = false;
  868. }
  869. void XMLStreamer::SealElement()
  870. {
  871. elementJustOpened = false;
  872. fprintf( fp, ">" );
  873. }
  874. void XMLStreamer::PushText( const char* text, bool cdata )
  875. {
  876. textDepth = depth-1;
  877. if ( elementJustOpened ) {
  878. SealElement();
  879. }
  880. if ( cdata )
  881. fprintf( fp, "<![CDATA[" );
  882. PrintString( text );
  883. if ( cdata )
  884. fprintf( fp, "]]>" );
  885. }
  886. void XMLStreamer::PushComment( const char* comment )
  887. {
  888. if ( elementJustOpened ) {
  889. SealElement();
  890. }
  891. PrintSpace( depth );
  892. fprintf( fp, "<!--%s-->\n", comment );
  893. }
  894. bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  895. {
  896. OpenElement( element.Name() );
  897. while ( attribute ) {
  898. PushAttribute( attribute->Name(), attribute->Value() );
  899. attribute = attribute->Next();
  900. }
  901. return true;
  902. }
  903. bool XMLStreamer::VisitExit( const XMLElement& element )
  904. {
  905. CloseElement();
  906. return true;
  907. }
  908. bool XMLStreamer::Visit( const XMLText& text )
  909. {
  910. PushText( text.Value() );
  911. return true;
  912. }
  913. bool XMLStreamer::Visit( const XMLComment& comment )
  914. {
  915. PushComment( comment.Value() );
  916. return true;
  917. }