tinyxml.cpp 36 KB

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