13 void write_csv_element<std::string>(std::ofstream& file,
const std::string& value) {
14 file <<
"\"" << value <<
"\"";
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)) {
22 : file_path(std::move(file_path)), append(append), separator(std::move(separator)) {}
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);
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);
48 if (!std::filesystem::exists(
file_path)) {
49 std::filesystem::create_directories(
file_path.parent_path());
60 if (!
file.is_open()) {
62 throw std::runtime_error(
"Could not open file for writing!");
71 if (row.size() !=
headers.size()) {
73 throw std::runtime_error(
"Row size does not match cols size!");
76 for (
size_t i = 0; i < row.size(); i++) {
void write_csv_element(std::ofstream &file, const T &value)
Writes a single element to the CSV file.
std::string separator
The separator to use between values.
CSVWriter & operator=(CSVWriter &)=delete
void initialize(const std::vector< std::string > &headers)
Initializes the CSV file and writes the header row.
bool append
Whether to append to the CSV file or overwrite it.
void writeRow(const std::vector< serializable_types > &row)
Writes a row to the CSV file.
std::vector< std::string > headers
The headers of the CSV file.
std::ofstream file
The file stream to write to.
CSVWriter(std::filesystem::path file_path, const std::vector< std::string > &headers, bool append, std::string separator=";")
Creates a new CSVWriter instance.
std::filesystem::path file_path
The path to the CSV file to write to.
static std::shared_ptr< spdlog::logger > logger
Publically accessible shared pointer to the logger.