logo

libbulletml

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

tinyxml.h (21110B)


  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. #ifndef TINYXML_INCLUDED
  19. #define TINYXML_INCLUDED
  20. #include <string>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <assert.h>
  25. #include <cstring>
  26. #include <cstdlib>
  27. class TiXmlDocument;
  28. class TiXmlElement;
  29. class TiXmlComment;
  30. class TiXmlUnknown;
  31. class TiXmlAttribute;
  32. class TiXmlText;
  33. class TiXmlDeclaration;
  34. /** TiXmlBase is a base class for every class in TinyXml.
  35. It does little except to establist that TinyXml classes
  36. can be printed and provide some utility functions.
  37. In XML, the document and elements can contain
  38. other elements and other types of nodes.
  39. @verbatim
  40. A Document can contain: Element (container or leaf)
  41. Comment (leaf)
  42. Unknown (leaf)
  43. Declaration( leaf )
  44. An Element can contain: Element (container or leaf)
  45. Text (leaf)
  46. Attributes (not on tree)
  47. Comment (leaf)
  48. Unknown (leaf)
  49. A Decleration contains: Attributes (not on tree)
  50. @endverbatim
  51. */
  52. class TiXmlBase
  53. {
  54. friend class TiXmlNode;
  55. friend class TiXmlElement;
  56. friend class TiXmlDocument;
  57. public:
  58. TiXmlBase() {}
  59. virtual ~TiXmlBase() {}
  60. /* All TinyXml classes can print themselves to a filestream.
  61. */
  62. virtual void Print( FILE* fp, int depth ) = 0;
  63. protected:
  64. /* General parsing helper method. Takes a pointer in,
  65. skips all the white space it finds, and returns a pointer
  66. to the first non-whitespace data.
  67. */
  68. static const char* SkipWhiteSpace( const char* p );
  69. /* Reads an XML name into the string provided. Returns
  70. a pointer just past the last character of the name,
  71. or 0 if the function has an error.
  72. */
  73. static const char* ReadName( const char* p, std::string* name );
  74. /* Reads text. Returns a pointer past the given end tag.
  75. Wickedly complex options, but it keeps the (sensitive)
  76. code in one place.
  77. */
  78. static const char* ReadText( const char* in, // where to start
  79. std::string* text, // the string read
  80. bool ignoreWhiteSpace, // whether to keep the white space
  81. const char* endTag, // what ends this text
  82. bool ignoreCase ); // whether to ignore case in the end tag
  83. enum
  84. {
  85. TIXML_NO_ERROR = 0,
  86. TIXML_ERROR_OPENING_FILE,
  87. TIXML_ERROR_OUT_OF_MEMORY,
  88. TIXML_ERROR_PARSING_ELEMENT,
  89. TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
  90. TIXML_ERROR_READING_ELEMENT_VALUE,
  91. TIXML_ERROR_READING_ATTRIBUTES,
  92. TIXML_ERROR_PARSING_EMPTY,
  93. TIXML_ERROR_READING_END_TAG,
  94. TIXML_ERROR_PARSING_UNKNOWN,
  95. TIXML_ERROR_PARSING_COMMENT,
  96. TIXML_ERROR_PARSING_DECLARATION,
  97. TIXML_ERROR_STRING_COUNT
  98. };
  99. static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
  100. };
  101. /** The parent class for everything in the Document Object Model.
  102. (Except for attributes, which are contained in elements.)
  103. Nodes have siblings, a parent, and children. A node can be
  104. in a document, or stand on its own. The type of a TyXmlNode
  105. can be queried, and it can be cast to its more defined type.
  106. */
  107. class TiXmlNode : public TiXmlBase
  108. {
  109. public:
  110. /** The types of XML nodes supported by TinyXml. (All the
  111. unsupported types are picked up by UNKNOWN.)
  112. */
  113. enum NodeType
  114. {
  115. DOCUMENT, ELEMENT, COMMENT, UNKNOWN, TEXT, DECLARATION, TYPECOUNT
  116. };
  117. virtual ~TiXmlNode();
  118. /** The meaning of 'value' changes for the specific type of
  119. TiXmlNode.
  120. @verbatim
  121. Document: filename of the xml file
  122. Element: name of the element
  123. Comment: the comment text
  124. Unknown: the tag contents
  125. Text: the text string
  126. @endverbatim
  127. The subclasses will wrap this function.
  128. */
  129. const std::string& Value() const { return value; }
  130. /** Changes the value of the node. Defined as:
  131. @verbatim
  132. Document: filename of the xml file
  133. Element: name of the element
  134. Comment: the comment text
  135. Unknown: the tag contents
  136. Text: the text string
  137. @endverbatim
  138. */
  139. void SetValue( const std::string& _value ) { value = _value; }
  140. /// Delete all the children of this node. Does not affect 'this'.
  141. void Clear();
  142. /// One step up the DOM.
  143. TiXmlNode* Parent() const { return parent; }
  144. TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children.
  145. TiXmlNode* FirstChild( const std::string& value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found.
  146. TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children.
  147. TiXmlNode* LastChild( const std::string& value ) const; /// The last child of this node matching 'value'. Will be null if there are no children.
  148. /** An alternate way to walk the children of a node.
  149. One way to iterate over nodes is:
  150. @verbatim
  151. for( child = parent->FirstChild(); child; child = child->NextSibling() )
  152. @endverbatim
  153. IterateChildren does the same thing with the syntax:
  154. @verbatim
  155. child = 0;
  156. while( child = parent->IterateChildren( child ) )
  157. @endverbatim
  158. IterateChildren takes the previous child as input and finds
  159. the next one. If the previous child is null, it returns the
  160. first. IterateChildren will return null when done.
  161. */
  162. TiXmlNode* IterateChildren( TiXmlNode* previous );
  163. /// This flavor of IterateChildren searches for children with a particular 'value'
  164. TiXmlNode* IterateChildren( const std::string& value, TiXmlNode* previous );
  165. /** Add a new node related to this. Adds a child past the LastChild.
  166. Returns a pointer to the new object or NULL if an error occured.
  167. */
  168. TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
  169. /** Add a new node related to this. Adds a child before the specified child.
  170. Returns a pointer to the new object or NULL if an error occured.
  171. */
  172. TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
  173. /** Add a new node related to this. Adds a child after the specified child.
  174. Returns a pointer to the new object or NULL if an error occured.
  175. */
  176. TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
  177. /** Replace a child of this node.
  178. Returns a pointer to the new object or NULL if an error occured.
  179. */
  180. TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
  181. /// Delete a child of this node.
  182. bool RemoveChild( TiXmlNode* removeThis );
  183. /// Navigate to a sibling node.
  184. TiXmlNode* PreviousSibling() const { return prev; }
  185. /// Navigate to a sibling node.
  186. TiXmlNode* PreviousSibling( const std::string& ) const;
  187. /// Navigate to a sibling node.
  188. TiXmlNode* NextSibling() const { return next; }
  189. /// Navigate to a sibling node with the given 'value'.
  190. TiXmlNode* NextSibling( const std::string& ) const;
  191. /** Convenience function to get through elements.
  192. Calls NextSibling and ToElement. Will skip all non-Element
  193. nodes. Returns 0 if there is not another element.
  194. */
  195. TiXmlElement* NextSiblingElement() const;
  196. /** Convenience function to get through elements.
  197. Calls NextSibling and ToElement. Will skip all non-Element
  198. nodes. Returns 0 if there is not another element.
  199. */
  200. TiXmlElement* NextSiblingElement( const std::string& ) const;
  201. /// Convenience function to get through elements.
  202. TiXmlElement* FirstChildElement() const;
  203. /// Convenience function to get through elements.
  204. TiXmlElement* FirstChildElement( const std::string& value ) const;
  205. /// Query the type (as an enumerated value, above) of this node.
  206. virtual int Type() { return type; }
  207. /** Return a pointer to the Document this node lives in.
  208. Returns null if not in a document.
  209. */
  210. TiXmlDocument* GetDocument() const;
  211. TiXmlDocument* ToDocument() const { return ( type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  212. TiXmlElement* ToElement() const { return ( type == ELEMENT ) ? (TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  213. TiXmlComment* ToComment() const { return ( type == COMMENT ) ? (TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  214. TiXmlUnknown* ToUnknown() const { return ( type == UNKNOWN ) ? (TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  215. TiXmlText* ToText() const { return ( type == TEXT ) ? (TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  216. TiXmlDeclaration* ToDeclaration() const { return ( type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  217. virtual TiXmlNode* Clone() const = 0;
  218. protected:
  219. TiXmlNode( NodeType type );
  220. virtual const char* Parse( const char* ) = 0;
  221. // The node is passed in by ownership. This object will delete it.
  222. TiXmlNode* LinkEndChild( TiXmlNode* addThis );
  223. // Figure out what is at *p, and parse it. Return a node if
  224. // successful, and update p.
  225. TiXmlNode* IdentifyAndParse( const char** p );
  226. void CopyToClone( TiXmlNode* target ) const { target->value = value; }
  227. TiXmlNode* parent;
  228. NodeType type;
  229. TiXmlNode* firstChild;
  230. TiXmlNode* lastChild;
  231. std::string value;
  232. TiXmlNode* prev;
  233. TiXmlNode* next;
  234. };
  235. /** An attribute is a name-value pair. Elements have an arbitrary
  236. number of attributes, each with a unique name.
  237. @note The attributes are not TiXmlNodes, since they are not
  238. part of the tinyXML document object model. There are other
  239. suggested ways to look at this problem.
  240. @note Attributes have a parent
  241. */
  242. class TiXmlAttribute : public TiXmlBase
  243. {
  244. friend class TiXmlAttributeSet;
  245. public:
  246. /// Construct an empty attribute.
  247. TiXmlAttribute() : prev( 0 ), next( 0 ) {}
  248. /// Construct an attribute with a name and value.
  249. TiXmlAttribute( const std::string& _name, const std::string& _value ) : name( _name ), value( _value ), prev( 0 ), next( 0 ) {}
  250. const std::string& Name() const { return name; } ///< Return the name of this attribute.
  251. const std::string& Value() const { return value; } ///< Return the value of this attribute.
  252. void SetName( const std::string& _name ) { name = _name; } ///< Set the name of this attribute.
  253. void SetValue( const std::string& _value ) { value = _value; } ///< Set the value.
  254. /// Get the next sibling attribute in the DOM. Returns null at end.
  255. TiXmlAttribute* Next();
  256. /// Get the previous sibling attribute in the DOM. Returns null at beginning.
  257. TiXmlAttribute* Previous();
  258. bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
  259. bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
  260. bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
  261. /* [internal use]
  262. Attribtue parsing starts: first letter of the name
  263. returns: the next char after the value end quote
  264. */
  265. const char* Parse( const char* );
  266. // [internal use]
  267. virtual void Print( FILE* fp, int depth );
  268. // [internal use]
  269. // Set the document pointer so the attribute can report errors.
  270. void SetDocument( TiXmlDocument* doc ) { document = doc; }
  271. private:
  272. TiXmlDocument* document; // A pointer back to a document, for error reporting.
  273. std::string name;
  274. std::string value;
  275. TiXmlAttribute* prev;
  276. TiXmlAttribute* next;
  277. };
  278. /* A class used to manage a group of attributes.
  279. It is only used internally, both by the ELEMENT and the DECLARATION.
  280. The set can be changed transparent to the Element and Declaration
  281. classes that use it, but NOT transparent to the Attribute
  282. which has to implement a next() and previous() method. Which makes
  283. it a bit problematic and prevents the use of STL.
  284. This version is implemented with circular lists because:
  285. - I like circular lists
  286. - it demonstrates some independence from the (typical) doubly linked list.
  287. */
  288. class TiXmlAttributeSet
  289. {
  290. public:
  291. TiXmlAttributeSet();
  292. ~TiXmlAttributeSet();
  293. void Add( TiXmlAttribute* attribute );
  294. void Remove( TiXmlAttribute* attribute );
  295. TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
  296. TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
  297. TiXmlAttribute* Find( const std::string& name ) const;
  298. private:
  299. TiXmlAttribute sentinel;
  300. };
  301. /** The element is a container class. It has a value, the element name,
  302. and can contain other elements, text, comments, and unknowns.
  303. Elements also contain an arbitrary number of attributes.
  304. */
  305. class TiXmlElement : public TiXmlNode
  306. {
  307. public:
  308. /// Construct an element.
  309. TiXmlElement( const std::string& value );
  310. virtual ~TiXmlElement();
  311. /** Given an attribute name, attribute returns the value
  312. for the attribute of that name, or null if none exists.
  313. */
  314. const std::string* Attribute( const std::string& name ) const;
  315. /** Given an attribute name, attribute returns the value
  316. for the attribute of that name, or null if none exists.
  317. */
  318. const std::string* Attribute( const std::string& name, int* i ) const;
  319. /** Sets an attribute of name to a given value. The attribute
  320. will be created if it does not exist, or changed if it does.
  321. */
  322. void SetAttribute( const std::string& name,
  323. const std::string& value );
  324. /** Sets an attribute of name to a given value. The attribute
  325. will be created if it does not exist, or changed if it does.
  326. */
  327. void SetAttribute( const std::string& name,
  328. int value );
  329. /** Deletes an attribute with the given name.
  330. */
  331. void RemoveAttribute( const std::string& name );
  332. TiXmlAttribute* FirstAttribute() { return attributeSet.First(); } ///< Access the first attribute in this element.
  333. TiXmlAttribute* LastAttribute() { return attributeSet.Last(); } ///< Access the last attribute in this element.
  334. // [internal use] Creates a new Element and returs it.
  335. virtual TiXmlNode* Clone() const;
  336. // [internal use]
  337. virtual void Print( FILE* fp, int depth );
  338. protected:
  339. /* [internal use]
  340. Attribtue parsing starts: next char past '<'
  341. returns: next char past '>'
  342. */
  343. virtual const char* Parse( const char* );
  344. const char* ReadValue( const char* p );
  345. private:
  346. TiXmlAttributeSet attributeSet;
  347. };
  348. /** An XML comment.
  349. */
  350. class TiXmlComment : public TiXmlNode
  351. {
  352. public:
  353. /// Constructs an empty comment.
  354. TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
  355. virtual ~TiXmlComment() {}
  356. // [internal use] Creates a new Element and returs it.
  357. virtual TiXmlNode* Clone() const;
  358. // [internal use]
  359. virtual void Print( FILE* fp, int depth );
  360. protected:
  361. /* [internal use]
  362. Attribtue parsing starts: at the ! of the !--
  363. returns: next char past '>'
  364. */
  365. virtual const char* Parse( const char* );
  366. };
  367. /** XML text. Contained in an element.
  368. */
  369. class TiXmlText : public TiXmlNode
  370. {
  371. public:
  372. TiXmlText() : TiXmlNode( TiXmlNode::TEXT ) {}
  373. virtual ~TiXmlText() {}
  374. // [internal use] Creates a new Element and returns it.
  375. virtual TiXmlNode* Clone() const;
  376. // [internal use]
  377. virtual void Print( FILE* fp, int depth );
  378. // [internal use]
  379. bool Blank(); // returns true if all white space and new lines
  380. /* [internal use]
  381. Attribtue parsing starts: First char of the text
  382. returns: next char past '>'
  383. */
  384. virtual const char* Parse( const char* );
  385. };
  386. /** XML Cdata section. Contained in an element.
  387. * Always start with <![CDATA[ and end with ]]>
  388. */
  389. class TiXmlCData : public TiXmlText
  390. {
  391. friend class TiXmlElement;
  392. public:
  393. /// Constructor.
  394. TiXmlCData ()
  395. {}
  396. virtual ~TiXmlCData() {}
  397. protected :
  398. virtual const char* Parse( const char* p );
  399. };
  400. /** In correct XML the declaration is the first entry in the file.
  401. @verbatim
  402. <?xml version="1.0" standalone="yes"?>
  403. @endverbatim
  404. TinyXml will happily read or write files without a declaration,
  405. however. There are 3 possible attributes to the declaration:
  406. version, encoding, and standalone.
  407. Note: In this version of the code, the attributes are
  408. handled as special cases, not generic attributes, simply
  409. because there can only be at most 3 and they are always the same.
  410. */
  411. class TiXmlDeclaration : public TiXmlNode
  412. {
  413. public:
  414. /// Construct an empty declaration.
  415. TiXmlDeclaration() : TiXmlNode( TiXmlNode::DECLARATION ) {}
  416. /// Construct.
  417. TiXmlDeclaration( const std::string& version,
  418. const std::string& encoding,
  419. const std::string& standalone );
  420. virtual ~TiXmlDeclaration() {}
  421. /// Version. Will return empty if none was found.
  422. const std::string& Version() { return version; }
  423. /// Encoding. Will return empty if none was found.
  424. const std::string& Encoding() { return encoding; }
  425. /// Is this a standalone document?
  426. const std::string& Standalone() { return standalone; }
  427. // [internal use] Creates a new Element and returs it.
  428. virtual TiXmlNode* Clone() const;
  429. // [internal use]
  430. virtual void Print( FILE* fp, int depth );
  431. protected:
  432. // [internal use]
  433. // Attribtue parsing starts: next char past '<'
  434. // returns: next char past '>'
  435. virtual const char* Parse( const char* );
  436. private:
  437. std::string version;
  438. std::string encoding;
  439. std::string standalone;
  440. };
  441. /** Any tag that tinyXml doesn't recognize is save as an
  442. unknown. It is a tag of text, but should not be modified.
  443. It will be written back to the XML, unchanged, when the file
  444. is saved.
  445. */
  446. class TiXmlUnknown : public TiXmlNode
  447. {
  448. public:
  449. TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
  450. virtual ~TiXmlUnknown() {}
  451. // [internal use]
  452. virtual TiXmlNode* Clone() const;
  453. // [internal use]
  454. virtual void Print( FILE* fp, int depth );
  455. protected:
  456. /* [internal use]
  457. Attribute parsing starts: First char of the text
  458. returns: next char past '>'
  459. */
  460. virtual const char* Parse( const char* );
  461. };
  462. /** Always the top level node. A document binds together all the
  463. XML pieces. It can be saved, loaded, and printed to the screen.
  464. The 'value' of a document node is the xml file name.
  465. */
  466. class TiXmlDocument : public TiXmlNode
  467. {
  468. public:
  469. /// Create an empty document, that has no name.
  470. TiXmlDocument();
  471. /// Create a document with a name. The name of the document is also the filename of the xml.
  472. TiXmlDocument( const std::string& documentName );
  473. virtual ~TiXmlDocument() {}
  474. /** Load a file using the current document value.
  475. Returns true if successful. Will delete any existing
  476. document data before loading.
  477. */
  478. bool LoadFile();
  479. /// Save a file using the current document value. Returns true if successful.
  480. bool SaveFile();
  481. /// Load a file using the given filename. Returns true if successful.
  482. bool LoadFile( const std::string& filename );
  483. /// Save a file using the given filename. Returns true if successful.
  484. bool SaveFile( const std::string& filename );
  485. /// Load a file using the given file pointer. Returns true if successful.
  486. bool LoadFile(FILE* fp);
  487. /// Parse the given null terminated block of xml data.
  488. const char* Parse( const char* );
  489. /// If, during parsing, a error occurs, Error will be set to true.
  490. bool Error() { return error; }
  491. /// Contains a textual (english) description of the error if one occurs.
  492. const std::string& ErrorDesc() { return errorDesc; }
  493. /// Write the document to a file -- usually invoked by SaveFile.
  494. virtual void Print( FILE* fp, int depth = 0 );
  495. /// Dump the document to standard out.
  496. void Print() { Print( stdout, 0 ); }
  497. // [internal use]
  498. virtual TiXmlNode* Clone() const;
  499. // [internal use]
  500. void SetError( int err ) { assert( err > 0 && err < TIXML_ERROR_STRING_COUNT );
  501. error = true;
  502. errorId = err;
  503. errorDesc = errorString[ errorId ]; }
  504. private:
  505. bool error;
  506. int errorId;
  507. std::string errorDesc;
  508. };
  509. #endif