diff options
Diffstat (limited to 'source/core/slang-stream.h')
| -rw-r--r-- | source/core/slang-stream.h | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/source/core/slang-stream.h b/source/core/slang-stream.h index 730ce70be..e33e1c601 100644 --- a/source/core/slang-stream.h +++ b/source/core/slang-stream.h @@ -6,45 +6,45 @@ namespace Slang { -class IOException : public Exception -{ -public: - IOException() - {} - IOException(const String & message) - : Slang::Exception(message) - { - } -}; - -class EndOfStreamException : public IOException -{ -public: - EndOfStreamException() - {} - EndOfStreamException(const String & message) - : IOException(message) - { - } -}; - enum class SeekOrigin { - Start, End, Current + Start, ///< Seek from the start of the stream + End, ///< Seek from the end of the stream + Current, ///< Seek from the current cursor position }; class Stream : public RefObject { public: virtual ~Stream() {} + /// Get the current 'cursor' position in the stream virtual Int64 getPosition()=0; - virtual void seek(SeekOrigin origin, Int64 offset)=0; - virtual size_t read(void * buffer, size_t length) = 0; - virtual size_t write(const void * buffer, size_t length) = 0; + /// Seek the cursor to a position. How the seek is performed is dependent on the 'origin' and the offset required. + /// NOTE that *any* seek will reset the 'end of stream' status. See 'read' for requirements for 'isEnd' to be reached. + virtual SlangResult seek(SeekOrigin origin, Int64 offset)=0; + /// Read from the current position into buffer. + /// If there are less bytes available than requested only the amount available will be read. outReadBytes holds the actual amount of bytes read. + /// It is valid (and not an error) for read to return 0 bytes read - even if the end of the stream. + /// + /// 'isEnd' only becomes true when a read is performed *past* the end of a stream. + /// If a non zero read is performed from the end then isEnd must be true. + /// + /// Will return an error if there is a reading failure. + virtual SlangResult read(void* buffer, size_t length, size_t& outReadBytes) = 0; + /// Write to the stream from current position + virtual SlangResult write(const void* buffer, size_t length) = 0; + /// True if the of the stream has been hit. The 'read' method has more discussion as to when this can occur. virtual bool isEnd() = 0; + /// Returns true if it's possible to read from the stream. virtual bool canRead() = 0; + /// Returns true when it's possible to write to the stream. virtual bool canWrite() = 0; + /// Close the stream. Once closed no more operations can be performed on the stream. + /// Implies any pending data is flushed. virtual void close() = 0; + + /// Helper function that will also *fail* if the specified amount of bytes aren't read. + SlangResult readExactly(void* buffer, size_t length); }; enum class FileMode @@ -69,9 +69,9 @@ public: typedef Stream Super; virtual Int64 getPosition() SLANG_OVERRIDE { return m_position; } - virtual void seek(SeekOrigin origin, Int64 offset) SLANG_OVERRIDE; - virtual size_t read(void * buffer, size_t length) SLANG_OVERRIDE; - virtual size_t write(const void * buffer, size_t length) SLANG_OVERRIDE { SLANG_UNUSED(buffer); SLANG_UNUSED(length); return 0; } + virtual SlangResult seek(SeekOrigin origin, Int64 offset) SLANG_OVERRIDE; + virtual SlangResult read(void * buffer, size_t length, size_t& outReadByts) SLANG_OVERRIDE; + virtual SlangResult write(const void * buffer, size_t length) SLANG_OVERRIDE { SLANG_UNUSED(buffer); SLANG_UNUSED(length); return SLANG_E_NOT_IMPLEMENTED; } virtual bool isEnd() SLANG_OVERRIDE { return m_atEnd; } virtual bool canRead() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Read)) != 0; } virtual bool canWrite() SLANG_OVERRIDE { return (int(m_access) & int(FileAccess::Write)) != 0; } @@ -120,7 +120,7 @@ class OwnedMemoryStream : public MemoryStreamBase public: typedef MemoryStreamBase Super; - virtual size_t write(const void* buffer, size_t length) SLANG_OVERRIDE; + virtual SlangResult write(const void* buffer, size_t length) SLANG_OVERRIDE; /// Set the contents void setContent(const void* contents, size_t contentsSize) @@ -151,21 +151,24 @@ public: typedef Stream Super; // Stream interface - virtual Int64 getPosition(); - virtual void seek(SeekOrigin origin, Int64 offset); - virtual size_t read(void* buffer, size_t length); - virtual size_t write(const void* buffer, size_t length); - virtual bool canRead(); - virtual bool canWrite(); - virtual void close(); - virtual bool isEnd(); - - FileStream(const String& fileName, FileMode fileMode = FileMode::Open); - FileStream(const String& fileName, FileMode fileMode, FileAccess access, FileShare share); + virtual Int64 getPosition() SLANG_OVERRIDE; + virtual SlangResult seek(SeekOrigin origin, Int64 offset) SLANG_OVERRIDE; + virtual SlangResult read(void* buffer, size_t length, size_t& outReadBytes) SLANG_OVERRIDE; + virtual SlangResult write(const void* buffer, size_t length) SLANG_OVERRIDE; + virtual bool canRead() SLANG_OVERRIDE; + virtual bool canWrite() SLANG_OVERRIDE; + virtual void close() SLANG_OVERRIDE; + virtual bool isEnd() SLANG_OVERRIDE; + + FileStream(); + + SlangResult init(const String& fileName, FileMode fileMode, FileAccess access, FileShare share); + SlangResult init(const String& fileName, FileMode fileMode = FileMode::Open); + ~FileStream(); private: - void _init(const String& fileName, FileMode fileMode, FileAccess access, FileShare share); + SlangResult _init(const String& fileName, FileMode fileMode, FileAccess access, FileShare share); FILE* m_handle; FileAccess m_fileAccess; |
