1
0

tinyxml2.cpp 50 KB

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