tinyxml2.cpp 56 KB

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