tinyxml2.cpp 57 KB

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