tinyxml2.cpp 38 KB

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