From 839292efaaf8d997b2cbca7abae8c6e575f6c01a Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 25 Jan 2022 17:42:19 +0800 Subject: ... --- src/common/CMakeLists.txt | 2 +- src/common/io/Win32FileStream.cpp | 127 --------------------------- src/common/platform/win/Exception.cpp | 7 ++ src/common/platform/win/Win32FileStream.cpp | 129 ++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 128 deletions(-) delete mode 100644 src/common/io/Win32FileStream.cpp create mode 100644 src/common/platform/win/Win32FileStream.cpp (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0316d9e6..481205a0 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -34,7 +34,7 @@ endif() if (WIN32) target_sources(cru_base PRIVATE - io/Win32FileStream.cpp + platform/win/Win32FileStream.cpp platform/win/Exception.cpp ) endif() diff --git a/src/common/io/Win32FileStream.cpp b/src/common/io/Win32FileStream.cpp deleted file mode 100644 index e7ee0e6a..00000000 --- a/src/common/io/Win32FileStream.cpp +++ /dev/null @@ -1,127 +0,0 @@ -#include "cru/common/io/Win32FileStream.hpp" - -#include "cru/common/Exception.hpp" -#include "cru/common/io/OpenFileFlag.hpp" - -#include - -namespace cru::io { -namespace details { -struct Win32FileStreamPrivate { - HANDLE handle; -}; -} // namespace details - -Win32FileStream::Win32FileStream(String path, OpenFileFlag flags) - : path_(std::move(path)), flags_(flags) { - p_ = new details::Win32FileStreamPrivate(); - - DWORD dwDesiredAccess = 0; - if (flags & OpenFileFlags::Read) { - dwDesiredAccess |= GENERIC_READ; - } - if (flags & OpenFileFlags::Write) { - dwDesiredAccess |= GENERIC_WRITE; - } - - DWORD dwCreationDisposition = 0; - if (flags & OpenFileFlags::Create) { - if (flags & OpenFileFlags::ThrowOnExist) { - dwCreationDisposition = CREATE_NEW; - } else { - dwCreationDisposition = OPEN_ALWAYS; - } - } else { - dwCreationDisposition = OPEN_EXISTING; - } - - p_->handle = - ::CreateFileW(path_.WinCStr(), dwDesiredAccess, 0, nullptr, - dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr); - - if (p_->handle == INVALID_HANDLE_VALUE) { - throw Exception(u"Failed to call CreateFileW."); - } -} - -Win32FileStream::~Win32FileStream() { - Close(); - delete p_; -} - -bool Win32FileStream::CanSeek() { return true; } - -Index Win32FileStream::Tell() { - CheckClosed(); - - LARGE_INTEGER offset; - offset.QuadPart = 0; - LARGE_INTEGER file_pointer; - if (::SetFilePointerEx(p_->handle, offset, &file_pointer, FILE_CURRENT) == - 0) { - throw Exception(u"Failed to call SetFilePointerEx."); - } - - return file_pointer.QuadPart; -} - -void Win32FileStream::Seek(Index offset, SeekOrigin origin) { - CheckClosed(); - - DWORD dwMoveMethod = 0; - - if (origin == SeekOrigin::Current) { - dwMoveMethod = FILE_CURRENT; - } else if (origin == SeekOrigin::End) { - dwMoveMethod = FILE_END; - } else { - dwMoveMethod = FILE_BEGIN; - } - - LARGE_INTEGER n_offset; - n_offset.QuadPart = offset; - if (::SetFilePointerEx(p_->handle, n_offset, nullptr, dwMoveMethod) == 0) { - throw Exception(u"Failed to call SetFilePointerEx."); - } -} - -bool Win32FileStream::CanRead() { return true; } - -Index Win32FileStream::Read(std::byte* buffer, Index offset, Index size) { - CheckClosed(); - - DWORD dwRead; - if (::ReadFile(p_->handle, buffer + offset, size, &dwRead, nullptr) == 0) { - throw Exception(u"Failed to call ReadFile."); - } - return dwRead; -} - -bool Win32FileStream::CanWrite() { return true; } - -Index Win32FileStream::Write(const std::byte* buffer, Index offset, - Index size) { - CheckClosed(); - - DWORD dwWritten; - if (::WriteFile(p_->handle, buffer + offset, size, &dwWritten, nullptr) == - 0) { - throw Exception(u"Failed to call WriteFile."); - } - - return dwWritten; -} - -void Win32FileStream::Close() { - if (closed_) return; - - ::CloseHandle(p_->handle); - - closed_ = true; -} - -void Win32FileStream::CheckClosed() { - if (closed_) throw Exception(u"Stream is closed."); -} - -} // namespace cru::io diff --git a/src/common/platform/win/Exception.cpp b/src/common/platform/win/Exception.cpp index 34ae8955..def12123 100644 --- a/src/common/platform/win/Exception.cpp +++ b/src/common/platform/win/Exception.cpp @@ -32,8 +32,15 @@ inline String Win32MakeMessage(DWORD error_code, String message) { Win32Error::Win32Error(std::string_view message) : Win32Error(::GetLastError(), message) {} +Win32Error::Win32Error(String message) + : Win32Error(::GetLastError(), message) {} + Win32Error::Win32Error(DWORD error_code, std::string_view message) : PlatformException(Win32MakeMessage( error_code, String::FromUtf8(message.data(), message.size()))), error_code_(error_code) {} + +Win32Error::Win32Error(DWORD error_code, String message) + : PlatformException(Win32MakeMessage(error_code, message)), + error_code_(error_code) {} } // namespace cru::platform::win diff --git a/src/common/platform/win/Win32FileStream.cpp b/src/common/platform/win/Win32FileStream.cpp new file mode 100644 index 00000000..5997c570 --- /dev/null +++ b/src/common/platform/win/Win32FileStream.cpp @@ -0,0 +1,129 @@ +#include "cru/common/platform/win/Win32FileStream.hpp" + +#include "cru/common/io/OpenFileFlag.hpp" +#include "cru/common/platform/win/Exception.hpp" + +#include + +namespace cru::platform::win { +using namespace cru::io; + +namespace details { +struct Win32FileStreamPrivate { + HANDLE handle; +}; +} // namespace details + +Win32FileStream::Win32FileStream(String path, OpenFileFlag flags) + : path_(std::move(path)), flags_(flags) { + p_ = new details::Win32FileStreamPrivate(); + + DWORD dwDesiredAccess = 0; + if (flags & OpenFileFlags::Read) { + dwDesiredAccess |= GENERIC_READ; + } + if (flags & OpenFileFlags::Write) { + dwDesiredAccess |= GENERIC_WRITE; + } + + DWORD dwCreationDisposition = 0; + if (flags & OpenFileFlags::Create) { + if (flags & OpenFileFlags::ThrowOnExist) { + dwCreationDisposition = CREATE_NEW; + } else { + dwCreationDisposition = OPEN_ALWAYS; + } + } else { + dwCreationDisposition = OPEN_EXISTING; + } + + p_->handle = + ::CreateFileW(path_.WinCStr(), dwDesiredAccess, 0, nullptr, + dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr); + + if (p_->handle == INVALID_HANDLE_VALUE) { + throw Win32Error(u"Failed to call CreateFileW."); + } +} + +Win32FileStream::~Win32FileStream() { + Close(); + delete p_; +} + +bool Win32FileStream::CanSeek() { return true; } + +Index Win32FileStream::Tell() { + CheckClosed(); + + LARGE_INTEGER offset; + offset.QuadPart = 0; + LARGE_INTEGER file_pointer; + if (::SetFilePointerEx(p_->handle, offset, &file_pointer, FILE_CURRENT) == + 0) { + throw Win32Error(u"Failed to call SetFilePointerEx."); + } + + return file_pointer.QuadPart; +} + +void Win32FileStream::Seek(Index offset, SeekOrigin origin) { + CheckClosed(); + + DWORD dwMoveMethod = 0; + + if (origin == SeekOrigin::Current) { + dwMoveMethod = FILE_CURRENT; + } else if (origin == SeekOrigin::End) { + dwMoveMethod = FILE_END; + } else { + dwMoveMethod = FILE_BEGIN; + } + + LARGE_INTEGER n_offset; + n_offset.QuadPart = offset; + if (::SetFilePointerEx(p_->handle, n_offset, nullptr, dwMoveMethod) == 0) { + throw Win32Error(u"Failed to call SetFilePointerEx."); + } +} + +bool Win32FileStream::CanRead() { return true; } + +Index Win32FileStream::Read(std::byte* buffer, Index offset, Index size) { + CheckClosed(); + + DWORD dwRead; + if (::ReadFile(p_->handle, buffer + offset, size, &dwRead, nullptr) == 0) { + throw Win32Error(u"Failed to call ReadFile."); + } + return dwRead; +} + +bool Win32FileStream::CanWrite() { return true; } + +Index Win32FileStream::Write(const std::byte* buffer, Index offset, + Index size) { + CheckClosed(); + + DWORD dwWritten; + if (::WriteFile(p_->handle, buffer + offset, size, &dwWritten, nullptr) == + 0) { + throw Win32Error(u"Failed to call WriteFile."); + } + + return dwWritten; +} + +void Win32FileStream::Close() { + if (closed_) return; + + ::CloseHandle(p_->handle); + + closed_ = true; +} + +void Win32FileStream::CheckClosed() { + if (closed_) throw Exception(u"Stream is closed."); +} + +} // namespace cru::platform::win -- cgit v1.2.3