1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include "file.hpp"
#include <cassert>
#include <errno.h>
#include <fcntl.h>
#include <stdexcept>
#include <string.h>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <components/files/conversion.hpp>
namespace Platform::File
{
static auto getNativeHandle(Handle handle)
{
assert(handle != Handle::Invalid);
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 std::filesystem::path& filename)
{
#ifdef O_BINARY
static const int openFlags = O_RDONLY | O_BINARY;
#else
static const int openFlags = O_RDONLY;
#endif
auto handle = ::open(filename.c_str(), openFlags, 0);
if (handle == -1)
{
throw std::system_error(errno, std::generic_category(),
std::string("Failed to open '") + Files::pathToUnicodeString(filename) + "' for reading");
}
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(nativeHandle, position, nativeSeekType) == -1)
{
throw std::system_error(errno, std::generic_category(), "An lseek() call failed");
}
}
size_t size(Handle handle)
{
const auto oldPos = tell(handle);
seek(handle, 0, SeekType::End);
const auto fileSize = tell(handle);
seek(handle, oldPos, SeekType::Begin);
return static_cast<size_t>(fileSize);
}
size_t tell(Handle handle)
{
auto nativeHandle = getNativeHandle(handle);
size_t position = ::lseek(nativeHandle, 0, SEEK_CUR);
if (position == size_t(-1))
{
throw std::system_error(errno, std::generic_category(), "An lseek() call failed");
}
return position;
}
size_t read(Handle handle, void* data, size_t size)
{
auto nativeHandle = getNativeHandle(handle);
int amount = ::read(nativeHandle, data, size);
if (amount == -1)
{
throw std::system_error(
errno, std::generic_category(), "An attempt to read " + std::to_string(size) + " bytes failed");
}
return amount;
}
}
|