fix compile errors on Windows
This commit is contained in:
@@ -7,119 +7,68 @@ extern "C" {
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
void NativeFile::Lock() {
|
||||
if (handle_) {
|
||||
throw nf7::Buffer::IOException("already locked");
|
||||
}
|
||||
|
||||
void NativeFile::Init() {
|
||||
DWORD acc = 0;
|
||||
if (flags_ & nf7::Buffer::kRead) {
|
||||
acc |= GENERIC_READ;
|
||||
}
|
||||
if (flags_ & nf7::Buffer::kWrite) {
|
||||
acc |= GENERIC_WRITE;
|
||||
}
|
||||
if (flags_ & kRead) acc |= GENERIC_READ;
|
||||
if (flags_ & kWrite) acc |= GENERIC_WRITE;
|
||||
|
||||
DWORD flags = 0;
|
||||
if (nflags_ & kCreateIf) {
|
||||
flags |= OPEN_ALWAYS;
|
||||
} else {
|
||||
flags |= OPEN_EXISTING;
|
||||
}
|
||||
DWORD flags = OPEN_ALWAYS;
|
||||
|
||||
HANDLE h = CreateFileA(
|
||||
path_.string().c_str(),
|
||||
acc, 0, nullptr, flags, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
throw IOException {"open failure"};
|
||||
throw NativeFile::Exception {"open failure"};
|
||||
}
|
||||
handle_ = reinterpret_cast<uintptr_t>(h);
|
||||
}
|
||||
void NativeFile::Unlock() {
|
||||
if (!handle_) {
|
||||
throw nf7::Buffer::IOException("not locked yet");
|
||||
}
|
||||
auto h = reinterpret_cast<HANDLE>(*handle_);
|
||||
NativeFile::~NativeFile() noexcept {
|
||||
auto h = reinterpret_cast<HANDLE>(handle_);
|
||||
if (!CloseHandle(h)) {
|
||||
throw nf7::Buffer::IOException("close failure");
|
||||
// ;(
|
||||
}
|
||||
handle_ = std::nullopt;
|
||||
}
|
||||
|
||||
size_t NativeFile::Read(size_t offset, uint8_t* buf, size_t size) {
|
||||
if (!handle_) {
|
||||
throw nf7::Buffer::IOException("not locked yet");
|
||||
}
|
||||
const auto h = reinterpret_cast<HANDLE>(*handle_);
|
||||
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 nf7::Buffer::IOException("failed to set file pointer");
|
||||
throw NativeFile::Exception {"failed to set file pointer"};
|
||||
}
|
||||
DWORD ret;
|
||||
if (!ReadFile(h, buf, static_cast<DWORD>(size), &ret, nullptr)) {
|
||||
throw nf7::Buffer::IOException("read failure");
|
||||
throw NativeFile::Exception {"read failure"};
|
||||
}
|
||||
return static_cast<size_t>(ret);
|
||||
}
|
||||
size_t NativeFile::Write(size_t offset, const uint8_t* buf, size_t size) {
|
||||
if (!handle_) {
|
||||
throw nf7::Buffer::IOException("not locked yet");
|
||||
}
|
||||
const auto h = reinterpret_cast<HANDLE>(*handle_);
|
||||
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 nf7::Buffer::IOException("failed to set file pointer");
|
||||
throw NativeFile::Exception {"failed to set file pointer"};
|
||||
}
|
||||
DWORD ret;
|
||||
if (!WriteFile(h, buf, static_cast<DWORD>(size), &ret, nullptr)) {
|
||||
throw nf7::Buffer::IOException("read failure");
|
||||
throw NativeFile::Exception {"read failure"};
|
||||
}
|
||||
return static_cast<size_t>(ret);
|
||||
}
|
||||
size_t NativeFile::Truncate(size_t size) {
|
||||
if (!handle_) {
|
||||
throw nf7::Buffer::IOException("not locked yet");
|
||||
}
|
||||
const auto h = reinterpret_cast<HANDLE>(*handle_);
|
||||
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 nf7::Buffer::IOException("failed to set file pointer");
|
||||
throw NativeFile::Exception {"failed to set file pointer"};
|
||||
}
|
||||
if (!SetEndOfFile(h)) {
|
||||
throw nf7::Buffer::IOException("SetEndOfFile failure");
|
||||
throw NativeFile::Exception {"SetEndOfFile failure"};
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t NativeFile::size() const {
|
||||
if (!handle_) {
|
||||
throw nf7::Buffer::IOException("not locked yet");
|
||||
}
|
||||
const auto h = reinterpret_cast<HANDLE>(*handle_);
|
||||
|
||||
DWORD high;
|
||||
DWORD low = GetFileSize(h, &high);
|
||||
if (low == INVALID_FILE_SIZE) {
|
||||
throw nf7::Buffer::IOException("GetFileSize failure");
|
||||
}
|
||||
return (static_cast<size_t>(high) << 32) | low;
|
||||
}
|
||||
|
||||
void NativeFile::CleanUp() noexcept {
|
||||
}
|
||||
void NativeFile::Abort() noexcept {
|
||||
}
|
||||
size_t NativeFile::GetMemoryUsage() const noexcept {
|
||||
return 0;
|
||||
}
|
||||
std::string NativeFile::GetDescription() const noexcept {
|
||||
return path_.string() + (handle_? " (active)": " (inactive)");
|
||||
}
|
||||
|
||||
} // namespace nf7
|
||||
|
@@ -61,7 +61,7 @@ class Dir final : public nf7::FileBase,
|
||||
try {
|
||||
ar(f);
|
||||
items_[name] = std::move(f);
|
||||
} catch (nf7::Exception& e) {
|
||||
} catch (nf7::Exception&) {
|
||||
env().Throw(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user