diff options
Diffstat (limited to 'include/cru/common')
-rw-r--r-- | include/cru/common/Exception.h | 17 | ||||
-rw-r--r-- | include/cru/common/io/CFileStream.h | 46 | ||||
-rw-r--r-- | include/cru/common/io/Stream.h | 11 | ||||
-rw-r--r-- | include/cru/common/platform/unix/ErrnoException.h | 18 |
4 files changed, 76 insertions, 16 deletions
diff --git a/include/cru/common/Exception.h b/include/cru/common/Exception.h index 365d2064..6f458d35 100644 --- a/include/cru/common/Exception.h +++ b/include/cru/common/Exception.h @@ -29,4 +29,21 @@ class CRU_BASE_API TextEncodeException : public Exception { public: using Exception::Exception; }; + +class ErrnoException : public Exception { + public: + ErrnoException() : ErrnoException(String{}) {} + explicit ErrnoException(const String& message); + ErrnoException(const String& message, int errno_code); + + CRU_DELETE_COPY(ErrnoException) + CRU_DELETE_MOVE(ErrnoException) + + ~ErrnoException() override = default; + + int GetErrnoCode() const { return errno_code_; } + + private: + int errno_code_; +}; } // namespace cru diff --git a/include/cru/common/io/CFileStream.h b/include/cru/common/io/CFileStream.h new file mode 100644 index 00000000..65de2ac7 --- /dev/null +++ b/include/cru/common/io/CFileStream.h @@ -0,0 +1,46 @@ +#pragma once + +#include "Stream.h" + +#include <cstdio> + +namespace cru::io { +class CRU_BASE_API CFileStream : public Stream { + public: + CFileStream(const char* path, const char* mode); + explicit CFileStream(std::FILE* file, bool readable = true, + bool writable = true, bool auto_close = true); + + CRU_DELETE_COPY(CFileStream) + CRU_DELETE_MOVE(CFileStream) + + ~CFileStream() override; + + public: + bool CanSeek() override; + Index Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override; + Index Tell() override; + void Rewind() override; + + bool CanRead() override; + Index Read(std::byte* buffer, Index offset, Index size) override; + + bool CanWrite() override; + Index Write(const std::byte* buffer, Index offset, Index size) override; + + void Flush() override; + + void Close() override; + + std::FILE* GetHandle() const; + + private: + void CheckClosed(); + + private: + std::FILE* file_; + bool readable_; + bool writable_; + bool auto_close_; +}; +} // namespace cru::io diff --git a/include/cru/common/io/Stream.h b/include/cru/common/io/Stream.h index 66be4468..f833b0b9 100644 --- a/include/cru/common/io/Stream.h +++ b/include/cru/common/io/Stream.h @@ -2,12 +2,23 @@ #include "../Base.h" +#include "../Exception.h" #include "../String.h" #include <cstddef> #include <vector> namespace cru::io { +class CRU_BASE_API StreamAlreadyClosedException : public Exception { + public: + using Exception::Exception; + + CRU_DEFAULT_COPY(StreamAlreadyClosedException) + CRU_DEFAULT_MOVE(StreamAlreadyClosedException) + + CRU_DEFAULT_DESTRUCTOR(StreamAlreadyClosedException) +}; + class CRU_BASE_API Stream : public Object { public: enum class SeekOrigin { Current, Begin, End }; diff --git a/include/cru/common/platform/unix/ErrnoException.h b/include/cru/common/platform/unix/ErrnoException.h index 00e1864b..97edcc78 100644 --- a/include/cru/common/platform/unix/ErrnoException.h +++ b/include/cru/common/platform/unix/ErrnoException.h @@ -7,22 +7,8 @@ #include "../Exception.h" namespace cru::platform::unix { -class ErrnoException : public PlatformException { - public: - ErrnoException() : ErrnoException(String{}) {} - explicit ErrnoException(const String& message); - ErrnoException(const String& message, int errno_code); - - CRU_DELETE_COPY(ErrnoException) - CRU_DELETE_MOVE(ErrnoException) - - ~ErrnoException() override = default; - - int GetErrnoCode() const { return errno_code_; } - - private: - int errno_code_; -}; +// Moved to Exception.h +using ErrnoException = cru::ErrnoException; } // namespace cru::platform::unix #endif |