From 4932f36da4fab3fc8965822bba473cbd20f7f405 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 12 Nov 2021 13:34:50 -0800 Subject: Fix compile errors under MSVC, remove PLATFORM_* macros in favor of compiler builtin macros, and add win32 support to FileStream --- core/src/Utils/IO/FileStream.cpp | 122 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 115 insertions(+), 7 deletions(-) (limited to 'core/src/Utils/IO/FileStream.cpp') diff --git a/core/src/Utils/IO/FileStream.cpp b/core/src/Utils/IO/FileStream.cpp index bc95b7e..b9ef2a7 100644 --- a/core/src/Utils/IO/FileStream.cpp +++ b/core/src/Utils/IO/FileStream.cpp @@ -3,17 +3,125 @@ #include #include -#if PLATFORM_WIN32 +namespace fs = std::filesystem; -// TODO +#if defined(_WIN32) +# define WIN32_LEAN_AND_MEAN +# define NOMINMAX +# include -#elif PLATFORM_MACOS || PLATFORM_LINUX +InputFileStream::InputFileStream(const fs::path& path) + : mOsFileHandle{ 0 } +{ + auto handle = reinterpret_cast(mOsFileHandle); + + *handle = CreateFileW( + path.c_str(), // fs::path::c_str() returns a wide string on Windows + GENERIC_READ, + /* No sharing */ 0, + /* Use default security*/ nullptr, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + /* No attribute template */ nullptr); + + // TODO handle error +} + +InputFileStream::~InputFileStream() +{ + auto handle = reinterpret_cast(mOsFileHandle); + CloseHandle(*handle); +} + +OutputFileStream::OutputFileStream(const fs::path& path, WriteMode mode) + : mOsFileHandle{ 0 } +{ + auto handle = reinterpret_cast(mOsFileHandle); + + DWORD creationDisposition; + switch (mode) { + case AppendFile: creationDisposition = OPEN_ALWAYS; break; + case TruncateFile: creationDisposition = CREATE_ALWAYS; break; + } + + *handle = CreateFileW( + path.c_str(), + GENERIC_WRITE, + /* No sharing */ 0, + /* Use default security*/ nullptr, + creationDisposition, + FILE_ATTRIBUTE_NORMAL, + /* No attribute template */ nullptr); + + // TODO handle error +} + +OutputFileStream::~OutputFileStream() +{ + auto handle = reinterpret_cast(mOsFileHandle); + CloseHandle(*handle); +} + +static IoResult::ErrorKind MapErrorCodeToIoResult(DWORD error) +{ + switch (error) { + // TODO + + default: + std::cerr << "Unimplemented win32 error code " << error << ", report bug immediately.\n"; + std::abort(); + } +} + +static IoResult ReadBytesDirect(HANDLE hFile, size_t byteCount, std::byte* bytes) +{ + DWORD bytesRead; + BOOL result = ReadFile(hFile, bytes, byteCount, &bytesRead, nullptr); + + if (result) { + return IoResult{ + .Error = IoResult::ERR_None, + .SystemError = 0, + .BytesMoved = bytesRead, + }; + } else { + DWORD errorCode = GetLastError(); + return IoResult{ + .Error = ::MapErrorCodeToIoResult(errorCode), + .SystemError = errorCode, + .BytesMoved = bytesRead, + }; + } +} + +static IoResult WriteBytesDirect(HANDLE hFile, size_t byteCount, const std::byte* bytes) +{ + DWORD bytesWritten; + BOOL result = WriteFile(hFile, bytes, byteCount, &bytesWritten, nullptr); + + if (result) { + return IoResult{ + .Error = IoResult::ERR_None, + .SystemError = 0, + .BytesMoved = bytesWritten, + }; + } else { + DWORD errorCode = GetLastError(); + return IoResult{ + .Error = ::MapErrorCodeToIoResult(errorCode), + .SystemError = errorCode, + .BytesMoved = bytesWritten, + }; + } +} + +#elif defined(__APPLE__) || defined(__linux__) # include # include # include # include -InputFileStream::InputFileStream(const std::filesystem::path& path) +InputFileStream::InputFileStream(const fs::path& path) : mOsFileHandle{ 0 } { auto fd = reinterpret_cast(mOsFileHandle); @@ -26,7 +134,7 @@ InputFileStream::~InputFileStream() close(*fd); } -OutputFileStream::OutputFileStream(const std::filesystem::path& path, WriteMode mode) +OutputFileStream::OutputFileStream(const fs::path& path, WriteMode mode) : mOsFileHandle{ 0 } { auto fd = reinterpret_cast(mOsFileHandle); @@ -70,7 +178,7 @@ static IoResult ReadBytesDirect(const char* osFileHandle, size_t byteCount, std: int err = errno; return IoResult{ .Error = ::MapErrnoToIoResult(err), - .SystemError = err, + .SystemError = (uint32_t)err, .BytesMoved = 0, }; } else { @@ -91,7 +199,7 @@ static IoResult WriteBytesDirect(const char* osFileHandle, size_t byteCount, con int err = errno; return IoResult{ .Error = ::MapErrnoToIoResult(err), - .SystemError = err, + .SystemError = (uint32_t)err, .BytesMoved = 0, }; } else { -- cgit v1.2.3-70-g09d2