You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tinyxml2.cpp 59KB

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