-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibscv.cpp
More file actions
339 lines (301 loc) · 9.69 KB
/
libscv.cpp
File metadata and controls
339 lines (301 loc) · 9.69 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// Created by Ângelo Mutti on 06/07/24.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <cstring>
#include <vector>
using namespace std;
/**
* Split a string by a delimiter.
*
* @param str The string to be split.
* @param delimiter The delimiter to split the string by.
*
* @return vector<string> The split string.
*/
vector<string> split(const string &str, const char delimiter) {
vector<string> tokens;
string token;
istringstream tokenStream(str);
while (getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
bool is_equal(const string &a, const string &b) {
return strcmp(a.c_str(), b.c_str());
}
bool is_greater_than(const string &a, const string &b) {
return strcmp(a.c_str(), b.c_str()) > 0;
}
bool is_less_than(const string &a, const string &b) {
return strcmp(a.c_str(), b.c_str()) < 0;
}
/**
* Check if a string contains a substring.
*
* @param mainStr The string to be checked.
* @param subStr The substring to check for.
*
* @return bool True if the string contains the substring, false otherwise.
*/
bool contains(const std::string &mainStr, const std::string &subStr) {
return mainStr.find(subStr) != std::string::npos;
}
/**
* Get the selected columns and their index pairs.
*
* @param header The header of the CSV data.
* @param selectedRows The selected columns to be processed.
*
* @return map<string, int> The selected columns and their index pairs.
*/
map<string, int> get_selected_column_and_index(const char header[], const char selectedRows[]) {
vector<string> headers = split(header, ',');
map<string, int> map;
for (int i = 0; i < headers.size(); i++) {
if (contains(selectedRows, headers[i])) {
map.insert(pair<string, int>(headers[i], i));
}
}
return map;
}
void filter_greater_than(vector<map<string, string> > &csvData, const string &filter) {
vector<string> filterParts = split(filter, '>');
string column = filterParts[0];
string value = filterParts[1];
auto it = csvData.begin();
while (it != csvData.end()) {
if (!is_greater_than(it->at(column), value)) {
it = csvData.erase(it);
} else {
++it;
}
}
}
void filter_is_equal(vector<map<string, string> > &csvData, const string &filter) {
vector<string> filterParts = split(filter, '=');
string column = filterParts[0];
string value = filterParts[1];
auto it = csvData.begin();
while (it != csvData.end()) {
if (is_equal(it->at(column), value)) {
it = csvData.erase(it);
} else {
++it;
}
}
}
void filter_greater_or_equal(vector<map<string, string> > &csvData, const string &filter) {
vector<string> filterParts = split(filter, '=');
string column = filterParts[0].substr(filterParts[0].size() - 1);
string value = filterParts[1];
auto it = csvData.begin();
while (it != csvData.end()) {
if (!is_greater_than(it->at(column), value) && !is_equal(it->at(column), value)) {
it = csvData.erase(it);
} else {
++it;
}
}
}
void filter_is_less_equal(vector<map<string, string> > &csvData, const string &filter) {
vector<string> filterParts = split(filter, '=');
string column = filterParts[0];
string value = filterParts[1];
auto it = csvData.begin();
while (it != csvData.end()) {
if (is_equal(it->at(column), value)) {
it = csvData.erase(it);
} else {
++it;
}
}
}
/**
* Filter the CSV data based on the row filter definitions. For this in secific is "!="
* @param csvData csv data to be filtered
* @param filter filter to be applied
*/
void filter_is_not_equal(vector<map<string, string> > &csvData, const string &filter) {
vector<string> filterParts = split(filter, '=');
string column = filterParts[0].substr(0, filterParts[0].size() - 1);
string value = filterParts[1];
auto it = csvData.begin();
while (it != csvData.end()) {
if (!is_equal(it->at(column), value)) {
it = csvData.erase(it);
} else {
++it;
}
}
}
void filter_less_than(vector<map<string, string> > &csvData, const string &filter) {
vector<string> filterParts = split(filter, '<');
string column = filterParts[0];
string value = filterParts[1];
auto it = csvData.begin();
while (it != csvData.end()) {
if (!is_less_than(it->at(column), value)) {
it = csvData.erase(it);
} else {
++it;
}
}
}
/**
* Filter the CSV data based on the row filter definitions.
* a filter that it excpets its the full rowFilterDefinitionsString, as in the example below:
*
* header1>1\nheader3<10
*
* @param csvData The CSV data to be filtered.
* @param rowFilterDefinitions The filters to be applied to the CSV data.
*
* @return void
*/
void filter_csv_data(vector<map<string, string> > &csvData, const char rowFilterDefinitions[]) {
vector<string> filters = split(rowFilterDefinitions, '\n');
for (const auto &filter: filters) {
try {
if (contains(filter, ">")) {
filter_greater_than(csvData, filter);
continue;
}
if (contains(filter, "<")) {
filter_less_than(csvData, filter);
continue;
}
if (contains(filter, "!=")) {
filter_is_not_equal(csvData, filter);
continue;
}
if (contains(filter, ">=")) {
filter_greater_or_equal(csvData, filter);
continue;
}
if (contains(filter, "<=")) {
filter_is_less_equal(csvData, filter);
continue;
}
if (contains(filter, "=")) {
filter_is_equal(csvData, filter);
continue;
}
cout << "Invalid filter: " << filter << "\n";
} catch (const std::exception &e) {
cout << "Header '" << "header" << "' not found in CSV file/string" << filter << "\n";
}
}
}
/**
* Print the CSV header.
*
* @param csvData The CSV data to be printed.
*
* @return void
*/
void print_csv_header(const vector<map<string, string> > &csvData) {
if (csvData.empty()) {
return;
}
string headers;
for (const auto &pair: csvData[0]) {
headers += pair.first + ",";
}
std::cout << headers.substr(0, headers.size() - 1) << std::endl;
}
/**
* Print the CSV data.
*
* @param csvData The CSV data to be printed.
*
* @return void
*/
void print_csv_data(const vector<map<string, string> > &csvData) {
if (csvData.empty()) {
return;
}
string currLine;
for (const auto &row: csvData) {
for (const auto &pair: row) {
currLine += pair.second + ",";
}
std::cout << currLine.substr(0, currLine.size() - 1) << std::endl;
currLine.clear();
}
}
/**
* Process a line of CSV data.
*
* @param line The line of CSV data to be processed.
* @param mapColumnIndex The selected columns and their index pairs.
* @param interestedColumns The columns to be selected from the CSV data.
*
* @return map<string, string> The processed line of CSV data.
*/
map<string, string> process_line(string line, map<string, int> mapColumnIndex,
vector<string> interestedColumns) {
vector<string> fields = split(line, ',');
map<string, string> row;
string currLine;
for (const auto &interestedColumn: interestedColumns) {
int index = mapColumnIndex[interestedColumn];
currLine += fields[index] + ",";
row.insert(pair<string, string>(interestedColumn, fields[index]));
}
return row;
}
/**
* Process the CSV data by applying filters and selecting columns.
*
* @param csv The CSV data to be processed.
* @param selectedColumns The columns to be selected from the CSV data.
* @param rowFilterDefinitions The filters to be applied to the CSV data.
*
* @return void
*/
void processCsv(const char csv[], const char selectedColumns[], const char rowFilterDefinitions[]) {
vector<string> csvLines = split(csv, '\n');
vector<map<string, string> > csvData;
map<string, int> mapColumnIndex = get_selected_column_and_index(csvLines[0].c_str(), selectedColumns);
vector<string> interestedColumns = split(selectedColumns, ',');
for (int i = 1; i < csvLines.size(); i++) {
csvData.push_back(process_line(csvLines[i], mapColumnIndex, interestedColumns));
}
print_csv_header(csvData);
filter_csv_data(csvData, rowFilterDefinitions);
print_csv_data(csvData);
}
/**
* Process the CSV data by applying filters and selecting columns.
*
* @param csvFilePath The file path of the CSV to be processed.
* @param selectedColumns The columns to be selected from the CSV data.
* @param rowFilterDefinitions The filters to be applied to the CSV data.
*
* @return void
*/
void processCsvFile(const char filename[], const char selectedColumns[], const char rowFilterDefinitions[]) {
vector<map<string, string> > csvData;
ifstream file(filename);
string line;
getline(file, line);
map<string, int> mapColumnIndex = get_selected_column_and_index(line.c_str(), selectedColumns);
vector<string> interestedColumns = split(selectedColumns, ',');
while (getline(file, line)) {
csvData.push_back(process_line(line, mapColumnIndex, interestedColumns));
}
file.close();
print_csv_header(csvData);
filter_csv_data(csvData, rowFilterDefinitions);
print_csv_data(csvData);
}
int main() {
processCsvFile("data.csv", "col1,col4,col3,col5", "col3!=l3c3");
return 0;
}