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