tinyxml2.cpp 44 KB

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