tinyxml.cpp 36 KB

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