CmDataStream.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include <istream>
  4. namespace CamelotFramework
  5. {
  6. /**
  7. * @brief General purpose class used for encapsulating the reading and writing of data from
  8. * and to various sources using a common interface.
  9. */
  10. class CM_UTILITY_EXPORT DataStream
  11. {
  12. public:
  13. enum AccessMode
  14. {
  15. READ = 1,
  16. WRITE = 2
  17. };
  18. public:
  19. /// Constructor for creating unnamed streams
  20. DataStream(UINT16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
  21. /// Constructor for creating named streams
  22. DataStream(const String& name, UINT16 accessMode = READ)
  23. : mName(name), mSize(0), mAccess(accessMode) {}
  24. /// Returns the name of the stream, if it has one.
  25. const String& getName(void) { return mName; }
  26. /// Gets the access mode of the stream
  27. UINT16 getAccessMode() const { return mAccess; }
  28. /** Reports whether this stream is readable. */
  29. virtual bool isReadable() const { return (mAccess & READ) != 0; }
  30. /** Reports whether this stream is writeable. */
  31. virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
  32. virtual ~DataStream() {}
  33. // Streaming operators
  34. template<typename T> DataStream& operator>>(T& val);
  35. /** Read the requisite number of bytes from the stream,
  36. stopping at the end of the file.
  37. @param buf Reference to a buffer pointer
  38. @param count Number of bytes to read
  39. @returns The number of bytes read
  40. */
  41. virtual size_t read(void* buf, size_t count) = 0;
  42. /** Write the requisite number of bytes from the stream (only applicable to
  43. streams that are not read-only)
  44. @param buf Pointer to a buffer containing the bytes to write
  45. @param count Number of bytes to write
  46. @returns The number of bytes written
  47. */
  48. virtual size_t write(const void* buf, size_t count)
  49. {
  50. (void)buf;
  51. (void)count;
  52. // default to not supported
  53. return 0;
  54. }
  55. /** Get a single line from the stream.
  56. @remarks
  57. The delimiter character is not included in the data
  58. returned, and it is skipped over so the next read will occur
  59. after it. The buffer contents will include a
  60. terminating character.
  61. @note
  62. If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
  63. otherwise, it'll produce unexpected results.
  64. @param buf Reference to a buffer pointer
  65. @param maxCount The maximum length of data to be read, excluding the terminating character
  66. @param delim The delimiter to stop at
  67. @returns The number of bytes read, excluding the terminating character
  68. */
  69. virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
  70. /** Returns a String containing the next line of data, optionally
  71. trimmed for whitespace.
  72. @remarks
  73. This is a convenience method for text streams only, allowing you to
  74. retrieve a String object containing the next line of data. The data
  75. is read up to the next newline character and the result trimmed if
  76. required.
  77. @note
  78. If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
  79. otherwise, it'll produce unexpected results.
  80. @param
  81. trimAfter If true, the line is trimmed for whitespace (as in
  82. String.trim(true,true))
  83. */
  84. virtual String getLine( bool trimAfter = true );
  85. /** Returns a String containing the entire stream.
  86. @remarks
  87. This is a convenience method for text streams only, allowing you to
  88. retrieve a String object containing all the data in the stream.
  89. */
  90. virtual String getAsString(void);
  91. /** Skip a single line from the stream.
  92. @note
  93. If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
  94. otherwise, it'll produce unexpected results.
  95. @param delim The delimiter(s) to stop at
  96. @returns The number of bytes skipped
  97. */
  98. virtual size_t skipLine(const String& delim = "\n");
  99. /** Skip a defined number of bytes. This can also be a negative value, in which case
  100. the file pointer rewinds a defined number of bytes. */
  101. virtual void skip(long count) = 0;
  102. /** Repositions the read point to a specified byte.
  103. */
  104. virtual void seek( size_t pos ) = 0;
  105. /** Returns the current byte offset from beginning */
  106. virtual size_t tell(void) const = 0;
  107. /** Returns true if the stream has reached the end.
  108. */
  109. virtual bool eof(void) const = 0;
  110. /** Returns the total size of the data to be read from the stream,
  111. or 0 if this is indeterminate for this stream.
  112. */
  113. size_t size(void) const { return mSize; }
  114. /** Close the stream; this makes further operations invalid. */
  115. virtual void close(void) = 0;
  116. protected:
  117. /// The name (e.g. resource name) that can be used to identify the source fot his data (optional)
  118. String mName;
  119. /// Size of the data in the stream (may be 0 if size cannot be determined)
  120. size_t mSize;
  121. /// What type of access is allowed (AccessMode)
  122. UINT16 mAccess;
  123. #define OGRE_STREAM_TEMP_SIZE 128
  124. };
  125. /** Shared pointer to allow data streams to be passed around without
  126. worrying about deallocation
  127. */
  128. typedef std::shared_ptr<DataStream> DataStreamPtr;
  129. /// List of DataStream items
  130. typedef List<DataStreamPtr>::type DataStreamList;
  131. /// Shared pointer to list of DataStream items
  132. typedef std::shared_ptr<DataStreamList> DataStreamListPtr;
  133. /** Common subclass of DataStream for handling data from chunks of memory.
  134. */
  135. class CM_UTILITY_EXPORT MemoryDataStream : public DataStream
  136. {
  137. protected:
  138. /// Pointer to the start of the data area
  139. UINT8* mData;
  140. /// Pointer to the current position in the memory
  141. UINT8* mPos;
  142. /// Pointer to the end of the memory
  143. UINT8* mEnd;
  144. /// Do we delete the memory on close
  145. bool mFreeOnClose;
  146. public:
  147. /** Wrap an existing memory chunk in a stream.
  148. @param pMem Pointer to the existing memory
  149. @param size The size of the memory chunk in bytes
  150. @param freeOnClose If true, the memory associated will be destroyed
  151. when the stream is destroyed. Note: it's important that if you set
  152. this option to true, that you allocated the memory using OGRE_ALLOC_T
  153. with a category of MEMCATEGORY_GENERAL ensure the freeing of memory
  154. matches up.
  155. @param readOnly Whether to make the stream on this memory read-only once created
  156. */
  157. MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
  158. /** Wrap an existing memory chunk in a named stream.
  159. @param name The name to give the stream
  160. @param pMem Pointer to the existing memory
  161. @param size The size of the memory chunk in bytes
  162. @param freeOnClose If true, the memory associated will be destroyed
  163. when the stream is destroyed. Note: it's important that if you set
  164. this option to true, that you allocated the memory using OGRE_ALLOC_T
  165. with a category of MEMCATEGORY_GENERAL ensure the freeing of memory
  166. matches up.
  167. @param readOnly Whether to make the stream on this memory read-only once created
  168. */
  169. MemoryDataStream(const String& name, void* pMem, size_t size,
  170. bool freeOnClose = false, bool readOnly = false);
  171. /** Create a stream which pre-buffers the contents of another stream.
  172. @remarks
  173. This constructor can be used to intentionally read in the entire
  174. contents of another stream, copying them to the internal buffer
  175. and thus making them available in memory as a single unit.
  176. @param sourceStream Another DataStream which will provide the source
  177. of data
  178. @param freeOnClose If true, the memory associated will be destroyed
  179. when the stream is destroyed.
  180. @param readOnly Whether to make the stream on this memory read-only once created
  181. */
  182. MemoryDataStream(DataStream& sourceStream,
  183. bool freeOnClose = true, bool readOnly = false);
  184. /** Create a stream which pre-buffers the contents of another stream.
  185. @remarks
  186. This constructor can be used to intentionally read in the entire
  187. contents of another stream, copying them to the internal buffer
  188. and thus making them available in memory as a single unit.
  189. @param sourceStream Weak reference to another DataStream which will provide the source
  190. of data
  191. @param freeOnClose If true, the memory associated will be destroyed
  192. when the stream is destroyed.
  193. @param readOnly Whether to make the stream on this memory read-only once created
  194. */
  195. MemoryDataStream(DataStreamPtr& sourceStream,
  196. bool freeOnClose = true, bool readOnly = false);
  197. /** Create a named stream which pre-buffers the contents of
  198. another stream.
  199. @remarks
  200. This constructor can be used to intentionally read in the entire
  201. contents of another stream, copying them to the internal buffer
  202. and thus making them available in memory as a single unit.
  203. @param name The name to give the stream
  204. @param sourceStream Another DataStream which will provide the source
  205. of data
  206. @param freeOnClose If true, the memory associated will be destroyed
  207. when the stream is destroyed.
  208. @param readOnly Whether to make the stream on this memory read-only once created
  209. */
  210. MemoryDataStream(const String& name, DataStream& sourceStream,
  211. bool freeOnClose = true, bool readOnly = false);
  212. /** Create a named stream which pre-buffers the contents of
  213. another stream.
  214. @remarks
  215. This constructor can be used to intentionally read in the entire
  216. contents of another stream, copying them to the internal buffer
  217. and thus making them available in memory as a single unit.
  218. @param name The name to give the stream
  219. @param sourceStream Another DataStream which will provide the source
  220. of data
  221. @param freeOnClose If true, the memory associated will be destroyed
  222. when the stream is destroyed.
  223. @param readOnly Whether to make the stream on this memory read-only once created
  224. */
  225. MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
  226. bool freeOnClose = true, bool readOnly = false);
  227. /** Create a stream with a brand new empty memory chunk.
  228. @param size The size of the memory chunk to create in bytes
  229. @param freeOnClose If true, the memory associated will be destroyed
  230. when the stream is destroyed.
  231. @param readOnly Whether to make the stream on this memory read-only once created
  232. */
  233. MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
  234. /** Create a named stream with a brand new empty memory chunk.
  235. @param name The name to give the stream
  236. @param size The size of the memory chunk to create in bytes
  237. @param freeOnClose If true, the memory associated will be destroyed
  238. when the stream is destroyed.
  239. @param readOnly Whether to make the stream on this memory read-only once created
  240. */
  241. MemoryDataStream(const String& name, size_t size,
  242. bool freeOnClose = true, bool readOnly = false);
  243. ~MemoryDataStream();
  244. /** Get a pointer to the start of the memory block this stream holds. */
  245. UINT8* getPtr(void) { return mData; }
  246. /** Get a pointer to the current position in the memory block this stream holds. */
  247. UINT8* getCurrentPtr(void) { return mPos; }
  248. /**
  249. * @copydoc DataStream::read
  250. */
  251. size_t read(void* buf, size_t count);
  252. /**
  253. * @copydoc DataStream::write
  254. */
  255. size_t write(const void* buf, size_t count);
  256. /**
  257. * @copydoc DataStream::readLine
  258. */
  259. size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
  260. /**
  261. * @copydoc DataStream::skipLine
  262. */
  263. size_t skipLine(const String& delim = "\n");
  264. /**
  265. * @copydoc DataStream::skip
  266. */
  267. void skip(long count);
  268. /**
  269. * @copydoc DataStream::seek
  270. */
  271. void seek( size_t pos );
  272. /**
  273. * @copydoc DataStream::tell
  274. */
  275. size_t tell(void) const;
  276. /**
  277. * @copydoc DataStream::eof
  278. */
  279. bool eof(void) const;
  280. /**
  281. * @copydoc DataStream::close
  282. */
  283. void close(void);
  284. /** Sets whether or not to free the encapsulated memory on close. */
  285. void setFreeOnClose(bool free) { mFreeOnClose = free; }
  286. };
  287. /** Shared pointer to allow memory data streams to be passed around without
  288. worrying about deallocation
  289. */
  290. typedef std::shared_ptr<MemoryDataStream> MemoryDataStreamPtr;
  291. /** Common subclass of DataStream for handling data from
  292. std::basic_istream.
  293. */
  294. class CM_UTILITY_EXPORT FileDataStream : public DataStream
  295. {
  296. protected:
  297. /// Reference to source stream (read)
  298. std::shared_ptr<std::istream> mpInStream;
  299. /// Reference to source file stream (read-only)
  300. std::shared_ptr<std::ifstream> mpFStreamRO;
  301. /// Reference to source file stream (read-write)
  302. std::shared_ptr<std::fstream> mpFStream;
  303. bool mFreeOnClose;
  304. void determineAccess();
  305. public:
  306. /** Construct a read-only stream from an STL stream
  307. @param s Pointer to source stream
  308. @param freeOnClose Whether to delete the underlying stream on
  309. destruction of this class
  310. */
  311. FileDataStream(std::shared_ptr<std::ifstream> s,
  312. bool freeOnClose = true);
  313. /** Construct a read-write stream from an STL stream
  314. @param s Pointer to source stream
  315. @param freeOnClose Whether to delete the underlying stream on
  316. destruction of this class
  317. */
  318. FileDataStream(std::shared_ptr<std::fstream> s,
  319. bool freeOnClose = true);
  320. /** Construct named read-only stream from an STL stream
  321. @param name The name to give this stream
  322. @param s Pointer to source stream
  323. @param freeOnClose Whether to delete the underlying stream on
  324. destruction of this class
  325. */
  326. FileDataStream(const String& name,
  327. std::shared_ptr<std::ifstream> s,
  328. bool freeOnClose = true);
  329. /** Construct named read-write stream from an STL stream
  330. @param name The name to give this stream
  331. @param s Pointer to source stream
  332. @param freeOnClose Whether to delete the underlying stream on
  333. destruction of this class
  334. */
  335. FileDataStream(const String& name,
  336. std::shared_ptr<std::fstream> s,
  337. bool freeOnClose = true);
  338. /** Construct named read-only stream from an STL stream, and tell it the size
  339. @remarks
  340. This variant tells the class the size of the stream too, which
  341. means this class does not need to seek to the end of the stream
  342. to determine the size up-front. This can be beneficial if you have
  343. metadata about the contents of the stream already.
  344. @param name The name to give this stream
  345. @param s Pointer to source stream
  346. @param size Size of the stream contents in bytes
  347. @param freeOnClose Whether to delete the underlying stream on
  348. destruction of this class. If you specify 'true' for this you
  349. must ensure that the stream was allocated using OGRE_NEW_T with
  350. MEMCATEGRORY_GENERAL.
  351. */
  352. FileDataStream(const String& name,
  353. std::shared_ptr<std::ifstream> s,
  354. size_t size,
  355. bool freeOnClose = true);
  356. /** Construct named read-write stream from an STL stream, and tell it the size
  357. @remarks
  358. This variant tells the class the size of the stream too, which
  359. means this class does not need to seek to the end of the stream
  360. to determine the size up-front. This can be beneficial if you have
  361. metadata about the contents of the stream already.
  362. @param name The name to give this stream
  363. @param s Pointer to source stream
  364. @param size Size of the stream contents in bytes
  365. @param freeOnClose Whether to delete the underlying stream on
  366. destruction of this class. If you specify 'true' for this you
  367. must ensure that the stream was allocated using OGRE_NEW_T with
  368. MEMCATEGRORY_GENERAL.
  369. */
  370. FileDataStream(const String& name,
  371. std::shared_ptr<std::fstream> s,
  372. size_t size,
  373. bool freeOnClose = true);
  374. ~FileDataStream();
  375. /**
  376. * @copydoc DataStream::read
  377. */
  378. size_t read(void* buf, size_t count);
  379. /**
  380. * @copydoc DataStream::write
  381. */
  382. size_t write(const void* buf, size_t count);
  383. /**
  384. * @copydoc DataStream::readLine
  385. */
  386. size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
  387. /**
  388. * @copydoc DataStream::skip
  389. */
  390. void skip(long count);
  391. /**
  392. * @copydoc DataStream::seek
  393. */
  394. void seek(size_t pos);
  395. /**
  396. * @copydoc DataStream::tell
  397. */
  398. size_t tell() const;
  399. /**
  400. * @copydoc DataStream::eof
  401. */
  402. bool eof() const;
  403. /**
  404. * @copydoc DataStream::close
  405. */
  406. void close();
  407. };
  408. }