51 static std::vector<std::string>
getColumnNames(std::string
const& fname,
65 std::vector<T>
const& vec)
68 std::is_same_v<T, std::string> || std::is_same_v<T, double> ||
69 std::is_same_v<T, int>,
70 "CsvInterface can only write vectors of strings, doubles or ints.");
75 if (vec_size != vec.size())
77 ERR(
"Vector size does not match existing data (should be "
90 bool write()
override;
101 static int readPoints(std::string
const& fname,
char delim,
102 std::vector<GeoLib::Point*>& points);
119 static int readPoints(std::string
const& fname,
char delim,
120 std::vector<GeoLib::Point*>& points,
121 std::string
const& x_column_name,
122 std::string
const& y_column_name,
123 std::string
const& z_column_name =
"");
140 std::string
const& fname,
char delim,
141 std::vector<GeoLib::Point*>& points, std::size_t x_column_idx,
142 std::size_t y_column_idx,
143 std::size_t z_column_idx = std::numeric_limits<std::size_t>::max());
154 template <
typename T>
156 std::string
const& fname,
char delim, std::string
const& column_name)
158 std::ifstream in(fname.c_str());
161 ERR(
"CsvInterface::readColumn(): Could not open file {:s}.", fname);
166 std::getline(in, line);
167 std::size_t
const column_idx =
169 if (column_idx == std::numeric_limits<std::size_t>::max())
171 ERR(
"Column '{:s}' not found in file header.", column_name);
177 template <
typename T>
178 static std::pair<int, std::vector<T>>
readColumn(std::string
const& fname,
180 std::size_t column_idx)
182 std::ifstream in(fname.c_str());
185 ERR(
"CsvInterface::readColumn(): Could not open file {:s}.", fname);
193 static int readPoints(std::ifstream& in,
char delim,
194 std::vector<GeoLib::Point*>& points,
195 std::array<std::size_t, 3>
const& column_idx);
198 template <
typename T>
199 static std::pair<int, std::vector<T>>
readColumn(std::ifstream& in,
201 std::size_t column_idx)
203 std::vector<T> data_array;
205 std::size_t line_count(0);
207 while (std::getline(in, line))
210 std::list<std::string>
const fields =
213 if (fields.size() < column_idx + 1)
215 ERR(
"Line {:d} contains not enough columns of data. Skipping "
221 auto it = fields.begin();
222 std::advance(it, column_idx);
224 std::istringstream stream(*it);
226 if (!(stream >> value))
228 ERR(
"Error reading value in line {:d}.", line_count);
233 data_array.push_back(value);
235 return {error_count, data_array};
240 static std::size_t
findColumn(std::string
const& line,
char delim,
241 std::string
const& column_name);
251 void writeValue(std::size_t vec_idx, std::size_t in_vec_idx);
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition of the Writer class.
Base class which enables writing an object to string, stringstream or file.
std::vector< std::any > _data
void writeValue(std::size_t vec_idx, std::size_t in_vec_idx)
std::vector< std::string > _vec_names
static std::vector< std::string > getColumnNames(std::string const &fname, char delim)
static std::pair< int, std::vector< T > > readColumn(std::string const &fname, char delim, std::size_t column_idx)
bool addVectorForWriting(std::string const &vec_name, std::vector< T > const &vec)
void setCsvHeader(bool write_header)
Stores if the CSV file to be written should include a header or not.
static std::size_t findColumn(std::string const &line, char delim, std::string const &column_name)
void addIndexVectorForWriting(std::size_t s)
Adds an index vector of size s to the CSV file.
static std::pair< int, std::vector< T > > readColumn(std::ifstream &in, char delim, std::size_t column_idx)
Actual column reader for public methods.
std::size_t getVectorSize(std::size_t idx) const
Returns the size of the vector with the given index.
std::size_t getNArrays() const
Returns the number of vectors currently staged for writing.
CsvInterface()
Constructor (only needed for writing files)
bool write() override
Writes the CSV file.
static std::pair< int, std::vector< T > > readColumn(std::string const &fname, char delim, std::string const &column_name)
static int readPoints(std::string const &fname, char delim, std::vector< GeoLib::Point * > &points)
std::vector< std::string > splitString(std::string const &str)
TemplateElement< PointRule1 > Point