-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValve_Parser.cpp
More file actions
112 lines (96 loc) · 2.58 KB
/
Valve_Parser.cpp
File metadata and controls
112 lines (96 loc) · 2.58 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
//Valve_Parser.cpp
#include "Valve_Parser.h"
#include "Valve.h"
#include <string>
#include <list>
#include <fstream>
#include <sstream>
#include <iostream>
#include <ctype.h>
using namespace std;
Valve_Parser::Valve_Parser(string filename)
{
load_and_parse(filename);
}
void Valve_Parser::load_and_parse(string filename)
{
cout << "Starting Parser" << endl;
ifstream ifs(filename.c_str());
bool debug = true;
string line;
string token;
string valve_name = "";
bool valve_normal_open;
int port_number;
for(int line_number = 0;
getline(ifs, line);
line_number++)
{
if(line.front() == '#')
continue;
if(line.size() == 0)
continue;
valve_name = "";
valve_normal_open = false;
//for word in line
stringstream line_stream(line);
for(int word_number = 0;
getline(line_stream, token, ' ');
word_number++)
{
if(token == "")
{
cout << "Hey there's some weird shit on line " << line_number << endl;
break;
}
if(word_number == 0)
{
valve_name = token;
}
/*
* Normal open/closed parsing
*/
else if(word_number == 1)
{
if(token == "open")
valve_normal_open = true;
else if(token == "closed")
valve_normal_open = false;
//bad keywords
else
{
cout << "Didn't find correct 'normal'";
cout << "field on line " << line_number;
cout << " valid values are open and closed";
cout << endl;
break;
}
}
/*
* Valve port parsing
*/
else if(word_number == 2)
{
//out of range
if(stoi(token) > 7 || stoi(token) < 0)
{
if(debug)
printf("valves can only be on port 1-7\n");
break;
}
//good parse
else
{
port_number = stoi(token);
}
}
} //end for word...
valves.push_back(new Valve(valve_name,
valve_normal_open,
port_number));
} //end for line...
}
list<Valve *> Valve_Parser::get_valves()
{
return valves;
}