tinyxml.cpp 36 KB

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