-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsv_reader.cpp
More file actions
199 lines (143 loc) · 5.15 KB
/
Copy pathCsv_reader.cpp
File metadata and controls
199 lines (143 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Copyright (C) 2024 Pierre BLAVY
This program (csv) is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Csv_reader.hpp"
#include "tools/str_cat.hpp"
#include <set>
#include <fstream>
//--- helpers ---
namespace {
template<typename Fn> //Fn is any callable as void(size_t, std::string&&);
void tokenize(std::string_view string, char sep, Fn fn){
if(string==""){return;}
std::string token;
size_t index=0;
for(char c : string){
if(c==sep){
fn(index, std::move(token) );
token="";
++index;
}else{
token +=c;
}
}
fn(index,std::move(token) );
}
template<typename Fn> //Fn is any callable as void(size_t, std::string&&);
void tokenize(std::istream &in, char sep, char endl, Fn fn){
if(!in){return;}
std::string token;
size_t index=0;
char c;
while (in.get(c)){
if(c==endl){fn(index,std::move(token) ); return;}
if(c==sep){
fn(index, std::move(token) );
token="";
++index;
}else{
token +=c;
}
}
fn(index,std::move(token) );
}
}//end namespace {
void csv::Csv_reader::read_header(std::istream &in){
fn_vector.resize(0);
line_count=0;
std::set<std::string_view> missing_cols;
for(const auto &p:colname_to_fn){
missing_cols.emplace(p.first);
}
std::set<std::string> seen_cols;
std::set<std::string> duplicated_cols;
auto fn_header = [&,this](size_t , std::string &&h){
at_header(h);
missing_cols.erase(h);
if(seen_cols.count(h)!=0){
duplicated_cols.insert(h);
}else{
seen_cols. insert(h);
}
auto x=colname_to_fn.find(h);
if(x==colname_to_fn.end()){
fn_vector.emplace_back( nullptr); //nothing defined
}else{
fn_vector.emplace_back( &x->second );
}
};
tokenize(in,sep,endl,fn_header);
//missing columns
if(!missing_cols.empty()){
std::string err = "Error in Csv_reader::read_header "+ std::to_string(missing_cols.size()) +" columns are missing. "
"missing =";
csv::str_cat(err, missing_cols, ", " );
err+=", found " + std::to_string(seen_cols.size()) +" columns = ";
csv::str_cat(err, seen_cols, ", " );
throw std::runtime_error( std::move(err) );
}
//duplicated columns
if(!duplicated_cols.empty()){
std::string err = "Error in Csv_reader::read_header duplicated columns names. "
"duplicated =";
str_cat(err, duplicated_cols, ", " );
throw std::runtime_error( std::move(err) );
}
}
bool csv::Csv_reader::read_line(std::istream &in){
std::string line_str;
bool ok= !!std::getline(in,line_str,endl);
if(!ok)[[unlikely]]{return false;}
++line_count;
auto fn_col=[&,this](size_t col, std::string&& token){
if(col>= fn_vector.size()){
std::string err = "Error in Csv_reader::read_line : too many item in line. line="+std::to_string(line_count)+", extra_token="+token;
}
//call function if defined
auto pfn = fn_vector[col];
if(pfn!=nullptr){
try{
at_token(token);
(*pfn)(line_count,std::move(token));
}catch(std::exception &e){
std::string err = "Error in Csv_reader::read_line : cannot handle token. line="+std::to_string(line_count)+", col="+ std::to_string(col)+", token="+token+", error="+e.what();
throw std::runtime_error(std::move(err));
}catch(...){
std::string err = "Unknown error in Csv_reader::read_line : cannot handle token. line="+std::to_string(line_count)+", col="+ std::to_string(col)+", token="+token;
throw std::runtime_error(std::move(err));
}
}
};
tokenize(line_str,sep,fn_col);
at_line(line_count);
return true;
}
void csv::Csv_reader::reset(){
line_count=0;
fn_vector.resize(0);
}
size_t csv::Csv_reader::read(std::istream &in, const std::string &name_){
reset();
name=name_;
read_header(in);
while(read_line(in)){};
return line_count;
}
size_t csv::Csv_reader::read(const std::filesystem::path &p){
std::ifstream in( p );
if(!in){
throw std::runtime_error("Error in Csv_reader::read_file, cannot open file. path="+p.generic_string() );
}
return read(in,p.generic_string());
}