tinyxml2.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  1. /*
  2. Original code by Lee Thomason (www.grinninglizard.com)
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any
  5. damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any
  7. purpose, including commercial applications, and to alter it and
  8. redistribute it freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must
  10. not claim that you wrote the original software. If you use this
  11. software in a product, an acknowledgment in the product documentation
  12. would be appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and
  14. must not be misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #include "tinyxml2.h"
  19. #if 1
  20. #include <cstdarg>
  21. #include <cstdio>
  22. #include <cstdlib>
  23. #include <new>
  24. #else
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include <new>
  30. #include <stdarg.h>
  31. #endif
  32. using namespace tinyxml2;
  33. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  34. static const char LF = LINE_FEED;
  35. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  36. static const char CR = CARRIAGE_RETURN;
  37. static const char SINGLE_QUOTE = '\'';
  38. static const char DOUBLE_QUOTE = '\"';
  39. // Bunch of unicode info at:
  40. // http://www.unicode.org/faq/utf_bom.html
  41. // ef bb bf (Microsoft "lead bytes") - designates UTF-8
  42. static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  43. static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  44. static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  45. #define DELETE_NODE( node ) { \
  46. if ( node ) { \
  47. MemPool* pool = node->memPool; \
  48. node->~XMLNode(); \
  49. pool->Free( node ); \
  50. } \
  51. }
  52. #define DELETE_ATTRIBUTE( attrib ) { \
  53. if ( attrib ) { \
  54. MemPool* pool = attrib->memPool; \
  55. attrib->~XMLAttribute(); \
  56. pool->Free( attrib ); \
  57. } \
  58. }
  59. struct Entity {
  60. const char* pattern;
  61. int length;
  62. char value;
  63. };
  64. static const int NUM_ENTITIES = 5;
  65. static const Entity entities[NUM_ENTITIES] =
  66. {
  67. { "quot", 4, DOUBLE_QUOTE },
  68. { "amp", 3, '&' },
  69. { "apos", 4, SINGLE_QUOTE },
  70. { "lt", 2, '<' },
  71. { "gt", 2, '>' }
  72. };
  73. StrPair::~StrPair()
  74. {
  75. Reset();
  76. }
  77. void StrPair::Reset()
  78. {
  79. if ( flags & NEEDS_DELETE ) {
  80. delete [] start;
  81. }
  82. flags = 0;
  83. start = 0;
  84. end = 0;
  85. }
  86. void StrPair::SetStr( const char* str, int flags )
  87. {
  88. Reset();
  89. size_t len = strlen( str );
  90. start = new char[ len+1 ];
  91. memcpy( start, str, len+1 );
  92. end = start + len;
  93. this->flags = flags | NEEDS_DELETE;
  94. }
  95. char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
  96. {
  97. TIXMLASSERT( endTag && *endTag );
  98. char* start = p; // fixme: hides a member
  99. char endChar = *endTag;
  100. int length = strlen( endTag );
  101. // Inner loop of text parsing.
  102. while ( *p ) {
  103. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  104. Set( start, p, strFlags );
  105. return p + length;
  106. }
  107. ++p;
  108. }
  109. return 0;
  110. }
  111. char* StrPair::ParseName( char* p )
  112. {
  113. char* start = p;
  114. start = p;
  115. if ( !start || !(*start) ) {
  116. return 0;
  117. }
  118. if ( !XMLUtil::IsAlpha( *p ) ) {
  119. return 0;
  120. }
  121. while( *p && (
  122. XMLUtil::IsAlphaNum( (unsigned char) *p )
  123. || *p == '_'
  124. || *p == '-'
  125. || *p == '.'
  126. || *p == ':' ))
  127. {
  128. ++p;
  129. }
  130. if ( p > start ) {
  131. Set( start, p, 0 );
  132. return p;
  133. }
  134. return 0;
  135. }
  136. const char* StrPair::GetStr()
  137. {
  138. if ( flags & NEEDS_FLUSH ) {
  139. *end = 0;
  140. flags ^= NEEDS_FLUSH;
  141. if ( flags ) {
  142. char* p = start; // the read pointer
  143. char* q = start; // the write pointer
  144. while( p < end ) {
  145. if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  146. // CR-LF pair becomes LF
  147. // CR alone becomes LF
  148. // LF-CR becomes LF
  149. if ( *(p+1) == LF ) {
  150. p += 2;
  151. }
  152. else {
  153. ++p;
  154. }
  155. *q++ = LF;
  156. }
  157. else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  158. if ( *(p+1) == CR ) {
  159. p += 2;
  160. }
  161. else {
  162. ++p;
  163. }
  164. *q++ = LF;
  165. }
  166. else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  167. int i=0;
  168. // Entities handled by tinyXML2:
  169. // - special entities in the entity table [in/out]
  170. // - numeric character reference [in]
  171. // &#20013; or &#x4e2d;
  172. if ( *(p+1) == '#' ) {
  173. char buf[10] = { 0 };
  174. int len;
  175. p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
  176. for( int i=0; i<len; ++i ) {
  177. *q++ = buf[i];
  178. }
  179. TIXMLASSERT( q <= p );
  180. }
  181. else {
  182. for( i=0; i<NUM_ENTITIES; ++i ) {
  183. if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
  184. && *(p+entities[i].length+1) == ';' )
  185. {
  186. // Found an entity convert;
  187. *q = entities[i].value;
  188. ++q;
  189. p += entities[i].length + 2;
  190. break;
  191. }
  192. }
  193. if ( i == NUM_ENTITIES ) {
  194. // fixme: treat as error?
  195. ++p;
  196. ++q;
  197. }
  198. }
  199. }
  200. else {
  201. *q = *p;
  202. ++p;
  203. ++q;
  204. }
  205. }
  206. *q = 0;
  207. }
  208. flags = (flags & NEEDS_DELETE);
  209. }
  210. return start;
  211. }
  212. // --------- XMLUtil ----------- //
  213. const char* XMLUtil::ReadBOM( const char* p, bool* bom )
  214. {
  215. *bom = false;
  216. const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
  217. // Check for BOM:
  218. if ( *(pu+0) == TIXML_UTF_LEAD_0
  219. && *(pu+1) == TIXML_UTF_LEAD_1
  220. && *(pu+2) == TIXML_UTF_LEAD_2 )
  221. {
  222. *bom = true;
  223. p += 3;
  224. }
  225. return p;
  226. }
  227. void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
  228. {
  229. const unsigned long BYTE_MASK = 0xBF;
  230. const unsigned long BYTE_MARK = 0x80;
  231. const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  232. if (input < 0x80)
  233. *length = 1;
  234. else if ( input < 0x800 )
  235. *length = 2;
  236. else if ( input < 0x10000 )
  237. *length = 3;
  238. else if ( input < 0x200000 )
  239. *length = 4;
  240. else
  241. { *length = 0; return; } // This code won't covert this correctly anyway.
  242. output += *length;
  243. // Scary scary fall throughs.
  244. switch (*length)
  245. {
  246. case 4:
  247. --output;
  248. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  249. input >>= 6;
  250. case 3:
  251. --output;
  252. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  253. input >>= 6;
  254. case 2:
  255. --output;
  256. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  257. input >>= 6;
  258. case 1:
  259. --output;
  260. *output = (char)(input | FIRST_BYTE_MARK[*length]);
  261. }
  262. }
  263. const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
  264. {
  265. // Presume an entity, and pull it out.
  266. *length = 0;
  267. if ( *(p+1) == '#' && *(p+2) )
  268. {
  269. unsigned long ucs = 0;
  270. int delta = 0;
  271. unsigned mult = 1;
  272. if ( *(p+2) == 'x' )
  273. {
  274. // Hexadecimal.
  275. if ( !*(p+3) ) return 0;
  276. const char* q = p+3;
  277. q = strchr( q, ';' );
  278. if ( !q || !*q ) return 0;
  279. delta = (q-p);
  280. --q;
  281. while ( *q != 'x' )
  282. {
  283. if ( *q >= '0' && *q <= '9' )
  284. ucs += mult * (*q - '0');
  285. else if ( *q >= 'a' && *q <= 'f' )
  286. ucs += mult * (*q - 'a' + 10);
  287. else if ( *q >= 'A' && *q <= 'F' )
  288. ucs += mult * (*q - 'A' + 10 );
  289. else
  290. return 0;
  291. mult *= 16;
  292. --q;
  293. }
  294. }
  295. else
  296. {
  297. // Decimal.
  298. if ( !*(p+2) ) return 0;
  299. const char* q = p+2;
  300. q = strchr( q, ';' );
  301. if ( !q || !*q ) return 0;
  302. delta = q-p;
  303. --q;
  304. while ( *q != '#' )
  305. {
  306. if ( *q >= '0' && *q <= '9' )
  307. ucs += mult * (*q - '0');
  308. else
  309. return 0;
  310. mult *= 10;
  311. --q;
  312. }
  313. }
  314. // convert the UCS to UTF-8
  315. ConvertUTF32ToUTF8( ucs, value, length );
  316. return p + delta + 1;
  317. }
  318. return p+1;
  319. }
  320. char* XMLDocument::Identify( char* p, XMLNode** node )
  321. {
  322. XMLNode* returnNode = 0;
  323. char* start = p;
  324. p = XMLUtil::SkipWhiteSpace( p );
  325. if( !p || !*p )
  326. {
  327. return p;
  328. }
  329. // What is this thing?
  330. // - Elements start with a letter or underscore, but xml is reserved.
  331. // - Comments: <!--
  332. // - Decleration: <?
  333. // - Everthing else is unknown to tinyxml.
  334. //
  335. static const char* xmlHeader = { "<?" };
  336. static const char* commentHeader = { "<!--" };
  337. static const char* dtdHeader = { "<!" };
  338. static const char* cdataHeader = { "<![CDATA[" };
  339. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  340. static const int xmlHeaderLen = 2;
  341. static const int commentHeaderLen = 4;
  342. static const int dtdHeaderLen = 2;
  343. static const int cdataHeaderLen = 9;
  344. static const int elementHeaderLen = 1;
  345. #if defined(_MSC_VER)
  346. #pragma warning ( push )
  347. #pragma warning ( disable : 4127 )
  348. #endif
  349. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  350. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  351. #if defined(_MSC_VER)
  352. #pragma warning (pop)
  353. #endif
  354. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  355. returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
  356. returnNode->memPool = &commentPool;
  357. p += xmlHeaderLen;
  358. }
  359. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  360. returnNode = new (commentPool.Alloc()) XMLComment( this );
  361. returnNode->memPool = &commentPool;
  362. p += commentHeaderLen;
  363. }
  364. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  365. XMLText* text = new (textPool.Alloc()) XMLText( this );
  366. returnNode = text;
  367. returnNode->memPool = &textPool;
  368. p += cdataHeaderLen;
  369. text->SetCData( true );
  370. }
  371. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  372. returnNode = new (commentPool.Alloc()) XMLUnknown( this );
  373. returnNode->memPool = &commentPool;
  374. p += dtdHeaderLen;
  375. }
  376. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  377. returnNode = new (elementPool.Alloc()) XMLElement( this );
  378. returnNode->memPool = &elementPool;
  379. p += elementHeaderLen;
  380. }
  381. else {
  382. returnNode = new (textPool.Alloc()) XMLText( this );
  383. returnNode->memPool = &textPool;
  384. p = start; // Back it up, all the text counts.
  385. }
  386. *node = returnNode;
  387. return p;
  388. }
  389. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  390. {
  391. if ( visitor->VisitEnter( *this ) )
  392. {
  393. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  394. {
  395. if ( !node->Accept( visitor ) )
  396. break;
  397. }
  398. }
  399. return visitor->VisitExit( *this );
  400. }
  401. // --------- XMLNode ----------- //
  402. XMLNode::XMLNode( XMLDocument* doc ) :
  403. document( doc ),
  404. parent( 0 ),
  405. firstChild( 0 ), lastChild( 0 ),
  406. prev( 0 ), next( 0 )
  407. {
  408. }
  409. XMLNode::~XMLNode()
  410. {
  411. DeleteChildren();
  412. if ( parent ) {
  413. parent->Unlink( this );
  414. }
  415. }
  416. void XMLNode::SetValue( const char* str, bool staticMem )
  417. {
  418. if ( staticMem )
  419. value.SetInternedStr( str );
  420. else
  421. value.SetStr( str );
  422. }
  423. void XMLNode::DeleteChildren()
  424. {
  425. while( firstChild ) {
  426. XMLNode* node = firstChild;
  427. Unlink( node );
  428. DELETE_NODE( node );
  429. }
  430. firstChild = lastChild = 0;
  431. }
  432. void XMLNode::Unlink( XMLNode* child )
  433. {
  434. TIXMLASSERT( child->parent == this );
  435. if ( child == firstChild )
  436. firstChild = firstChild->next;
  437. if ( child == lastChild )
  438. lastChild = lastChild->prev;
  439. if ( child->prev ) {
  440. child->prev->next = child->next;
  441. }
  442. if ( child->next ) {
  443. child->next->prev = child->prev;
  444. }
  445. child->parent = 0;
  446. }
  447. void XMLNode::DeleteChild( XMLNode* node )
  448. {
  449. TIXMLASSERT( node->parent == this );
  450. DELETE_NODE( node );
  451. }
  452. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  453. {
  454. if ( lastChild ) {
  455. TIXMLASSERT( firstChild );
  456. TIXMLASSERT( lastChild->next == 0 );
  457. lastChild->next = addThis;
  458. addThis->prev = lastChild;
  459. lastChild = addThis;
  460. addThis->next = 0;
  461. }
  462. else {
  463. TIXMLASSERT( firstChild == 0 );
  464. firstChild = lastChild = addThis;
  465. addThis->prev = 0;
  466. addThis->next = 0;
  467. }
  468. addThis->parent = this;
  469. return addThis;
  470. }
  471. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  472. {
  473. if ( firstChild ) {
  474. TIXMLASSERT( lastChild );
  475. TIXMLASSERT( firstChild->prev == 0 );
  476. firstChild->prev = addThis;
  477. addThis->next = firstChild;
  478. firstChild = addThis;
  479. addThis->prev = 0;
  480. }
  481. else {
  482. TIXMLASSERT( lastChild == 0 );
  483. firstChild = lastChild = addThis;
  484. addThis->prev = 0;
  485. addThis->next = 0;
  486. }
  487. addThis->parent = this;
  488. return addThis;
  489. }
  490. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  491. {
  492. TIXMLASSERT( afterThis->parent == this );
  493. if ( afterThis->parent != this )
  494. return 0;
  495. if ( afterThis->next == 0 ) {
  496. // The last node or the only node.
  497. return InsertEndChild( addThis );
  498. }
  499. addThis->prev = afterThis;
  500. addThis->next = afterThis->next;
  501. afterThis->next->prev = addThis;
  502. afterThis->next = addThis;
  503. addThis->parent = this;
  504. return addThis;
  505. }
  506. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  507. {
  508. for( XMLNode* node=firstChild; node; node=node->next ) {
  509. XMLElement* element = node->ToElement();
  510. if ( element ) {
  511. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  512. return element;
  513. }
  514. }
  515. }
  516. return 0;
  517. }
  518. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  519. {
  520. for( XMLNode* node=lastChild; node; node=node->prev ) {
  521. XMLElement* element = node->ToElement();
  522. if ( element ) {
  523. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  524. return element;
  525. }
  526. }
  527. }
  528. return 0;
  529. }
  530. const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
  531. {
  532. for( XMLNode* element=this->next; element; element = element->next ) {
  533. if ( element->ToElement()
  534. && (!value || XMLUtil::StringEqual( value, element->Value() )))
  535. {
  536. return element->ToElement();
  537. }
  538. }
  539. return 0;
  540. }
  541. const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
  542. {
  543. for( XMLNode* element=this->prev; element; element = element->prev ) {
  544. if ( element->ToElement()
  545. && (!value || XMLUtil::StringEqual( value, element->Value() )))
  546. {
  547. return element->ToElement();
  548. }
  549. }
  550. return 0;
  551. }
  552. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  553. {
  554. // This is a recursive method, but thinking about it "at the current level"
  555. // it is a pretty simple flat list:
  556. // <foo/>
  557. // <!-- comment -->
  558. //
  559. // With a special case:
  560. // <foo>
  561. // </foo>
  562. // <!-- comment -->
  563. //
  564. // Where the closing element (/foo) *must* be the next thing after the opening
  565. // element, and the names must match. BUT the tricky bit is that the closing
  566. // element will be read by the child.
  567. //
  568. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  569. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  570. while( p && *p ) {
  571. XMLNode* node = 0;
  572. p = document->Identify( p, &node );
  573. if ( p == 0 || node == 0 ) {
  574. break;
  575. }
  576. StrPair endTag;
  577. p = node->ParseDeep( p, &endTag );
  578. if ( !p ) {
  579. DELETE_NODE( node );
  580. node = 0;
  581. if ( !document->Error() ) {
  582. document->SetError( XML_ERROR_PARSING, 0, 0 );
  583. }
  584. break;
  585. }
  586. // We read the end tag. Return it to the parent.
  587. if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
  588. if ( parentEnd ) {
  589. *parentEnd = ((XMLElement*)node)->value;
  590. }
  591. DELETE_NODE( node );
  592. return p;
  593. }
  594. // Handle an end tag returned to this level.
  595. // And handle a bunch of annoying errors.
  596. XMLElement* ele = node->ToElement();
  597. if ( ele ) {
  598. if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
  599. document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  600. p = 0;
  601. }
  602. else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
  603. document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  604. p = 0;
  605. }
  606. else if ( !endTag.Empty() ) {
  607. if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
  608. document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  609. p = 0;
  610. }
  611. }
  612. }
  613. if ( p == 0 ) {
  614. DELETE_NODE( node );
  615. node = 0;
  616. }
  617. if ( node ) {
  618. this->InsertEndChild( node );
  619. }
  620. }
  621. return 0;
  622. }
  623. // --------- XMLText ---------- //
  624. char* XMLText::ParseDeep( char* p, StrPair* )
  625. {
  626. const char* start = p;
  627. if ( this->CData() ) {
  628. p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  629. if ( !p ) {
  630. document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
  631. }
  632. return p;
  633. }
  634. else {
  635. p = value.ParseText( p, "<", document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES );
  636. if ( !p ) {
  637. document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
  638. }
  639. if ( p && *p ) {
  640. return p-1;
  641. }
  642. }
  643. return 0;
  644. }
  645. XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
  646. {
  647. if ( !doc ) {
  648. doc = document;
  649. }
  650. XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
  651. text->SetCData( this->CData() );
  652. return text;
  653. }
  654. bool XMLText::ShallowEqual( const XMLNode* compare ) const
  655. {
  656. return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
  657. }
  658. bool XMLText::Accept( XMLVisitor* visitor ) const
  659. {
  660. return visitor->Visit( *this );
  661. }
  662. // --------- XMLComment ---------- //
  663. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  664. {
  665. }
  666. XMLComment::~XMLComment()
  667. {
  668. //printf( "~XMLComment\n" );
  669. }
  670. char* XMLComment::ParseDeep( char* p, StrPair* )
  671. {
  672. // Comment parses as text.
  673. const char* start = p;
  674. p = value.ParseText( p, "-->", StrPair::COMMENT );
  675. if ( p == 0 ) {
  676. document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
  677. }
  678. return p;
  679. }
  680. XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
  681. {
  682. if ( !doc ) {
  683. doc = document;
  684. }
  685. XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
  686. return comment;
  687. }
  688. bool XMLComment::ShallowEqual( const XMLNode* compare ) const
  689. {
  690. return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() ));
  691. }
  692. bool XMLComment::Accept( XMLVisitor* visitor ) const
  693. {
  694. return visitor->Visit( *this );
  695. }
  696. // --------- XMLDeclaration ---------- //
  697. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  698. {
  699. }
  700. XMLDeclaration::~XMLDeclaration()
  701. {
  702. //printf( "~XMLDeclaration\n" );
  703. }
  704. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  705. {
  706. // Declaration parses as text.
  707. const char* start = p;
  708. p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  709. if ( p == 0 ) {
  710. document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
  711. }
  712. return p;
  713. }
  714. XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
  715. {
  716. if ( !doc ) {
  717. doc = document;
  718. }
  719. XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
  720. return dec;
  721. }
  722. bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
  723. {
  724. return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() ));
  725. }
  726. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  727. {
  728. return visitor->Visit( *this );
  729. }
  730. // --------- XMLUnknown ---------- //
  731. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  732. {
  733. }
  734. XMLUnknown::~XMLUnknown()
  735. {
  736. }
  737. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  738. {
  739. // Unknown parses as text.
  740. const char* start = p;
  741. p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  742. if ( !p ) {
  743. document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
  744. }
  745. return p;
  746. }
  747. XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
  748. {
  749. if ( !doc ) {
  750. doc = document;
  751. }
  752. XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
  753. return text;
  754. }
  755. bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
  756. {
  757. return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() ));
  758. }
  759. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  760. {
  761. return visitor->Visit( *this );
  762. }
  763. // --------- XMLAttribute ---------- //
  764. char* XMLAttribute::ParseDeep( char* p, bool processEntities )
  765. {
  766. p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
  767. if ( !p || !*p ) return 0;
  768. char endTag[2] = { *p, 0 };
  769. ++p;
  770. p = value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
  771. //if ( value.Empty() ) return 0;
  772. return p;
  773. }
  774. void XMLAttribute::SetName( const char* n )
  775. {
  776. name.SetStr( n );
  777. }
  778. int XMLAttribute::QueryIntValue( int* value ) const
  779. {
  780. if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
  781. return XML_NO_ERROR;
  782. return XML_WRONG_ATTRIBUTE_TYPE;
  783. }
  784. int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
  785. {
  786. if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
  787. return XML_NO_ERROR;
  788. return XML_WRONG_ATTRIBUTE_TYPE;
  789. }
  790. int XMLAttribute::QueryBoolValue( bool* value ) const
  791. {
  792. int ival = -1;
  793. QueryIntValue( &ival );
  794. if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
  795. *value = true;
  796. return XML_NO_ERROR;
  797. }
  798. else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
  799. *value = false;
  800. return XML_NO_ERROR;
  801. }
  802. return XML_WRONG_ATTRIBUTE_TYPE;
  803. }
  804. int XMLAttribute::QueryDoubleValue( double* value ) const
  805. {
  806. if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
  807. return XML_NO_ERROR;
  808. return XML_WRONG_ATTRIBUTE_TYPE;
  809. }
  810. int XMLAttribute::QueryFloatValue( float* value ) const
  811. {
  812. if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
  813. return XML_NO_ERROR;
  814. return XML_WRONG_ATTRIBUTE_TYPE;
  815. }
  816. void XMLAttribute::SetAttribute( const char* v )
  817. {
  818. value.SetStr( v );
  819. }
  820. void XMLAttribute::SetAttribute( int v )
  821. {
  822. char buf[BUF_SIZE];
  823. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
  824. value.SetStr( buf );
  825. }
  826. void XMLAttribute::SetAttribute( unsigned v )
  827. {
  828. char buf[BUF_SIZE];
  829. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
  830. value.SetStr( buf );
  831. }
  832. void XMLAttribute::SetAttribute( bool v )
  833. {
  834. char buf[BUF_SIZE];
  835. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
  836. value.SetStr( buf );
  837. }
  838. void XMLAttribute::SetAttribute( double v )
  839. {
  840. char buf[BUF_SIZE];
  841. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  842. value.SetStr( buf );
  843. }
  844. void XMLAttribute::SetAttribute( float v )
  845. {
  846. char buf[BUF_SIZE];
  847. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  848. value.SetStr( buf );
  849. }
  850. // --------- XMLElement ---------- //
  851. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  852. closingType( 0 ),
  853. rootAttribute( 0 )
  854. {
  855. }
  856. XMLElement::~XMLElement()
  857. {
  858. while( rootAttribute ) {
  859. XMLAttribute* next = rootAttribute->next;
  860. DELETE_ATTRIBUTE( rootAttribute );
  861. rootAttribute = next;
  862. }
  863. }
  864. XMLAttribute* XMLElement::FindAttribute( const char* name )
  865. {
  866. XMLAttribute* a = 0;
  867. for( a=rootAttribute; a; a = a->next ) {
  868. if ( XMLUtil::StringEqual( a->Name(), name ) )
  869. return a;
  870. }
  871. return 0;
  872. }
  873. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  874. {
  875. XMLAttribute* a = 0;
  876. for( a=rootAttribute; a; a = a->next ) {
  877. if ( XMLUtil::StringEqual( a->Name(), name ) )
  878. return a;
  879. }
  880. return 0;
  881. }
  882. const char* XMLElement::Attribute( const char* name, const char* value ) const
  883. {
  884. const XMLAttribute* a = FindAttribute( name );
  885. if ( !a )
  886. return 0;
  887. if ( !value || XMLUtil::StringEqual( a->Value(), value ))
  888. return a->Value();
  889. return 0;
  890. }
  891. const char* XMLElement::GetText() const
  892. {
  893. if ( FirstChild() && FirstChild()->ToText() ) {
  894. return FirstChild()->ToText()->Value();
  895. }
  896. return 0;
  897. }
  898. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  899. {
  900. XMLAttribute* attrib = FindAttribute( name );
  901. if ( !attrib ) {
  902. attrib = new (document->attributePool.Alloc() ) XMLAttribute();
  903. attrib->memPool = &document->attributePool;
  904. LinkAttribute( attrib );
  905. attrib->SetName( name );
  906. }
  907. return attrib;
  908. }
  909. void XMLElement::LinkAttribute( XMLAttribute* attrib )
  910. {
  911. if ( rootAttribute ) {
  912. XMLAttribute* end = rootAttribute;
  913. while ( end->next )
  914. end = end->next;
  915. end->next = attrib;
  916. }
  917. else {
  918. rootAttribute = attrib;
  919. }
  920. }
  921. void XMLElement::DeleteAttribute( const char* name )
  922. {
  923. XMLAttribute* prev = 0;
  924. for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
  925. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  926. if ( prev ) {
  927. prev->next = a->next;
  928. }
  929. else {
  930. rootAttribute = a->next;
  931. }
  932. DELETE_ATTRIBUTE( a );
  933. break;
  934. }
  935. prev = a;
  936. }
  937. }
  938. char* XMLElement::ParseAttributes( char* p )
  939. {
  940. const char* start = p;
  941. // Read the attributes.
  942. while( p ) {
  943. p = XMLUtil::SkipWhiteSpace( p );
  944. if ( !p || !(*p) ) {
  945. document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
  946. return 0;
  947. }
  948. // attribute.
  949. if ( XMLUtil::IsAlpha( *p ) ) {
  950. XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute();
  951. attrib->memPool = &document->attributePool;
  952. p = attrib->ParseDeep( p, document->ProcessEntities() );
  953. if ( !p || Attribute( attrib->Name() ) ) {
  954. DELETE_ATTRIBUTE( attrib );
  955. document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
  956. return 0;
  957. }
  958. LinkAttribute( attrib );
  959. }
  960. // end of the tag
  961. else if ( *p == '/' && *(p+1) == '>' ) {
  962. closingType = CLOSED;
  963. return p+2; // done; sealed element.
  964. }
  965. // end of the tag
  966. else if ( *p == '>' ) {
  967. ++p;
  968. break;
  969. }
  970. else {
  971. document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
  972. return 0;
  973. }
  974. }
  975. return p;
  976. }
  977. //
  978. // <ele></ele>
  979. // <ele>foo<b>bar</b></ele>
  980. //
  981. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  982. {
  983. // Read the element name.
  984. p = XMLUtil::SkipWhiteSpace( p );
  985. if ( !p ) return 0;
  986. // The closing element is the </element> form. It is
  987. // parsed just like a regular element then deleted from
  988. // the DOM.
  989. if ( *p == '/' ) {
  990. closingType = CLOSING;
  991. ++p;
  992. }
  993. p = value.ParseName( p );
  994. if ( value.Empty() ) return 0;
  995. p = ParseAttributes( p );
  996. if ( !p || !*p || closingType )
  997. return p;
  998. p = XMLNode::ParseDeep( p, strPair );
  999. return p;
  1000. }
  1001. XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
  1002. {
  1003. if ( !doc ) {
  1004. doc = document;
  1005. }
  1006. XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
  1007. for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
  1008. element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
  1009. }
  1010. return element;
  1011. }
  1012. bool XMLElement::ShallowEqual( const XMLNode* compare ) const
  1013. {
  1014. const XMLElement* other = compare->ToElement();
  1015. if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
  1016. const XMLAttribute* a=FirstAttribute();
  1017. const XMLAttribute* b=other->FirstAttribute();
  1018. while ( a && b ) {
  1019. if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
  1020. return false;
  1021. }
  1022. }
  1023. if ( a || b ) {
  1024. // different count
  1025. return false;
  1026. }
  1027. return true;
  1028. }
  1029. return false;
  1030. }
  1031. bool XMLElement::Accept( XMLVisitor* visitor ) const
  1032. {
  1033. if ( visitor->VisitEnter( *this, rootAttribute ) )
  1034. {
  1035. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
  1036. {
  1037. if ( !node->Accept( visitor ) )
  1038. break;
  1039. }
  1040. }
  1041. return visitor->VisitExit( *this );
  1042. }
  1043. // --------- XMLDocument ----------- //
  1044. XMLDocument::XMLDocument( bool _processEntities ) :
  1045. XMLNode( 0 ),
  1046. writeBOM( false ),
  1047. processEntities( _processEntities ),
  1048. errorID( 0 ),
  1049. errorStr1( 0 ),
  1050. errorStr2( 0 ),
  1051. charBuffer( 0 )
  1052. {
  1053. document = this; // avoid warning about 'this' in initializer list
  1054. }
  1055. XMLDocument::~XMLDocument()
  1056. {
  1057. DeleteChildren();
  1058. delete [] charBuffer;
  1059. #if 0
  1060. textPool.Trace( "text" );
  1061. elementPool.Trace( "element" );
  1062. commentPool.Trace( "comment" );
  1063. attributePool.Trace( "attribute" );
  1064. #endif
  1065. TIXMLASSERT( textPool.CurrentAllocs() == 0 );
  1066. TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
  1067. TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
  1068. TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
  1069. }
  1070. void XMLDocument::InitDocument()
  1071. {
  1072. errorID = XML_NO_ERROR;
  1073. errorStr1 = 0;
  1074. errorStr2 = 0;
  1075. delete [] charBuffer;
  1076. charBuffer = 0;
  1077. }
  1078. XMLElement* XMLDocument::NewElement( const char* name )
  1079. {
  1080. XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
  1081. ele->memPool = &elementPool;
  1082. ele->SetName( name );
  1083. return ele;
  1084. }
  1085. XMLComment* XMLDocument::NewComment( const char* str )
  1086. {
  1087. XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
  1088. comment->memPool = &commentPool;
  1089. comment->SetValue( str );
  1090. return comment;
  1091. }
  1092. XMLText* XMLDocument::NewText( const char* str )
  1093. {
  1094. XMLText* text = new (textPool.Alloc()) XMLText( this );
  1095. text->memPool = &textPool;
  1096. text->SetValue( str );
  1097. return text;
  1098. }
  1099. XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
  1100. {
  1101. XMLDeclaration* dec = new (commentPool.Alloc()) XMLDeclaration( this );
  1102. dec->memPool = &commentPool;
  1103. dec->SetValue( str );
  1104. return dec;
  1105. }
  1106. XMLUnknown* XMLDocument::NewUnknown( const char* str )
  1107. {
  1108. XMLUnknown* unk = new (commentPool.Alloc()) XMLUnknown( this );
  1109. unk->memPool = &commentPool;
  1110. unk->SetValue( str );
  1111. return unk;
  1112. }
  1113. int XMLDocument::LoadFile( const char* filename )
  1114. {
  1115. DeleteChildren();
  1116. InitDocument();
  1117. #if defined(_MSC_VER)
  1118. #pragma warning ( push )
  1119. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  1120. #endif
  1121. FILE* fp = fopen( filename, "rb" );
  1122. #if defined(_MSC_VER)
  1123. #pragma warning ( pop )
  1124. #endif
  1125. if ( !fp ) {
  1126. SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
  1127. return errorID;
  1128. }
  1129. LoadFile( fp );
  1130. fclose( fp );
  1131. return errorID;
  1132. }
  1133. int XMLDocument::LoadFile( FILE* fp )
  1134. {
  1135. DeleteChildren();
  1136. InitDocument();
  1137. fseek( fp, 0, SEEK_END );
  1138. unsigned size = ftell( fp );
  1139. fseek( fp, 0, SEEK_SET );
  1140. if ( size == 0 ) {
  1141. return errorID;
  1142. }
  1143. charBuffer = new char[size+1];
  1144. fread( charBuffer, size, 1, fp );
  1145. charBuffer[size] = 0;
  1146. const char* p = charBuffer;
  1147. p = XMLUtil::SkipWhiteSpace( p );
  1148. p = XMLUtil::ReadBOM( p, &writeBOM );
  1149. if ( !p || !*p ) {
  1150. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1151. return errorID;
  1152. }
  1153. ParseDeep( charBuffer + (p-charBuffer), 0 );
  1154. return errorID;
  1155. }
  1156. void XMLDocument::SaveFile( const char* filename )
  1157. {
  1158. #if defined(_MSC_VER)
  1159. #pragma warning ( push )
  1160. #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
  1161. #endif
  1162. FILE* fp = fopen( filename, "w" );
  1163. #if defined(_MSC_VER)
  1164. #pragma warning ( pop )
  1165. #endif
  1166. if ( fp ) {
  1167. XMLPrinter stream( fp );
  1168. Print( &stream );
  1169. fclose( fp );
  1170. }
  1171. else {
  1172. SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
  1173. }
  1174. }
  1175. int XMLDocument::Parse( const char* p )
  1176. {
  1177. DeleteChildren();
  1178. InitDocument();
  1179. if ( !p || !*p ) {
  1180. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1181. return errorID;
  1182. }
  1183. p = XMLUtil::SkipWhiteSpace( p );
  1184. p = XMLUtil::ReadBOM( p, &writeBOM );
  1185. if ( !p || !*p ) {
  1186. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1187. return errorID;
  1188. }
  1189. size_t len = strlen( p );
  1190. charBuffer = new char[ len+1 ];
  1191. memcpy( charBuffer, p, len+1 );
  1192. ParseDeep( charBuffer, 0 );
  1193. return errorID;
  1194. }
  1195. void XMLDocument::Print( XMLPrinter* streamer )
  1196. {
  1197. XMLPrinter stdStreamer( stdout );
  1198. if ( !streamer )
  1199. streamer = &stdStreamer;
  1200. Accept( streamer );
  1201. }
  1202. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  1203. {
  1204. errorID = error;
  1205. errorStr1 = str1;
  1206. errorStr2 = str2;
  1207. }
  1208. void XMLDocument::PrintError() const
  1209. {
  1210. if ( errorID ) {
  1211. static const int LEN = 20;
  1212. char buf1[LEN] = { 0 };
  1213. char buf2[LEN] = { 0 };
  1214. if ( errorStr1 ) {
  1215. TIXML_SNPRINTF( buf1, LEN, "%s", errorStr1 );
  1216. buf1[LEN-1] = 0;
  1217. }
  1218. if ( errorStr2 ) {
  1219. TIXML_SNPRINTF( buf2, LEN, "%s", errorStr2 );
  1220. buf2[LEN-1] = 0;
  1221. }
  1222. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  1223. errorID, buf1, buf2 );
  1224. }
  1225. }
  1226. XMLPrinter::XMLPrinter( FILE* file ) :
  1227. elementJustOpened( false ),
  1228. firstElement( true ),
  1229. fp( file ),
  1230. depth( 0 ),
  1231. textDepth( -1 ),
  1232. processEntities( true )
  1233. {
  1234. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1235. entityFlag[i] = false;
  1236. restrictedEntityFlag[i] = false;
  1237. }
  1238. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1239. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  1240. if ( entities[i].value < ENTITY_RANGE ) {
  1241. entityFlag[ (int)entities[i].value ] = true;
  1242. }
  1243. }
  1244. restrictedEntityFlag[(int)'&'] = true;
  1245. restrictedEntityFlag[(int)'<'] = true;
  1246. restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
  1247. buffer.Push( 0 );
  1248. }
  1249. void XMLPrinter::Print( const char* format, ... )
  1250. {
  1251. va_list va;
  1252. va_start( va, format );
  1253. if ( fp ) {
  1254. vfprintf( fp, format, va );
  1255. }
  1256. else {
  1257. // This seems brutally complex. Haven't figured out a better
  1258. // way on windows.
  1259. #ifdef _MSC_VER
  1260. int len = -1;
  1261. int expand = 1000;
  1262. while ( len < 0 ) {
  1263. len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
  1264. if ( len < 0 ) {
  1265. accumulator.PushArr( expand );
  1266. expand *= 3/2;
  1267. }
  1268. }
  1269. char* p = buffer.PushArr( len ) - 1;
  1270. memcpy( p, accumulator.Mem(), len+1 );
  1271. #else
  1272. int len = vsnprintf( 0, 0, format, va );
  1273. // Close out and re-start the va-args
  1274. va_end( va );
  1275. va_start( va, format );
  1276. char* p = buffer.PushArr( len ) - 1;
  1277. vsnprintf( p, len+1, format, va );
  1278. #endif
  1279. }
  1280. va_end( va );
  1281. }
  1282. void XMLPrinter::PrintSpace( int depth )
  1283. {
  1284. for( int i=0; i<depth; ++i ) {
  1285. Print( " " );
  1286. }
  1287. }
  1288. void XMLPrinter::PrintString( const char* p, bool restricted )
  1289. {
  1290. // Look for runs of bytes between entities to print.
  1291. const char* q = p;
  1292. const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
  1293. if ( processEntities ) {
  1294. while ( *q ) {
  1295. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1296. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1297. // Check for entities. If one is found, flush
  1298. // the stream up until the entity, write the
  1299. // entity, and keep looking.
  1300. if ( flag[*q] ) {
  1301. while ( p < q ) {
  1302. Print( "%c", *p );
  1303. ++p;
  1304. }
  1305. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1306. if ( entities[i].value == *q ) {
  1307. Print( "&%s;", entities[i].pattern );
  1308. break;
  1309. }
  1310. }
  1311. ++p;
  1312. }
  1313. }
  1314. ++q;
  1315. }
  1316. }
  1317. // Flush the remaining string. This will be the entire
  1318. // string if an entity wasn't found.
  1319. if ( !processEntities || (q-p > 0) ) {
  1320. Print( "%s", p );
  1321. }
  1322. }
  1323. void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
  1324. {
  1325. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1326. if ( writeBOM ) {
  1327. Print( "%s", bom );
  1328. }
  1329. if ( writeDec ) {
  1330. PushDeclaration( "xml version=\"1.0\"" );
  1331. }
  1332. }
  1333. void XMLPrinter::OpenElement( const char* name )
  1334. {
  1335. if ( elementJustOpened ) {
  1336. SealElement();
  1337. }
  1338. stack.Push( name );
  1339. if ( textDepth < 0 && !firstElement ) {
  1340. Print( "\n" );
  1341. PrintSpace( depth );
  1342. }
  1343. Print( "<%s", name );
  1344. elementJustOpened = true;
  1345. firstElement = false;
  1346. ++depth;
  1347. }
  1348. void XMLPrinter::PushAttribute( const char* name, const char* value )
  1349. {
  1350. TIXMLASSERT( elementJustOpened );
  1351. Print( " %s=\"", name );
  1352. PrintString( value, false );
  1353. Print( "\"" );
  1354. }
  1355. void XMLPrinter::PushAttribute( const char* name, int v )
  1356. {
  1357. char buf[BUF_SIZE];
  1358. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
  1359. PushAttribute( name, buf );
  1360. }
  1361. void XMLPrinter::PushAttribute( const char* name, unsigned v )
  1362. {
  1363. char buf[BUF_SIZE];
  1364. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
  1365. PushAttribute( name, buf );
  1366. }
  1367. void XMLPrinter::PushAttribute( const char* name, bool v )
  1368. {
  1369. char buf[BUF_SIZE];
  1370. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
  1371. PushAttribute( name, buf );
  1372. }
  1373. void XMLPrinter::PushAttribute( const char* name, double v )
  1374. {
  1375. char buf[BUF_SIZE];
  1376. TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
  1377. PushAttribute( name, buf );
  1378. }
  1379. void XMLPrinter::CloseElement()
  1380. {
  1381. --depth;
  1382. const char* name = stack.Pop();
  1383. if ( elementJustOpened ) {
  1384. Print( "/>" );
  1385. }
  1386. else {
  1387. if ( textDepth < 0 ) {
  1388. Print( "\n" );
  1389. PrintSpace( depth );
  1390. }
  1391. Print( "</%s>", name );
  1392. }
  1393. if ( textDepth == depth )
  1394. textDepth = -1;
  1395. if ( depth == 0 )
  1396. Print( "\n" );
  1397. elementJustOpened = false;
  1398. }
  1399. void XMLPrinter::SealElement()
  1400. {
  1401. elementJustOpened = false;
  1402. Print( ">" );
  1403. }
  1404. void XMLPrinter::PushText( const char* text, bool cdata )
  1405. {
  1406. textDepth = depth-1;
  1407. if ( elementJustOpened ) {
  1408. SealElement();
  1409. }
  1410. if ( cdata ) {
  1411. Print( "<![CDATA[" );
  1412. Print( "%s", text );
  1413. Print( "]]>" );
  1414. }
  1415. else {
  1416. PrintString( text, true );
  1417. }
  1418. }
  1419. void XMLPrinter::PushComment( const char* comment )
  1420. {
  1421. if ( elementJustOpened ) {
  1422. SealElement();
  1423. }
  1424. if ( textDepth < 0 && !firstElement ) {
  1425. Print( "\n" );
  1426. PrintSpace( depth );
  1427. }
  1428. firstElement = false;
  1429. Print( "<!--%s-->", comment );
  1430. }
  1431. void XMLPrinter::PushDeclaration( const char* value )
  1432. {
  1433. if ( elementJustOpened ) {
  1434. SealElement();
  1435. }
  1436. if ( textDepth < 0 && !firstElement) {
  1437. Print( "\n" );
  1438. PrintSpace( depth );
  1439. }
  1440. firstElement = false;
  1441. Print( "<?%s?>", value );
  1442. }
  1443. void XMLPrinter::PushUnknown( const char* value )
  1444. {
  1445. if ( elementJustOpened ) {
  1446. SealElement();
  1447. }
  1448. if ( textDepth < 0 && !firstElement ) {
  1449. Print( "\n" );
  1450. PrintSpace( depth );
  1451. }
  1452. firstElement = false;
  1453. Print( "<!%s>", value );
  1454. }
  1455. bool XMLPrinter::VisitEnter( const XMLDocument& doc )
  1456. {
  1457. processEntities = doc.ProcessEntities();
  1458. if ( doc.HasBOM() ) {
  1459. PushHeader( true, false );
  1460. }
  1461. return true;
  1462. }
  1463. bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1464. {
  1465. OpenElement( element.Name() );
  1466. while ( attribute ) {
  1467. PushAttribute( attribute->Name(), attribute->Value() );
  1468. attribute = attribute->Next();
  1469. }
  1470. return true;
  1471. }
  1472. bool XMLPrinter::VisitExit( const XMLElement& )
  1473. {
  1474. CloseElement();
  1475. return true;
  1476. }
  1477. bool XMLPrinter::Visit( const XMLText& text )
  1478. {
  1479. PushText( text.Value(), text.CData() );
  1480. return true;
  1481. }
  1482. bool XMLPrinter::Visit( const XMLComment& comment )
  1483. {
  1484. PushComment( comment.Value() );
  1485. return true;
  1486. }
  1487. bool XMLPrinter::Visit( const XMLDeclaration& declaration )
  1488. {
  1489. PushDeclaration( declaration.Value() );
  1490. return true;
  1491. }
  1492. bool XMLPrinter::Visit( const XMLUnknown& unknown )
  1493. {
  1494. PushUnknown( unknown.Value() );
  1495. return true;
  1496. }