1
0

tinyxml2.cpp 44 KB

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