2#include <cgv/utils/file.h>
7const char* csv_reader_base::get_ws()
const
9 return ((flags & CSV_SPACE) != 0) ?
" " :
"";
11const char* csv_reader_base::get_sep()
const
13 if ((flags & CSV_COMMA) != 0) {
14 if ((flags & CSV_TAB) != 0)
20 if ((flags & CSV_TAB) != 0)
24bool csv_reader_base::has_heading()
const
26 return (flags & CSV_HEADING) != 0;
28bool csv_reader_base::fail()
const
32bool csv_reader_base::find_column_index(
const std::string& col_name,
size_t& col_index)
const
34 auto iter = std::find(col_names.begin(), col_names.end(), col_name);
35 if (iter == col_names.end())
37 col_index = unsigned(iter - col_names.begin());
40csv_reader_base::csv_reader_base(
const std::string& file_name,
CSV_Flags _flags)
43 if (!cgv::utils::file::read(file_name, content,
true)) {
44 last_error =
"cannot read <" + file_name +
">";
49 while (li < lines.size()) {
50 if (!lines[li].empty())
54 if (li >= lines.size()) {
55 last_error =
"all lines in csv file are empty";
60 std::vector<cgv::utils::token> tokens;
62 for (
auto tok : tokens)
65 nr_cols = col_names.size();
69bool csv_reader_base::parse_next_line(std::vector<cgv::utils::token>& tokens)
const
71 while (li < lines.size()) {
72 if (lines[li].empty()) {
77 std::vector<cgv::utils::token> sep_tokens;
81 bool last_was_empty =
true;
82 for (
auto tok : sep_tokens) {
83 if (tok ==
"," || tok ==
"\t") {
87 last_was_empty =
true;
90 tokens.push_back(tok);
91 last_was_empty =
false;
95 nr_cols = tokens.size();
97 if (tokens.size() < col_names.size()) {
void split_to_tokens(const char *begin, const char *end, std::vector< token > &tokens, const std::string &separators, bool merge_separators, const std::string &open_parenthesis, const std::string &close_parenthesis, const std::string &whitespaces, unsigned int max_nr_tokens)
this function splits a text range into tokens.
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
void split_to_lines(const char *global_begin, const char *global_end, std::vector< line > &lines, bool truncate_trailing_spaces)
this function splits a text range at the newline characters into single lines.
CSV_Flags
different flags used by csv_reader classes
this header is dependency free
representation of a token in a text by two pointers begin and end, that point to the first character ...