forked from 3BioCompBio/SpikeProSARS-CoV-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVparser.cpp
268 lines (224 loc) · 5.93 KB
/
CSVparser.cpp
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
#include <fstream>
#include <sstream>
#include <iomanip>
#include "CSVparser.hpp"
namespace csv {
Parser::Parser(const std::string &data, const DataType &type, char sep)
: _type(type), _sep(sep)
{
std::string line;
if (type == eFILE)
{
_file = data;
std::ifstream ifile(_file.c_str());
if (ifile.is_open())
{
while (ifile.good())
{
getline(ifile, line);
if (line != "")
_originalFile.push_back(line);
}
ifile.close();
if (_originalFile.size() == 0)
throw Error(std::string("No Data in ").append(_file));
parseHeader();
parseContent();
}
else
throw Error(std::string("Failed to open ").append(_file));
}
else
{
std::istringstream stream(data);
while (std::getline(stream, line))
if (line != "")
_originalFile.push_back(line);
if (_originalFile.size() == 0)
throw Error(std::string("No Data in pure content"));
parseHeader();
parseContent();
}
}
Parser::~Parser(void)
{
std::vector<Row *>::iterator it;
for (it = _content.begin(); it != _content.end(); it++)
delete *it;
}
void Parser::parseHeader(void)
{
std::stringstream ss(_originalFile[0]);
std::string item;
while (std::getline(ss, item, _sep))
_header.push_back(item);
}
void Parser::parseContent(void)
{
std::vector<std::string>::iterator it;
it = _originalFile.begin();
it++; // skip header
for (; it != _originalFile.end(); it++)
{
bool quoted = false;
int tokenStart = 0;
unsigned int i = 0;
Row *row = new Row(_header);
for (; i != it->length(); i++)
{
if (it->at(i) == '"')
quoted = ((quoted) ? (false) : (true));
else if (it->at(i) == ',' && !quoted)
{
row->push(it->substr(tokenStart, i - tokenStart));
tokenStart = i + 1;
}
}
//end
row->push(it->substr(tokenStart, it->length() - tokenStart));
// if value(s) missing
if (row->size() != _header.size())
throw Error("corrupted data !");
_content.push_back(row);
}
}
Row &Parser::getRow(unsigned int rowPosition) const
{
if (rowPosition < _content.size())
return *(_content[rowPosition]);
throw Error("can't return this row (doesn't exist)");
}
Row &Parser::operator[](unsigned int rowPosition) const
{
return Parser::getRow(rowPosition);
}
unsigned int Parser::rowCount(void) const
{
return _content.size();
}
unsigned int Parser::columnCount(void) const
{
return _header.size();
}
std::vector<std::string> Parser::getHeader(void) const
{
return _header;
}
const std::string Parser::getHeaderElement(unsigned int pos) const
{
if (pos >= _header.size())
throw Error("can't return this header (doesn't exist)");
return _header[pos];
}
bool Parser::deleteRow(unsigned int pos)
{
if (pos < _content.size())
{
delete *(_content.begin() + pos);
_content.erase(_content.begin() + pos);
return true;
}
return false;
}
bool Parser::addRow(unsigned int pos, const std::vector<std::string> &r)
{
Row *row = new Row(_header);
for (auto it = r.begin(); it != r.end(); it++)
row->push(*it);
if (pos <= _content.size())
{
_content.insert(_content.begin() + pos, row);
return true;
}
return false;
}
void Parser::sync(void) const
{
if (_type == DataType::eFILE)
{
std::ofstream f;
f.open(_file, std::ios::out | std::ios::trunc);
// header
unsigned int i = 0;
for (auto it = _header.begin(); it != _header.end(); it++)
{
f << *it;
if (i < _header.size() - 1)
f << ",";
else
f << std::endl;
i++;
}
for (auto it = _content.begin(); it != _content.end(); it++)
f << **it << std::endl;
f.close();
}
}
const std::string &Parser::getFileName(void) const
{
return _file;
}
/*
** ROW
*/
Row::Row(const std::vector<std::string> &header)
: _header(header) {}
Row::~Row(void) {}
unsigned int Row::size(void) const
{
return _values.size();
}
void Row::push(const std::string &value)
{
_values.push_back(value);
}
bool Row::set(const std::string &key, const std::string &value)
{
std::vector<std::string>::const_iterator it;
int pos = 0;
for (it = _header.begin(); it != _header.end(); it++)
{
if (key == *it)
{
_values[pos] = value;
return true;
}
pos++;
}
return false;
}
const std::string Row::operator[](unsigned int valuePosition) const
{
if (valuePosition < _values.size())
return _values[valuePosition];
throw Error("can't return this value (doesn't exist)");
}
const std::string Row::operator[](const std::string &key) const
{
std::vector<std::string>::const_iterator it;
int pos = 0;
for (it = _header.begin(); it != _header.end(); it++)
{
if (key == *it)
return _values[pos];
pos++;
}
throw Error("can't return this value (doesn't exist)");
}
std::ostream &operator<<(std::ostream &os, const Row &row)
{
for (unsigned int i = 0; i != row._values.size(); i++)
os << row._values[i] << " | ";
return os;
}
std::ofstream &operator<<(std::ofstream &os, const Row &row)
{
for (unsigned int i = 0; i != row._values.size(); i++)
{
os << row._values[i];
if (i < row._values.size() - 1)
os << ",";
}
return os;
}
}