CPP-snippets 0.0.1
A silly C++ project to use for demonstrating code integration
Loading...
Searching...
No Matches
interface.cpp
1#include <ostream>
2#include <stdexcept>
3
4#include "interface.hpp"
5
6IOInterface::IOInterface(int argc, char* argv[]) {
7
8 std::cout << " -> Initializing Interface ..." << std::endl;
9 if (argc != 1){
10 try {
11 if (!std::filesystem::exists(argv[1])) {
12 std::cerr << "[Interface] Config file not found: " << argv[1] << '\n';
13 }
14 m_input = toml::parse_file(argv[1]);
15 std::cout << "### Settings from TOML: ###\n" << m_input << std::endl;
16 }
17 catch (const toml::parse_error& err) {
18 std::cerr << "[Interface] Parse error: " << err.description()
19 << "\n at " << err.source().begin << '\n';
20 }
21 } else {
22 throw std::runtime_error("Usage: <program> <config.toml>");
23 }
24 // --- Create output directory on initialization ---
25 std::filesystem::path output_dir = m_input.get("output")->value_or("");
26 try {
27 if (!output_dir.empty()) {
28 std::filesystem::create_directories(output_dir);
29 std::cout << " -> Output directory ready: " << output_dir << std::endl;
30 }
31 else {
32 throw std::runtime_error("No output directory defined in config.toml");
33 }
34 }
35 catch (const std::filesystem::filesystem_error& e) {
36 throw std::runtime_error(
37 "Failed to create output directory '" + output_dir.string() + "': " + e.what()
38 );
39 }
40};
41
42void IOInterface::write2csv(const std::string& filename, Eigen::VectorXd& vector_data, const std::string& comment) const {
43 std::cout << " Writing " << filename << std::endl;
44
45 std::filesystem::path output_dir = m_input.get("output")->value_or("");
46 std::ofstream output_stream(output_dir / filename);
47
48 if (!output_stream.is_open()) {
49 throw std::runtime_error("Cannot open file: " + (output_dir / filename).string());
50 }
51
52 output_stream << "# data length " << vector_data.size() << comment << std::endl;
53 output_stream << std::scientific << vector_data << '\n';
54}
55
56IOInterface::~IOInterface() {
57 try {
58 std::filesystem::path output_dir = m_input.get("output")->value_or("");
59
60 if (output_dir.empty()) {
61 std::cerr << "[Interface] No output directory, cannot write config_used.toml\n";
62 return;
63 }
64
65 std::filesystem::path out_file = output_dir / "config_used.toml";
66 std::ofstream os(out_file);
67
68 if (!os.is_open()) {
69 std::cerr << "[Interface] Failed to write config_used.toml\n";
70 return;
71 }
72
73 os << m_input << "\n"; // toml++ automatically formats the table
74 os.close();
75
76 std::cout << " -> Wrote config_used.toml to " << out_file << std::endl;
77 }
78 catch (const std::exception& e) {
79 std::cerr << "[Interface] Error writing config_used.toml: " << e.what() << "\n";
80 }
81}
82
IOInterface(int argc, char *argv[])
Construct a new Interface and load the TOML configuration file.
Definition interface.cpp:6
void write2csv(const std::string &filename, const Eigen::MatrixBase< Derived > &mat_data, const std::string &comment) const
Write a matrix to a CSV file in scientific notation.
Definition interface.hpp:55