1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-07-01 13:11:34 +00:00

Cleanup and fixes

This commit is contained in:
ζeh Matt 2022-07-16 21:00:50 +03:00
parent 1b70ff775d
commit 3d5f898920
No known key found for this signature in database
GPG key ID: 18CE582C71A225B0
3 changed files with 25 additions and 27 deletions

View file

@ -8,6 +8,8 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdexcept>
#include <string>
namespace Platform::File {
@ -29,7 +31,6 @@ namespace Platform::File {
Handle open(const char* filename)
{
// Posix
#ifdef O_BINARY
static const int openFlags = O_RDONLY | O_BINARY;
#else
@ -39,9 +40,7 @@ namespace Platform::File {
auto handle = ::open(filename, openFlags, 0);
if (handle == -1)
{
std::ostringstream os;
os << "Failed to open '" << filename << "' for reading: " << strerror(errno);
throw std::runtime_error(os.str());
throw std::runtime_error(std::string("Failed to open '") + filename + "' for reading: " + strerror(errno));
}
return static_cast<Handle>(handle);
}
@ -58,7 +57,7 @@ namespace Platform::File {
const auto nativeHandle = getNativeHandle(handle);
const auto nativeSeekType = getNativeSeekType(type);
if (::lseek(toNativeHandle(mHandle), position, SEEK_SET) == -1)
if (::lseek(nativeHandle, position, nativeSeekType) == -1)
{
throw std::runtime_error("An lseek() call failed: " + std::string(strerror(errno)));
}
@ -66,14 +65,14 @@ namespace Platform::File {
size_t size(Handle handle)
{
auto nativeHandle = getNativeHandle(handle);
auto oldPos = tell(handle);
const auto oldPos = tell(handle);
seek(handle, 0, SeekType::End);
auto size = tell(handle);
const auto fileSize = tell(handle);
seek(handle, oldPos, SeekType::Begin);
return static_cast<size_t>(size);
return static_cast<size_t>(fileSize);
}
size_t tell(Handle handle)
@ -92,7 +91,7 @@ namespace Platform::File {
{
auto nativeHandle = getNativeHandle(handle);
int amount = ::read(toNativeHandle(mHandle), data, size);
int amount = ::read(nativeHandle, data, size);
if (amount == -1)
{
throw std::runtime_error("An attempt to read " + std::to_string(size) + " bytes failed: " + strerror(errno));

View file

@ -5,6 +5,8 @@
#include <errno.h>
#include <string.h>
#include <string>
#include <stdexcept>
namespace Platform::File {
@ -26,13 +28,10 @@ namespace Platform::File {
Handle open(const char* filename)
{
// Stdio
FILE* handle = fopen(filename, "rb");
if (handle == nullptr)
{
std::ostringstream os;
os << "Failed to open '" << filename << "' for reading: " << strerror(errno);
throw std::runtime_error(os.str());
throw std::runtime_error(std::string("Failed to open '") + filename + "' for reading: " + strerror(errno));
}
return static_cast<Handle>(reinterpret_cast<intptr_t>(handle));
}
@ -49,19 +48,20 @@ namespace Platform::File {
const auto nativeSeekType = getNativeSeekType(type);
if (fseek(nativeHandle, position, nativeSeekType) != 0)
{
throw std::runtime_error("An fseek() call failed: " + std::string(strerror(errno)));
throw std::runtime_error(std::string("An fseek() call failed: ") + strerror(errno));
}
}
size_t size(Handle handle)
{
auto nativeHandle = getNativeHandle(handle);
auto oldPos = tell(handle);
const auto oldPos = tell(handle);
seek(handle, 0, SeekType::End);
auto size = tell(handle);
const auto fileSize = tell(handle);
seek(handle, oldPos, SeekType::Begin);
return static_cast<size_t>(size);
return static_cast<size_t>(fileSize);
}
size_t tell(Handle handle)
@ -71,7 +71,7 @@ namespace Platform::File {
long position = ftell(nativeHandle);
if (position == -1)
{
throw std::runtime_error("An ftell() call failed: " + std::string(strerror(errno)));
throw std::runtime_error(std::string("An ftell() call failed: ") + strerror(errno));
}
return static_cast<size_t>(position);
}
@ -83,7 +83,7 @@ namespace Platform::File {
int amount = fread(data, 1, size, nativeHandle);
if (amount == 0 && ferror(nativeHandle))
{
throw std::runtime_error("An attempt to read " + std::to_string(size) + " bytes failed: " + strerror(errno));
throw std::runtime_error(std::string("An attempt to read ") + std::to_string(size) + " bytes failed: " + strerror(errno));
}
return static_cast<size_t>(amount);
}

View file

@ -5,6 +5,7 @@
#include <components/windows.hpp>
#include <string>
#include <stdexcept>
#include <boost/locale.hpp>
namespace Platform::File {
@ -31,9 +32,7 @@ namespace Platform::File {
HANDLE handle = CreateFileW(wname.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (handle == INVALID_HANDLE_VALUE)
{
std::ostringstream os;
os << "Failed to open '" << filename << "' for reading: " << GetLastError();
throw std::runtime_error(os.str());
throw std::runtime_error(std::string("Failed to open '") + filename + "' for reading: " + std::to_string(GetLastError()));
}
return static_cast<Handle>(reinterpret_cast<intptr_t>(handle));
}
@ -53,7 +52,7 @@ namespace Platform::File {
{
if (auto errCode = GetLastError(); errCode != ERROR_SUCCESS)
{
throw std::runtime_error("An fseek() call failed: " + std::to_string(errCode));
throw std::runtime_error(std::string("An fseek() call failed: ") + std::to_string(errCode));
}
}
}
@ -93,7 +92,7 @@ namespace Platform::File {
DWORD bytesRead{};
if (!ReadFile(nativeHandle, data, static_cast<DWORD>(size), &bytesRead, nullptr))
throw std::runtime_error("A read operation on a file failed: " + std::to_string(GetLastError()));
throw std::runtime_error(std::string("A read operation on a file failed: ") + std::to_string(GetLastError()));
return bytesRead;
}