diff --git a/common/FileSystem.cpp b/common/FileSystem.cpp index 1f303303254c7..5c998995ebb16 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