diff options
author | crupest <crupest@outlook.com> | 2023-10-05 22:02:25 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-10-05 22:02:25 +0800 |
commit | 1550828518ff4719db88f35e088207816866a073 (patch) | |
tree | 217e35f48f6c08152bc607d804ad78c0a292e3cd | |
parent | 90feae14e708fbb0b313503a6e76cfbd77f94ce1 (diff) | |
download | cru-1550828518ff4719db88f35e088207816866a073.tar.gz cru-1550828518ff4719db88f35e088207816866a073.tar.bz2 cru-1550828518ff4719db88f35e088207816866a073.zip |
...
-rw-r--r-- | cru-words.txt | 1 | ||||
-rw-r--r-- | include/cru/common/io/CFileStream.h | 2 | ||||
-rw-r--r-- | include/cru/common/io/FileNotExistException.h | 19 | ||||
-rw-r--r-- | include/cru/common/io/FileStream.h | 46 | ||||
-rw-r--r-- | include/cru/common/io/Stream.h | 2 | ||||
-rw-r--r-- | include/cru/common/platform/unix/UnixFileStream.h | 5 | ||||
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/io/CFileStream.cpp | 6 | ||||
-rw-r--r-- | src/common/io/FileNotExistException.cpp | 12 | ||||
-rw-r--r-- | src/common/io/Stream.cpp | 4 | ||||
-rw-r--r-- | src/common/platform/unix/UnixFileStream.cpp | 12 | ||||
-rw-r--r-- | src/ui/ThemeManager.cpp | 1 |
12 files changed, 27 insertions, 84 deletions
diff --git a/cru-words.txt b/cru-words.txt index e69de29b..61ec74fb 100644 --- a/cru-words.txt +++ b/cru-words.txt @@ -0,0 +1 @@ +emscripten diff --git a/include/cru/common/io/CFileStream.h b/include/cru/common/io/CFileStream.h index 65de2ac7..be23ac4a 100644 --- a/include/cru/common/io/CFileStream.h +++ b/include/cru/common/io/CFileStream.h @@ -24,9 +24,11 @@ class CRU_BASE_API CFileStream : public Stream { bool CanRead() override; Index Read(std::byte* buffer, Index offset, Index size) override; + using Stream::Read; bool CanWrite() override; Index Write(const std::byte* buffer, Index offset, Index size) override; + using Stream::Write; void Flush() override; diff --git a/include/cru/common/io/FileNotExistException.h b/include/cru/common/io/FileNotExistException.h deleted file mode 100644 index f49271b1..00000000 --- a/include/cru/common/io/FileNotExistException.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "../Base.h" -#include "../Exception.h" - -namespace cru::io { - class CRU_BASE_API FileNotExistException : public Exception { - public: - FileNotExistException(String path); - - CRU_DEFAULT_COPY(FileNotExistException) - CRU_DEFAULT_MOVE(FileNotExistException) - - ~FileNotExistException() override = default; - - private: - String path_; - }; -} diff --git a/include/cru/common/io/FileStream.h b/include/cru/common/io/FileStream.h deleted file mode 100644 index 4eab612f..00000000 --- a/include/cru/common/io/FileStream.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Here are some notes about FileStream: - * - * 1. FileStream is currently implemented as a typedef of the corresponding - * specific XxxFileStream class implemented on each platform and controlled with - * preprocessor commands. There might be some other ways like proxy pattern but - * I do this way for simplicity. So your duty to implement a new platform is to - * define a new class and ensure it implements all the required interface. And - * in this way you are free to expose any other additional interface like for - * specific platform. - * - * 2. Since each platform defines their own way to open a file, especially the - * flags to open a file, we have to define a common interface. I decide to - * mimic Linux flags so on platforms where there is no direct support on certain - * flags we try our best to simulate it and make a note for users. - * - * (TODO: Currently the problem is that when I implemented for Windows and UNIX - * I didn't take this into consideration so I have to fix this inconsistency - * later.) - * - * The requirement of FileStream: - * 1. It must be derived from Stream, of course. - * 2. It must have a constructor FileStream(String path, io::OpenFlag flags), so - * user can construct one with the same interface. - */ - -#pragma once - -#include "../PreConfig.h" - -#ifdef CRU_PLATFORM_UNIX -#include "../platform/unix/UnixFileStream.h" -namespace cru::io { -using FileStream = platform::unix::UnixFileStream; -} -#elif CRU_PLATFORM_WINDOWS -#include "../platform/win/Win32FileStream.h" -namespace cru::io { -using FileStream = platform::win::Win32FileStream; -} -#elif CRU_PLATFORM_EMSCRIPTEN -#include "../platform/web/WebFileStream.h" -namespace cru::io { -using FileStream = platform::web::WebFileStream; -} -#endif diff --git a/include/cru/common/io/Stream.h b/include/cru/common/io/Stream.h index 2388874e..d24931da 100644 --- a/include/cru/common/io/Stream.h +++ b/include/cru/common/io/Stream.h @@ -32,7 +32,7 @@ class CRU_BASE_API StreamOperationNotSupportedException : public Exception { class CRU_BASE_API StreamAlreadyClosedException : public Exception { public: - using Exception::Exception; + StreamAlreadyClosedException(); CRU_DEFAULT_COPY(StreamAlreadyClosedException) CRU_DEFAULT_MOVE(StreamAlreadyClosedException) diff --git a/include/cru/common/platform/unix/UnixFileStream.h b/include/cru/common/platform/unix/UnixFileStream.h index bf13358b..4b86649e 100644 --- a/include/cru/common/platform/unix/UnixFileStream.h +++ b/include/cru/common/platform/unix/UnixFileStream.h @@ -8,6 +8,9 @@ namespace cru::platform::unix { class UnixFileStream : public io::Stream { + private: + static constexpr auto kLogTag = u"cru::platform::unix::UnixFileStream"; + public: UnixFileStream(const char* path, int oflag); UnixFileStream(int fd, bool can_seek, bool can_read, bool can_write, @@ -32,6 +35,8 @@ class UnixFileStream : public io::Stream { void Close() override; + int GetFileDescriptor() const { return file_descriptor_; } + private: void CheckClosed(); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 31ec94db..97b49baf 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(CruBase StringToNumberConverter.cpp StringUtil.cpp io/CFileStream.cpp - io/FileNotExistException.cpp io/Stream.cpp io/Resource.cpp io/MemoryStream.cpp diff --git a/src/common/io/CFileStream.cpp b/src/common/io/CFileStream.cpp index de195538..29d4819f 100644 --- a/src/common/io/CFileStream.cpp +++ b/src/common/io/CFileStream.cpp @@ -97,6 +97,7 @@ bool CFileStream::CanRead() { Index CFileStream::Read(std::byte* buffer, Index offset, Index size) { CheckClosed(); + StreamOperationNotSupportedException::CheckRead(readable_); auto count = std::fread(buffer + offset, 1, size, file_); return count; } @@ -108,6 +109,7 @@ bool CFileStream::CanWrite() { Index CFileStream::Write(const std::byte* buffer, Index offset, Index size) { CheckClosed(); + StreamOperationNotSupportedException::CheckWrite(writable_); auto count = std::fwrite(buffer + offset, 1, size, file_); return count; } @@ -125,8 +127,6 @@ void CFileStream::Close() { } void CFileStream::CheckClosed() { - if (file_ == nullptr) { - throw StreamAlreadyClosedException(u"File is closed."); - } + StreamAlreadyClosedException::Check(file_ == nullptr); } } // namespace cru::io diff --git a/src/common/io/FileNotExistException.cpp b/src/common/io/FileNotExistException.cpp deleted file mode 100644 index a2e1fdb1..00000000 --- a/src/common/io/FileNotExistException.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "cru/common/io/FileNotExistException.h" - -#include "cru/common/Exception.h" -#include "cru/common/Format.h" - -#include <utility> - -namespace cru::io { - FileNotExistException::FileNotExistException(String path): Exception(), path_(std::move(path)) { - SetMessage(Format(u"File {} does not exist.", path_)); - } -} diff --git a/src/common/io/Stream.cpp b/src/common/io/Stream.cpp index 07677fa8..97669944 100644 --- a/src/common/io/Stream.cpp +++ b/src/common/io/Stream.cpp @@ -1,4 +1,5 @@ #include "cru/common/io/Stream.h" +#include "cru/common/Exception.h" #include "cru/common/Format.h" #include <utility> @@ -22,6 +23,9 @@ void StreamOperationNotSupportedException::CheckWrite(bool writable) { if (!writable) throw StreamOperationNotSupportedException(u"write"); } +StreamAlreadyClosedException::StreamAlreadyClosedException() + : Exception(u"Stream is already closed.") {} + void StreamAlreadyClosedException::Check(bool closed) { if (closed) throw StreamAlreadyClosedException(); } diff --git a/src/common/platform/unix/UnixFileStream.cpp b/src/common/platform/unix/UnixFileStream.cpp index 39870507..a952a7f2 100644 --- a/src/common/platform/unix/UnixFileStream.cpp +++ b/src/common/platform/unix/UnixFileStream.cpp @@ -2,10 +2,12 @@ #include "cru/common/Exception.h" #include "cru/common/Format.h" #include "cru/common/io/Stream.h" +#include "cru/common/log/Logger.h" #include <fcntl.h> #include <sys/fcntl.h> #include <unistd.h> +#include <cerrno> namespace cru::platform::unix { using namespace cru::io; @@ -56,7 +58,15 @@ UnixFileStream::UnixFileStream(int fd, bool can_seek, bool can_read, auto_close_ = auto_close; } -UnixFileStream::~UnixFileStream() { Close(); } +UnixFileStream::~UnixFileStream() { + if (auto_close_ && file_descriptor_ >= 0) { + if (::close(file_descriptor_) == -1) { + // We are in destructor, so we can not throw. + CRU_LOG_WARN(u"Failed to close file descriptor {}, errno {}.", + file_descriptor_, errno); + } + } +} bool UnixFileStream::CanSeek() { CheckClosed(); diff --git a/src/ui/ThemeManager.cpp b/src/ui/ThemeManager.cpp index 93dc9a9f..7358637e 100644 --- a/src/ui/ThemeManager.cpp +++ b/src/ui/ThemeManager.cpp @@ -2,7 +2,6 @@ #include "Helper.h" #include "cru/common/StringUtil.h" -#include "cru/common/io/FileStream.h" #include "cru/common/io/Resource.h" #include "cru/platform/graphics/Brush.h" #include "cru/platform/graphics/Factory.h" |