Sequoia
Loading...
Searching...
No Matches
Streaming.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2021. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
15
16#include <filesystem>
17#include <optional>
18
19namespace sequoia
20{
21 [[nodiscard]]
22 std::string report_failed_read(const std::filesystem::path& file);
23
24 [[nodiscard]]
25 std::string report_failed_write(const std::filesystem::path& file);
26
27
28 [[nodiscard]]
29 std::optional<std::string> read_to_string(const std::filesystem::path& file);
30
31 void write_to_file(const std::filesystem::path& file, std::string_view text, std::ios_base::openmode mode=std::ios_base::out);
32
33 template<std::invocable<std::string&> Fn>
34 void read_modify_write(const std::filesystem::path& file, Fn fn)
35 {
36 if(auto text{read_to_string(file)})
37 {
38 fn(*text);
39 write_to_file(file, *text);
40 }
41 else
42 {
43 throw std::runtime_error{report_failed_read(file)};
44 }
45 }
46}
Concepts which are sufficiently general to appear in the sequoia namespace.