rename NativeFile -> NFile
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class NativeFile final {
|
||||
class NFile final {
|
||||
public:
|
||||
class Exception final : public nf7::Exception {
|
||||
using nf7::Exception::Exception;
|
||||
@@ -23,16 +23,16 @@ class NativeFile final {
|
||||
};
|
||||
using Flags = uint8_t;
|
||||
|
||||
NativeFile() = delete;
|
||||
NativeFile(const std::filesystem::path& path, Flags flags) :
|
||||
NFile() = delete;
|
||||
NFile(const std::filesystem::path& path, Flags flags) :
|
||||
path_(path), flags_(flags) {
|
||||
Init();
|
||||
}
|
||||
~NativeFile() noexcept;
|
||||
NativeFile(const NativeFile&) = delete;
|
||||
NativeFile(NativeFile&&) = delete;
|
||||
NativeFile& operator=(const NativeFile&) = delete;
|
||||
NativeFile& operator=(NativeFile&&) = delete;
|
||||
~NFile() noexcept;
|
||||
NFile(const NFile&) = delete;
|
||||
NFile(NFile&&) = delete;
|
||||
NFile& operator=(const NFile&) = delete;
|
||||
NFile& operator=(NFile&&) = delete;
|
||||
|
||||
size_t Read(size_t offset, uint8_t* buf, size_t size);
|
||||
size_t Write(size_t offset, const uint8_t* buf, size_t size);
|
@@ -1,4 +1,4 @@
|
||||
#include "common/native_file.hh"
|
||||
#include "common/nfile.hh"
|
||||
|
||||
extern "C" {
|
||||
#include <fcntl.h>
|
||||
@@ -11,7 +11,7 @@ extern "C" {
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
void NativeFile::Init() {
|
||||
void NFile::Init() {
|
||||
int flags = 0;
|
||||
if ((flags_ & kRead) && (flags_ & kWrite)) {
|
||||
flags |= O_RDWR | O_CREAT;
|
||||
@@ -23,45 +23,45 @@ void NativeFile::Init() {
|
||||
|
||||
int fd = open(path_.string().c_str(), flags, 0600);
|
||||
if (fd < 0) {
|
||||
throw NativeFile::Exception {"open failure"};
|
||||
throw NFile::Exception {"open failure"};
|
||||
}
|
||||
handle_ = static_cast<uint64_t>(fd);
|
||||
}
|
||||
NativeFile::~NativeFile() noexcept {
|
||||
NFile::~NFile() noexcept {
|
||||
const auto fd = static_cast<int>(handle_);
|
||||
if (close(fd) == -1) {
|
||||
// ;(
|
||||
}
|
||||
}
|
||||
|
||||
size_t NativeFile::Read(size_t offset, uint8_t* buf, size_t size) {
|
||||
size_t NFile::Read(size_t offset, uint8_t* buf, size_t size) {
|
||||
const auto fd = static_cast<int>(handle_);
|
||||
const auto off = static_cast<off_t>(offset);
|
||||
if (lseek(fd, off, SEEK_SET) == off-1) {
|
||||
throw NativeFile::Exception {"lseek failure"};
|
||||
throw NFile::Exception {"lseek failure"};
|
||||
}
|
||||
const auto ret = read(fd, buf, size);
|
||||
if (ret == -1) {
|
||||
throw NativeFile::Exception {"read failure"};
|
||||
throw NFile::Exception {"read failure"};
|
||||
}
|
||||
return static_cast<size_t>(ret);
|
||||
}
|
||||
size_t NativeFile::Write(size_t offset, const uint8_t* buf, size_t size) {
|
||||
size_t NFile::Write(size_t offset, const uint8_t* buf, size_t size) {
|
||||
const auto fd = static_cast<int>(handle_);
|
||||
const auto off = static_cast<off_t>(offset);
|
||||
if (lseek(fd, off, SEEK_SET) == off-1) {
|
||||
throw nf7::NativeFile::Exception {"lseek failure"};
|
||||
throw nf7::NFile::Exception {"lseek failure"};
|
||||
}
|
||||
const auto ret = write(fd, buf, size);
|
||||
if (ret == -1) {
|
||||
throw nf7::NativeFile::Exception {"write failure"};
|
||||
throw nf7::NFile::Exception {"write failure"};
|
||||
}
|
||||
return static_cast<size_t>(ret);
|
||||
}
|
||||
size_t NativeFile::Truncate(size_t size) {
|
||||
size_t NFile::Truncate(size_t size) {
|
||||
const auto fd = static_cast<int>(handle_);
|
||||
if (ftruncate(fd, static_cast<off_t>(size)) == 0) {
|
||||
throw nf7::NativeFile::Exception {"ftruncate failure"};
|
||||
throw nf7::NFile::Exception {"ftruncate failure"};
|
||||
}
|
||||
return size;
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
#include "common/native_file.hh"
|
||||
#include "common/nfile.hh"
|
||||
|
||||
extern "C" {
|
||||
#include <windows.h>
|
||||
@@ -7,7 +7,7 @@ extern "C" {
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
void NativeFile::Init() {
|
||||
void NFile::Init() {
|
||||
DWORD acc = 0;
|
||||
DWORD flags = 0;
|
||||
if (flags_ & kRead) {
|
||||
@@ -24,55 +24,55 @@ void NativeFile::Init() {
|
||||
path_.string().c_str(),
|
||||
acc, 0, nullptr, flags, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
throw NativeFile::Exception {"open failure"};
|
||||
throw NFile::Exception {"open failure"};
|
||||
}
|
||||
handle_ = reinterpret_cast<uintptr_t>(h);
|
||||
}
|
||||
NativeFile::~NativeFile() noexcept {
|
||||
NFile::~NFile() noexcept {
|
||||
auto h = reinterpret_cast<HANDLE>(handle_);
|
||||
if (!CloseHandle(h)) {
|
||||
// ;(
|
||||
}
|
||||
}
|
||||
|
||||
size_t NativeFile::Read(size_t offset, uint8_t* buf, size_t size) {
|
||||
size_t NFile::Read(size_t offset, uint8_t* buf, size_t size) {
|
||||
const auto h = reinterpret_cast<HANDLE>(handle_);
|
||||
|
||||
LONG off_low = offset & 0xFFFFFFFF;
|
||||
LONG off_high = offset >> 32;
|
||||
if (INVALID_SET_FILE_POINTER == SetFilePointer(h, off_low, &off_high, FILE_BEGIN)) {
|
||||
throw NativeFile::Exception {"failed to set file pointer"};
|
||||
throw NFile::Exception {"failed to set file pointer"};
|
||||
}
|
||||
DWORD ret;
|
||||
if (!ReadFile(h, buf, static_cast<DWORD>(size), &ret, nullptr)) {
|
||||
throw NativeFile::Exception {"read failure"};
|
||||
throw NFile::Exception {"read failure"};
|
||||
}
|
||||
return static_cast<size_t>(ret);
|
||||
}
|
||||
size_t NativeFile::Write(size_t offset, const uint8_t* buf, size_t size) {
|
||||
size_t NFile::Write(size_t offset, const uint8_t* buf, size_t size) {
|
||||
const auto h = reinterpret_cast<HANDLE>(handle_);
|
||||
|
||||
LONG off_low = offset & 0xFFFFFFFF;
|
||||
LONG off_high = offset >> 32;
|
||||
if (INVALID_SET_FILE_POINTER == SetFilePointer(h, off_low, &off_high, FILE_BEGIN)) {
|
||||
throw NativeFile::Exception {"failed to set file pointer"};
|
||||
throw NFile::Exception {"failed to set file pointer"};
|
||||
}
|
||||
DWORD ret;
|
||||
if (!WriteFile(h, buf, static_cast<DWORD>(size), &ret, nullptr)) {
|
||||
throw NativeFile::Exception {"read failure"};
|
||||
throw NFile::Exception {"read failure"};
|
||||
}
|
||||
return static_cast<size_t>(ret);
|
||||
}
|
||||
size_t NativeFile::Truncate(size_t size) {
|
||||
size_t NFile::Truncate(size_t size) {
|
||||
const auto h = reinterpret_cast<HANDLE>(handle_);
|
||||
|
||||
LONG off_low = size & 0xFFFFFFFF;
|
||||
LONG off_high = size >> 32;
|
||||
if (INVALID_SET_FILE_POINTER == SetFilePointer(h, off_low, &off_high, FILE_BEGIN)) {
|
||||
throw NativeFile::Exception {"failed to set file pointer"};
|
||||
throw NFile::Exception {"failed to set file pointer"};
|
||||
}
|
||||
if (!SetEndOfFile(h)) {
|
||||
throw NativeFile::Exception {"SetEndOfFile failure"};
|
||||
throw NFile::Exception {"SetEndOfFile failure"};
|
||||
}
|
||||
return size;
|
||||
}
|
Reference in New Issue
Block a user