-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsv_reader.hpp
More file actions
103 lines (74 loc) · 2.89 KB
/
Copy pathCsv_reader.hpp
File metadata and controls
103 lines (74 loc) · 2.89 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
/*
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.
*/
#ifndef CSV_READER_PIERRE_HPP
#define CSV_READER_PIERRE_HPP
#include <unordered_map>
#include <vector>
#include <string>
#include <functional>
#include <istream>
#include <filesystem>
namespace csv{
//USAGE :
//Csv_reader r('\t');
//
//--- define parser --
//std::pair<std::string,std::string> p;
//std::vector< std::pair<std::string,std::string> > v;
//r.add_column("col1",[&](size_t line, std::string&& token){p.first =token;});
//r.add_column("col2",[&](size_t line, std::string&& token){p.second=token;});
//r.at_line=[&](size_t){v.push_back(p);}
//
//Optional : simplify column names
//r.at_header = [](std::string&s){csv::trim(s);}
//
//Optional : simplify tokens
//r.at_token = [](std::string&s){csv::trim(s);}
//
//--- run parser ---
//r.read("something.tsv");
class Csv_reader{
public:
typedef std::function<void(size_t line, std::string&&)> Fn_column;
typedef std::function<void(size_t line)> Fn_line; //line 0 is header, line 1 is first data line
explicit Csv_reader(char sep_='\t' ,char endl_='\n'):sep(sep_),endl(endl_){}
template<typename Fn>
void add_column(std::string col_name, Fn&& fn){
colname_to_fn[col_name] = std::forward<Fn>(fn);
}
size_t read(std::istream &in, const std::string &name);
size_t read(const std::filesystem::path &p);
Fn_line at_line=[](size_t){};
//called at the end of each line
std::function<void(std::string&)> at_header = [](std::string&){};
//called after reading a header => this is the place to simplify header names
//for example by removing quotes, triming them ...
std::function<void(std::string&)> at_token = [](std::string&){};
//called after reading a token on a defined column => this is the place to simplify tokens
//for example by removing quotes, triming them ...
char sep;
char endl;
private:
std::string name; //used to produce clear error messages
size_t line_count=0;
std::unordered_map<std::string,Fn_column> colname_to_fn;
std::vector<Fn_column*> fn_vector;
//details : read file line by line
void read_header(std::istream &in);
bool read_line (std::istream &in);
void reset();
};
}
#endif