tinyxml.cpp 39 KB

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