-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseDat.cpp
More file actions
57 lines (51 loc) · 1.6 KB
/
parseDat.cpp
File metadata and controls
57 lines (51 loc) · 1.6 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
#include "parseDat.h"
#include <fstream>
#include <sstream>
using std::cout;
using std::endl;
using std::ifstream;
using std::ostream;
parseData::parseData() {}
// converts csv file into 1 D vector
void parseData::fileToVect(const string &file) {
ifstream ifs(file);
string str;
while (getline(ifs, str)) {
csvVector.push_back(str);
}
ifs.close();
}
// takes the 1 D vector and converts it into 2D
void parseData::vectorToTwoDimension() {
for (string &str : csvVector) {
csvInTwoDimension.push_back(splitString(str));
}
}
// splits the string
vector<string> parseData::splitString(string str) {
vector<string> v;
string line;
std::stringstream sstr(str);
while (getline(sstr, line)) { // read full line
const char *mystart = line.c_str(); // prepare to parse the line - start is
// position of begin of field
bool instring{false};
for (const char *p = mystart; *p; p++) { // iterate through the string
if (*p == '"') // toggle flag if we're btw double quote
instring = !instring;
else if (*p == ',' && !instring) { // if comma OUTSIDE double quote
v.push_back(string(mystart, p - mystart)); // keep the field
mystart = p + 1; // and start parsing next one
}
}
v.push_back(string(
mystart)); // last field delimited by end of line instead of comma
}
return v;
}
// creates and returns 2-D vector. public function.
vector<vector<string>> parseData::getDataVector(string file) {
fileToVect(file);
vectorToTwoDimension();
return csvInTwoDimension;
}