From 61db8cec2b9a89c8cc4b203506a8c0179aaee9b1 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sat, 30 Dec 2023 17:08:22 +0100 Subject: [PATCH] chore: add open_output() to file_access --- include/dwarfs/file_access.h | 5 +++++ src/dwarfs/file_access_generic.cpp | 29 ++++++++++++++++++++++++++--- test/test_helpers.h | 6 ++++++ test/test_iolayer.cpp | 21 +++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/include/dwarfs/file_access.h b/include/dwarfs/file_access.h index f1a0b14c..21c7201e 100644 --- a/include/dwarfs/file_access.h +++ b/include/dwarfs/file_access.h @@ -61,6 +61,11 @@ class file_access { open_input_binary(std::filesystem::path const& path, std::error_code& ec) const = 0; + virtual std::unique_ptr + open_output(std::filesystem::path const& path) const = 0; + virtual std::unique_ptr + open_output(std::filesystem::path const& path, std::error_code& ec) const = 0; + virtual std::unique_ptr open_output_binary(std::filesystem::path const& path) const = 0; virtual std::unique_ptr diff --git a/src/dwarfs/file_access_generic.cpp b/src/dwarfs/file_access_generic.cpp index 68803170..591827cf 100644 --- a/src/dwarfs/file_access_generic.cpp +++ b/src/dwarfs/file_access_generic.cpp @@ -70,8 +70,9 @@ class file_input_stream : public input_stream { class file_output_stream : public output_stream { public: - file_output_stream(std::filesystem::path const& path, std::error_code& ec) - : os_{path.string().c_str(), std::ios::binary | std::ios::trunc} { + file_output_stream(std::filesystem::path const& path, std::error_code& ec, + std::ios_base::openmode mode) + : os_{path.string().c_str(), mode} { if (os_.bad() || os_.fail() || !os_.is_open()) { assign_error_code(ec); } @@ -137,10 +138,32 @@ class file_access_generic : public file_access { return rv; } + std::unique_ptr + open_output(std::filesystem::path const& path, + std::error_code& ec) const override { + auto rv = std::make_unique(path, ec, std::ios::trunc); + if (ec) { + rv.reset(); + } + return rv; + } + + std::unique_ptr + open_output(std::filesystem::path const& path) const override { + std::error_code ec; + auto rv = open_output(path, ec); + if (ec) { + throw std::system_error(ec, + fmt::format("open_output('{}')", path.string())); + } + return rv; + } + std::unique_ptr open_output_binary(std::filesystem::path const& path, std::error_code& ec) const override { - auto rv = std::make_unique(path, ec); + auto rv = std::make_unique( + path, ec, std::ios::binary | std::ios::trunc); if (ec) { rv.reset(); } diff --git a/test/test_helpers.h b/test/test_helpers.h index 30cfb993..4489dcb4 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -180,6 +180,12 @@ class test_file_access : public file_access { std::unique_ptr open_input_binary(std::filesystem::path const& path) const override; + std::unique_ptr + open_output(std::filesystem::path const& path, + std::error_code& ec) const override; + std::unique_ptr + open_output(std::filesystem::path const& path) const override; + std::unique_ptr open_output_binary(std::filesystem::path const& path, std::error_code& ec) const override; diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index fda8b0db..6d9c7959 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -111,6 +111,27 @@ test_file_access::open_input_binary(std::filesystem::path const& path) const { return rv; } +std::unique_ptr +test_file_access::open_output(std::filesystem::path const& path, + std::error_code& ec) const { + auto rv = std::make_unique(path, ec, this); + if (ec) { + rv.reset(); + } + return rv; +} + +std::unique_ptr +test_file_access::open_output(std::filesystem::path const& path) const { + std::error_code ec; + auto rv = open_output(path, ec); + if (ec) { + throw std::system_error(ec, + fmt::format("open_output('{}')", path.string())); + } + return rv; +} + std::unique_ptr test_file_access::open_output_binary(std::filesystem::path const& path, std::error_code& ec) const {