8#include <cgv/math/fvec.h>
10#include <cgv/utils/lib_begin.h>
23 CSV_DEFAULT = CSV_HEADING | CSV_COMMA| CSV_TAB | CSV_QUOTES
30 mutable std::string last_error;
31 mutable bool failed =
false;
32 mutable size_t li = 0;
33 mutable size_t nr_cols = 0;
37 std::vector<std::string> col_names;
38 std::vector<cgv::utils::line> lines;
39 const char* get_ws()
const;
40 const char* get_sep()
const;
41 bool has_heading()
const;
42 bool find_column_index(
const std::string& col_name,
size_t& col_index)
const;
44 bool parse_next_line(std::vector<cgv::utils::token>& tokens)
const;
57 bool parse_matrix(std::vector<std::vector<T>>& matrix, T init = T(0)) {
58 std::vector<cgv::utils::token> tokens;
59 while (parse_next_line(tokens)) {
60 std::vector<T> vec(tokens.size(), init);
61 for (
size_t i = 0; i < tokens.size(); ++i)
63 matrix.emplace_back(vec);
68 template <
typename T,
unsigned N>
70 std::vector<cgv::utils::token> tokens;
71 while (parse_next_line(tokens)) {
73 for (
size_t i = 0; i < tokens.size() && i < N; ++i)
75 matrix.emplace_back(vec);
116 typedef std::pair<size_t, std::function<bool (
const std::string&, T&)>> setter;
117 std::vector<setter> setters;
122 bool add_entry(
size_t col_index, std::function<
bool(
const std::string&, T&)> s) {
123 setters.push_back({ col_index, s });
127 template <
typename M>
129 add_entry(col_index, [ptr](
const std::string& tok, T& obj) ->
bool {
135 template <
typename M>
136 bool add_entry(M T::* ptr, const std::string& col_name) {
138 if (!find_column_index(col_name, col_index))
143 bool add_entry(
const std::string& col_name, std::function<
bool(
const std::string&, T&)> s) {
145 if (!find_column_index(col_name, col_index))
152 std::vector<cgv::utils::token> tokens;
153 while (parse_next_line(tokens)) {
155 for (
auto s : setters) {
156 if (s.first >= tokens.size()) {
166 data.emplace_back(
object);
175#include <cgv/config/lib_end.h>
More advanced text processing for splitting text into lines or tokens.
A vector with zero based index.
csv reader for parsing matrices of a homogenous number type
bool parse_matrix(std::vector< cgv::math::fvec< T, N > > &matrix, T init=T(0))
parse matrix into nested vector struct and initialize entries to given init value (only rectangular m...
csv_matrix_reader(const std::string &file_name, CSV_Flags _flags=CSV_SPACE)
construct from csv file with flags defaulting to use of space as separator
bool parse_matrix(std::vector< std::vector< T > > &matrix, T init=T(0))
parse matrix into nested vector struct and initialize entries to given init value (only rectangular m...
base class of csv parser responsible for splitting file into lines and tokens
csv reader for parsing csv files to a specific type T that can store one object with different entrie...
bool add_entry(const std::string &col_name, std::function< bool(const std::string &, T &)> s)
add new entry based on heading of column with custom setter function
bool parse(std::vector< T > &data)
parse csv file content into given data vector extracting added entries into members of objects of typ...
csv_reader(const std::string &file_name, CSV_Flags _flags=CSV_DEFAULT)
construct from csv file with default flags (comma or tab as separator plus heading line) and extract ...
bool add_entry(M T::*ptr, const std::string &col_name)
add new entry based on heading of column (assumes construction with has_heading=true) with default se...
bool add_entry(M T::*ptr, size_t col_index)
add new entry based on column index (0, 1, ...) with default setter for member type
bool add_entry(size_t col_index, std::function< bool(const std::string &, T &)> s)
add new entry based on column index (0, 1, ...) with custom setter function
Helper functions to convert numeric types into strings using std streams.
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
CSV_Flags
different flags used by csv_reader classes
bool from_string(std::string &v, const std::string &s)
specialization to extract string value from string
this header is dependency free