CmDataStream.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. #include <istream>
  27. namespace CamelotFramework
  28. {
  29. /** \addtogroup Core
  30. * @{
  31. */
  32. /** \addtogroup Resources
  33. * @{
  34. */
  35. /** General purpose class used for encapsulating the reading and writing of data.
  36. @remarks
  37. This class performs basically the same tasks as std::basic_istream,
  38. except that it does not have any formatting capabilities, and is
  39. designed to be subclassed to receive data from multiple sources,
  40. including libraries which have no compatibility with the STL's
  41. stream interfaces. As such, this is an abstraction of a set of
  42. wrapper classes which pretend to be standard stream classes but
  43. can actually be implemented quite differently.
  44. @par
  45. Generally, if a plugin or application provides an ArchiveFactory,
  46. it should also provide a DataStream subclass which will be used
  47. to stream data out of that Archive implementation, unless it can
  48. use one of the common implementations included.
  49. @note
  50. Ogre makes no guarantees about thread safety, for performance reasons.
  51. If you wish to access stream data asynchronously then you should
  52. organise your own mutexes to avoid race conditions.
  53. */
  54. class CM_UTILITY_EXPORT DataStream
  55. {
  56. public:
  57. enum AccessMode
  58. {
  59. READ = 1,
  60. WRITE = 2
  61. };
  62. protected:
  63. /// The name (e.g. resource name) that can be used to identify the source fot his data (optional)
  64. String mName;
  65. /// Size of the data in the stream (may be 0 if size cannot be determined)
  66. size_t mSize;
  67. /// What type of access is allowed (AccessMode)
  68. UINT16 mAccess;
  69. #define OGRE_STREAM_TEMP_SIZE 128
  70. public:
  71. /// Constructor for creating unnamed streams
  72. DataStream(UINT16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
  73. /// Constructor for creating named streams
  74. DataStream(const String& name, UINT16 accessMode = READ)
  75. : mName(name), mSize(0), mAccess(accessMode) {}
  76. /// Returns the name of the stream, if it has one.
  77. const String& getName(void) { return mName; }
  78. /// Gets the access mode of the stream
  79. UINT16 getAccessMode() const { return mAccess; }
  80. /** Reports whether this stream is readable. */
  81. virtual bool isReadable() const { return (mAccess & READ) != 0; }
  82. /** Reports whether this stream is writeable. */
  83. virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
  84. virtual ~DataStream() {}
  85. // Streaming operators
  86. template<typename T> DataStream& operator>>(T& val);
  87. /** Read the requisite number of bytes from the stream,
  88. stopping at the end of the file.
  89. @param buf Reference to a buffer pointer
  90. @param count Number of bytes to read
  91. @returns The number of bytes read
  92. */
  93. virtual size_t read(void* buf, size_t count) = 0;
  94. /** Write the requisite number of bytes from the stream (only applicable to
  95. streams that are not read-only)
  96. @param buf Pointer to a buffer containing the bytes to write
  97. @param count Number of bytes to write
  98. @returns The number of bytes written
  99. */
  100. virtual size_t write(const void* buf, size_t count)
  101. {
  102. (void)buf;
  103. (void)count;
  104. // default to not supported
  105. return 0;
  106. }
  107. /** Get a single line from the stream.
  108. @remarks
  109. The delimiter character is not included in the data
  110. returned, and it is skipped over so the next read will occur
  111. after it. The buffer contents will include a
  112. terminating character.
  113. @note
  114. If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
  115. otherwise, it'll produce unexpected results.
  116. @param buf Reference to a buffer pointer
  117. @param maxCount The maximum length of data to be read, excluding the terminating character
  118. @param delim The delimiter to stop at
  119. @returns The number of bytes read, excluding the terminating character
  120. */
  121. virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
  122. /** Returns a String containing the next line of data, optionally
  123. trimmed for whitespace.
  124. @remarks
  125. This is a convenience method for text streams only, allowing you to
  126. retrieve a String object containing the next line of data. The data
  127. is read up to the next newline character and the result trimmed if
  128. required.
  129. @note
  130. If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
  131. otherwise, it'll produce unexpected results.
  132. @param
  133. trimAfter If true, the line is trimmed for whitespace (as in
  134. String.trim(true,true))
  135. */
  136. virtual String getLine( bool trimAfter = true );
  137. /** Returns a String containing the entire stream.
  138. @remarks
  139. This is a convenience method for text streams only, allowing you to
  140. retrieve a String object containing all the data in the stream.
  141. */
  142. virtual String getAsString(void);
  143. /** Skip a single line from the stream.
  144. @note
  145. If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
  146. otherwise, it'll produce unexpected results.
  147. @param delim The delimiter(s) to stop at
  148. @returns The number of bytes skipped
  149. */
  150. virtual size_t skipLine(const String& delim = "\n");
  151. /** Skip a defined number of bytes. This can also be a negative value, in which case
  152. the file pointer rewinds a defined number of bytes. */
  153. virtual void skip(long count) = 0;
  154. /** Repositions the read point to a specified byte.
  155. */
  156. virtual void seek( size_t pos ) = 0;
  157. /** Returns the current byte offset from beginning */
  158. virtual size_t tell(void) const = 0;
  159. /** Returns true if the stream has reached the end.
  160. */
  161. virtual bool eof(void) const = 0;
  162. /** Returns the total size of the data to be read from the stream,
  163. or 0 if this is indeterminate for this stream.
  164. */
  165. size_t size(void) const { return mSize; }
  166. /** Close the stream; this makes further operations invalid. */
  167. virtual void close(void) = 0;
  168. };
  169. /** Shared pointer to allow data streams to be passed around without
  170. worrying about deallocation
  171. */
  172. typedef std::shared_ptr<DataStream> DataStreamPtr;
  173. /// List of DataStream items
  174. typedef List<DataStreamPtr>::type DataStreamList;
  175. /// Shared pointer to list of DataStream items
  176. typedef std::shared_ptr<DataStreamList> DataStreamListPtr;
  177. /** Common subclass of DataStream for handling data from chunks of memory.
  178. */
  179. class CM_UTILITY_EXPORT MemoryDataStream : public DataStream
  180. {
  181. protected:
  182. /// Pointer to the start of the data area
  183. UINT8* mData;
  184. /// Pointer to the current position in the memory
  185. UINT8* mPos;
  186. /// Pointer to the end of the memory
  187. UINT8* mEnd;
  188. /// Do we delete the memory on close
  189. bool mFreeOnClose;
  190. public:
  191. /** Wrap an existing memory chunk in a stream.
  192. @param pMem Pointer to the existing memory
  193. @param size The size of the memory chunk in bytes
  194. @param freeOnClose If true, the memory associated will be destroyed
  195. when the stream is destroyed. Note: it's important that if you set
  196. this option to true, that you allocated the memory using OGRE_ALLOC_T
  197. with a category of MEMCATEGORY_GENERAL ensure the freeing of memory
  198. matches up.
  199. @param readOnly Whether to make the stream on this memory read-only once created
  200. */
  201. MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
  202. /** Wrap an existing memory chunk in a named stream.
  203. @param name The name to give the stream
  204. @param pMem Pointer to the existing memory
  205. @param size The size of the memory chunk in bytes
  206. @param freeOnClose If true, the memory associated will be destroyed
  207. when the stream is destroyed. Note: it's important that if you set
  208. this option to true, that you allocated the memory using OGRE_ALLOC_T
  209. with a category of MEMCATEGORY_GENERAL ensure the freeing of memory
  210. matches up.
  211. @param readOnly Whether to make the stream on this memory read-only once created
  212. */
  213. MemoryDataStream(const String& name, void* pMem, size_t size,
  214. bool freeOnClose = false, bool readOnly = false);
  215. /** Create a stream which pre-buffers the contents of another stream.
  216. @remarks
  217. This constructor can be used to intentionally read in the entire
  218. contents of another stream, copying them to the internal buffer
  219. and thus making them available in memory as a single unit.
  220. @param sourceStream Another DataStream which will provide the source
  221. of data
  222. @param freeOnClose If true, the memory associated will be destroyed
  223. when the stream is destroyed.
  224. @param readOnly Whether to make the stream on this memory read-only once created
  225. */
  226. MemoryDataStream(DataStream& sourceStream,
  227. bool freeOnClose = true, bool readOnly = false);
  228. /** Create a stream which pre-buffers the contents of another stream.
  229. @remarks
  230. This constructor can be used to intentionally read in the entire
  231. contents of another stream, copying them to the internal buffer
  232. and thus making them available in memory as a single unit.
  233. @param sourceStream Weak reference to another DataStream which will provide the source
  234. of data
  235. @param freeOnClose If true, the memory associated will be destroyed
  236. when the stream is destroyed.
  237. @param readOnly Whether to make the stream on this memory read-only once created
  238. */
  239. MemoryDataStream(DataStreamPtr& sourceStream,
  240. bool freeOnClose = true, bool readOnly = false);
  241. /** Create a named stream which pre-buffers the contents of
  242. another stream.
  243. @remarks
  244. This constructor can be used to intentionally read in the entire
  245. contents of another stream, copying them to the internal buffer
  246. and thus making them available in memory as a single unit.
  247. @param name The name to give the stream
  248. @param sourceStream Another DataStream which will provide the source
  249. of data
  250. @param freeOnClose If true, the memory associated will be destroyed
  251. when the stream is destroyed.
  252. @param readOnly Whether to make the stream on this memory read-only once created
  253. */
  254. MemoryDataStream(const String& name, DataStream& sourceStream,
  255. bool freeOnClose = true, bool readOnly = false);
  256. /** Create a named stream which pre-buffers the contents of
  257. another stream.
  258. @remarks
  259. This constructor can be used to intentionally read in the entire
  260. contents of another stream, copying them to the internal buffer
  261. and thus making them available in memory as a single unit.
  262. @param name The name to give the stream
  263. @param sourceStream Another DataStream which will provide the source
  264. of data
  265. @param freeOnClose If true, the memory associated will be destroyed
  266. when the stream is destroyed.
  267. @param readOnly Whether to make the stream on this memory read-only once created
  268. */
  269. MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
  270. bool freeOnClose = true, bool readOnly = false);
  271. /** Create a stream with a brand new empty memory chunk.
  272. @param size The size of the memory chunk to create in bytes
  273. @param freeOnClose If true, the memory associated will be destroyed
  274. when the stream is destroyed.
  275. @param readOnly Whether to make the stream on this memory read-only once created
  276. */
  277. MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
  278. /** Create a named stream with a brand new empty memory chunk.
  279. @param name The name to give the stream
  280. @param size The size of the memory chunk to create in bytes
  281. @param freeOnClose If true, the memory associated will be destroyed
  282. when the stream is destroyed.
  283. @param readOnly Whether to make the stream on this memory read-only once created
  284. */
  285. MemoryDataStream(const String& name, size_t size,
  286. bool freeOnClose = true, bool readOnly = false);
  287. ~MemoryDataStream();
  288. /** Get a pointer to the start of the memory block this stream holds. */
  289. UINT8* getPtr(void) { return mData; }
  290. /** Get a pointer to the current position in the memory block this stream holds. */
  291. UINT8* getCurrentPtr(void) { return mPos; }
  292. /** @copydoc DataStream::read
  293. */
  294. size_t read(void* buf, size_t count);
  295. /** @copydoc DataStream::write
  296. */
  297. size_t write(const void* buf, size_t count);
  298. /** @copydoc DataStream::readLine
  299. */
  300. size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
  301. /** @copydoc DataStream::skipLine
  302. */
  303. size_t skipLine(const String& delim = "\n");
  304. /** @copydoc DataStream::skip
  305. */
  306. void skip(long count);
  307. /** @copydoc DataStream::seek
  308. */
  309. void seek( size_t pos );
  310. /** @copydoc DataStream::tell
  311. */
  312. size_t tell(void) const;
  313. /** @copydoc DataStream::eof
  314. */
  315. bool eof(void) const;
  316. /** @copydoc DataStream::close
  317. */
  318. void close(void);
  319. /** Sets whether or not to free the encapsulated memory on close. */
  320. void setFreeOnClose(bool free) { mFreeOnClose = free; }
  321. };
  322. /** Shared pointer to allow memory data streams to be passed around without
  323. worrying about deallocation
  324. */
  325. typedef std::shared_ptr<MemoryDataStream> MemoryDataStreamPtr;
  326. /** Common subclass of DataStream for handling data from
  327. std::basic_istream.
  328. */
  329. class CM_UTILITY_EXPORT FileDataStream : public DataStream
  330. {
  331. protected:
  332. /// Reference to source stream (read)
  333. std::shared_ptr<std::istream> mpInStream;
  334. /// Reference to source file stream (read-only)
  335. std::shared_ptr<std::ifstream> mpFStreamRO;
  336. /// Reference to source file stream (read-write)
  337. std::shared_ptr<std::fstream> mpFStream;
  338. bool mFreeOnClose;
  339. void determineAccess();
  340. public:
  341. /** Construct a read-only stream from an STL stream
  342. @param s Pointer to source stream
  343. @param freeOnClose Whether to delete the underlying stream on
  344. destruction of this class
  345. */
  346. FileDataStream(std::shared_ptr<std::ifstream> s,
  347. bool freeOnClose = true);
  348. /** Construct a read-write stream from an STL stream
  349. @param s Pointer to source stream
  350. @param freeOnClose Whether to delete the underlying stream on
  351. destruction of this class
  352. */
  353. FileDataStream(std::shared_ptr<std::fstream> s,
  354. bool freeOnClose = true);
  355. /** Construct named read-only stream from an STL stream
  356. @param name The name to give this stream
  357. @param s Pointer to source stream
  358. @param freeOnClose Whether to delete the underlying stream on
  359. destruction of this class
  360. */
  361. FileDataStream(const String& name,
  362. std::shared_ptr<std::ifstream> s,
  363. bool freeOnClose = true);
  364. /** Construct named read-write stream from an STL stream
  365. @param name The name to give this stream
  366. @param s Pointer to source stream
  367. @param freeOnClose Whether to delete the underlying stream on
  368. destruction of this class
  369. */
  370. FileDataStream(const String& name,
  371. std::shared_ptr<std::fstream> s,
  372. bool freeOnClose = true);
  373. /** Construct named read-only stream from an STL stream, and tell it the size
  374. @remarks
  375. This variant tells the class the size of the stream too, which
  376. means this class does not need to seek to the end of the stream
  377. to determine the size up-front. This can be beneficial if you have
  378. metadata about the contents of the stream already.
  379. @param name The name to give this stream
  380. @param s Pointer to source stream
  381. @param size Size of the stream contents in bytes
  382. @param freeOnClose Whether to delete the underlying stream on
  383. destruction of this class. If you specify 'true' for this you
  384. must ensure that the stream was allocated using OGRE_NEW_T with
  385. MEMCATEGRORY_GENERAL.
  386. */
  387. FileDataStream(const String& name,
  388. std::shared_ptr<std::ifstream> s,
  389. size_t size,
  390. bool freeOnClose = true);
  391. /** Construct named read-write stream from an STL stream, and tell it the size
  392. @remarks
  393. This variant tells the class the size of the stream too, which
  394. means this class does not need to seek to the end of the stream
  395. to determine the size up-front. This can be beneficial if you have
  396. metadata about the contents of the stream already.
  397. @param name The name to give this stream
  398. @param s Pointer to source stream
  399. @param size Size of the stream contents in bytes
  400. @param freeOnClose Whether to delete the underlying stream on
  401. destruction of this class. If you specify 'true' for this you
  402. must ensure that the stream was allocated using OGRE_NEW_T with
  403. MEMCATEGRORY_GENERAL.
  404. */
  405. FileDataStream(const String& name,
  406. std::shared_ptr<std::fstream> s,
  407. size_t size,
  408. bool freeOnClose = true);
  409. ~FileDataStream();
  410. /** @copydoc DataStream::read
  411. */
  412. size_t read(void* buf, size_t count);
  413. /** @copydoc DataStream::write
  414. */
  415. size_t write(const void* buf, size_t count);
  416. /** @copydoc DataStream::readLine
  417. */
  418. size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
  419. /** @copydoc DataStream::skip
  420. */
  421. void skip(long count);
  422. /** @copydoc DataStream::seek
  423. */
  424. void seek( size_t pos );
  425. /** @copydoc DataStream::tell
  426. */
  427. size_t tell(void) const;
  428. /** @copydoc DataStream::eof
  429. */
  430. bool eof(void) const;
  431. /** @copydoc DataStream::close
  432. */
  433. void close(void);
  434. };
  435. /** @} */
  436. }