Obtain file sizes without seeking where possible

This commit is contained in:
Silent 2021-05-21 17:51:26 +02:00
parent 95388cfbb1
commit 724a3c0579
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1
2 changed files with 10 additions and 7 deletions

View File

@ -57,8 +57,7 @@ namespace File
}
std::vector<uint8_t> result;
fs.seekg(0, std::ios::end);
auto fsize = static_cast<size_t>(fs.tellg());
auto fsize = Platform::GetFileSize(path);
if (fsize > SIZE_MAX)
{
std::string message = String::StdFormat(
@ -68,7 +67,6 @@ namespace File
else
{
result.resize(fsize);
fs.seekg(0);
fs.read(reinterpret_cast<char*>(result.data()), result.size());
fs.exceptions(fs.failbit);
}

View File

@ -1,5 +1,5 @@
/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
* Copyright (c) 2014-2021 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
@ -16,6 +16,8 @@
#ifndef _WIN32
# include <sys/stat.h>
#else
# include <io.h>
#endif
#if defined(__linux__) && !defined(__ANDROID__)
@ -98,9 +100,12 @@ namespace OpenRCT2
throw IOException(String::StdFormat("Unable to open '%s'", path));
}
Seek(0, STREAM_SEEK_END);
_fileSize = GetPosition();
Seek(0, STREAM_SEEK_BEGIN);
#ifdef _WIN32
_fileSize = _filelengthi64(_fileno(_file));
#else
std::error_code ec;
_fileSize = fs::file_size(fs::u8path(path), ec);
#endif
_ownsFilePtr = true;
}