4#ifndef ADAPTYST_OUTPUT_HPP_
5#define ADAPTYST_OUTPUT_HPP_
8#include <nlohmann/json.hpp>
13 namespace fs = std::filesystem;
25 this->metadata = nlohmann::json::object();
39 if (this->metadata[key] != value) {
40 this->metadata[key] = value;
60 return this->metadata.value(key, default_value);
73 return this->metadata[key].get<T>();
93 void setup(fs::path path) {
94 this->path = fs::absolute(path);
96 fs::create_directories(this->path);
97 }
catch (std::exception &e) {
98 throw std::runtime_error(
"Could not create directory " +
99 this->path.string() +
": " +
100 std::string(e.what()));
103 fs::path metadata_path = this->path /
"dirmeta.json";
105 if (fs::exists(metadata_path)) {
106 std::ifstream metadata_stream(metadata_path);
108 if (!metadata_stream) {
109 throw std::runtime_error(
"Could not open " +
110 (this->path /
"dirmeta.json").
string() +
114 std::string json_str((std::istreambuf_iterator<char>(metadata_stream)),
115 std::istreambuf_iterator<char>());
116 this->
metadata = nlohmann::json::parse(json_str);
136 return this->path.c_str();
145 return Path(this->path / second);
155 return Path(this->path / second);
162 std::ofstream metadata_stream(this->path /
"dirmeta.json");
164 if (!metadata_stream) {
165 throw std::runtime_error(
"Could not open " +
166 (this->path /
"dirmeta.json").
string() +
170 metadata_stream << this->
metadata.dump() << std::endl;
197 std::string extension =
"",
198 bool truncate =
true) :
path(
path) {
201 fs::path new_path =
path.path / (
name + extension);
203 if (fs::exists(new_path)) {
204 if (fs::is_directory(new_path)) {
205 throw std::runtime_error(new_path.string() +
" is "
208 this->istream = std::ifstream(new_path);
212 this->ostream = std::ofstream(new_path, std::ios_base::out |
214 std::ios_base::trunc : std::ios_base::app));
216 if (!this->ostream) {
217 throw std::runtime_error(
"Could not open " +
222 fs::path metadata_path =
path.path / (
"meta_" + this->name +
".json");
224 if (fs::exists(metadata_path)) {
225 std::ifstream metadata_stream(metadata_path);
227 if (!metadata_stream) {
228 throw std::runtime_error(
"Could not open " +
229 (
path.path / (
"meta_" + this->name +
".json")).string() +
233 std::string json_str((std::istreambuf_iterator<char>(metadata_stream)),
234 std::istreambuf_iterator<char>());
235 this->
metadata = nlohmann::json::parse(json_str);
240 this->path = source.path;
241 this->name = source.name;
242 this->istream = std::move(source.istream);
243 this->ostream = std::move(source.ostream);
244 this->
metadata.swap(source.metadata);
254 return this->istream;
263 return this->ostream;
270 fs::path metadata_path =
path.path / (
"meta_" + this->name +
".json");
272 std::ofstream metadata_stream(metadata_path);
274 if (!metadata_stream) {
275 throw std::runtime_error(
"Could not open " + metadata_path.string() +
279 metadata_stream << this->
metadata.dump() << std::endl;
285 typename T::first_type;
286 typename T::second_type;
287 } && std::is_same_v<T, std::pair<
typename T::first_type,
288 typename T::second_type> >;
311 if constexpr (is_pair<T>) {
312 this->istream >> val.first;
313 this->istream >> val.second;
315 this->istream >> val;
332 return this->vec[index];
341 return this->vec.size();
351 this->vec.push_back(val);
354 this->
ostream << val.first <<
" " << val.second << std::endl;
356 this->
ostream << val << std::endl;
T operator[](int index)
Definition output.hpp:331
void push_back(T val)
Definition output.hpp:350
int size()
Definition output.hpp:340
Array(Path &path, std::string name)
Definition output.hpp:307
File & operator=(File &&source)
Definition output.hpp:239
File(Path &path, std::string name, std::string extension="", bool truncate=true)
Definition output.hpp:196
std::ofstream ostream
Definition output.hpp:184
std::string name
Definition output.hpp:182
std::ifstream & get_istream()
Definition output.hpp:253
Path & path
Definition output.hpp:181
std::ofstream & get_ostream()
Definition output.hpp:262
void save_metadata()
Definition output.hpp:269
std::ifstream istream
Definition output.hpp:183
void save_metadata()
Definition output.hpp:161
const char * get_path_name()
Definition output.hpp:135
Path operator/(const char *second)
Definition output.hpp:154
friend class File
Definition output.hpp:88
Path(fs::path path)
Definition output.hpp:126
Path operator/(std::string second)
Definition output.hpp:144
Definition output.hpp:284