1
0

tinyxml2.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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 0;
  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: <?
  170. // - Everthing else is unknown to tinyxml.
  171. //
  172. static const char* xmlHeader = { "<?" };
  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 = 2;
  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 {
  212. returnNode = new (textPool.Alloc()) XMLText( this );
  213. returnNode->memPool = &textPool;
  214. p = start; // Back it up, all the text counts.
  215. }
  216. *node = returnNode;
  217. return p;
  218. }
  219. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  220. {
  221. if ( visitor->VisitEnter( *this ) )
  222. {
  223. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  224. {
  225. if ( !node->Accept( visitor ) )
  226. break;
  227. }
  228. }
  229. return visitor->VisitExit( *this );
  230. }
  231. // --------- XMLNode ----------- //
  232. XMLNode::XMLNode( XMLDocument* doc ) :
  233. document( doc ),
  234. parent( 0 ),
  235. firstChild( 0 ), lastChild( 0 ),
  236. prev( 0 ), next( 0 )
  237. {
  238. }
  239. XMLNode::~XMLNode()
  240. {
  241. ClearChildren();
  242. if ( parent ) {
  243. parent->Unlink( this );
  244. }
  245. }
  246. void XMLNode::SetValue( const char* str, bool staticMem )
  247. {
  248. if ( staticMem )
  249. value.SetInternedStr( str );
  250. else
  251. value.SetStr( str );
  252. }
  253. void XMLNode::ClearChildren()
  254. {
  255. while( firstChild ) {
  256. XMLNode* node = firstChild;
  257. Unlink( node );
  258. DELETE_NODE( node );
  259. }
  260. firstChild = lastChild = 0;
  261. }
  262. void XMLNode::Unlink( XMLNode* child )
  263. {
  264. TIXMLASSERT( child->parent == this );
  265. if ( child == firstChild )
  266. firstChild = firstChild->next;
  267. if ( child == lastChild )
  268. lastChild = lastChild->prev;
  269. if ( child->prev ) {
  270. child->prev->next = child->next;
  271. }
  272. if ( child->next ) {
  273. child->next->prev = child->prev;
  274. }
  275. child->parent = 0;
  276. }
  277. void XMLNode::DeleteChild( XMLNode* node )
  278. {
  279. TIXMLASSERT( node->parent == this );
  280. DELETE_NODE( node );
  281. }
  282. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  283. {
  284. if ( lastChild ) {
  285. TIXMLASSERT( firstChild );
  286. TIXMLASSERT( lastChild->next == 0 );
  287. lastChild->next = addThis;
  288. addThis->prev = lastChild;
  289. lastChild = addThis;
  290. addThis->next = 0;
  291. }
  292. else {
  293. TIXMLASSERT( firstChild == 0 );
  294. firstChild = lastChild = addThis;
  295. addThis->prev = 0;
  296. addThis->next = 0;
  297. }
  298. addThis->parent = this;
  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->prev = 0;
  310. }
  311. else {
  312. TIXMLASSERT( lastChild == 0 );
  313. firstChild = lastChild = addThis;
  314. addThis->prev = 0;
  315. addThis->next = 0;
  316. }
  317. addThis->parent = this;
  318. return addThis;
  319. }
  320. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  321. {
  322. TIXMLASSERT( afterThis->parent == this );
  323. if ( afterThis->parent != this )
  324. return 0;
  325. if ( afterThis->next == 0 ) {
  326. // The last node or the only node.
  327. return InsertEndChild( addThis );
  328. }
  329. addThis->prev = afterThis;
  330. addThis->next = afterThis->next;
  331. afterThis->next->prev = addThis;
  332. afterThis->next = addThis;
  333. addThis->parent = this;
  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. char* XMLNode::ParseDeep( char* p )
  361. {
  362. while( p && *p ) {
  363. XMLNode* node = 0;
  364. p = document->Identify( p, &node );
  365. if ( p && node ) {
  366. p = node->ParseDeep( p );
  367. // FIXME: is it the correct closing element?
  368. if ( node->IsClosingElement() ) {
  369. DELETE_NODE( node );
  370. return p;
  371. }
  372. this->InsertEndChild( node );
  373. }
  374. }
  375. return 0;
  376. }
  377. // --------- XMLText ---------- //
  378. char* XMLText::ParseDeep( char* p )
  379. {
  380. const char* start = p;
  381. if ( this->CData() ) {
  382. p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  383. if ( !p ) {
  384. document->SetError( XMLDocument::ERROR_PARSING_CDATA, start, 0 );
  385. }
  386. return p;
  387. }
  388. else {
  389. p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
  390. if ( !p ) {
  391. document->SetError( XMLDocument::ERROR_PARSING_TEXT, start, 0 );
  392. }
  393. if ( p && *p ) {
  394. return p-1;
  395. }
  396. }
  397. return 0;
  398. }
  399. bool XMLText::Accept( XMLVisitor* visitor ) const
  400. {
  401. return visitor->Visit( *this );
  402. }
  403. // --------- XMLComment ---------- //
  404. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  405. {
  406. }
  407. XMLComment::~XMLComment()
  408. {
  409. //printf( "~XMLComment\n" );
  410. }
  411. char* XMLComment::ParseDeep( char* p )
  412. {
  413. // Comment parses as text.
  414. const char* start = p;
  415. p = value.ParseText( p, "-->", StrPair::COMMENT );
  416. if ( p == 0 ) {
  417. document->SetError( XMLDocument::ERROR_PARSING_COMMENT, start, 0 );
  418. }
  419. return p;
  420. }
  421. bool XMLComment::Accept( XMLVisitor* visitor ) const
  422. {
  423. return visitor->Visit( *this );
  424. }
  425. // --------- XMLDeclaration ---------- //
  426. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  427. {
  428. }
  429. XMLDeclaration::~XMLDeclaration()
  430. {
  431. //printf( "~XMLDeclaration\n" );
  432. }
  433. char* XMLDeclaration::ParseDeep( char* p )
  434. {
  435. // Declaration parses as text.
  436. const char* start = p;
  437. p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  438. if ( p == 0 ) {
  439. document->SetError( XMLDocument::ERROR_PARSING_DECLARATION, start, 0 );
  440. }
  441. return p;
  442. }
  443. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  444. {
  445. return visitor->Visit( *this );
  446. }
  447. // --------- XMLUnknown ---------- //
  448. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  449. {
  450. }
  451. XMLUnknown::~XMLUnknown()
  452. {
  453. }
  454. char* XMLUnknown::ParseDeep( char* p )
  455. {
  456. // Unknown parses as text.
  457. const char* start = p;
  458. p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  459. if ( !p ) {
  460. document->SetError( XMLDocument::ERROR_PARSING_UNKNOWN, start, 0 );
  461. }
  462. return p;
  463. }
  464. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  465. {
  466. return visitor->Visit( *this );
  467. }
  468. // --------- XMLAttribute ---------- //
  469. char* XMLAttribute::ParseDeep( char* p )
  470. {
  471. p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
  472. if ( !p || !*p ) return 0;
  473. char endTag[2] = { *p, 0 };
  474. ++p;
  475. p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
  476. if ( value.Empty() ) return 0;
  477. return p;
  478. }
  479. void XMLAttribute::SetName( const char* n )
  480. {
  481. name.SetStr( n );
  482. }
  483. int XMLAttribute::QueryIntAttribute( int* value ) const
  484. {
  485. if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
  486. return ATTRIBUTE_SUCCESS;
  487. return WRONG_ATTRIBUTE_TYPE;
  488. }
  489. int XMLAttribute::QueryUnsignedAttribute( unsigned int* value ) const
  490. {
  491. if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
  492. return ATTRIBUTE_SUCCESS;
  493. return WRONG_ATTRIBUTE_TYPE;
  494. }
  495. int XMLAttribute::QueryBoolAttribute( bool* value ) const
  496. {
  497. int ival = -1;
  498. QueryIntAttribute( &ival );
  499. if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
  500. *value = true;
  501. return ATTRIBUTE_SUCCESS;
  502. }
  503. else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
  504. *value = false;
  505. return ATTRIBUTE_SUCCESS;
  506. }
  507. return WRONG_ATTRIBUTE_TYPE;
  508. }
  509. int XMLAttribute::QueryDoubleAttribute( double* value ) const
  510. {
  511. if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
  512. return ATTRIBUTE_SUCCESS;
  513. return WRONG_ATTRIBUTE_TYPE;
  514. }
  515. int XMLAttribute::QueryFloatAttribute( float* value ) const
  516. {
  517. if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
  518. return ATTRIBUTE_SUCCESS;
  519. return WRONG_ATTRIBUTE_TYPE;
  520. }
  521. void XMLAttribute::SetAttribute( const char* v )
  522. {
  523. value.SetStr( v );
  524. }
  525. void XMLAttribute::SetAttribute( int v )
  526. {
  527. char buf[BUF_SIZE];
  528. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
  529. value.SetStr( buf );
  530. }
  531. void XMLAttribute::SetAttribute( unsigned v )
  532. {
  533. char buf[BUF_SIZE];
  534. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
  535. value.SetStr( buf );
  536. }
  537. void XMLAttribute::SetAttribute( bool v )
  538. {
  539. char buf[BUF_SIZE];
  540. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
  541. value.SetStr( buf );
  542. }
  543. void XMLAttribute::SetAttribute( double v )
  544. {
  545. char buf[BUF_SIZE];
  546. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  547. value.SetStr( buf );
  548. }
  549. void XMLAttribute::SetAttribute( float v )
  550. {
  551. char buf[BUF_SIZE];
  552. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  553. value.SetStr( buf );
  554. }
  555. // --------- XMLElement ---------- //
  556. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  557. closing( false ),
  558. rootAttribute( 0 )
  559. //lastAttribute( 0 )
  560. {
  561. }
  562. XMLElement::~XMLElement()
  563. {
  564. while( rootAttribute ) {
  565. XMLAttribute* next = rootAttribute->next;
  566. DELETE_ATTRIBUTE( rootAttribute );
  567. rootAttribute = next;
  568. }
  569. }
  570. XMLAttribute* XMLElement::FindAttribute( const char* name )
  571. {
  572. XMLAttribute* a = 0;
  573. for( a=rootAttribute; a; a = a->next ) {
  574. if ( XMLUtil::StringEqual( a->Name(), name ) )
  575. return a;
  576. }
  577. return 0;
  578. }
  579. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  580. {
  581. XMLAttribute* a = 0;
  582. for( a=rootAttribute; a; a = a->next ) {
  583. if ( XMLUtil::StringEqual( a->Name(), name ) )
  584. return a;
  585. }
  586. return 0;
  587. }
  588. const char* XMLElement::GetText() const
  589. {
  590. if ( FirstChild() && FirstChild()->ToText() ) {
  591. return FirstChild()->ToText()->Value();
  592. }
  593. return 0;
  594. }
  595. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  596. {
  597. XMLAttribute* attrib = FindAttribute( name );
  598. if ( !attrib ) {
  599. attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  600. attrib->memPool = &document->attributePool;
  601. LinkAttribute( attrib );
  602. attrib->SetName( name );
  603. }
  604. return attrib;
  605. }
  606. void XMLElement::LinkAttribute( XMLAttribute* attrib )
  607. {
  608. if ( rootAttribute ) {
  609. XMLAttribute* end = rootAttribute;
  610. while ( end->next )
  611. end = end->next;
  612. end->next = attrib;
  613. }
  614. else {
  615. rootAttribute = attrib;
  616. }
  617. }
  618. void XMLElement::DeleteAttribute( const char* name )
  619. {
  620. XMLAttribute* prev = 0;
  621. for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
  622. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  623. if ( prev ) {
  624. prev->next = a->next;
  625. }
  626. else {
  627. rootAttribute = a->next;
  628. }
  629. DELETE_ATTRIBUTE( a );
  630. break;
  631. }
  632. prev = a;
  633. }
  634. }
  635. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  636. {
  637. const char* start = p;
  638. *closedElement = false;
  639. // Read the attributes.
  640. while( p ) {
  641. p = XMLUtil::SkipWhiteSpace( p );
  642. if ( !p || !(*p) ) {
  643. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, Name() );
  644. return 0;
  645. }
  646. // attribute.
  647. if ( XMLUtil::IsAlpha( *p ) ) {
  648. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
  649. attrib->memPool = &document->attributePool;
  650. p = attrib->ParseDeep( p );
  651. if ( !p ) {
  652. DELETE_ATTRIBUTE( attrib );
  653. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  654. return 0;
  655. }
  656. LinkAttribute( attrib );
  657. }
  658. // end of the tag
  659. else if ( *p == '/' && *(p+1) == '>' ) {
  660. if ( closing ) {
  661. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  662. return 0;
  663. }
  664. *closedElement = true;
  665. return p+2; // done; sealed element.
  666. }
  667. // end of the tag
  668. else if ( *p == '>' ) {
  669. ++p;
  670. break;
  671. }
  672. else {
  673. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  674. return 0;
  675. }
  676. }
  677. return p;
  678. }
  679. //
  680. // <ele></ele>
  681. // <ele>foo<b>bar</b></ele>
  682. //
  683. char* XMLElement::ParseDeep( char* p )
  684. {
  685. // Read the element name.
  686. p = XMLUtil::SkipWhiteSpace( p );
  687. if ( !p ) return 0;
  688. const char* start = p;
  689. // The closing element is the </element> form. It is
  690. // parsed just like a regular element then deleted from
  691. // the DOM.
  692. if ( *p == '/' ) {
  693. closing = true;
  694. ++p;
  695. }
  696. p = value.ParseName( p );
  697. if ( value.Empty() ) return 0;
  698. bool elementClosed=false;
  699. p = ParseAttributes( p, &elementClosed );
  700. if ( !p || !*p || elementClosed || closing )
  701. return p;
  702. p = XMLNode::ParseDeep( p );
  703. return p;
  704. }
  705. bool XMLElement::Accept( XMLVisitor* visitor ) const
  706. {
  707. if ( visitor->VisitEnter( *this, rootAttribute ) )
  708. {
  709. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  710. {
  711. if ( !node->Accept( visitor ) )
  712. break;
  713. }
  714. }
  715. return visitor->VisitExit( *this );
  716. }
  717. // --------- XMLDocument ----------- //
  718. XMLDocument::XMLDocument() :
  719. XMLNode( 0 ),
  720. charBuffer( 0 )
  721. {
  722. document = this; // avoid warning about 'this' in initializer list
  723. }
  724. XMLDocument::~XMLDocument()
  725. {
  726. ClearChildren();
  727. delete [] charBuffer;
  728. #if 0
  729. textPool.Trace( "text" );
  730. elementPool.Trace( "element" );
  731. commentPool.Trace( "comment" );
  732. attributePool.Trace( "attribute" );
  733. #endif
  734. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  735. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  736. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  737. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  738. }
  739. void XMLDocument::InitDocument()
  740. {
  741. errorID = NO_ERROR;
  742. errorStr1 = 0;
  743. errorStr2 = 0;
  744. delete [] charBuffer;
  745. charBuffer = 0;
  746. }
  747. XMLElement* XMLDocument::NewElement( const char* name )
  748. {
  749. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  750. ele->memPool = &elementPool;
  751. ele->SetName( name );
  752. return ele;
  753. }
  754. XMLComment* XMLDocument::NewComment( const char* str )
  755. {
  756. XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
  757. comment->memPool = &commentPool;
  758. comment->SetValue( str );
  759. return comment;
  760. }
  761. XMLText* XMLDocument::NewText( const char* str )
  762. {
  763. XMLText* text = new (textPool.Alloc()) XMLText( this );
  764. text->memPool = &textPool;
  765. text->SetValue( str );
  766. return text;
  767. }
  768. int XMLDocument::Load( const char* filename )
  769. {
  770. ClearChildren();
  771. InitDocument();
  772. FILE* fp = fopen( filename, "rb" );
  773. if ( !fp ) {
  774. SetError( ERROR_FILE_NOT_FOUND, filename, 0 );
  775. return errorID;
  776. }
  777. Load( fp );
  778. fclose( fp );
  779. return errorID;
  780. }
  781. int XMLDocument::Load( FILE* fp )
  782. {
  783. ClearChildren();
  784. InitDocument();
  785. fseek( fp, 0, SEEK_END );
  786. unsigned size = ftell( fp );
  787. fseek( fp, 0, SEEK_SET );
  788. charBuffer = new char[size+1];
  789. fread( charBuffer, size, 1, fp );
  790. charBuffer[size] = 0;
  791. ParseDeep( charBuffer );
  792. return errorID;
  793. }
  794. void XMLDocument::Save( const char* filename )
  795. {
  796. FILE* fp = fopen( filename, "w" );
  797. XMLStreamer stream( fp );
  798. Print( &stream );
  799. fclose( fp );
  800. }
  801. int XMLDocument::Parse( const char* p )
  802. {
  803. ClearChildren();
  804. InitDocument();
  805. if ( !p || !*p ) {
  806. return true; // correctly parse an empty string?
  807. }
  808. size_t len = strlen( p );
  809. charBuffer = new char[ len+1 ];
  810. memcpy( charBuffer, p, len+1 );
  811. ParseDeep( charBuffer );
  812. return errorID;
  813. }
  814. void XMLDocument::Print( XMLStreamer* streamer )
  815. {
  816. XMLStreamer stdStreamer( stdout );
  817. if ( !streamer )
  818. streamer = &stdStreamer;
  819. //for( XMLNode* node = firstChild; node; node=node->next ) {
  820. // node->Print( streamer );
  821. //}
  822. Accept( streamer );
  823. }
  824. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  825. {
  826. errorID = error;
  827. errorStr1 = str1;
  828. errorStr2 = str2;
  829. }
  830. void XMLDocument::PrintError() const
  831. {
  832. if ( errorID ) {
  833. char buf1[20] = { 0 };
  834. char buf2[20] = { 0 };
  835. if ( errorStr1 ) {
  836. strncpy( buf1, errorStr1, 20 );
  837. buf1[19] = 0;
  838. }
  839. if ( errorStr2 ) {
  840. strncpy( buf2, errorStr2, 20 );
  841. buf2[19] = 0;
  842. }
  843. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  844. errorID, buf1, buf2 );
  845. }
  846. }
  847. XMLStreamer::XMLStreamer( FILE* file ) :
  848. elementJustOpened( false ),
  849. firstElement( true ),
  850. fp( file ),
  851. depth( 0 ),
  852. textDepth( -1 )
  853. {
  854. for( int i=0; i<ENTITY_RANGE; ++i ) {
  855. entityFlag[i] = false;
  856. restrictedEntityFlag[i] = false;
  857. }
  858. for( int i=0; i<NUM_ENTITIES; ++i ) {
  859. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  860. if ( entities[i].value < ENTITY_RANGE ) {
  861. entityFlag[ entities[i].value ] = true;
  862. }
  863. }
  864. restrictedEntityFlag['&'] = true;
  865. restrictedEntityFlag['<'] = true;
  866. buffer.Push( 0 );
  867. }
  868. void XMLStreamer::Print( const char* format, ... )
  869. {
  870. va_list va;
  871. va_start( va, format );
  872. if ( fp ) {
  873. vfprintf( fp, format, va );
  874. }
  875. else {
  876. // This seems brutally complex. Haven't figured out a better
  877. // way on windows.
  878. #ifdef _MSC_VER
  879. int len = -1;
  880. int expand = 1000;
  881. while ( len < 0 ) {
  882. len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
  883. if ( len < 0 ) {
  884. accumulator.PushArr( expand );
  885. expand *= 3/2;
  886. }
  887. }
  888. char* p = buffer.PushArr( len ) - 1;
  889. memcpy( p, accumulator.Mem(), len+1 );
  890. #else
  891. int len = vsnprintf( 0, 0, format, va );
  892. char* p = buffer.PushArr( len ) - 1;
  893. vsprintf_s( p, len+1, format, va );
  894. #endif
  895. }
  896. va_end( va );
  897. }
  898. void XMLStreamer::PrintSpace( int depth )
  899. {
  900. for( int i=0; i<depth; ++i ) {
  901. Print( " " );
  902. }
  903. }
  904. void XMLStreamer::PrintString( const char* p, bool restricted )
  905. {
  906. // Look for runs of bytes between entities to print.
  907. const char* q = p;
  908. const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
  909. while ( *q ) {
  910. if ( *q < ENTITY_RANGE ) {
  911. // Check for entities. If one is found, flush
  912. // the stream up until the entity, write the
  913. // entity, and keep looking.
  914. if ( flag[*q] ) {
  915. while ( p < q ) {
  916. Print( "%c", *p );
  917. ++p;
  918. }
  919. for( int i=0; i<NUM_ENTITIES; ++i ) {
  920. if ( entities[i].value == *q ) {
  921. Print( "&%s;", entities[i].pattern );
  922. break;
  923. }
  924. }
  925. ++p;
  926. }
  927. }
  928. ++q;
  929. }
  930. // Flush the remaining string. This will be the entire
  931. // string if an entity wasn't found.
  932. if ( q-p > 0 ) {
  933. Print( "%s", p );
  934. }
  935. }
  936. void XMLStreamer::OpenElement( const char* name )
  937. {
  938. if ( elementJustOpened ) {
  939. SealElement();
  940. }
  941. stack.Push( name );
  942. if ( textDepth < 0 && !firstElement ) {
  943. Print( "\n" );
  944. PrintSpace( depth );
  945. }
  946. Print( "<%s", name );
  947. elementJustOpened = true;
  948. firstElement = false;
  949. ++depth;
  950. }
  951. void XMLStreamer::PushAttribute( const char* name, const char* value )
  952. {
  953. TIXMLASSERT( elementJustOpened );
  954. Print( " %s=\"", name );
  955. PrintString( value, false );
  956. Print( "\"" );
  957. }
  958. void XMLStreamer::CloseElement()
  959. {
  960. --depth;
  961. const char* name = stack.Pop();
  962. if ( elementJustOpened ) {
  963. Print( "/>" );
  964. }
  965. else {
  966. if ( textDepth < 0 ) {
  967. Print( "\n" );
  968. PrintSpace( depth );
  969. }
  970. Print( "</%s>", name );
  971. }
  972. if ( textDepth == depth )
  973. textDepth = -1;
  974. if ( depth == 0 )
  975. Print( "\n" );
  976. elementJustOpened = false;
  977. }
  978. void XMLStreamer::SealElement()
  979. {
  980. elementJustOpened = false;
  981. Print( ">" );
  982. }
  983. void XMLStreamer::PushText( const char* text, bool cdata )
  984. {
  985. textDepth = depth-1;
  986. if ( elementJustOpened ) {
  987. SealElement();
  988. }
  989. if ( cdata ) {
  990. Print( "<![CDATA[" );
  991. Print( "%s", text );
  992. Print( "]]>" );
  993. }
  994. else {
  995. PrintString( text, true );
  996. }
  997. }
  998. void XMLStreamer::PushComment( const char* comment )
  999. {
  1000. if ( elementJustOpened ) {
  1001. SealElement();
  1002. }
  1003. if ( textDepth < 0 && !firstElement ) {
  1004. Print( "\n" );
  1005. PrintSpace( depth );
  1006. }
  1007. firstElement = false;
  1008. Print( "<!--%s-->", comment );
  1009. }
  1010. void XMLStreamer::PushDeclaration( const char* value )
  1011. {
  1012. if ( elementJustOpened ) {
  1013. SealElement();
  1014. }
  1015. if ( textDepth < 0 && !firstElement) {
  1016. Print( "\n" );
  1017. PrintSpace( depth );
  1018. }
  1019. firstElement = false;
  1020. Print( "<?%s?>", value );
  1021. }
  1022. void XMLStreamer::PushUnknown( const char* value )
  1023. {
  1024. if ( elementJustOpened ) {
  1025. SealElement();
  1026. }
  1027. if ( textDepth < 0 && !firstElement ) {
  1028. Print( "\n" );
  1029. PrintSpace( depth );
  1030. }
  1031. firstElement = false;
  1032. Print( "<!%s>", value );
  1033. }
  1034. bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1035. {
  1036. OpenElement( element.Name() );
  1037. while ( attribute ) {
  1038. PushAttribute( attribute->Name(), attribute->Value() );
  1039. attribute = attribute->Next();
  1040. }
  1041. return true;
  1042. }
  1043. bool XMLStreamer::VisitExit( const XMLElement& element )
  1044. {
  1045. CloseElement();
  1046. return true;
  1047. }
  1048. bool XMLStreamer::Visit( const XMLText& text )
  1049. {
  1050. PushText( text.Value() );
  1051. return true;
  1052. }
  1053. bool XMLStreamer::Visit( const XMLComment& comment )
  1054. {
  1055. PushComment( comment.Value() );
  1056. return true;
  1057. }
  1058. bool XMLStreamer::Visit( const XMLDeclaration& declaration )
  1059. {
  1060. PushDeclaration( declaration.Value() );
  1061. return true;
  1062. }
  1063. bool XMLStreamer::Visit( const XMLUnknown& unknown )
  1064. {
  1065. PushUnknown( unknown.Value() );
  1066. return true;
  1067. }