BsDataStream.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. #include <istream>
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Filesystem
  9. * @{
  10. */
  11. /** Supported encoding types for strings. */
  12. enum class StringEncoding
  13. {
  14. UTF8 = 1,
  15. UTF16 = 2
  16. };
  17. /**
  18. * General purpose class used for encapsulating the reading and writing of data from and to various sources using a
  19. * common interface.
  20. */
  21. class BS_UTILITY_EXPORT DataStream
  22. {
  23. public:
  24. enum AccessMode
  25. {
  26. READ = 1,
  27. WRITE = 2
  28. };
  29. public:
  30. /** Creates an unnamed stream. */
  31. DataStream(UINT16 accessMode = READ)
  32. :mSize(0), mAccess(accessMode)
  33. { }
  34. /** Creates a named stream. */
  35. DataStream(const String& name, UINT16 accessMode = READ)
  36. :mName(name), mSize(0), mAccess(accessMode) {}
  37. virtual ~DataStream() {}
  38. const String& getName(void) { return mName; }
  39. UINT16 getAccessMode() const { return mAccess; }
  40. virtual bool isReadable() const { return (mAccess & READ) != 0; }
  41. virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
  42. virtual bool isFile() const = 0;
  43. /** Reads data from the buffer and copies it to the specified value. */
  44. template<typename T> DataStream& operator>>(T& val);
  45. /**
  46. * Read the requisite number of bytes from the stream, stopping at the end of the file.
  47. *
  48. * @param[in] buf Pre-allocated buffer to read the data into.
  49. * @param[in] count Number of bytes to read.
  50. * @return Number of bytes actually read.
  51. *
  52. * @note Stream must be created with READ access mode.
  53. */
  54. virtual size_t read(void* buf, size_t count) = 0;
  55. /**
  56. * Write the requisite number of bytes to the stream.
  57. *
  58. * @param[in] buf Buffer containing bytes to write.
  59. * @param[in] count Number of bytes to write.
  60. * @return Number of bytes actually written.
  61. *
  62. * @note Stream must be created with WRITE access mode.
  63. */
  64. virtual size_t write(const void* buf, size_t count) { return 0; }
  65. /**
  66. * Writes the provided narrow string to the steam. String is convered to the required encoding before being written.
  67. *
  68. * @param[in] string String containing narrow characters to write, encoded as UTF8.
  69. * @param[in] encoding Encoding to convert the string to before writing.
  70. */
  71. virtual void writeString(const String& string, StringEncoding encoding = StringEncoding::UTF8);
  72. /**
  73. * Writes the provided wide string to the steam. String is convered to the required encoding before being written.
  74. *
  75. * @param[in] string String containing wide characters to write, encoded as specified by platform for
  76. * wide characters.
  77. * @param[in] encoding Encoding to convert the string to before writing.
  78. */
  79. virtual void writeString(const WString& string, StringEncoding encoding = StringEncoding::UTF16);
  80. /**
  81. * Returns a string containing the entire stream.
  82. *
  83. * @return String data encoded as UTF-8.
  84. *
  85. * @note This is a convenience method for text streams only, allowing you to retrieve a String object containing
  86. * all the data in the stream.
  87. */
  88. virtual String getAsString();
  89. /**
  90. * Returns a wide string containing the entire stream.
  91. *
  92. * @return Wide string encoded as specified by current platform.
  93. *
  94. * @note This is a convenience method for text streams only, allowing you to retrieve a WString object
  95. * containing all the data in the stream.
  96. */
  97. virtual WString getAsWString();
  98. /**
  99. * Skip a defined number of bytes. This can also be a negative value, in which case the file pointer rewinds a
  100. * defined number of bytes.
  101. */
  102. virtual void skip(size_t count) = 0;
  103. /** Repositions the read point to a specified byte. */
  104. virtual void seek(size_t pos) = 0;
  105. /** Returns the current byte offset from beginning. */
  106. virtual size_t tell() const = 0;
  107. /** Returns true if the stream has reached the end. */
  108. virtual bool eof() const = 0;
  109. /** Returns the total size of the data to be read from the stream, or 0 if this is indeterminate for this stream. */
  110. size_t size() const { return mSize; }
  111. /** Close the stream. This makes further operations invalid. */
  112. virtual void close() = 0;
  113. protected:
  114. static const UINT32 StreamTempSize;
  115. String mName;
  116. size_t mSize;
  117. UINT16 mAccess;
  118. };
  119. /** Data stream for handling data from memory. */
  120. class BS_UTILITY_EXPORT MemoryDataStream : public DataStream
  121. {
  122. public:
  123. /**
  124. * Wrap an existing memory chunk in a stream.
  125. *
  126. * @param[in] memory Memory to wrap the data stream around.
  127. * @param[in] size Size of the memory chunk in bytes.
  128. * @param[in] freeOnClose Should the memory buffer be freed when the data stream goes out of scope.
  129. */
  130. MemoryDataStream(void* memory, size_t size, bool freeOnClose = true);
  131. /**
  132. * Create a stream which pre-buffers the contents of another stream. Data from the other buffer will be entirely
  133. * read and stored in an internal buffer.
  134. *
  135. * @param[in] sourceStream Stream to read data from.
  136. */
  137. MemoryDataStream(DataStream& sourceStream);
  138. /**
  139. * Create a stream which pre-buffers the contents of another stream. Data from the other buffer will be entirely
  140. * read and stored in an internal buffer.
  141. *
  142. * @param[in] sourceStream Stream to read data from.
  143. */
  144. MemoryDataStream(const SPtr<DataStream>& sourceStream);
  145. ~MemoryDataStream();
  146. bool isFile() const override { return false; }
  147. /** Get a pointer to the start of the memory block this stream holds. */
  148. UINT8* getPtr() const { return mData; }
  149. /** Get a pointer to the current position in the memory block this stream holds. */
  150. UINT8* getCurrentPtr() const { return mPos; }
  151. /** @copydoc DataStream::read */
  152. size_t read(void* buf, size_t count) override;
  153. /** @copydoc DataStream::write */
  154. size_t write(const void* buf, size_t count) override;
  155. /** @copydoc DataStream::skip */
  156. void skip(size_t count) override;
  157. /** @copydoc DataStream::seek */
  158. void seek(size_t pos) override;
  159. /** @copydoc DataStream::tell */
  160. size_t tell() const override;
  161. /** @copydoc DataStream::eof */
  162. bool eof() const override;
  163. /** @copydoc DataStream::close */
  164. void close() override;
  165. protected:
  166. UINT8* mData;
  167. UINT8* mPos;
  168. UINT8* mEnd;
  169. bool mFreeOnClose;
  170. };
  171. /** Data stream for handling data from standard streams. */
  172. class BS_UTILITY_EXPORT FileDataStream : public DataStream
  173. {
  174. public:
  175. /**
  176. * Construct read-only stream from an standard stream.
  177. *
  178. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  179. */
  180. FileDataStream(SPtr<std::ifstream> s, bool freeOnClose = true);
  181. /**
  182. * Construct read-write stream from an standard stream.
  183. *
  184. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  185. */
  186. FileDataStream(SPtr<std::fstream> s, bool freeOnClose = true);
  187. /**
  188. * Construct read-only stream from an standard stream, and tell it the size.
  189. *
  190. * Size parameter allows you to specify the size without requiring us to seek to the end of the stream to find
  191. * the size.
  192. *
  193. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  194. */
  195. FileDataStream(SPtr<std::ifstream> s, size_t size, bool freeOnClose = true);
  196. /**
  197. * Construct read-write stream from an standard stream, and tell it the size.
  198. *
  199. * Size parameter allows you to specify the size without requiring us to seek to the end of the stream to find
  200. * the size.
  201. *
  202. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  203. */
  204. FileDataStream(SPtr<std::fstream> s, size_t size, bool freeOnClose = true);
  205. ~FileDataStream();
  206. bool isFile() const override { return true; }
  207. /** @copydoc DataStream::read */
  208. size_t read(void* buf, size_t count) override;
  209. /** @copydoc DataStream::write */
  210. size_t write(const void* buf, size_t count) override;
  211. /** @copydoc DataStream::skip */
  212. void skip(size_t count) override;
  213. /** @copydoc DataStream::seek */
  214. void seek(size_t pos) override;
  215. /** @copydoc DataStream::tell */
  216. size_t tell() const override;
  217. /** @copydoc DataStream::eof */
  218. bool eof() const override;
  219. /** @copydoc DataStream::close */
  220. void close() override;
  221. protected:
  222. SPtr<std::istream> mpInStream;
  223. SPtr<std::ifstream> mpFStreamRO;
  224. SPtr<std::fstream> mpFStream;
  225. bool mFreeOnClose;
  226. void determineAccess();
  227. };
  228. /** @} */
  229. }