BsDataStream.h 8.0 KB

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