2
0

tinyxml.cpp 36 KB

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