tinyxml.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. Original code by Lee Thomason (www.grinninglizard.com)
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must
  11. not claim that you wrote the original software. If you use this
  12. software in a product, an acknowledgment in the product documentation
  13. would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. #include <ctype.h>
  20. #ifdef TIXML_USE_STL
  21. #include <sstream>
  22. #include <iostream>
  23. #endif
  24. #include "tinyxml.h"
  25. FILE* TiXmlFOpen( const char* filename, const char* mode );
  26. bool TiXmlBase::condenseWhiteSpace = true;
  27. // Microsoft compiler security
  28. FILE* TiXmlFOpen( const char* filename, const char* mode )
  29. {
  30. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  31. FILE* fp = 0;
  32. errno_t err = fopen_s( &fp, filename, mode );
  33. if ( !err && fp )
  34. return fp;
  35. return 0;
  36. #else
  37. return fopen( filename, mode );
  38. #endif
  39. }
  40. void TiXmlBase::EncodeString( const TIXML_STRING& str, TIXML_STRING* outString )
  41. {
  42. int i=0;
  43. while( i<(int)str.length() )
  44. {
  45. unsigned char c = (unsigned char) str[i];
  46. if ( c == '&'
  47. && i < ( (int)str.length() - 2 )
  48. && str[i+1] == '#'
  49. && str[i+2] == 'x' )
  50. {
  51. // Hexadecimal character reference.
  52. // Pass through unchanged.
  53. // &#xA9; -- copyright symbol, for example.
  54. //
  55. // The -1 is a bug fix from Rob Laveaux. It keeps
  56. // an overflow from happening if there is no ';'.
  57. // There are actually 2 ways to exit this loop -
  58. // while fails (error case) and break (semicolon found).
  59. // However, there is no mechanism (currently) for
  60. // this function to return an error.
  61. while ( i<(int)str.length()-1 )
  62. {
  63. outString->append( str.c_str() + i, 1 );
  64. ++i;
  65. if ( str[i] == ';' )
  66. break;
  67. }
  68. }
  69. else if ( c == '&' )
  70. {
  71. outString->append( entity[0].str, entity[0].strLength );
  72. ++i;
  73. }
  74. else if ( c == '<' )
  75. {
  76. outString->append( entity[1].str, entity[1].strLength );
  77. ++i;
  78. }
  79. else if ( c == '>' )
  80. {
  81. outString->append( entity[2].str, entity[2].strLength );
  82. ++i;
  83. }
  84. else if ( c == '\"' )
  85. {
  86. outString->append( entity[3].str, entity[3].strLength );
  87. ++i;
  88. }
  89. else if ( c == '\'' )
  90. {
  91. outString->append( entity[4].str, entity[4].strLength );
  92. ++i;
  93. }
  94. else if ( c < 32 )
  95. {
  96. // Easy pass at non-alpha/numeric/symbol
  97. // Below 32 is symbolic.
  98. char buf[ 32 ];
  99. #if defined(TIXML_SNPRINTF)
  100. TIXML_SNPRINTF( buf, sizeof(buf), "&#x%02X;", (unsigned) ( c & 0xff ) );
  101. #else
  102. sprintf( buf, "&#x%02X;", (unsigned) ( c & 0xff ) );
  103. #endif
  104. //*ME: warning C4267: convert 'size_t' to 'int'
  105. //*ME: Int-Cast to make compiler happy ...
  106. outString->append( buf, (int)strlen( buf ) );
  107. ++i;
  108. }
  109. else
  110. {
  111. //char realc = (char) c;
  112. //outString->append( &realc, 1 );
  113. *outString += (char) c; // somewhat more efficient function call.
  114. ++i;
  115. }
  116. }
  117. }
  118. TiXmlNode::TiXmlNode( NodeType _type ) : TiXmlBase()
  119. {
  120. parent = 0;
  121. type = _type;
  122. firstChild = 0;
  123. lastChild = 0;
  124. prev = 0;
  125. next = 0;
  126. }
  127. TiXmlNode::~TiXmlNode()
  128. {
  129. TiXmlNode* node = firstChild;
  130. TiXmlNode* temp = 0;
  131. while ( node )
  132. {
  133. temp = node;
  134. node = node->next;
  135. delete temp;
  136. }
  137. }
  138. void TiXmlNode::CopyTo( TiXmlNode* target ) const
  139. {
  140. target->SetValue (value.c_str() );
  141. target->userData = userData;
  142. target->location = location;
  143. }
  144. void TiXmlNode::Clear()
  145. {
  146. TiXmlNode* node = firstChild;
  147. TiXmlNode* temp = 0;
  148. while ( node )
  149. {
  150. temp = node;
  151. node = node->next;
  152. delete temp;
  153. }
  154. firstChild = 0;
  155. lastChild = 0;
  156. }
  157. TiXmlNode* TiXmlNode::LinkEndChild( TiXmlNode* node )
  158. {
  159. assert( node->parent == 0 || node->parent == this );
  160. assert( node->GetDocument() == 0 || node->GetDocument() == this->GetDocument() );
  161. if ( node->Type() == TiXmlNode::TINYXML_DOCUMENT )
  162. {
  163. delete node;
  164. if ( GetDocument() )
  165. GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  166. return 0;
  167. }
  168. node->parent = this;
  169. node->prev = lastChild;
  170. node->next = 0;
  171. if ( lastChild )
  172. lastChild->next = node;
  173. else
  174. firstChild = node; // it was an empty list.
  175. lastChild = node;
  176. return node;
  177. }
  178. TiXmlNode* TiXmlNode::InsertEndChild( const TiXmlNode& addThis )
  179. {
  180. if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
  181. {
  182. if ( GetDocument() )
  183. GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  184. return 0;
  185. }
  186. TiXmlNode* node = addThis.Clone();
  187. if ( !node )
  188. return 0;
  189. return LinkEndChild( node );
  190. }
  191. TiXmlNode* TiXmlNode::InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis )
  192. {
  193. if ( !beforeThis || beforeThis->parent != this ) {
  194. return 0;
  195. }
  196. if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
  197. {
  198. if ( GetDocument() )
  199. GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  200. return 0;
  201. }
  202. TiXmlNode* node = addThis.Clone();
  203. if ( !node )
  204. return 0;
  205. node->parent = this;
  206. node->next = beforeThis;
  207. node->prev = beforeThis->prev;
  208. if ( beforeThis->prev )
  209. {
  210. beforeThis->prev->next = node;
  211. }
  212. else
  213. {
  214. assert( firstChild == beforeThis );
  215. firstChild = node;
  216. }
  217. beforeThis->prev = node;
  218. return node;
  219. }
  220. TiXmlNode* TiXmlNode::InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis )
  221. {
  222. if ( !afterThis || afterThis->parent != this ) {
  223. return 0;
  224. }
  225. if ( addThis.Type() == TiXmlNode::TINYXML_DOCUMENT )
  226. {
  227. if ( GetDocument() )
  228. GetDocument()->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  229. return 0;
  230. }
  231. TiXmlNode* node = addThis.Clone();
  232. if ( !node )
  233. return 0;
  234. node->parent = this;
  235. node->prev = afterThis;
  236. node->next = afterThis->next;
  237. if ( afterThis->next )
  238. {
  239. afterThis->next->prev = node;
  240. }
  241. else
  242. {
  243. assert( lastChild == afterThis );
  244. lastChild = node;
  245. }
  246. afterThis->next = node;
  247. return node;
  248. }
  249. TiXmlNode* TiXmlNode::ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis )
  250. {
  251. if ( !replaceThis )
  252. return 0;
  253. if ( replaceThis->parent != this )
  254. return 0;
  255. if ( withThis.ToDocument() ) {
  256. // A document can never be a child. Thanks to Noam.
  257. TiXmlDocument* document = GetDocument();
  258. if ( document )
  259. document->SetError( TIXML_ERROR_DOCUMENT_TOP_ONLY, 0, 0, TIXML_ENCODING_UNKNOWN );
  260. return 0;
  261. }
  262. TiXmlNode* node = withThis.Clone();
  263. if ( !node )
  264. return 0;
  265. node->next = replaceThis->next;
  266. node->prev = replaceThis->prev;
  267. if ( replaceThis->next )
  268. replaceThis->next->prev = node;
  269. else
  270. lastChild = node;
  271. if ( replaceThis->prev )
  272. replaceThis->prev->next = node;
  273. else
  274. firstChild = node;
  275. delete replaceThis;
  276. node->parent = this;
  277. return node;
  278. }
  279. bool TiXmlNode::RemoveChild( TiXmlNode* removeThis )
  280. {
  281. if ( !removeThis ) {
  282. return false;
  283. }
  284. if ( removeThis->parent != this )
  285. {
  286. assert( 0 );
  287. return false;
  288. }
  289. if ( removeThis->next )
  290. removeThis->next->prev = removeThis->prev;
  291. else
  292. lastChild = removeThis->prev;
  293. if ( removeThis->prev )
  294. removeThis->prev->next = removeThis->next;
  295. else
  296. firstChild = removeThis->next;
  297. delete removeThis;
  298. return true;
  299. }
  300. const TiXmlNode* TiXmlNode::FirstChild( const char * _value ) const
  301. {
  302. const TiXmlNode* node;
  303. for ( node = firstChild; node; node = node->next )
  304. {
  305. if ( strcmp( node->Value(), _value ) == 0 )
  306. return node;
  307. }
  308. return 0;
  309. }
  310. const TiXmlNode* TiXmlNode::LastChild( const char * _value ) const
  311. {
  312. const TiXmlNode* node;
  313. for ( node = lastChild; node; node = node->prev )
  314. {
  315. if ( strcmp( node->Value(), _value ) == 0 )
  316. return node;
  317. }
  318. return 0;
  319. }
  320. const TiXmlNode* TiXmlNode::IterateChildren( const TiXmlNode* previous ) const
  321. {
  322. if ( !previous )
  323. {
  324. return FirstChild();
  325. }
  326. else
  327. {
  328. assert( previous->parent == this );
  329. return previous->NextSibling();
  330. }
  331. }
  332. const TiXmlNode* TiXmlNode::IterateChildren( const char * val, const TiXmlNode* previous ) const
  333. {
  334. if ( !previous )
  335. {
  336. return FirstChild( val );
  337. }
  338. else
  339. {
  340. assert( previous->parent == this );
  341. return previous->NextSibling( val );
  342. }
  343. }
  344. const TiXmlNode* TiXmlNode::NextSibling( const char * _value ) const
  345. {
  346. const TiXmlNode* node;
  347. for ( node = next; node; node = node->next )
  348. {
  349. if ( strcmp( node->Value(), _value ) == 0 )
  350. return node;
  351. }
  352. return 0;
  353. }
  354. const TiXmlNode* TiXmlNode::PreviousSibling( const char * _value ) const
  355. {
  356. const TiXmlNode* node;
  357. for ( node = prev; node; node = node->prev )
  358. {
  359. if ( strcmp( node->Value(), _value ) == 0 )
  360. return node;
  361. }
  362. return 0;
  363. }
  364. void TiXmlElement::RemoveAttribute( const char * name )
  365. {
  366. #ifdef TIXML_USE_STL
  367. TIXML_STRING str( name );
  368. TiXmlAttribute* node = attributeSet.Find( str );
  369. #else
  370. TiXmlAttribute* node = attributeSet.Find( name );
  371. #endif
  372. if ( node )
  373. {
  374. attributeSet.Remove( node );
  375. delete node;
  376. }
  377. }
  378. const TiXmlElement* TiXmlNode::FirstChildElement() const
  379. {
  380. const TiXmlNode* node;
  381. for ( node = FirstChild();
  382. node;
  383. node = node->NextSibling() )
  384. {
  385. if ( node->ToElement() )
  386. return node->ToElement();
  387. }
  388. return 0;
  389. }
  390. const TiXmlElement* TiXmlNode::FirstChildElement( const char * _value ) const
  391. {
  392. const TiXmlNode* node;
  393. for ( node = FirstChild( _value );
  394. node;
  395. node = node->NextSibling( _value ) )
  396. {
  397. if ( node->ToElement() )
  398. return node->ToElement();
  399. }
  400. return 0;
  401. }
  402. const TiXmlElement* TiXmlNode::NextSiblingElement() const
  403. {
  404. const TiXmlNode* node;
  405. for ( node = NextSibling();
  406. node;
  407. node = node->NextSibling() )
  408. {
  409. if ( node->ToElement() )
  410. return node->ToElement();
  411. }
  412. return 0;
  413. }
  414. const TiXmlElement* TiXmlNode::NextSiblingElement( const char * _value ) const
  415. {
  416. const TiXmlNode* node;
  417. for ( node = NextSibling( _value );
  418. node;
  419. node = node->NextSibling( _value ) )
  420. {
  421. if ( node->ToElement() )
  422. return node->ToElement();
  423. }
  424. return 0;
  425. }
  426. const TiXmlDocument* TiXmlNode::GetDocument() const
  427. {
  428. const TiXmlNode* node;
  429. for( node = this; node; node = node->parent )
  430. {
  431. if ( node->ToDocument() )
  432. return node->ToDocument();
  433. }
  434. return 0;
  435. }
  436. TiXmlElement::TiXmlElement (const char * _value)
  437. : TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
  438. {
  439. firstChild = lastChild = 0;
  440. value = _value;
  441. }
  442. #ifdef TIXML_USE_STL
  443. TiXmlElement::TiXmlElement( const std::string& _value )
  444. : TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
  445. {
  446. firstChild = lastChild = 0;
  447. value = _value;
  448. }
  449. #endif
  450. TiXmlElement::TiXmlElement( const TiXmlElement& copy)
  451. : TiXmlNode( TiXmlNode::TINYXML_ELEMENT )
  452. {
  453. firstChild = lastChild = 0;
  454. copy.CopyTo( this );
  455. }
  456. TiXmlElement& TiXmlElement::operator=( const TiXmlElement& base )
  457. {
  458. ClearThis();
  459. base.CopyTo( this );
  460. return *this;
  461. }
  462. TiXmlElement::~TiXmlElement()
  463. {
  464. ClearThis();
  465. }
  466. void TiXmlElement::ClearThis()
  467. {
  468. Clear();
  469. while( attributeSet.First() )
  470. {
  471. TiXmlAttribute* node = attributeSet.First();
  472. attributeSet.Remove( node );
  473. delete node;
  474. }
  475. }
  476. const char* TiXmlElement::Attribute( const char* name ) const
  477. {
  478. const TiXmlAttribute* node = attributeSet.Find( name );
  479. if ( node )
  480. return node->Value();
  481. return 0;
  482. }
  483. #ifdef TIXML_USE_STL
  484. const std::string* TiXmlElement::Attribute( const std::string& name ) const
  485. {
  486. const TiXmlAttribute* attrib = attributeSet.Find( name );
  487. if ( attrib )
  488. return &attrib->ValueStr();
  489. return 0;
  490. }
  491. #endif
  492. const char* TiXmlElement::Attribute( const char* name, int* i ) const
  493. {
  494. const TiXmlAttribute* attrib = attributeSet.Find( name );
  495. const char* result = 0;
  496. if ( attrib ) {
  497. result = attrib->Value();
  498. if ( i ) {
  499. attrib->QueryIntValue( i );
  500. }
  501. }
  502. return result;
  503. }
  504. #ifdef TIXML_USE_STL
  505. const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) const
  506. {
  507. const TiXmlAttribute* attrib = attributeSet.Find( name );
  508. const std::string* result = 0;
  509. if ( attrib ) {
  510. result = &attrib->ValueStr();
  511. if ( i ) {
  512. attrib->QueryIntValue( i );
  513. }
  514. }
  515. return result;
  516. }
  517. #endif
  518. const char* TiXmlElement::Attribute( const char* name, double* d ) const
  519. {
  520. const TiXmlAttribute* attrib = attributeSet.Find( name );
  521. const char* result = 0;
  522. if ( attrib ) {
  523. result = attrib->Value();
  524. if ( d ) {
  525. attrib->QueryDoubleValue( d );
  526. }
  527. }
  528. return result;
  529. }
  530. #ifdef TIXML_USE_STL
  531. const std::string* TiXmlElement::Attribute( const std::string& name, double* d ) const
  532. {
  533. const TiXmlAttribute* attrib = attributeSet.Find( name );
  534. const std::string* result = 0;
  535. if ( attrib ) {
  536. result = &attrib->ValueStr();
  537. if ( d ) {
  538. attrib->QueryDoubleValue( d );
  539. }
  540. }
  541. return result;
  542. }
  543. #endif
  544. int TiXmlElement::QueryIntAttribute( const char* name, int* ival ) const
  545. {
  546. const TiXmlAttribute* attrib = attributeSet.Find( name );
  547. if ( !attrib )
  548. return TIXML_NO_ATTRIBUTE;
  549. return attrib->QueryIntValue( ival );
  550. }
  551. int TiXmlElement::QueryUnsignedAttribute( const char* name, unsigned* value ) const
  552. {
  553. const TiXmlAttribute* node = attributeSet.Find( name );
  554. if ( !node )
  555. return TIXML_NO_ATTRIBUTE;
  556. int ival = 0;
  557. int result = node->QueryIntValue( &ival );
  558. *value = (unsigned)ival;
  559. return result;
  560. }
  561. int TiXmlElement::QueryBoolAttribute( const char* name, bool* bval ) const
  562. {
  563. const TiXmlAttribute* node = attributeSet.Find( name );
  564. if ( !node )
  565. return TIXML_NO_ATTRIBUTE;
  566. int result = TIXML_WRONG_TYPE;
  567. if ( StringEqual( node->Value(), "true", true, TIXML_ENCODING_UNKNOWN )
  568. || StringEqual( node->Value(), "yes", true, TIXML_ENCODING_UNKNOWN )
  569. || StringEqual( node->Value(), "1", true, TIXML_ENCODING_UNKNOWN ) )
  570. {
  571. *bval = true;
  572. result = TIXML_SUCCESS;
  573. }
  574. else if ( StringEqual( node->Value(), "false", true, TIXML_ENCODING_UNKNOWN )
  575. || StringEqual( node->Value(), "no", true, TIXML_ENCODING_UNKNOWN )
  576. || StringEqual( node->Value(), "0", true, TIXML_ENCODING_UNKNOWN ) )
  577. {
  578. *bval = false;
  579. result = TIXML_SUCCESS;
  580. }
  581. return result;
  582. }
  583. #ifdef TIXML_USE_STL
  584. int TiXmlElement::QueryIntAttribute( const std::string& name, int* ival ) const
  585. {
  586. const TiXmlAttribute* attrib = attributeSet.Find( name );
  587. if ( !attrib )
  588. return TIXML_NO_ATTRIBUTE;
  589. return attrib->QueryIntValue( ival );
  590. }
  591. #endif
  592. int TiXmlElement::QueryDoubleAttribute( const char* name, double* dval ) const
  593. {
  594. const TiXmlAttribute* attrib = attributeSet.Find( name );
  595. if ( !attrib )
  596. return TIXML_NO_ATTRIBUTE;
  597. return attrib->QueryDoubleValue( dval );
  598. }
  599. #ifdef TIXML_USE_STL
  600. int TiXmlElement::QueryDoubleAttribute( const std::string& name, double* dval ) const
  601. {
  602. const TiXmlAttribute* attrib = attributeSet.Find( name );
  603. if ( !attrib )
  604. return TIXML_NO_ATTRIBUTE;
  605. return attrib->QueryDoubleValue( dval );
  606. }
  607. #endif
  608. void TiXmlElement::SetAttribute( const char * name, int val )
  609. {
  610. TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  611. if ( attrib ) {
  612. attrib->SetIntValue( val );
  613. }
  614. }
  615. #ifdef TIXML_USE_STL
  616. void TiXmlElement::SetAttribute( const std::string& name, int val )
  617. {
  618. TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  619. if ( attrib ) {
  620. attrib->SetIntValue( val );
  621. }
  622. }
  623. #endif
  624. void TiXmlElement::SetDoubleAttribute( const char * name, double val )
  625. {
  626. TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  627. if ( attrib ) {
  628. attrib->SetDoubleValue( val );
  629. }
  630. }
  631. #ifdef TIXML_USE_STL
  632. void TiXmlElement::SetDoubleAttribute( const std::string& name, double val )
  633. {
  634. TiXmlAttribute* attrib = attributeSet.FindOrCreate( name );
  635. if ( attrib ) {
  636. attrib->SetDoubleValue( val );
  637. }
  638. }
  639. #endif
  640. void TiXmlElement::SetAttribute( const char * cname, const char * cvalue )
  641. {
  642. TiXmlAttribute* attrib = attributeSet.FindOrCreate( cname );
  643. if ( attrib ) {
  644. attrib->SetValue( cvalue );
  645. }
  646. }
  647. #ifdef TIXML_USE_STL
  648. void TiXmlElement::SetAttribute( const std::string& _name, const std::string& _value )
  649. {
  650. TiXmlAttribute* attrib = attributeSet.FindOrCreate( _name );
  651. if ( attrib ) {
  652. attrib->SetValue( _value );
  653. }
  654. }
  655. #endif
  656. void TiXmlElement::Print( FILE* cfile, int depth ) const
  657. {
  658. int i;
  659. assert( cfile );
  660. for ( i=0; i<depth; i++ ) {
  661. fprintf( cfile, " " );
  662. }
  663. fprintf( cfile, "<%s", value.c_str() );
  664. const TiXmlAttribute* attrib;
  665. for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() )
  666. {
  667. fprintf( cfile, " " );
  668. attrib->Print( cfile, depth );
  669. }
  670. // There are 3 different formatting approaches:
  671. // 1) An element without children is printed as a <foo /> node
  672. // 2) An element with only a text child is printed as <foo> text </foo>
  673. // 3) An element with children is printed on multiple lines.
  674. TiXmlNode* node;
  675. if ( !firstChild )
  676. {
  677. fprintf( cfile, " />" );
  678. }
  679. else if ( firstChild == lastChild && firstChild->ToText() )
  680. {
  681. fprintf( cfile, ">" );
  682. firstChild->Print( cfile, depth + 1 );
  683. fprintf( cfile, "</%s>", value.c_str() );
  684. }
  685. else
  686. {
  687. fprintf( cfile, ">" );
  688. for ( node = firstChild; node; node=node->NextSibling() )
  689. {
  690. if ( !node->ToText() )
  691. {
  692. fprintf( cfile, "\n" );
  693. }
  694. node->Print( cfile, depth+1 );
  695. }
  696. fprintf( cfile, "\n" );
  697. for( i=0; i<depth; ++i ) {
  698. fprintf( cfile, " " );
  699. }
  700. fprintf( cfile, "</%s>", value.c_str() );
  701. }
  702. }
  703. void TiXmlElement::CopyTo( TiXmlElement* target ) const
  704. {
  705. // superclass:
  706. TiXmlNode::CopyTo( target );
  707. // Element class:
  708. // Clone the attributes, then clone the children.
  709. const TiXmlAttribute* attribute = 0;
  710. for( attribute = attributeSet.First();
  711. attribute;
  712. attribute = attribute->Next() )
  713. {
  714. target->SetAttribute( attribute->Name(), attribute->Value() );
  715. }
  716. TiXmlNode* node = 0;
  717. for ( node = firstChild; node; node = node->NextSibling() )
  718. {
  719. target->LinkEndChild( node->Clone() );
  720. }
  721. }
  722. bool TiXmlElement::Accept( TiXmlVisitor* visitor ) const
  723. {
  724. if ( visitor->VisitEnter( *this, attributeSet.First() ) )
  725. {
  726. for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  727. {
  728. if ( !node->Accept( visitor ) )
  729. break;
  730. }
  731. }
  732. return visitor->VisitExit( *this );
  733. }
  734. TiXmlNode* TiXmlElement::Clone() const
  735. {
  736. TiXmlElement* clone = new TiXmlElement( Value() );
  737. if ( !clone )
  738. return 0;
  739. CopyTo( clone );
  740. return clone;
  741. }
  742. const char* TiXmlElement::GetText() const
  743. {
  744. const TiXmlNode* child = this->FirstChild();
  745. if ( child ) {
  746. const TiXmlText* childText = child->ToText();
  747. if ( childText ) {
  748. return childText->Value();
  749. }
  750. }
  751. return 0;
  752. }
  753. TiXmlDocument::TiXmlDocument() : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  754. {
  755. tabsize = 4;
  756. useMicrosoftBOM = false;
  757. ClearError();
  758. }
  759. TiXmlDocument::TiXmlDocument( const char * documentName ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  760. {
  761. tabsize = 4;
  762. useMicrosoftBOM = false;
  763. value = documentName;
  764. ClearError();
  765. }
  766. #ifdef TIXML_USE_STL
  767. TiXmlDocument::TiXmlDocument( const std::string& documentName ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  768. {
  769. tabsize = 4;
  770. useMicrosoftBOM = false;
  771. value = documentName;
  772. ClearError();
  773. }
  774. #endif
  775. TiXmlDocument::TiXmlDocument( const TiXmlDocument& copy ) : TiXmlNode( TiXmlNode::TINYXML_DOCUMENT )
  776. {
  777. copy.CopyTo( this );
  778. }
  779. TiXmlDocument& TiXmlDocument::operator=( const TiXmlDocument& copy )
  780. {
  781. Clear();
  782. copy.CopyTo( this );
  783. return *this;
  784. }
  785. bool TiXmlDocument::LoadFile( TiXmlEncoding encoding )
  786. {
  787. return LoadFile( Value(), encoding );
  788. }
  789. bool TiXmlDocument::SaveFile() const
  790. {
  791. return SaveFile( Value() );
  792. }
  793. bool TiXmlDocument::LoadFile( const char* _filename, TiXmlEncoding encoding )
  794. {
  795. TIXML_STRING filename( _filename );
  796. value = filename;
  797. // reading in binary mode so that tinyxml can normalize the EOL
  798. FILE* file = TiXmlFOpen( value.c_str (), "rb" );
  799. if ( file )
  800. {
  801. bool result = LoadFile( file, encoding );
  802. fclose( file );
  803. return result;
  804. }
  805. else
  806. {
  807. SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
  808. return false;
  809. }
  810. }
  811. bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding )
  812. {
  813. if ( !file )
  814. {
  815. SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
  816. return false;
  817. }
  818. // Delete the existing data:
  819. Clear();
  820. location.Clear();
  821. // Get the file size, so we can pre-allocate the string. HUGE speed impact.
  822. long length = 0;
  823. fseek( file, 0, SEEK_END );
  824. length = ftell( file );
  825. fseek( file, 0, SEEK_SET );
  826. // Strange case, but good to handle up front.
  827. if ( length <= 0 )
  828. {
  829. SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
  830. return false;
  831. }
  832. // Subtle bug here. TinyXml did use fgets. But from the XML spec:
  833. // 2.11 End-of-Line Handling
  834. // <snip>
  835. // <quote>
  836. // ...the XML processor MUST behave as if it normalized all line breaks in external
  837. // parsed entities (including the document entity) on input, before parsing, by translating
  838. // both the two-character sequence #xD #xA and any #xD that is not followed by #xA to
  839. // a single #xA character.
  840. // </quote>
  841. //
  842. // It is not clear fgets does that, and certainly isn't clear it works cross platform.
  843. // Generally, you expect fgets to translate from the convention of the OS to the c/unix
  844. // convention, and not work generally.
  845. /*
  846. while( fgets( buf, sizeof(buf), file ) )
  847. {
  848. data += buf;
  849. }
  850. */
  851. char* buf = new char[ length+1 ];
  852. buf[0] = 0;
  853. if ( fread( buf, length, 1, file ) != 1 ) {
  854. delete [] buf;
  855. SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
  856. return false;
  857. }
  858. // Process the buffer in place to normalize new lines. (See comment above.)
  859. // Copies from the 'p' to 'q' pointer, where p can advance faster if
  860. // a newline-carriage return is hit.
  861. //
  862. // Wikipedia:
  863. // Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or
  864. // CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A)...
  865. // * LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
  866. // * CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
  867. // * CR: Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9
  868. const char* p = buf; // the read head
  869. char* q = buf; // the write head
  870. const char CR = 0x0d;
  871. const char LF = 0x0a;
  872. buf[length] = 0;
  873. while( *p ) {
  874. assert( p < (buf+length) );
  875. assert( q <= (buf+length) );
  876. assert( q <= p );
  877. if ( *p == CR ) {
  878. *q++ = LF;
  879. p++;
  880. if ( *p == LF ) { // check for CR+LF (and skip LF)
  881. p++;
  882. }
  883. }
  884. else {
  885. *q++ = *p++;
  886. }
  887. }
  888. assert( q <= (buf+length) );
  889. *q = 0;
  890. Parse( buf, 0, encoding );
  891. delete [] buf;
  892. return !Error();
  893. }
  894. bool TiXmlDocument::SaveFile( const char * filename ) const
  895. {
  896. // The old c stuff lives on...
  897. FILE* fp = TiXmlFOpen( filename, "w" );
  898. if ( fp )
  899. {
  900. bool result = SaveFile( fp );
  901. fclose( fp );
  902. return result;
  903. }
  904. return false;
  905. }
  906. bool TiXmlDocument::SaveFile( FILE* fp ) const
  907. {
  908. if ( useMicrosoftBOM )
  909. {
  910. const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  911. const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  912. const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  913. fputc( TIXML_UTF_LEAD_0, fp );
  914. fputc( TIXML_UTF_LEAD_1, fp );
  915. fputc( TIXML_UTF_LEAD_2, fp );
  916. }
  917. Print( fp, 0 );
  918. return (ferror(fp) == 0);
  919. }
  920. void TiXmlDocument::CopyTo( TiXmlDocument* target ) const
  921. {
  922. TiXmlNode::CopyTo( target );
  923. target->error = error;
  924. target->errorId = errorId;
  925. target->errorDesc = errorDesc;
  926. target->tabsize = tabsize;
  927. target->errorLocation = errorLocation;
  928. target->useMicrosoftBOM = useMicrosoftBOM;
  929. TiXmlNode* node = 0;
  930. for ( node = firstChild; node; node = node->NextSibling() )
  931. {
  932. target->LinkEndChild( node->Clone() );
  933. }
  934. }
  935. TiXmlNode* TiXmlDocument::Clone() const
  936. {
  937. TiXmlDocument* clone = new TiXmlDocument();
  938. if ( !clone )
  939. return 0;
  940. CopyTo( clone );
  941. return clone;
  942. }
  943. void TiXmlDocument::Print( FILE* cfile, int depth ) const
  944. {
  945. assert( cfile );
  946. for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  947. {
  948. node->Print( cfile, depth );
  949. fprintf( cfile, "\n" );
  950. }
  951. }
  952. bool TiXmlDocument::Accept( TiXmlVisitor* visitor ) const
  953. {
  954. if ( visitor->VisitEnter( *this ) )
  955. {
  956. for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  957. {
  958. if ( !node->Accept( visitor ) )
  959. break;
  960. }
  961. }
  962. return visitor->VisitExit( *this );
  963. }
  964. const TiXmlAttribute* TiXmlAttribute::Next() const
  965. {
  966. // We are using knowledge of the sentinel. The sentinel
  967. // have a value or name.
  968. if ( next->value.empty() && next->name.empty() )
  969. return 0;
  970. return next;
  971. }
  972. /*
  973. TiXmlAttribute* TiXmlAttribute::Next()
  974. {
  975. // We are using knowledge of the sentinel. The sentinel
  976. // have a value or name.
  977. if ( next->value.empty() && next->name.empty() )
  978. return 0;
  979. return next;
  980. }
  981. */
  982. const TiXmlAttribute* TiXmlAttribute::Previous() const
  983. {
  984. // We are using knowledge of the sentinel. The sentinel
  985. // have a value or name.
  986. if ( prev->value.empty() && prev->name.empty() )
  987. return 0;
  988. return prev;
  989. }
  990. /*
  991. TiXmlAttribute* TiXmlAttribute::Previous()
  992. {
  993. // We are using knowledge of the sentinel. The sentinel
  994. // have a value or name.
  995. if ( prev->value.empty() && prev->name.empty() )
  996. return 0;
  997. return prev;
  998. }
  999. */
  1000. void TiXmlAttribute::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
  1001. {
  1002. TIXML_STRING n, v;
  1003. EncodeString( name, &n );
  1004. EncodeString( value, &v );
  1005. if (value.find ('\"') == TIXML_STRING::npos) {
  1006. if ( cfile ) {
  1007. fprintf (cfile, "%s=\"%s\"", n.c_str(), v.c_str() );
  1008. }
  1009. if ( str ) {
  1010. (*str) += n; (*str) += "=\""; (*str) += v; (*str) += "\"";
  1011. }
  1012. }
  1013. else {
  1014. if ( cfile ) {
  1015. fprintf (cfile, "%s='%s'", n.c_str(), v.c_str() );
  1016. }
  1017. if ( str ) {
  1018. (*str) += n; (*str) += "='"; (*str) += v; (*str) += "'";
  1019. }
  1020. }
  1021. }
  1022. int TiXmlAttribute::QueryIntValue( int* ival ) const
  1023. {
  1024. if ( TIXML_SSCANF( value.c_str(), "%d", ival ) == 1 )
  1025. return TIXML_SUCCESS;
  1026. return TIXML_WRONG_TYPE;
  1027. }
  1028. int TiXmlAttribute::QueryDoubleValue( double* dval ) const
  1029. {
  1030. if ( TIXML_SSCANF( value.c_str(), "%lf", dval ) == 1 )
  1031. return TIXML_SUCCESS;
  1032. return TIXML_WRONG_TYPE;
  1033. }
  1034. void TiXmlAttribute::SetIntValue( int _value )
  1035. {
  1036. char buf [64];
  1037. #if defined(TIXML_SNPRINTF)
  1038. TIXML_SNPRINTF(buf, sizeof(buf), "%d", _value);
  1039. #else
  1040. sprintf (buf, "%d", _value);
  1041. #endif
  1042. SetValue (buf);
  1043. }
  1044. void TiXmlAttribute::SetDoubleValue( double _value )
  1045. {
  1046. char buf [256];
  1047. #if defined(TIXML_SNPRINTF)
  1048. TIXML_SNPRINTF( buf, sizeof(buf), "%g", _value);
  1049. #else
  1050. sprintf (buf, "%g", _value);
  1051. #endif
  1052. SetValue (buf);
  1053. }
  1054. int TiXmlAttribute::IntValue() const
  1055. {
  1056. return atoi (value.c_str ());
  1057. }
  1058. double TiXmlAttribute::DoubleValue() const
  1059. {
  1060. return atof (value.c_str ());
  1061. }
  1062. TiXmlComment::TiXmlComment( const TiXmlComment& copy ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT )
  1063. {
  1064. copy.CopyTo( this );
  1065. }
  1066. TiXmlComment& TiXmlComment::operator=( const TiXmlComment& base )
  1067. {
  1068. Clear();
  1069. base.CopyTo( this );
  1070. return *this;
  1071. }
  1072. void TiXmlComment::Print( FILE* cfile, int depth ) const
  1073. {
  1074. assert( cfile );
  1075. for ( int i=0; i<depth; i++ )
  1076. {
  1077. fprintf( cfile, " " );
  1078. }
  1079. fprintf( cfile, "<!--%s-->", value.c_str() );
  1080. }
  1081. void TiXmlComment::CopyTo( TiXmlComment* target ) const
  1082. {
  1083. TiXmlNode::CopyTo( target );
  1084. }
  1085. bool TiXmlComment::Accept( TiXmlVisitor* visitor ) const
  1086. {
  1087. return visitor->Visit( *this );
  1088. }
  1089. TiXmlNode* TiXmlComment::Clone() const
  1090. {
  1091. TiXmlComment* clone = new TiXmlComment();
  1092. if ( !clone )
  1093. return 0;
  1094. CopyTo( clone );
  1095. return clone;
  1096. }
  1097. void TiXmlText::Print( FILE* cfile, int depth ) const
  1098. {
  1099. assert( cfile );
  1100. if ( cdata )
  1101. {
  1102. int i;
  1103. fprintf( cfile, "\n" );
  1104. for ( i=0; i<depth; i++ ) {
  1105. fprintf( cfile, " " );
  1106. }
  1107. fprintf( cfile, "<![CDATA[%s]]>\n", value.c_str() ); // unformatted output
  1108. }
  1109. else
  1110. {
  1111. TIXML_STRING buffer;
  1112. EncodeString( value, &buffer );
  1113. fprintf( cfile, "%s", buffer.c_str() );
  1114. }
  1115. }
  1116. void TiXmlText::CopyTo( TiXmlText* target ) const
  1117. {
  1118. TiXmlNode::CopyTo( target );
  1119. target->cdata = cdata;
  1120. }
  1121. bool TiXmlText::Accept( TiXmlVisitor* visitor ) const
  1122. {
  1123. return visitor->Visit( *this );
  1124. }
  1125. TiXmlNode* TiXmlText::Clone() const
  1126. {
  1127. TiXmlText* clone = 0;
  1128. clone = new TiXmlText( "" );
  1129. if ( !clone )
  1130. return 0;
  1131. CopyTo( clone );
  1132. return clone;
  1133. }
  1134. TiXmlDeclaration::TiXmlDeclaration( const char * _version,
  1135. const char * _encoding,
  1136. const char * _standalone )
  1137. : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1138. {
  1139. version = _version;
  1140. encoding = _encoding;
  1141. standalone = _standalone;
  1142. }
  1143. #ifdef TIXML_USE_STL
  1144. TiXmlDeclaration::TiXmlDeclaration( const std::string& _version,
  1145. const std::string& _encoding,
  1146. const std::string& _standalone )
  1147. : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1148. {
  1149. version = _version;
  1150. encoding = _encoding;
  1151. standalone = _standalone;
  1152. }
  1153. #endif
  1154. TiXmlDeclaration::TiXmlDeclaration( const TiXmlDeclaration& copy )
  1155. : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1156. {
  1157. copy.CopyTo( this );
  1158. }
  1159. TiXmlDeclaration& TiXmlDeclaration::operator=( const TiXmlDeclaration& copy )
  1160. {
  1161. Clear();
  1162. copy.CopyTo( this );
  1163. return *this;
  1164. }
  1165. void TiXmlDeclaration::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
  1166. {
  1167. if ( cfile ) fprintf( cfile, "<?xml " );
  1168. if ( str ) (*str) += "<?xml ";
  1169. if ( !version.empty() ) {
  1170. if ( cfile ) fprintf (cfile, "version=\"%s\" ", version.c_str ());
  1171. if ( str ) { (*str) += "version=\""; (*str) += version; (*str) += "\" "; }
  1172. }
  1173. if ( !encoding.empty() ) {
  1174. if ( cfile ) fprintf (cfile, "encoding=\"%s\" ", encoding.c_str ());
  1175. if ( str ) { (*str) += "encoding=\""; (*str) += encoding; (*str) += "\" "; }
  1176. }
  1177. if ( !standalone.empty() ) {
  1178. if ( cfile ) fprintf (cfile, "standalone=\"%s\" ", standalone.c_str ());
  1179. if ( str ) { (*str) += "standalone=\""; (*str) += standalone; (*str) += "\" "; }
  1180. }
  1181. if ( cfile ) fprintf( cfile, "?>" );
  1182. if ( str ) (*str) += "?>";
  1183. }
  1184. void TiXmlDeclaration::CopyTo( TiXmlDeclaration* target ) const
  1185. {
  1186. TiXmlNode::CopyTo( target );
  1187. target->version = version;
  1188. target->encoding = encoding;
  1189. target->standalone = standalone;
  1190. }
  1191. bool TiXmlDeclaration::Accept( TiXmlVisitor* visitor ) const
  1192. {
  1193. return visitor->Visit( *this );
  1194. }
  1195. TiXmlNode* TiXmlDeclaration::Clone() const
  1196. {
  1197. TiXmlDeclaration* clone = new TiXmlDeclaration();
  1198. if ( !clone )
  1199. return 0;
  1200. CopyTo( clone );
  1201. return clone;
  1202. }
  1203. void TiXmlUnknown::Print( FILE* cfile, int depth ) const
  1204. {
  1205. for ( int i=0; i<depth; i++ )
  1206. fprintf( cfile, " " );
  1207. fprintf( cfile, "<%s>", value.c_str() );
  1208. }
  1209. void TiXmlUnknown::CopyTo( TiXmlUnknown* target ) const
  1210. {
  1211. TiXmlNode::CopyTo( target );
  1212. }
  1213. bool TiXmlUnknown::Accept( TiXmlVisitor* visitor ) const
  1214. {
  1215. return visitor->Visit( *this );
  1216. }
  1217. TiXmlNode* TiXmlUnknown::Clone() const
  1218. {
  1219. TiXmlUnknown* clone = new TiXmlUnknown();
  1220. if ( !clone )
  1221. return 0;
  1222. CopyTo( clone );
  1223. return clone;
  1224. }
  1225. TiXmlAttributeSet::TiXmlAttributeSet()
  1226. {
  1227. sentinel.next = &sentinel;
  1228. sentinel.prev = &sentinel;
  1229. }
  1230. TiXmlAttributeSet::~TiXmlAttributeSet()
  1231. {
  1232. assert( sentinel.next == &sentinel );
  1233. assert( sentinel.prev == &sentinel );
  1234. }
  1235. void TiXmlAttributeSet::Add( TiXmlAttribute* addMe )
  1236. {
  1237. #ifdef TIXML_USE_STL
  1238. assert( !Find( TIXML_STRING( addMe->Name() ) ) ); // Shouldn't be multiply adding to the set.
  1239. #else
  1240. assert( !Find( addMe->Name() ) ); // Shouldn't be multiply adding to the set.
  1241. #endif
  1242. addMe->next = &sentinel;
  1243. addMe->prev = sentinel.prev;
  1244. sentinel.prev->next = addMe;
  1245. sentinel.prev = addMe;
  1246. }
  1247. void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe )
  1248. {
  1249. TiXmlAttribute* node;
  1250. for( node = sentinel.next; node != &sentinel; node = node->next )
  1251. {
  1252. if ( node == removeMe )
  1253. {
  1254. node->prev->next = node->next;
  1255. node->next->prev = node->prev;
  1256. node->next = 0;
  1257. node->prev = 0;
  1258. return;
  1259. }
  1260. }
  1261. assert( 0 ); // we tried to remove a non-linked attribute.
  1262. }
  1263. #ifdef TIXML_USE_STL
  1264. TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const
  1265. {
  1266. for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
  1267. {
  1268. if ( node->name == name )
  1269. return node;
  1270. }
  1271. return 0;
  1272. }
  1273. TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const std::string& _name )
  1274. {
  1275. TiXmlAttribute* attrib = Find( _name );
  1276. if ( !attrib ) {
  1277. attrib = new TiXmlAttribute();
  1278. Add( attrib );
  1279. attrib->SetName( _name );
  1280. }
  1281. return attrib;
  1282. }
  1283. #endif
  1284. TiXmlAttribute* TiXmlAttributeSet::Find( const char* name ) const
  1285. {
  1286. for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
  1287. {
  1288. if ( strcmp( node->name.c_str(), name ) == 0 )
  1289. return node;
  1290. }
  1291. return 0;
  1292. }
  1293. TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const char* _name )
  1294. {
  1295. TiXmlAttribute* attrib = Find( _name );
  1296. if ( !attrib ) {
  1297. attrib = new TiXmlAttribute();
  1298. Add( attrib );
  1299. attrib->SetName( _name );
  1300. }
  1301. return attrib;
  1302. }
  1303. #ifdef TIXML_USE_STL
  1304. std::istream& operator>> (std::istream & in, TiXmlNode & base)
  1305. {
  1306. TIXML_STRING tag;
  1307. tag.reserve( 8 * 1000 );
  1308. base.StreamIn( &in, &tag );
  1309. base.Parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING );
  1310. return in;
  1311. }
  1312. #endif
  1313. #ifdef TIXML_USE_STL
  1314. std::ostream& operator<< (std::ostream & out, const TiXmlNode & base)
  1315. {
  1316. TiXmlPrinter printer;
  1317. printer.SetStreamPrinting();
  1318. base.Accept( &printer );
  1319. out << printer.Str();
  1320. return out;
  1321. }
  1322. std::string& operator<< (std::string& out, const TiXmlNode& base )
  1323. {
  1324. TiXmlPrinter printer;
  1325. printer.SetStreamPrinting();
  1326. base.Accept( &printer );
  1327. out.append( printer.Str() );
  1328. return out;
  1329. }
  1330. #endif
  1331. TiXmlHandle TiXmlHandle::FirstChild() const
  1332. {
  1333. if ( node )
  1334. {
  1335. TiXmlNode* child = node->FirstChild();
  1336. if ( child )
  1337. return TiXmlHandle( child );
  1338. }
  1339. return TiXmlHandle( 0 );
  1340. }
  1341. TiXmlHandle TiXmlHandle::FirstChild( const char * value ) const
  1342. {
  1343. if ( node )
  1344. {
  1345. TiXmlNode* child = node->FirstChild( value );
  1346. if ( child )
  1347. return TiXmlHandle( child );
  1348. }
  1349. return TiXmlHandle( 0 );
  1350. }
  1351. TiXmlHandle TiXmlHandle::FirstChildElement() const
  1352. {
  1353. if ( node )
  1354. {
  1355. TiXmlElement* child = node->FirstChildElement();
  1356. if ( child )
  1357. return TiXmlHandle( child );
  1358. }
  1359. return TiXmlHandle( 0 );
  1360. }
  1361. TiXmlHandle TiXmlHandle::FirstChildElement( const char * value ) const
  1362. {
  1363. if ( node )
  1364. {
  1365. TiXmlElement* child = node->FirstChildElement( value );
  1366. if ( child )
  1367. return TiXmlHandle( child );
  1368. }
  1369. return TiXmlHandle( 0 );
  1370. }
  1371. TiXmlHandle TiXmlHandle::Child( int count ) const
  1372. {
  1373. if ( node )
  1374. {
  1375. int i;
  1376. TiXmlNode* child = node->FirstChild();
  1377. for ( i=0;
  1378. child && i<count;
  1379. child = child->NextSibling(), ++i )
  1380. {
  1381. // nothing
  1382. }
  1383. if ( child )
  1384. return TiXmlHandle( child );
  1385. }
  1386. return TiXmlHandle( 0 );
  1387. }
  1388. TiXmlHandle TiXmlHandle::Child( const char* value, int count ) const
  1389. {
  1390. if ( node )
  1391. {
  1392. int i;
  1393. TiXmlNode* child = node->FirstChild( value );
  1394. for ( i=0;
  1395. child && i<count;
  1396. child = child->NextSibling( value ), ++i )
  1397. {
  1398. // nothing
  1399. }
  1400. if ( child )
  1401. return TiXmlHandle( child );
  1402. }
  1403. return TiXmlHandle( 0 );
  1404. }
  1405. TiXmlHandle TiXmlHandle::ChildElement( int count ) const
  1406. {
  1407. if ( node )
  1408. {
  1409. int i;
  1410. TiXmlElement* child = node->FirstChildElement();
  1411. for ( i=0;
  1412. child && i<count;
  1413. child = child->NextSiblingElement(), ++i )
  1414. {
  1415. // nothing
  1416. }
  1417. if ( child )
  1418. return TiXmlHandle( child );
  1419. }
  1420. return TiXmlHandle( 0 );
  1421. }
  1422. TiXmlHandle TiXmlHandle::ChildElement( const char* value, int count ) const
  1423. {
  1424. if ( node )
  1425. {
  1426. int i;
  1427. TiXmlElement* child = node->FirstChildElement( value );
  1428. for ( i=0;
  1429. child && i<count;
  1430. child = child->NextSiblingElement( value ), ++i )
  1431. {
  1432. // nothing
  1433. }
  1434. if ( child )
  1435. return TiXmlHandle( child );
  1436. }
  1437. return TiXmlHandle( 0 );
  1438. }
  1439. bool TiXmlPrinter::VisitEnter( const TiXmlDocument& )
  1440. {
  1441. return true;
  1442. }
  1443. bool TiXmlPrinter::VisitExit( const TiXmlDocument& )
  1444. {
  1445. return true;
  1446. }
  1447. bool TiXmlPrinter::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
  1448. {
  1449. DoIndent();
  1450. buffer += "<";
  1451. buffer += element.Value();
  1452. for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
  1453. {
  1454. buffer += " ";
  1455. attrib->Print( 0, 0, &buffer );
  1456. }
  1457. if ( !element.FirstChild() )
  1458. {
  1459. buffer += " />";
  1460. DoLineBreak();
  1461. }
  1462. else
  1463. {
  1464. buffer += ">";
  1465. if ( element.FirstChild()->ToText()
  1466. && element.LastChild() == element.FirstChild()
  1467. && element.FirstChild()->ToText()->CDATA() == false )
  1468. {
  1469. simpleTextPrint = true;
  1470. // no DoLineBreak()!
  1471. }
  1472. else
  1473. {
  1474. DoLineBreak();
  1475. }
  1476. }
  1477. ++depth;
  1478. return true;
  1479. }
  1480. bool TiXmlPrinter::VisitExit( const TiXmlElement& element )
  1481. {
  1482. --depth;
  1483. if ( !element.FirstChild() )
  1484. {
  1485. // nothing.
  1486. }
  1487. else
  1488. {
  1489. if ( simpleTextPrint )
  1490. {
  1491. simpleTextPrint = false;
  1492. }
  1493. else
  1494. {
  1495. DoIndent();
  1496. }
  1497. buffer += "</";
  1498. buffer += element.Value();
  1499. buffer += ">";
  1500. DoLineBreak();
  1501. }
  1502. return true;
  1503. }
  1504. bool TiXmlPrinter::Visit( const TiXmlText& text )
  1505. {
  1506. if ( text.CDATA() )
  1507. {
  1508. DoIndent();
  1509. buffer += "<![CDATA[";
  1510. buffer += text.Value();
  1511. buffer += "]]>";
  1512. DoLineBreak();
  1513. }
  1514. else if ( simpleTextPrint )
  1515. {
  1516. TIXML_STRING str;
  1517. TiXmlBase::EncodeString( text.ValueTStr(), &str );
  1518. buffer += str;
  1519. }
  1520. else
  1521. {
  1522. DoIndent();
  1523. TIXML_STRING str;
  1524. TiXmlBase::EncodeString( text.ValueTStr(), &str );
  1525. buffer += str;
  1526. DoLineBreak();
  1527. }
  1528. return true;
  1529. }
  1530. bool TiXmlPrinter::Visit( const TiXmlDeclaration& declaration )
  1531. {
  1532. DoIndent();
  1533. declaration.Print( 0, 0, &buffer );
  1534. DoLineBreak();
  1535. return true;
  1536. }
  1537. bool TiXmlPrinter::Visit( const TiXmlComment& comment )
  1538. {
  1539. DoIndent();
  1540. buffer += "<!--";
  1541. buffer += comment.Value();
  1542. buffer += "-->";
  1543. DoLineBreak();
  1544. return true;
  1545. }
  1546. bool TiXmlPrinter::Visit( const TiXmlUnknown& unknown )
  1547. {
  1548. DoIndent();
  1549. buffer += "<";
  1550. buffer += unknown.Value();
  1551. buffer += ">";
  1552. DoLineBreak();
  1553. return true;
  1554. }