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. 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. TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );
  453. returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
  454. returnNode->_memPool = &_commentPool;
  455. p += xmlHeaderLen;
  456. }
  457. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  458. TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );
  459. returnNode = new (_commentPool.Alloc()) XMLComment( this );
  460. returnNode->_memPool = &_commentPool;
  461. p += commentHeaderLen;
  462. }
  463. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  464. TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
  465. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  466. returnNode = text;
  467. returnNode->_memPool = &_textPool;
  468. p += cdataHeaderLen;
  469. text->SetCData( true );
  470. }
  471. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  472. TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );
  473. returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
  474. returnNode->_memPool = &_commentPool;
  475. p += dtdHeaderLen;
  476. }
  477. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  478. TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );
  479. returnNode = new (_elementPool.Alloc()) XMLElement( this );
  480. returnNode->_memPool = &_elementPool;
  481. p += elementHeaderLen;
  482. }
  483. else {
  484. TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
  485. returnNode = new (_textPool.Alloc()) XMLText( this );
  486. returnNode->_memPool = &_textPool;
  487. p = start; // Back it up, all the text counts.
  488. }
  489. *node = returnNode;
  490. return p;
  491. }
  492. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  493. {
  494. if ( visitor->VisitEnter( *this ) ) {
  495. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  496. if ( !node->Accept( visitor ) ) {
  497. break;
  498. }
  499. }
  500. }
  501. return visitor->VisitExit( *this );
  502. }
  503. // --------- XMLNode ----------- //
  504. XMLNode::XMLNode( XMLDocument* doc ) :
  505. _document( doc ),
  506. _parent( 0 ),
  507. _firstChild( 0 ), _lastChild( 0 ),
  508. _prev( 0 ), _next( 0 ),
  509. _memPool( 0 )
  510. {
  511. }
  512. XMLNode::~XMLNode()
  513. {
  514. DeleteChildren();
  515. if ( _parent ) {
  516. _parent->Unlink( this );
  517. }
  518. }
  519. const char* XMLNode::Value() const
  520. {
  521. return _value.GetStr();
  522. }
  523. void XMLNode::SetValue( const char* str, bool staticMem )
  524. {
  525. if ( staticMem ) {
  526. _value.SetInternedStr( str );
  527. }
  528. else {
  529. _value.SetStr( str );
  530. }
  531. }
  532. void XMLNode::DeleteChildren()
  533. {
  534. while( _firstChild ) {
  535. TIXMLASSERT( _firstChild->_document == _document );
  536. XMLNode* node = _firstChild;
  537. Unlink( node );
  538. DeleteNode( node );
  539. }
  540. _firstChild = _lastChild = 0;
  541. }
  542. void XMLNode::Unlink( XMLNode* child )
  543. {
  544. TIXMLASSERT( child->_document == _document );
  545. if ( child == _firstChild ) {
  546. _firstChild = _firstChild->_next;
  547. }
  548. if ( child == _lastChild ) {
  549. _lastChild = _lastChild->_prev;
  550. }
  551. if ( child->_prev ) {
  552. child->_prev->_next = child->_next;
  553. }
  554. if ( child->_next ) {
  555. child->_next->_prev = child->_prev;
  556. }
  557. child->_parent = 0;
  558. }
  559. void XMLNode::DeleteChild( XMLNode* node )
  560. {
  561. TIXMLASSERT( node->_document == _document );
  562. TIXMLASSERT( node->_parent == this );
  563. DeleteNode( node );
  564. }
  565. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  566. {
  567. TIXMLASSERT( addThis );
  568. if ( addThis->_document != _document ) {
  569. TIXMLASSERT( false );
  570. return 0;
  571. }
  572. if (addThis->_parent)
  573. addThis->_parent->Unlink( addThis );
  574. else
  575. addThis->_memPool->SetTracked();
  576. if ( _lastChild ) {
  577. TIXMLASSERT( _firstChild );
  578. TIXMLASSERT( _lastChild->_next == 0 );
  579. _lastChild->_next = addThis;
  580. addThis->_prev = _lastChild;
  581. _lastChild = addThis;
  582. addThis->_next = 0;
  583. }
  584. else {
  585. TIXMLASSERT( _firstChild == 0 );
  586. _firstChild = _lastChild = addThis;
  587. addThis->_prev = 0;
  588. addThis->_next = 0;
  589. }
  590. addThis->_parent = this;
  591. return addThis;
  592. }
  593. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  594. {
  595. TIXMLASSERT( addThis );
  596. if ( addThis->_document != _document ) {
  597. TIXMLASSERT( false );
  598. return 0;
  599. }
  600. if (addThis->_parent)
  601. addThis->_parent->Unlink( addThis );
  602. else
  603. addThis->_memPool->SetTracked();
  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. if (addThis->_parent)
  638. addThis->_parent->Unlink( addThis );
  639. else
  640. addThis->_memPool->SetTracked();
  641. addThis->_prev = afterThis;
  642. addThis->_next = afterThis->_next;
  643. afterThis->_next->_prev = addThis;
  644. afterThis->_next = addThis;
  645. addThis->_parent = this;
  646. return addThis;
  647. }
  648. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  649. {
  650. for( XMLNode* node=_firstChild; node; node=node->_next ) {
  651. XMLElement* element = node->ToElement();
  652. if ( element ) {
  653. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  654. return element;
  655. }
  656. }
  657. }
  658. return 0;
  659. }
  660. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  661. {
  662. for( XMLNode* node=_lastChild; node; node=node->_prev ) {
  663. XMLElement* element = node->ToElement();
  664. if ( element ) {
  665. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  666. return element;
  667. }
  668. }
  669. }
  670. return 0;
  671. }
  672. const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
  673. {
  674. for( XMLNode* node=this->_next; node; node = node->_next ) {
  675. const XMLElement* element = node->ToElement();
  676. if ( element
  677. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  678. return element;
  679. }
  680. }
  681. return 0;
  682. }
  683. const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
  684. {
  685. for( XMLNode* node=_prev; node; node = node->_prev ) {
  686. const XMLElement* element = node->ToElement();
  687. if ( element
  688. && (!value || XMLUtil::StringEqual( value, node->Value() ))) {
  689. return element;
  690. }
  691. }
  692. return 0;
  693. }
  694. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  695. {
  696. // This is a recursive method, but thinking about it "at the current level"
  697. // it is a pretty simple flat list:
  698. // <foo/>
  699. // <!-- comment -->
  700. //
  701. // With a special case:
  702. // <foo>
  703. // </foo>
  704. // <!-- comment -->
  705. //
  706. // Where the closing element (/foo) *must* be the next thing after the opening
  707. // element, and the names must match. BUT the tricky bit is that the closing
  708. // element will be read by the child.
  709. //
  710. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  711. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  712. while( p && *p ) {
  713. XMLNode* node = 0;
  714. p = _document->Identify( p, &node );
  715. if ( p == 0 || node == 0 ) {
  716. break;
  717. }
  718. StrPair endTag;
  719. p = node->ParseDeep( p, &endTag );
  720. if ( !p ) {
  721. DeleteNode( node );
  722. if ( !_document->Error() ) {
  723. _document->SetError( XML_ERROR_PARSING, 0, 0 );
  724. }
  725. break;
  726. }
  727. XMLElement* ele = node->ToElement();
  728. if ( ele ) {
  729. // We read the end tag. Return it to the parent.
  730. if ( ele->ClosingType() == XMLElement::CLOSING ) {
  731. if ( parentEnd ) {
  732. ele->_value.TransferTo( parentEnd );
  733. }
  734. node->_memPool->SetTracked(); // created and then immediately deleted.
  735. DeleteNode( node );
  736. return p;
  737. }
  738. // Handle an end tag returned to this level.
  739. // And handle a bunch of annoying errors.
  740. bool mismatch = false;
  741. if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
  742. mismatch = true;
  743. }
  744. else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
  745. mismatch = true;
  746. }
  747. else if ( !endTag.Empty() ) {
  748. if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
  749. mismatch = true;
  750. }
  751. }
  752. if ( mismatch ) {
  753. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  754. DeleteNode( node );
  755. break;
  756. }
  757. }
  758. InsertEndChild( node );
  759. }
  760. return 0;
  761. }
  762. void XMLNode::DeleteNode( XMLNode* node )
  763. {
  764. if ( node == 0 ) {
  765. return;
  766. }
  767. MemPool* pool = node->_memPool;
  768. node->~XMLNode();
  769. pool->Free( node );
  770. }
  771. // --------- XMLText ---------- //
  772. char* XMLText::ParseDeep( char* p, StrPair* )
  773. {
  774. const char* start = p;
  775. if ( this->CData() ) {
  776. p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  777. if ( !p ) {
  778. _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
  779. }
  780. return p;
  781. }
  782. else {
  783. int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
  784. if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
  785. flags |= StrPair::COLLAPSE_WHITESPACE;
  786. }
  787. p = _value.ParseText( p, "<", flags );
  788. if ( !p ) {
  789. _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
  790. }
  791. if ( p && *p ) {
  792. return p-1;
  793. }
  794. }
  795. return 0;
  796. }
  797. XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
  798. {
  799. if ( !doc ) {
  800. doc = _document;
  801. }
  802. XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
  803. text->SetCData( this->CData() );
  804. return text;
  805. }
  806. bool XMLText::ShallowEqual( const XMLNode* compare ) const
  807. {
  808. const XMLText* text = compare->ToText();
  809. return ( text && XMLUtil::StringEqual( text->Value(), Value() ) );
  810. }
  811. bool XMLText::Accept( XMLVisitor* visitor ) const
  812. {
  813. TIXMLASSERT( visitor );
  814. return visitor->Visit( *this );
  815. }
  816. // --------- XMLComment ---------- //
  817. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  818. {
  819. }
  820. XMLComment::~XMLComment()
  821. {
  822. }
  823. char* XMLComment::ParseDeep( char* p, StrPair* )
  824. {
  825. // Comment parses as text.
  826. const char* start = p;
  827. p = _value.ParseText( p, "-->", StrPair::COMMENT );
  828. if ( p == 0 ) {
  829. _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
  830. }
  831. return p;
  832. }
  833. XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
  834. {
  835. if ( !doc ) {
  836. doc = _document;
  837. }
  838. XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
  839. return comment;
  840. }
  841. bool XMLComment::ShallowEqual( const XMLNode* compare ) const
  842. {
  843. TIXMLASSERT( compare );
  844. const XMLComment* comment = compare->ToComment();
  845. return ( comment && XMLUtil::StringEqual( comment->Value(), Value() ));
  846. }
  847. bool XMLComment::Accept( XMLVisitor* visitor ) const
  848. {
  849. TIXMLASSERT( visitor );
  850. return visitor->Visit( *this );
  851. }
  852. // --------- XMLDeclaration ---------- //
  853. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  854. {
  855. }
  856. XMLDeclaration::~XMLDeclaration()
  857. {
  858. //printf( "~XMLDeclaration\n" );
  859. }
  860. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  861. {
  862. // Declaration parses as text.
  863. const char* start = p;
  864. p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  865. if ( p == 0 ) {
  866. _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
  867. }
  868. return p;
  869. }
  870. XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
  871. {
  872. if ( !doc ) {
  873. doc = _document;
  874. }
  875. XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
  876. return dec;
  877. }
  878. bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
  879. {
  880. TIXMLASSERT( compare );
  881. const XMLDeclaration* declaration = compare->ToDeclaration();
  882. return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() ));
  883. }
  884. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  885. {
  886. TIXMLASSERT( visitor );
  887. return visitor->Visit( *this );
  888. }
  889. // --------- XMLUnknown ---------- //
  890. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  891. {
  892. }
  893. XMLUnknown::~XMLUnknown()
  894. {
  895. }
  896. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  897. {
  898. // Unknown parses as text.
  899. const char* start = p;
  900. p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  901. if ( !p ) {
  902. _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
  903. }
  904. return p;
  905. }
  906. XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
  907. {
  908. if ( !doc ) {
  909. doc = _document;
  910. }
  911. XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
  912. return text;
  913. }
  914. bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
  915. {
  916. TIXMLASSERT( compare );
  917. const XMLUnknown* unknown = compare->ToUnknown();
  918. return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() ));
  919. }
  920. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  921. {
  922. TIXMLASSERT( visitor );
  923. return visitor->Visit( *this );
  924. }
  925. // --------- XMLAttribute ---------- //
  926. const char* XMLAttribute::Name() const
  927. {
  928. return _name.GetStr();
  929. }
  930. const char* XMLAttribute::Value() const
  931. {
  932. return _value.GetStr();
  933. }
  934. char* XMLAttribute::ParseDeep( char* p, bool processEntities )
  935. {
  936. // Parse using the name rules: bug fix, was using ParseText before
  937. p = _name.ParseName( p );
  938. if ( !p || !*p ) {
  939. return 0;
  940. }
  941. // Skip white space before =
  942. p = XMLUtil::SkipWhiteSpace( p );
  943. if ( !p || *p != '=' ) {
  944. return 0;
  945. }
  946. ++p; // move up to opening quote
  947. p = XMLUtil::SkipWhiteSpace( p );
  948. if ( *p != '\"' && *p != '\'' ) {
  949. return 0;
  950. }
  951. char endTag[2] = { *p, 0 };
  952. ++p; // move past opening quote
  953. p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
  954. return p;
  955. }
  956. void XMLAttribute::SetName( const char* n )
  957. {
  958. _name.SetStr( n );
  959. }
  960. XMLError XMLAttribute::QueryIntValue( int* value ) const
  961. {
  962. if ( XMLUtil::ToInt( Value(), value )) {
  963. return XML_NO_ERROR;
  964. }
  965. return XML_WRONG_ATTRIBUTE_TYPE;
  966. }
  967. XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
  968. {
  969. if ( XMLUtil::ToUnsigned( Value(), value )) {
  970. return XML_NO_ERROR;
  971. }
  972. return XML_WRONG_ATTRIBUTE_TYPE;
  973. }
  974. XMLError XMLAttribute::QueryBoolValue( bool* value ) const
  975. {
  976. if ( XMLUtil::ToBool( Value(), value )) {
  977. return XML_NO_ERROR;
  978. }
  979. return XML_WRONG_ATTRIBUTE_TYPE;
  980. }
  981. XMLError XMLAttribute::QueryFloatValue( float* value ) const
  982. {
  983. if ( XMLUtil::ToFloat( Value(), value )) {
  984. return XML_NO_ERROR;
  985. }
  986. return XML_WRONG_ATTRIBUTE_TYPE;
  987. }
  988. XMLError XMLAttribute::QueryDoubleValue( double* value ) const
  989. {
  990. if ( XMLUtil::ToDouble( Value(), value )) {
  991. return XML_NO_ERROR;
  992. }
  993. return XML_WRONG_ATTRIBUTE_TYPE;
  994. }
  995. void XMLAttribute::SetAttribute( const char* v )
  996. {
  997. _value.SetStr( v );
  998. }
  999. void XMLAttribute::SetAttribute( int v )
  1000. {
  1001. char buf[BUF_SIZE];
  1002. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1003. _value.SetStr( buf );
  1004. }
  1005. void XMLAttribute::SetAttribute( unsigned v )
  1006. {
  1007. char buf[BUF_SIZE];
  1008. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1009. _value.SetStr( buf );
  1010. }
  1011. void XMLAttribute::SetAttribute( bool v )
  1012. {
  1013. char buf[BUF_SIZE];
  1014. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1015. _value.SetStr( buf );
  1016. }
  1017. void XMLAttribute::SetAttribute( double v )
  1018. {
  1019. char buf[BUF_SIZE];
  1020. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1021. _value.SetStr( buf );
  1022. }
  1023. void XMLAttribute::SetAttribute( float v )
  1024. {
  1025. char buf[BUF_SIZE];
  1026. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1027. _value.SetStr( buf );
  1028. }
  1029. // --------- XMLElement ---------- //
  1030. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  1031. _closingType( 0 ),
  1032. _rootAttribute( 0 )
  1033. {
  1034. }
  1035. XMLElement::~XMLElement()
  1036. {
  1037. while( _rootAttribute ) {
  1038. XMLAttribute* next = _rootAttribute->_next;
  1039. DeleteAttribute( _rootAttribute );
  1040. _rootAttribute = next;
  1041. }
  1042. }
  1043. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  1044. {
  1045. for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
  1046. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  1047. return a;
  1048. }
  1049. }
  1050. return 0;
  1051. }
  1052. const char* XMLElement::Attribute( const char* name, const char* value ) const
  1053. {
  1054. const XMLAttribute* a = FindAttribute( name );
  1055. if ( !a ) {
  1056. return 0;
  1057. }
  1058. if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
  1059. return a->Value();
  1060. }
  1061. return 0;
  1062. }
  1063. const char* XMLElement::GetText() const
  1064. {
  1065. if ( FirstChild() && FirstChild()->ToText() ) {
  1066. return FirstChild()->Value();
  1067. }
  1068. return 0;
  1069. }
  1070. void XMLElement::SetText( const char* inText )
  1071. {
  1072. if ( FirstChild() && FirstChild()->ToText() )
  1073. FirstChild()->SetValue( inText );
  1074. else {
  1075. XMLText* theText = GetDocument()->NewText( inText );
  1076. InsertFirstChild( theText );
  1077. }
  1078. }
  1079. void XMLElement::SetText( int v )
  1080. {
  1081. char buf[BUF_SIZE];
  1082. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1083. SetText( buf );
  1084. }
  1085. void XMLElement::SetText( unsigned v )
  1086. {
  1087. char buf[BUF_SIZE];
  1088. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1089. SetText( buf );
  1090. }
  1091. void XMLElement::SetText( bool v )
  1092. {
  1093. char buf[BUF_SIZE];
  1094. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1095. SetText( buf );
  1096. }
  1097. void XMLElement::SetText( float v )
  1098. {
  1099. char buf[BUF_SIZE];
  1100. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1101. SetText( buf );
  1102. }
  1103. void XMLElement::SetText( double v )
  1104. {
  1105. char buf[BUF_SIZE];
  1106. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1107. SetText( buf );
  1108. }
  1109. XMLError XMLElement::QueryIntText( int* ival ) const
  1110. {
  1111. if ( FirstChild() && FirstChild()->ToText() ) {
  1112. const char* t = FirstChild()->Value();
  1113. if ( XMLUtil::ToInt( t, ival ) ) {
  1114. return XML_SUCCESS;
  1115. }
  1116. return XML_CAN_NOT_CONVERT_TEXT;
  1117. }
  1118. return XML_NO_TEXT_NODE;
  1119. }
  1120. XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
  1121. {
  1122. if ( FirstChild() && FirstChild()->ToText() ) {
  1123. const char* t = FirstChild()->Value();
  1124. if ( XMLUtil::ToUnsigned( t, uval ) ) {
  1125. return XML_SUCCESS;
  1126. }
  1127. return XML_CAN_NOT_CONVERT_TEXT;
  1128. }
  1129. return XML_NO_TEXT_NODE;
  1130. }
  1131. XMLError XMLElement::QueryBoolText( bool* bval ) const
  1132. {
  1133. if ( FirstChild() && FirstChild()->ToText() ) {
  1134. const char* t = FirstChild()->Value();
  1135. if ( XMLUtil::ToBool( t, bval ) ) {
  1136. return XML_SUCCESS;
  1137. }
  1138. return XML_CAN_NOT_CONVERT_TEXT;
  1139. }
  1140. return XML_NO_TEXT_NODE;
  1141. }
  1142. XMLError XMLElement::QueryDoubleText( double* dval ) const
  1143. {
  1144. if ( FirstChild() && FirstChild()->ToText() ) {
  1145. const char* t = FirstChild()->Value();
  1146. if ( XMLUtil::ToDouble( t, dval ) ) {
  1147. return XML_SUCCESS;
  1148. }
  1149. return XML_CAN_NOT_CONVERT_TEXT;
  1150. }
  1151. return XML_NO_TEXT_NODE;
  1152. }
  1153. XMLError XMLElement::QueryFloatText( float* fval ) const
  1154. {
  1155. if ( FirstChild() && FirstChild()->ToText() ) {
  1156. const char* t = FirstChild()->Value();
  1157. if ( XMLUtil::ToFloat( t, fval ) ) {
  1158. return XML_SUCCESS;
  1159. }
  1160. return XML_CAN_NOT_CONVERT_TEXT;
  1161. }
  1162. return XML_NO_TEXT_NODE;
  1163. }
  1164. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  1165. {
  1166. XMLAttribute* last = 0;
  1167. XMLAttribute* attrib = 0;
  1168. for( attrib = _rootAttribute;
  1169. attrib;
  1170. last = attrib, attrib = attrib->_next ) {
  1171. if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
  1172. break;
  1173. }
  1174. }
  1175. if ( !attrib ) {
  1176. TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
  1177. attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1178. attrib->_memPool = &_document->_attributePool;
  1179. if ( last ) {
  1180. last->_next = attrib;
  1181. }
  1182. else {
  1183. _rootAttribute = attrib;
  1184. }
  1185. attrib->SetName( name );
  1186. attrib->_memPool->SetTracked(); // always created and linked.
  1187. }
  1188. return attrib;
  1189. }
  1190. void XMLElement::DeleteAttribute( const char* name )
  1191. {
  1192. XMLAttribute* prev = 0;
  1193. for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
  1194. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  1195. if ( prev ) {
  1196. prev->_next = a->_next;
  1197. }
  1198. else {
  1199. _rootAttribute = a->_next;
  1200. }
  1201. DeleteAttribute( a );
  1202. break;
  1203. }
  1204. prev = a;
  1205. }
  1206. }
  1207. char* XMLElement::ParseAttributes( char* p )
  1208. {
  1209. const char* start = p;
  1210. XMLAttribute* prevAttribute = 0;
  1211. // Read the attributes.
  1212. while( p ) {
  1213. p = XMLUtil::SkipWhiteSpace( p );
  1214. if ( !p || !(*p) ) {
  1215. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
  1216. return 0;
  1217. }
  1218. // attribute.
  1219. if (XMLUtil::IsNameStartChar( *p ) ) {
  1220. TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
  1221. XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1222. attrib->_memPool = &_document->_attributePool;
  1223. attrib->_memPool->SetTracked();
  1224. p = attrib->ParseDeep( p, _document->ProcessEntities() );
  1225. if ( !p || Attribute( attrib->Name() ) ) {
  1226. DeleteAttribute( attrib );
  1227. _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
  1228. return 0;
  1229. }
  1230. // There is a minor bug here: if the attribute in the source xml
  1231. // document is duplicated, it will not be detected and the
  1232. // attribute will be doubly added. However, tracking the 'prevAttribute'
  1233. // avoids re-scanning the attribute list. Preferring performance for
  1234. // now, may reconsider in the future.
  1235. if ( prevAttribute ) {
  1236. prevAttribute->_next = attrib;
  1237. }
  1238. else {
  1239. _rootAttribute = attrib;
  1240. }
  1241. prevAttribute = attrib;
  1242. }
  1243. // end of the tag
  1244. else if ( *p == '/' && *(p+1) == '>' ) {
  1245. _closingType = CLOSED;
  1246. return p+2; // done; sealed element.
  1247. }
  1248. // end of the tag
  1249. else if ( *p == '>' ) {
  1250. ++p;
  1251. break;
  1252. }
  1253. else {
  1254. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
  1255. return 0;
  1256. }
  1257. }
  1258. return p;
  1259. }
  1260. void XMLElement::DeleteAttribute( XMLAttribute* attribute )
  1261. {
  1262. if ( attribute == 0 ) {
  1263. return;
  1264. }
  1265. MemPool* pool = attribute->_memPool;
  1266. attribute->~XMLAttribute();
  1267. pool->Free( attribute );
  1268. }
  1269. //
  1270. // <ele></ele>
  1271. // <ele>foo<b>bar</b></ele>
  1272. //
  1273. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  1274. {
  1275. // Read the element name.
  1276. p = XMLUtil::SkipWhiteSpace( p );
  1277. if ( !p ) {
  1278. return 0;
  1279. }
  1280. // The closing element is the </element> form. It is
  1281. // parsed just like a regular element then deleted from
  1282. // the DOM.
  1283. if ( *p == '/' ) {
  1284. _closingType = CLOSING;
  1285. ++p;
  1286. }
  1287. p = _value.ParseName( p );
  1288. if ( _value.Empty() ) {
  1289. return 0;
  1290. }
  1291. p = ParseAttributes( p );
  1292. if ( !p || !*p || _closingType ) {
  1293. return p;
  1294. }
  1295. p = XMLNode::ParseDeep( p, strPair );
  1296. return p;
  1297. }
  1298. XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
  1299. {
  1300. if ( !doc ) {
  1301. doc = _document;
  1302. }
  1303. XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
  1304. for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
  1305. element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
  1306. }
  1307. return element;
  1308. }
  1309. bool XMLElement::ShallowEqual( const XMLNode* compare ) const
  1310. {
  1311. TIXMLASSERT( compare );
  1312. const XMLElement* other = compare->ToElement();
  1313. if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
  1314. const XMLAttribute* a=FirstAttribute();
  1315. const XMLAttribute* b=other->FirstAttribute();
  1316. while ( a && b ) {
  1317. if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
  1318. return false;
  1319. }
  1320. a = a->Next();
  1321. b = b->Next();
  1322. }
  1323. if ( a || b ) {
  1324. // different count
  1325. return false;
  1326. }
  1327. return true;
  1328. }
  1329. return false;
  1330. }
  1331. bool XMLElement::Accept( XMLVisitor* visitor ) const
  1332. {
  1333. TIXMLASSERT( visitor );
  1334. if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
  1335. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  1336. if ( !node->Accept( visitor ) ) {
  1337. break;
  1338. }
  1339. }
  1340. }
  1341. return visitor->VisitExit( *this );
  1342. }
  1343. // --------- XMLDocument ----------- //
  1344. // Warning: List must match 'enum XMLError'
  1345. const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = {
  1346. "XML_SUCCESS",
  1347. "XML_NO_ATTRIBUTE",
  1348. "XML_WRONG_ATTRIBUTE_TYPE",
  1349. "XML_ERROR_FILE_NOT_FOUND",
  1350. "XML_ERROR_FILE_COULD_NOT_BE_OPENED",
  1351. "XML_ERROR_FILE_READ_ERROR",
  1352. "XML_ERROR_ELEMENT_MISMATCH",
  1353. "XML_ERROR_PARSING_ELEMENT",
  1354. "XML_ERROR_PARSING_ATTRIBUTE",
  1355. "XML_ERROR_IDENTIFYING_TAG",
  1356. "XML_ERROR_PARSING_TEXT",
  1357. "XML_ERROR_PARSING_CDATA",
  1358. "XML_ERROR_PARSING_COMMENT",
  1359. "XML_ERROR_PARSING_DECLARATION",
  1360. "XML_ERROR_PARSING_UNKNOWN",
  1361. "XML_ERROR_EMPTY_DOCUMENT",
  1362. "XML_ERROR_MISMATCHED_ELEMENT",
  1363. "XML_ERROR_PARSING",
  1364. "XML_CAN_NOT_CONVERT_TEXT",
  1365. "XML_NO_TEXT_NODE"
  1366. };
  1367. XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
  1368. XMLNode( 0 ),
  1369. _writeBOM( false ),
  1370. _processEntities( processEntities ),
  1371. _errorID( XML_NO_ERROR ),
  1372. _whitespace( whitespace ),
  1373. _errorStr1( 0 ),
  1374. _errorStr2( 0 ),
  1375. _charBuffer( 0 )
  1376. {
  1377. _document = this; // avoid warning about 'this' in initializer list
  1378. }
  1379. XMLDocument::~XMLDocument()
  1380. {
  1381. Clear();
  1382. }
  1383. void XMLDocument::Clear()
  1384. {
  1385. DeleteChildren();
  1386. #ifdef DEBUG
  1387. const bool hadError = Error();
  1388. #endif
  1389. _errorID = XML_NO_ERROR;
  1390. _errorStr1 = 0;
  1391. _errorStr2 = 0;
  1392. delete [] _charBuffer;
  1393. _charBuffer = 0;
  1394. #if 0
  1395. _textPool.Trace( "text" );
  1396. _elementPool.Trace( "element" );
  1397. _commentPool.Trace( "comment" );
  1398. _attributePool.Trace( "attribute" );
  1399. #endif
  1400. #ifdef DEBUG
  1401. if ( !hadError ) {
  1402. TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
  1403. TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
  1404. TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
  1405. TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
  1406. }
  1407. #endif
  1408. }
  1409. XMLElement* XMLDocument::NewElement( const char* name )
  1410. {
  1411. TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );
  1412. XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
  1413. ele->_memPool = &_elementPool;
  1414. ele->SetName( name );
  1415. return ele;
  1416. }
  1417. XMLComment* XMLDocument::NewComment( const char* str )
  1418. {
  1419. TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );
  1420. XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
  1421. comment->_memPool = &_commentPool;
  1422. comment->SetValue( str );
  1423. return comment;
  1424. }
  1425. XMLText* XMLDocument::NewText( const char* str )
  1426. {
  1427. TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
  1428. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  1429. text->_memPool = &_textPool;
  1430. text->SetValue( str );
  1431. return text;
  1432. }
  1433. XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
  1434. {
  1435. TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );
  1436. XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
  1437. dec->_memPool = &_commentPool;
  1438. dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
  1439. return dec;
  1440. }
  1441. XMLUnknown* XMLDocument::NewUnknown( const char* str )
  1442. {
  1443. TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );
  1444. XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
  1445. unk->_memPool = &_commentPool;
  1446. unk->SetValue( str );
  1447. return unk;
  1448. }
  1449. static FILE* callfopen( const char* filepath, const char* mode )
  1450. {
  1451. TIXMLASSERT( filepath );
  1452. TIXMLASSERT( mode );
  1453. #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
  1454. FILE* fp = 0;
  1455. errno_t err = fopen_s( &fp, filepath, mode );
  1456. if ( err ) {
  1457. return 0;
  1458. }
  1459. #else
  1460. FILE* fp = fopen( filepath, mode );
  1461. #endif
  1462. return fp;
  1463. }
  1464. void XMLDocument::DeleteNode( XMLNode* node ) {
  1465. TIXMLASSERT( node );
  1466. TIXMLASSERT(node->_document == this );
  1467. if (node->_parent) {
  1468. node->_parent->DeleteChild( node );
  1469. }
  1470. else {
  1471. // Isn't in the tree.
  1472. // Use the parent delete.
  1473. // Also, we need to mark it tracked: we 'know'
  1474. // it was never used.
  1475. node->_memPool->SetTracked();
  1476. // Call the static XMLNode version:
  1477. XMLNode::DeleteNode(node);
  1478. }
  1479. }
  1480. XMLError XMLDocument::LoadFile( const char* filename )
  1481. {
  1482. Clear();
  1483. FILE* fp = callfopen( filename, "rb" );
  1484. if ( !fp ) {
  1485. SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
  1486. return _errorID;
  1487. }
  1488. LoadFile( fp );
  1489. fclose( fp );
  1490. return _errorID;
  1491. }
  1492. XMLError XMLDocument::LoadFile( FILE* fp )
  1493. {
  1494. Clear();
  1495. fseek( fp, 0, SEEK_SET );
  1496. if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
  1497. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1498. return _errorID;
  1499. }
  1500. fseek( fp, 0, SEEK_END );
  1501. const long filelength = ftell( fp );
  1502. fseek( fp, 0, SEEK_SET );
  1503. if ( filelength == -1L ) {
  1504. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1505. return _errorID;
  1506. }
  1507. const size_t size = filelength;
  1508. if ( size == 0 ) {
  1509. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1510. return _errorID;
  1511. }
  1512. _charBuffer = new char[size+1];
  1513. size_t read = fread( _charBuffer, 1, size, fp );
  1514. if ( read != size ) {
  1515. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1516. return _errorID;
  1517. }
  1518. _charBuffer[size] = 0;
  1519. const char* p = _charBuffer;
  1520. p = XMLUtil::SkipWhiteSpace( p );
  1521. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1522. if ( !p || !*p ) {
  1523. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1524. return _errorID;
  1525. }
  1526. ParseDeep( _charBuffer + (p-_charBuffer), 0 );
  1527. return _errorID;
  1528. }
  1529. XMLError XMLDocument::SaveFile( const char* filename, bool compact )
  1530. {
  1531. FILE* fp = callfopen( filename, "w" );
  1532. if ( !fp ) {
  1533. SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
  1534. return _errorID;
  1535. }
  1536. SaveFile(fp, compact);
  1537. fclose( fp );
  1538. return _errorID;
  1539. }
  1540. XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
  1541. {
  1542. XMLPrinter stream( fp, compact );
  1543. Print( &stream );
  1544. return _errorID;
  1545. }
  1546. XMLError XMLDocument::Parse( const char* p, size_t len )
  1547. {
  1548. Clear();
  1549. if ( len == 0 || !p || !*p ) {
  1550. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1551. return _errorID;
  1552. }
  1553. if ( len == (size_t)(-1) ) {
  1554. len = strlen( p );
  1555. }
  1556. _charBuffer = new char[ len+1 ];
  1557. memcpy( _charBuffer, p, len );
  1558. _charBuffer[len] = 0;
  1559. const char* start = p;
  1560. p = XMLUtil::SkipWhiteSpace( p );
  1561. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1562. if ( !p || !*p ) {
  1563. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1564. return _errorID;
  1565. }
  1566. ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc.
  1567. ParseDeep( _charBuffer+delta, 0 );
  1568. if ( Error() ) {
  1569. // clean up now essentially dangling memory.
  1570. // and the parse fail can put objects in the
  1571. // pools that are dead and inaccessible.
  1572. DeleteChildren();
  1573. _elementPool.Clear();
  1574. _attributePool.Clear();
  1575. _textPool.Clear();
  1576. _commentPool.Clear();
  1577. }
  1578. return _errorID;
  1579. }
  1580. void XMLDocument::Print( XMLPrinter* streamer ) const
  1581. {
  1582. XMLPrinter stdStreamer( stdout );
  1583. if ( !streamer ) {
  1584. streamer = &stdStreamer;
  1585. }
  1586. Accept( streamer );
  1587. }
  1588. void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
  1589. {
  1590. TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
  1591. _errorID = error;
  1592. _errorStr1 = str1;
  1593. _errorStr2 = str2;
  1594. }
  1595. const char* XMLDocument::ErrorName() const
  1596. {
  1597. TIXMLASSERT( _errorID >= 0 && _errorID < XML_ERROR_COUNT );
  1598. return _errorNames[_errorID];
  1599. }
  1600. void XMLDocument::PrintError() const
  1601. {
  1602. if ( Error() ) {
  1603. static const int LEN = 20;
  1604. char buf1[LEN] = { 0 };
  1605. char buf2[LEN] = { 0 };
  1606. if ( _errorStr1 ) {
  1607. TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
  1608. }
  1609. if ( _errorStr2 ) {
  1610. TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
  1611. }
  1612. printf( "XMLDocument error id=%d '%s' str1=%s str2=%s\n",
  1613. _errorID, ErrorName(), buf1, buf2 );
  1614. }
  1615. }
  1616. XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
  1617. _elementJustOpened( false ),
  1618. _firstElement( true ),
  1619. _fp( file ),
  1620. _depth( depth ),
  1621. _textDepth( -1 ),
  1622. _processEntities( true ),
  1623. _compactMode( compact )
  1624. {
  1625. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1626. _entityFlag[i] = false;
  1627. _restrictedEntityFlag[i] = false;
  1628. }
  1629. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1630. const char entityValue = entities[i].value;
  1631. TIXMLASSERT( 0 <= entityValue && entityValue < ENTITY_RANGE );
  1632. _entityFlag[ (unsigned char)entityValue ] = true;
  1633. }
  1634. _restrictedEntityFlag[(unsigned char)'&'] = true;
  1635. _restrictedEntityFlag[(unsigned char)'<'] = true;
  1636. _restrictedEntityFlag[(unsigned char)'>'] = true; // not required, but consistency is nice
  1637. _buffer.Push( 0 );
  1638. }
  1639. void XMLPrinter::Print( const char* format, ... )
  1640. {
  1641. va_list va;
  1642. va_start( va, format );
  1643. if ( _fp ) {
  1644. vfprintf( _fp, format, va );
  1645. }
  1646. else {
  1647. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1648. #if defined(WINCE)
  1649. int len = 512;
  1650. do {
  1651. len = len*2;
  1652. char* str = new char[len]();
  1653. len = _vsnprintf(str, len, format, va);
  1654. delete[] str;
  1655. }while (len < 0);
  1656. #else
  1657. int len = _vscprintf( format, va );
  1658. #endif
  1659. #else
  1660. int len = vsnprintf( 0, 0, format, va );
  1661. #endif
  1662. // Close out and re-start the va-args
  1663. va_end( va );
  1664. va_start( va, format );
  1665. char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator.
  1666. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1667. #if defined(WINCE)
  1668. _vsnprintf( p, len+1, format, va );
  1669. #else
  1670. vsnprintf_s( p, len+1, _TRUNCATE, format, va );
  1671. #endif
  1672. #else
  1673. vsnprintf( p, len+1, format, va );
  1674. #endif
  1675. }
  1676. va_end( va );
  1677. }
  1678. void XMLPrinter::PrintSpace( int depth )
  1679. {
  1680. for( int i=0; i<depth; ++i ) {
  1681. Print( " " );
  1682. }
  1683. }
  1684. void XMLPrinter::PrintString( const char* p, bool restricted )
  1685. {
  1686. // Look for runs of bytes between entities to print.
  1687. const char* q = p;
  1688. const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
  1689. if ( _processEntities ) {
  1690. while ( *q ) {
  1691. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1692. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1693. // Check for entities. If one is found, flush
  1694. // the stream up until the entity, write the
  1695. // entity, and keep looking.
  1696. if ( flag[(unsigned char)(*q)] ) {
  1697. while ( p < q ) {
  1698. Print( "%c", *p );
  1699. ++p;
  1700. }
  1701. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1702. if ( entities[i].value == *q ) {
  1703. Print( "&%s;", entities[i].pattern );
  1704. break;
  1705. }
  1706. }
  1707. ++p;
  1708. }
  1709. }
  1710. ++q;
  1711. }
  1712. }
  1713. // Flush the remaining string. This will be the entire
  1714. // string if an entity wasn't found.
  1715. if ( !_processEntities || (q-p > 0) ) {
  1716. Print( "%s", p );
  1717. }
  1718. }
  1719. void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
  1720. {
  1721. if ( writeBOM ) {
  1722. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1723. Print( "%s", bom );
  1724. }
  1725. if ( writeDec ) {
  1726. PushDeclaration( "xml version=\"1.0\"" );
  1727. }
  1728. }
  1729. void XMLPrinter::OpenElement( const char* name, bool compactMode )
  1730. {
  1731. SealElementIfJustOpened();
  1732. _stack.Push( name );
  1733. if ( _textDepth < 0 && !_firstElement && !compactMode ) {
  1734. Print( "\n" );
  1735. }
  1736. if ( !compactMode ) {
  1737. PrintSpace( _depth );
  1738. }
  1739. Print( "<%s", name );
  1740. _elementJustOpened = true;
  1741. _firstElement = false;
  1742. ++_depth;
  1743. }
  1744. void XMLPrinter::PushAttribute( const char* name, const char* value )
  1745. {
  1746. TIXMLASSERT( _elementJustOpened );
  1747. Print( " %s=\"", name );
  1748. PrintString( value, false );
  1749. Print( "\"" );
  1750. }
  1751. void XMLPrinter::PushAttribute( const char* name, int v )
  1752. {
  1753. char buf[BUF_SIZE];
  1754. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1755. PushAttribute( name, buf );
  1756. }
  1757. void XMLPrinter::PushAttribute( const char* name, unsigned v )
  1758. {
  1759. char buf[BUF_SIZE];
  1760. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1761. PushAttribute( name, buf );
  1762. }
  1763. void XMLPrinter::PushAttribute( const char* name, bool v )
  1764. {
  1765. char buf[BUF_SIZE];
  1766. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1767. PushAttribute( name, buf );
  1768. }
  1769. void XMLPrinter::PushAttribute( const char* name, double v )
  1770. {
  1771. char buf[BUF_SIZE];
  1772. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1773. PushAttribute( name, buf );
  1774. }
  1775. void XMLPrinter::CloseElement( bool compactMode )
  1776. {
  1777. --_depth;
  1778. const char* name = _stack.Pop();
  1779. if ( _elementJustOpened ) {
  1780. Print( "/>" );
  1781. }
  1782. else {
  1783. if ( _textDepth < 0 && !compactMode) {
  1784. Print( "\n" );
  1785. PrintSpace( _depth );
  1786. }
  1787. Print( "</%s>", name );
  1788. }
  1789. if ( _textDepth == _depth ) {
  1790. _textDepth = -1;
  1791. }
  1792. if ( _depth == 0 && !compactMode) {
  1793. Print( "\n" );
  1794. }
  1795. _elementJustOpened = false;
  1796. }
  1797. void XMLPrinter::SealElementIfJustOpened()
  1798. {
  1799. if ( !_elementJustOpened ) {
  1800. return;
  1801. }
  1802. _elementJustOpened = false;
  1803. Print( ">" );
  1804. }
  1805. void XMLPrinter::PushText( const char* text, bool cdata )
  1806. {
  1807. _textDepth = _depth-1;
  1808. SealElementIfJustOpened();
  1809. if ( cdata ) {
  1810. Print( "<![CDATA[" );
  1811. Print( "%s", text );
  1812. Print( "]]>" );
  1813. }
  1814. else {
  1815. PrintString( text, true );
  1816. }
  1817. }
  1818. void XMLPrinter::PushText( int value )
  1819. {
  1820. char buf[BUF_SIZE];
  1821. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1822. PushText( buf, false );
  1823. }
  1824. void XMLPrinter::PushText( unsigned value )
  1825. {
  1826. char buf[BUF_SIZE];
  1827. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1828. PushText( buf, false );
  1829. }
  1830. void XMLPrinter::PushText( bool value )
  1831. {
  1832. char buf[BUF_SIZE];
  1833. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1834. PushText( buf, false );
  1835. }
  1836. void XMLPrinter::PushText( float value )
  1837. {
  1838. char buf[BUF_SIZE];
  1839. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1840. PushText( buf, false );
  1841. }
  1842. void XMLPrinter::PushText( double value )
  1843. {
  1844. char buf[BUF_SIZE];
  1845. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1846. PushText( buf, false );
  1847. }
  1848. void XMLPrinter::PushComment( const char* comment )
  1849. {
  1850. SealElementIfJustOpened();
  1851. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1852. Print( "\n" );
  1853. PrintSpace( _depth );
  1854. }
  1855. _firstElement = false;
  1856. Print( "<!--%s-->", comment );
  1857. }
  1858. void XMLPrinter::PushDeclaration( const char* value )
  1859. {
  1860. SealElementIfJustOpened();
  1861. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1862. Print( "\n" );
  1863. PrintSpace( _depth );
  1864. }
  1865. _firstElement = false;
  1866. Print( "<?%s?>", value );
  1867. }
  1868. void XMLPrinter::PushUnknown( const char* value )
  1869. {
  1870. SealElementIfJustOpened();
  1871. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1872. Print( "\n" );
  1873. PrintSpace( _depth );
  1874. }
  1875. _firstElement = false;
  1876. Print( "<!%s>", value );
  1877. }
  1878. bool XMLPrinter::VisitEnter( const XMLDocument& doc )
  1879. {
  1880. _processEntities = doc.ProcessEntities();
  1881. if ( doc.HasBOM() ) {
  1882. PushHeader( true, false );
  1883. }
  1884. return true;
  1885. }
  1886. bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1887. {
  1888. const XMLElement* parentElem = element.Parent()->ToElement();
  1889. bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode;
  1890. OpenElement( element.Name(), compactMode );
  1891. while ( attribute ) {
  1892. PushAttribute( attribute->Name(), attribute->Value() );
  1893. attribute = attribute->Next();
  1894. }
  1895. return true;
  1896. }
  1897. bool XMLPrinter::VisitExit( const XMLElement& element )
  1898. {
  1899. CloseElement( CompactMode(element) );
  1900. return true;
  1901. }
  1902. bool XMLPrinter::Visit( const XMLText& text )
  1903. {
  1904. PushText( text.Value(), text.CData() );
  1905. return true;
  1906. }
  1907. bool XMLPrinter::Visit( const XMLComment& comment )
  1908. {
  1909. PushComment( comment.Value() );
  1910. return true;
  1911. }
  1912. bool XMLPrinter::Visit( const XMLDeclaration& declaration )
  1913. {
  1914. PushDeclaration( declaration.Value() );
  1915. return true;
  1916. }
  1917. bool XMLPrinter::Visit( const XMLUnknown& unknown )
  1918. {
  1919. PushUnknown( unknown.Value() );
  1920. return true;
  1921. }
  1922. } // namespace tinyxml2