diff options
author | crupest <crupest@outlook.com> | 2022-01-15 18:34:55 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-15 18:34:55 +0800 |
commit | 76b631144e008006efb3894c16e6fd04608d0b68 (patch) | |
tree | f676581ac216e910124dd42bd748d9f4a963217a /src/common | |
parent | ae638fc0c2cad09ae0ab69707852588aedc6e0df (diff) | |
download | cru-76b631144e008006efb3894c16e6fd04608d0b68.tar.gz cru-76b631144e008006efb3894c16e6fd04608d0b68.tar.bz2 cru-76b631144e008006efb3894c16e6fd04608d0b68.zip |
...
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/common/ErrnoException.cpp | 11 | ||||
-rw-r--r-- | src/common/io/Stream.cpp | 2 | ||||
-rw-r--r-- | src/common/io/UnixFileStream.cpp | 125 |
4 files changed, 146 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index b91b380c..19821797 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -26,6 +26,14 @@ target_include_directories(cru_base PUBLIC ${CRU_INCLUDE_DIR}) target_compile_definitions(cru_base PUBLIC $<$<CONFIG:Debug>:CRU_DEBUG>) target_compile_definitions(cru_base PRIVATE CRU_BASE_EXPORT_API) +if (UNIX) + target_sources(cru_base PRIVATE + io/UnixFileStream.cpp + PUBLIC + ${CRU_BASE_INCLUDE_DIR}/io/UnixFileStream.hpp + ) +endif() + if (WIN32) target_compile_definitions(cru_base PUBLIC CRU_PLATFORM_WINDOWS) elseif(APPLE) diff --git a/src/common/ErrnoException.cpp b/src/common/ErrnoException.cpp new file mode 100644 index 00000000..df2349ac --- /dev/null +++ b/src/common/ErrnoException.cpp @@ -0,0 +1,11 @@ +#include "cru/common/ErrnoException.hpp" + +#include <errno.h> + +namespace cru { +ErrnoException::ErrnoException(const String& message) + : ErrnoException(message, errno) {} + +ErrnoException::ErrnoException(const String& message, int errno_code) + : Exception(message), errno_code_(errno_code) {} +} // namespace cru diff --git a/src/common/io/Stream.cpp b/src/common/io/Stream.cpp index 6addfdc0..1a39b653 100644 --- a/src/common/io/Stream.cpp +++ b/src/common/io/Stream.cpp @@ -18,4 +18,6 @@ Index Stream::Read(std::byte* buffer, Index size) { Index Stream::Write(const std::byte* buffer, Index size) { return Write(buffer, 0, size); } + +void Stream::Close() {} } // namespace cru::io diff --git a/src/common/io/UnixFileStream.cpp b/src/common/io/UnixFileStream.cpp new file mode 100644 index 00000000..23c11dad --- /dev/null +++ b/src/common/io/UnixFileStream.cpp @@ -0,0 +1,125 @@ +#include "cru/common/io/UnixFileStream.hpp" +#include "cru/common/ErrnoException.hpp" +#include "cru/common/io/OpenFileFlag.hpp" + +#include <fcntl.h> +#include <unistd.h> + +namespace cru::io { + +namespace { +int MapOpenFileFlag(OpenFileFlag flag) { + int result = 0; + if (flag & OpenFileFlags::Read) { + if (flag & OpenFileFlags::Write) { + result |= O_RDWR; + } else { + result |= O_RDONLY; + } + } else { + if (flag & OpenFileFlags::Write) { + result |= O_WRONLY; + } else { + throw Exception(u"Invalid open file flag."); + } + } + + if (flag | OpenFileFlags::Append) { + result |= O_APPEND; + } + + if (flag | OpenFileFlags::Create) { + result |= O_CREAT; + } + + if (flag | 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(String path, OpenFileFlag flags) { + file_descriptor_ = ::open(path.ToUtf8().c_str(), MapOpenFileFlag(flags)); + if (file_descriptor_ == -1) { + throw ErrnoException(u"Failed to open file."); + } +} + +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."); + } +} + +void UnixFileStream::CheckClosed() { + if (closed_) { + throw Exception(u"File is closed."); + } +} +} // namespace cru::io |