tinyxml2.cpp 50 KB

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