From c82f800794981970b021539549713447ee258096 Mon Sep 17 00:00:00 2001 From: Silent Date: Tue, 18 Jul 2023 18:26:20 +0200 Subject: [PATCH] FileSystem: Use _filelengthi64 to get the file size on Windows --- common/FileSystem.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/common/FileSystem.cpp b/common/FileSystem.cpp index 1f30330325..5c998995eb 100644 --- a/common/FileSystem.cpp +++ b/common/FileSystem.cpp @@ -38,6 +38,7 @@ #if defined(_WIN32) #include "RedtapeWindows.h" +#include #include #include #include @@ -747,6 +748,13 @@ s64 FileSystem::FTell64(std::FILE* fp) s64 FileSystem::FSize64(std::FILE* fp) { +#ifdef _WIN32 + const int fd = _fileno(fp); + if (fd >= 0) + { + return _filelengthi64(fd); + } +#else const s64 pos = FTell64(fp); if (pos >= 0) { @@ -757,6 +765,7 @@ s64 FileSystem::FSize64(std::FILE* fp) return size; } } +#endif return -1; } @@ -781,12 +790,11 @@ std::optional> FileSystem::ReadBinaryFile(const char* filename) std::optional> FileSystem::ReadBinaryFile(std::FILE* fp) { - std::fseek(fp, 0, SEEK_END); - const long size = std::ftell(fp); - std::fseek(fp, 0, SEEK_SET); + const s64 size = FSize64(fp); if (size < 0) return std::nullopt; + std::fseek(fp, 0, SEEK_SET); std::vector res(static_cast(size)); if (size > 0 && std::fread(res.data(), 1u, static_cast(size), fp) != static_cast(size)) return std::nullopt; @@ -805,12 +813,11 @@ std::optional FileSystem::ReadFileToString(const char* filename) std::optional FileSystem::ReadFileToString(std::FILE* fp) { - std::fseek(fp, 0, SEEK_END); - const long size = std::ftell(fp); - std::fseek(fp, 0, SEEK_SET); + const s64 size = FSize64(fp); if (size < 0) return std::nullopt; + std::fseek(fp, 0, SEEK_SET); std::string res; res.resize(static_cast(size)); // NOTE - assumes mode 'rb', for example, this will fail over missing Windows carriage return bytes