Newer
Older
monitord / lame-3.97 / ACM / tinyxml / .svn / text-base / tinyxml.h.svn-base
@root root on 23 Jan 2012 25 KB Migration from SVN revision 455
  1. /*
  2. Copyright (c) 2000 Lee Thomason (www.grinninglizard.com)
  3.  
  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.  
  8. Permission is granted to anyone to use this software for any
  9. purpose, including commercial applications, and to alter it and
  10. redistribute it freely, subject to the following restrictions:
  11.  
  12. 1. The origin of this software must not be misrepresented; you must
  13. not claim that you wrote the original software. If you use this
  14. software in a product, an acknowledgment in the product documentation
  15. would be appreciated but is not required.
  16.  
  17. 2. Altered source versions must be plainly marked as such, and
  18. must not be misrepresented as being the original software.
  19.  
  20. 3. This notice may not be removed or altered from any source
  21. distribution.
  22. */
  23.  
  24.  
  25. #ifndef TINYXML_INCLUDED
  26. #define TINYXML_INCLUDED
  27.  
  28. #pragma warning( disable : 4530 )
  29. #pragma warning( disable : 4786 )
  30.  
  31. #include <string>
  32. #include <stdio.h>
  33. #include <assert.h>
  34.  
  35. class TiXmlDocument;
  36. class TiXmlElement;
  37. class TiXmlComment;
  38. class TiXmlUnknown;
  39. class TiXmlAttribute;
  40. class TiXmlText;
  41. class TiXmlDeclaration;
  42.  
  43.  
  44. // Help out windows:
  45. #if defined( _DEBUG ) && !defined( DEBUG )
  46. #define DEBUG
  47. #endif
  48.  
  49. #if defined( DEBUG ) && defined( _MSC_VER )
  50. #include <windows.h>
  51. #define TIXML_LOG OutputDebugString
  52. #else
  53. #define TIXML_LOG printf
  54. #endif
  55.  
  56.  
  57. /** TiXmlBase is a base class for every class in TinyXml.
  58. It does little except to establish that TinyXml classes
  59. can be printed and provide some utility functions.
  60.  
  61. In XML, the document and elements can contain
  62. other elements and other types of nodes.
  63.  
  64. @verbatim
  65. A Document can contain: Element (container or leaf)
  66. Comment (leaf)
  67. Unknown (leaf)
  68. Declaration( leaf )
  69.  
  70. An Element can contain: Element (container or leaf)
  71. Text (leaf)
  72. Attributes (not on tree)
  73. Comment (leaf)
  74. Unknown (leaf)
  75.  
  76. A Decleration contains: Attributes (not on tree)
  77. @endverbatim
  78. */
  79. class TiXmlBase
  80. {
  81. friend class TiXmlNode;
  82. friend class TiXmlElement;
  83. friend class TiXmlDocument;
  84. public:
  85. TiXmlBase() {}
  86. virtual ~TiXmlBase() {}
  87. /** All TinyXml classes can print themselves to a filestream.
  88. This is a formatted print, and will insert tabs and newlines.
  89. (For an unformatted stream, use the << operator.)
  90. */
  91. virtual void Print( FILE* cfile, int depth ) const = 0;
  92.  
  93. // [internal] Underlying implementation of the operator <<
  94. virtual void StreamOut ( std::ostream* out ) const = 0;
  95.  
  96. /** The world does not agree on whether white space should be kept or
  97. not. In order to make everyone happy, these global, static functions
  98. are provided to set whether or not TinyXml will condense all white space
  99. into a single space or not. The default is to condense. Note changing these
  100. values is not thread safe.
  101. */
  102. static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
  103.  
  104. /// Return the current white space setting.
  105. static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
  106.  
  107. protected:
  108. static const char* SkipWhiteSpace( const char* );
  109. static bool StreamWhiteSpace( std::istream* in, std::string* tag );
  110. static bool IsWhiteSpace( int c ) { return ( isspace( c ) || c == '\n' || c == '\r' ); }
  111.  
  112. /* Read to the specified character.
  113. Returns true if the character found and no error.
  114. */
  115. static bool StreamTo( std::istream* in, int character, std::string* tag );
  116.  
  117. /* Reads an XML name into the string provided. Returns
  118. a pointer just past the last character of the name,
  119. or 0 if the function has an error.
  120. */
  121. static const char* ReadName( const char*, std::string* name );
  122.  
  123. /* Reads text. Returns a pointer past the given end tag.
  124. Wickedly complex options, but it keeps the (sensitive) code in one place.
  125. */
  126. static const char* ReadText( const char* in, // where to start
  127. std::string* text, // the string read
  128. bool ignoreWhiteSpace, // whether to keep the white space
  129. const char* endTag, // what ends this text
  130. bool ignoreCase ); // whether to ignore case in the end tag
  131.  
  132. virtual const char* Parse( const char* p ) = 0;
  133.  
  134. // If an entity has been found, transform it into a character.
  135. static const char* GetEntity( const char* in, char* value );
  136.  
  137. // Get a character, while interpreting entities.
  138. inline static const char* GetChar( const char* p, char* value )
  139. {
  140. assert( p );
  141. if ( *p == '&' )
  142. {
  143. return GetEntity( p, value );
  144. }
  145. else
  146. {
  147. *value = *p;
  148. return p+1;
  149. }
  150. }
  151.  
  152. // Puts a string to a stream, expanding entities as it goes.
  153. // Note this should not contian the '<', '>', etc, or they will be transformed into entities!
  154. static void PutString( const std::string& str, std::ostream* stream );
  155.  
  156. // Return true if the next characters in the stream are any of the endTag sequences.
  157. bool static StringEqual( const char* p,
  158. const char* endTag,
  159. bool ignoreCase );
  160.  
  161. enum
  162. {
  163. TIXML_NO_ERROR = 0,
  164. TIXML_ERROR,
  165. TIXML_ERROR_OPENING_FILE,
  166. TIXML_ERROR_OUT_OF_MEMORY,
  167. TIXML_ERROR_PARSING_ELEMENT,
  168. TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
  169. TIXML_ERROR_READING_ELEMENT_VALUE,
  170. TIXML_ERROR_READING_ATTRIBUTES,
  171. TIXML_ERROR_PARSING_EMPTY,
  172. TIXML_ERROR_READING_END_TAG,
  173. TIXML_ERROR_PARSING_UNKNOWN,
  174. TIXML_ERROR_PARSING_COMMENT,
  175. TIXML_ERROR_PARSING_DECLARATION,
  176. TIXML_ERROR_DOCUMENT_EMPTY,
  177.  
  178. TIXML_ERROR_STRING_COUNT
  179. };
  180. static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
  181.  
  182. private:
  183. struct Entity
  184. {
  185. const char* str;
  186. unsigned int strLength;
  187. int chr;
  188. };
  189. enum
  190. {
  191. NUM_ENTITY = 5,
  192. MAX_ENTITY_LENGTH = 6
  193.  
  194. };
  195. static Entity entity[ NUM_ENTITY ];
  196. static bool condenseWhiteSpace;
  197. };
  198.  
  199.  
  200. /** The parent class for everything in the Document Object Model.
  201. (Except for attributes, which are contained in elements.)
  202. Nodes have siblings, a parent, and children. A node can be
  203. in a document, or stand on its own. The type of a TiXmlNode
  204. can be queried, and it can be cast to its more defined type.
  205. */
  206. class TiXmlNode : public TiXmlBase
  207. {
  208. public:
  209.  
  210. /** An output stream operator, for every class. Note that this outputs
  211. without any newlines or formatting, as opposed to Print(), which
  212. includes tabs and new lines.
  213.  
  214. The operator<< and operator>> are not completely symmetric. Writing
  215. a node to a stream is very well defined. You'll get a nice stream
  216. of output, without any extra whitespace or newlines.
  217. But reading is not as well defined. (As it always is.) If you create
  218. a TiXmlElement (for example) and read that from an input stream,
  219. the text needs to define an element or junk will result. This is
  220. true of all input streams, but it's worth keeping in mind.
  221.  
  222. A TiXmlDocument will read nodes until it reads a root element.
  223. */
  224. friend std::ostream& operator<< ( std::ostream& out, const TiXmlNode& base )
  225. {
  226. base.StreamOut( &out );
  227. return out;
  228. }
  229.  
  230. /** An input stream operator, for every class. Tolerant of newlines and
  231. formatting, but doesn't expect them.
  232. */
  233. friend std::istream& operator>> ( std::istream& in, TiXmlNode& base )
  234. {
  235. std::string tag;
  236. tag.reserve( 8 * 1000 );
  237. base.StreamIn( &in, &tag );
  238. base.Parse( tag.c_str() );
  239. return in;
  240. }
  241.  
  242. /** The types of XML nodes supported by TinyXml. (All the
  243. unsupported types are picked up by UNKNOWN.)
  244. */
  245. enum NodeType
  246. {
  247. DOCUMENT,
  248. ELEMENT,
  249. COMMENT,
  250. UNKNOWN,
  251. TEXT,
  252. DECLARATION,
  253. TYPECOUNT
  254. };
  255.  
  256. virtual ~TiXmlNode();
  257.  
  258. /** The meaning of 'value' changes for the specific type of
  259. TiXmlNode.
  260. @verbatim
  261. Document: filename of the xml file
  262. Element: name of the element
  263. Comment: the comment text
  264. Unknown: the tag contents
  265. Text: the text string
  266. @endverbatim
  267.  
  268. The subclasses will wrap this function.
  269. */
  270. const std::string& Value() const { return value; }
  271.  
  272. /** Changes the value of the node. Defined as:
  273. @verbatim
  274. Document: filename of the xml file
  275. Element: name of the element
  276. Comment: the comment text
  277. Unknown: the tag contents
  278. Text: the text string
  279. @endverbatim
  280. */
  281. void SetValue( const std::string& _value ) { value = _value; }
  282.  
  283. /// Delete all the children of this node. Does not affect 'this'.
  284. void Clear();
  285.  
  286. /// One step up the DOM.
  287. TiXmlNode* Parent() const { return parent; }
  288.  
  289. TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children.
  290. TiXmlNode* FirstChild( const std::string& value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found.
  291. TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children.
  292. TiXmlNode* LastChild( const std::string& value ) const; /// The last child of this node matching 'value'. Will be null if there are no children.
  293.  
  294. /** An alternate way to walk the children of a node.
  295. One way to iterate over nodes is:
  296. @verbatim
  297. for( child = parent->FirstChild(); child; child = child->NextSibling() )
  298. @endverbatim
  299.  
  300. IterateChildren does the same thing with the syntax:
  301. @verbatim
  302. child = 0;
  303. while( child = parent->IterateChildren( child ) )
  304. @endverbatim
  305.  
  306. IterateChildren takes the previous child as input and finds
  307. the next one. If the previous child is null, it returns the
  308. first. IterateChildren will return null when done.
  309. */
  310. TiXmlNode* IterateChildren( TiXmlNode* previous ) const;
  311.  
  312. /// This flavor of IterateChildren searches for children with a particular 'value'
  313. TiXmlNode* IterateChildren( const std::string& value, TiXmlNode* previous ) const;
  314. /** Add a new node related to this. Adds a child past the LastChild.
  315. Returns a pointer to the new object or NULL if an error occured.
  316. */
  317. TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
  318.  
  319. /** Add a new node related to this. Adds a child before the specified child.
  320. Returns a pointer to the new object or NULL if an error occured.
  321. */
  322. TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
  323.  
  324. /** Add a new node related to this. Adds a child after the specified child.
  325. Returns a pointer to the new object or NULL if an error occured.
  326. */
  327. TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
  328. /** Replace a child of this node.
  329. Returns a pointer to the new object or NULL if an error occured.
  330. */
  331. TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
  332. /// Delete a child of this node.
  333. bool RemoveChild( TiXmlNode* removeThis );
  334.  
  335. /// Navigate to a sibling node.
  336. TiXmlNode* PreviousSibling() const { return prev; }
  337.  
  338. /// Navigate to a sibling node.
  339. TiXmlNode* PreviousSibling( const std::string& ) const;
  340. /// Navigate to a sibling node.
  341. TiXmlNode* NextSibling() const { return next; }
  342.  
  343. /// Navigate to a sibling node with the given 'value'.
  344. TiXmlNode* NextSibling( const std::string& ) const;
  345.  
  346. /** Convenience function to get through elements.
  347. Calls NextSibling and ToElement. Will skip all non-Element
  348. nodes. Returns 0 if there is not another element.
  349. */
  350. TiXmlElement* NextSiblingElement() const;
  351.  
  352. /** Convenience function to get through elements.
  353. Calls NextSibling and ToElement. Will skip all non-Element
  354. nodes. Returns 0 if there is not another element.
  355. */
  356. TiXmlElement* NextSiblingElement( const std::string& ) const;
  357.  
  358. /// Convenience function to get through elements.
  359. TiXmlElement* FirstChildElement() const;
  360. /// Convenience function to get through elements.
  361. TiXmlElement* FirstChildElement( const std::string& value ) const;
  362.  
  363. /// Query the type (as an enumerated value, above) of this node.
  364. virtual int Type() const { return type; }
  365.  
  366. /** Return a pointer to the Document this node lives in.
  367. Returns null if not in a document.
  368. */
  369. TiXmlDocument* GetDocument() const;
  370.  
  371. /// Returns true if this node has no children.
  372. bool NoChildren() const { return !firstChild; }
  373.  
  374. TiXmlDocument* ToDocument() const { return ( this && type == DOCUMENT ) ? (TiXmlDocument*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  375. TiXmlElement* ToElement() const { return ( this && type == ELEMENT ) ? (TiXmlElement*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  376. TiXmlComment* ToComment() const { return ( this && type == COMMENT ) ? (TiXmlComment*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  377. TiXmlUnknown* ToUnknown() const { return ( this && type == UNKNOWN ) ? (TiXmlUnknown*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  378. TiXmlText* ToText() const { return ( this && type == TEXT ) ? (TiXmlText*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  379. TiXmlDeclaration* ToDeclaration() const { return ( this && type == DECLARATION ) ? (TiXmlDeclaration*) this : 0; } ///< Cast to a more defined type. Will return null not of the requested type.
  380.  
  381. virtual TiXmlNode* Clone() const = 0;
  382.  
  383. // The real work of the input operator.
  384. virtual void StreamIn( std::istream* in, std::string* tag ) = 0;
  385.  
  386. protected:
  387. TiXmlNode( NodeType type );
  388.  
  389. // The node is passed in by ownership. This object will delete it.
  390. TiXmlNode* LinkEndChild( TiXmlNode* addThis );
  391.  
  392. // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
  393. TiXmlNode* Identify( const char* start );
  394.  
  395. void CopyToClone( TiXmlNode* target ) const { target->value = value; }
  396.  
  397. TiXmlNode* parent;
  398. NodeType type;
  399. TiXmlNode* firstChild;
  400. TiXmlNode* lastChild;
  401.  
  402. std::string value;
  403. TiXmlNode* prev;
  404. TiXmlNode* next;
  405. };
  406.  
  407.  
  408. /** An attribute is a name-value pair. Elements have an arbitrary
  409. number of attributes, each with a unique name.
  410.  
  411. @note The attributes are not TiXmlNodes, since they are not
  412. part of the tinyXML document object model. There are other
  413. suggested ways to look at this problem.
  414.  
  415. @note Attributes have a parent
  416. */
  417. class TiXmlAttribute : public TiXmlBase
  418. {
  419. friend class TiXmlAttributeSet;
  420.  
  421. public:
  422. /// Construct an empty attribute.
  423. TiXmlAttribute() : prev( 0 ), next( 0 ) {}
  424.  
  425. /// Construct an attribute with a name and value.
  426. TiXmlAttribute( const std::string& _name, const std::string& _value ) : name( _name ), value( _value ), prev( 0 ), next( 0 ) {}
  427.  
  428. const std::string& Name() const { return name; } ///< Return the name of this attribute.
  429.  
  430. const std::string& Value() const { return value; } ///< Return the value of this attribute.
  431. const int IntValue() const; ///< Return the value of this attribute, converted to an integer.
  432. const double DoubleValue() const; ///< Return the value of this attribute, converted to a double.
  433.  
  434. void SetName( const std::string& _name ) { name = _name; } ///< Set the name of this attribute.
  435. void SetValue( const std::string& _value ) { value = _value; } ///< Set the value.
  436. void SetIntValue( int value ); ///< Set the value from an integer.
  437. void SetDoubleValue( double value ); ///< Set the value from a double.
  438.  
  439. /// Get the next sibling attribute in the DOM. Returns null at end.
  440. TiXmlAttribute* Next() const;
  441. /// Get the previous sibling attribute in the DOM. Returns null at beginning.
  442. TiXmlAttribute* Previous() const;
  443.  
  444. bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
  445. bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
  446. bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
  447.  
  448. /* [internal use]
  449. Attribtue parsing starts: first letter of the name
  450. returns: the next char after the value end quote
  451. */
  452. virtual const char* Parse( const char* p );
  453.  
  454. // [internal use]
  455. virtual void Print( FILE* cfile, int depth ) const;
  456.  
  457. // [internal use]
  458. virtual void StreamOut( std::ostream* out ) const;
  459.  
  460. // [internal use]
  461. // Set the document pointer so the attribute can report errors.
  462. void SetDocument( TiXmlDocument* doc ) { document = doc; }
  463.  
  464. private:
  465. TiXmlDocument* document; // A pointer back to a document, for error reporting.
  466. std::string name;
  467. std::string value;
  468.  
  469. TiXmlAttribute* prev;
  470. TiXmlAttribute* next;
  471. };
  472.  
  473.  
  474. /* A class used to manage a group of attributes.
  475. It is only used internally, both by the ELEMENT and the DECLARATION.
  476. The set can be changed transparent to the Element and Declaration
  477. classes that use it, but NOT transparent to the Attribute
  478. which has to implement a next() and previous() method. Which makes
  479. it a bit problematic and prevents the use of STL.
  480.  
  481. This version is implemented with circular lists because:
  482. - I like circular lists
  483. - it demonstrates some independence from the (typical) doubly linked list.
  484. */
  485. class TiXmlAttributeSet
  486. {
  487. public:
  488. TiXmlAttributeSet();
  489. ~TiXmlAttributeSet();
  490.  
  491. void Add( TiXmlAttribute* attribute );
  492. void Remove( TiXmlAttribute* attribute );
  493.  
  494. TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
  495. TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
  496. TiXmlAttribute* Find( const std::string& name ) const;
  497.  
  498. private:
  499. TiXmlAttribute sentinel;
  500. };
  501.  
  502.  
  503. /** The element is a container class. It has a value, the element name,
  504. and can contain other elements, text, comments, and unknowns.
  505. Elements also contain an arbitrary number of attributes.
  506. */
  507. class TiXmlElement : public TiXmlNode
  508. {
  509. public:
  510. /// Construct an element.
  511. TiXmlElement( const std::string& value );
  512.  
  513. virtual ~TiXmlElement();
  514.  
  515. /** Given an attribute name, attribute returns the value
  516. for the attribute of that name, or null if none exists.
  517. */
  518. const std::string* Attribute( const std::string& name ) const;
  519.  
  520. /** Given an attribute name, attribute returns the value
  521. for the attribute of that name, or null if none exists.
  522. */
  523. const std::string* Attribute( const std::string& name, int* i ) const;
  524.  
  525. /** Sets an attribute of name to a given value. The attribute
  526. will be created if it does not exist, or changed if it does.
  527. */
  528. void SetAttribute( const std::string& name,
  529. const std::string& value );
  530.  
  531. /** Sets an attribute of name to a given value. The attribute
  532. will be created if it does not exist, or changed if it does.
  533. */
  534. void SetAttribute( const std::string& name,
  535. int value );
  536.  
  537. /** Deletes an attribute with the given name.
  538. */
  539. void RemoveAttribute( const std::string& name );
  540.  
  541. TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
  542. TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); } ///< Access the last attribute in this element.
  543.  
  544. // [internal use] Creates a new Element and returs it.
  545. virtual TiXmlNode* Clone() const;
  546. // [internal use]
  547. virtual void Print( FILE* cfile, int depth ) const;
  548. // [internal use]
  549. virtual void StreamOut ( std::ostream* out ) const;
  550. // [internal use]
  551. virtual void StreamIn( std::istream* in, std::string* tag );
  552.  
  553. protected:
  554. /* [internal use]
  555. Attribtue parsing starts: next char past '<'
  556. returns: next char past '>'
  557. */
  558. virtual const char* Parse( const char* p );
  559.  
  560. /* [internal use]
  561. Reads the "value" of the element -- another element, or text.
  562. This should terminate with the current end tag.
  563. */
  564. const char* ReadValue( const char* in );
  565. bool ReadValue( std::istream* in );
  566.  
  567. private:
  568. TiXmlAttributeSet attributeSet;
  569. };
  570.  
  571.  
  572. /** An XML comment.
  573. */
  574. class TiXmlComment : public TiXmlNode
  575. {
  576. public:
  577. /// Constructs an empty comment.
  578. TiXmlComment() : TiXmlNode( TiXmlNode::COMMENT ) {}
  579. virtual ~TiXmlComment() {}
  580.  
  581. // [internal use] Creates a new Element and returs it.
  582. virtual TiXmlNode* Clone() const;
  583. // [internal use]
  584. virtual void Print( FILE* cfile, int depth ) const;
  585. // [internal use]
  586. virtual void StreamOut ( std::ostream* out ) const;
  587. // [internal use]
  588. virtual void StreamIn( std::istream* in, std::string* tag );
  589.  
  590. protected:
  591. /* [internal use]
  592. Attribtue parsing starts: at the ! of the !--
  593. returns: next char past '>'
  594. */
  595. virtual const char* Parse( const char* p );
  596. };
  597.  
  598.  
  599. /** XML text. Contained in an element.
  600. */
  601. class TiXmlText : public TiXmlNode
  602. {
  603. public:
  604. TiXmlText( const std::string& initValue ) : TiXmlNode( TiXmlNode::TEXT ) { SetValue( initValue ); }
  605. virtual ~TiXmlText() {}
  606.  
  607.  
  608. // [internal use] Creates a new Element and returns it.
  609. virtual TiXmlNode* Clone() const;
  610. // [internal use]
  611. virtual void Print( FILE* cfile, int depth ) const;
  612. // [internal use]
  613. virtual void StreamOut ( std::ostream* out ) const;
  614. // [internal use]
  615. bool Blank() const; // returns true if all white space and new lines
  616. /* [internal use]
  617. Attribtue parsing starts: First char of the text
  618. returns: next char past '>'
  619. */
  620. virtual const char* Parse( const char* p );
  621. // [internal use]
  622. virtual void StreamIn( std::istream* in, std::string* tag );
  623. };
  624.  
  625.  
  626. /** In correct XML the declaration is the first entry in the file.
  627. @verbatim
  628. <?xml version="1.0" standalone="yes"?>
  629. @endverbatim
  630.  
  631. TinyXml will happily read or write files without a declaration,
  632. however. There are 3 possible attributes to the declaration:
  633. version, encoding, and standalone.
  634.  
  635. Note: In this version of the code, the attributes are
  636. handled as special cases, not generic attributes, simply
  637. because there can only be at most 3 and they are always the same.
  638. */
  639. class TiXmlDeclaration : public TiXmlNode
  640. {
  641. public:
  642. /// Construct an empty declaration.
  643. TiXmlDeclaration() : TiXmlNode( TiXmlNode::DECLARATION ) {}
  644.  
  645. /// Construct.
  646. TiXmlDeclaration( const std::string& version,
  647. const std::string& encoding,
  648. const std::string& standalone );
  649.  
  650. virtual ~TiXmlDeclaration() {}
  651.  
  652. /// Version. Will return empty if none was found.
  653. const std::string& Version() const { return version; }
  654. /// Encoding. Will return empty if none was found.
  655. const std::string& Encoding() const { return encoding; }
  656. /// Is this a standalone document?
  657. const std::string& Standalone() const { return standalone; }
  658.  
  659. // [internal use] Creates a new Element and returs it.
  660. virtual TiXmlNode* Clone() const;
  661. // [internal use]
  662. virtual void Print( FILE* cfile, int depth ) const;
  663. // [internal use]
  664. virtual void StreamOut ( std::ostream* out ) const;
  665. // [internal use]
  666. virtual void StreamIn( std::istream* in, std::string* tag );
  667.  
  668. protected:
  669. // [internal use]
  670. // Attribtue parsing starts: next char past '<'
  671. // returns: next char past '>'
  672. virtual const char* Parse( const char* p );
  673.  
  674. private:
  675. std::string version;
  676. std::string encoding;
  677. std::string standalone;
  678. };
  679.  
  680.  
  681. /** Any tag that tinyXml doesn't recognize is save as an
  682. unknown. It is a tag of text, but should not be modified.
  683. It will be written back to the XML, unchanged, when the file
  684. is saved.
  685. */
  686. class TiXmlUnknown : public TiXmlNode
  687. {
  688. public:
  689. TiXmlUnknown() : TiXmlNode( TiXmlNode::UNKNOWN ) {}
  690. virtual ~TiXmlUnknown() {}
  691.  
  692. // [internal use]
  693. virtual TiXmlNode* Clone() const;
  694. // [internal use]
  695. virtual void Print( FILE* cfile, int depth ) const;
  696. // [internal use]
  697. virtual void StreamOut ( std::ostream* out ) const;
  698. // [internal use]
  699. virtual void StreamIn( std::istream* in, std::string* tag );
  700.  
  701. protected:
  702. /* [internal use]
  703. Attribute parsing starts: First char of the text
  704. returns: next char past '>'
  705. */
  706. virtual const char* Parse( const char* p );
  707. };
  708.  
  709.  
  710. /** Always the top level node. A document binds together all the
  711. XML pieces. It can be saved, loaded, and printed to the screen.
  712. The 'value' of a document node is the xml file name.
  713. */
  714. class TiXmlDocument : public TiXmlNode
  715. {
  716. public:
  717. /// Create an empty document, that has no name.
  718. TiXmlDocument();
  719. /// Create a document with a name. The name of the document is also the filename of the xml.
  720. TiXmlDocument( const std::string& documentName );
  721. virtual ~TiXmlDocument() {}
  722.  
  723. /** Load a file using the current document value.
  724. Returns true if successful. Will delete any existing
  725. document data before loading.
  726. */
  727. bool LoadFile();
  728. /// Save a file using the current document value. Returns true if successful.
  729. bool SaveFile() const;
  730. /// Load a file using the given filename. Returns true if successful.
  731. bool LoadFile( const std::string& filename );
  732. /// Save a file using the given filename. Returns true if successful.
  733. bool SaveFile( const std::string& filename ) const;
  734.  
  735. /// Parse the given null terminated block of xml data.
  736. virtual const char* Parse( const char* p );
  737.  
  738. /** Get the root element -- the only top level element -- of the document.
  739. In well formed XML, there should only be one. TinyXml is tolerant of
  740. multiple elements at the document level.
  741. */
  742. TiXmlElement* RootElement() const { return FirstChildElement(); }
  743. /// If, during parsing, a error occurs, Error will be set to true.
  744. bool Error() const { return error; }
  745.  
  746. /// Contains a textual (english) description of the error if one occurs.
  747. const std::string& ErrorDesc() const { return errorDesc; }
  748.  
  749. /** Generally, you probably want the error string ( ErrorDesc() ). But if you
  750. prefer the ErrorId, this function will fetch it.
  751. */
  752. const int ErrorId() const { return errorId; }
  753.  
  754. /// If you have handled the error, it can be reset with this call.
  755. void ClearError() { error = false; errorId = 0; errorDesc = ""; }
  756. /** Dump the document to standard out. */
  757. void Print() const { Print( stdout, 0 ); }
  758.  
  759. // [internal use]
  760. virtual void Print( FILE* cfile, int depth = 0 ) const;
  761. // [internal use]
  762. virtual void StreamOut ( std::ostream* out ) const;
  763. // [internal use]
  764. virtual TiXmlNode* Clone() const;
  765. // [internal use]
  766. void SetError( int err ) { assert( err > 0 && err < TIXML_ERROR_STRING_COUNT );
  767. error = true;
  768. errorId = err;
  769. errorDesc = errorString[ errorId ]; }
  770. // [internal use]
  771. virtual void StreamIn( std::istream* in, std::string* tag );
  772.  
  773. protected:
  774.  
  775. private:
  776. bool error;
  777. int errorId;
  778. std::string errorDesc;
  779. };
  780.  
  781.  
  782.  
  783. #endif
  784.