tinyxml2.cpp 70 KB

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