-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleFetcher.cpp
More file actions
185 lines (135 loc) · 3.81 KB
/
RuleFetcher.cpp
File metadata and controls
185 lines (135 loc) · 3.81 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
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
#include "RuleFetcher.h"
typedef boost::shared_ptr< IRule > RulePtr;
vector<RulePtr> RuleFetcher::GetRuleList(vector<std::string> a_stringList)
{
vector<RulePtr> ruleList;
std::string element;
RulePtr rulePtr;
for(int i = 0; i < a_stringList.size(); i++)
{
element = a_stringList[i];
rulePtr.reset();
rulePtr = ParseLine(element);
ruleList.push_back(rulePtr);
}
return ruleList;
}
RuleFetcher::~RuleFetcher()
{
}
RuleFetcher::RuleFetcher()
{
}
RulePtr RuleFetcher::ParseLine(string a_line)
{
int length = 0;
/* Start domain */
string::size_type markerStartDomain = a_line.find(":");
string startDomain = a_line.substr(0, markerStartDomain);
int i_startDomain = atoi(startDomain.c_str());
/* Start position */
string::size_type markerStartPosition
= a_line.find_first_of(":", markerStartDomain + 1);
length = markerStartPosition - (markerStartDomain + 1);
string startPositionStr = a_line.substr(markerStartDomain + 1, length);
int i_startPosition = atoi(startPositionStr.c_str());
/* Start direction */
string::size_type markerStartDirection
= a_line.find_first_of(";", markerStartPosition + 1);
length = markerStartDirection - (markerStartPosition + 1);
string startDirection =
a_line.substr(markerStartPosition + 1, length);
DIRECTION d_startDirection = ConvertStringToEDirection(startDirection);
/* END DOMAIN */
string::size_type markerEndDomain
= a_line.find_first_of(":", markerStartDirection + 1);
length = markerEndDomain - (markerStartDirection + 1 );
string endDomainStr = a_line.substr(markerStartDirection + 1, length);
int i_endDomain = atoi(endDomainStr.c_str());
/* END POSITION */
string::size_type markerEndPosition
= a_line.find_first_of(":", markerEndDomain + 1);
length = markerEndPosition - (markerEndDomain + 1);
string endPositionStr = a_line.substr(markerEndDomain + 1, length);
int i_endPosition = atoi(endPositionStr.c_str());
/* END DIRECTION */
string::size_type markerEndDirection
= a_line.find_first_of(";", markerEndPosition + 1);
length = markerEndDirection - (markerEndPosition + 1);
string endDirection =
a_line.substr(markerEndPosition + 1, length);
DIRECTION d_endDirection = ConvertStringToEDirection(endDirection);
RulePtr rulePtr(new Rule(i_startDomain,
i_endDomain,
d_startDirection,
d_endDirection,
i_startPosition,
i_endPosition));
return rulePtr;
}
DIRECTION RuleFetcher::ConvertStringToEDirection(std::string a_direction)
{
DIRECTION direction;
if(a_direction.compare("DOWN") == 0)
{
direction = DOWN;
}
else if(a_direction.compare("UP") == 0)
{
direction = UP;
}
else if(a_direction.compare("LEFT") == 0)
{
direction = LEFT;
}
else
{
direction = RIGHT;
}
return direction;
}
vector<std::string> RuleFetcher::ReadRuleFile(std::string a_fileName)
{
vector<std::string> listOfLines;
string line;
ifstream myfile (a_fileName.c_str());
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
listOfLines.push_back(line);
}
myfile.close();
}
return listOfLines;
}
void RuleFetcher::Initialize(std::string a_fileName)
{
vector<string> rawRules =
ReadRuleFile(a_fileName);
m_ruleList = GetRuleList(rawRules);
}
RulePtr RuleFetcher::GetRule(int a_startDomain,
int a_startPosition,
DIRECTION a_startDirection)
{
RulePtr rulePtr;
IRule* element;
for(int i = 0; i < m_ruleList.size() ; i++)
{
element = m_ruleList[i].get();
/*
TEST_ASSERT( rule->GetOldFetcherDirectory() == 1);
TEST_ASSERT( rule->GetOldDirection() == UP);
TEST_ASSERT( rule->GetOldPosition() == 2);
*/
if(element->GetOldFetcherDirectory() == a_startDomain &&
element->GetOldPosition() == a_startPosition &&
element->GetOldDirection() == a_startDirection)
{
return m_ruleList[i];
}
}
return rulePtr;
}