tinyxml2.cpp 51 KB

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