tinyxml.cpp 39 KB

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