Molecular Dynamics Simulation  1.0
CSVWriter.cpp
Go to the documentation of this file.
1 #include "CSVWriter.h"
2 
3 #include <utility>
4 
5 #include "io/logger/Logger.h"
6 
7 template <typename T>
8 void write_csv_element(std::ofstream& file, const T& value) {
9  file << value;
10 }
11 
12 template <>
13 void write_csv_element<std::string>(std::ofstream& file, const std::string& value) {
14  file << "\"" << value << "\"";
15 }
16 
17 CSVWriter::CSVWriter(std::filesystem::path file_path, const std::vector<std::string>& headers, bool append, std::string separator)
18  : file_path(std::move(file_path)), append(append), headers(headers), separator(std::move(separator)) {
20 }
21 CSVWriter::CSVWriter(std::filesystem::path file_path, bool append, std::string separator)
22  : file_path(std::move(file_path)), append(append), separator(std::move(separator)) {}
23 
25  this->file_path = std::move(rhs.file_path);
26  this->headers = std::move(rhs.headers);
27  this->separator = std::move(rhs.separator);
28  this->file = std::move(rhs.file);
29 }
31  this->file_path = std::move(rhs.file_path);
32  this->headers = std::move(rhs.headers);
33  this->separator = std::move(rhs.separator);
34  this->file = std::move(rhs.file);
35  return *this;
36 }
37 
39  Logger::logger->debug("Closing CSVWriter for file {}...", file_path.string());
40  file.close();
41 }
42 
43 void CSVWriter::initialize(const std::vector<std::string>& headers) {
44  Logger::logger->debug("Creating CSVWriter for file {}...", file_path.string());
45 
46  this->headers = headers;
47 
48  if (!std::filesystem::exists(file_path)) {
49  std::filesystem::create_directories(file_path.parent_path());
50  }
51 
52  if (append) {
53  Logger::logger->warn("Appending to file {}...", file_path.string());
54  file.open(file_path, std::ios_base::app);
55  } else {
56  Logger::logger->warn("Overwriting file {}...", file_path.string());
57  file.open(file_path, std::ios_base::out);
58  }
59 
60  if (!file.is_open()) {
61  Logger::logger->error("Could not open file {} for writing!", file_path.string());
62  throw std::runtime_error("Could not open file for writing!");
63  }
64 
65  if (!append) {
66  writeRow({headers.begin(), headers.end()});
67  }
68 }
69 
70 void CSVWriter::writeRow(const std::vector<CSVWriter::serializable_types>& row) {
71  if (row.size() != headers.size()) {
72  Logger::logger->error("Row size ({}) does not match cols size ({})!", row.size(), headers.size());
73  throw std::runtime_error("Row size does not match cols size!");
74  }
75 
76  for (size_t i = 0; i < row.size(); i++) {
77  if (i > 0) {
78  file << separator;
79  }
80  std::visit([&](auto&& arg) { write_csv_element(file, arg); }, row[i]);
81  }
82  file << std::endl;
83 }
void write_csv_element(std::ofstream &file, const T &value)
Writes a single element to the CSV file.
Definition: CSVWriter.cpp:8
std::string separator
The separator to use between values.
Definition: CSVWriter.h:104
CSVWriter & operator=(CSVWriter &)=delete
void initialize(const std::vector< std::string > &headers)
Initializes the CSV file and writes the header row.
Definition: CSVWriter.cpp:43
bool append
Whether to append to the CSV file or overwrite it.
Definition: CSVWriter.h:94
void writeRow(const std::vector< serializable_types > &row)
Writes a row to the CSV file.
Definition: CSVWriter.cpp:70
std::vector< std::string > headers
The headers of the CSV file.
Definition: CSVWriter.h:99
std::ofstream file
The file stream to write to.
Definition: CSVWriter.h:84
CSVWriter(std::filesystem::path file_path, const std::vector< std::string > &headers, bool append, std::string separator=";")
Creates a new CSVWriter instance.
Definition: CSVWriter.cpp:17
std::filesystem::path file_path
The path to the CSV file to write to.
Definition: CSVWriter.h:89
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.
Definition: Logger.h:35