fileStream.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _PLATFORM_FILEIO_H_
  25. #include "platform/platformFileIO.h"
  26. #endif
  27. #ifndef _STREAM_H_
  28. #include "io/stream.h"
  29. #endif
  30. class FileStream : public Stream
  31. {
  32. public:
  33. enum AccessMode
  34. {
  35. Read = File::Read,
  36. Write = File::Write,
  37. ReadWrite = File::ReadWrite,
  38. WriteAppend = File::WriteAppend
  39. };
  40. enum
  41. {
  42. BUFFER_SIZE = 8 * 1024, // this can be changed to anything appropriate
  43. BUFFER_INVALID = 0xffffffff // file offsets must all be less than this
  44. };
  45. protected:
  46. File mFile; // file being streamed
  47. U32 mStreamCaps; // dependent on access mode
  48. U8 mBuffer[BUFFER_SIZE];
  49. U32 mBuffHead; // first valid position of buffer (from start-of-file)
  50. U32 mBuffPos; // next read or write will occur here
  51. U32 mBuffTail; // last valid position in buffer (inclusive)
  52. bool mDirty; // whether buffer has been written to
  53. bool mEOF; // whether disk reads have reached the end-of-file
  54. FileStream(const FileStream &i_fileStrm); // disable copy constructor
  55. FileStream& operator=(const FileStream &i_fileStrm); // disable assignment operator
  56. public:
  57. FileStream(); // default constructor
  58. virtual ~FileStream(); // destructor
  59. // mandatory methods from Stream base class...
  60. virtual bool hasCapability(const Capability i_cap) const;
  61. virtual U32 getPosition() const;
  62. virtual bool setPosition(const U32 i_newPosition);
  63. virtual U32 getStreamSize();
  64. // additional methods needed for a file stream...
  65. virtual bool open(const char *i_pFilename, AccessMode i_openMode);
  66. virtual void close();
  67. bool Flush();
  68. protected:
  69. // more mandatory methods from Stream base class...
  70. virtual bool _read(const U32 i_numBytes, void *o_pBuffer);
  71. virtual bool _write(const U32 i_numBytes, const void* i_pBuffer);
  72. void init();
  73. bool fillBuffer(const U32 i_startPosition);
  74. void clearBuffer();
  75. static void calcBlockHead(const U32 i_position, U32 *o_blockHead);
  76. static void calcBlockBounds(const U32 i_position, U32 *o_blockHead, U32 *o_blockTail);
  77. void setStatus();
  78. };
  79. #endif // _FILE_STREAM_H