fsTinyXml.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/tinyxml2.h"
  26. #endif
  27. #include "platform/platform.h"
  28. #ifndef _FILESTREAM_H_
  29. #include "core/stream/fileStream.h"
  30. #endif
  31. class VfsXMLPrinter : public tinyxml2::XMLPrinter
  32. {
  33. public:
  34. VfsXMLPrinter(FileStream& stream, bool compact = false, int depth = 0);
  35. ~VfsXMLPrinter() override;
  36. // Re-implement protected functionality in TinyXML2 library, and make it public
  37. // (This is a bit dirty, but it's necessary for the PrettyXMLPrinter)
  38. bool CompactMode(const tinyxml2::XMLElement& element) override { return tinyxml2::XMLPrinter::CompactMode(element); }
  39. void PrintSpace(int depth) override { tinyxml2::XMLPrinter::PrintSpace(depth); }
  40. inline void Write(const char* data) { Write(data, strlen(data)); }
  41. // Add VFS friendly implementations of output functions
  42. void Print(const char* format, ...) override;
  43. void Write(const char* data, size_t size) override;
  44. void Putc(char ch) override;
  45. // Accept a virtual FileStream instead of a FILE pointer
  46. FileStream& m_Stream;
  47. };
  48. class VfsXMLDocument : public tinyxml2::XMLDocument
  49. {
  50. public:
  51. bool LoadFile(FileStream& stream);
  52. bool SaveFile(FileStream& stream);
  53. /// Load a file using the given filename. Returns true if successful.
  54. bool LoadFile(const char* filename);
  55. /// Save a file using the given filename. Returns true if successful.
  56. bool SaveFile(const char* filename);
  57. /// Clears the error flags.
  58. void ClearError();
  59. tinyxml2::XMLError _errorID;
  60. mutable tinyxml2::StrPair _errorStr;
  61. int _errorLineNum;
  62. // Create a parallel SetError implementation since it is private in tinyxml2
  63. void SetError(tinyxml2::XMLError error, int lineNum, const char* format, ...);
  64. /// Return true if there was an error parsing the document.
  65. bool Error() const
  66. {
  67. return _errorID != tinyxml2::XML_SUCCESS || XMLDocument::Error();
  68. }
  69. /// Return the errorID.
  70. tinyxml2::XMLError ErrorID() const
  71. {
  72. if (XMLDocument::Error())
  73. {
  74. return XMLDocument::ErrorID();
  75. }
  76. else
  77. {
  78. return _errorID;
  79. }
  80. }
  81. const char* ErrorName() const
  82. {
  83. if (XMLDocument::Error())
  84. {
  85. return XMLDocument::ErrorName();
  86. }
  87. else
  88. {
  89. return ErrorIDToName(_errorID);
  90. }
  91. }
  92. /** Returns a "long form" error description. A hopefully helpful
  93. diagnostic with location, line number, and/or additional info.
  94. */
  95. const char* ErrorStr() const
  96. {
  97. if (XMLDocument::Error())
  98. {
  99. return XMLDocument::ErrorStr();
  100. }
  101. else
  102. {
  103. return _errorStr.Empty() ? "" : _errorStr.GetStr();
  104. }
  105. }
  106. /// Return the line where the error occurred, or zero if unknown.
  107. int ErrorLineNum() const
  108. {
  109. if (XMLDocument::Error())
  110. {
  111. return XMLDocument::ErrorLineNum();
  112. }
  113. else
  114. {
  115. return _errorLineNum;
  116. }
  117. }
  118. };
  119. class PrettyXMLPrinter : public tinyxml2::XMLPrinter
  120. {
  121. // Re-implement private functionality in TinyXML2
  122. static const char LINE_FEED = static_cast<char>(0x0a); // all line endings are normalized to LF
  123. static const char LF = LINE_FEED;
  124. static const char CARRIAGE_RETURN = static_cast<char>(0x0d); // CR gets filtered out
  125. static const char CR = CARRIAGE_RETURN;
  126. static const char SINGLE_QUOTE = '\'';
  127. static const char DOUBLE_QUOTE = '\"';
  128. struct Entity
  129. {
  130. const char* pattern;
  131. int length;
  132. char value;
  133. };
  134. static const int NUM_ENTITIES = 5;
  135. static constexpr Entity entities[NUM_ENTITIES] = {
  136. {"quot", 4, DOUBLE_QUOTE},
  137. {"amp", 3, '&'},
  138. {"apos", 4, SINGLE_QUOTE},
  139. {"lt", 2, '<'},
  140. {"gt", 2, '>'}
  141. };
  142. public:
  143. PrettyXMLPrinter(VfsXMLPrinter& innerPrinter, int depth = 0);
  144. /// Visit a document.
  145. virtual bool VisitEnter(const tinyxml2::XMLDocument& doc)
  146. {
  147. mProcessEntities = doc.ProcessEntities();
  148. return mInnerPrinter.VisitEnter(doc);
  149. }
  150. /// Visit a document.
  151. virtual bool VisitExit(const tinyxml2::XMLDocument& doc)
  152. {
  153. return mInnerPrinter.VisitExit(doc);
  154. }
  155. /// Visit an element.
  156. virtual bool VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute);
  157. /// Visit an element.
  158. virtual bool VisitExit(const tinyxml2::XMLElement& element)
  159. {
  160. mDepth--;
  161. return mInnerPrinter.VisitExit(element);
  162. }
  163. /// Visit a declaration.
  164. virtual bool Visit(const tinyxml2::XMLDeclaration& declaration)
  165. {
  166. return mInnerPrinter.Visit(declaration);
  167. }
  168. /// Visit a text node.
  169. virtual bool Visit(const tinyxml2::XMLText& text)
  170. {
  171. return mInnerPrinter.Visit(text);
  172. }
  173. /// Visit a comment node.
  174. virtual bool Visit(const tinyxml2::XMLComment& comment)
  175. {
  176. return mInnerPrinter.Visit(comment);
  177. }
  178. /// Visit an unknown node.
  179. virtual bool Visit(const tinyxml2::XMLUnknown& unknown)
  180. {
  181. return mInnerPrinter.Visit(unknown);
  182. }
  183. void PushAttribute(const char* name, const char* value, bool compactMode);
  184. // Re-implement private functionality in TinyXML2 library, this is just a copy-paste job
  185. void PrintString(const char*, bool restrictedEntitySet); // prints out, after detecting entities.
  186. // The inner printer we are wrapping, we only support VfsXMLPrinter based classes because
  187. // stock tinyxml printer is very closed
  188. VfsXMLPrinter& mInnerPrinter;
  189. // Track private fields that are necessary for private functionality in TinyXML2
  190. int mDepth;
  191. bool mProcessEntities;
  192. bool mCompactMode;
  193. enum
  194. {
  195. ENTITY_RANGE = 64,
  196. BUF_SIZE = 200
  197. };
  198. bool mEntityFlag[ENTITY_RANGE];
  199. bool mRestrictedEntityFlag[ENTITY_RANGE];
  200. };
  201. #endif //_FSTINYXML_H_