tinyxml2.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  1. /*
  2. Original code by Lee Thomason (www.grinninglizard.com)
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any
  5. damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any
  7. purpose, including commercial applications, and to alter it and
  8. redistribute it freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must
  10. not claim that you wrote the original software. If you use this
  11. software in a product, an acknowledgment in the product documentation
  12. would be appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and
  14. must not be misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #include"stdafx.h"
  19. #include "tinyxml2.h"
  20. #include <new> // yes, this one new style header, is in the Android SDK.
  21. # ifdef ANDROID_NDK
  22. # include <stddef.h>
  23. #else
  24. # include <cstddef>
  25. #endif
  26. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  27. static const char LF = LINE_FEED;
  28. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  29. static const char CR = CARRIAGE_RETURN;
  30. static const char SINGLE_QUOTE = '\'';
  31. static const char DOUBLE_QUOTE = '\"';
  32. // Bunch of unicode info at:
  33. // http://www.unicode.org/faq/utf_bom.html
  34. // ef bb bf (Microsoft "lead bytes") - designates UTF-8
  35. static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  36. static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  37. static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  38. #define DELETE_NODE( node ) { \
  39. if ( node ) { \
  40. MemPool* pool = node->_memPool; \
  41. node->~XMLNode(); \
  42. pool->Free( node ); \
  43. } \
  44. }
  45. #define DELETE_ATTRIBUTE( attrib ) { \
  46. if ( attrib ) { \
  47. MemPool* pool = attrib->_memPool; \
  48. attrib->~XMLAttribute(); \
  49. pool->Free( attrib ); \
  50. } \
  51. }
  52. namespace tinyxml2
  53. {
  54. struct Entity {
  55. const char* pattern;
  56. int length;
  57. char value;
  58. };
  59. static const int NUM_ENTITIES = 5;
  60. static const Entity entities[NUM_ENTITIES] = {
  61. { "quot", 4, DOUBLE_QUOTE },
  62. { "amp", 3, '&' },
  63. { "apos", 4, SINGLE_QUOTE },
  64. { "lt", 2, '<' },
  65. { "gt", 2, '>' }
  66. };
  67. StrPair::~StrPair()
  68. {
  69. Reset();
  70. }
  71. void StrPair::Reset()
  72. {
  73. if ( _flags & NEEDS_DELETE ) {
  74. delete [] _start;
  75. }
  76. _flags = 0;
  77. _start = 0;
  78. _end = 0;
  79. }
  80. void StrPair::SetStr( const char* str, int flags )
  81. {
  82. Reset();
  83. size_t len = strlen( str );
  84. _start = new char[ len+1 ];
  85. memcpy( _start, str, len+1 );
  86. _end = _start + len;
  87. _flags = flags | NEEDS_DELETE;
  88. }
  89. char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
  90. {
  91. TIXMLASSERT( endTag && *endTag );
  92. char* start = p; // fixme: hides a member
  93. char endChar = *endTag;
  94. size_t length = strlen( endTag );
  95. // Inner loop of text parsing.
  96. while ( *p ) {
  97. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  98. Set( start, p, strFlags );
  99. return p + length;
  100. }
  101. ++p;
  102. }
  103. return 0;
  104. }
  105. char* StrPair::ParseName( char* p )
  106. {
  107. char* start = p;
  108. if ( !start || !(*start) ) {
  109. return 0;
  110. }
  111. while( *p && ( p == start ? XMLUtil::IsNameStartChar( *p ) : XMLUtil::IsNameChar( *p ) )) {
  112. ++p;
  113. }
  114. if ( p > start ) {
  115. Set( start, p, 0 );
  116. return p;
  117. }
  118. return 0;
  119. }
  120. void StrPair::CollapseWhitespace()
  121. {
  122. // Trim leading space.
  123. _start = XMLUtil::SkipWhiteSpace( _start );
  124. if ( _start && *_start ) {
  125. char* p = _start; // the read pointer
  126. char* q = _start; // the write pointer
  127. while( *p ) {
  128. if ( XMLUtil::IsWhiteSpace( *p )) {
  129. p = XMLUtil::SkipWhiteSpace( p );
  130. if ( *p == 0 ) {
  131. break; // don't write to q; this trims the trailing space.
  132. }
  133. *q = ' ';
  134. ++q;
  135. }
  136. *q = *p;
  137. ++q;
  138. ++p;
  139. }
  140. *q = 0;
  141. }
  142. }
  143. const char* StrPair::GetStr()
  144. {
  145. if ( _flags & NEEDS_FLUSH ) {
  146. *_end = 0;
  147. _flags ^= NEEDS_FLUSH;
  148. if ( _flags ) {
  149. char* p = _start; // the read pointer
  150. char* q = _start; // the write pointer
  151. while( p < _end ) {
  152. if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
  153. // CR-LF pair becomes LF
  154. // CR alone becomes LF
  155. // LF-CR becomes LF
  156. if ( *(p+1) == LF ) {
  157. p += 2;
  158. }
  159. else {
  160. ++p;
  161. }
  162. *q++ = LF;
  163. }
  164. else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
  165. if ( *(p+1) == CR ) {
  166. p += 2;
  167. }
  168. else {
  169. ++p;
  170. }
  171. *q++ = LF;
  172. }
  173. else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
  174. // Entities handled by tinyXML2:
  175. // - special entities in the entity table [in/out]
  176. // - numeric character reference [in]
  177. // &#20013; or &#x4e2d;
  178. if ( *(p+1) == '#' ) {
  179. char buf[10] = { 0 };
  180. int len;
  181. p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
  182. for( int i=0; i<len; ++i ) {
  183. *q++ = buf[i];
  184. }
  185. TIXMLASSERT( q <= p );
  186. }
  187. else {
  188. int i=0;
  189. for(; i<NUM_ENTITIES; ++i ) {
  190. if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
  191. && *(p+entities[i].length+1) == ';' ) {
  192. // Found an entity convert;
  193. *q = entities[i].value;
  194. ++q;
  195. p += entities[i].length + 2;
  196. break;
  197. }
  198. }
  199. if ( i == NUM_ENTITIES ) {
  200. // fixme: treat as error?
  201. ++p;
  202. ++q;
  203. }
  204. }
  205. }
  206. else {
  207. *q = *p;
  208. ++p;
  209. ++q;
  210. }
  211. }
  212. *q = 0;
  213. }
  214. // The loop below has plenty going on, and this
  215. // is a less useful mode. Break it out.
  216. if ( _flags & COLLAPSE_WHITESPACE ) {
  217. CollapseWhitespace();
  218. }
  219. _flags = (_flags & NEEDS_DELETE);
  220. }
  221. return _start;
  222. }
  223. // --------- XMLUtil ----------- //
  224. const char* XMLUtil::ReadBOM( const char* p, bool* bom )
  225. {
  226. *bom = false;
  227. const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
  228. // Check for BOM:
  229. if ( *(pu+0) == TIXML_UTF_LEAD_0
  230. && *(pu+1) == TIXML_UTF_LEAD_1
  231. && *(pu+2) == TIXML_UTF_LEAD_2 ) {
  232. *bom = true;
  233. p += 3;
  234. }
  235. return p;
  236. }
  237. void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
  238. {
  239. const unsigned long BYTE_MASK = 0xBF;
  240. const unsigned long BYTE_MARK = 0x80;
  241. const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  242. if (input < 0x80) {
  243. *length = 1;
  244. }
  245. else if ( input < 0x800 ) {
  246. *length = 2;
  247. }
  248. else if ( input < 0x10000 ) {
  249. *length = 3;
  250. }
  251. else if ( input < 0x200000 ) {
  252. *length = 4;
  253. }
  254. else {
  255. *length = 0; // This code won't covert this correctly anyway.
  256. return;
  257. }
  258. output += *length;
  259. // Scary scary fall throughs.
  260. switch (*length) {
  261. case 4:
  262. --output;
  263. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  264. input >>= 6;
  265. case 3:
  266. --output;
  267. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  268. input >>= 6;
  269. case 2:
  270. --output;
  271. *output = (char)((input | BYTE_MARK) & BYTE_MASK);
  272. input >>= 6;
  273. case 1:
  274. --output;
  275. *output = (char)(input | FIRST_BYTE_MARK[*length]);
  276. default:
  277. break;
  278. }
  279. }
  280. const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
  281. {
  282. // Presume an entity, and pull it out.
  283. *length = 0;
  284. if ( *(p+1) == '#' && *(p+2) ) {
  285. unsigned long ucs = 0;
  286. ptrdiff_t delta = 0;
  287. unsigned mult = 1;
  288. if ( *(p+2) == 'x' ) {
  289. // Hexadecimal.
  290. if ( !*(p+3) ) {
  291. return 0;
  292. }
  293. const char* q = p+3;
  294. q = strchr( q, ';' );
  295. if ( !q || !*q ) {
  296. return 0;
  297. }
  298. delta = q-p;
  299. --q;
  300. while ( *q != 'x' ) {
  301. if ( *q >= '0' && *q <= '9' ) {
  302. ucs += mult * (*q - '0');
  303. }
  304. else if ( *q >= 'a' && *q <= 'f' ) {
  305. ucs += mult * (*q - 'a' + 10);
  306. }
  307. else if ( *q >= 'A' && *q <= 'F' ) {
  308. ucs += mult * (*q - 'A' + 10 );
  309. }
  310. else {
  311. return 0;
  312. }
  313. mult *= 16;
  314. --q;
  315. }
  316. }
  317. else {
  318. // Decimal.
  319. if ( !*(p+2) ) {
  320. return 0;
  321. }
  322. const char* q = p+2;
  323. q = strchr( q, ';' );
  324. if ( !q || !*q ) {
  325. return 0;
  326. }
  327. delta = q-p;
  328. --q;
  329. while ( *q != '#' ) {
  330. if ( *q >= '0' && *q <= '9' ) {
  331. ucs += mult * (*q - '0');
  332. }
  333. else {
  334. return 0;
  335. }
  336. mult *= 10;
  337. --q;
  338. }
  339. }
  340. // convert the UCS to UTF-8
  341. ConvertUTF32ToUTF8( ucs, value, length );
  342. return p + delta + 1;
  343. }
  344. return p+1;
  345. }
  346. void XMLUtil::ToStr( int v, char* buffer, int bufferSize )
  347. {
  348. TIXML_SNPRINTF( buffer, bufferSize, "%d", v );
  349. }
  350. void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
  351. {
  352. TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
  353. }
  354. void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
  355. {
  356. TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 );
  357. }
  358. void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
  359. {
  360. TIXML_SNPRINTF( buffer, bufferSize, "%f", v );
  361. }
  362. void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
  363. {
  364. TIXML_SNPRINTF( buffer, bufferSize, "%f", v );
  365. }
  366. bool XMLUtil::ToInt( const char* str, int* value )
  367. {
  368. if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {
  369. return true;
  370. }
  371. return false;
  372. }
  373. bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
  374. {
  375. if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
  376. return true;
  377. }
  378. return false;
  379. }
  380. bool XMLUtil::ToBool( const char* str, bool* value )
  381. {
  382. int ival = 0;
  383. if ( ToInt( str, &ival )) {
  384. *value = (ival==0) ? false : true;
  385. return true;
  386. }
  387. if ( StringEqual( str, "true" ) ) {
  388. *value = true;
  389. return true;
  390. }
  391. else if ( StringEqual( str, "false" ) ) {
  392. *value = false;
  393. return true;
  394. }
  395. return false;
  396. }
  397. bool XMLUtil::ToFloat( const char* str, float* value )
  398. {
  399. if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {
  400. return true;
  401. }
  402. return false;
  403. }
  404. bool XMLUtil::ToDouble( const char* str, double* value )
  405. {
  406. if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {
  407. return true;
  408. }
  409. return false;
  410. }
  411. char* XMLDocument::Identify( char* p, XMLNode** node )
  412. {
  413. XMLNode* returnNode = 0;
  414. char* start = p;
  415. p = XMLUtil::SkipWhiteSpace( p );
  416. if( !p || !*p ) {
  417. return p;
  418. }
  419. // What is this thing?
  420. // - Elements start with a letter or underscore, but xml is reserved.
  421. // - Comments: <!--
  422. // - Declaration: <?
  423. // - Everything else is unknown to tinyxml.
  424. //
  425. static const char* xmlHeader = { "<?" };
  426. static const char* commentHeader = { "<!--" };
  427. static const char* dtdHeader = { "<!" };
  428. static const char* cdataHeader = { "<![CDATA[" };
  429. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  430. static const int xmlHeaderLen = 2;
  431. static const int commentHeaderLen = 4;
  432. static const int dtdHeaderLen = 2;
  433. static const int cdataHeaderLen = 9;
  434. static const int elementHeaderLen = 1;
  435. #if defined(_MSC_VER)
  436. #pragma warning ( push )
  437. #pragma warning ( disable : 4127 )
  438. #endif
  439. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
  440. TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
  441. #if defined(_MSC_VER)
  442. #pragma warning (pop)
  443. #endif
  444. if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
  445. returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
  446. returnNode->_memPool = &_commentPool;
  447. p += xmlHeaderLen;
  448. }
  449. else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
  450. returnNode = new (_commentPool.Alloc()) XMLComment( this );
  451. returnNode->_memPool = &_commentPool;
  452. p += commentHeaderLen;
  453. }
  454. else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
  455. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  456. returnNode = text;
  457. returnNode->_memPool = &_textPool;
  458. p += cdataHeaderLen;
  459. text->SetCData( true );
  460. }
  461. else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
  462. returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
  463. returnNode->_memPool = &_commentPool;
  464. p += dtdHeaderLen;
  465. }
  466. else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
  467. returnNode = new (_elementPool.Alloc()) XMLElement( this );
  468. returnNode->_memPool = &_elementPool;
  469. p += elementHeaderLen;
  470. }
  471. else {
  472. returnNode = new (_textPool.Alloc()) XMLText( this );
  473. returnNode->_memPool = &_textPool;
  474. p = start; // Back it up, all the text counts.
  475. }
  476. *node = returnNode;
  477. return p;
  478. }
  479. bool XMLDocument::Accept( XMLVisitor* visitor ) const
  480. {
  481. if ( visitor->VisitEnter( *this ) ) {
  482. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  483. if ( !node->Accept( visitor ) ) {
  484. break;
  485. }
  486. }
  487. }
  488. return visitor->VisitExit( *this );
  489. }
  490. // --------- XMLNode ----------- //
  491. XMLNode::XMLNode( XMLDocument* doc ) :
  492. _document( doc ),
  493. _parent( 0 ),
  494. _firstChild( 0 ), _lastChild( 0 ),
  495. _prev( 0 ), _next( 0 ),
  496. _memPool( 0 )
  497. {
  498. }
  499. XMLNode::~XMLNode()
  500. {
  501. DeleteChildren();
  502. if ( _parent ) {
  503. _parent->Unlink( this );
  504. }
  505. }
  506. void XMLNode::SetValue( const char* str, bool staticMem )
  507. {
  508. if ( staticMem ) {
  509. _value.SetInternedStr( str );
  510. }
  511. else {
  512. _value.SetStr( str );
  513. }
  514. }
  515. void XMLNode::DeleteChildren()
  516. {
  517. while( _firstChild ) {
  518. XMLNode* node = _firstChild;
  519. Unlink( node );
  520. DELETE_NODE( node );
  521. }
  522. _firstChild = _lastChild = 0;
  523. }
  524. void XMLNode::Unlink( XMLNode* child )
  525. {
  526. TIXMLASSERT( child->_parent == this );
  527. if ( child == _firstChild ) {
  528. _firstChild = _firstChild->_next;
  529. }
  530. if ( child == _lastChild ) {
  531. _lastChild = _lastChild->_prev;
  532. }
  533. if ( child->_prev ) {
  534. child->_prev->_next = child->_next;
  535. }
  536. if ( child->_next ) {
  537. child->_next->_prev = child->_prev;
  538. }
  539. child->_parent = 0;
  540. }
  541. void XMLNode::DeleteChild( XMLNode* node )
  542. {
  543. TIXMLASSERT( node->_parent == this );
  544. DELETE_NODE( node );
  545. }
  546. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  547. {
  548. if ( _lastChild ) {
  549. TIXMLASSERT( _firstChild );
  550. TIXMLASSERT( _lastChild->_next == 0 );
  551. _lastChild->_next = addThis;
  552. addThis->_prev = _lastChild;
  553. _lastChild = addThis;
  554. addThis->_next = 0;
  555. }
  556. else {
  557. TIXMLASSERT( _firstChild == 0 );
  558. _firstChild = _lastChild = addThis;
  559. addThis->_prev = 0;
  560. addThis->_next = 0;
  561. }
  562. addThis->_parent = this;
  563. addThis->_memPool->SetTracked();
  564. return addThis;
  565. }
  566. XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
  567. {
  568. if ( _firstChild ) {
  569. TIXMLASSERT( _lastChild );
  570. TIXMLASSERT( _firstChild->_prev == 0 );
  571. _firstChild->_prev = addThis;
  572. addThis->_next = _firstChild;
  573. _firstChild = addThis;
  574. addThis->_prev = 0;
  575. }
  576. else {
  577. TIXMLASSERT( _lastChild == 0 );
  578. _firstChild = _lastChild = addThis;
  579. addThis->_prev = 0;
  580. addThis->_next = 0;
  581. }
  582. addThis->_parent = this;
  583. addThis->_memPool->SetTracked();
  584. return addThis;
  585. }
  586. XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
  587. {
  588. TIXMLASSERT( afterThis->_parent == this );
  589. if ( afterThis->_parent != this ) {
  590. return 0;
  591. }
  592. if ( afterThis->_next == 0 ) {
  593. // The last node or the only node.
  594. return InsertEndChild( addThis );
  595. }
  596. addThis->_prev = afterThis;
  597. addThis->_next = afterThis->_next;
  598. afterThis->_next->_prev = addThis;
  599. afterThis->_next = addThis;
  600. addThis->_parent = this;
  601. addThis->_memPool->SetTracked();
  602. return addThis;
  603. }
  604. const XMLElement* XMLNode::FirstChildElement( const char* value ) const
  605. {
  606. for( XMLNode* node=_firstChild; node; node=node->_next ) {
  607. XMLElement* element = node->ToElement();
  608. if ( element ) {
  609. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  610. return element;
  611. }
  612. }
  613. }
  614. return 0;
  615. }
  616. const XMLElement* XMLNode::LastChildElement( const char* value ) const
  617. {
  618. for( XMLNode* node=_lastChild; node; node=node->_prev ) {
  619. XMLElement* element = node->ToElement();
  620. if ( element ) {
  621. if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
  622. return element;
  623. }
  624. }
  625. }
  626. return 0;
  627. }
  628. const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
  629. {
  630. for( XMLNode* element=this->_next; element; element = element->_next ) {
  631. if ( element->ToElement()
  632. && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
  633. return element->ToElement();
  634. }
  635. }
  636. return 0;
  637. }
  638. const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
  639. {
  640. for( XMLNode* element=_prev; element; element = element->_prev ) {
  641. if ( element->ToElement()
  642. && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
  643. return element->ToElement();
  644. }
  645. }
  646. return 0;
  647. }
  648. char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
  649. {
  650. // This is a recursive method, but thinking about it "at the current level"
  651. // it is a pretty simple flat list:
  652. // <foo/>
  653. // <!-- comment -->
  654. //
  655. // With a special case:
  656. // <foo>
  657. // </foo>
  658. // <!-- comment -->
  659. //
  660. // Where the closing element (/foo) *must* be the next thing after the opening
  661. // element, and the names must match. BUT the tricky bit is that the closing
  662. // element will be read by the child.
  663. //
  664. // 'endTag' is the end tag for this node, it is returned by a call to a child.
  665. // 'parentEnd' is the end tag for the parent, which is filled in and returned.
  666. while( p && *p ) {
  667. XMLNode* node = 0;
  668. p = _document->Identify( p, &node );
  669. if ( p == 0 || node == 0 ) {
  670. break;
  671. }
  672. StrPair endTag;
  673. p = node->ParseDeep( p, &endTag );
  674. if ( !p ) {
  675. DELETE_NODE( node );
  676. node = 0;
  677. if ( !_document->Error() ) {
  678. _document->SetError( XML_ERROR_PARSING, 0, 0 );
  679. }
  680. break;
  681. }
  682. // We read the end tag. Return it to the parent.
  683. if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
  684. if ( parentEnd ) {
  685. *parentEnd = static_cast<XMLElement*>(node)->_value;
  686. }
  687. node->_memPool->SetTracked(); // created and then immediately deleted.
  688. DELETE_NODE( node );
  689. return p;
  690. }
  691. // Handle an end tag returned to this level.
  692. // And handle a bunch of annoying errors.
  693. XMLElement* ele = node->ToElement();
  694. if ( ele ) {
  695. if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
  696. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  697. p = 0;
  698. }
  699. else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
  700. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  701. p = 0;
  702. }
  703. else if ( !endTag.Empty() ) {
  704. if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
  705. _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
  706. p = 0;
  707. }
  708. }
  709. }
  710. if ( p == 0 ) {
  711. DELETE_NODE( node );
  712. node = 0;
  713. }
  714. if ( node ) {
  715. this->InsertEndChild( node );
  716. }
  717. }
  718. return 0;
  719. }
  720. // --------- XMLText ---------- //
  721. char* XMLText::ParseDeep( char* p, StrPair* )
  722. {
  723. const char* start = p;
  724. if ( this->CData() ) {
  725. p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  726. if ( !p ) {
  727. _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
  728. }
  729. return p;
  730. }
  731. else {
  732. int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
  733. if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
  734. flags |= StrPair::COLLAPSE_WHITESPACE;
  735. }
  736. p = _value.ParseText( p, "<", flags );
  737. if ( !p ) {
  738. _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
  739. }
  740. if ( p && *p ) {
  741. return p-1;
  742. }
  743. }
  744. return 0;
  745. }
  746. XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
  747. {
  748. if ( !doc ) {
  749. doc = _document;
  750. }
  751. XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
  752. text->SetCData( this->CData() );
  753. return text;
  754. }
  755. bool XMLText::ShallowEqual( const XMLNode* compare ) const
  756. {
  757. return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
  758. }
  759. bool XMLText::Accept( XMLVisitor* visitor ) const
  760. {
  761. return visitor->Visit( *this );
  762. }
  763. // --------- XMLComment ---------- //
  764. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  765. {
  766. }
  767. XMLComment::~XMLComment()
  768. {
  769. }
  770. char* XMLComment::ParseDeep( char* p, StrPair* )
  771. {
  772. // Comment parses as text.
  773. const char* start = p;
  774. p = _value.ParseText( p, "-->", StrPair::COMMENT );
  775. if ( p == 0 ) {
  776. _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
  777. }
  778. return p;
  779. }
  780. XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
  781. {
  782. if ( !doc ) {
  783. doc = _document;
  784. }
  785. XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
  786. return comment;
  787. }
  788. bool XMLComment::ShallowEqual( const XMLNode* compare ) const
  789. {
  790. return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() ));
  791. }
  792. bool XMLComment::Accept( XMLVisitor* visitor ) const
  793. {
  794. return visitor->Visit( *this );
  795. }
  796. // --------- XMLDeclaration ---------- //
  797. XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
  798. {
  799. }
  800. XMLDeclaration::~XMLDeclaration()
  801. {
  802. //printf( "~XMLDeclaration\n" );
  803. }
  804. char* XMLDeclaration::ParseDeep( char* p, StrPair* )
  805. {
  806. // Declaration parses as text.
  807. const char* start = p;
  808. p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  809. if ( p == 0 ) {
  810. _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
  811. }
  812. return p;
  813. }
  814. XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
  815. {
  816. if ( !doc ) {
  817. doc = _document;
  818. }
  819. XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
  820. return dec;
  821. }
  822. bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
  823. {
  824. return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() ));
  825. }
  826. bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
  827. {
  828. return visitor->Visit( *this );
  829. }
  830. // --------- XMLUnknown ---------- //
  831. XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
  832. {
  833. }
  834. XMLUnknown::~XMLUnknown()
  835. {
  836. }
  837. char* XMLUnknown::ParseDeep( char* p, StrPair* )
  838. {
  839. // Unknown parses as text.
  840. const char* start = p;
  841. p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
  842. if ( !p ) {
  843. _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
  844. }
  845. return p;
  846. }
  847. XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
  848. {
  849. if ( !doc ) {
  850. doc = _document;
  851. }
  852. XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
  853. return text;
  854. }
  855. bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
  856. {
  857. return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() ));
  858. }
  859. bool XMLUnknown::Accept( XMLVisitor* visitor ) const
  860. {
  861. return visitor->Visit( *this );
  862. }
  863. // --------- XMLAttribute ---------- //
  864. char* XMLAttribute::ParseDeep( char* p, bool processEntities )
  865. {
  866. // Parse using the name rules: bug fix, was using ParseText before
  867. p = _name.ParseName( p );
  868. if ( !p || !*p ) {
  869. return 0;
  870. }
  871. // Skip white space before =
  872. p = XMLUtil::SkipWhiteSpace( p );
  873. if ( !p || *p != '=' ) {
  874. return 0;
  875. }
  876. ++p; // move up to opening quote
  877. p = XMLUtil::SkipWhiteSpace( p );
  878. if ( *p != '\"' && *p != '\'' ) {
  879. return 0;
  880. }
  881. char endTag[2] = { *p, 0 };
  882. ++p; // move past opening quote
  883. p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
  884. return p;
  885. }
  886. void XMLAttribute::SetName( const char* n )
  887. {
  888. _name.SetStr( n );
  889. }
  890. XMLError XMLAttribute::QueryIntValue( int* value ) const
  891. {
  892. if ( XMLUtil::ToInt( Value(), value )) {
  893. return XML_NO_ERROR;
  894. }
  895. return XML_WRONG_ATTRIBUTE_TYPE;
  896. }
  897. XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
  898. {
  899. if ( XMLUtil::ToUnsigned( Value(), value )) {
  900. return XML_NO_ERROR;
  901. }
  902. return XML_WRONG_ATTRIBUTE_TYPE;
  903. }
  904. XMLError XMLAttribute::QueryBoolValue( bool* value ) const
  905. {
  906. if ( XMLUtil::ToBool( Value(), value )) {
  907. return XML_NO_ERROR;
  908. }
  909. return XML_WRONG_ATTRIBUTE_TYPE;
  910. }
  911. XMLError XMLAttribute::QueryFloatValue( float* value ) const
  912. {
  913. if ( XMLUtil::ToFloat( Value(), value )) {
  914. return XML_NO_ERROR;
  915. }
  916. return XML_WRONG_ATTRIBUTE_TYPE;
  917. }
  918. XMLError XMLAttribute::QueryDoubleValue( double* value ) const
  919. {
  920. if ( XMLUtil::ToDouble( Value(), value )) {
  921. return XML_NO_ERROR;
  922. }
  923. return XML_WRONG_ATTRIBUTE_TYPE;
  924. }
  925. void XMLAttribute::SetAttribute( const char* v )
  926. {
  927. _value.SetStr( v );
  928. }
  929. void XMLAttribute::SetAttribute( int v )
  930. {
  931. char buf[BUF_SIZE];
  932. XMLUtil::ToStr( v, buf, BUF_SIZE );
  933. _value.SetStr( buf );
  934. }
  935. void XMLAttribute::SetAttribute( unsigned v )
  936. {
  937. char buf[BUF_SIZE];
  938. XMLUtil::ToStr( v, buf, BUF_SIZE );
  939. _value.SetStr( buf );
  940. }
  941. void XMLAttribute::SetAttribute( bool v )
  942. {
  943. char buf[BUF_SIZE];
  944. XMLUtil::ToStr( v, buf, BUF_SIZE );
  945. _value.SetStr( buf );
  946. }
  947. void XMLAttribute::SetAttribute( double v )
  948. {
  949. char buf[BUF_SIZE];
  950. XMLUtil::ToStr( v, buf, BUF_SIZE );
  951. _value.SetStr( buf );
  952. }
  953. void XMLAttribute::SetAttribute( float v )
  954. {
  955. char buf[BUF_SIZE];
  956. XMLUtil::ToStr( v, buf, BUF_SIZE );
  957. _value.SetStr( buf );
  958. }
  959. // --------- XMLElement ---------- //
  960. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  961. _closingType( 0 ),
  962. _rootAttribute( 0 )
  963. {
  964. }
  965. XMLElement::~XMLElement()
  966. {
  967. while( _rootAttribute ) {
  968. XMLAttribute* next = _rootAttribute->_next;
  969. DELETE_ATTRIBUTE( _rootAttribute );
  970. _rootAttribute = next;
  971. }
  972. }
  973. XMLAttribute* XMLElement::FindAttribute( const char* name )
  974. {
  975. XMLAttribute* a = 0;
  976. for( a=_rootAttribute; a; a = a->_next ) {
  977. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  978. return a;
  979. }
  980. }
  981. return 0;
  982. }
  983. const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
  984. {
  985. XMLAttribute* a = 0;
  986. for( a=_rootAttribute; a; a = a->_next ) {
  987. if ( XMLUtil::StringEqual( a->Name(), name ) ) {
  988. return a;
  989. }
  990. }
  991. return 0;
  992. }
  993. const char* XMLElement::Attribute( const char* name, const char* value ) const
  994. {
  995. const XMLAttribute* a = FindAttribute( name );
  996. if ( !a ) {
  997. return 0;
  998. }
  999. if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
  1000. return a->Value();
  1001. }
  1002. return 0;
  1003. }
  1004. const char* XMLElement::GetText() const
  1005. {
  1006. if ( FirstChild() && FirstChild()->ToText() ) {
  1007. return FirstChild()->ToText()->Value();
  1008. }
  1009. return 0;
  1010. }
  1011. XMLError XMLElement::QueryIntText( int* ival ) const
  1012. {
  1013. if ( FirstChild() && FirstChild()->ToText() ) {
  1014. const char* t = FirstChild()->ToText()->Value();
  1015. if ( XMLUtil::ToInt( t, ival ) ) {
  1016. return XML_SUCCESS;
  1017. }
  1018. return XML_CAN_NOT_CONVERT_TEXT;
  1019. }
  1020. return XML_NO_TEXT_NODE;
  1021. }
  1022. XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
  1023. {
  1024. if ( FirstChild() && FirstChild()->ToText() ) {
  1025. const char* t = FirstChild()->ToText()->Value();
  1026. if ( XMLUtil::ToUnsigned( t, uval ) ) {
  1027. return XML_SUCCESS;
  1028. }
  1029. return XML_CAN_NOT_CONVERT_TEXT;
  1030. }
  1031. return XML_NO_TEXT_NODE;
  1032. }
  1033. XMLError XMLElement::QueryBoolText( bool* bval ) const
  1034. {
  1035. if ( FirstChild() && FirstChild()->ToText() ) {
  1036. const char* t = FirstChild()->ToText()->Value();
  1037. if ( XMLUtil::ToBool( t, bval ) ) {
  1038. return XML_SUCCESS;
  1039. }
  1040. return XML_CAN_NOT_CONVERT_TEXT;
  1041. }
  1042. return XML_NO_TEXT_NODE;
  1043. }
  1044. XMLError XMLElement::QueryDoubleText( double* dval ) const
  1045. {
  1046. if ( FirstChild() && FirstChild()->ToText() ) {
  1047. const char* t = FirstChild()->ToText()->Value();
  1048. if ( XMLUtil::ToDouble( t, dval ) ) {
  1049. return XML_SUCCESS;
  1050. }
  1051. return XML_CAN_NOT_CONVERT_TEXT;
  1052. }
  1053. return XML_NO_TEXT_NODE;
  1054. }
  1055. XMLError XMLElement::QueryFloatText( float* fval ) const
  1056. {
  1057. if ( FirstChild() && FirstChild()->ToText() ) {
  1058. const char* t = FirstChild()->ToText()->Value();
  1059. if ( XMLUtil::ToFloat( t, fval ) ) {
  1060. return XML_SUCCESS;
  1061. }
  1062. return XML_CAN_NOT_CONVERT_TEXT;
  1063. }
  1064. return XML_NO_TEXT_NODE;
  1065. }
  1066. XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
  1067. {
  1068. XMLAttribute* last = 0;
  1069. XMLAttribute* attrib = 0;
  1070. for( attrib = _rootAttribute;
  1071. attrib;
  1072. last = attrib, attrib = attrib->_next ) {
  1073. if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
  1074. break;
  1075. }
  1076. }
  1077. if ( !attrib ) {
  1078. attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1079. attrib->_memPool = &_document->_attributePool;
  1080. if ( last ) {
  1081. last->_next = attrib;
  1082. }
  1083. else {
  1084. _rootAttribute = attrib;
  1085. }
  1086. attrib->SetName( name );
  1087. attrib->_memPool->SetTracked(); // always created and linked.
  1088. }
  1089. return attrib;
  1090. }
  1091. void XMLElement::DeleteAttribute( const char* name )
  1092. {
  1093. XMLAttribute* prev = 0;
  1094. for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
  1095. if ( XMLUtil::StringEqual( name, a->Name() ) ) {
  1096. if ( prev ) {
  1097. prev->_next = a->_next;
  1098. }
  1099. else {
  1100. _rootAttribute = a->_next;
  1101. }
  1102. DELETE_ATTRIBUTE( a );
  1103. break;
  1104. }
  1105. prev = a;
  1106. }
  1107. }
  1108. char* XMLElement::ParseAttributes( char* p )
  1109. {
  1110. const char* start = p;
  1111. XMLAttribute* prevAttribute = 0;
  1112. // Read the attributes.
  1113. while( p ) {
  1114. p = XMLUtil::SkipWhiteSpace( p );
  1115. if ( !p || !(*p) ) {
  1116. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
  1117. return 0;
  1118. }
  1119. // attribute.
  1120. if (XMLUtil::IsNameStartChar( *p ) ) {
  1121. XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
  1122. attrib->_memPool = &_document->_attributePool;
  1123. attrib->_memPool->SetTracked();
  1124. p = attrib->ParseDeep( p, _document->ProcessEntities() );
  1125. if ( !p || Attribute( attrib->Name() ) ) {
  1126. DELETE_ATTRIBUTE( attrib );
  1127. _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
  1128. return 0;
  1129. }
  1130. // There is a minor bug here: if the attribute in the source xml
  1131. // document is duplicated, it will not be detected and the
  1132. // attribute will be doubly added. However, tracking the 'prevAttribute'
  1133. // avoids re-scanning the attribute list. Preferring performance for
  1134. // now, may reconsider in the future.
  1135. if ( prevAttribute ) {
  1136. prevAttribute->_next = attrib;
  1137. }
  1138. else {
  1139. _rootAttribute = attrib;
  1140. }
  1141. prevAttribute = attrib;
  1142. }
  1143. // end of the tag
  1144. else if ( *p == '/' && *(p+1) == '>' ) {
  1145. _closingType = CLOSED;
  1146. return p+2; // done; sealed element.
  1147. }
  1148. // end of the tag
  1149. else if ( *p == '>' ) {
  1150. ++p;
  1151. break;
  1152. }
  1153. else {
  1154. _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
  1155. return 0;
  1156. }
  1157. }
  1158. return p;
  1159. }
  1160. //
  1161. // <ele></ele>
  1162. // <ele>foo<b>bar</b></ele>
  1163. //
  1164. char* XMLElement::ParseDeep( char* p, StrPair* strPair )
  1165. {
  1166. // Read the element name.
  1167. p = XMLUtil::SkipWhiteSpace( p );
  1168. if ( !p ) {
  1169. return 0;
  1170. }
  1171. // The closing element is the </element> form. It is
  1172. // parsed just like a regular element then deleted from
  1173. // the DOM.
  1174. if ( *p == '/' ) {
  1175. _closingType = CLOSING;
  1176. ++p;
  1177. }
  1178. p = _value.ParseName( p );
  1179. if ( _value.Empty() ) {
  1180. return 0;
  1181. }
  1182. p = ParseAttributes( p );
  1183. if ( !p || !*p || _closingType ) {
  1184. return p;
  1185. }
  1186. p = XMLNode::ParseDeep( p, strPair );
  1187. return p;
  1188. }
  1189. XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
  1190. {
  1191. if ( !doc ) {
  1192. doc = _document;
  1193. }
  1194. XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
  1195. for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
  1196. element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
  1197. }
  1198. return element;
  1199. }
  1200. bool XMLElement::ShallowEqual( const XMLNode* compare ) const
  1201. {
  1202. const XMLElement* other = compare->ToElement();
  1203. if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
  1204. const XMLAttribute* a=FirstAttribute();
  1205. const XMLAttribute* b=other->FirstAttribute();
  1206. while ( a && b ) {
  1207. if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
  1208. return false;
  1209. }
  1210. a = a->Next();
  1211. b = b->Next();
  1212. }
  1213. if ( a || b ) {
  1214. // different count
  1215. return false;
  1216. }
  1217. return true;
  1218. }
  1219. return false;
  1220. }
  1221. bool XMLElement::Accept( XMLVisitor* visitor ) const
  1222. {
  1223. if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
  1224. for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
  1225. if ( !node->Accept( visitor ) ) {
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. return visitor->VisitExit( *this );
  1231. }
  1232. // --------- XMLDocument ----------- //
  1233. XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
  1234. XMLNode( 0 ),
  1235. _writeBOM( false ),
  1236. _processEntities( processEntities ),
  1237. _errorID( XML_NO_ERROR ),
  1238. _whitespace( whitespace ),
  1239. _errorStr1( 0 ),
  1240. _errorStr2( 0 ),
  1241. _charBuffer( 0 )
  1242. {
  1243. _document = this; // avoid warning about 'this' in initializer list
  1244. }
  1245. XMLDocument::~XMLDocument()
  1246. {
  1247. DeleteChildren();
  1248. delete [] _charBuffer;
  1249. #if 0
  1250. _textPool.Trace( "text" );
  1251. _elementPool.Trace( "element" );
  1252. _commentPool.Trace( "comment" );
  1253. _attributePool.Trace( "attribute" );
  1254. #endif
  1255. #ifdef DEBUG
  1256. if ( Error() == false ) {
  1257. TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
  1258. TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
  1259. TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
  1260. TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
  1261. }
  1262. #endif
  1263. }
  1264. void XMLDocument::Clear()
  1265. {
  1266. DeleteChildren();
  1267. _errorID = XML_NO_ERROR;
  1268. _errorStr1 = 0;
  1269. _errorStr2 = 0;
  1270. delete [] _charBuffer;
  1271. _charBuffer = 0;
  1272. }
  1273. XMLElement* XMLDocument::NewElement( const char* name )
  1274. {
  1275. XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
  1276. ele->_memPool = &_elementPool;
  1277. ele->SetName( name );
  1278. return ele;
  1279. }
  1280. XMLComment* XMLDocument::NewComment( const char* str )
  1281. {
  1282. XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
  1283. comment->_memPool = &_commentPool;
  1284. comment->SetValue( str );
  1285. return comment;
  1286. }
  1287. XMLText* XMLDocument::NewText( const char* str )
  1288. {
  1289. XMLText* text = new (_textPool.Alloc()) XMLText( this );
  1290. text->_memPool = &_textPool;
  1291. text->SetValue( str );
  1292. return text;
  1293. }
  1294. XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
  1295. {
  1296. XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
  1297. dec->_memPool = &_commentPool;
  1298. dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
  1299. return dec;
  1300. }
  1301. XMLUnknown* XMLDocument::NewUnknown( const char* str )
  1302. {
  1303. XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
  1304. unk->_memPool = &_commentPool;
  1305. unk->SetValue( str );
  1306. return unk;
  1307. }
  1308. XMLError XMLDocument::LoadFile( const char* filename )
  1309. {
  1310. Clear();
  1311. FILE* fp = 0;
  1312. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1313. errno_t err = fopen_s(&fp, filename, "rb" );
  1314. if ( !fp || err) {
  1315. #else
  1316. fp = fopen( filename, "rb" );
  1317. if ( !fp) {
  1318. #endif
  1319. SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
  1320. return _errorID;
  1321. }
  1322. LoadFile( fp );
  1323. fclose( fp );
  1324. return _errorID;
  1325. }
  1326. XMLError XMLDocument::LoadFile( FILE* fp )
  1327. {
  1328. Clear();
  1329. fseek( fp, 0, SEEK_END );
  1330. size_t size = ftell( fp );
  1331. fseek( fp, 0, SEEK_SET );
  1332. if ( size == 0 ) {
  1333. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1334. return _errorID;
  1335. }
  1336. _charBuffer = new char[size+1];
  1337. size_t read = fread( _charBuffer, 1, size, fp );
  1338. if ( read != size ) {
  1339. SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
  1340. return _errorID;
  1341. }
  1342. _charBuffer[size] = 0;
  1343. const char* p = _charBuffer;
  1344. p = XMLUtil::SkipWhiteSpace( p );
  1345. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1346. if ( !p || !*p ) {
  1347. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1348. return _errorID;
  1349. }
  1350. ParseDeep( _charBuffer + (p-_charBuffer), 0 );
  1351. return _errorID;
  1352. }
  1353. XMLError XMLDocument::SaveFile( const char* filename, bool compact )
  1354. {
  1355. FILE* fp = 0;
  1356. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  1357. errno_t err = fopen_s(&fp, filename, "w" );
  1358. if ( !fp || err) {
  1359. #else
  1360. fp = fopen( filename, "w" );
  1361. if ( !fp) {
  1362. #endif
  1363. SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
  1364. return _errorID;
  1365. }
  1366. SaveFile(fp, compact);
  1367. fclose( fp );
  1368. return _errorID;
  1369. }
  1370. XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
  1371. {
  1372. XMLPrinter stream( fp, compact );
  1373. Print( &stream );
  1374. return _errorID;
  1375. }
  1376. XMLError XMLDocument::Parse( const char* p, size_t len )
  1377. {
  1378. const char* start = p;
  1379. Clear();
  1380. if ( !p || !*p ) {
  1381. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1382. return _errorID;
  1383. }
  1384. if ( len == (size_t)(-1) ) {
  1385. len = strlen( p );
  1386. }
  1387. _charBuffer = new char[ len+1 ];
  1388. memcpy( _charBuffer, p, len );
  1389. _charBuffer[len] = 0;
  1390. p = XMLUtil::SkipWhiteSpace( p );
  1391. p = XMLUtil::ReadBOM( p, &_writeBOM );
  1392. if ( !p || !*p ) {
  1393. SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
  1394. return _errorID;
  1395. }
  1396. ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc.
  1397. ParseDeep( _charBuffer+delta, 0 );
  1398. return _errorID;
  1399. }
  1400. void XMLDocument::Print( XMLPrinter* streamer ) const
  1401. {
  1402. XMLPrinter stdStreamer( stdout );
  1403. if ( !streamer ) {
  1404. streamer = &stdStreamer;
  1405. }
  1406. Accept( streamer );
  1407. }
  1408. void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
  1409. {
  1410. _errorID = error;
  1411. _errorStr1 = str1;
  1412. _errorStr2 = str2;
  1413. }
  1414. void XMLDocument::PrintError() const
  1415. {
  1416. if ( _errorID ) {
  1417. static const int LEN = 20;
  1418. char buf1[LEN] = { 0 };
  1419. char buf2[LEN] = { 0 };
  1420. if ( _errorStr1 ) {
  1421. TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
  1422. }
  1423. if ( _errorStr2 ) {
  1424. TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
  1425. }
  1426. printf( "XMLDocument error id=%d str1=%s str2=%s\n",
  1427. _errorID, buf1, buf2 );
  1428. }
  1429. }
  1430. XMLPrinter::XMLPrinter( FILE* file, bool compact ) :
  1431. _elementJustOpened( false ),
  1432. _firstElement( true ),
  1433. _fp( file ),
  1434. _depth( 0 ),
  1435. _textDepth( -1 ),
  1436. _processEntities( true ),
  1437. _compactMode( compact )
  1438. {
  1439. for( int i=0; i<ENTITY_RANGE; ++i ) {
  1440. _entityFlag[i] = false;
  1441. _restrictedEntityFlag[i] = false;
  1442. }
  1443. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1444. TIXMLASSERT( entities[i].value < ENTITY_RANGE );
  1445. if ( entities[i].value < ENTITY_RANGE ) {
  1446. _entityFlag[ (int)entities[i].value ] = true;
  1447. }
  1448. }
  1449. _restrictedEntityFlag[(int)'&'] = true;
  1450. _restrictedEntityFlag[(int)'<'] = true;
  1451. _restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
  1452. _buffer.Push( 0 );
  1453. }
  1454. void XMLPrinter::Print( const char* format, ... )
  1455. {
  1456. va_list va;
  1457. va_start( va, format );
  1458. if ( _fp ) {
  1459. vfprintf( _fp, format, va );
  1460. }
  1461. else {
  1462. // This seems brutally complex. Haven't figured out a better
  1463. // way on windows.
  1464. #ifdef _MSC_VER
  1465. int len = -1;
  1466. int expand = 1000;
  1467. while ( len < 0 ) {
  1468. len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va );
  1469. if ( len < 0 ) {
  1470. expand *= 3/2;
  1471. _accumulator.PushArr( expand );
  1472. }
  1473. }
  1474. char* p = _buffer.PushArr( len ) - 1;
  1475. memcpy( p, _accumulator.Mem(), len+1 );
  1476. #else
  1477. int len = vsnprintf( 0, 0, format, va );
  1478. // Close out and re-start the va-args
  1479. va_end( va );
  1480. va_start( va, format );
  1481. char* p = _buffer.PushArr( len ) - 1;
  1482. vsnprintf( p, len+1, format, va );
  1483. #endif
  1484. }
  1485. va_end( va );
  1486. }
  1487. void XMLPrinter::PrintSpace( int depth )
  1488. {
  1489. for( int i=0; i<depth; ++i ) {
  1490. Print( " " );
  1491. }
  1492. }
  1493. void XMLPrinter::PrintString( const char* p, bool restricted )
  1494. {
  1495. // Look for runs of bytes between entities to print.
  1496. const char* q = p;
  1497. const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
  1498. if ( _processEntities ) {
  1499. while ( *q ) {
  1500. // Remember, char is sometimes signed. (How many times has that bitten me?)
  1501. if ( *q > 0 && *q < ENTITY_RANGE ) {
  1502. // Check for entities. If one is found, flush
  1503. // the stream up until the entity, write the
  1504. // entity, and keep looking.
  1505. if ( flag[(unsigned)(*q)] ) {
  1506. while ( p < q ) {
  1507. Print( "%c", *p );
  1508. ++p;
  1509. }
  1510. for( int i=0; i<NUM_ENTITIES; ++i ) {
  1511. if ( entities[i].value == *q ) {
  1512. Print( "&%s;", entities[i].pattern );
  1513. break;
  1514. }
  1515. }
  1516. ++p;
  1517. }
  1518. }
  1519. ++q;
  1520. }
  1521. }
  1522. // Flush the remaining string. This will be the entire
  1523. // string if an entity wasn't found.
  1524. if ( !_processEntities || (q-p > 0) ) {
  1525. Print( "%s", p );
  1526. }
  1527. }
  1528. void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
  1529. {
  1530. if ( writeBOM ) {
  1531. static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
  1532. Print( "%s", bom );
  1533. }
  1534. if ( writeDec ) {
  1535. PushDeclaration( "xml version=\"1.0\"" );
  1536. }
  1537. }
  1538. void XMLPrinter::OpenElement( const char* name )
  1539. {
  1540. if ( _elementJustOpened ) {
  1541. SealElement();
  1542. }
  1543. _stack.Push( name );
  1544. if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
  1545. Print( "\n" );
  1546. PrintSpace( _depth );
  1547. }
  1548. Print( "<%s", name );
  1549. _elementJustOpened = true;
  1550. _firstElement = false;
  1551. ++_depth;
  1552. }
  1553. void XMLPrinter::PushAttribute( const char* name, const char* value )
  1554. {
  1555. TIXMLASSERT( _elementJustOpened );
  1556. Print( " %s=\"", name );
  1557. PrintString( value, false );
  1558. Print( "\"" );
  1559. }
  1560. void XMLPrinter::PushAttribute( const char* name, int v )
  1561. {
  1562. char buf[BUF_SIZE];
  1563. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1564. PushAttribute( name, buf );
  1565. }
  1566. void XMLPrinter::PushAttribute( const char* name, unsigned v )
  1567. {
  1568. char buf[BUF_SIZE];
  1569. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1570. PushAttribute( name, buf );
  1571. }
  1572. void XMLPrinter::PushAttribute( const char* name, bool v )
  1573. {
  1574. char buf[BUF_SIZE];
  1575. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1576. PushAttribute( name, buf );
  1577. }
  1578. void XMLPrinter::PushAttribute( const char* name, double v )
  1579. {
  1580. char buf[BUF_SIZE];
  1581. XMLUtil::ToStr( v, buf, BUF_SIZE );
  1582. PushAttribute( name, buf );
  1583. }
  1584. void XMLPrinter::CloseElement()
  1585. {
  1586. --_depth;
  1587. const char* name = _stack.Pop();
  1588. if ( _elementJustOpened ) {
  1589. Print( "/>" );
  1590. }
  1591. else {
  1592. if ( _textDepth < 0 && !_compactMode) {
  1593. Print( "\n" );
  1594. PrintSpace( _depth );
  1595. }
  1596. Print( "</%s>", name );
  1597. }
  1598. if ( _textDepth == _depth ) {
  1599. _textDepth = -1;
  1600. }
  1601. if ( _depth == 0 && !_compactMode) {
  1602. Print( "\n" );
  1603. }
  1604. _elementJustOpened = false;
  1605. }
  1606. void XMLPrinter::SealElement()
  1607. {
  1608. _elementJustOpened = false;
  1609. Print( ">" );
  1610. }
  1611. void XMLPrinter::PushText( const char* text, bool cdata )
  1612. {
  1613. _textDepth = _depth-1;
  1614. if ( _elementJustOpened ) {
  1615. SealElement();
  1616. }
  1617. if ( cdata ) {
  1618. Print( "<![CDATA[" );
  1619. Print( "%s", text );
  1620. Print( "]]>" );
  1621. }
  1622. else {
  1623. PrintString( text, true );
  1624. }
  1625. }
  1626. void XMLPrinter::PushText( int value )
  1627. {
  1628. char buf[BUF_SIZE];
  1629. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1630. PushText( buf, false );
  1631. }
  1632. void XMLPrinter::PushText( unsigned value )
  1633. {
  1634. char buf[BUF_SIZE];
  1635. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1636. PushText( buf, false );
  1637. }
  1638. void XMLPrinter::PushText( bool value )
  1639. {
  1640. char buf[BUF_SIZE];
  1641. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1642. PushText( buf, false );
  1643. }
  1644. void XMLPrinter::PushText( float value )
  1645. {
  1646. char buf[BUF_SIZE];
  1647. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1648. PushText( buf, false );
  1649. }
  1650. void XMLPrinter::PushText( double value )
  1651. {
  1652. char buf[BUF_SIZE];
  1653. XMLUtil::ToStr( value, buf, BUF_SIZE );
  1654. PushText( buf, false );
  1655. }
  1656. void XMLPrinter::PushComment( const char* comment )
  1657. {
  1658. if ( _elementJustOpened ) {
  1659. SealElement();
  1660. }
  1661. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1662. Print( "\n" );
  1663. PrintSpace( _depth );
  1664. }
  1665. _firstElement = false;
  1666. Print( "<!--%s-->", comment );
  1667. }
  1668. void XMLPrinter::PushDeclaration( const char* value )
  1669. {
  1670. if ( _elementJustOpened ) {
  1671. SealElement();
  1672. }
  1673. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1674. Print( "\n" );
  1675. PrintSpace( _depth );
  1676. }
  1677. _firstElement = false;
  1678. Print( "<?%s?>", value );
  1679. }
  1680. void XMLPrinter::PushUnknown( const char* value )
  1681. {
  1682. if ( _elementJustOpened ) {
  1683. SealElement();
  1684. }
  1685. if ( _textDepth < 0 && !_firstElement && !_compactMode) {
  1686. Print( "\n" );
  1687. PrintSpace( _depth );
  1688. }
  1689. _firstElement = false;
  1690. Print( "<!%s>", value );
  1691. }
  1692. bool XMLPrinter::VisitEnter( const XMLDocument& doc )
  1693. {
  1694. _processEntities = doc.ProcessEntities();
  1695. if ( doc.HasBOM() ) {
  1696. PushHeader( true, false );
  1697. }
  1698. return true;
  1699. }
  1700. bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
  1701. {
  1702. OpenElement( element.Name() );
  1703. while ( attribute ) {
  1704. PushAttribute( attribute->Name(), attribute->Value() );
  1705. attribute = attribute->Next();
  1706. }
  1707. return true;
  1708. }
  1709. bool XMLPrinter::VisitExit( const XMLElement& )
  1710. {
  1711. CloseElement();
  1712. return true;
  1713. }
  1714. bool XMLPrinter::Visit( const XMLText& text )
  1715. {
  1716. PushText( text.Value(), text.CData() );
  1717. return true;
  1718. }
  1719. bool XMLPrinter::Visit( const XMLComment& comment )
  1720. {
  1721. PushComment( comment.Value() );
  1722. return true;
  1723. }
  1724. bool XMLPrinter::Visit( const XMLDeclaration& declaration )
  1725. {
  1726. PushDeclaration( declaration.Value() );
  1727. return true;
  1728. }
  1729. bool XMLPrinter::Visit( const XMLUnknown& unknown )
  1730. {
  1731. PushUnknown( unknown.Value() );
  1732. return true;
  1733. }
  1734. } // namespace tinyxml2