BsDataStream.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /** Reads data from the buffer and copies it to the specified value. */
  43. template<typename T> DataStream& operator>>(T& val);
  44. /**
  45. * Read the requisite number of bytes from the stream, stopping at the end of the file.
  46. *
  47. * @param[in] buf Pre-allocated buffer to read the data into.
  48. * @param[in] count Number of bytes to read.
  49. * @return Number of bytes actually read.
  50. *
  51. * @note Stream must be created with READ access mode.
  52. */
  53. virtual size_t read(void* buf, size_t count) = 0;
  54. /**
  55. * Write the requisite number of bytes to the stream.
  56. *
  57. * @param[in] buf Buffer containing bytes to write.
  58. * @param[in] count Number of bytes to write.
  59. * @return Number of bytes actually written.
  60. *
  61. * @note Stream must be created with WRITE access mode.
  62. */
  63. virtual size_t write(const void* buf, size_t count) { return 0; }
  64. /**
  65. * Writes the provided narrow string to the steam. String is convered to the required encoding before being written.
  66. *
  67. * @param[in] string String containing narrow characters to write, encoded as UTF8.
  68. * @param[in] encoding Encoding to convert the string to before writing.
  69. */
  70. virtual void writeString(const String& string, StringEncoding encoding = StringEncoding::UTF8);
  71. /**
  72. * Writes the provided wide string to the steam. String is convered to the required encoding before being written.
  73. *
  74. * @param[in] string String containing wide characters to write, encoded as specified by platform for
  75. * wide characters.
  76. * @param[in] encoding Encoding to convert the string to before writing.
  77. */
  78. virtual void writeString(const WString& string, StringEncoding encoding = StringEncoding::UTF16);
  79. /**
  80. * Returns a string containing the entire stream.
  81. *
  82. * @return String data encoded as UTF-8.
  83. *
  84. * @note This is a convenience method for text streams only, allowing you to retrieve a String object containing
  85. * all the data in the stream.
  86. */
  87. virtual String getAsString();
  88. /**
  89. * Returns a wide string containing the entire stream.
  90. *
  91. * @return Wide string encoded as specified by current platform.
  92. *
  93. * @note This is a convenience method for text streams only, allowing you to retrieve a WString object
  94. * containing all the data in the stream.
  95. */
  96. virtual WString getAsWString();
  97. /**
  98. * Skip a defined number of bytes. This can also be a negative value, in which case the file pointer rewinds a
  99. * defined number of bytes.
  100. */
  101. virtual void skip(size_t count) = 0;
  102. /** Repositions the read point to a specified byte. */
  103. virtual void seek(size_t pos) = 0;
  104. /** Returns the current byte offset from beginning. */
  105. virtual size_t tell() const = 0;
  106. /** Returns true if the stream has reached the end. */
  107. virtual bool eof() const = 0;
  108. /** Returns the total size of the data to be read from the stream, or 0 if this is indeterminate for this stream. */
  109. size_t size() const { return mSize; }
  110. /** Close the stream. This makes further operations invalid. */
  111. virtual void close() = 0;
  112. protected:
  113. static const UINT32 StreamTempSize;
  114. String mName;
  115. size_t mSize;
  116. UINT16 mAccess;
  117. };
  118. /** Data stream for handling data from memory. */
  119. class BS_UTILITY_EXPORT MemoryDataStream : public DataStream
  120. {
  121. public:
  122. /**
  123. * Wrap an existing memory chunk in a stream.
  124. *
  125. * @param[in] memory Memory to wrap the data stream around.
  126. * @param[in] size Size of the memory chunk in bytes.
  127. */
  128. MemoryDataStream(void* memory, size_t size);
  129. /**
  130. * Create a stream which pre-buffers the contents of another stream. Data from the other buffer will be entirely
  131. * read and stored in an internal buffer.
  132. *
  133. * @param[in] sourceStream Stream to read data from.
  134. */
  135. MemoryDataStream(DataStream& sourceStream);
  136. /**
  137. * Create a stream which pre-buffers the contents of another stream. Data from the other buffer will be entirely
  138. * read and stored in an internal buffer.
  139. *
  140. * @param[in] sourceStream Stream to read data from.
  141. */
  142. MemoryDataStream(const SPtr<DataStream>& sourceStream);
  143. ~MemoryDataStream();
  144. /** Get a pointer to the start of the memory block this stream holds. */
  145. UINT8* getPtr() const { return mData; }
  146. /** Get a pointer to the current position in the memory block this stream holds. */
  147. UINT8* getCurrentPtr() const { return mPos; }
  148. /** @copydoc DataStream::read */
  149. size_t read(void* buf, size_t count) override;
  150. /** @copydoc DataStream::write */
  151. size_t write(const void* buf, size_t count) override;
  152. /** @copydoc DataStream::skip */
  153. void skip(size_t count) override;
  154. /** @copydoc DataStream::seek */
  155. void seek(size_t pos) override;
  156. /** @copydoc DataStream::tell */
  157. size_t tell() const override;
  158. /** @copydoc DataStream::eof */
  159. bool eof() const override;
  160. /** @copydoc DataStream::close */
  161. void close() override;
  162. protected:
  163. UINT8* mData;
  164. UINT8* mPos;
  165. UINT8* mEnd;
  166. bool mFreeOnClose;
  167. };
  168. /** Data stream for handling data from standard streams. */
  169. class BS_UTILITY_EXPORT FileDataStream : public DataStream
  170. {
  171. public:
  172. /**
  173. * Construct read-only stream from an standard stream.
  174. *
  175. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  176. */
  177. FileDataStream(SPtr<std::ifstream> s, bool freeOnClose = true);
  178. /**
  179. * Construct read-write stream from an standard stream.
  180. *
  181. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  182. */
  183. FileDataStream(SPtr<std::fstream> s, bool freeOnClose = true);
  184. /**
  185. * Construct read-only stream from an standard stream, and tell it the size.
  186. *
  187. * Size parameter allows you to specify the size without requiring us to seek to the end of the stream to find
  188. * the size.
  189. *
  190. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  191. */
  192. FileDataStream(SPtr<std::ifstream> s, size_t size, bool freeOnClose = true);
  193. /**
  194. * Construct read-write stream from an standard stream, and tell it the size.
  195. *
  196. * Size parameter allows you to specify the size without requiring us to seek to the end of the stream to find
  197. * the size.
  198. *
  199. * If @p freeOnClose is true, the STL stream will be freed once the data stream is closed.
  200. */
  201. FileDataStream(SPtr<std::fstream> s, size_t size, bool freeOnClose = true);
  202. ~FileDataStream();
  203. /** @copydoc DataStream::read */
  204. size_t read(void* buf, size_t count) override;
  205. /** @copydoc DataStream::write */
  206. size_t write(const void* buf, size_t count) override;
  207. /** @copydoc DataStream::skip */
  208. void skip(size_t count) override;
  209. /** @copydoc DataStream::seek */
  210. void seek(size_t pos) override;
  211. /** @copydoc DataStream::tell */
  212. size_t tell() const override;
  213. /** @copydoc DataStream::eof */
  214. bool eof() const override;
  215. /** @copydoc DataStream::close */
  216. void close() override;
  217. protected:
  218. SPtr<std::istream> mpInStream;
  219. SPtr<std::ifstream> mpFStreamRO;
  220. SPtr<std::fstream> mpFStream;
  221. bool mFreeOnClose;
  222. void determineAccess();
  223. };
  224. /** @} */
  225. }