From 71c01a175a939d1a519ab235fdfdeec1101f8b84 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 25 Jan 2022 17:48:53 +0800 Subject: ... --- src/common/CMakeLists.txt | 4 +- src/common/ErrnoException.cpp | 14 --- src/common/io/UnixFileStream.cpp | 134 --------------------------- src/common/platform/unix/ErrnoException.cpp | 14 +++ src/common/platform/unix/UnixFileStream.cpp | 135 ++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 150 deletions(-) delete mode 100644 src/common/ErrnoException.cpp delete mode 100644 src/common/io/UnixFileStream.cpp create mode 100644 src/common/platform/unix/ErrnoException.cpp create mode 100644 src/common/platform/unix/UnixFileStream.cpp (limited to 'src') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 481205a0..200ea7fa 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -17,8 +17,8 @@ target_compile_definitions(cru_base PUBLIC $<$:CRU_DEBUG>) if (UNIX) target_sources(cru_base PRIVATE - ErrnoException.cpp - io/UnixFileStream.cpp + platform/unix/ErrnoException.cpp + platform/unix/UnixFileStream.cpp ) endif() diff --git a/src/common/ErrnoException.cpp b/src/common/ErrnoException.cpp deleted file mode 100644 index 731ae3e5..00000000 --- a/src/common/ErrnoException.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "cru/common/ErrnoException.hpp" - -#include "cru/common/Format.hpp" - -#include - -namespace cru { -ErrnoException::ErrnoException(const String& message) - : ErrnoException(message, errno) {} - -ErrnoException::ErrnoException(const String& message, int errno_code) - : Exception(Format(u"{}. Errno is {}.", message, errno_code)), - errno_code_(errno_code) {} -} // namespace cru diff --git a/src/common/io/UnixFileStream.cpp b/src/common/io/UnixFileStream.cpp deleted file mode 100644 index 615c36f1..00000000 --- a/src/common/io/UnixFileStream.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include "cru/common/io/UnixFileStream.hpp" -#include "cru/common/ErrnoException.hpp" -#include "cru/common/io/OpenFileFlag.hpp" -#include "cru/common/Format.hpp" - -#include -#include -#include - -namespace cru::io { - -namespace { -int MapOpenFileFlag(OpenFileFlag flags) { - int result = 0; - if (flags & OpenFileFlags::Read) { - if (flags & OpenFileFlags::Write) { - result |= O_RDWR; - } else { - result |= O_RDONLY; - } - } else { - if (flags & OpenFileFlags::Write) { - result |= O_WRONLY; - } else { - throw Exception(u"Invalid open file flag."); - } - } - - if (flags & OpenFileFlags::Append) { - result |= O_APPEND; - } - - if (flags & OpenFileFlags::Create) { - result |= O_CREAT; - } - - if (flags & OpenFileFlags::ThrowOnExist) { - result |= O_EXCL; - } - - return result; -} - -int MapSeekOrigin(Stream::SeekOrigin origin) { - switch (origin) { - case Stream::SeekOrigin::Begin: - return SEEK_SET; - case Stream::SeekOrigin::Current: - return SEEK_CUR; - case Stream::SeekOrigin::End: - return SEEK_END; - default: - throw Exception(u"Invalid seek origin."); - } -} -} // namespace - -UnixFileStream::~UnixFileStream() { Close(); } - -UnixFileStream::UnixFileStream(String path, OpenFileFlag flags) - : path_(std::move(path)), flags_(flags) { - auto p = path_.ToUtf8(); - file_descriptor_ = - ::open(p.c_str(), MapOpenFileFlag(flags_), S_IRUSR | S_IWUSR); - if (file_descriptor_ == -1) { - throw ErrnoException( - Format(u"Failed to open file {} with flags {}.", path_, flags_.value)); - } -} - -bool UnixFileStream::CanSeek() { - CheckClosed(); - return true; -} - -Index UnixFileStream::Tell() { - CheckClosed(); - auto result = ::lseek(file_descriptor_, 0, SEEK_CUR); - if (result == -1) { - throw ErrnoException(u"Failed to get file position."); - } - return result; -} - -void UnixFileStream::Seek(Index offset, SeekOrigin origin) { - CheckClosed(); - int result = ::lseek(file_descriptor_, offset, MapSeekOrigin(origin)); - if (result == -1) { - throw ErrnoException(u"Failed to seek file."); - } -} - -bool UnixFileStream::CanRead() { - CheckClosed(); - return flags_ & OpenFileFlags::Read; -} - -Index UnixFileStream::Read(std::byte *buffer, Index offset, Index size) { - CheckClosed(); - auto result = ::read(file_descriptor_, buffer + offset, size); - if (result == -1) { - throw ErrnoException(u"Failed to read file."); - } - return result; -} - -bool UnixFileStream::CanWrite() { - CheckClosed(); - return flags_ & OpenFileFlags::Write; -} - -Index UnixFileStream::Write(const std::byte *buffer, Index offset, Index size) { - CheckClosed(); - auto result = ::write(file_descriptor_, buffer + offset, size); - if (result == -1) { - throw ErrnoException(u"Failed to write file."); - } - return result; -} - -void UnixFileStream::Close() { - if (closed_) return; - if (::close(file_descriptor_) == -1) { - throw ErrnoException(u"Failed to close file."); - } - closed_ = true; -} - -void UnixFileStream::CheckClosed() { - if (closed_) { - throw Exception(u"File is closed."); - } -} -} // namespace cru::io diff --git a/src/common/platform/unix/ErrnoException.cpp b/src/common/platform/unix/ErrnoException.cpp new file mode 100644 index 00000000..14e6a5a2 --- /dev/null +++ b/src/common/platform/unix/ErrnoException.cpp @@ -0,0 +1,14 @@ +#include "cru/common/platform/unix/ErrnoException.hpp" + +#include "cru/common/Format.hpp" + +#include + +namespace cru::platform::unix { +ErrnoException::ErrnoException(const String& message) + : ErrnoException(message, errno) {} + +ErrnoException::ErrnoException(const String& message, int errno_code) + : PlatformException(Format(u"{}. Errno is {}.", message, errno_code)), + errno_code_(errno_code) {} +} // namespace cru::platform::unix diff --git a/src/common/platform/unix/UnixFileStream.cpp b/src/common/platform/unix/UnixFileStream.cpp new file mode 100644 index 00000000..bb8d4207 --- /dev/null +++ b/src/common/platform/unix/UnixFileStream.cpp @@ -0,0 +1,135 @@ +#include "cru/common/platform/unix/UnixFileStream.hpp" +#include "cru/common/Format.hpp" +#include "cru/common/io/OpenFileFlag.hpp" +#include "cru/common/platform/unix/ErrnoException.hpp" + +#include +#include +#include + +namespace cru::platform::unix { +using namespace cru::io; + +namespace { +int MapOpenFileFlag(OpenFileFlag flags) { + int result = 0; + if (flags & OpenFileFlags::Read) { + if (flags & OpenFileFlags::Write) { + result |= O_RDWR; + } else { + result |= O_RDONLY; + } + } else { + if (flags & OpenFileFlags::Write) { + result |= O_WRONLY; + } else { + throw Exception(u"Invalid open file flag."); + } + } + + if (flags & OpenFileFlags::Append) { + result |= O_APPEND; + } + + if (flags & OpenFileFlags::Create) { + result |= O_CREAT; + } + + if (flags & OpenFileFlags::ThrowOnExist) { + result |= O_EXCL; + } + + return result; +} + +int MapSeekOrigin(Stream::SeekOrigin origin) { + switch (origin) { + case Stream::SeekOrigin::Begin: + return SEEK_SET; + case Stream::SeekOrigin::Current: + return SEEK_CUR; + case Stream::SeekOrigin::End: + return SEEK_END; + default: + throw Exception(u"Invalid seek origin."); + } +} +} // namespace + +UnixFileStream::~UnixFileStream() { Close(); } + +UnixFileStream::UnixFileStream(String path, OpenFileFlag flags) + : path_(std::move(path)), flags_(flags) { + auto p = path_.ToUtf8(); + file_descriptor_ = + ::open(p.c_str(), MapOpenFileFlag(flags_), S_IRUSR | S_IWUSR); + if (file_descriptor_ == -1) { + throw ErrnoException( + Format(u"Failed to open file {} with flags {}.", path_, flags_.value)); + } +} + +bool UnixFileStream::CanSeek() { + CheckClosed(); + return true; +} + +Index UnixFileStream::Tell() { + CheckClosed(); + auto result = ::lseek(file_descriptor_, 0, SEEK_CUR); + if (result == -1) { + throw ErrnoException(u"Failed to get file position."); + } + return result; +} + +void UnixFileStream::Seek(Index offset, SeekOrigin origin) { + CheckClosed(); + int result = ::lseek(file_descriptor_, offset, MapSeekOrigin(origin)); + if (result == -1) { + throw ErrnoException(u"Failed to seek file."); + } +} + +bool UnixFileStream::CanRead() { + CheckClosed(); + return flags_ & OpenFileFlags::Read; +} + +Index UnixFileStream::Read(std::byte *buffer, Index offset, Index size) { + CheckClosed(); + auto result = ::read(file_descriptor_, buffer + offset, size); + if (result == -1) { + throw ErrnoException(u"Failed to read file."); + } + return result; +} + +bool UnixFileStream::CanWrite() { + CheckClosed(); + return flags_ & OpenFileFlags::Write; +} + +Index UnixFileStream::Write(const std::byte *buffer, Index offset, Index size) { + CheckClosed(); + auto result = ::write(file_descriptor_, buffer + offset, size); + if (result == -1) { + throw ErrnoException(u"Failed to write file."); + } + return result; +} + +void UnixFileStream::Close() { + if (closed_) return; + if (::close(file_descriptor_) == -1) { + throw ErrnoException(u"Failed to close file."); + } + closed_ = true; +} + +void UnixFileStream::CheckClosed() { + if (closed_) { + throw Exception(u"File is closed."); + } +} +} // namespace cru::platform::unix -- cgit v1.2.3