diff options
| author | Martin Ridgers <[email protected]> | 2021-09-16 16:41:12 +0200 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-09-16 16:41:12 +0200 |
| commit | 7e6050246217e381408b7d649d5d0edae344d8e1 (patch) | |
| tree | 1a034011deb8a921f12e629e0679bb71e48afdeb /zencore/filesystem.cpp | |
| parent | POSIX implementation of WriteFile() (diff) | |
| download | zen-7e6050246217e381408b7d649d5d0edae344d8e1.tar.xz zen-7e6050246217e381408b7d649d5d0edae344d8e1.zip | |
POSIX implementation of ReadFile()
Diffstat (limited to 'zencore/filesystem.cpp')
| -rw-r--r-- | zencore/filesystem.cpp | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp index 6c5c32565..a042fb7a1 100644 --- a/zencore/filesystem.cpp +++ b/zencore/filesystem.cpp @@ -525,6 +525,10 @@ WriteFile(std::filesystem::path Path, IoBuffer Data) FileContents ReadFile(std::filesystem::path Path) { + uint64_t FileSizeBytes; + void* Handle; + +#if ZEN_PLATFORM_WINDOWS ATL::CHandle FromFile(CreateFileW(Path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr)); if (FromFile == INVALID_HANDLE_VALUE) { @@ -538,12 +542,25 @@ ReadFile(std::filesystem::path Path) return FileContents{.ErrorCode = std::error_code(::GetLastError(), std::system_category())}; } - const uint64_t FileSizeBytes = FileSize.EndOfFile.QuadPart; + FileSizeBytes = FileSize.EndOfFile.QuadPart; + Handle = FromFile.Detach(); +#else + int Fd = open(Path.c_str(), O_RDONLY); + if (Fd < 0) + { + return FileContents{.ErrorCode = std::error_code(zen::GetLastError(), std::system_category())}; + } - FileContents Contents; + static_assert(sizeof(decltype(stat::st_size)) == sizeof(uint64_t), "fstat() doesn't support large files"); + struct stat Stat; + fstat(Fd, &Stat); - Contents.Data.emplace_back(IoBuffer(IoBuffer::File, FromFile.Detach(), 0, FileSizeBytes)); + FileSizeBytes = Stat.st_size; + Handle = (void*)uintptr_t(Fd); +#endif + FileContents Contents; + Contents.Data.emplace_back(IoBuffer(IoBuffer::File, Handle, 0, FileSizeBytes)); return Contents; } |