-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyParser.cxx
65 lines (59 loc) · 1.71 KB
/
MyParser.cxx
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
#include "MyParser.h"
using namespace std;
void LineParser(ifstream& file_to_parse, vector<string>& lines){
if (!file_to_parse)
{
printf("ERROR in LineParser > file not found.\n");
exit(1);
}
while (true){
if (file_to_parse.eof()){break;}
string line;
getline(file_to_parse,line);
if (line[0] != '#' && line.size() > 0){
lines.push_back(line);
}
}
}
void CSVParser(ifstream& file_to_parse,vector<vector<string> >& spreadsheet){
vector<string> lines;
LineParser(file_to_parse,lines);
for (vector<string>::iterator line=lines.begin();line!=lines.end();++line){
string field_content;
istringstream linestream(*line);
if (!linestream.eof()){
spreadsheet.push_back(vector<string>());
while(1){
getline(linestream,field_content,',');
spreadsheet.back().push_back(field_content);
if (linestream.eof())break;
}
}
}
}
void ColonParser(ifstream& file_to_parse,vector<vector<string> >& spreadsheet){
vector<string> lines;
LineParser(file_to_parse,lines);
for (vector<string>::iterator line=lines.begin();line!=lines.end();++line){
string field_content;
istringstream linestream(*line);
if (!linestream.eof()){
spreadsheet.push_back(vector<string>());
getline(linestream,field_content,':');
if (field_content=="//"){
spreadsheet.pop_back();
continue;
}
while(1){
spreadsheet.back().push_back(field_content);
if (linestream.eof())break;
getline(linestream,field_content,':');
if (field_content=="//")break;
}
}
}
}
string RemoveWhitespaces(string& s){
s.erase(std::remove_if(s.begin(),s.end(),[](char c){return std::isspace(static_cast<unsigned char>(c));}),s.end());
return s;
}