1
0

tinyxml2.cpp 78 KB

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