-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQCsv.hpp
More file actions
152 lines (124 loc) · 3.87 KB
/
Copy pathQCsv.hpp
File metadata and controls
152 lines (124 loc) · 3.87 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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <sstream>
class QCsv
{
public:
std::map<std::string, std::vector<std::string>> df;
std::vector<std::string> headers;
void ReadCsvFile(const std::string &csvfn)
{
csvfile.open(csvfn, std::ios::in);
if(!csvfile.is_open())
{
std::cerr << "QCsv: Error while opening a file" << std::endl;
return;
}
ReadCsv();
}
void PrintDf()
{
if(headers.empty())
{
std::cerr << "QCsv: Please define QCsv::headers string vector" << std::endl;
return;
}
for(const std::string &h : headers)
std::cout << h << '\t';
std::cout << std::endl;
for(size_t i = 0 ; i < df[headers[0]].size() ; i++)
{
for(const std::string &h : headers)
std::cout << df[h][i] << '\t';
std::cout << std::endl;
}
}
void WriteCsv(const std::string &csvfn)
{
if(headers.empty())
{
std::cerr << "QCsv: Please define QCsv::headers string vector" << std::endl;
return;
}
std::ofstream writecsvfile;
writecsvfile.open(csvfn);
for(size_t i = 0 ; i < headers.size() ; i++)
{
if(i == headers.size()-1)
writecsvfile << headers[i];
else
writecsvfile << headers[i] << ",";
}
writecsvfile << '\n';
for(size_t i = 0 ; i < df[headers[0]].size() ; i++)
{
for(size_t j = 0 ; j < headers.size() ; j++)
{
if(j == headers.size()-1)
writecsvfile << df[headers[j]][i];
else
writecsvfile << df[headers[j]][i] << ",";
}
writecsvfile << '\n';
}
}
template<typename T>
std::vector<std::string> convertAVectorToStringVector(const std::vector<T> &vec)
{
std::vector<std::string> res;
for(size_t i = 0 ; i < vec.size() ; i++)
res.push_back(std::to_string(vec[i]));
return res;
}
std::vector<std::string>& operator[](const std::string &h)
{
return df[h];
}
~QCsv()
{
csvfile.close();
}
private:
std::fstream csvfile;
void ReadCsv()
{
if(csvfile.is_open())
{
std::string sa;
std::vector<std::string> row;
bool firstline = true;
while(std::getline(csvfile, sa))
{
if(firstline)
{
headers = Split(sa);
firstline = false;
}
else
{
row = Split(sa);
for(size_t i = 0 ; i < headers.size() ; i++)
df[headers[i]].push_back(row[i]);
}
}
}
else
{
std::cerr << "QCsv: Error while reading the file" << std::endl;
return;
}
}
std::vector<std::string>
Split(const std::string &s)
{
std::string t;
std::stringstream ss(s);
std::vector<std::string> res;
while(std::getline(ss, t, ','))
res.push_back(t);
return res;
}
};