aboutsummaryrefslogtreecommitdiff
path: root/core/src/Utils/IO/FileStream.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2021-11-12 13:34:50 -0800
committerrtk0c <[email protected]>2021-11-12 13:34:50 -0800
commit4932f36da4fab3fc8965822bba473cbd20f7f405 (patch)
tree7317eaacb0d64de183a2bdce8e07d7120c29e5de /core/src/Utils/IO/FileStream.cpp
parent816bbe7993adf4a41ace7bae06bfe6a5921308b8 (diff)
Fix compile errors under MSVC, remove PLATFORM_* macros in favor of compiler builtin macros, and add win32 support to FileStream
Diffstat (limited to 'core/src/Utils/IO/FileStream.cpp')
-rw-r--r--core/src/Utils/IO/FileStream.cpp122
1 files changed, 115 insertions, 7 deletions
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 <cstring>
#include <iostream>
-#if PLATFORM_WIN32
+namespace fs = std::filesystem;
-// TODO
+#if defined(_WIN32)
+# define WIN32_LEAN_AND_MEAN
+# define NOMINMAX
+# include <Windows.h>
-#elif PLATFORM_MACOS || PLATFORM_LINUX
+InputFileStream::InputFileStream(const fs::path& path)
+ : mOsFileHandle{ 0 }
+{
+ auto handle = reinterpret_cast<HANDLE*>(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<HANDLE*>(mOsFileHandle);
+ CloseHandle(*handle);
+}
+
+OutputFileStream::OutputFileStream(const fs::path& path, WriteMode mode)
+ : mOsFileHandle{ 0 }
+{
+ auto handle = reinterpret_cast<HANDLE*>(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<HANDLE*>(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 <fcntl.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
-InputFileStream::InputFileStream(const std::filesystem::path& path)
+InputFileStream::InputFileStream(const fs::path& path)
: mOsFileHandle{ 0 }
{
auto fd = reinterpret_cast<int*>(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<int*>(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 {