tinyxml.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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. // If we have a file, assume it is all one big XML file, and read it in.
  795. // The document parser may decide the document ends sooner than the entire file, however.
  796. TIXML_STRING data;
  797. data.reserve( length );
  798. // Subtle bug here. TinyXml did use fgets. But from the XML spec:
  799. // 2.11 End-of-Line Handling
  800. // <snip>
  801. // <quote>
  802. // ...the XML processor MUST behave as if it normalized all line breaks in external
  803. // parsed entities (including the document entity) on input, before parsing, by translating
  804. // both the two-character sequence #xD #xA and any #xD that is not followed by #xA to
  805. // a single #xA character.
  806. // </quote>
  807. //
  808. // It is not clear fgets does that, and certainly isn't clear it works cross platform.
  809. // Generally, you expect fgets to translate from the convention of the OS to the c/unix
  810. // convention, and not work generally.
  811. /*
  812. while( fgets( buf, sizeof(buf), file ) )
  813. {
  814. data += buf;
  815. }
  816. */
  817. char* buf = new char[ length+1 ];
  818. buf[0] = 0;
  819. if ( fread( buf, length, 1, file ) != 1 ) {
  820. delete [] buf;
  821. SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
  822. return false;
  823. }
  824. const char* lastPos = buf;
  825. const char* p = buf;
  826. buf[length] = 0;
  827. while( *p ) {
  828. assert( p < (buf+length) );
  829. if ( *p == 0xa ) {
  830. // Newline character. No special rules for this. Append all the characters
  831. // since the last string, and include the newline.
  832. data.append( lastPos, (p-lastPos+1) ); // append, include the newline
  833. ++p; // move past the newline
  834. lastPos = p; // and point to the new buffer (may be 0)
  835. assert( p <= (buf+length) );
  836. }
  837. else if ( *p == 0xd ) {
  838. // Carriage return. Append what we have so far, then
  839. // handle moving forward in the buffer.
  840. if ( (p-lastPos) > 0 ) {
  841. data.append( lastPos, p-lastPos ); // do not add the CR
  842. }
  843. data += (char)0xa; // a proper newline
  844. if ( *(p+1) == 0xa ) {
  845. // Carriage return - new line sequence
  846. p += 2;
  847. lastPos = p;
  848. assert( p <= (buf+length) );
  849. }
  850. else {
  851. // it was followed by something else...that is presumably characters again.
  852. ++p;
  853. lastPos = p;
  854. assert( p <= (buf+length) );
  855. }
  856. }
  857. else {
  858. ++p;
  859. }
  860. }
  861. // Handle any left over characters.
  862. if ( p-lastPos ) {
  863. data.append( lastPos, p-lastPos );
  864. }
  865. delete [] buf;
  866. buf = 0;
  867. Parse( data.c_str(), 0, encoding );
  868. if ( Error() )
  869. return false;
  870. else
  871. return true;
  872. }
  873. bool TiXmlDocument::SaveFile( const char * filename ) const
  874. {
  875. // The old c stuff lives on...
  876. FILE* fp = TiXmlFOpen( filename, "w" );
  877. if ( fp )
  878. {
  879. bool result = SaveFile( fp );
  880. fclose( fp );
  881. return result;
  882. }
  883. return false;
  884. }
  885. bool TiXmlDocument::SaveFile( FILE* fp ) const
  886. {
  887. if ( useMicrosoftBOM )
  888. {
  889. const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
  890. const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
  891. const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
  892. fputc( TIXML_UTF_LEAD_0, fp );
  893. fputc( TIXML_UTF_LEAD_1, fp );
  894. fputc( TIXML_UTF_LEAD_2, fp );
  895. }
  896. Print( fp, 0 );
  897. return (ferror(fp) == 0);
  898. }
  899. void TiXmlDocument::CopyTo( TiXmlDocument* target ) const
  900. {
  901. TiXmlNode::CopyTo( target );
  902. target->error = error;
  903. target->errorId = errorId;
  904. target->errorDesc = errorDesc;
  905. target->tabsize = tabsize;
  906. target->errorLocation = errorLocation;
  907. target->useMicrosoftBOM = useMicrosoftBOM;
  908. TiXmlNode* node = 0;
  909. for ( node = firstChild; node; node = node->NextSibling() )
  910. {
  911. target->LinkEndChild( node->Clone() );
  912. }
  913. }
  914. TiXmlNode* TiXmlDocument::Clone() const
  915. {
  916. TiXmlDocument* clone = new TiXmlDocument();
  917. if ( !clone )
  918. return 0;
  919. CopyTo( clone );
  920. return clone;
  921. }
  922. void TiXmlDocument::Print( FILE* cfile, int depth ) const
  923. {
  924. assert( cfile );
  925. for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  926. {
  927. node->Print( cfile, depth );
  928. fprintf( cfile, "\n" );
  929. }
  930. }
  931. bool TiXmlDocument::Accept( TiXmlVisitor* visitor ) const
  932. {
  933. if ( visitor->VisitEnter( *this ) )
  934. {
  935. for ( const TiXmlNode* node=FirstChild(); node; node=node->NextSibling() )
  936. {
  937. if ( !node->Accept( visitor ) )
  938. break;
  939. }
  940. }
  941. return visitor->VisitExit( *this );
  942. }
  943. const TiXmlAttribute* TiXmlAttribute::Next() const
  944. {
  945. // We are using knowledge of the sentinel. The sentinel
  946. // have a value or name.
  947. if ( next->value.empty() && next->name.empty() )
  948. return 0;
  949. return next;
  950. }
  951. /*
  952. TiXmlAttribute* TiXmlAttribute::Next()
  953. {
  954. // We are using knowledge of the sentinel. The sentinel
  955. // have a value or name.
  956. if ( next->value.empty() && next->name.empty() )
  957. return 0;
  958. return next;
  959. }
  960. */
  961. const TiXmlAttribute* TiXmlAttribute::Previous() const
  962. {
  963. // We are using knowledge of the sentinel. The sentinel
  964. // have a value or name.
  965. if ( prev->value.empty() && prev->name.empty() )
  966. return 0;
  967. return prev;
  968. }
  969. /*
  970. TiXmlAttribute* TiXmlAttribute::Previous()
  971. {
  972. // We are using knowledge of the sentinel. The sentinel
  973. // have a value or name.
  974. if ( prev->value.empty() && prev->name.empty() )
  975. return 0;
  976. return prev;
  977. }
  978. */
  979. void TiXmlAttribute::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
  980. {
  981. TIXML_STRING n, v;
  982. EncodeString( name, &n );
  983. EncodeString( value, &v );
  984. if (value.find ('\"') == TIXML_STRING::npos) {
  985. if ( cfile ) {
  986. fprintf (cfile, "%s=\"%s\"", n.c_str(), v.c_str() );
  987. }
  988. if ( str ) {
  989. (*str) += n; (*str) += "=\""; (*str) += v; (*str) += "\"";
  990. }
  991. }
  992. else {
  993. if ( cfile ) {
  994. fprintf (cfile, "%s='%s'", n.c_str(), v.c_str() );
  995. }
  996. if ( str ) {
  997. (*str) += n; (*str) += "='"; (*str) += v; (*str) += "'";
  998. }
  999. }
  1000. }
  1001. int TiXmlAttribute::QueryIntValue( int* ival ) const
  1002. {
  1003. if ( TIXML_SSCANF( value.c_str(), "%d", ival ) == 1 )
  1004. return TIXML_SUCCESS;
  1005. return TIXML_WRONG_TYPE;
  1006. }
  1007. int TiXmlAttribute::QueryDoubleValue( double* dval ) const
  1008. {
  1009. if ( TIXML_SSCANF( value.c_str(), "%lf", dval ) == 1 )
  1010. return TIXML_SUCCESS;
  1011. return TIXML_WRONG_TYPE;
  1012. }
  1013. void TiXmlAttribute::SetIntValue( int _value )
  1014. {
  1015. char buf [64];
  1016. #if defined(TIXML_SNPRINTF)
  1017. TIXML_SNPRINTF(buf, sizeof(buf), "%d", _value);
  1018. #else
  1019. sprintf (buf, "%d", _value);
  1020. #endif
  1021. SetValue (buf);
  1022. }
  1023. void TiXmlAttribute::SetDoubleValue( double _value )
  1024. {
  1025. char buf [256];
  1026. #if defined(TIXML_SNPRINTF)
  1027. TIXML_SNPRINTF( buf, sizeof(buf), "%g", _value);
  1028. #else
  1029. sprintf (buf, "%g", _value);
  1030. #endif
  1031. SetValue (buf);
  1032. }
  1033. int TiXmlAttribute::IntValue() const
  1034. {
  1035. return atoi (value.c_str ());
  1036. }
  1037. double TiXmlAttribute::DoubleValue() const
  1038. {
  1039. return atof (value.c_str ());
  1040. }
  1041. TiXmlComment::TiXmlComment( const TiXmlComment& copy ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT )
  1042. {
  1043. copy.CopyTo( this );
  1044. }
  1045. void TiXmlComment::operator=( const TiXmlComment& base )
  1046. {
  1047. Clear();
  1048. base.CopyTo( this );
  1049. }
  1050. void TiXmlComment::Print( FILE* cfile, int depth ) const
  1051. {
  1052. assert( cfile );
  1053. for ( int i=0; i<depth; i++ )
  1054. {
  1055. fprintf( cfile, " " );
  1056. }
  1057. fprintf( cfile, "<!--%s-->", value.c_str() );
  1058. }
  1059. void TiXmlComment::CopyTo( TiXmlComment* target ) const
  1060. {
  1061. TiXmlNode::CopyTo( target );
  1062. }
  1063. bool TiXmlComment::Accept( TiXmlVisitor* visitor ) const
  1064. {
  1065. return visitor->Visit( *this );
  1066. }
  1067. TiXmlNode* TiXmlComment::Clone() const
  1068. {
  1069. TiXmlComment* clone = new TiXmlComment();
  1070. if ( !clone )
  1071. return 0;
  1072. CopyTo( clone );
  1073. return clone;
  1074. }
  1075. void TiXmlText::Print( FILE* cfile, int depth ) const
  1076. {
  1077. assert( cfile );
  1078. if ( cdata )
  1079. {
  1080. int i;
  1081. fprintf( cfile, "\n" );
  1082. for ( i=0; i<depth; i++ ) {
  1083. fprintf( cfile, " " );
  1084. }
  1085. fprintf( cfile, "<![CDATA[%s]]>\n", value.c_str() ); // unformatted output
  1086. }
  1087. else
  1088. {
  1089. TIXML_STRING buffer;
  1090. EncodeString( value, &buffer );
  1091. fprintf( cfile, "%s", buffer.c_str() );
  1092. }
  1093. }
  1094. void TiXmlText::CopyTo( TiXmlText* target ) const
  1095. {
  1096. TiXmlNode::CopyTo( target );
  1097. target->cdata = cdata;
  1098. }
  1099. bool TiXmlText::Accept( TiXmlVisitor* visitor ) const
  1100. {
  1101. return visitor->Visit( *this );
  1102. }
  1103. TiXmlNode* TiXmlText::Clone() const
  1104. {
  1105. TiXmlText* clone = 0;
  1106. clone = new TiXmlText( "" );
  1107. if ( !clone )
  1108. return 0;
  1109. CopyTo( clone );
  1110. return clone;
  1111. }
  1112. TiXmlDeclaration::TiXmlDeclaration( const char * _version,
  1113. const char * _encoding,
  1114. const char * _standalone )
  1115. : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1116. {
  1117. version = _version;
  1118. encoding = _encoding;
  1119. standalone = _standalone;
  1120. }
  1121. #ifdef TIXML_USE_STL
  1122. TiXmlDeclaration::TiXmlDeclaration( const std::string& _version,
  1123. const std::string& _encoding,
  1124. const std::string& _standalone )
  1125. : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1126. {
  1127. version = _version;
  1128. encoding = _encoding;
  1129. standalone = _standalone;
  1130. }
  1131. #endif
  1132. TiXmlDeclaration::TiXmlDeclaration( const TiXmlDeclaration& copy )
  1133. : TiXmlNode( TiXmlNode::TINYXML_DECLARATION )
  1134. {
  1135. copy.CopyTo( this );
  1136. }
  1137. void TiXmlDeclaration::operator=( const TiXmlDeclaration& copy )
  1138. {
  1139. Clear();
  1140. copy.CopyTo( this );
  1141. }
  1142. void TiXmlDeclaration::Print( FILE* cfile, int /*depth*/, TIXML_STRING* str ) const
  1143. {
  1144. if ( cfile ) fprintf( cfile, "<?xml " );
  1145. if ( str ) (*str) += "<?xml ";
  1146. if ( !version.empty() ) {
  1147. if ( cfile ) fprintf (cfile, "version=\"%s\" ", version.c_str ());
  1148. if ( str ) { (*str) += "version=\""; (*str) += version; (*str) += "\" "; }
  1149. }
  1150. if ( !encoding.empty() ) {
  1151. if ( cfile ) fprintf (cfile, "encoding=\"%s\" ", encoding.c_str ());
  1152. if ( str ) { (*str) += "encoding=\""; (*str) += encoding; (*str) += "\" "; }
  1153. }
  1154. if ( !standalone.empty() ) {
  1155. if ( cfile ) fprintf (cfile, "standalone=\"%s\" ", standalone.c_str ());
  1156. if ( str ) { (*str) += "standalone=\""; (*str) += standalone; (*str) += "\" "; }
  1157. }
  1158. if ( cfile ) fprintf( cfile, "?>" );
  1159. if ( str ) (*str) += "?>";
  1160. }
  1161. void TiXmlDeclaration::CopyTo( TiXmlDeclaration* target ) const
  1162. {
  1163. TiXmlNode::CopyTo( target );
  1164. target->version = version;
  1165. target->encoding = encoding;
  1166. target->standalone = standalone;
  1167. }
  1168. bool TiXmlDeclaration::Accept( TiXmlVisitor* visitor ) const
  1169. {
  1170. return visitor->Visit( *this );
  1171. }
  1172. TiXmlNode* TiXmlDeclaration::Clone() const
  1173. {
  1174. TiXmlDeclaration* clone = new TiXmlDeclaration();
  1175. if ( !clone )
  1176. return 0;
  1177. CopyTo( clone );
  1178. return clone;
  1179. }
  1180. void TiXmlUnknown::Print( FILE* cfile, int depth ) const
  1181. {
  1182. for ( int i=0; i<depth; i++ )
  1183. fprintf( cfile, " " );
  1184. fprintf( cfile, "<%s>", value.c_str() );
  1185. }
  1186. void TiXmlUnknown::CopyTo( TiXmlUnknown* target ) const
  1187. {
  1188. TiXmlNode::CopyTo( target );
  1189. }
  1190. bool TiXmlUnknown::Accept( TiXmlVisitor* visitor ) const
  1191. {
  1192. return visitor->Visit( *this );
  1193. }
  1194. TiXmlNode* TiXmlUnknown::Clone() const
  1195. {
  1196. TiXmlUnknown* clone = new TiXmlUnknown();
  1197. if ( !clone )
  1198. return 0;
  1199. CopyTo( clone );
  1200. return clone;
  1201. }
  1202. TiXmlAttributeSet::TiXmlAttributeSet()
  1203. {
  1204. sentinel.next = &sentinel;
  1205. sentinel.prev = &sentinel;
  1206. }
  1207. TiXmlAttributeSet::~TiXmlAttributeSet()
  1208. {
  1209. assert( sentinel.next == &sentinel );
  1210. assert( sentinel.prev == &sentinel );
  1211. }
  1212. void TiXmlAttributeSet::Add( TiXmlAttribute* addMe )
  1213. {
  1214. #ifdef TIXML_USE_STL
  1215. assert( !Find( TIXML_STRING( addMe->Name() ) ) ); // Shouldn't be multiply adding to the set.
  1216. #else
  1217. assert( !Find( addMe->Name() ) ); // Shouldn't be multiply adding to the set.
  1218. #endif
  1219. addMe->next = &sentinel;
  1220. addMe->prev = sentinel.prev;
  1221. sentinel.prev->next = addMe;
  1222. sentinel.prev = addMe;
  1223. }
  1224. void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe )
  1225. {
  1226. TiXmlAttribute* node;
  1227. for( node = sentinel.next; node != &sentinel; node = node->next )
  1228. {
  1229. if ( node == removeMe )
  1230. {
  1231. node->prev->next = node->next;
  1232. node->next->prev = node->prev;
  1233. node->next = 0;
  1234. node->prev = 0;
  1235. return;
  1236. }
  1237. }
  1238. assert( 0 ); // we tried to remove a non-linked attribute.
  1239. }
  1240. #ifdef TIXML_USE_STL
  1241. TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const
  1242. {
  1243. for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
  1244. {
  1245. if ( node->name == name )
  1246. return node;
  1247. }
  1248. return 0;
  1249. }
  1250. TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const std::string& _name )
  1251. {
  1252. TiXmlAttribute* attrib = Find( _name );
  1253. if ( !attrib ) {
  1254. attrib = new TiXmlAttribute();
  1255. Add( attrib );
  1256. attrib->SetName( _name );
  1257. }
  1258. return attrib;
  1259. }
  1260. #endif
  1261. TiXmlAttribute* TiXmlAttributeSet::Find( const char* name ) const
  1262. {
  1263. for( TiXmlAttribute* node = sentinel.next; node != &sentinel; node = node->next )
  1264. {
  1265. if ( strcmp( node->name.c_str(), name ) == 0 )
  1266. return node;
  1267. }
  1268. return 0;
  1269. }
  1270. TiXmlAttribute* TiXmlAttributeSet::FindOrCreate( const char* _name )
  1271. {
  1272. TiXmlAttribute* attrib = Find( _name );
  1273. if ( !attrib ) {
  1274. attrib = new TiXmlAttribute();
  1275. Add( attrib );
  1276. attrib->SetName( _name );
  1277. }
  1278. return attrib;
  1279. }
  1280. #ifdef TIXML_USE_STL
  1281. std::istream& operator>> (std::istream & in, TiXmlNode & base)
  1282. {
  1283. TIXML_STRING tag;
  1284. tag.reserve( 8 * 1000 );
  1285. base.StreamIn( &in, &tag );
  1286. base.Parse( tag.c_str(), 0, TIXML_DEFAULT_ENCODING );
  1287. return in;
  1288. }
  1289. #endif
  1290. #ifdef TIXML_USE_STL
  1291. std::ostream& operator<< (std::ostream & out, const TiXmlNode & base)
  1292. {
  1293. TiXmlPrinter printer;
  1294. printer.SetStreamPrinting();
  1295. base.Accept( &printer );
  1296. out << printer.Str();
  1297. return out;
  1298. }
  1299. std::string& operator<< (std::string& out, const TiXmlNode& base )
  1300. {
  1301. TiXmlPrinter printer;
  1302. printer.SetStreamPrinting();
  1303. base.Accept( &printer );
  1304. out.append( printer.Str() );
  1305. return out;
  1306. }
  1307. #endif
  1308. TiXmlHandle TiXmlHandle::FirstChild() const
  1309. {
  1310. if ( node )
  1311. {
  1312. TiXmlNode* child = node->FirstChild();
  1313. if ( child )
  1314. return TiXmlHandle( child );
  1315. }
  1316. return TiXmlHandle( 0 );
  1317. }
  1318. TiXmlHandle TiXmlHandle::FirstChild( const char * value ) const
  1319. {
  1320. if ( node )
  1321. {
  1322. TiXmlNode* child = node->FirstChild( value );
  1323. if ( child )
  1324. return TiXmlHandle( child );
  1325. }
  1326. return TiXmlHandle( 0 );
  1327. }
  1328. TiXmlHandle TiXmlHandle::FirstChildElement() const
  1329. {
  1330. if ( node )
  1331. {
  1332. TiXmlElement* child = node->FirstChildElement();
  1333. if ( child )
  1334. return TiXmlHandle( child );
  1335. }
  1336. return TiXmlHandle( 0 );
  1337. }
  1338. TiXmlHandle TiXmlHandle::FirstChildElement( const char * value ) const
  1339. {
  1340. if ( node )
  1341. {
  1342. TiXmlElement* child = node->FirstChildElement( value );
  1343. if ( child )
  1344. return TiXmlHandle( child );
  1345. }
  1346. return TiXmlHandle( 0 );
  1347. }
  1348. TiXmlHandle TiXmlHandle::Child( int count ) const
  1349. {
  1350. if ( node )
  1351. {
  1352. int i;
  1353. TiXmlNode* child = node->FirstChild();
  1354. for ( i=0;
  1355. child && i<count;
  1356. child = child->NextSibling(), ++i )
  1357. {
  1358. // nothing
  1359. }
  1360. if ( child )
  1361. return TiXmlHandle( child );
  1362. }
  1363. return TiXmlHandle( 0 );
  1364. }
  1365. TiXmlHandle TiXmlHandle::Child( const char* value, int count ) const
  1366. {
  1367. if ( node )
  1368. {
  1369. int i;
  1370. TiXmlNode* child = node->FirstChild( value );
  1371. for ( i=0;
  1372. child && i<count;
  1373. child = child->NextSibling( value ), ++i )
  1374. {
  1375. // nothing
  1376. }
  1377. if ( child )
  1378. return TiXmlHandle( child );
  1379. }
  1380. return TiXmlHandle( 0 );
  1381. }
  1382. TiXmlHandle TiXmlHandle::ChildElement( int count ) const
  1383. {
  1384. if ( node )
  1385. {
  1386. int i;
  1387. TiXmlElement* child = node->FirstChildElement();
  1388. for ( i=0;
  1389. child && i<count;
  1390. child = child->NextSiblingElement(), ++i )
  1391. {
  1392. // nothing
  1393. }
  1394. if ( child )
  1395. return TiXmlHandle( child );
  1396. }
  1397. return TiXmlHandle( 0 );
  1398. }
  1399. TiXmlHandle TiXmlHandle::ChildElement( const char* value, int count ) const
  1400. {
  1401. if ( node )
  1402. {
  1403. int i;
  1404. TiXmlElement* child = node->FirstChildElement( value );
  1405. for ( i=0;
  1406. child && i<count;
  1407. child = child->NextSiblingElement( value ), ++i )
  1408. {
  1409. // nothing
  1410. }
  1411. if ( child )
  1412. return TiXmlHandle( child );
  1413. }
  1414. return TiXmlHandle( 0 );
  1415. }
  1416. bool TiXmlPrinter::VisitEnter( const TiXmlDocument& )
  1417. {
  1418. return true;
  1419. }
  1420. bool TiXmlPrinter::VisitExit( const TiXmlDocument& )
  1421. {
  1422. return true;
  1423. }
  1424. bool TiXmlPrinter::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
  1425. {
  1426. DoIndent();
  1427. buffer += "<";
  1428. buffer += element.Value();
  1429. for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
  1430. {
  1431. buffer += " ";
  1432. attrib->Print( 0, 0, &buffer );
  1433. }
  1434. if ( !element.FirstChild() )
  1435. {
  1436. buffer += " />";
  1437. DoLineBreak();
  1438. }
  1439. else
  1440. {
  1441. buffer += ">";
  1442. if ( element.FirstChild()->ToText()
  1443. && element.LastChild() == element.FirstChild()
  1444. && element.FirstChild()->ToText()->CDATA() == false )
  1445. {
  1446. simpleTextPrint = true;
  1447. // no DoLineBreak()!
  1448. }
  1449. else
  1450. {
  1451. DoLineBreak();
  1452. }
  1453. }
  1454. ++depth;
  1455. return true;
  1456. }
  1457. bool TiXmlPrinter::VisitExit( const TiXmlElement& element )
  1458. {
  1459. --depth;
  1460. if ( !element.FirstChild() )
  1461. {
  1462. // nothing.
  1463. }
  1464. else
  1465. {
  1466. if ( simpleTextPrint )
  1467. {
  1468. simpleTextPrint = false;
  1469. }
  1470. else
  1471. {
  1472. DoIndent();
  1473. }
  1474. buffer += "</";
  1475. buffer += element.Value();
  1476. buffer += ">";
  1477. DoLineBreak();
  1478. }
  1479. return true;
  1480. }
  1481. bool TiXmlPrinter::Visit( const TiXmlText& text )
  1482. {
  1483. if ( text.CDATA() )
  1484. {
  1485. DoIndent();
  1486. buffer += "<![CDATA[";
  1487. buffer += text.Value();
  1488. buffer += "]]>";
  1489. DoLineBreak();
  1490. }
  1491. else if ( simpleTextPrint )
  1492. {
  1493. TIXML_STRING str;
  1494. TiXmlBase::EncodeString( text.ValueTStr(), &str );
  1495. buffer += str;
  1496. }
  1497. else
  1498. {
  1499. DoIndent();
  1500. TIXML_STRING str;
  1501. TiXmlBase::EncodeString( text.ValueTStr(), &str );
  1502. buffer += str;
  1503. DoLineBreak();
  1504. }
  1505. return true;
  1506. }
  1507. bool TiXmlPrinter::Visit( const TiXmlDeclaration& declaration )
  1508. {
  1509. DoIndent();
  1510. declaration.Print( 0, 0, &buffer );
  1511. DoLineBreak();
  1512. return true;
  1513. }
  1514. bool TiXmlPrinter::Visit( const TiXmlComment& comment )
  1515. {
  1516. DoIndent();
  1517. buffer += "<!--";
  1518. buffer += comment.Value();
  1519. buffer += "-->";
  1520. DoLineBreak();
  1521. return true;
  1522. }
  1523. bool TiXmlPrinter::Visit( const TiXmlUnknown& unknown )
  1524. {
  1525. DoIndent();
  1526. buffer += "<";
  1527. buffer += unknown.Value();
  1528. buffer += ">";
  1529. DoLineBreak();
  1530. return true;
  1531. }