tinyxml.cpp 36 KB

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