tinyxml2.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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, "%.15f", 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. void XMLNode::SetValue( const char* str, bool staticMem )
  506. {
  507. if ( staticMem ) {
  508. _value.SetInternedStr( str );
  509. }
  510. else {
  511. _value.SetStr( str );
  512. }
  513. }
  514. void XMLNode::DeleteChildren()
  515. {
  516. while( _firstChild ) {
  517. XMLNode* node = _firstChild;
  518. Unlink( node );
  519. DELETE_NODE( node );
  520. }
  521. _firstChild = _lastChild = 0;
  522. }
  523. void XMLNode::Unlink( XMLNode* child )
  524. {
  525. TIXMLASSERT( child->_parent == this );
  526. if ( child == _firstChild ) {
  527. _firstChild = _firstChild->_next;
  528. }
  529. if ( child == _lastChild ) {
  530. _lastChild = _lastChild->_prev;
  531. }
  532. if ( child->_prev ) {
  533. child->_prev->_next = child->_next;
  534. }
  535. if ( child->_next ) {
  536. child->_next->_prev = child->_prev;
  537. }
  538. child->_parent = 0;
  539. }
  540. void XMLNode::DeleteChild( XMLNode* node )
  541. {
  542. TIXMLASSERT( node->_parent == this );
  543. DELETE_NODE( node );
  544. }
  545. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  546. {
  547. if ( _lastChild ) {
  548. TIXMLASSERT( _firstChild );
  549. TIXMLASSERT( _lastChild->_next == 0 );
  550. _lastChild->_next = addThis;
  551. addThis->_prev = _lastChild;
  552. _lastChild = addThis;
  553. addThis->_next = 0;
  554. }
  555. else {
  556. TIXMLASSERT( _firstChild == 0 );
  557. _firstChild = _lastChild = addThis;
  558. addThis->_prev = 0;
  559. addThis->_next = 0;
  560. }
  561. addThis->_parent = this;
  562. addThis->_memPool->SetTracked();
  563. return addThis;
  564. }
  565. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  566. {
  567. if ( _firstChild ) {
  568. TIXMLASSERT( _lastChild );
  569. TIXMLASSERT( _firstChild->_prev == 0 );
  570. _firstChild->_prev = addThis;
  571. addThis->_next = _firstChild;
  572. _firstChild = addThis;
  573. addThis->_prev = 0;
  574. }
  575. else {
  576. TIXMLASSERT( _lastChild == 0 );
  577. _firstChild = _lastChild = addThis;
  578. addThis->_prev = 0;
  579. addThis->_next = 0;
  580. }
  581. addThis->_parent = this;
  582. addThis->_memPool->SetTracked();
  583. return addThis;
  584. }
  585. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  586. {
  587. TIXMLASSERT( afterThis->_parent == this );
  588. if ( afterThis->_parent != this ) {
  589. return 0;
  590. }
  591. if ( afterThis->_next == 0 ) {
  592. // The last node or the only node.
  593. return InsertEndChild( addThis );
  594. }
  595. addThis->_prev = afterThis;
  596. addThis->_next = afterThis->_next;
  597. afterThis->_next->_prev = addThis;
  598. afterThis->_next = addThis;
  599. addThis->_parent = this;
  600. addThis->_memPool->SetTracked();
  601. return addThis;
  602. }
  603. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  604. {
  605. for( XMLNode* node=_firstChild; node; node=node->_next ) {
  606. XMLElement* element = node->ToElement();
  607. if ( element ) {
  608. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  609. return element;
  610. }
  611. }
  612. }
  613. return 0;
  614. }
  615. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  616. {
  617. for( XMLNode* node=_lastChild; node; node=node->_prev ) {
  618. XMLElement* element = node->ToElement();
  619. if ( element ) {
  620. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  621. return element;
  622. }
  623. }
  624. }
  625. return 0;
  626. }
  627. const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
  628. {
  629. for( XMLNode* element=this->_next; element; element = element->_next ) {
  630. if ( element->ToElement()
  631. && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
  632. return element->ToElement();
  633. }
  634. }
  635. return 0;
  636. }
  637. const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
  638. {
  639. for( XMLNode* element=_prev; element; element = element->_prev ) {
  640. if ( element->ToElement()
  641. && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
  642. return element->ToElement();
  643. }
  644. }
  645. return 0;
  646. }
  647. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  648. {
  649. // This is a recursive method, but thinking about it "at the current level"
  650. // it is a pretty simple flat list:
  651. // <foo/>
  652. // <!-- comment -->
  653. //
  654. // With a special case:
  655. // <foo>
  656. // </foo>
  657. // <!-- comment -->
  658. //
  659. // Where the closing element (/foo) *must* be the next thing after the opening
  660. // element, and the names must match. BUT the tricky bit is that the closing
  661. // element will be read by the child.
  662. //
  663. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  664. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  665. while( p && *p ) {
  666. XMLNode* node = 0;
  667. p = _document->Identify( p, &node );
  668. if ( p == 0 || node == 0 ) {
  669. break;
  670. }
  671. StrPair endTag;
  672. p = node->ParseDeep( p, &endTag );
  673. if ( !p ) {
  674. DELETE_NODE( node );
  675. node = 0;
  676. if ( !_document->Error() ) {
  677. _document->SetError( XML_ERROR_PARSING, 0, 0 );
  678. }
  679. break;
  680. }
  681. // We read the end tag. Return it to the parent.
  682. if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
  683. if ( parentEnd ) {
  684. *parentEnd = static_cast<XMLElement*>(node)->_value;
  685. }
  686. node->_memPool->SetTracked(); // created and then immediately deleted.
  687. DELETE_NODE( node );
  688. return p;
  689. }
  690. // Handle an end tag returned to this level.
  691. // And handle a bunch of annoying errors.
  692. XMLElement* ele = node->ToElement();
  693. if ( ele ) {
  694. if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
  695. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  696. p = 0;
  697. }
  698. else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
  699. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  700. p = 0;
  701. }
  702. else if ( !endTag.Empty() ) {
  703. if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
  704. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  705. p = 0;
  706. }
  707. }
  708. }
  709. if ( p == 0 ) {
  710. DELETE_NODE( node );
  711. node = 0;
  712. }
  713. if ( node ) {
  714. this->InsertEndChild( node );
  715. }
  716. }
  717. return 0;
  718. }
  719. // --------- XMLText ---------- //
  720. char* XMLText::ParseDeep( char* p, StrPair* )
  721. {
  722. const char* start = p;
  723. if ( this->CData() ) {
  724. p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  725. if ( !p ) {
  726. _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
  727. }
  728. return p;
  729. }
  730. else {
  731. int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
  732. if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
  733. flags |= StrPair::COLLAPSE_WHITESPACE;
  734. }
  735. p = _value.ParseText( p, "<", flags );
  736. if ( !p ) {
  737. _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
  738. }
  739. if ( p && *p ) {
  740. return p-1;
  741. }
  742. }
  743. return 0;
  744. }
  745. XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
  746. {
  747. if ( !doc ) {
  748. doc = _document;
  749. }
  750. XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
  751. text->SetCData( this->CData() );
  752. return text;
  753. }
  754. bool XMLText::ShallowEqual( const XMLNode* compare ) const
  755. {
  756. return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
  757. }
  758. bool XMLText::Accept( XMLVisitor* visitor ) const
  759. {
  760. return visitor->Visit( *this );
  761. }
  762. // --------- XMLComment ---------- //
  763. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  764. {
  765. }
  766. XMLComment::~XMLComment()
  767. {
  768. }
  769. char* XMLComment::ParseDeep( char* p, StrPair* )
  770. {
  771. // Comment parses as text.
  772. const char* start = p;
  773. p = _value.ParseText( p, "-->", StrPair::COMMENT );
  774. if ( p == 0 ) {
  775. _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
  776. }
  777. return p;
  778. }
  779. XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
  780. {
  781. if ( !doc ) {
  782. doc = _document;
  783. }
  784. XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
  785. return comment;
  786. }
  787. bool XMLComment::ShallowEqual( const XMLNode* compare ) const
  788. {
  789. return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() ));
  790. }
  791. bool XMLComment::Accept( XMLVisitor* visitor ) const
  792. {
  793. return visitor->Visit( *this );
  794. }
  795. // --------- XMLDeclaration ---------- //
  796. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  797. {
  798. }
  799. XMLDeclaration::~XMLDeclaration()
  800. {
  801. //printf( "~XMLDeclaration\n" );
  802. }
  803. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  804. {
  805. // Declaration parses as text.
  806. const char* start = p;
  807. p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  808. if ( p == 0 ) {
  809. _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
  810. }
  811. return p;
  812. }
  813. XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
  814. {
  815. if ( !doc ) {
  816. doc = _document;
  817. }
  818. XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
  819. return dec;
  820. }
  821. bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
  822. {
  823. return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() ));
  824. }
  825. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  826. {
  827. return visitor->Visit( *this );
  828. }
  829. // --------- XMLUnknown ---------- //
  830. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  831. {
  832. }
  833. XMLUnknown::~XMLUnknown()
  834. {
  835. }
  836. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  837. {
  838. // Unknown parses as text.
  839. const char* start = p;
  840. p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  841. if ( !p ) {
  842. _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
  843. }
  844. return p;
  845. }
  846. XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
  847. {
  848. if ( !doc ) {
  849. doc = _document;
  850. }
  851. XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
  852. return text;
  853. }
  854. bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
  855. {
  856. return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() ));
  857. }
  858. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  859. {
  860. return visitor->Visit( *this );
  861. }
  862. // --------- XMLAttribute ---------- //
  863. char* XMLAttribute::ParseDeep( char* p, bool processEntities )
  864. {
  865. // Parse using the name rules: bug fix, was using ParseText before
  866. p = _name.ParseName( p );
  867. if ( !p || !*p ) {
  868. return 0;
  869. }
  870. // Skip white space before =
  871. p = XMLUtil::SkipWhiteSpace( p );
  872. if ( !p || *p != '=' ) {
  873. return 0;
  874. }
  875. ++p; // move up to opening quote
  876. p = XMLUtil::SkipWhiteSpace( p );
  877. if ( *p != '\"' && *p != '\'' ) {
  878. return 0;
  879. }
  880. char endTag[2] = { *p, 0 };
  881. ++p; // move past opening quote
  882. p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
  883. return p;
  884. }
  885. void XMLAttribute::SetName( const char* n )
  886. {
  887. _name.SetStr( n );
  888. }
  889. XMLError XMLAttribute::QueryIntValue( int* value ) const
  890. {
  891. if ( XMLUtil::ToInt( Value(), value )) {
  892. return XML_NO_ERROR;
  893. }
  894. return XML_WRONG_ATTRIBUTE_TYPE;
  895. }
  896. XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
  897. {
  898. if ( XMLUtil::ToUnsigned( Value(), value )) {
  899. return XML_NO_ERROR;
  900. }
  901. return XML_WRONG_ATTRIBUTE_TYPE;
  902. }
  903. XMLError XMLAttribute::QueryBoolValue( bool* value ) const
  904. {
  905. if ( XMLUtil::ToBool( Value(), value )) {
  906. return XML_NO_ERROR;
  907. }
  908. return XML_WRONG_ATTRIBUTE_TYPE;
  909. }
  910. XMLError XMLAttribute::QueryFloatValue( float* value ) const
  911. {
  912. if ( XMLUtil::ToFloat( Value(), value )) {
  913. return XML_NO_ERROR;
  914. }
  915. return XML_WRONG_ATTRIBUTE_TYPE;
  916. }
  917. XMLError XMLAttribute::QueryDoubleValue( double* value ) const
  918. {
  919. if ( XMLUtil::ToDouble( Value(), value )) {
  920. return XML_NO_ERROR;
  921. }
  922. return XML_WRONG_ATTRIBUTE_TYPE;
  923. }
  924. void XMLAttribute::SetAttribute( const char* v )
  925. {
  926. _value.SetStr( v );
  927. }
  928. void XMLAttribute::SetAttribute( int v )
  929. {
  930. char buf[BUF_SIZE];
  931. XMLUtil::ToStr( v, buf, BUF_SIZE );
  932. _value.SetStr( buf );
  933. }
  934. void XMLAttribute::SetAttribute( unsigned v )
  935. {
  936. char buf[BUF_SIZE];
  937. XMLUtil::ToStr( v, buf, BUF_SIZE );
  938. _value.SetStr( buf );
  939. }
  940. void XMLAttribute::SetAttribute( bool v )
  941. {
  942. char buf[BUF_SIZE];
  943. XMLUtil::ToStr( v, buf, BUF_SIZE );
  944. _value.SetStr( buf );
  945. }
  946. void XMLAttribute::SetAttribute( double v )
  947. {
  948. char buf[BUF_SIZE];
  949. XMLUtil::ToStr( v, buf, BUF_SIZE );
  950. _value.SetStr( buf );
  951. }
  952. void XMLAttribute::SetAttribute( float v )
  953. {
  954. char buf[BUF_SIZE];
  955. XMLUtil::ToStr( v, buf, BUF_SIZE );
  956. _value.SetStr( buf );
  957. }
  958. // --------- XMLElement ---------- //
  959. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  960. _closingType( 0 ),
  961. _rootAttribute( 0 )
  962. {
  963. }
  964. XMLElement::~XMLElement()
  965. {
  966. while( _rootAttribute ) {
  967. XMLAttribute* next = _rootAttribute->_next;
  968. DELETE_ATTRIBUTE( _rootAttribute );
  969. _rootAttribute = next;
  970. }
  971. }
  972. XMLAttribute* XMLElement::FindAttribute( const char* name )
  973. {
  974. XMLAttribute* a = 0;
  975. for( a=_rootAttribute; a; a = a->_next ) {
  976. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  977. return a;
  978. }
  979. }
  980. return 0;
  981. }
  982. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  983. {
  984. XMLAttribute* a = 0;
  985. for( a=_rootAttribute; a; a = a->_next ) {
  986. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  987. return a;
  988. }
  989. }
  990. return 0;
  991. }
  992. const char* XMLElement::Attribute( const char* name, const char* value ) const
  993. {
  994. const XMLAttribute* a = FindAttribute( name );
  995. if ( !a ) {
  996. return 0;
  997. }
  998. if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
  999. return a->Value();
  1000. }
  1001. return 0;
  1002. }
  1003. const char* XMLElement::GetText() const
  1004. {
  1005. if ( FirstChild() && FirstChild()->ToText() ) {
  1006. return FirstChild()->ToText()->Value();
  1007. }
  1008. return 0;
  1009. }
  1010. XMLError XMLElement::QueryIntText( int* ival ) const
  1011. {
  1012. if ( FirstChild() && FirstChild()->ToText() ) {
  1013. const char* t = FirstChild()->ToText()->Value();
  1014. if ( XMLUtil::ToInt( t, ival ) ) {
  1015. return XML_SUCCESS;
  1016. }
  1017. return XML_CAN_NOT_CONVERT_TEXT;
  1018. }
  1019. return XML_NO_TEXT_NODE;
  1020. }
  1021. XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
  1022. {
  1023. if ( FirstChild() && FirstChild()->ToText() ) {
  1024. const char* t = FirstChild()->ToText()->Value();
  1025. if ( XMLUtil::ToUnsigned( t, uval ) ) {
  1026. return XML_SUCCESS;
  1027. }
  1028. return XML_CAN_NOT_CONVERT_TEXT;
  1029. }
  1030. return XML_NO_TEXT_NODE;
  1031. }
  1032. XMLError XMLElement::QueryBoolText( bool* bval ) const
  1033. {
  1034. if ( FirstChild() && FirstChild()->ToText() ) {
  1035. const char* t = FirstChild()->ToText()->Value();
  1036. if ( XMLUtil::ToBool( t, bval ) ) {
  1037. return XML_SUCCESS;
  1038. }
  1039. return XML_CAN_NOT_CONVERT_TEXT;
  1040. }
  1041. return XML_NO_TEXT_NODE;
  1042. }
  1043. XMLError XMLElement::QueryDoubleText( double* dval ) const
  1044. {
  1045. if ( FirstChild() && FirstChild()->ToText() ) {
  1046. const char* t = FirstChild()->ToText()->Value();
  1047. if ( XMLUtil::ToDouble( t, dval ) ) {
  1048. return XML_SUCCESS;
  1049. }
  1050. return XML_CAN_NOT_CONVERT_TEXT;
  1051. }
  1052. return XML_NO_TEXT_NODE;
  1053. }
  1054. XMLError XMLElement::QueryFloatText( float* fval ) const
  1055. {
  1056. if ( FirstChild() && FirstChild()->ToText() ) {
  1057. const char* t = FirstChild()->ToText()->Value();
  1058. if ( XMLUtil::ToFloat( t, fval ) ) {
  1059. return XML_SUCCESS;
  1060. }
  1061. return XML_CAN_NOT_CONVERT_TEXT;
  1062. }
  1063. return XML_NO_TEXT_NODE;
  1064. }
  1065. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  1066. {
  1067. XMLAttribute* last = 0;
  1068. XMLAttribute* attrib = 0;
  1069. for( attrib = _rootAttribute;
  1070. attrib;
  1071. last = attrib, attrib = attrib->_next ) {
  1072. if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
  1073. break;
  1074. }
  1075. }
  1076. if ( !attrib ) {
  1077. attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1078. attrib->_memPool = &_document->_attributePool;
  1079. if ( last ) {
  1080. last->_next = attrib;
  1081. }
  1082. else {
  1083. _rootAttribute = attrib;
  1084. }
  1085. attrib->SetName( name );
  1086. attrib->_memPool->SetTracked(); // always created and linked.
  1087. }
  1088. return attrib;
  1089. }
  1090. void XMLElement::DeleteAttribute( const char* name )
  1091. {
  1092. XMLAttribute* prev = 0;
  1093. for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
  1094. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  1095. if ( prev ) {
  1096. prev->_next = a->_next;
  1097. }
  1098. else {
  1099. _rootAttribute = a->_next;
  1100. }
  1101. DELETE_ATTRIBUTE( a );
  1102. break;
  1103. }
  1104. prev = a;
  1105. }
  1106. }
  1107. char* XMLElement::ParseAttributes( char* p )
  1108. {
  1109. const char* start = p;
  1110. XMLAttribute* prevAttribute = 0;
  1111. // Read the attributes.
  1112. while( p ) {
  1113. p = XMLUtil::SkipWhiteSpace( p );
  1114. if ( !p || !(*p) ) {
  1115. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
  1116. return 0;
  1117. }
  1118. // attribute.
  1119. if (XMLUtil::IsNameStartChar( *p ) ) {
  1120. XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1121. attrib->_memPool = &_document->_attributePool;
  1122. attrib->_memPool->SetTracked();
  1123. p = attrib->ParseDeep( p, _document->ProcessEntities() );
  1124. if ( !p || Attribute( attrib->Name() ) ) {
  1125. DELETE_ATTRIBUTE( attrib );
  1126. _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
  1127. return 0;
  1128. }
  1129. // There is a minor bug here: if the attribute in the source xml
  1130. // document is duplicated, it will not be detected and the
  1131. // attribute will be doubly added. However, tracking the 'prevAttribute'
  1132. // avoids re-scanning the attribute list. Preferring performance for
  1133. // now, may reconsider in the future.
  1134. if ( prevAttribute ) {
  1135. prevAttribute->_next = attrib;
  1136. }
  1137. else {
  1138. _rootAttribute = attrib;
  1139. }
  1140. prevAttribute = attrib;
  1141. }
  1142. // end of the tag
  1143. else if ( *p == '/' && *(p+1) == '>' ) {
  1144. _closingType = CLOSED;
  1145. return p+2; // done; sealed element.
  1146. }
  1147. // end of the tag
  1148. else if ( *p == '>' ) {
  1149. ++p;
  1150. break;
  1151. }
  1152. else {
  1153. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
  1154. return 0;
  1155. }
  1156. }
  1157. return p;
  1158. }
  1159. //
  1160. // <ele></ele>
  1161. // <ele>foo<b>bar</b></ele>
  1162. //
  1163. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  1164. {
  1165. // Read the element name.
  1166. p = XMLUtil::SkipWhiteSpace( p );
  1167. if ( !p ) {
  1168. return 0;
  1169. }
  1170. // The closing element is the </element> form. It is
  1171. // parsed just like a regular element then deleted from
  1172. // the DOM.
  1173. if ( *p == '/' ) {
  1174. _closingType = CLOSING;
  1175. ++p;
  1176. }
  1177. p = _value.ParseName( p );
  1178. if ( _value.Empty() ) {
  1179. return 0;
  1180. }
  1181. p = ParseAttributes( p );
  1182. if ( !p || !*p || _closingType ) {
  1183. return p;
  1184. }
  1185. p = XMLNode::ParseDeep( p, strPair );
  1186. return p;
  1187. }
  1188. XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
  1189. {
  1190. if ( !doc ) {
  1191. doc = _document;
  1192. }
  1193. XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
  1194. for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
  1195. element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
  1196. }
  1197. return element;
  1198. }
  1199. bool XMLElement::ShallowEqual( const XMLNode* compare ) const
  1200. {
  1201. const XMLElement* other = compare->ToElement();
  1202. if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
  1203. const XMLAttribute* a=FirstAttribute();
  1204. const XMLAttribute* b=other->FirstAttribute();
  1205. while ( a && b ) {
  1206. if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
  1207. return false;
  1208. }
  1209. a = a->Next();
  1210. b = b->Next();
  1211. }
  1212. if ( a || b ) {
  1213. // different count
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. return false;
  1219. }
  1220. bool XMLElement::Accept( XMLVisitor* visitor ) const
  1221. {
  1222. if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
  1223. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  1224. if ( !node->Accept( visitor ) ) {
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. return visitor->VisitExit( *this );
  1230. }
  1231. // --------- XMLDocument ----------- //
  1232. XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
  1233. XMLNode( 0 ),
  1234. _writeBOM( false ),
  1235. _processEntities( processEntities ),
  1236. _errorID( XML_NO_ERROR ),
  1237. _whitespace( whitespace ),
  1238. _errorStr1( 0 ),
  1239. _errorStr2( 0 ),
  1240. _charBuffer( 0 )
  1241. {
  1242. _document = this; // avoid warning about 'this' in initializer list
  1243. }
  1244. XMLDocument::~XMLDocument()
  1245. {
  1246. DeleteChildren();
  1247. delete [] _charBuffer;
  1248. #if 0
  1249. _textPool.Trace( "text" );
  1250. _elementPool.Trace( "element" );
  1251. _commentPool.Trace( "comment" );
  1252. _attributePool.Trace( "attribute" );
  1253. #endif
  1254. #ifdef DEBUG
  1255. if ( Error() == false ) {
  1256. TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
  1257. TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
  1258. TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
  1259. TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
  1260. }
  1261. #endif
  1262. }
  1263. void XMLDocument::Clear()
  1264. {
  1265. DeleteChildren();
  1266. _errorID = XML_NO_ERROR;
  1267. _errorStr1 = 0;
  1268. _errorStr2 = 0;
  1269. delete [] _charBuffer;
  1270. _charBuffer = 0;
  1271. }
  1272. XMLElement* XMLDocument::NewElement( const char* name )
  1273. {
  1274. XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
  1275. ele->_memPool = &_elementPool;
  1276. ele->SetName( name );
  1277. return ele;
  1278. }
  1279. XMLComment* XMLDocument::NewComment( const char* str )
  1280. {
  1281. XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
  1282. comment->_memPool = &_commentPool;
  1283. comment->SetValue( str );
  1284. return comment;
  1285. }
  1286. XMLText* XMLDocument::NewText( const char* str )
  1287. {
  1288. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  1289. text->_memPool = &_textPool;
  1290. text->SetValue( str );
  1291. return text;
  1292. }
  1293. XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
  1294. {
  1295. XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
  1296. dec->_memPool = &_commentPool;
  1297. dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
  1298. return dec;
  1299. }
  1300. XMLUnknown* XMLDocument::NewUnknown( const char* str )
  1301. {
  1302. XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
  1303. unk->_memPool = &_commentPool;
  1304. unk->SetValue( str );
  1305. return unk;
  1306. }
  1307. XMLError XMLDocument::LoadFile( const char* filename )
  1308. {
  1309. Clear();
  1310. FILE* fp = 0;
  1311. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1312. errno_t err = fopen_s(&fp, filename, "rb" );
  1313. if ( !fp || err) {
  1314. #else
  1315. fp = fopen( filename, "rb" );
  1316. if ( !fp) {
  1317. #endif
  1318. SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
  1319. return _errorID;
  1320. }
  1321. LoadFile( fp );
  1322. fclose( fp );
  1323. return _errorID;
  1324. }
  1325. XMLError XMLDocument::LoadFile( FILE* fp )
  1326. {
  1327. Clear();
  1328. fseek( fp, 0, SEEK_END );
  1329. size_t size = ftell( fp );
  1330. fseek( fp, 0, SEEK_SET );
  1331. if ( size == 0 ) {
  1332. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1333. return _errorID;
  1334. }
  1335. _charBuffer = new char[size+1];
  1336. size_t read = fread( _charBuffer, 1, size, fp );
  1337. if ( read != size ) {
  1338. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1339. return _errorID;
  1340. }
  1341. _charBuffer[size] = 0;
  1342. const char* p = _charBuffer;
  1343. p = XMLUtil::SkipWhiteSpace( p );
  1344. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1345. if ( !p || !*p ) {
  1346. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1347. return _errorID;
  1348. }
  1349. ParseDeep( _charBuffer + (p-_charBuffer), 0 );
  1350. return _errorID;
  1351. }
  1352. XMLError XMLDocument::SaveFile( const char* filename, bool compact )
  1353. {
  1354. FILE* fp = 0;
  1355. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1356. errno_t err = fopen_s(&fp, filename, "w" );
  1357. if ( !fp || err) {
  1358. #else
  1359. fp = fopen( filename, "w" );
  1360. if ( !fp) {
  1361. #endif
  1362. SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
  1363. return _errorID;
  1364. }
  1365. SaveFile(fp, compact);
  1366. fclose( fp );
  1367. return _errorID;
  1368. }
  1369. XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
  1370. {
  1371. XMLPrinter stream( fp, compact );
  1372. Print( &stream );
  1373. return _errorID;
  1374. }
  1375. XMLError XMLDocument::Parse( const char* p, size_t len )
  1376. {
  1377. const char* start = p;
  1378. Clear();
  1379. if ( !p || !*p ) {
  1380. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1381. return _errorID;
  1382. }
  1383. if ( len == (size_t)(-1) ) {
  1384. len = strlen( p );
  1385. }
  1386. _charBuffer = new char[ len+1 ];
  1387. memcpy( _charBuffer, p, len );
  1388. _charBuffer[len] = 0;
  1389. p = XMLUtil::SkipWhiteSpace( p );
  1390. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1391. if ( !p || !*p ) {
  1392. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1393. return _errorID;
  1394. }
  1395. ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc.
  1396. ParseDeep( _charBuffer+delta, 0 );
  1397. return _errorID;
  1398. }
  1399. void XMLDocument::Print( XMLPrinter* streamer )
  1400. {
  1401. XMLPrinter stdStreamer( stdout );
  1402. if ( !streamer ) {
  1403. streamer = &stdStreamer;
  1404. }
  1405. Accept( streamer );
  1406. }
  1407. void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
  1408. {
  1409. _errorID = error;
  1410. _errorStr1 = str1;
  1411. _errorStr2 = str2;
  1412. }
  1413. void XMLDocument::PrintError() const
  1414. {
  1415. if ( _errorID ) {
  1416. static const int LEN = 20;
  1417. char buf1[LEN] = { 0 };
  1418. char buf2[LEN] = { 0 };
  1419. if ( _errorStr1 ) {
  1420. TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
  1421. }
  1422. if ( _errorStr2 ) {
  1423. TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
  1424. }
  1425. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  1426. _errorID, buf1, buf2 );
  1427. }
  1428. }
  1429. XMLPrinter::XMLPrinter( FILE* file, bool compact ) :
  1430. _elementJustOpened( false ),
  1431. _firstElement( true ),
  1432. _fp( file ),
  1433. _depth( 0 ),
  1434. _textDepth( -1 ),
  1435. _processEntities( true ),
  1436. _compactMode( compact )
  1437. {
  1438. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1439. _entityFlag[i] = false;
  1440. _restrictedEntityFlag[i] = false;
  1441. }
  1442. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1443. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  1444. if ( entities[i].value < ENTITY_RANGE ) {
  1445. _entityFlag[ (int)entities[i].value ] = true;
  1446. }
  1447. }
  1448. _restrictedEntityFlag[(int)'&'] = true;
  1449. _restrictedEntityFlag[(int)'<'] = true;
  1450. _restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
  1451. _buffer.Push( 0 );
  1452. }
  1453. void XMLPrinter::Print( const char* format, ... )
  1454. {
  1455. va_list va;
  1456. va_start( va, format );
  1457. if ( _fp ) {
  1458. vfprintf( _fp, format, va );
  1459. }
  1460. else {
  1461. // This seems brutally complex. Haven't figured out a better
  1462. // way on windows.
  1463. #ifdef _MSC_VER
  1464. int len = -1;
  1465. int expand = 1000;
  1466. while ( len < 0 ) {
  1467. len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va );
  1468. if ( len < 0 ) {
  1469. expand *= 3/2;
  1470. _accumulator.PushArr( expand );
  1471. }
  1472. }
  1473. char* p = _buffer.PushArr( len ) - 1;
  1474. memcpy( p, _accumulator.Mem(), len+1 );
  1475. #else
  1476. int len = vsnprintf( 0, 0, format, va );
  1477. // Close out and re-start the va-args
  1478. va_end( va );
  1479. va_start( va, format );
  1480. char* p = _buffer.PushArr( len ) - 1;
  1481. vsnprintf( p, len+1, format, va );
  1482. #endif
  1483. }
  1484. va_end( va );
  1485. }
  1486. void XMLPrinter::PrintSpace( int depth )
  1487. {
  1488. for( int i=0; i<depth; ++i ) {
  1489. Print( " " );
  1490. }
  1491. }
  1492. void XMLPrinter::PrintString( const char* p, bool restricted )
  1493. {
  1494. // Look for runs of bytes between entities to print.
  1495. const char* q = p;
  1496. const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
  1497. if ( _processEntities ) {
  1498. while ( *q ) {
  1499. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1500. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1501. // Check for entities. If one is found, flush
  1502. // the stream up until the entity, write the
  1503. // entity, and keep looking.
  1504. if ( flag[(unsigned)(*q)] ) {
  1505. while ( p < q ) {
  1506. Print( "%c", *p );
  1507. ++p;
  1508. }
  1509. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1510. if ( entities[i].value == *q ) {
  1511. Print( "&%s;", entities[i].pattern );
  1512. break;
  1513. }
  1514. }
  1515. ++p;
  1516. }
  1517. }
  1518. ++q;
  1519. }
  1520. }
  1521. // Flush the remaining string. This will be the entire
  1522. // string if an entity wasn't found.
  1523. if ( !_processEntities || (q-p > 0) ) {
  1524. Print( "%s", p );
  1525. }
  1526. }
  1527. void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
  1528. {
  1529. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1530. if ( writeBOM ) {
  1531. Print( "%s", bom );
  1532. }
  1533. if ( writeDec ) {
  1534. PushDeclaration( "xml version=\"1.0\"" );
  1535. }
  1536. }
  1537. void XMLPrinter::OpenElement( const char* name )
  1538. {
  1539. if ( _elementJustOpened ) {
  1540. SealElement();
  1541. }
  1542. _stack.Push( name );
  1543. if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
  1544. Print( "\n" );
  1545. PrintSpace( _depth );
  1546. }
  1547. Print( "<%s", name );
  1548. _elementJustOpened = true;
  1549. _firstElement = false;
  1550. ++_depth;
  1551. }
  1552. void XMLPrinter::PushAttribute( const char* name, const char* value )
  1553. {
  1554. TIXMLASSERT( _elementJustOpened );
  1555. Print( " %s=\"", name );
  1556. PrintString( value, false );
  1557. Print( "\"" );
  1558. }
  1559. void XMLPrinter::PushAttribute( const char* name, int v )
  1560. {
  1561. char buf[BUF_SIZE];
  1562. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1563. PushAttribute( name, buf );
  1564. }
  1565. void XMLPrinter::PushAttribute( const char* name, unsigned v )
  1566. {
  1567. char buf[BUF_SIZE];
  1568. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1569. PushAttribute( name, buf );
  1570. }
  1571. void XMLPrinter::PushAttribute( const char* name, bool v )
  1572. {
  1573. char buf[BUF_SIZE];
  1574. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1575. PushAttribute( name, buf );
  1576. }
  1577. void XMLPrinter::PushAttribute( const char* name, double v )
  1578. {
  1579. char buf[BUF_SIZE];
  1580. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1581. PushAttribute( name, buf );
  1582. }
  1583. void XMLPrinter::CloseElement()
  1584. {
  1585. --_depth;
  1586. const char* name = _stack.Pop();
  1587. if ( _elementJustOpened ) {
  1588. Print( "/>" );
  1589. }
  1590. else {
  1591. if ( _textDepth < 0 && !_compactMode) {
  1592. Print( "\n" );
  1593. PrintSpace( _depth );
  1594. }
  1595. Print( "</%s>", name );
  1596. }
  1597. if ( _textDepth == _depth ) {
  1598. _textDepth = -1;
  1599. }
  1600. if ( _depth == 0 && !_compactMode) {
  1601. Print( "\n" );
  1602. }
  1603. _elementJustOpened = false;
  1604. }
  1605. void XMLPrinter::SealElement()
  1606. {
  1607. _elementJustOpened = false;
  1608. Print( ">" );
  1609. }
  1610. void XMLPrinter::PushText( const char* text, bool cdata )
  1611. {
  1612. _textDepth = _depth-1;
  1613. if ( _elementJustOpened ) {
  1614. SealElement();
  1615. }
  1616. if ( cdata ) {
  1617. Print( "<![CDATA[" );
  1618. Print( "%s", text );
  1619. Print( "]]>" );
  1620. }
  1621. else {
  1622. PrintString( text, true );
  1623. }
  1624. }
  1625. void XMLPrinter::PushText( int value )
  1626. {
  1627. char buf[BUF_SIZE];
  1628. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1629. PushText( buf, false );
  1630. }
  1631. void XMLPrinter::PushText( unsigned value )
  1632. {
  1633. char buf[BUF_SIZE];
  1634. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1635. PushText( buf, false );
  1636. }
  1637. void XMLPrinter::PushText( bool value )
  1638. {
  1639. char buf[BUF_SIZE];
  1640. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1641. PushText( buf, false );
  1642. }
  1643. void XMLPrinter::PushText( float value )
  1644. {
  1645. char buf[BUF_SIZE];
  1646. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1647. PushText( buf, false );
  1648. }
  1649. void XMLPrinter::PushText( double value )
  1650. {
  1651. char buf[BUF_SIZE];
  1652. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1653. PushText( buf, false );
  1654. }
  1655. void XMLPrinter::PushComment( const char* comment )
  1656. {
  1657. if ( _elementJustOpened ) {
  1658. SealElement();
  1659. }
  1660. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1661. Print( "\n" );
  1662. PrintSpace( _depth );
  1663. }
  1664. _firstElement = false;
  1665. Print( "<!--%s-->", comment );
  1666. }
  1667. void XMLPrinter::PushDeclaration( const char* value )
  1668. {
  1669. if ( _elementJustOpened ) {
  1670. SealElement();
  1671. }
  1672. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1673. Print( "\n" );
  1674. PrintSpace( _depth );
  1675. }
  1676. _firstElement = false;
  1677. Print( "<?%s?>", value );
  1678. }
  1679. void XMLPrinter::PushUnknown( const char* value )
  1680. {
  1681. if ( _elementJustOpened ) {
  1682. SealElement();
  1683. }
  1684. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1685. Print( "\n" );
  1686. PrintSpace( _depth );
  1687. }
  1688. _firstElement = false;
  1689. Print( "<!%s>", value );
  1690. }
  1691. bool XMLPrinter::VisitEnter( const XMLDocument& doc )
  1692. {
  1693. _processEntities = doc.ProcessEntities();
  1694. if ( doc.HasBOM() ) {
  1695. PushHeader( true, false );
  1696. }
  1697. return true;
  1698. }
  1699. bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1700. {
  1701. OpenElement( element.Name() );
  1702. while ( attribute ) {
  1703. PushAttribute( attribute->Name(), attribute->Value() );
  1704. attribute = attribute->Next();
  1705. }
  1706. return true;
  1707. }
  1708. bool XMLPrinter::VisitExit( const XMLElement& )
  1709. {
  1710. CloseElement();
  1711. return true;
  1712. }
  1713. bool XMLPrinter::Visit( const XMLText& text )
  1714. {
  1715. PushText( text.Value(), text.CData() );
  1716. return true;
  1717. }
  1718. bool XMLPrinter::Visit( const XMLComment& comment )
  1719. {
  1720. PushComment( comment.Value() );
  1721. return true;
  1722. }
  1723. bool XMLPrinter::Visit( const XMLDeclaration& declaration )
  1724. {
  1725. PushDeclaration( declaration.Value() );
  1726. return true;
  1727. }
  1728. bool XMLPrinter::Visit( const XMLUnknown& unknown )
  1729. {
  1730. PushUnknown( unknown.Value() );
  1731. return true;
  1732. }
  1733. } // namespace tinyxml2