tinyxml2.cpp 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. #include <stdarg.h>
  8. //#pragma warning ( disable : 4291 )
  9. using namespace tinyxml2;
  10. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  11. static const char LF = LINE_FEED;
  12. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  13. static const char CR = CARRIAGE_RETURN;
  14. static const char SINGLE_QUOTE = '\'';
  15. static const char DOUBLE_QUOTE = '\"';
  16. #define DELETE_NODE( node ) { MemPool* pool = node->memPool; node->~XMLNode(); pool->Free( node ); }
  17. #define DELETE_ATTRIBUTE( attrib ) { MemPool* pool = attrib->memPool; attrib->~XMLAttribute(); pool->Free( attrib ); }
  18. struct Entity {
  19. const char* pattern;
  20. int length;
  21. char value;
  22. };
  23. static const int NUM_ENTITIES = 5;
  24. static const Entity entities[NUM_ENTITIES] =
  25. {
  26. { "quot", 4, DOUBLE_QUOTE },
  27. { "amp", 3, '&' },
  28. { "apos", 4, SINGLE_QUOTE },
  29. { "lt", 2, '<' },
  30. { "gt", 2, '>' }
  31. };
  32. StrPair::~StrPair()
  33. {
  34. Reset();
  35. }
  36. void StrPair::Reset()
  37. {
  38. if ( flags & NEEDS_DELETE ) {
  39. delete [] start;
  40. }
  41. flags = 0;
  42. start = 0;
  43. end = 0;
  44. }
  45. void StrPair::SetStr( const char* str, int flags )
  46. {
  47. Reset();
  48. size_t len = strlen( str );
  49. start = new char[ len+1 ];
  50. memcpy( start, str, len+1 );
  51. end = start + len;
  52. this->flags = flags | NEEDS_DELETE;
  53. }
  54. char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
  55. {
  56. TIXMLASSERT( endTag && *endTag );
  57. char* start = p; // fixme: hides a member
  58. char endChar = *endTag;
  59. int length = strlen( endTag );
  60. // Inner loop of text parsing.
  61. while ( *p ) {
  62. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  63. Set( start, p, strFlags );
  64. return p + length;
  65. }
  66. ++p;
  67. }
  68. return p;
  69. }
  70. char* StrPair::ParseName( char* p )
  71. {
  72. char* start = p;
  73. start = p;
  74. if ( !start || !(*start) ) {
  75. return 0;
  76. }
  77. if ( !XMLUtil::IsAlpha( *p ) ) {
  78. return 0;
  79. }
  80. while( *p && (
  81. XMLUtil::IsAlphaNum( (unsigned char) *p )
  82. || *p == '_'
  83. || *p == '-'
  84. || *p == '.'
  85. || *p == ':' ))
  86. {
  87. ++p;
  88. }
  89. if ( p > start ) {
  90. Set( start, p, 0 );
  91. return p;
  92. }
  93. return 0;
  94. }
  95. const char* StrPair::GetStr()
  96. {
  97. if ( flags & NEEDS_FLUSH ) {
  98. *end = 0;
  99. flags ^= NEEDS_FLUSH;
  100. if ( flags ) {
  101. char* p = start;
  102. char* q = start;
  103. while( p < end ) {
  104. if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  105. // CR-LF pair becomes LF
  106. // CR alone becomes LF
  107. // LF-CR becomes LF
  108. if ( *(p+1) == LF ) {
  109. p += 2;
  110. }
  111. else {
  112. ++p;
  113. }
  114. *q++ = LF;
  115. }
  116. else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  117. if ( *(p+1) == CR ) {
  118. p += 2;
  119. }
  120. else {
  121. ++p;
  122. }
  123. *q++ = LF;
  124. }
  125. else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  126. int i=0;
  127. for( i=0; i<NUM_ENTITIES; ++i ) {
  128. if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
  129. && *(p+entities[i].length+1) == ';' )
  130. {
  131. // Found an entity convert;
  132. *q = entities[i].value;
  133. ++q;
  134. p += entities[i].length + 2;
  135. break;
  136. }
  137. }
  138. if ( i == NUM_ENTITIES ) {
  139. // fixme: treat as error?
  140. ++p;
  141. ++q;
  142. }
  143. }
  144. else {
  145. *q = *p;
  146. ++p;
  147. ++q;
  148. }
  149. }
  150. *q = 0;
  151. }
  152. flags = (flags & NEEDS_DELETE);
  153. }
  154. return start;
  155. }
  156. // --------- XMLUtil ----------- //
  157. char* XMLDocument::Identify( char* p, XMLNode** node )
  158. {
  159. XMLNode* returnNode = 0;
  160. char* start = p;
  161. p = XMLUtil::SkipWhiteSpace( p );
  162. if( !p || !*p )
  163. {
  164. return 0;
  165. }
  166. // What is this thing?
  167. // - Elements start with a letter or underscore, but xml is reserved.
  168. // - Comments: <!--
  169. // - Decleration: <?xml
  170. // - Everthing else is unknown to tinyxml.
  171. //
  172. static const char* xmlHeader = { "<?xml" };
  173. static const char* commentHeader = { "<!--" };
  174. static const char* dtdHeader = { "<!" };
  175. static const char* cdataHeader = { "<![CDATA[" };
  176. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  177. static const int xmlHeaderLen = 5;
  178. static const int commentHeaderLen = 4;
  179. static const int dtdHeaderLen = 2;
  180. static const int cdataHeaderLen = 9;
  181. static const int elementHeaderLen = 1;
  182. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  183. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  184. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  185. returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
  186. returnNode->memPool = &commentPool;
  187. p += xmlHeaderLen;
  188. }
  189. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  190. returnNode = new (commentPool.Alloc()) XMLComment( this );
  191. returnNode->memPool = &commentPool;
  192. p += commentHeaderLen;
  193. }
  194. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  195. XMLText* text = new (textPool.Alloc()) XMLText( this );
  196. returnNode = text;
  197. returnNode->memPool = &textPool;
  198. p += cdataHeaderLen;
  199. text->SetCData( true );
  200. }
  201. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  202. returnNode = new (commentPool.Alloc()) XMLUnknown( this );
  203. returnNode->memPool = &commentPool;
  204. p += dtdHeaderLen;
  205. }
  206. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  207. returnNode = new (elementPool.Alloc()) XMLElement( this );
  208. returnNode->memPool = &elementPool;
  209. p += elementHeaderLen;
  210. }
  211. else if ( (*p != '<') && XMLUtil::IsAlphaNum( *p ) ) {
  212. returnNode = new (textPool.Alloc()) XMLText( this );
  213. returnNode->memPool = &textPool;
  214. p = start; // Back it up, all the text counts.
  215. }
  216. else {
  217. this->SetError( ERROR_IDENTIFYING_TAG, p, 0 );
  218. p = 0;
  219. returnNode = 0;
  220. }
  221. *node = returnNode;
  222. return p;
  223. }
  224. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  225. {
  226. if ( visitor->VisitEnter( *this ) )
  227. {
  228. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  229. {
  230. if ( !node->Accept( visitor ) )
  231. break;
  232. }
  233. }
  234. return visitor->VisitExit( *this );
  235. }
  236. // --------- XMLNode ----------- //
  237. XMLNode::XMLNode( XMLDocument* doc ) :
  238. document( doc ),
  239. parent( 0 ),
  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::SetValue( const char* str, bool staticMem )
  252. {
  253. if ( staticMem )
  254. value.SetInternedStr( str );
  255. else
  256. value.SetStr( str );
  257. }
  258. void XMLNode::ClearChildren()
  259. {
  260. while( firstChild ) {
  261. XMLNode* node = firstChild;
  262. Unlink( node );
  263. DELETE_NODE( node );
  264. }
  265. firstChild = lastChild = 0;
  266. }
  267. void XMLNode::Unlink( XMLNode* child )
  268. {
  269. TIXMLASSERT( child->parent == this );
  270. if ( child == firstChild )
  271. firstChild = firstChild->next;
  272. if ( child == lastChild )
  273. lastChild = lastChild->prev;
  274. if ( child->prev ) {
  275. child->prev->next = child->next;
  276. }
  277. if ( child->next ) {
  278. child->next->prev = child->prev;
  279. }
  280. child->parent = 0;
  281. }
  282. void XMLNode::DeleteChild( XMLNode* node )
  283. {
  284. TIXMLASSERT( node->parent == this );
  285. DELETE_NODE( node );
  286. }
  287. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  288. {
  289. if ( lastChild ) {
  290. TIXMLASSERT( firstChild );
  291. TIXMLASSERT( lastChild->next == 0 );
  292. lastChild->next = addThis;
  293. addThis->prev = lastChild;
  294. lastChild = addThis;
  295. addThis->next = 0;
  296. }
  297. else {
  298. TIXMLASSERT( firstChild == 0 );
  299. firstChild = lastChild = addThis;
  300. addThis->prev = 0;
  301. addThis->next = 0;
  302. }
  303. addThis->parent = this;
  304. return addThis;
  305. }
  306. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  307. {
  308. if ( firstChild ) {
  309. TIXMLASSERT( lastChild );
  310. TIXMLASSERT( firstChild->prev == 0 );
  311. firstChild->prev = addThis;
  312. addThis->next = firstChild;
  313. firstChild = addThis;
  314. addThis->prev = 0;
  315. }
  316. else {
  317. TIXMLASSERT( lastChild == 0 );
  318. firstChild = lastChild = addThis;
  319. addThis->prev = 0;
  320. addThis->next = 0;
  321. }
  322. addThis->parent = this;
  323. return addThis;
  324. }
  325. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  326. {
  327. TIXMLASSERT( afterThis->parent == this );
  328. if ( afterThis->parent != this )
  329. return 0;
  330. if ( afterThis->next == 0 ) {
  331. // The last node or the only node.
  332. return InsertEndChild( addThis );
  333. }
  334. addThis->prev = afterThis;
  335. addThis->next = afterThis->next;
  336. afterThis->next->prev = addThis;
  337. afterThis->next = addThis;
  338. addThis->parent = this;
  339. return addThis;
  340. }
  341. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  342. {
  343. for( XMLNode* node=firstChild; node; node=node->next ) {
  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. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  354. {
  355. for( XMLNode* node=lastChild; node; node=node->prev ) {
  356. XMLElement* element = node->ToElement();
  357. if ( element ) {
  358. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  359. return element;
  360. }
  361. }
  362. }
  363. return 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. void XMLAttribute::SetName( const char* n )
  464. {
  465. name.SetStr( n );
  466. }
  467. int XMLAttribute::QueryIntAttribute( int* value ) const
  468. {
  469. if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
  470. return ATTRIBUTE_SUCCESS;
  471. return WRONG_ATTRIBUTE_TYPE;
  472. }
  473. int XMLAttribute::QueryUnsignedAttribute( unsigned int* value ) const
  474. {
  475. if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
  476. return ATTRIBUTE_SUCCESS;
  477. return WRONG_ATTRIBUTE_TYPE;
  478. }
  479. int XMLAttribute::QueryBoolAttribute( bool* value ) const
  480. {
  481. int ival = -1;
  482. QueryIntAttribute( &ival );
  483. if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
  484. *value = true;
  485. return ATTRIBUTE_SUCCESS;
  486. }
  487. else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
  488. *value = false;
  489. return ATTRIBUTE_SUCCESS;
  490. }
  491. return WRONG_ATTRIBUTE_TYPE;
  492. }
  493. int XMLAttribute::QueryDoubleAttribute( double* value ) const
  494. {
  495. if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
  496. return ATTRIBUTE_SUCCESS;
  497. return WRONG_ATTRIBUTE_TYPE;
  498. }
  499. int XMLAttribute::QueryFloatAttribute( float* value ) const
  500. {
  501. if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
  502. return ATTRIBUTE_SUCCESS;
  503. return WRONG_ATTRIBUTE_TYPE;
  504. }
  505. void XMLAttribute::SetAttribute( const char* v )
  506. {
  507. value.SetStr( v );
  508. }
  509. void XMLAttribute::SetAttribute( int v )
  510. {
  511. char buf[BUF_SIZE];
  512. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
  513. value.SetStr( buf );
  514. }
  515. void XMLAttribute::SetAttribute( unsigned v )
  516. {
  517. char buf[BUF_SIZE];
  518. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
  519. value.SetStr( buf );
  520. }
  521. void XMLAttribute::SetAttribute( bool v )
  522. {
  523. char buf[BUF_SIZE];
  524. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
  525. value.SetStr( buf );
  526. }
  527. void XMLAttribute::SetAttribute( double v )
  528. {
  529. char buf[BUF_SIZE];
  530. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  531. value.SetStr( buf );
  532. }
  533. void XMLAttribute::SetAttribute( float v )
  534. {
  535. char buf[BUF_SIZE];
  536. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  537. value.SetStr( buf );
  538. }
  539. // --------- XMLElement ---------- //
  540. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  541. closing( false ),
  542. rootAttribute( 0 )
  543. //lastAttribute( 0 )
  544. {
  545. }
  546. XMLElement::~XMLElement()
  547. {
  548. while( rootAttribute ) {
  549. XMLAttribute* next = rootAttribute->next;
  550. DELETE_ATTRIBUTE( rootAttribute );
  551. rootAttribute = next;
  552. }
  553. }
  554. XMLAttribute* XMLElement::FindAttribute( const char* name )
  555. {
  556. XMLAttribute* a = 0;
  557. for( a=rootAttribute; a; a = a->next ) {
  558. if ( XMLUtil::StringEqual( a->Name(), name ) )
  559. return a;
  560. }
  561. return 0;
  562. }
  563. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  564. {
  565. XMLAttribute* a = 0;
  566. for( a=rootAttribute; a; a = a->next ) {
  567. if ( XMLUtil::StringEqual( a->Name(), name ) )
  568. return a;
  569. }
  570. return 0;
  571. }
  572. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  573. {
  574. XMLAttribute* attrib = FindAttribute( name );
  575. if ( !attrib ) {
  576. attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  577. attrib->memPool = &document->attributePool;
  578. LinkAttribute( attrib );
  579. attrib->SetName( name );
  580. }
  581. return attrib;
  582. }
  583. void XMLElement::LinkAttribute( XMLAttribute* attrib )
  584. {
  585. if ( rootAttribute ) {
  586. XMLAttribute* end = rootAttribute;
  587. while ( end->next )
  588. end = end->next;
  589. end->next = attrib;
  590. }
  591. else {
  592. rootAttribute = attrib;
  593. }
  594. }
  595. void XMLElement::DeleteAttribute( const char* name )
  596. {
  597. XMLAttribute* prev = 0;
  598. for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
  599. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  600. if ( prev ) {
  601. prev->next = a->next;
  602. }
  603. else {
  604. rootAttribute = a->next;
  605. }
  606. DELETE_ATTRIBUTE( a );
  607. break;
  608. }
  609. prev = a;
  610. }
  611. }
  612. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  613. {
  614. const char* start = p;
  615. *closedElement = false;
  616. // Read the attributes.
  617. while( p ) {
  618. p = XMLUtil::SkipWhiteSpace( p );
  619. if ( !p || !(*p) ) {
  620. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
  621. return 0;
  622. }
  623. // attribute.
  624. if ( XMLUtil::IsAlpha( *p ) ) {
  625. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  626. attrib->memPool = &document->attributePool;
  627. p = attrib->ParseDeep( p );
  628. if ( !p ) {
  629. DELETE_ATTRIBUTE( attrib );
  630. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  631. return 0;
  632. }
  633. LinkAttribute( attrib );
  634. }
  635. // end of the tag
  636. else if ( *p == '/' && *(p+1) == '>' ) {
  637. if ( closing ) {
  638. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  639. return 0;
  640. }
  641. *closedElement = true;
  642. return p+2; // done; sealed element.
  643. }
  644. // end of the tag
  645. else if ( *p == '>' ) {
  646. ++p;
  647. break;
  648. }
  649. else {
  650. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  651. return 0;
  652. }
  653. }
  654. return p;
  655. }
  656. //
  657. // <ele></ele>
  658. // <ele>foo<b>bar</b></ele>
  659. //
  660. char* XMLElement::ParseDeep( char* p )
  661. {
  662. // Read the element name.
  663. p = XMLUtil::SkipWhiteSpace( p );
  664. if ( !p ) return 0;
  665. const char* start = p;
  666. // The closing element is the </element> form. It is
  667. // parsed just like a regular element then deleted from
  668. // the DOM.
  669. if ( *p == '/' ) {
  670. closing = true;
  671. ++p;
  672. }
  673. p = value.ParseName( p );
  674. if ( value.Empty() ) return 0;
  675. bool elementClosed=false;
  676. p = ParseAttributes( p, &elementClosed );
  677. if ( !p || !*p || elementClosed || closing )
  678. return p;
  679. p = XMLNode::ParseDeep( p );
  680. return p;
  681. }
  682. bool XMLElement::Accept( XMLVisitor* visitor ) const
  683. {
  684. if ( visitor->VisitEnter( *this, rootAttribute ) )
  685. {
  686. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  687. {
  688. if ( !node->Accept( visitor ) )
  689. break;
  690. }
  691. }
  692. return visitor->VisitExit( *this );
  693. }
  694. // --------- XMLDocument ----------- //
  695. XMLDocument::XMLDocument() :
  696. XMLNode( 0 ),
  697. charBuffer( 0 )
  698. {
  699. document = this; // avoid warning about 'this' in initializer list
  700. }
  701. XMLDocument::~XMLDocument()
  702. {
  703. ClearChildren();
  704. delete [] charBuffer;
  705. #if 0
  706. textPool.Trace( "text" );
  707. elementPool.Trace( "element" );
  708. commentPool.Trace( "comment" );
  709. attributePool.Trace( "attribute" );
  710. #endif
  711. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  712. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  713. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  714. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  715. }
  716. void XMLDocument::InitDocument()
  717. {
  718. errorID = NO_ERROR;
  719. errorStr1 = 0;
  720. errorStr2 = 0;
  721. delete [] charBuffer;
  722. charBuffer = 0;
  723. }
  724. XMLElement* XMLDocument::NewElement( const char* name )
  725. {
  726. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  727. ele->memPool = &elementPool;
  728. ele->SetName( name );
  729. return ele;
  730. }
  731. XMLComment* XMLDocument::NewComment( const char* str )
  732. {
  733. XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
  734. comment->memPool = &commentPool;
  735. comment->SetValue( str );
  736. return comment;
  737. }
  738. XMLText* XMLDocument::NewText( const char* str )
  739. {
  740. XMLText* text = new (textPool.Alloc()) XMLText( this );
  741. text->memPool = &textPool;
  742. text->SetValue( str );
  743. return text;
  744. }
  745. int XMLDocument::Parse( const char* p )
  746. {
  747. ClearChildren();
  748. InitDocument();
  749. if ( !p || !*p ) {
  750. return true; // correctly parse an empty string?
  751. }
  752. size_t len = strlen( p );
  753. charBuffer = new char[ len+1 ];
  754. memcpy( charBuffer, p, len+1 );
  755. XMLNode* node = 0;
  756. char* q = ParseDeep( charBuffer );
  757. return errorID;
  758. }
  759. void XMLDocument::Print( XMLStreamer* streamer )
  760. {
  761. XMLStreamer stdStreamer( stdout );
  762. if ( !streamer )
  763. streamer = &stdStreamer;
  764. //for( XMLNode* node = firstChild; node; node=node->next ) {
  765. // node->Print( streamer );
  766. //}
  767. Accept( streamer );
  768. }
  769. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  770. {
  771. errorID = error;
  772. printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 ); // fixme: remove
  773. errorStr1 = str1;
  774. errorStr2 = str2;
  775. }
  776. XMLStreamer::XMLStreamer( FILE* file ) : fp( file ), depth( 0 ), elementJustOpened( false ), textDepth( -1 )
  777. {
  778. for( int i=0; i<ENTITY_RANGE; ++i ) {
  779. entityFlag[i] = false;
  780. }
  781. for( int i=0; i<NUM_ENTITIES; ++i ) {
  782. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  783. if ( entities[i].value < ENTITY_RANGE ) {
  784. entityFlag[ entities[i].value ] = true;
  785. }
  786. }
  787. buffer.Push( 0 );
  788. }
  789. void XMLStreamer::Print( const char* format, ... )
  790. {
  791. va_list va;
  792. va_start( va, format );
  793. if ( fp ) {
  794. vfprintf( fp, format, va );
  795. }
  796. else {
  797. // This seems brutally complex. Haven't figured out a better
  798. // way on windows.
  799. #ifdef _MSC_VER
  800. int len = -1;
  801. while ( len < 0 ) {
  802. len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
  803. if ( len < 0 ) {
  804. accumulator.PushArr( 1000 );
  805. }
  806. }
  807. char* p = buffer.PushArr( len ) - 1;
  808. memcpy( p, accumulator.Mem(), len+1 );
  809. #else
  810. int len = vsnprintf( 0, 0, format, va );
  811. char* p = buffer.PushArr( len ) - 1;
  812. vsprintf_s( p, len+1, format, va );
  813. #endif
  814. }
  815. va_end( va );
  816. }
  817. void XMLStreamer::PrintSpace( int depth )
  818. {
  819. for( int i=0; i<depth; ++i ) {
  820. Print( " " );
  821. }
  822. }
  823. void XMLStreamer::PrintString( const char* p )
  824. {
  825. // Look for runs of bytes between entities to print.
  826. const char* q = p;
  827. while ( *q ) {
  828. if ( *q < ENTITY_RANGE ) {
  829. // Check for entities. If one is found, flush
  830. // the stream up until the entity, write the
  831. // entity, and keep looking.
  832. if ( entityFlag[*q] ) {
  833. while ( p < q ) {
  834. Print( "%c", *p );
  835. ++p;
  836. }
  837. for( int i=0; i<NUM_ENTITIES; ++i ) {
  838. if ( entities[i].value == *q ) {
  839. Print( "&%s;", entities[i].pattern );
  840. break;
  841. }
  842. }
  843. ++p;
  844. }
  845. }
  846. ++q;
  847. }
  848. // Flush the remaining string. This will be the entire
  849. // string if an entity wasn't found.
  850. if ( q-p > 0 ) {
  851. Print( "%s", p );
  852. }
  853. }
  854. void XMLStreamer::OpenElement( const char* name )
  855. {
  856. if ( elementJustOpened ) {
  857. SealElement();
  858. }
  859. stack.Push( name );
  860. if ( textDepth < 0 && depth > 0) {
  861. Print( "\n" );
  862. PrintSpace( depth );
  863. }
  864. Print( "<%s", name );
  865. elementJustOpened = true;
  866. ++depth;
  867. }
  868. void XMLStreamer::PushAttribute( const char* name, const char* value )
  869. {
  870. TIXMLASSERT( elementJustOpened );
  871. Print( " %s=\"", name );
  872. PrintString( value );
  873. Print( "\"" );
  874. }
  875. void XMLStreamer::CloseElement()
  876. {
  877. --depth;
  878. const char* name = stack.Pop();
  879. if ( elementJustOpened ) {
  880. Print( "/>" );
  881. }
  882. else {
  883. if ( textDepth < 0 ) {
  884. Print( "\n" );
  885. PrintSpace( depth );
  886. }
  887. Print( "</%s>", name );
  888. }
  889. if ( textDepth == depth )
  890. textDepth = -1;
  891. if ( depth == 0 )
  892. Print( "\n" );
  893. elementJustOpened = false;
  894. }
  895. void XMLStreamer::SealElement()
  896. {
  897. elementJustOpened = false;
  898. Print( ">" );
  899. }
  900. void XMLStreamer::PushText( const char* text, bool cdata )
  901. {
  902. textDepth = depth-1;
  903. if ( elementJustOpened ) {
  904. SealElement();
  905. }
  906. if ( cdata )
  907. Print( "<![CDATA[" );
  908. PrintString( text );
  909. if ( cdata )
  910. Print( "]]>" );
  911. }
  912. void XMLStreamer::PushComment( const char* comment )
  913. {
  914. if ( elementJustOpened ) {
  915. SealElement();
  916. }
  917. if ( textDepth < 0 && depth > 0) {
  918. Print( "\n" );
  919. PrintSpace( depth );
  920. }
  921. Print( "<!--%s-->", comment );
  922. }
  923. bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  924. {
  925. OpenElement( element.Name() );
  926. while ( attribute ) {
  927. PushAttribute( attribute->Name(), attribute->Value() );
  928. attribute = attribute->Next();
  929. }
  930. return true;
  931. }
  932. bool XMLStreamer::VisitExit( const XMLElement& element )
  933. {
  934. CloseElement();
  935. return true;
  936. }
  937. bool XMLStreamer::Visit( const XMLText& text )
  938. {
  939. PushText( text.Value() );
  940. return true;
  941. }
  942. bool XMLStreamer::Visit( const XMLComment& comment )
  943. {
  944. PushComment( comment.Value() );
  945. return true;
  946. }