From a0d17bd3c59b9cb523dcf2b40209733233633d03 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sat, 14 Oct 2023 18:59:54 +0200 Subject: [PATCH] load_string and save_string in separate files --- include/rfl/io/load_string.hpp | 29 +++++++++++++++++++++++++++++ include/rfl/io/save_string.hpp | 29 +++++++++++++++++++++++++++++ include/rfl/json/load.hpp | 16 ++-------------- include/rfl/json/save.hpp | 11 ++--------- 4 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 include/rfl/io/load_string.hpp create mode 100644 include/rfl/io/save_string.hpp diff --git a/include/rfl/io/load_string.hpp b/include/rfl/io/load_string.hpp new file mode 100644 index 00000000..4a8c28d0 --- /dev/null +++ b/include/rfl/io/load_string.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_IO_LOAD_STRING_HPP_ +#define RFL_IO_LOAD_STRING_HPP_ + +#include +#include +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace io { + +Result load_string(const std::string& _fname) { + std::ifstream infile(_fname); + if (infile.is_open()) { + auto r = std::string(std::istreambuf_iterator(infile), + std::istreambuf_iterator()); + infile.close(); + return r; + } else { + return Error("Unable to open file '" + _fname + + "' or file could not be found."); + } +} + +} // namespace io +} // namespace rfl + +#endif diff --git a/include/rfl/io/save_string.hpp b/include/rfl/io/save_string.hpp new file mode 100644 index 00000000..dcef60ed --- /dev/null +++ b/include/rfl/io/save_string.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_IO_SAVE_STRING_HPP_ +#define RFL_IO_SAVE_STRING_HPP_ + +#include +#include +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace io { + +Result save_string(const std::string& _fname, + const std::string& _str) { + try { + std::ofstream outfile; + outfile.open(_fname); + outfile << _str; + outfile.close(); + } catch (std::exception& e) { + return Error(e.what()); + } + return Nothing{}; +} + +} // namespace io +} // namespace rfl + +#endif diff --git a/include/rfl/json/load.hpp b/include/rfl/json/load.hpp index a0813c07..2f34febf 100644 --- a/include/rfl/json/load.hpp +++ b/include/rfl/json/load.hpp @@ -1,11 +1,8 @@ #ifndef RFL_JSON_LOAD_HPP_ #define RFL_JSON_LOAD_HPP_ -#include -#include -#include - #include "rfl/Result.hpp" +#include "rfl/io/load_string.hpp" #include "rfl/json/read.hpp" namespace rfl { @@ -13,16 +10,7 @@ namespace json { template Result load(const std::string& _fname) { - std::ifstream infile(_fname); - if (infile.is_open()) { - auto r = read(std::string(std::istreambuf_iterator(infile), - std::istreambuf_iterator())); - infile.close(); - return r; - } else { - return Error("Unable to open file '" + _fname + - "' or file could not be found."); - } + return rfl::io::load_string(_fname).and_then(read); } } // namespace json diff --git a/include/rfl/json/save.hpp b/include/rfl/json/save.hpp index 4dfb77f6..614f3917 100644 --- a/include/rfl/json/save.hpp +++ b/include/rfl/json/save.hpp @@ -6,6 +6,7 @@ #include #include "rfl/Result.hpp" +#include "rfl/io/save_string.hpp" #include "rfl/json/write.hpp" namespace rfl { @@ -13,15 +14,7 @@ namespace json { template Result save(const std::string& _fname, const T& _obj) { - try { - std::ofstream outfile; - outfile.open(_fname); - outfile << write(_obj); - outfile.close(); - } catch (std::exception& e) { - return Error(e.what()); - } - return Nothing{}; + return rfl::io::save_string(write(_obj)); } } // namespace json