fsTinyXml.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _FSTINYXML_H_
  23. #define _FSTINYXML_H_
  24. #ifndef TINYXML_INCLUDED
  25. #include "tinyxml/tinyxml.h"
  26. #endif
  27. #include "platform/platform.h"
  28. #ifndef _FILESTREAM_H_
  29. #include "core/stream/fileStream.h"
  30. #endif
  31. class fsTiXmlNode
  32. {
  33. public:
  34. virtual void Print( FileStream& stream, int depth ) const = 0;
  35. };
  36. class fsTiXmlDocument : public TiXmlDocument, public fsTiXmlNode
  37. {
  38. public:
  39. bool LoadFile( FileStream &stream, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  40. bool SaveFile( FileStream &stream ) const;
  41. /// Load a file using the given filename. Returns true if successful.
  42. bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  43. /// Save a file using the given filename. Returns true if successful.
  44. bool SaveFile( const char * filename ) const;
  45. virtual void Print( FileStream& stream, int depth ) const;
  46. virtual TiXmlNode* Clone() const
  47. {
  48. TiXmlDocument* clone = new fsTiXmlDocument();
  49. if ( !clone )
  50. return 0;
  51. CopyTo( clone );
  52. return clone;
  53. }
  54. TiXmlNode* Identify( const char* p, TiXmlEncoding encoding );
  55. };
  56. class fsTiXmlAttribute : public TiXmlAttribute, public fsTiXmlNode
  57. {
  58. public:
  59. virtual void Print( FileStream& stream, int depth, TIXML_STRING* str ) const;
  60. virtual void Print( FileStream& stream, int depth) const
  61. {
  62. Print(stream, depth, 0);
  63. }
  64. };
  65. class fsTiXmlDeclaration : public TiXmlDeclaration, public fsTiXmlNode
  66. {
  67. public:
  68. fsTiXmlDeclaration(){};
  69. fsTiXmlDeclaration( const char* _version,
  70. const char* _encoding,
  71. const char* _standalone ) : TiXmlDeclaration(_version, _encoding, _standalone) { }
  72. virtual void Print( FileStream& stream, int depth, TIXML_STRING* str ) const;
  73. virtual void Print( FileStream& stream, int depth) const
  74. {
  75. Print(stream, depth, 0);
  76. }
  77. virtual TiXmlNode* Clone() const
  78. {
  79. fsTiXmlDeclaration* clone = new fsTiXmlDeclaration();
  80. if ( !clone )
  81. return 0;
  82. CopyTo( clone );
  83. return clone;
  84. }
  85. };
  86. class fsTiXmlElement : public TiXmlElement, public fsTiXmlNode
  87. {
  88. public:
  89. fsTiXmlElement(const char* in_value) : TiXmlElement(in_value) { }
  90. virtual void Print( FileStream& stream, int depth ) const;
  91. virtual TiXmlNode* Clone() const
  92. {
  93. TiXmlElement* clone = new fsTiXmlElement( Value() );
  94. if ( !clone )
  95. return 0;
  96. CopyTo( clone );
  97. return clone;
  98. }
  99. void SetAttribute( const char* name, const char * _value )
  100. {
  101. TiXmlAttribute* attrib = attributeSet.Find( name );
  102. if(!attrib)
  103. {
  104. attrib = new fsTiXmlAttribute();
  105. attributeSet.Add( attrib );
  106. attrib->SetName( name );
  107. }
  108. if ( attrib ) {
  109. attrib->SetValue( _value );
  110. }
  111. }
  112. void SetAttribute( const char * name, int value )
  113. {
  114. TiXmlAttribute* attrib = attributeSet.Find( name );
  115. if(!attrib)
  116. {
  117. attrib = new fsTiXmlAttribute();
  118. attributeSet.Add( attrib );
  119. attrib->SetName( name );
  120. }
  121. if ( attrib ) {
  122. attrib->SetIntValue( value );
  123. }
  124. }
  125. TiXmlNode* Identify( const char* p, TiXmlEncoding encoding );
  126. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  127. };
  128. class fsTiXmlComment : public TiXmlComment, public fsTiXmlNode
  129. {
  130. public:
  131. virtual void Print( FileStream& stream, int depth ) const;
  132. virtual TiXmlNode* Clone() const
  133. {
  134. TiXmlComment* clone = new fsTiXmlComment();
  135. if ( !clone )
  136. return 0;
  137. CopyTo( clone );
  138. return clone;
  139. }
  140. };
  141. class fsTiXmlText : public TiXmlText, public fsTiXmlNode
  142. {
  143. public:
  144. fsTiXmlText (const char * initValue ) : TiXmlText(initValue) { }
  145. virtual void Print( FileStream& stream, int depth ) const;
  146. virtual TiXmlNode* Clone() const
  147. {
  148. TiXmlText* clone = 0;
  149. clone = new fsTiXmlText( "" );
  150. if ( !clone )
  151. return 0;
  152. CopyTo( clone );
  153. return clone;
  154. }
  155. };
  156. class fsTiXmlUnknown : public TiXmlUnknown, public fsTiXmlNode
  157. {
  158. public:
  159. virtual void Print( FileStream& stream, int depth ) const;
  160. virtual TiXmlNode* Clone() const
  161. {
  162. TiXmlUnknown* clone = new fsTiXmlUnknown();
  163. if ( !clone )
  164. return 0;
  165. CopyTo( clone );
  166. return clone;
  167. }
  168. };
  169. static bool AttemptPrintTiNode(class fsTiXmlDocument* node, FileStream& stream, int depth)
  170. {
  171. fsTiXmlDocument* fsDoc = dynamic_cast<fsTiXmlDocument*>(node);
  172. if(fsDoc != NULL)
  173. {
  174. fsDoc->Print(stream, depth);
  175. return true;
  176. }
  177. fsTiXmlUnknown* fsUnk = dynamic_cast<fsTiXmlUnknown*>(node);
  178. if(fsUnk != NULL)
  179. {
  180. fsUnk->Print(stream, depth);
  181. return true;
  182. }
  183. fsTiXmlText* fsTxt = dynamic_cast<fsTiXmlText*>(node);
  184. if(fsTxt != NULL)
  185. {
  186. fsTxt->Print(stream, depth);
  187. return true;
  188. }
  189. fsTiXmlComment* fsCom = dynamic_cast<fsTiXmlComment*>(node);
  190. if(fsCom != NULL)
  191. {
  192. fsCom->Print(stream, depth);
  193. return true;
  194. }
  195. fsTiXmlElement* fsElm = dynamic_cast<fsTiXmlElement*>(node);
  196. if(fsElm != NULL)
  197. {
  198. fsElm->Print(stream, depth);
  199. return true;
  200. }
  201. fsTiXmlDeclaration* fsDec = dynamic_cast<fsTiXmlDeclaration*>(node);
  202. if(fsDec != NULL)
  203. {
  204. fsDec->Print(stream, depth);
  205. return true;
  206. }
  207. fsTiXmlAttribute* fsAtt = dynamic_cast<fsTiXmlAttribute*>(node);
  208. if(fsAtt != NULL)
  209. {
  210. fsAtt->Print(stream, depth);
  211. return true;
  212. }
  213. return false;
  214. }
  215. #endif //_FSTINYXML_H_