update io::Device::read
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
|
||||
#include "../path.hpp"
|
||||
@@ -13,7 +15,7 @@ namespace io {
|
||||
virtual std::filesystem::path resolve(std::string_view path) = 0;
|
||||
|
||||
virtual void write(std::string_view path, const void* data, size_t size) = 0;
|
||||
virtual void read(std::string_view path, void* data, size_t size) = 0;
|
||||
virtual std::unique_ptr<std::istream> read(std::string_view path) = 0;
|
||||
|
||||
virtual size_t size(std::string_view path) = 0;
|
||||
|
||||
@@ -43,8 +45,8 @@ namespace io {
|
||||
parent->write((root / path).pathPart(), data, size);
|
||||
}
|
||||
|
||||
void read(std::string_view path, void* data, size_t size) override {
|
||||
parent->read((root / path).pathPart(), data, size);
|
||||
std::unique_ptr<std::istream> read(std::string_view path) override {
|
||||
return parent->read((root / path).pathPart());
|
||||
}
|
||||
|
||||
size_t size(std::string_view path) override {
|
||||
|
||||
@@ -35,13 +35,13 @@ void StdfsDevice::write(std::string_view path, const void* data, size_t size) {
|
||||
output.write((const char*)data, size);
|
||||
}
|
||||
|
||||
void StdfsDevice::read(std::string_view path, void* data, size_t size) {
|
||||
std::unique_ptr<std::istream> StdfsDevice::read(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
std::ifstream input(resolved, std::ios::binary);
|
||||
if (!input.is_open()) {
|
||||
auto input = std::make_unique<std::ifstream>(resolved, std::ios::binary);
|
||||
if (!*input) {
|
||||
throw std::runtime_error("could not to open file " + resolved.u8string());
|
||||
}
|
||||
input.read((char*)data, size);
|
||||
return input;
|
||||
}
|
||||
|
||||
size_t StdfsDevice::size(std::string_view path) {
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace io {
|
||||
|
||||
std::filesystem::path resolve(std::string_view path) override;
|
||||
void write(std::string_view path, const void* data, size_t size) override;
|
||||
void read(std::string_view path, void* data, size_t size) override;
|
||||
std::unique_ptr<std::istream> read(std::string_view path) override;
|
||||
size_t size(std::string_view path) override;
|
||||
bool exists(std::string_view path) override;
|
||||
bool isdir(std::string_view path) override;
|
||||
|
||||
Reference in New Issue
Block a user