-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickcsv.hpp
178 lines (157 loc) · 5.47 KB
/
quickcsv.hpp
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
/*
* quickcsv.h
*
* URL: https://github.com/GreyRaphael/quickcsv
* Version: 1.0
*
* Copyright (C) 2023-2023 Wei Ge
* All rights reserved.
*
* quick is distributed under the BSD 3-Clause license, see LICENSE for details.
*
*/
#pragma once
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
namespace quickcsv {
inline int EMPTY_INT = 0;
inline float EMPTY_FLOAT = NAN;
inline auto read_raw_csv(std::string const& filename, std::vector<std::string> usecols = {}, size_t skiprows = 0, char sep = ',') {
// read csv row by row, support utf8
// example: read_csv("test.csv", {"last", "time"}, ',');
bool file_exist = std::filesystem::exists(filename);
if (!file_exist) {
std::cout << filename << " not exist!\n";
std::exit(EXIT_SUCCESS);
}
std::ifstream file(filename, std::ios::in);
std::string line;
std::stringstream ss;
std::string cell;
for (size_t i = 0; i < skiprows; ++i) {
std::getline(file, line);
}
// read headers
std::vector<std::string> header_vector;
header_vector.reserve(8);
std::getline(file, line);
ss << line;
while (std::getline(ss, cell, sep)) {
header_vector.emplace_back(cell);
}
ss.clear();
usecols = (usecols.size() > 0) ? usecols : header_vector;
// process headers
std::vector<int> index_vect;
index_vect.reserve(header_vector.size());
for (auto const& hearder_name : header_vector) {
auto it = std::find(usecols.begin(), usecols.end(), hearder_name);
if (it != usecols.end()) {
index_vect.emplace_back(std::distance(usecols.begin(), it));
} else {
index_vect.emplace_back(-1);
}
}
std::vector<std::vector<std::string>> table;
table.reserve(32);
uint col_nums = usecols.size();
while (std::getline(file, line)) {
if (line.size() < 1) {
break;
}
std::vector<std::string> row_vector(col_nums);
ss << line;
for (uint i = 0; std::getline(ss, cell, sep); ++i) {
int col_index = index_vect[i];
if (col_index != -1) {
row_vector[col_index] = cell;
}
}
ss.clear();
table.emplace_back(row_vector);
}
file.close();
return table;
}
template <typename T>
struct Converter {
static auto convert(const std::string& str) {
T val{};
try {
if constexpr (std::is_same_v<T, int>)
val = std::stoi(str);
else if constexpr (std::is_same_v<T, long>)
val = std::stol(str);
else if constexpr (std::is_same_v<T, long long>)
val = std::stoll(str);
else if constexpr (std::is_same_v<T, unsigned>)
val = std::stoul(str);
else if constexpr (std::is_same_v<T, unsigned long>)
val = std::stoul(str);
else if constexpr (std::is_same_v<T, unsigned long long>)
val = std::stoull(str);
else if constexpr (std::is_same_v<T, char>)
val = str.empty() ? EMPTY_INT : str[0];
else if constexpr (std::is_same_v<T, float>)
val = std::stof(str);
else if constexpr (std::is_same_v<T, double>)
val = std::stod(str);
else if constexpr (std::is_same_v<T, long double>)
val = std::stold(str);
else
val = str;
} catch (...) {
if constexpr (std::is_same_v<T, int>)
val = EMPTY_INT;
else if constexpr (std::is_same_v<T, long>)
val = EMPTY_INT;
else if constexpr (std::is_same_v<T, long long>)
val = EMPTY_INT;
else if constexpr (std::is_same_v<T, unsigned>)
val = EMPTY_INT;
else if constexpr (std::is_same_v<T, unsigned long>)
val = EMPTY_INT;
else if constexpr (std::is_same_v<T, unsigned long long>)
val = EMPTY_INT;
else if constexpr (std::is_same_v<T, float>)
val = EMPTY_FLOAT;
else if constexpr (std::is_same_v<T, double>)
val = EMPTY_FLOAT;
else if constexpr (std::is_same_v<T, long double>)
val = EMPTY_FLOAT;
}
return val;
}
};
template <typename... Ts>
struct Parser {
constexpr static int N = sizeof...(Ts);
// the inline specifier allows the static member containter to be defined within the class itself
inline static std::tuple<std::vector<Ts>...> CONTAINER{};
template <int... I>
static void ParseTable(const std::vector<std::vector<std::string>>& table, std::integer_sequence<int, I...>) {
(std::get<I>(CONTAINER).reserve(table.size()), ...);
for (const auto& row : table) {
(std::get<I>(CONTAINER).push_back(Converter<Ts>::convert(row[I])), ...);
}
}
static auto Parse(const std::vector<std::vector<std::string>>& table) {
ParseTable(table, std::make_integer_sequence<int, N>());
return CONTAINER;
}
};
struct Param {
char sep = ',';
size_t skiprows = 0;
std::vector<std::string> usecols{};
};
template <typename... Ts>
auto read_csv(std::string const& filename, Param const& parameters = Param{}) {
auto table = read_raw_csv(filename, parameters.usecols, parameters.skiprows, parameters.sep);
return Parser<Ts...>::Parse(table);
}
} // namespace quickcsv