logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://anongit.hacktivis.me/git/libbulletml.git/

tinyxml.cpp (14929B)


  1. /*
  2. Copyright (c) 2000 Lee Thomason (www.grinninglizard.com)
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any
  5. damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any
  7. purpose, including commercial applications, and to alter it and
  8. redistribute it freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must
  10. not claim that you wrote the original software. If you use this
  11. software in a product, an acknowledgment in the product documentation
  12. would be appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and
  14. must not be misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #include "tinyxml.h"
  19. TiXmlNode::TiXmlNode( NodeType _type )
  20. {
  21. parent = 0;
  22. type = _type;
  23. firstChild = 0;
  24. lastChild = 0;
  25. prev = 0;
  26. next = 0;
  27. }
  28. TiXmlNode::~TiXmlNode()
  29. {
  30. TiXmlNode* node = firstChild;
  31. TiXmlNode* temp = 0;
  32. while ( node )
  33. {
  34. temp = node;
  35. node = node->next;
  36. delete temp;
  37. }
  38. }
  39. void TiXmlNode::Clear()
  40. {
  41. TiXmlNode* node = firstChild;
  42. TiXmlNode* temp = 0;
  43. while ( node )
  44. {
  45. temp = node;
  46. node = node->next;
  47. delete temp;
  48. }
  49. firstChild = 0;
  50. lastChild = 0;
  51. }
  52. TiXmlNode* TiXmlNode::LinkEndChild( TiXmlNode* node )
  53. {
  54. node->parent = this;
  55. node->prev = lastChild;
  56. node->next = 0;
  57. if ( lastChild )
  58. lastChild->next = node;
  59. else
  60. firstChild = node; // it was an empty list.
  61. lastChild = node;
  62. return node;
  63. }
  64. TiXmlNode* TiXmlNode::InsertEndChild( const TiXmlNode& addThis )
  65. {
  66. TiXmlNode* node = addThis.Clone();
  67. if ( !node )
  68. return 0;
  69. return LinkEndChild( node );
  70. }
  71. TiXmlNode* TiXmlNode::InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis )
  72. {
  73. if ( beforeThis->parent != this )
  74. return 0;
  75. TiXmlNode* node = addThis.Clone();
  76. if ( !node )
  77. return 0;
  78. node->parent = this;
  79. node->next = beforeThis;
  80. node->prev = beforeThis->prev;
  81. beforeThis->prev->next = node;
  82. beforeThis->prev = node;
  83. return node;
  84. }
  85. TiXmlNode* TiXmlNode::InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis )
  86. {
  87. if ( afterThis->parent != this )
  88. return 0;
  89. TiXmlNode* node = addThis.Clone();
  90. if ( !node )
  91. return 0;
  92. node->parent = this;
  93. node->prev = afterThis;
  94. node->next = afterThis->next;
  95. afterThis->next->prev = node;
  96. afterThis->next = node;
  97. return node;
  98. }
  99. TiXmlNode* TiXmlNode::ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis )
  100. {
  101. if ( replaceThis->parent != this )
  102. return 0;
  103. TiXmlNode* node = withThis.Clone();
  104. if ( !node )
  105. return 0;
  106. node->next = replaceThis->next;
  107. node->prev = replaceThis->prev;
  108. if ( replaceThis->next )
  109. replaceThis->next->prev = node;
  110. else
  111. lastChild = node;
  112. if ( replaceThis->prev )
  113. replaceThis->prev->next = node;
  114. else
  115. firstChild = node;
  116. delete replaceThis;
  117. return node;
  118. }
  119. bool TiXmlNode::RemoveChild( TiXmlNode* removeThis )
  120. {
  121. if ( removeThis->parent != this )
  122. {
  123. assert( 0 );
  124. return false;
  125. }
  126. if ( removeThis->next )
  127. removeThis->next->prev = removeThis->prev;
  128. else
  129. lastChild = removeThis->prev;
  130. if ( removeThis->prev )
  131. removeThis->prev->next = removeThis->next;
  132. else
  133. firstChild = removeThis->next;
  134. delete removeThis;
  135. return true;
  136. }
  137. TiXmlNode* TiXmlNode::FirstChild( const std::string& value ) const
  138. {
  139. TiXmlNode* node;
  140. for ( node = firstChild; node; node = node->next )
  141. {
  142. if ( node->Value() == value )
  143. return node;
  144. }
  145. return 0;
  146. }
  147. TiXmlNode* TiXmlNode::LastChild( const std::string& value ) const
  148. {
  149. TiXmlNode* node;
  150. for ( node = lastChild; node; node = node->prev )
  151. {
  152. if ( node->Value() == value )
  153. return node;
  154. }
  155. return 0;
  156. }
  157. TiXmlNode* TiXmlNode::IterateChildren( TiXmlNode* previous )
  158. {
  159. if ( !previous )
  160. {
  161. return FirstChild();
  162. }
  163. else
  164. {
  165. assert( previous->parent == this );
  166. return previous->NextSibling();
  167. }
  168. }
  169. TiXmlNode* TiXmlNode::IterateChildren( const std::string& val, TiXmlNode* previous )
  170. {
  171. if ( !previous )
  172. {
  173. return FirstChild( val );
  174. }
  175. else
  176. {
  177. assert( previous->parent == this );
  178. return previous->NextSibling( val );
  179. }
  180. }
  181. TiXmlNode* TiXmlNode::NextSibling( const std::string& value ) const
  182. {
  183. TiXmlNode* node;
  184. for ( node = next; node; node = node->next )
  185. {
  186. if ( node->Value() == value )
  187. return node;
  188. }
  189. return 0;
  190. }
  191. TiXmlNode* TiXmlNode::PreviousSibling( const std::string& value ) const
  192. {
  193. TiXmlNode* node;
  194. for ( node = prev; node; node = node->prev )
  195. {
  196. if ( node->Value() == value )
  197. return node;
  198. }
  199. return 0;
  200. }
  201. void TiXmlElement::RemoveAttribute( const std::string& name )
  202. {
  203. TiXmlAttribute* node = attributeSet.Find( name );
  204. if ( node )
  205. {
  206. attributeSet.Remove( node );
  207. delete node;
  208. }
  209. }
  210. TiXmlElement* TiXmlNode::FirstChildElement() const
  211. {
  212. TiXmlNode* node;
  213. for ( node = FirstChild();
  214. node;
  215. node = node->NextSibling() )
  216. {
  217. if ( node->ToElement() )
  218. return node->ToElement();
  219. }
  220. return 0;
  221. }
  222. TiXmlElement* TiXmlNode::FirstChildElement( const std::string& value ) const
  223. {
  224. TiXmlNode* node;
  225. for ( node = FirstChild( value );
  226. node;
  227. node = node->NextSibling( value ) )
  228. {
  229. if ( node->ToElement() )
  230. return node->ToElement();
  231. }
  232. return 0;
  233. }
  234. TiXmlElement* TiXmlNode::NextSiblingElement() const
  235. {
  236. TiXmlNode* node;
  237. for ( node = NextSibling();
  238. node;
  239. node = node->NextSibling() )
  240. {
  241. if ( node->ToElement() )
  242. return node->ToElement();
  243. }
  244. return 0;
  245. }
  246. TiXmlElement* TiXmlNode::NextSiblingElement( const std::string& value ) const
  247. {
  248. TiXmlNode* node;
  249. for ( node = NextSibling( value );
  250. node;
  251. node = node->NextSibling( value ) )
  252. {
  253. if ( node->ToElement() )
  254. return node->ToElement();
  255. }
  256. return 0;
  257. }
  258. TiXmlDocument* TiXmlNode::GetDocument() const
  259. {
  260. const TiXmlNode* node;
  261. for( node = this; node; node = node->parent )
  262. {
  263. if ( node->ToDocument() )
  264. return node->ToDocument();
  265. }
  266. return 0;
  267. }
  268. // TiXmlElement::TiXmlElement()
  269. // : TiXmlNode( TiXmlNode::ELEMENT )
  270. // {
  271. // }
  272. TiXmlElement::TiXmlElement( const std::string& _value )
  273. : TiXmlNode( TiXmlNode::ELEMENT )
  274. {
  275. firstChild = lastChild = 0;
  276. value = _value;
  277. }
  278. TiXmlElement::~TiXmlElement()
  279. {
  280. while( attributeSet.First() )
  281. {
  282. TiXmlAttribute* node = attributeSet.First();
  283. attributeSet.Remove( node );
  284. delete node;
  285. }
  286. }
  287. const std::string* TiXmlElement::Attribute( const std::string& name ) const
  288. {
  289. TiXmlAttribute* node = attributeSet.Find( name );
  290. if ( node )
  291. return &(node->Value() );
  292. return 0;
  293. }
  294. const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) const
  295. {
  296. const std::string* s = Attribute( name );
  297. if ( s )
  298. *i = atoi( s->c_str() );
  299. else
  300. *i = 0;
  301. return s;
  302. }
  303. void TiXmlElement::SetAttribute( const std::string& name, int val )
  304. {
  305. char buf[64];
  306. sprintf( buf, "%d", val );
  307. std::string v = buf;
  308. SetAttribute( name, v );
  309. }
  310. void TiXmlElement::SetAttribute( const std::string& name, const std::string& value )
  311. {
  312. TiXmlAttribute* node = attributeSet.Find( name );
  313. if ( node )
  314. {
  315. node->SetValue( value );
  316. return;
  317. }
  318. TiXmlAttribute* attrib = new TiXmlAttribute( name, value );
  319. if ( attrib )
  320. {
  321. attributeSet.Add( attrib );
  322. }
  323. else
  324. {
  325. TiXmlDocument* document = GetDocument();
  326. if ( document ) document->SetError( TIXML_ERROR_OUT_OF_MEMORY );
  327. }
  328. }
  329. void TiXmlElement::Print( FILE* fp, int depth )
  330. {
  331. int i;
  332. for ( i=0; i<depth; i++ )
  333. fprintf( fp, " " );
  334. fprintf( fp, "<%s", value.c_str() );
  335. TiXmlAttribute* attrib;
  336. for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() )
  337. {
  338. fprintf( fp, " " );
  339. attrib->Print( fp, 0 );
  340. }
  341. // If this node has children, give it a closing tag. Else
  342. // make it an empty tag.
  343. TiXmlNode* node;
  344. if ( firstChild )
  345. {
  346. fprintf( fp, ">" );
  347. for ( node = firstChild; node; node=node->NextSibling() )
  348. {
  349. if ( !node->ToText() )
  350. fprintf( fp, "\n" );
  351. node->Print( fp, depth+1 );
  352. }
  353. fprintf( fp, "\n" );
  354. for ( i=0; i<depth; i++ )
  355. fprintf( fp, " " );
  356. fprintf( fp, "</%s>", value.c_str() );
  357. }
  358. else
  359. {
  360. fprintf( fp, " />" );
  361. }
  362. }
  363. TiXmlNode* TiXmlElement::Clone() const
  364. {
  365. TiXmlElement* clone = new TiXmlElement( Value() );
  366. if ( !clone )
  367. return 0;
  368. CopyToClone( clone );
  369. // Clone the attributes, then clone the children.
  370. TiXmlAttribute* attribute = 0;
  371. for( attribute = attributeSet.First();
  372. attribute;
  373. attribute = attribute->Next() )
  374. {
  375. clone->SetAttribute( attribute->Name(), attribute->Value() );
  376. }
  377. TiXmlNode* node = 0;
  378. for ( node = firstChild; node; node = node->NextSibling() )
  379. {
  380. clone->LinkEndChild( node->Clone() );
  381. }
  382. return clone;
  383. }
  384. TiXmlDocument::TiXmlDocument() : TiXmlNode( TiXmlNode::DOCUMENT )
  385. {
  386. error = false;
  387. // factory = new TiXmlFactory();
  388. }
  389. TiXmlDocument::TiXmlDocument( const std::string& documentName ) : TiXmlNode( TiXmlNode::DOCUMENT )
  390. {
  391. // factory = new TiXmlFactory();
  392. value = documentName;
  393. error = false;
  394. }
  395. // void TiXmlDocument::SetFactory( TiXmlFactory* f )
  396. // {
  397. // delete factory;
  398. // factory = f;
  399. // }
  400. bool TiXmlDocument::LoadFile()
  401. {
  402. return LoadFile( value );
  403. }
  404. bool TiXmlDocument::SaveFile()
  405. {
  406. return SaveFile( value );
  407. }
  408. bool TiXmlDocument::LoadFile( FILE* fp )
  409. {
  410. // Delete the existing data:
  411. Clear();
  412. unsigned size, first;
  413. first = ftell( fp );
  414. fseek( fp, 0, SEEK_END );
  415. size = ftell( fp ) - first + 1;
  416. fseek( fp, first, SEEK_SET );
  417. char* buf = new char[size];
  418. char* p = buf;
  419. while( fgets( p, size, fp ) )
  420. {
  421. p = strchr( p, 0 );
  422. }
  423. fclose( fp );
  424. Parse( buf );
  425. delete [] buf;
  426. if ( !Error() )
  427. return true;
  428. return false;
  429. }
  430. bool TiXmlDocument::LoadFile( const std::string& filename )
  431. {
  432. // Delete the existing data:
  433. Clear();
  434. // Load the new data:
  435. FILE* fp = fopen( filename.c_str(), "r" );
  436. if ( fp )
  437. {
  438. return LoadFile(fp);
  439. }
  440. else
  441. {
  442. SetError( TIXML_ERROR_OPENING_FILE );
  443. }
  444. return false;
  445. }
  446. bool TiXmlDocument::SaveFile( const std::string& filename )
  447. {
  448. FILE* fp = fopen( filename.c_str(), "w" );
  449. if ( fp )
  450. {
  451. Print( fp, 0 );
  452. fclose( fp );
  453. return true;
  454. }
  455. return false;
  456. }
  457. TiXmlNode* TiXmlDocument::Clone() const
  458. {
  459. TiXmlDocument* clone = new TiXmlDocument();
  460. if ( !clone )
  461. return 0;
  462. CopyToClone( clone );
  463. clone->error = error;
  464. clone->errorDesc = errorDesc;
  465. TiXmlNode* node = 0;
  466. for ( node = firstChild; node; node = node->NextSibling() )
  467. {
  468. clone->LinkEndChild( node->Clone() );
  469. }
  470. return clone;
  471. }
  472. void TiXmlDocument::Print( FILE* fp, int )
  473. {
  474. TiXmlNode* node;
  475. for ( node=FirstChild(); node; node=node->NextSibling() )
  476. {
  477. node->Print( fp, 0 );
  478. fprintf( fp, "\n" );
  479. }
  480. }
  481. TiXmlAttribute* TiXmlAttribute::Next()
  482. {
  483. // We are using knowledge of the sentinel. The sentinel
  484. // have a value or name.
  485. if ( next->value.empty() && next->name.empty() )
  486. return 0;
  487. return next;
  488. }
  489. TiXmlAttribute* TiXmlAttribute::Previous()
  490. {
  491. // We are using knowledge of the sentinel. The sentinel
  492. // have a value or name.
  493. if ( prev->value.empty() && prev->name.empty() )
  494. return 0;
  495. return prev;
  496. }
  497. void TiXmlAttribute::Print( FILE* fp, int )
  498. {
  499. if ( value.find( '\"' ) != std::string::npos )
  500. fprintf( fp, "%s='%s'", name.c_str(), value.c_str() );
  501. else
  502. fprintf( fp, "%s=\"%s\"", name.c_str(), value.c_str() );
  503. }
  504. void TiXmlComment::Print( FILE* fp, int depth )
  505. {
  506. for ( int i=0; i<depth; i++ )
  507. fprintf( fp, " " );
  508. fprintf( fp, "<!--%s-->", value.c_str() );
  509. }
  510. TiXmlNode* TiXmlComment::Clone() const
  511. {
  512. TiXmlComment* clone = new TiXmlComment();
  513. if ( !clone )
  514. return 0;
  515. CopyToClone( clone );
  516. return clone;
  517. }
  518. void TiXmlText::Print( FILE* fp, int )
  519. {
  520. fprintf( fp, "%s", value.c_str() );
  521. }
  522. TiXmlNode* TiXmlText::Clone() const
  523. {
  524. TiXmlText* clone = 0;
  525. clone = new TiXmlText();
  526. if ( !clone )
  527. return 0;
  528. CopyToClone( clone );
  529. return clone;
  530. }
  531. TiXmlDeclaration::TiXmlDeclaration( const std::string& _version,
  532. const std::string& _encoding,
  533. const std::string& _standalone )
  534. : TiXmlNode( TiXmlNode::DECLARATION )
  535. {
  536. version = _version;
  537. encoding = _encoding;
  538. standalone = _standalone;
  539. }
  540. void TiXmlDeclaration::Print( FILE* fp, int )
  541. {
  542. std::string out = "<?xml ";
  543. if ( !version.empty() )
  544. {
  545. out += "version=\"";
  546. out += version;
  547. out += "\" ";
  548. }
  549. if ( !encoding.empty() )
  550. {
  551. out += "encoding=\"";
  552. out += encoding;
  553. out += "\" ";
  554. }
  555. if ( !standalone.empty() )
  556. {
  557. out += "standalone=\"";
  558. out += standalone;
  559. out += "\" ";
  560. }
  561. out += "?>";
  562. fprintf( fp, "%s", out.c_str() );
  563. }
  564. TiXmlNode* TiXmlDeclaration::Clone() const
  565. {
  566. TiXmlDeclaration* clone = new TiXmlDeclaration();
  567. if ( !clone )
  568. return 0;
  569. CopyToClone( clone );
  570. clone->version = version;
  571. clone->encoding = encoding;
  572. clone->standalone = standalone;
  573. return clone;
  574. }
  575. void TiXmlUnknown::Print( FILE* fp, int depth )
  576. {
  577. for ( int i=0; i<depth; i++ )
  578. fprintf( fp, " " );
  579. fprintf( fp, "<%s>", value.c_str() );
  580. }
  581. TiXmlNode* TiXmlUnknown::Clone() const
  582. {
  583. TiXmlUnknown* clone = new TiXmlUnknown();
  584. if ( !clone )
  585. return 0;
  586. CopyToClone( clone );
  587. return clone;
  588. }
  589. TiXmlAttributeSet::TiXmlAttributeSet()
  590. {
  591. sentinel.next = &sentinel;
  592. sentinel.prev = &sentinel;
  593. }
  594. TiXmlAttributeSet::~TiXmlAttributeSet()
  595. {
  596. assert( sentinel.next == &sentinel );
  597. assert( sentinel.prev == &sentinel );
  598. }
  599. void TiXmlAttributeSet::Add( TiXmlAttribute* addMe )
  600. {
  601. assert( !Find( addMe->Name() ) ); // Shouldn't be multiply adding to the set.
  602. addMe->next = &sentinel;
  603. addMe->prev = sentinel.prev;
  604. sentinel.prev->next = addMe;
  605. sentinel.prev = addMe;
  606. }
  607. void TiXmlAttributeSet::Remove( TiXmlAttribute* removeMe )
  608. {
  609. TiXmlAttribute* node;
  610. for( node = sentinel.next; node != &sentinel; node = node->next )
  611. {
  612. if ( node == removeMe )
  613. {
  614. node->prev->next = node->next;
  615. node->next->prev = node->prev;
  616. node->next = 0;
  617. node->prev = 0;
  618. return;
  619. }
  620. }
  621. assert( 0 ); // we tried to remove a non-linked attribute.
  622. }
  623. TiXmlAttribute* TiXmlAttributeSet::Find( const std::string& name ) const
  624. {
  625. TiXmlAttribute* node;
  626. for( node = sentinel.next; node != &sentinel; node = node->next )
  627. {
  628. if ( node->Name() == name )
  629. return node;
  630. }
  631. return 0;
  632. }