tinyxml.cpp 36 KB

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