Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
csv.h
Go to the documentation of this file.
1/* Brush
2copyright 2026 William La Cava
3license: GNU/GPL v3
4*/
5
6#ifndef CSV_H
7#define CSV_H
8
9#include <cstdio>
10#include <fstream>
11#include <memory>
12#include <sstream>
13#include <string>
14#include <type_traits>
15#include <vector>
16
17#include "error.h"
18
19namespace Brush { namespace Util {
20
34{
35public:
36 CsvWriter() = default;
37
38 CsvWriter(const std::string& path, const std::vector<std::string>& header)
39 {
40 open(path, header);
41 }
42
43 void open(const std::string& path, const std::vector<std::string>& header)
44 {
45 close();
46
47 n_columns = header.size();
48 const std::string header_line = format_row(header);
49
50 std::string existing;
51 {
52 std::ifstream in(path);
53 if (in.is_open())
54 std::getline(in, existing);
55 }
56
57 if (!existing.empty() && existing != header_line)
59 "Log file '" + path + "' already exists with different columns.\n"
60 " expected: " + header_line + "\n"
61 " found : " + existing + "\n"
62 "Use a different logfile or remove the old one.");
63
64 out = std::make_shared<std::ofstream>(path, std::ofstream::app);
65 if (!out->is_open())
66 HANDLE_ERROR_THROW("Failed to open log file: " + path);
67
68 if (existing.empty())
69 *out << header_line << '\n' << std::flush;
70 }
71
72 bool is_open() const { return out && out->is_open(); }
73
74 void close()
75 {
76 if (is_open())
77 out->close();
78 out.reset();
79 }
80
81 void write_row(const std::vector<std::string>& fields)
82 {
83 if (!is_open())
84 HANDLE_ERROR_THROW("CsvWriter: writing to a closed file");
85
86 if (fields.size() != n_columns)
88 "CsvWriter: row has " + std::to_string(fields.size())
89 + " fields, header has " + std::to_string(n_columns));
90
91 *out << format_row(fields) << '\n' << std::flush;
92 }
93
95 static std::string escape(const std::string& field)
96 {
97 if (field.find_first_of(",\"\n\r") == std::string::npos)
98 return field;
99
100 std::string quoted = "\"";
101 for (char c : field)
102 {
103 if (c == '"')
104 quoted += '"';
105 quoted += c;
106 }
107 quoted += '"';
108 return quoted;
109 }
110
112 template <typename V>
113 static std::string field(const V& value)
114 {
115 if constexpr (std::is_same_v<V, bool>)
116 return value ? "1" : "0";
117 else if constexpr (std::is_floating_point_v<V>)
118 {
119 char buf[32];
120 std::snprintf(buf, sizeof(buf), "%.9g", static_cast<double>(value));
121 return buf;
122 }
123 else if constexpr (std::is_arithmetic_v<V>)
124 return std::to_string(value);
125 else
126 {
127 std::ostringstream ss;
128 ss << value;
129 return ss.str();
130 }
131 }
132
133private:
134 static std::string format_row(const std::vector<std::string>& fields)
135 {
136 std::string line;
137 for (size_t i = 0; i < fields.size(); ++i)
138 {
139 if (i > 0)
140 line += ',';
141 line += escape(fields[i]);
142 }
143 return line;
144 }
145
146 // shared so that the owner (e.g. Engine) stays copyable
147 std::shared_ptr<std::ofstream> out;
148 size_t n_columns = 0;
149};
150
151} } // Util, Brush
152
153#endif
static std::string format_row(const std::vector< std::string > &fields)
Definition csv.h:134
bool is_open() const
Definition csv.h:72
std::shared_ptr< std::ofstream > out
Definition csv.h:147
static std::string escape(const std::string &field)
quotes a field if it contains a separator, a quote or a line break
Definition csv.h:95
void write_row(const std::vector< std::string > &fields)
Definition csv.h:81
static std::string field(const V &value)
converts a value to a field. Floats use enough digits to round-trip.
Definition csv.h:113
CsvWriter(const std::string &path, const std::vector< std::string > &header)
Definition csv.h:38
void open(const std::string &path, const std::vector< std::string > &header)
Definition csv.h:43
#define HANDLE_ERROR_THROW(err)
Definition error.h:27
namespace containing various utility functions
Definition csv.h:19
bool in(const V &v, const T &i)
check if element is in vector.
Definition utils.h:192
< nsga2 selection operator for getting the front
Definition bandit.cpp:3