2
0

BsDataStream.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /**
  112. * Creates a copy of this stream.
  113. *
  114. * @param[in] copyData If true the internal stream data will be copied as well, otherwise it will just
  115. * reference the data from the original stream (in which case the caller must ensure the
  116. * original stream outlives the clone). This is not relevant for file streams.
  117. */
  118. virtual SPtr<DataStream> clone(bool copyData = true) const = 0;
  119. /** Close the stream. This makes further operations invalid. */
  120. virtual void close() = 0;
  121. protected:
  122. static const UINT32 StreamTempSize;
  123. String mName;
  124. size_t mSize;
  125. UINT16 mAccess;
  126. };
  127. /** Data stream for handling data from memory. */
  128. class BS_UTILITY_EXPORT MemoryDataStream : public DataStream
  129. {
  130. public:
  131. /**
  132. * Wrap an existing memory chunk in a stream.
  133. *
  134. * @param[in] memory Memory to wrap the data stream around.
  135. * @param[in] size Size of the memory chunk in bytes.
  136. * @param[in] freeOnClose Should the memory buffer be freed when the data stream goes out of scope.
  137. */
  138. MemoryDataStream(void* memory, size_t size, bool freeOnClose = true);
  139. /**
  140. * Create a stream which pre-buffers the contents of another stream. Data from the other buffer will be entirely
  141. * read and stored in an internal buffer.
  142. *
  143. * @param[in] sourceStream Stream to read data from.
  144. */
  145. MemoryDataStream(DataStream& sourceStream);
  146. /**
  147. * Create a stream which pre-buffers the contents of another stream. Data from the other buffer will be entirely
  148. * read and stored in an internal buffer.
  149. *
  150. * @param[in] sourceStream Stream to read data from.
  151. */
  152. MemoryDataStream(const SPtr<DataStream>& sourceStream);
  153. ~MemoryDataStream();
  154. bool isFile() const override { return false; }
  155. /** Get a pointer to the start of the memory block this stream holds. */
  156. UINT8* getPtr() const { return mData; }
  157. /** Get a pointer to the current position in the memory block this stream holds. */
  158. UINT8* getCurrentPtr() const { return mPos; }
  159. /** @copydoc DataStream::read */
  160. size_t read(void* buf, size_t count) override;
  161. /** @copydoc DataStream::write */
  162. size_t write(const void* buf, size_t count) override;
  163. /** @copydoc DataStream::skip */
  164. void skip(size_t count) override;
  165. /** @copydoc DataStream::seek */
  166. void seek(size_t pos) override;
  167. /** @copydoc DataStream::tell */
  168. size_t tell() const override;
  169. /** @copydoc DataStream::eof */
  170. bool eof() const override;
  171. /** @copydoc DataStream::clone */
  172. SPtr<DataStream> clone(bool copyData = true) const override;
  173. /** @copydoc DataStream::close */
  174. void close() override;
  175. protected:
  176. UINT8* mData;
  177. UINT8* mPos;
  178. UINT8* mEnd;
  179. bool mFreeOnClose;
  180. };
  181. /** Data stream for handling data from standard streams. */
  182. class BS_UTILITY_EXPORT FileDataStream : public DataStream
  183. {
  184. public:
  185. /**
  186. * Construct a file stream.
  187. *
  188. * @param[in] filePath Path of the file to open.
  189. * @param[in] accessMode Determines should the file be opened in read, write or read/write mode.
  190. * @param[in] freeOnClose Determines should the internal stream be freed once the data stream is closed or goes
  191. * out of scope.
  192. */
  193. FileDataStream(const Path& filePath, AccessMode accessMode = READ, bool freeOnClose = true);
  194. ~FileDataStream();
  195. bool isFile() const override { return true; }
  196. /** @copydoc DataStream::read */
  197. size_t read(void* buf, size_t count) override;
  198. /** @copydoc DataStream::write */
  199. size_t write(const void* buf, size_t count) override;
  200. /** @copydoc DataStream::skip */
  201. void skip(size_t count) override;
  202. /** @copydoc DataStream::seek */
  203. void seek(size_t pos) override;
  204. /** @copydoc DataStream::tell */
  205. size_t tell() const override;
  206. /** @copydoc DataStream::eof */
  207. bool eof() const override;
  208. /** @copydoc DataStream::clone */
  209. SPtr<DataStream> clone(bool copyData = true) const override;
  210. /** @copydoc DataStream::close */
  211. void close() override;
  212. /** Returns the path of the file opened by the stream. */
  213. const Path& getPath() const { return mPath; }
  214. protected:
  215. Path mPath;
  216. SPtr<std::istream> mInStream;
  217. SPtr<std::ifstream> mFStreamRO;
  218. SPtr<std::fstream> mFStream;
  219. bool mFreeOnClose;
  220. };
  221. /** @} */
  222. }