tinyxml2.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351
  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. #include <new> // yes, this one new style header, is in the Android SDK.
  20. #if defined(ANDROID_NDK) || defined(__QNXNTO__)
  21. # include <stddef.h>
  22. #else
  23. # include <cstddef>
  24. #endif
  25. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  26. static const char LF = LINE_FEED;
  27. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  28. static const char CR = CARRIAGE_RETURN;
  29. static const char SINGLE_QUOTE = '\'';
  30. static const char DOUBLE_QUOTE = '\"';
  31. // Bunch of unicode info at:
  32. // http://www.unicode.org/faq/utf_bom.html
  33. // ef bb bf (Microsoft "lead bytes") - designates UTF-8
  34. static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  35. static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  36. static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  37. namespace tinyxml2
  38. {
  39. struct Entity {
  40. const char* pattern;
  41. int length;
  42. char value;
  43. };
  44. static const int NUM_ENTITIES = 5;
  45. static const Entity entities[NUM_ENTITIES] = {
  46. { "quot", 4, DOUBLE_QUOTE },
  47. { "amp", 3, '&' },
  48. { "apos", 4, SINGLE_QUOTE },
  49. { "lt", 2, '<' },
  50. { "gt", 2, '>' }
  51. };
  52. StrPair::~StrPair()
  53. {
  54. Reset();
  55. }
  56. void StrPair::TransferTo( StrPair* other )
  57. {
  58. if ( this == other ) {
  59. return;
  60. }
  61. // This in effect implements the assignment operator by "moving"
  62. // ownership (as in auto_ptr).
  63. TIXMLASSERT( other->_flags == 0 );
  64. TIXMLASSERT( other->_start == 0 );
  65. TIXMLASSERT( other->_end == 0 );
  66. other->Reset();
  67. other->_flags = _flags;
  68. other->_start = _start;
  69. other->_end = _end;
  70. _flags = 0;
  71. _start = 0;
  72. _end = 0;
  73. }
  74. void StrPair::Reset()
  75. {
  76. if ( _flags & NEEDS_DELETE ) {
  77. delete [] _start;
  78. }
  79. _flags = 0;
  80. _start = 0;
  81. _end = 0;
  82. }
  83. void StrPair::SetStr( const char* str, int flags )
  84. {
  85. Reset();
  86. size_t len = strlen( str );
  87. _start = new char[ len+1 ];
  88. memcpy( _start, str, len+1 );
  89. _end = _start + len;
  90. _flags = flags | NEEDS_DELETE;
  91. }
  92. char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
  93. {
  94. TIXMLASSERT( endTag && *endTag );
  95. char* start = p;
  96. char endChar = *endTag;
  97. size_t length = strlen( endTag );
  98. // Inner loop of text parsing.
  99. while ( *p ) {
  100. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  101. Set( start, p, strFlags );
  102. return p + length;
  103. }
  104. ++p;
  105. }
  106. return 0;
  107. }
  108. char* StrPair::ParseName( char* p )
  109. {
  110. if ( !p || !(*p) ) {
  111. return 0;
  112. }
  113. if ( !XMLUtil::IsNameStartChar( *p ) ) {
  114. return 0;
  115. }
  116. char* const start = p;
  117. ++p;
  118. while ( *p && XMLUtil::IsNameChar( *p ) ) {
  119. ++p;
  120. }
  121. Set( start, p, 0 );
  122. return p;
  123. }
  124. void StrPair::CollapseWhitespace()
  125. {
  126. // Adjusting _start would cause undefined behavior on delete[]
  127. TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
  128. // Trim leading space.
  129. _start = XMLUtil::SkipWhiteSpace( _start );
  130. if ( *_start ) {
  131. char* p = _start; // the read pointer
  132. char* q = _start; // the write pointer
  133. while( *p ) {
  134. if ( XMLUtil::IsWhiteSpace( *p )) {
  135. p = XMLUtil::SkipWhiteSpace( p );
  136. if ( *p == 0 ) {
  137. break; // don't write to q; this trims the trailing space.
  138. }
  139. *q = ' ';
  140. ++q;
  141. }
  142. *q = *p;
  143. ++q;
  144. ++p;
  145. }
  146. *q = 0;
  147. }
  148. }
  149. const char* StrPair::GetStr()
  150. {
  151. TIXMLASSERT( _start );
  152. TIXMLASSERT( _end );
  153. if ( _flags & NEEDS_FLUSH ) {
  154. *_end = 0;
  155. _flags ^= NEEDS_FLUSH;
  156. if ( _flags ) {
  157. char* p = _start; // the read pointer
  158. char* q = _start; // the write pointer
  159. while( p < _end ) {
  160. if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  161. // CR-LF pair becomes LF
  162. // CR alone becomes LF
  163. // LF-CR becomes LF
  164. if ( *(p+1) == LF ) {
  165. p += 2;
  166. }
  167. else {
  168. ++p;
  169. }
  170. *q++ = LF;
  171. }
  172. else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  173. if ( *(p+1) == CR ) {
  174. p += 2;
  175. }
  176. else {
  177. ++p;
  178. }
  179. *q++ = LF;
  180. }
  181. else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  182. // Entities handled by tinyXML2:
  183. // - special entities in the entity table [in/out]
  184. // - numeric character reference [in]
  185. // &#20013; or &#x4e2d;
  186. if ( *(p+1) == '#' ) {
  187. const int buflen = 10;
  188. char buf[buflen] = { 0 };
  189. int len = 0;
  190. char* adjusted = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
  191. if ( adjusted == 0 ) {
  192. *q = *p;
  193. ++p;
  194. ++q;
  195. }
  196. else {
  197. TIXMLASSERT( 0 <= len && len <= buflen );
  198. TIXMLASSERT( q + len <= adjusted );
  199. p = adjusted;
  200. memcpy( q, buf, len );
  201. q += len;
  202. }
  203. }
  204. else {
  205. int i=0;
  206. for(; i<NUM_ENTITIES; ++i ) {
  207. const Entity& entity = entities[i];
  208. if ( strncmp( p + 1, entity.pattern, entity.length ) == 0
  209. && *( p + entity.length + 1 ) == ';' ) {
  210. // Found an entity - convert.
  211. *q = entity.value;
  212. ++q;
  213. p += entity.length + 2;
  214. break;
  215. }
  216. }
  217. if ( i == NUM_ENTITIES ) {
  218. // fixme: treat as error?
  219. ++p;
  220. ++q;
  221. }
  222. }
  223. }
  224. else {
  225. *q = *p;
  226. ++p;
  227. ++q;
  228. }
  229. }
  230. *q = 0;
  231. }
  232. // The loop below has plenty going on, and this
  233. // is a less useful mode. Break it out.
  234. if ( _flags & COLLAPSE_WHITESPACE ) {
  235. CollapseWhitespace();
  236. }
  237. _flags = (_flags & NEEDS_DELETE);
  238. }
  239. TIXMLASSERT( _start );
  240. return _start;
  241. }
  242. // --------- XMLUtil ----------- //
  243. const char* XMLUtil::ReadBOM( const char* p, bool* bom )
  244. {
  245. TIXMLASSERT( p );
  246. TIXMLASSERT( bom );
  247. *bom = false;
  248. const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
  249. // Check for BOM:
  250. if ( *(pu+0) == TIXML_UTF_LEAD_0
  251. && *(pu+1) == TIXML_UTF_LEAD_1
  252. && *(pu+2) == TIXML_UTF_LEAD_2 ) {
  253. *bom = true;
  254. p += 3;
  255. }
  256. TIXMLASSERT( p );
  257. return p;
  258. }
  259. void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
  260. {
  261. const unsigned long BYTE_MASK = 0xBF;
  262. const unsigned long BYTE_MARK = 0x80;
  263. const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  264. if (input < 0x80) {
  265. *length = 1;
  266. }
  267. else if ( input < 0x800 ) {
  268. *length = 2;
  269. }
  270. else if ( input < 0x10000 ) {
  271. *length = 3;
  272. }
  273. else if ( input < 0x200000 ) {
  274. *length = 4;
  275. }
  276. else {
  277. *length = 0; // This code won't convert this correctly anyway.
  278. return;
  279. }
  280. output += *length;
  281. // Scary scary fall throughs.
  282. switch (*length) {
  283. case 4:
  284. --output;
  285. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  286. input >>= 6;
  287. case 3:
  288. --output;
  289. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  290. input >>= 6;
  291. case 2:
  292. --output;
  293. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  294. input >>= 6;
  295. case 1:
  296. --output;
  297. *output = (char)(input | FIRST_BYTE_MARK[*length]);
  298. break;
  299. default:
  300. TIXMLASSERT( false );
  301. }
  302. }
  303. const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
  304. {
  305. // Presume an entity, and pull it out.
  306. *length = 0;
  307. if ( *(p+1) == '#' && *(p+2) ) {
  308. unsigned long ucs = 0;
  309. TIXMLASSERT( sizeof( ucs ) >= 4 );
  310. ptrdiff_t delta = 0;
  311. unsigned mult = 1;
  312. static const char SEMICOLON = ';';
  313. if ( *(p+2) == 'x' ) {
  314. // Hexadecimal.
  315. const char* q = p+3;
  316. if ( !(*q) ) {
  317. return 0;
  318. }
  319. q = strchr( q, SEMICOLON );
  320. if ( !q ) {
  321. return 0;
  322. }
  323. TIXMLASSERT( *q == SEMICOLON );
  324. delta = q-p;
  325. --q;
  326. while ( *q != 'x' ) {
  327. unsigned int digit = 0;
  328. if ( *q >= '0' && *q <= '9' ) {
  329. digit = *q - '0';
  330. }
  331. else if ( *q >= 'a' && *q <= 'f' ) {
  332. digit = *q - 'a' + 10;
  333. }
  334. else if ( *q >= 'A' && *q <= 'F' ) {
  335. digit = *q - 'A' + 10;
  336. }
  337. else {
  338. return 0;
  339. }
  340. TIXMLASSERT( digit >= 0 && digit < 16);
  341. TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
  342. const unsigned int digitScaled = mult * digit;
  343. TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
  344. ucs += digitScaled;
  345. TIXMLASSERT( mult <= UINT_MAX / 16 );
  346. mult *= 16;
  347. --q;
  348. }
  349. }
  350. else {
  351. // Decimal.
  352. const char* q = p+2;
  353. if ( !(*q) ) {
  354. return 0;
  355. }
  356. q = strchr( q, SEMICOLON );
  357. if ( !q ) {
  358. return 0;
  359. }
  360. TIXMLASSERT( *q == SEMICOLON );
  361. delta = q-p;
  362. --q;
  363. while ( *q != '#' ) {
  364. if ( *q >= '0' && *q <= '9' ) {
  365. const unsigned int digit = *q - '0';
  366. TIXMLASSERT( digit >= 0 && digit < 10);
  367. TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
  368. const unsigned int digitScaled = mult * digit;
  369. TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
  370. ucs += digitScaled;
  371. }
  372. else {
  373. return 0;
  374. }
  375. TIXMLASSERT( mult <= UINT_MAX / 10 );
  376. mult *= 10;
  377. --q;
  378. }
  379. }
  380. // convert the UCS to UTF-8
  381. ConvertUTF32ToUTF8( ucs, value, length );
  382. return p + delta + 1;
  383. }
  384. return p+1;
  385. }
  386. void XMLUtil::ToStr( int v, char* buffer, int bufferSize )
  387. {
  388. TIXML_SNPRINTF( buffer, bufferSize, "%d", v );
  389. }
  390. void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
  391. {
  392. TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
  393. }
  394. void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
  395. {
  396. TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 );
  397. }
  398. /*
  399. ToStr() of a number is a very tricky topic.
  400. https://github.com/leethomason/tinyxml2/issues/106
  401. */
  402. void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
  403. {
  404. TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v );
  405. }
  406. void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
  407. {
  408. TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v );
  409. }
  410. bool XMLUtil::ToInt( const char* str, int* value )
  411. {
  412. if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {
  413. return true;
  414. }
  415. return false;
  416. }
  417. bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
  418. {
  419. if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
  420. return true;
  421. }
  422. return false;
  423. }
  424. bool XMLUtil::ToBool( const char* str, bool* value )
  425. {
  426. int ival = 0;
  427. if ( ToInt( str, &ival )) {
  428. *value = (ival==0) ? false : true;
  429. return true;
  430. }
  431. if ( StringEqual( str, "true" ) ) {
  432. *value = true;
  433. return true;
  434. }
  435. else if ( StringEqual( str, "false" ) ) {
  436. *value = false;
  437. return true;
  438. }
  439. return false;
  440. }
  441. bool XMLUtil::ToFloat( const char* str, float* value )
  442. {
  443. if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {
  444. return true;
  445. }
  446. return false;
  447. }
  448. bool XMLUtil::ToDouble( const char* str, double* value )
  449. {
  450. if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {
  451. return true;
  452. }
  453. return false;
  454. }
  455. char* XMLDocument::Identify( char* p, XMLNode** node )
  456. {
  457. TIXMLASSERT( node );
  458. TIXMLASSERT( p );
  459. char* const start = p;
  460. p = XMLUtil::SkipWhiteSpace( p );
  461. if( !*p ) {
  462. *node = 0;
  463. TIXMLASSERT( p );
  464. return p;
  465. }
  466. // What is this thing?
  467. // These strings define the matching patters:
  468. static const char* xmlHeader = { "<?" };
  469. static const char* commentHeader = { "<!--" };
  470. static const char* dtdHeader = { "<!" };
  471. static const char* cdataHeader = { "<![CDATA[" };
  472. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  473. static const int xmlHeaderLen = 2;
  474. static const int commentHeaderLen = 4;
  475. static const int dtdHeaderLen = 2;
  476. static const int cdataHeaderLen = 9;
  477. static const int elementHeaderLen = 1;
  478. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  479. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  480. XMLNode* returnNode = 0;
  481. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  482. TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );
  483. returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
  484. returnNode->_memPool = &_commentPool;
  485. p += xmlHeaderLen;
  486. }
  487. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  488. TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );
  489. returnNode = new (_commentPool.Alloc()) XMLComment( this );
  490. returnNode->_memPool = &_commentPool;
  491. p += commentHeaderLen;
  492. }
  493. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  494. TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
  495. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  496. returnNode = text;
  497. returnNode->_memPool = &_textPool;
  498. p += cdataHeaderLen;
  499. text->SetCData( true );
  500. }
  501. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  502. TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );
  503. returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
  504. returnNode->_memPool = &_commentPool;
  505. p += dtdHeaderLen;
  506. }
  507. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  508. TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );
  509. returnNode = new (_elementPool.Alloc()) XMLElement( this );
  510. returnNode->_memPool = &_elementPool;
  511. p += elementHeaderLen;
  512. }
  513. else {
  514. TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
  515. returnNode = new (_textPool.Alloc()) XMLText( this );
  516. returnNode->_memPool = &_textPool;
  517. p = start; // Back it up, all the text counts.
  518. }
  519. TIXMLASSERT( returnNode );
  520. TIXMLASSERT( p );
  521. *node = returnNode;
  522. return p;
  523. }
  524. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  525. {
  526. TIXMLASSERT( visitor );
  527. if ( visitor->VisitEnter( *this ) ) {
  528. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  529. if ( !node->Accept( visitor ) ) {
  530. break;
  531. }
  532. }
  533. }
  534. return visitor->VisitExit( *this );
  535. }
  536. // --------- XMLNode ----------- //
  537. XMLNode::XMLNode( XMLDocument* doc ) :
  538. _document( doc ),
  539. _parent( 0 ),
  540. _firstChild( 0 ), _lastChild( 0 ),
  541. _prev( 0 ), _next( 0 ),
  542. _memPool( 0 )
  543. {
  544. }
  545. XMLNode::~XMLNode()
  546. {
  547. DeleteChildren();
  548. if ( _parent ) {
  549. _parent->Unlink( this );
  550. }
  551. }
  552. const char* XMLNode::Value() const
  553. {
  554. return _value.GetStr();
  555. }
  556. void XMLNode::SetValue( const char* str, bool staticMem )
  557. {
  558. if ( staticMem ) {
  559. _value.SetInternedStr( str );
  560. }
  561. else {
  562. _value.SetStr( str );
  563. }
  564. }
  565. void XMLNode::DeleteChildren()
  566. {
  567. while( _firstChild ) {
  568. TIXMLASSERT( _firstChild->_document == _document );
  569. XMLNode* node = _firstChild;
  570. Unlink( node );
  571. DeleteNode( node );
  572. }
  573. _firstChild = _lastChild = 0;
  574. }
  575. void XMLNode::Unlink( XMLNode* child )
  576. {
  577. TIXMLASSERT( child );
  578. TIXMLASSERT( child->_document == _document );
  579. if ( child == _firstChild ) {
  580. _firstChild = _firstChild->_next;
  581. }
  582. if ( child == _lastChild ) {
  583. _lastChild = _lastChild->_prev;
  584. }
  585. if ( child->_prev ) {
  586. child->_prev->_next = child->_next;
  587. }
  588. if ( child->_next ) {
  589. child->_next->_prev = child->_prev;
  590. }
  591. child->_parent = 0;
  592. }
  593. void XMLNode::DeleteChild( XMLNode* node )
  594. {
  595. TIXMLASSERT( node );
  596. TIXMLASSERT( node->_document == _document );
  597. TIXMLASSERT( node->_parent == this );
  598. DeleteNode( node );
  599. }
  600. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  601. {
  602. TIXMLASSERT( addThis );
  603. if ( addThis->_document != _document ) {
  604. TIXMLASSERT( false );
  605. return 0;
  606. }
  607. InsertChildPreamble( addThis );
  608. if ( _lastChild ) {
  609. TIXMLASSERT( _firstChild );
  610. TIXMLASSERT( _lastChild->_next == 0 );
  611. _lastChild->_next = addThis;
  612. addThis->_prev = _lastChild;
  613. _lastChild = addThis;
  614. addThis->_next = 0;
  615. }
  616. else {
  617. TIXMLASSERT( _firstChild == 0 );
  618. _firstChild = _lastChild = addThis;
  619. addThis->_prev = 0;
  620. addThis->_next = 0;
  621. }
  622. addThis->_parent = this;
  623. return addThis;
  624. }
  625. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  626. {
  627. TIXMLASSERT( addThis );
  628. if ( addThis->_document != _document ) {
  629. TIXMLASSERT( false );
  630. return 0;
  631. }
  632. InsertChildPreamble( addThis );
  633. if ( _firstChild ) {
  634. TIXMLASSERT( _lastChild );
  635. TIXMLASSERT( _firstChild->_prev == 0 );
  636. _firstChild->_prev = addThis;
  637. addThis->_next = _firstChild;
  638. _firstChild = addThis;
  639. addThis->_prev = 0;
  640. }
  641. else {
  642. TIXMLASSERT( _lastChild == 0 );
  643. _firstChild = _lastChild = addThis;
  644. addThis->_prev = 0;
  645. addThis->_next = 0;
  646. }
  647. addThis->_parent = this;
  648. return addThis;
  649. }
  650. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  651. {
  652. TIXMLASSERT( addThis );
  653. if ( addThis->_document != _document ) {
  654. TIXMLASSERT( false );
  655. return 0;
  656. }
  657. TIXMLASSERT( afterThis );
  658. if ( afterThis->_parent != this ) {
  659. TIXMLASSERT( false );
  660. return 0;
  661. }
  662. if ( afterThis->_next == 0 ) {
  663. // The last node or the only node.
  664. return InsertEndChild( addThis );
  665. }
  666. InsertChildPreamble( addThis );
  667. addThis->_prev = afterThis;
  668. addThis->_next = afterThis->_next;
  669. afterThis->_next->_prev = addThis;
  670. afterThis->_next = addThis;
  671. addThis->_parent = this;
  672. return addThis;
  673. }
  674. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  675. {
  676. for( XMLNode* node=_firstChild; node; node=node->_next ) {
  677. XMLElement* element = node->ToElement();
  678. if ( element ) {
  679. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  680. return element;
  681. }
  682. }
  683. }
  684. return 0;
  685. }
  686. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  687. {
  688. for( XMLNode* node=_lastChild; node; node=node->_prev ) {
  689. XMLElement* element = node->ToElement();
  690. if ( element ) {
  691. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  692. return element;
  693. }
  694. }
  695. }
  696. return 0;
  697. }
  698. const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
  699. {
  700. for( XMLNode* node=this->_next; node; node = node->_next ) {
  701. const XMLElement* element = node->ToElement();
  702. if ( element
  703. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  704. return element;
  705. }
  706. }
  707. return 0;
  708. }
  709. const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
  710. {
  711. for( XMLNode* node=_prev; node; node = node->_prev ) {
  712. const XMLElement* element = node->ToElement();
  713. if ( element
  714. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  715. return element;
  716. }
  717. }
  718. return 0;
  719. }
  720. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  721. {
  722. // This is a recursive method, but thinking about it "at the current level"
  723. // it is a pretty simple flat list:
  724. // <foo/>
  725. // <!-- comment -->
  726. //
  727. // With a special case:
  728. // <foo>
  729. // </foo>
  730. // <!-- comment -->
  731. //
  732. // Where the closing element (/foo) *must* be the next thing after the opening
  733. // element, and the names must match. BUT the tricky bit is that the closing
  734. // element will be read by the child.
  735. //
  736. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  737. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  738. while( p && *p ) {
  739. XMLNode* node = 0;
  740. p = _document->Identify( p, &node );
  741. if ( node == 0 ) {
  742. break;
  743. }
  744. StrPair endTag;
  745. p = node->ParseDeep( p, &endTag );
  746. if ( !p ) {
  747. DeleteNode( node );
  748. if ( !_document->Error() ) {
  749. _document->SetError( XML_ERROR_PARSING, 0, 0 );
  750. }
  751. break;
  752. }
  753. XMLElement* ele = node->ToElement();
  754. if ( ele ) {
  755. // We read the end tag. Return it to the parent.
  756. if ( ele->ClosingType() == XMLElement::CLOSING ) {
  757. if ( parentEnd ) {
  758. ele->_value.TransferTo( parentEnd );
  759. }
  760. node->_memPool->SetTracked(); // created and then immediately deleted.
  761. DeleteNode( node );
  762. return p;
  763. }
  764. // Handle an end tag returned to this level.
  765. // And handle a bunch of annoying errors.
  766. bool mismatch = false;
  767. if ( endTag.Empty() ) {
  768. if ( ele->ClosingType() == XMLElement::OPEN ) {
  769. mismatch = true;
  770. }
  771. }
  772. else {
  773. if ( ele->ClosingType() != XMLElement::OPEN ) {
  774. mismatch = true;
  775. }
  776. else if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() ) ) {
  777. mismatch = true;
  778. }
  779. }
  780. if ( mismatch ) {
  781. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  782. DeleteNode( node );
  783. break;
  784. }
  785. }
  786. InsertEndChild( node );
  787. }
  788. return 0;
  789. }
  790. void XMLNode::DeleteNode( XMLNode* node )
  791. {
  792. if ( node == 0 ) {
  793. return;
  794. }
  795. MemPool* pool = node->_memPool;
  796. node->~XMLNode();
  797. pool->Free( node );
  798. }
  799. void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const
  800. {
  801. TIXMLASSERT( insertThis );
  802. TIXMLASSERT( insertThis->_document == _document );
  803. if ( insertThis->_parent )
  804. insertThis->_parent->Unlink( insertThis );
  805. else
  806. insertThis->_memPool->SetTracked();
  807. }
  808. // --------- XMLText ---------- //
  809. char* XMLText::ParseDeep( char* p, StrPair* )
  810. {
  811. const char* start = p;
  812. if ( this->CData() ) {
  813. p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  814. if ( !p ) {
  815. _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
  816. }
  817. return p;
  818. }
  819. else {
  820. int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
  821. if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
  822. flags |= StrPair::COLLAPSE_WHITESPACE;
  823. }
  824. p = _value.ParseText( p, "<", flags );
  825. if ( p && *p ) {
  826. return p-1;
  827. }
  828. if ( !p ) {
  829. _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
  830. }
  831. }
  832. return 0;
  833. }
  834. XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
  835. {
  836. if ( !doc ) {
  837. doc = _document;
  838. }
  839. XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
  840. text->SetCData( this->CData() );
  841. return text;
  842. }
  843. bool XMLText::ShallowEqual( const XMLNode* compare ) const
  844. {
  845. const XMLText* text = compare->ToText();
  846. return ( text && XMLUtil::StringEqual( text->Value(), Value() ) );
  847. }
  848. bool XMLText::Accept( XMLVisitor* visitor ) const
  849. {
  850. TIXMLASSERT( visitor );
  851. return visitor->Visit( *this );
  852. }
  853. // --------- XMLComment ---------- //
  854. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  855. {
  856. }
  857. XMLComment::~XMLComment()
  858. {
  859. }
  860. char* XMLComment::ParseDeep( char* p, StrPair* )
  861. {
  862. // Comment parses as text.
  863. const char* start = p;
  864. p = _value.ParseText( p, "-->", StrPair::COMMENT );
  865. if ( p == 0 ) {
  866. _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
  867. }
  868. return p;
  869. }
  870. XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
  871. {
  872. if ( !doc ) {
  873. doc = _document;
  874. }
  875. XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
  876. return comment;
  877. }
  878. bool XMLComment::ShallowEqual( const XMLNode* compare ) const
  879. {
  880. TIXMLASSERT( compare );
  881. const XMLComment* comment = compare->ToComment();
  882. return ( comment && XMLUtil::StringEqual( comment->Value(), Value() ));
  883. }
  884. bool XMLComment::Accept( XMLVisitor* visitor ) const
  885. {
  886. TIXMLASSERT( visitor );
  887. return visitor->Visit( *this );
  888. }
  889. // --------- XMLDeclaration ---------- //
  890. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  891. {
  892. }
  893. XMLDeclaration::~XMLDeclaration()
  894. {
  895. //printf( "~XMLDeclaration\n" );
  896. }
  897. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  898. {
  899. // Declaration parses as text.
  900. const char* start = p;
  901. p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  902. if ( p == 0 ) {
  903. _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
  904. }
  905. return p;
  906. }
  907. XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
  908. {
  909. if ( !doc ) {
  910. doc = _document;
  911. }
  912. XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
  913. return dec;
  914. }
  915. bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
  916. {
  917. TIXMLASSERT( compare );
  918. const XMLDeclaration* declaration = compare->ToDeclaration();
  919. return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() ));
  920. }
  921. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  922. {
  923. TIXMLASSERT( visitor );
  924. return visitor->Visit( *this );
  925. }
  926. // --------- XMLUnknown ---------- //
  927. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  928. {
  929. }
  930. XMLUnknown::~XMLUnknown()
  931. {
  932. }
  933. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  934. {
  935. // Unknown parses as text.
  936. const char* start = p;
  937. p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  938. if ( !p ) {
  939. _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
  940. }
  941. return p;
  942. }
  943. XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
  944. {
  945. if ( !doc ) {
  946. doc = _document;
  947. }
  948. XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
  949. return text;
  950. }
  951. bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
  952. {
  953. TIXMLASSERT( compare );
  954. const XMLUnknown* unknown = compare->ToUnknown();
  955. return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() ));
  956. }
  957. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  958. {
  959. TIXMLASSERT( visitor );
  960. return visitor->Visit( *this );
  961. }
  962. // --------- XMLAttribute ---------- //
  963. const char* XMLAttribute::Name() const
  964. {
  965. return _name.GetStr();
  966. }
  967. const char* XMLAttribute::Value() const
  968. {
  969. return _value.GetStr();
  970. }
  971. char* XMLAttribute::ParseDeep( char* p, bool processEntities )
  972. {
  973. // Parse using the name rules: bug fix, was using ParseText before
  974. p = _name.ParseName( p );
  975. if ( !p || !*p ) {
  976. return 0;
  977. }
  978. // Skip white space before =
  979. p = XMLUtil::SkipWhiteSpace( p );
  980. if ( *p != '=' ) {
  981. return 0;
  982. }
  983. ++p; // move up to opening quote
  984. p = XMLUtil::SkipWhiteSpace( p );
  985. if ( *p != '\"' && *p != '\'' ) {
  986. return 0;
  987. }
  988. char endTag[2] = { *p, 0 };
  989. ++p; // move past opening quote
  990. p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
  991. return p;
  992. }
  993. void XMLAttribute::SetName( const char* n )
  994. {
  995. _name.SetStr( n );
  996. }
  997. XMLError XMLAttribute::QueryIntValue( int* value ) const
  998. {
  999. if ( XMLUtil::ToInt( Value(), value )) {
  1000. return XML_NO_ERROR;
  1001. }
  1002. return XML_WRONG_ATTRIBUTE_TYPE;
  1003. }
  1004. XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
  1005. {
  1006. if ( XMLUtil::ToUnsigned( Value(), value )) {
  1007. return XML_NO_ERROR;
  1008. }
  1009. return XML_WRONG_ATTRIBUTE_TYPE;
  1010. }
  1011. XMLError XMLAttribute::QueryBoolValue( bool* value ) const
  1012. {
  1013. if ( XMLUtil::ToBool( Value(), value )) {
  1014. return XML_NO_ERROR;
  1015. }
  1016. return XML_WRONG_ATTRIBUTE_TYPE;
  1017. }
  1018. XMLError XMLAttribute::QueryFloatValue( float* value ) const
  1019. {
  1020. if ( XMLUtil::ToFloat( Value(), value )) {
  1021. return XML_NO_ERROR;
  1022. }
  1023. return XML_WRONG_ATTRIBUTE_TYPE;
  1024. }
  1025. XMLError XMLAttribute::QueryDoubleValue( double* value ) const
  1026. {
  1027. if ( XMLUtil::ToDouble( Value(), value )) {
  1028. return XML_NO_ERROR;
  1029. }
  1030. return XML_WRONG_ATTRIBUTE_TYPE;
  1031. }
  1032. void XMLAttribute::SetAttribute( const char* v )
  1033. {
  1034. _value.SetStr( v );
  1035. }
  1036. void XMLAttribute::SetAttribute( int v )
  1037. {
  1038. char buf[BUF_SIZE];
  1039. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1040. _value.SetStr( buf );
  1041. }
  1042. void XMLAttribute::SetAttribute( unsigned v )
  1043. {
  1044. char buf[BUF_SIZE];
  1045. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1046. _value.SetStr( buf );
  1047. }
  1048. void XMLAttribute::SetAttribute( bool v )
  1049. {
  1050. char buf[BUF_SIZE];
  1051. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1052. _value.SetStr( buf );
  1053. }
  1054. void XMLAttribute::SetAttribute( double v )
  1055. {
  1056. char buf[BUF_SIZE];
  1057. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1058. _value.SetStr( buf );
  1059. }
  1060. void XMLAttribute::SetAttribute( float v )
  1061. {
  1062. char buf[BUF_SIZE];
  1063. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1064. _value.SetStr( buf );
  1065. }
  1066. // --------- XMLElement ---------- //
  1067. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  1068. _closingType( 0 ),
  1069. _rootAttribute( 0 )
  1070. {
  1071. }
  1072. XMLElement::~XMLElement()
  1073. {
  1074. while( _rootAttribute ) {
  1075. XMLAttribute* next = _rootAttribute->_next;
  1076. DeleteAttribute( _rootAttribute );
  1077. _rootAttribute = next;
  1078. }
  1079. }
  1080. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  1081. {
  1082. for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
  1083. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  1084. return a;
  1085. }
  1086. }
  1087. return 0;
  1088. }
  1089. const char* XMLElement::Attribute( const char* name, const char* value ) const
  1090. {
  1091. const XMLAttribute* a = FindAttribute( name );
  1092. if ( !a ) {
  1093. return 0;
  1094. }
  1095. if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
  1096. return a->Value();
  1097. }
  1098. return 0;
  1099. }
  1100. const char* XMLElement::GetText() const
  1101. {
  1102. if ( FirstChild() && FirstChild()->ToText() ) {
  1103. return FirstChild()->Value();
  1104. }
  1105. return 0;
  1106. }
  1107. void XMLElement::SetText( const char* inText )
  1108. {
  1109. if ( FirstChild() && FirstChild()->ToText() )
  1110. FirstChild()->SetValue( inText );
  1111. else {
  1112. XMLText* theText = GetDocument()->NewText( inText );
  1113. InsertFirstChild( theText );
  1114. }
  1115. }
  1116. void XMLElement::SetText( int v )
  1117. {
  1118. char buf[BUF_SIZE];
  1119. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1120. SetText( buf );
  1121. }
  1122. void XMLElement::SetText( unsigned v )
  1123. {
  1124. char buf[BUF_SIZE];
  1125. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1126. SetText( buf );
  1127. }
  1128. void XMLElement::SetText( bool v )
  1129. {
  1130. char buf[BUF_SIZE];
  1131. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1132. SetText( buf );
  1133. }
  1134. void XMLElement::SetText( float v )
  1135. {
  1136. char buf[BUF_SIZE];
  1137. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1138. SetText( buf );
  1139. }
  1140. void XMLElement::SetText( double v )
  1141. {
  1142. char buf[BUF_SIZE];
  1143. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1144. SetText( buf );
  1145. }
  1146. XMLError XMLElement::QueryIntText( int* ival ) const
  1147. {
  1148. if ( FirstChild() && FirstChild()->ToText() ) {
  1149. const char* t = FirstChild()->Value();
  1150. if ( XMLUtil::ToInt( t, ival ) ) {
  1151. return XML_SUCCESS;
  1152. }
  1153. return XML_CAN_NOT_CONVERT_TEXT;
  1154. }
  1155. return XML_NO_TEXT_NODE;
  1156. }
  1157. XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
  1158. {
  1159. if ( FirstChild() && FirstChild()->ToText() ) {
  1160. const char* t = FirstChild()->Value();
  1161. if ( XMLUtil::ToUnsigned( t, uval ) ) {
  1162. return XML_SUCCESS;
  1163. }
  1164. return XML_CAN_NOT_CONVERT_TEXT;
  1165. }
  1166. return XML_NO_TEXT_NODE;
  1167. }
  1168. XMLError XMLElement::QueryBoolText( bool* bval ) const
  1169. {
  1170. if ( FirstChild() && FirstChild()->ToText() ) {
  1171. const char* t = FirstChild()->Value();
  1172. if ( XMLUtil::ToBool( t, bval ) ) {
  1173. return XML_SUCCESS;
  1174. }
  1175. return XML_CAN_NOT_CONVERT_TEXT;
  1176. }
  1177. return XML_NO_TEXT_NODE;
  1178. }
  1179. XMLError XMLElement::QueryDoubleText( double* dval ) const
  1180. {
  1181. if ( FirstChild() && FirstChild()->ToText() ) {
  1182. const char* t = FirstChild()->Value();
  1183. if ( XMLUtil::ToDouble( t, dval ) ) {
  1184. return XML_SUCCESS;
  1185. }
  1186. return XML_CAN_NOT_CONVERT_TEXT;
  1187. }
  1188. return XML_NO_TEXT_NODE;
  1189. }
  1190. XMLError XMLElement::QueryFloatText( float* fval ) const
  1191. {
  1192. if ( FirstChild() && FirstChild()->ToText() ) {
  1193. const char* t = FirstChild()->Value();
  1194. if ( XMLUtil::ToFloat( t, fval ) ) {
  1195. return XML_SUCCESS;
  1196. }
  1197. return XML_CAN_NOT_CONVERT_TEXT;
  1198. }
  1199. return XML_NO_TEXT_NODE;
  1200. }
  1201. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  1202. {
  1203. XMLAttribute* last = 0;
  1204. XMLAttribute* attrib = 0;
  1205. for( attrib = _rootAttribute;
  1206. attrib;
  1207. last = attrib, attrib = attrib->_next ) {
  1208. if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
  1209. break;
  1210. }
  1211. }
  1212. if ( !attrib ) {
  1213. TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
  1214. attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1215. attrib->_memPool = &_document->_attributePool;
  1216. if ( last ) {
  1217. last->_next = attrib;
  1218. }
  1219. else {
  1220. _rootAttribute = attrib;
  1221. }
  1222. attrib->SetName( name );
  1223. attrib->_memPool->SetTracked(); // always created and linked.
  1224. }
  1225. return attrib;
  1226. }
  1227. void XMLElement::DeleteAttribute( const char* name )
  1228. {
  1229. XMLAttribute* prev = 0;
  1230. for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
  1231. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  1232. if ( prev ) {
  1233. prev->_next = a->_next;
  1234. }
  1235. else {
  1236. _rootAttribute = a->_next;
  1237. }
  1238. DeleteAttribute( a );
  1239. break;
  1240. }
  1241. prev = a;
  1242. }
  1243. }
  1244. char* XMLElement::ParseAttributes( char* p )
  1245. {
  1246. const char* start = p;
  1247. XMLAttribute* prevAttribute = 0;
  1248. // Read the attributes.
  1249. while( p ) {
  1250. p = XMLUtil::SkipWhiteSpace( p );
  1251. if ( !(*p) ) {
  1252. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
  1253. return 0;
  1254. }
  1255. // attribute.
  1256. if (XMLUtil::IsNameStartChar( *p ) ) {
  1257. TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
  1258. XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1259. attrib->_memPool = &_document->_attributePool;
  1260. attrib->_memPool->SetTracked();
  1261. p = attrib->ParseDeep( p, _document->ProcessEntities() );
  1262. if ( !p || Attribute( attrib->Name() ) ) {
  1263. DeleteAttribute( attrib );
  1264. _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
  1265. return 0;
  1266. }
  1267. // There is a minor bug here: if the attribute in the source xml
  1268. // document is duplicated, it will not be detected and the
  1269. // attribute will be doubly added. However, tracking the 'prevAttribute'
  1270. // avoids re-scanning the attribute list. Preferring performance for
  1271. // now, may reconsider in the future.
  1272. if ( prevAttribute ) {
  1273. prevAttribute->_next = attrib;
  1274. }
  1275. else {
  1276. _rootAttribute = attrib;
  1277. }
  1278. prevAttribute = attrib;
  1279. }
  1280. // end of the tag
  1281. else if ( *p == '/' && *(p+1) == '>' ) {
  1282. _closingType = CLOSED;
  1283. return p+2; // done; sealed element.
  1284. }
  1285. // end of the tag
  1286. else if ( *p == '>' ) {
  1287. ++p;
  1288. break;
  1289. }
  1290. else {
  1291. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
  1292. return 0;
  1293. }
  1294. }
  1295. return p;
  1296. }
  1297. void XMLElement::DeleteAttribute( XMLAttribute* attribute )
  1298. {
  1299. if ( attribute == 0 ) {
  1300. return;
  1301. }
  1302. MemPool* pool = attribute->_memPool;
  1303. attribute->~XMLAttribute();
  1304. pool->Free( attribute );
  1305. }
  1306. //
  1307. // <ele></ele>
  1308. // <ele>foo<b>bar</b></ele>
  1309. //
  1310. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  1311. {
  1312. // Read the element name.
  1313. p = XMLUtil::SkipWhiteSpace( p );
  1314. // The closing element is the </element> form. It is
  1315. // parsed just like a regular element then deleted from
  1316. // the DOM.
  1317. if ( *p == '/' ) {
  1318. _closingType = CLOSING;
  1319. ++p;
  1320. }
  1321. p = _value.ParseName( p );
  1322. if ( _value.Empty() ) {
  1323. return 0;
  1324. }
  1325. p = ParseAttributes( p );
  1326. if ( !p || !*p || _closingType ) {
  1327. return p;
  1328. }
  1329. p = XMLNode::ParseDeep( p, strPair );
  1330. return p;
  1331. }
  1332. XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
  1333. {
  1334. if ( !doc ) {
  1335. doc = _document;
  1336. }
  1337. XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
  1338. for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
  1339. element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
  1340. }
  1341. return element;
  1342. }
  1343. bool XMLElement::ShallowEqual( const XMLNode* compare ) const
  1344. {
  1345. TIXMLASSERT( compare );
  1346. const XMLElement* other = compare->ToElement();
  1347. if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
  1348. const XMLAttribute* a=FirstAttribute();
  1349. const XMLAttribute* b=other->FirstAttribute();
  1350. while ( a && b ) {
  1351. if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
  1352. return false;
  1353. }
  1354. a = a->Next();
  1355. b = b->Next();
  1356. }
  1357. if ( a || b ) {
  1358. // different count
  1359. return false;
  1360. }
  1361. return true;
  1362. }
  1363. return false;
  1364. }
  1365. bool XMLElement::Accept( XMLVisitor* visitor ) const
  1366. {
  1367. TIXMLASSERT( visitor );
  1368. if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
  1369. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  1370. if ( !node->Accept( visitor ) ) {
  1371. break;
  1372. }
  1373. }
  1374. }
  1375. return visitor->VisitExit( *this );
  1376. }
  1377. // --------- XMLDocument ----------- //
  1378. // Warning: List must match 'enum XMLError'
  1379. const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = {
  1380. "XML_SUCCESS",
  1381. "XML_NO_ATTRIBUTE",
  1382. "XML_WRONG_ATTRIBUTE_TYPE",
  1383. "XML_ERROR_FILE_NOT_FOUND",
  1384. "XML_ERROR_FILE_COULD_NOT_BE_OPENED",
  1385. "XML_ERROR_FILE_READ_ERROR",
  1386. "XML_ERROR_ELEMENT_MISMATCH",
  1387. "XML_ERROR_PARSING_ELEMENT",
  1388. "XML_ERROR_PARSING_ATTRIBUTE",
  1389. "XML_ERROR_IDENTIFYING_TAG",
  1390. "XML_ERROR_PARSING_TEXT",
  1391. "XML_ERROR_PARSING_CDATA",
  1392. "XML_ERROR_PARSING_COMMENT",
  1393. "XML_ERROR_PARSING_DECLARATION",
  1394. "XML_ERROR_PARSING_UNKNOWN",
  1395. "XML_ERROR_EMPTY_DOCUMENT",
  1396. "XML_ERROR_MISMATCHED_ELEMENT",
  1397. "XML_ERROR_PARSING",
  1398. "XML_CAN_NOT_CONVERT_TEXT",
  1399. "XML_NO_TEXT_NODE"
  1400. };
  1401. XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
  1402. XMLNode( 0 ),
  1403. _writeBOM( false ),
  1404. _processEntities( processEntities ),
  1405. _errorID( XML_NO_ERROR ),
  1406. _whitespace( whitespace ),
  1407. _errorStr1( 0 ),
  1408. _errorStr2( 0 ),
  1409. _charBuffer( 0 )
  1410. {
  1411. _document = this; // avoid warning about 'this' in initializer list
  1412. }
  1413. XMLDocument::~XMLDocument()
  1414. {
  1415. Clear();
  1416. }
  1417. void XMLDocument::Clear()
  1418. {
  1419. DeleteChildren();
  1420. #ifdef DEBUG
  1421. const bool hadError = Error();
  1422. #endif
  1423. _errorID = XML_NO_ERROR;
  1424. _errorStr1 = 0;
  1425. _errorStr2 = 0;
  1426. delete [] _charBuffer;
  1427. _charBuffer = 0;
  1428. #if 0
  1429. _textPool.Trace( "text" );
  1430. _elementPool.Trace( "element" );
  1431. _commentPool.Trace( "comment" );
  1432. _attributePool.Trace( "attribute" );
  1433. #endif
  1434. #ifdef DEBUG
  1435. if ( !hadError ) {
  1436. TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
  1437. TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
  1438. TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
  1439. TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
  1440. }
  1441. #endif
  1442. }
  1443. XMLElement* XMLDocument::NewElement( const char* name )
  1444. {
  1445. TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );
  1446. XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
  1447. ele->_memPool = &_elementPool;
  1448. ele->SetName( name );
  1449. return ele;
  1450. }
  1451. XMLComment* XMLDocument::NewComment( const char* str )
  1452. {
  1453. TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );
  1454. XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
  1455. comment->_memPool = &_commentPool;
  1456. comment->SetValue( str );
  1457. return comment;
  1458. }
  1459. XMLText* XMLDocument::NewText( const char* str )
  1460. {
  1461. TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
  1462. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  1463. text->_memPool = &_textPool;
  1464. text->SetValue( str );
  1465. return text;
  1466. }
  1467. XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
  1468. {
  1469. TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );
  1470. XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
  1471. dec->_memPool = &_commentPool;
  1472. dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
  1473. return dec;
  1474. }
  1475. XMLUnknown* XMLDocument::NewUnknown( const char* str )
  1476. {
  1477. TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );
  1478. XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
  1479. unk->_memPool = &_commentPool;
  1480. unk->SetValue( str );
  1481. return unk;
  1482. }
  1483. static FILE* callfopen( const char* filepath, const char* mode )
  1484. {
  1485. TIXMLASSERT( filepath );
  1486. TIXMLASSERT( mode );
  1487. #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
  1488. FILE* fp = 0;
  1489. errno_t err = fopen_s( &fp, filepath, mode );
  1490. if ( err ) {
  1491. return 0;
  1492. }
  1493. #else
  1494. FILE* fp = fopen( filepath, mode );
  1495. #endif
  1496. return fp;
  1497. }
  1498. void XMLDocument::DeleteNode( XMLNode* node ) {
  1499. TIXMLASSERT( node );
  1500. TIXMLASSERT(node->_document == this );
  1501. if (node->_parent) {
  1502. node->_parent->DeleteChild( node );
  1503. }
  1504. else {
  1505. // Isn't in the tree.
  1506. // Use the parent delete.
  1507. // Also, we need to mark it tracked: we 'know'
  1508. // it was never used.
  1509. node->_memPool->SetTracked();
  1510. // Call the static XMLNode version:
  1511. XMLNode::DeleteNode(node);
  1512. }
  1513. }
  1514. XMLError XMLDocument::LoadFile( const char* filename )
  1515. {
  1516. Clear();
  1517. FILE* fp = callfopen( filename, "rb" );
  1518. if ( !fp ) {
  1519. SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
  1520. return _errorID;
  1521. }
  1522. LoadFile( fp );
  1523. fclose( fp );
  1524. return _errorID;
  1525. }
  1526. XMLError XMLDocument::LoadFile( FILE* fp )
  1527. {
  1528. Clear();
  1529. fseek( fp, 0, SEEK_SET );
  1530. if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
  1531. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1532. return _errorID;
  1533. }
  1534. fseek( fp, 0, SEEK_END );
  1535. const long filelength = ftell( fp );
  1536. fseek( fp, 0, SEEK_SET );
  1537. if ( filelength == -1L ) {
  1538. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1539. return _errorID;
  1540. }
  1541. const size_t size = filelength;
  1542. if ( size == 0 ) {
  1543. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1544. return _errorID;
  1545. }
  1546. _charBuffer = new char[size+1];
  1547. size_t read = fread( _charBuffer, 1, size, fp );
  1548. if ( read != size ) {
  1549. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1550. return _errorID;
  1551. }
  1552. _charBuffer[size] = 0;
  1553. Parse();
  1554. return _errorID;
  1555. }
  1556. XMLError XMLDocument::SaveFile( const char* filename, bool compact )
  1557. {
  1558. FILE* fp = callfopen( filename, "w" );
  1559. if ( !fp ) {
  1560. SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
  1561. return _errorID;
  1562. }
  1563. SaveFile(fp, compact);
  1564. fclose( fp );
  1565. return _errorID;
  1566. }
  1567. XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
  1568. {
  1569. // Clear any error from the last save, otherwise it will get reported
  1570. // for *this* call.
  1571. SetError( XML_NO_ERROR, 0, 0 );
  1572. XMLPrinter stream( fp, compact );
  1573. Print( &stream );
  1574. return _errorID;
  1575. }
  1576. XMLError XMLDocument::Parse( const char* p, size_t len )
  1577. {
  1578. Clear();
  1579. if ( len == 0 || !p || !*p ) {
  1580. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1581. return _errorID;
  1582. }
  1583. if ( len == (size_t)(-1) ) {
  1584. len = strlen( p );
  1585. }
  1586. _charBuffer = new char[ len+1 ];
  1587. memcpy( _charBuffer, p, len );
  1588. _charBuffer[len] = 0;
  1589. Parse();
  1590. if ( Error() ) {
  1591. // clean up now essentially dangling memory.
  1592. // and the parse fail can put objects in the
  1593. // pools that are dead and inaccessible.
  1594. DeleteChildren();
  1595. _elementPool.Clear();
  1596. _attributePool.Clear();
  1597. _textPool.Clear();
  1598. _commentPool.Clear();
  1599. }
  1600. return _errorID;
  1601. }
  1602. void XMLDocument::Print( XMLPrinter* streamer ) const
  1603. {
  1604. XMLPrinter stdStreamer( stdout );
  1605. if ( !streamer ) {
  1606. streamer = &stdStreamer;
  1607. }
  1608. Accept( streamer );
  1609. }
  1610. void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
  1611. {
  1612. TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
  1613. _errorID = error;
  1614. _errorStr1 = str1;
  1615. _errorStr2 = str2;
  1616. }
  1617. const char* XMLDocument::ErrorName() const
  1618. {
  1619. TIXMLASSERT( _errorID >= 0 && _errorID < XML_ERROR_COUNT );
  1620. return _errorNames[_errorID];
  1621. }
  1622. void XMLDocument::PrintError() const
  1623. {
  1624. if ( Error() ) {
  1625. static const int LEN = 20;
  1626. char buf1[LEN] = { 0 };
  1627. char buf2[LEN] = { 0 };
  1628. if ( _errorStr1 ) {
  1629. TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
  1630. }
  1631. if ( _errorStr2 ) {
  1632. TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
  1633. }
  1634. printf( "XMLDocument error id=%d '%s' str1=%s str2=%s\n",
  1635. _errorID, ErrorName(), buf1, buf2 );
  1636. }
  1637. }
  1638. void XMLDocument::Parse()
  1639. {
  1640. TIXMLASSERT( NoChildren() ); // Clear() must have been called previously
  1641. TIXMLASSERT( _charBuffer );
  1642. char* p = _charBuffer;
  1643. p = XMLUtil::SkipWhiteSpace( p );
  1644. p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) );
  1645. if ( !*p ) {
  1646. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1647. return;
  1648. }
  1649. ParseDeep(p, 0 );
  1650. }
  1651. XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
  1652. _elementJustOpened( false ),
  1653. _firstElement( true ),
  1654. _fp( file ),
  1655. _depth( depth ),
  1656. _textDepth( -1 ),
  1657. _processEntities( true ),
  1658. _compactMode( compact )
  1659. {
  1660. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1661. _entityFlag[i] = false;
  1662. _restrictedEntityFlag[i] = false;
  1663. }
  1664. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1665. const char entityValue = entities[i].value;
  1666. TIXMLASSERT( 0 <= entityValue && entityValue < ENTITY_RANGE );
  1667. _entityFlag[ (unsigned char)entityValue ] = true;
  1668. }
  1669. _restrictedEntityFlag[(unsigned char)'&'] = true;
  1670. _restrictedEntityFlag[(unsigned char)'<'] = true;
  1671. _restrictedEntityFlag[(unsigned char)'>'] = true; // not required, but consistency is nice
  1672. _buffer.Push( 0 );
  1673. }
  1674. void XMLPrinter::Print( const char* format, ... )
  1675. {
  1676. va_list va;
  1677. va_start( va, format );
  1678. if ( _fp ) {
  1679. vfprintf( _fp, format, va );
  1680. }
  1681. else {
  1682. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1683. #if defined(WINCE)
  1684. int len = 512;
  1685. do {
  1686. len = len*2;
  1687. char* str = new char[len]();
  1688. len = _vsnprintf(str, len, format, va);
  1689. delete[] str;
  1690. }while (len < 0);
  1691. #else
  1692. int len = _vscprintf( format, va );
  1693. #endif
  1694. #else
  1695. int len = vsnprintf( 0, 0, format, va );
  1696. #endif
  1697. // Close out and re-start the va-args
  1698. va_end( va );
  1699. va_start( va, format );
  1700. TIXMLASSERT( _buffer.Size() > 0 && _buffer[_buffer.Size() - 1] == 0 );
  1701. char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator.
  1702. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1703. #if defined(WINCE)
  1704. _vsnprintf( p, len+1, format, va );
  1705. #else
  1706. vsnprintf_s( p, len+1, _TRUNCATE, format, va );
  1707. #endif
  1708. #else
  1709. vsnprintf( p, len+1, format, va );
  1710. #endif
  1711. }
  1712. va_end( va );
  1713. }
  1714. void XMLPrinter::PrintSpace( int depth )
  1715. {
  1716. for( int i=0; i<depth; ++i ) {
  1717. Print( " " );
  1718. }
  1719. }
  1720. void XMLPrinter::PrintString( const char* p, bool restricted )
  1721. {
  1722. // Look for runs of bytes between entities to print.
  1723. const char* q = p;
  1724. if ( _processEntities ) {
  1725. const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
  1726. while ( *q ) {
  1727. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1728. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1729. // Check for entities. If one is found, flush
  1730. // the stream up until the entity, write the
  1731. // entity, and keep looking.
  1732. if ( flag[(unsigned char)(*q)] ) {
  1733. while ( p < q ) {
  1734. Print( "%c", *p );
  1735. ++p;
  1736. }
  1737. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1738. if ( entities[i].value == *q ) {
  1739. Print( "&%s;", entities[i].pattern );
  1740. break;
  1741. }
  1742. }
  1743. ++p;
  1744. }
  1745. }
  1746. ++q;
  1747. }
  1748. }
  1749. // Flush the remaining string. This will be the entire
  1750. // string if an entity wasn't found.
  1751. if ( !_processEntities || (q-p > 0) ) {
  1752. Print( "%s", p );
  1753. }
  1754. }
  1755. void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
  1756. {
  1757. if ( writeBOM ) {
  1758. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1759. Print( "%s", bom );
  1760. }
  1761. if ( writeDec ) {
  1762. PushDeclaration( "xml version=\"1.0\"" );
  1763. }
  1764. }
  1765. void XMLPrinter::OpenElement( const char* name, bool compactMode )
  1766. {
  1767. SealElementIfJustOpened();
  1768. _stack.Push( name );
  1769. if ( _textDepth < 0 && !_firstElement && !compactMode ) {
  1770. Print( "\n" );
  1771. }
  1772. if ( !compactMode ) {
  1773. PrintSpace( _depth );
  1774. }
  1775. Print( "<%s", name );
  1776. _elementJustOpened = true;
  1777. _firstElement = false;
  1778. ++_depth;
  1779. }
  1780. void XMLPrinter::PushAttribute( const char* name, const char* value )
  1781. {
  1782. TIXMLASSERT( _elementJustOpened );
  1783. Print( " %s=\"", name );
  1784. PrintString( value, false );
  1785. Print( "\"" );
  1786. }
  1787. void XMLPrinter::PushAttribute( const char* name, int v )
  1788. {
  1789. char buf[BUF_SIZE];
  1790. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1791. PushAttribute( name, buf );
  1792. }
  1793. void XMLPrinter::PushAttribute( const char* name, unsigned v )
  1794. {
  1795. char buf[BUF_SIZE];
  1796. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1797. PushAttribute( name, buf );
  1798. }
  1799. void XMLPrinter::PushAttribute( const char* name, bool v )
  1800. {
  1801. char buf[BUF_SIZE];
  1802. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1803. PushAttribute( name, buf );
  1804. }
  1805. void XMLPrinter::PushAttribute( const char* name, double v )
  1806. {
  1807. char buf[BUF_SIZE];
  1808. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1809. PushAttribute( name, buf );
  1810. }
  1811. void XMLPrinter::CloseElement( bool compactMode )
  1812. {
  1813. --_depth;
  1814. const char* name = _stack.Pop();
  1815. if ( _elementJustOpened ) {
  1816. Print( "/>" );
  1817. }
  1818. else {
  1819. if ( _textDepth < 0 && !compactMode) {
  1820. Print( "\n" );
  1821. PrintSpace( _depth );
  1822. }
  1823. Print( "</%s>", name );
  1824. }
  1825. if ( _textDepth == _depth ) {
  1826. _textDepth = -1;
  1827. }
  1828. if ( _depth == 0 && !compactMode) {
  1829. Print( "\n" );
  1830. }
  1831. _elementJustOpened = false;
  1832. }
  1833. void XMLPrinter::SealElementIfJustOpened()
  1834. {
  1835. if ( !_elementJustOpened ) {
  1836. return;
  1837. }
  1838. _elementJustOpened = false;
  1839. Print( ">" );
  1840. }
  1841. void XMLPrinter::PushText( const char* text, bool cdata )
  1842. {
  1843. _textDepth = _depth-1;
  1844. SealElementIfJustOpened();
  1845. if ( cdata ) {
  1846. Print( "<![CDATA[" );
  1847. Print( "%s", text );
  1848. Print( "]]>" );
  1849. }
  1850. else {
  1851. PrintString( text, true );
  1852. }
  1853. }
  1854. void XMLPrinter::PushText( int value )
  1855. {
  1856. char buf[BUF_SIZE];
  1857. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1858. PushText( buf, false );
  1859. }
  1860. void XMLPrinter::PushText( unsigned value )
  1861. {
  1862. char buf[BUF_SIZE];
  1863. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1864. PushText( buf, false );
  1865. }
  1866. void XMLPrinter::PushText( bool value )
  1867. {
  1868. char buf[BUF_SIZE];
  1869. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1870. PushText( buf, false );
  1871. }
  1872. void XMLPrinter::PushText( float value )
  1873. {
  1874. char buf[BUF_SIZE];
  1875. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1876. PushText( buf, false );
  1877. }
  1878. void XMLPrinter::PushText( double value )
  1879. {
  1880. char buf[BUF_SIZE];
  1881. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1882. PushText( buf, false );
  1883. }
  1884. void XMLPrinter::PushComment( const char* comment )
  1885. {
  1886. SealElementIfJustOpened();
  1887. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1888. Print( "\n" );
  1889. PrintSpace( _depth );
  1890. }
  1891. _firstElement = false;
  1892. Print( "<!--%s-->", comment );
  1893. }
  1894. void XMLPrinter::PushDeclaration( const char* value )
  1895. {
  1896. SealElementIfJustOpened();
  1897. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1898. Print( "\n" );
  1899. PrintSpace( _depth );
  1900. }
  1901. _firstElement = false;
  1902. Print( "<?%s?>", value );
  1903. }
  1904. void XMLPrinter::PushUnknown( const char* value )
  1905. {
  1906. SealElementIfJustOpened();
  1907. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1908. Print( "\n" );
  1909. PrintSpace( _depth );
  1910. }
  1911. _firstElement = false;
  1912. Print( "<!%s>", value );
  1913. }
  1914. bool XMLPrinter::VisitEnter( const XMLDocument& doc )
  1915. {
  1916. _processEntities = doc.ProcessEntities();
  1917. if ( doc.HasBOM() ) {
  1918. PushHeader( true, false );
  1919. }
  1920. return true;
  1921. }
  1922. bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1923. {
  1924. const XMLElement* parentElem = NULL;
  1925. if ( element.Parent() ) {
  1926. parentElem = element.Parent()->ToElement();
  1927. }
  1928. bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode;
  1929. OpenElement( element.Name(), compactMode );
  1930. while ( attribute ) {
  1931. PushAttribute( attribute->Name(), attribute->Value() );
  1932. attribute = attribute->Next();
  1933. }
  1934. return true;
  1935. }
  1936. bool XMLPrinter::VisitExit( const XMLElement& element )
  1937. {
  1938. CloseElement( CompactMode(element) );
  1939. return true;
  1940. }
  1941. bool XMLPrinter::Visit( const XMLText& text )
  1942. {
  1943. PushText( text.Value(), text.CData() );
  1944. return true;
  1945. }
  1946. bool XMLPrinter::Visit( const XMLComment& comment )
  1947. {
  1948. PushComment( comment.Value() );
  1949. return true;
  1950. }
  1951. bool XMLPrinter::Visit( const XMLDeclaration& declaration )
  1952. {
  1953. PushDeclaration( declaration.Value() );
  1954. return true;
  1955. }
  1956. bool XMLPrinter::Visit( const XMLUnknown& unknown )
  1957. {
  1958. PushUnknown( unknown.Value() );
  1959. return true;
  1960. }
  1961. } // namespace tinyxml2