-
Notifications
You must be signed in to change notification settings - Fork 3
/
csv_parser_example.cc
126 lines (98 loc) · 2.85 KB
/
csv_parser_example.cc
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
#define NANOCSV_IMPLEMENTATION
#include "nanocsv.h"
static void print_help() {
std::cout << "csv_parser_example input.csv (num_threads) (delimiter) (ignore_header)\n";
std::cout << " num_threads: -1(= use all theads)\n";
std::cout << " delimiter: ' '(whitespace)\n";
std::cout << " ignore_header: 1(ignore header). 0 to consider header line\n";
}
int main(int argc, char **argv)
{
if (argc < 2) {
print_help();
}
std::string filename("./data/array-4-5.csv");
int num_threads = -1; // -1 = use all system threads
char delimiter = ' ';
bool ignore_header = true;
if (argc > 1) {
std::string s(argv[1]);
if ((s == "-h") || (s == "--help")) {
print_help();
exit(1);
}
}
if (argc > 1) {
filename = argv[1];
}
if (argc > 2) {
num_threads = std::atoi(argv[2]);
}
if (argc > 3) {
delimiter = argv[3][0];
}
if (argc > 4) {
ignore_header = static_cast<bool>(std::atoi(argv[4]));
}
nanocsv::ParseOption<float> option;
option.delimiter = delimiter;
option.req_num_threads = num_threads;
option.verbose = true;
option.ignore_header = ignore_header;
std::string warn;
std::string err;
nanocsv::CSV<float> csv;
bool ret = nanocsv::ParseCSVFromFile(filename, option, &csv, &warn, &err);
if (!warn.empty()) {
std::cout << "WARN: " << warn << "\n";
}
if (!ret) {
if (!err.empty()) {
std::cout << "ERROR: " << err << "\n";
}
return EXIT_FAILURE;
}
if (option.replace_na) {
std::cout << "N/A value replaced with " << option.na_value << "\n";
}
if (option.replace_null) {
std::cout << "Null(empty) value replaced with " << option.null_value << "\n";
}
std::cout << "num records(rows) = " << csv.num_records << "\n";
std::cout << "num fields(columns) = " << csv.num_fields << "\n";
// Limit printing records and fields
const size_t print_num_records = 20;
const size_t print_num_fields = 8;
bool recoreds_skipped = false;
if (csv.header.size()) {
for (size_t j = 0; j < std::min(print_num_fields, csv.header.size()); j++) {
std::cout << "header[" << j << "] = " << csv.header[j] << "\n";
}
}
for (size_t j = 0; j < csv.num_records; j++) {
if ((print_num_records < j) && (j != (csv.num_records - 1))) {
if (!recoreds_skipped) {
std::cout << "...\n";
recoreds_skipped = true;
}
continue;
}
std::cout << "[" << j << "] ";
bool fields_skipped = false;
for (size_t i = 0; i < csv.num_fields; i++) {
if ((print_num_fields < i) && (i != (csv.num_fields - 1))) {
if (!fields_skipped) {
std::cout << " ... ";
fields_skipped = true;
}
continue;
}
std::cout << csv.values[j * csv.num_fields + i];
if (i != (csv.num_fields - 1)) {
std::cout << ", ";
}
}
std::cout << "\n";
}
return EXIT_SUCCESS;
}