mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-12 22:10:54 -04:00
chore: add throwing file_access methods for convenience
This commit is contained in:
parent
ac3248f966
commit
619abd8dc1
@ -49,11 +49,20 @@ class file_access {
|
||||
virtual ~file_access() = default;
|
||||
|
||||
virtual bool exists(std::filesystem::path const& path) const = 0;
|
||||
|
||||
virtual std::unique_ptr<input_stream>
|
||||
open_input(std::filesystem::path const& path) const = 0;
|
||||
virtual std::unique_ptr<input_stream>
|
||||
open_input(std::filesystem::path const& path, std::error_code& ec) const = 0;
|
||||
|
||||
virtual std::unique_ptr<input_stream>
|
||||
open_input_binary(std::filesystem::path const& path) const = 0;
|
||||
virtual std::unique_ptr<input_stream>
|
||||
open_input_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const = 0;
|
||||
|
||||
virtual std::unique_ptr<output_stream>
|
||||
open_output_binary(std::filesystem::path const& path) const = 0;
|
||||
virtual std::unique_ptr<output_stream>
|
||||
open_output_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const = 0;
|
||||
|
@ -23,6 +23,8 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <folly/portability/Windows.h>
|
||||
#endif
|
||||
@ -103,6 +105,17 @@ class file_access_generic : public file_access {
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<input_stream>
|
||||
open_input(std::filesystem::path const& path) const override {
|
||||
std::error_code ec;
|
||||
auto rv = open_input(path, ec);
|
||||
if (ec) {
|
||||
throw std::system_error(ec,
|
||||
fmt::format("open_input('{}')", path.string()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<input_stream>
|
||||
open_input_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const override {
|
||||
@ -113,6 +126,17 @@ class file_access_generic : public file_access {
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<input_stream>
|
||||
open_input_binary(std::filesystem::path const& path) const override {
|
||||
std::error_code ec;
|
||||
auto rv = open_input_binary(path, ec);
|
||||
if (ec) {
|
||||
throw std::system_error(
|
||||
ec, fmt::format("open_input_binary('{}')", path.string()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<output_stream>
|
||||
open_output_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const override {
|
||||
@ -122,6 +146,17 @@ class file_access_generic : public file_access {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<output_stream>
|
||||
open_output_binary(std::filesystem::path const& path) const override {
|
||||
std::error_code ec;
|
||||
auto rv = open_output_binary(path, ec);
|
||||
if (ec) {
|
||||
throw std::system_error(
|
||||
ec, fmt::format("open_output_binary('{}')", path.string()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
@ -168,14 +168,23 @@ class test_terminal : public terminal {
|
||||
class test_file_access : public file_access {
|
||||
public:
|
||||
bool exists(std::filesystem::path const& path) const override;
|
||||
|
||||
std::unique_ptr<input_stream> open_input(std::filesystem::path const& path,
|
||||
std::error_code& ec) const override;
|
||||
std::unique_ptr<input_stream>
|
||||
open_input(std::filesystem::path const& path) const override;
|
||||
|
||||
std::unique_ptr<input_stream>
|
||||
open_input_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const override;
|
||||
std::unique_ptr<input_stream>
|
||||
open_input_binary(std::filesystem::path const& path) const override;
|
||||
|
||||
std::unique_ptr<output_stream>
|
||||
open_output_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const override;
|
||||
std::unique_ptr<output_stream>
|
||||
open_output_binary(std::filesystem::path const& path) const override;
|
||||
|
||||
void set_file(std::filesystem::path const& path, std::string contents) const;
|
||||
std::optional<std::string> get_file(std::filesystem::path const& path) const;
|
||||
|
@ -84,12 +84,33 @@ test_file_access::open_input(std::filesystem::path const& path,
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<input_stream>
|
||||
test_file_access::open_input(std::filesystem::path const& path) const {
|
||||
std::error_code ec;
|
||||
auto rv = open_input(path, ec);
|
||||
if (ec) {
|
||||
throw std::system_error(ec, fmt::format("open_input('{}')", path.string()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<input_stream>
|
||||
test_file_access::open_input_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const {
|
||||
return open_input(path, ec);
|
||||
}
|
||||
|
||||
std::unique_ptr<input_stream>
|
||||
test_file_access::open_input_binary(std::filesystem::path const& path) const {
|
||||
std::error_code ec;
|
||||
auto rv = open_input_binary(path, ec);
|
||||
if (ec) {
|
||||
throw std::system_error(
|
||||
ec, fmt::format("open_input_binary('{}')", path.string()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<output_stream>
|
||||
test_file_access::open_output_binary(std::filesystem::path const& path,
|
||||
std::error_code& ec) const {
|
||||
@ -100,6 +121,17 @@ test_file_access::open_output_binary(std::filesystem::path const& path,
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::unique_ptr<output_stream>
|
||||
test_file_access::open_output_binary(std::filesystem::path const& path) const {
|
||||
std::error_code ec;
|
||||
auto rv = open_output_binary(path, ec);
|
||||
if (ec) {
|
||||
throw std::system_error(
|
||||
ec, fmt::format("open_output_binary('{}')", path.string()));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
void test_file_access::set_file(std::filesystem::path const& path,
|
||||
std::string content) const {
|
||||
files_[path] = std::move(content);
|
||||
|
Loading…
x
Reference in New Issue
Block a user