fileStream.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _FILESTREAM_H_
  23. #define _FILESTREAM_H_
  24. #ifndef _VOLUME_H_
  25. #include "core/volume.h"
  26. #endif
  27. #ifndef _STREAM_H_
  28. #include "core/stream/stream.h"
  29. #endif
  30. class FileStream : public Stream
  31. {
  32. public:
  33. enum : U32
  34. {
  35. BUFFER_SIZE = 8 * 1024, // this can be changed to anything appropriate [in k]
  36. BUFFER_INVALID = 0xffffffff // file offsets must all be less than this
  37. };
  38. typedef char Ch; //!< Character type. Only support char.
  39. public:
  40. FileStream(); // default constructor
  41. virtual ~FileStream(); // destructor
  42. // This function will allocate a new FileStream and open it.
  43. // If it fails to allocate or fails to open, it will return NULL.
  44. // The caller is responsible for deleting the instance.
  45. static FileStream *createAndOpen(const String &inFileName, Torque::FS::File::AccessMode inMode);
  46. // mandatory methods from Stream base class...
  47. virtual bool hasCapability(const Capability i_cap) const;
  48. virtual U32 getPosition() const;
  49. virtual bool setPosition(const U32 i_newPosition);
  50. virtual U32 getStreamSize();
  51. // additional methods needed for a file stream...
  52. virtual bool open(const String &inFileName, Torque::FS::File::AccessMode inMode);
  53. virtual void close();
  54. bool flush();
  55. //rjson compatibility
  56. bool Flush() { return flush(); }
  57. FileStream* clone() const;
  58. protected:
  59. // more mandatory methods from Stream base class...
  60. virtual bool _read(const U32 i_numBytes, void *o_pBuffer);
  61. virtual bool _write(const U32 i_numBytes, const void* i_pBuffer);
  62. void init();
  63. bool fillBuffer(const U32 i_startPosition);
  64. void clearBuffer();
  65. static void calcBlockHead(const U32 i_position, U32 *o_blockHead);
  66. static void calcBlockBounds(const U32 i_position, U32 *o_blockHead, U32 *o_blockTail);
  67. void setStatus();
  68. U32 mStreamCaps; // dependent on access mode
  69. private:
  70. Torque::FS::FileRef mFile; // file being streamed
  71. U8 mBuffer[BUFFER_SIZE];
  72. U32 mBuffHead; // first valid position of buffer (from start-of-file)
  73. U32 mBuffPos; // next read or write will occur here
  74. U32 mBuffTail; // last valid position in buffer (inclusive)
  75. bool mDirty; // whether buffer has been written to
  76. bool mEOF; // whether disk reads have reached the end-of-file
  77. FileStream(const FileStream &i_fileStrm); // disable copy constructor
  78. FileStream& operator=(const FileStream &i_fileStrm); // disable assignment operator
  79. };
  80. #endif // _FILE_STREAM_H