mirror of https://github.com/OpenMW/openmw.git
Move platform specific file code into platform and cleanup LowLevelFile
parent
2ef2e93a46
commit
1b70ff775d
@ -0,0 +1,35 @@
|
||||
#ifndef OPENMW_COMPONENTS_PLATFORM_FILE_HPP
|
||||
#define OPENMW_COMPONENTS_PLATFORM_FILE_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string_view>
|
||||
|
||||
namespace Platform::File {
|
||||
|
||||
enum class Handle : intptr_t
|
||||
{
|
||||
Invalid = -1
|
||||
};
|
||||
|
||||
enum class SeekType
|
||||
{
|
||||
Begin,
|
||||
Current,
|
||||
End
|
||||
};
|
||||
|
||||
Handle open(const char* filename);
|
||||
|
||||
void close(Handle handle);
|
||||
|
||||
size_t size(Handle handle);
|
||||
|
||||
void seek(Handle handle, size_t Position, SeekType type = SeekType::Begin);
|
||||
size_t tell(Handle handle);
|
||||
|
||||
size_t read(Handle handle, void* data, size_t size);
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif // OPENMW_COMPONENTS_PLATFORM_FILE_HPP
|
@ -0,0 +1,105 @@
|
||||
#include "platform.internal.hpp"
|
||||
#include "file.hpp"
|
||||
|
||||
#if PLATFORM_TYPE == PLATFORM_TYPE_POSIX
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Platform::File {
|
||||
|
||||
static auto getNativeHandle(Handle handle)
|
||||
{
|
||||
return static_cast<int>(handle);
|
||||
}
|
||||
|
||||
static int getNativeSeekType(SeekType seek)
|
||||
{
|
||||
if (seek == SeekType::Begin)
|
||||
return SEEK_SET;
|
||||
if (seek == SeekType::Current)
|
||||
return SEEK_CUR;
|
||||
if (seek == SeekType::End)
|
||||
return SEEK_END;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Handle open(const char* filename)
|
||||
{
|
||||
// Posix
|
||||
#ifdef O_BINARY
|
||||
static const int openFlags = O_RDONLY | O_BINARY;
|
||||
#else
|
||||
static const int openFlags = O_RDONLY;
|
||||
#endif
|
||||
|
||||
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());
|
||||
}
|
||||
return static_cast<Handle>(handle);
|
||||
}
|
||||
|
||||
void close(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
::close(nativeHandle);
|
||||
}
|
||||
|
||||
void seek(Handle handle, size_t position, SeekType type /*= SeekType::Begin*/)
|
||||
{
|
||||
const auto nativeHandle = getNativeHandle(handle);
|
||||
const auto nativeSeekType = getNativeSeekType(type);
|
||||
|
||||
if (::lseek(toNativeHandle(mHandle), position, SEEK_SET) == -1)
|
||||
{
|
||||
throw std::runtime_error("An lseek() call failed: " + std::string(strerror(errno)));
|
||||
}
|
||||
}
|
||||
|
||||
size_t size(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
auto oldPos = tell(handle);
|
||||
|
||||
seek(handle, 0, SeekType::End);
|
||||
auto size = tell(handle);
|
||||
|
||||
return static_cast<size_t>(size);
|
||||
}
|
||||
|
||||
size_t tell(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
size_t position = ::lseek(nativeHandle, 0, SEEK_CUR);
|
||||
if (position == size_t(-1))
|
||||
{
|
||||
throw std::runtime_error("An lseek() call failed: " + std::string(strerror(errno)));
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
size_t read(Handle handle, void* data, size_t size)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
int amount = ::read(toNativeHandle(mHandle), data, size);
|
||||
if (amount == -1)
|
||||
{
|
||||
throw std::runtime_error("An attempt to read " + std::to_string(size) + " bytes failed: " + strerror(errno));
|
||||
}
|
||||
return amount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,93 @@
|
||||
#include "platform.internal.hpp"
|
||||
#include "file.hpp"
|
||||
|
||||
#if PLATFORM_TYPE == PLATFORM_TYPE_STDIO
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Platform::File {
|
||||
|
||||
static auto getNativeHandle(Handle handle)
|
||||
{
|
||||
return reinterpret_cast<FILE*>(static_cast<intptr_t>(handle));
|
||||
}
|
||||
|
||||
static int getNativeSeekType(SeekType seek)
|
||||
{
|
||||
if (seek == SeekType::Begin)
|
||||
return SEEK_SET;
|
||||
if (seek == SeekType::Current)
|
||||
return SEEK_CUR;
|
||||
if (seek == SeekType::End)
|
||||
return SEEK_END;
|
||||
return -1;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
return static_cast<Handle>(reinterpret_cast<intptr_t>(handle));
|
||||
}
|
||||
|
||||
void close(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
fclose(nativeHandle);
|
||||
}
|
||||
|
||||
void seek(Handle handle, size_t position, SeekType type /*= SeekType::Begin*/)
|
||||
{
|
||||
const auto nativeHandle = getNativeHandle(handle);
|
||||
const auto nativeSeekType = getNativeSeekType(type);
|
||||
if (fseek(nativeHandle, position, nativeSeekType) != 0)
|
||||
{
|
||||
throw std::runtime_error("An fseek() call failed: " + std::string(strerror(errno)));
|
||||
}
|
||||
}
|
||||
|
||||
size_t size(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
auto oldPos = tell(handle);
|
||||
|
||||
seek(handle, 0, SeekType::End);
|
||||
auto size = tell(handle);
|
||||
|
||||
return static_cast<size_t>(size);
|
||||
}
|
||||
|
||||
size_t tell(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
long position = ftell(nativeHandle);
|
||||
if (position == -1)
|
||||
{
|
||||
throw std::runtime_error("An ftell() call failed: " + std::string(strerror(errno)));
|
||||
}
|
||||
return static_cast<size_t>(position);
|
||||
}
|
||||
|
||||
size_t read(Handle handle, void* data, size_t size)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
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));
|
||||
}
|
||||
return static_cast<size_t>(amount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,102 @@
|
||||
#include "platform.internal.hpp"
|
||||
#include "file.hpp"
|
||||
|
||||
#if PLATFORM_TYPE == PLATFORM_TYPE_WIN32
|
||||
|
||||
#include <components/windows.hpp>
|
||||
#include <string>
|
||||
#include <boost/locale.hpp>
|
||||
|
||||
namespace Platform::File {
|
||||
|
||||
static auto getNativeHandle(Handle handle)
|
||||
{
|
||||
return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle));
|
||||
}
|
||||
|
||||
static int getNativeSeekType(SeekType seek)
|
||||
{
|
||||
if (seek == SeekType::Begin)
|
||||
return FILE_BEGIN;
|
||||
if (seek == SeekType::Current)
|
||||
return FILE_CURRENT;
|
||||
if (seek == SeekType::End)
|
||||
return FILE_END;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Handle open(const char* filename)
|
||||
{
|
||||
std::wstring wname = boost::locale::conv::utf_to_utf<wchar_t>(filename);
|
||||
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());
|
||||
}
|
||||
return static_cast<Handle>(reinterpret_cast<intptr_t>(handle));
|
||||
}
|
||||
|
||||
void close(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
CloseHandle(nativeHandle);
|
||||
}
|
||||
|
||||
void seek(Handle handle, size_t position, SeekType type /*= SeekType::Begin*/)
|
||||
{
|
||||
const auto nativeHandle = getNativeHandle(handle);
|
||||
const auto nativeSeekType = getNativeSeekType(type);
|
||||
|
||||
if (SetFilePointer(nativeHandle, static_cast<LONG>(position), nullptr, nativeSeekType) == INVALID_SET_FILE_POINTER)
|
||||
{
|
||||
if (auto errCode = GetLastError(); errCode != ERROR_SUCCESS)
|
||||
{
|
||||
throw std::runtime_error("An fseek() call failed: " + std::to_string(errCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t size(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
assert(isValidHandle(mHandle));
|
||||
|
||||
BY_HANDLE_FILE_INFORMATION info;
|
||||
|
||||
if (!GetFileInformationByHandle(nativeHandle, &info))
|
||||
throw std::runtime_error("A query operation on a file failed.");
|
||||
|
||||
if (info.nFileSizeHigh != 0)
|
||||
throw std::runtime_error("Files greater that 4GB are not supported.");
|
||||
|
||||
return info.nFileSizeLow;
|
||||
}
|
||||
|
||||
size_t tell(Handle handle)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
DWORD value = SetFilePointer(nativeHandle, 0, nullptr, SEEK_CUR);
|
||||
if (value == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
|
||||
throw std::runtime_error("A query operation on a file failed.");
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
size_t read(Handle handle, void* data, size_t size)
|
||||
{
|
||||
auto nativeHandle = getNativeHandle(handle);
|
||||
|
||||
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()));
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,19 @@
|
||||
#ifndef COMPONENT_PLATFORM_FILE_HPP
|
||||
#define COMPONENT_PLATFORM_FILE_HPP
|
||||
|
||||
namespace Platform
|
||||
{
|
||||
#define PLATFORM_TYPE_STDIO 0
|
||||
#define PLATFORM_TYPE_WIN32 1
|
||||
#define PLATFORM_TYPE_POSIX 2
|
||||
|
||||
#if defined(__linux) || defined(__unix) || defined(__posix)
|
||||
#define PLATFORM_TYPE PLATFORM_TYPE_POSIX
|
||||
#elif defined(_WIN32)
|
||||
#define PLATFORM_TYPE PLATFORM_TYPE_WIN32
|
||||
#else
|
||||
#define PLATFORM_TYPE PLATFORM_TYPE_STDIO
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue