-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.cpp
258 lines (229 loc) · 5.92 KB
/
schedule.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
/*
* schedule.cpp
*
* Created on: Nov 26, 2015
* Author: jose
*/
#include "schedule.h"
#include "time_interval.h"
CSchedule::CSchedule()
{
}
bool CSchedule::OpenConfigFile(std::string file)
{
m_ConfigFile.open(file.c_str());
if(!m_ConfigFile.is_open())
{
std::cerr << "ERROR: Can not open configuration file!\n";
return false;
}
else
return true;
}
bool CSchedule::GetLink(STrainProcess& unit)
{
if(!m_ConfigFile.is_open())
{
std::cerr << "ERROR: Call method CSchedule::OpenConfigFile() first!\n";
return false;
}
std::string line;
unsigned int i = 0;
while(getline(m_ConfigFile, line))
{
if(!ParseLine(line, i, unit))
{
std::cerr << "ERROR: Syntax error in configuration file on line: " << i + 1 << std::endl;
return false;
}
if(i == 4)
{
if(unit.through != "")
{
getline(m_ConfigFile, line);
}
return true;
}
i++;
}
unit.name = "EOF";
return true;
}
bool CSchedule::ParseLine(std::string line, unsigned int counter, STrainProcess& conf_struct)
{
unsigned int line_num = counter % NUM_OF_LINES;
switch(line_num)
{
case 0:
if(line[0] == '[' && line[line.length() - 1] == ']')
{
conf_struct.name = line.substr(1, line.length() - 2);
}
else
return false;
break;
case 1:
if(line.substr(0, 6) == "From: ")
{
conf_struct.from = line.substr(6, line.find('-') - 7);
conf_struct.appears = atoi(line.substr(line.find('-') + 2, 2).c_str()) * 60
+ atoi(line.substr(line.find('-') + 5, 2).c_str());
}
else
return false;
break;
case 2:
if(line.substr(0, 4) == "To: ")
{
conf_struct.through = "";
conf_struct.to = line.substr(4, line.find('-') - 5);
conf_struct.disappears = atoi(line.substr(line.find('-') + 2, 2).c_str()) * 60
+ atoi(line.substr(line.find('-') + 5, 2).c_str());
conf_struct.comes = 0;
conf_struct.leaves = 0;
}
else
{
if(line.substr(0, 15) == "Through: Zilina")
{
conf_struct.through = "Zilina";
conf_struct.comes = atoi(line.substr(line.find('-') + 2, 2).c_str()) * 60
+ atoi(line.substr(line.find('-') + 5, 2).c_str());
conf_struct.leaves = atoi(line.substr(line.find(',') + 2, 2).c_str()) * 60
+ atoi(line.substr(line.find(',') + + 5, 2).c_str());
}
else
return false;
}
break;
case 3:
if(line.substr(0, 4) == "To: ")
{
conf_struct.to = line.substr(4, line.find('-') - 5);
conf_struct.disappears = atoi(line.substr(line.find('-') + 2, 2).c_str()) * 60
+ atoi(line.substr(line.find('-') + 5, 2).c_str());
conf_struct.late = 0;
}
else
{
if(line.substr(0, 6) == "Late: ")
{
conf_struct.late = atoi(line.substr(line.find(':') + 2, line.length() - 6).c_str());
}
else
return false;
}
break;
case 4:
if(line.substr(0, 6) == "Late: ")
{
conf_struct.late = atoi(line.substr(line.find(':') + 2, line.length() - 6).c_str());
}
else
{
if(line.length() == 0)
break;
else
return false;
}
break;
case 5:
break;
default:
return false;
}
return true;
}
bool CSchedule::ParseAndPlan(CMainStation& mainStation)
{
CSchedule::STrainProcess unit;
unit.name = "";
while(1)
{
if(GetLink(unit))
{
if(unit.name == "EOF")
return true;
CStation* pStationFrom;
CStation* pStationTo;
if(unit.from != "Zilina")
{
if(!mainStation.HasAdjacentStation(unit.from))
{
DBG_LOG("WARINING: " << unit.from << "station not found, skipping..");
continue;
}
pStationFrom = &mainStation.GetAdjacentStation(unit.from);
}
else
{
pStationFrom = &mainStation;
}
if(unit.to != "Zilina")
{
if(!mainStation.HasAdjacentStation(unit.to))
{
DBG_LOG("WARINING: " << unit.to << "station not found, skipping..");
continue;
}
pStationTo = &mainStation.GetAdjacentStation(unit.to);
}
else
{
pStationTo = &mainStation;
}
// travelling over midnight - move to next day
if(!unit.through.empty() )
{
// comes
if(unit.comes < unit.appears)
{
unit.comes += 60*24;
unit.leaves += 60*24;
unit.disappears += 60*24;
DBG_LOG("SHIFT " << unit.name);
}
// leaves
if(unit.leaves < unit.comes)
{
unit.leaves += 60*24;
unit.disappears += 60*24;
DBG_LOG("SHIFT " << unit.name);
}
// disappears
if(unit.disappears < unit.leaves)
{
unit.disappears += 60*24;
DBG_LOG("SHIFT " << unit.name);
}
}
// no stop in main station
else if(unit.disappears < unit.appears)
{
unit.disappears += 60*24;
DBG_LOG("SHIFT " << unit.name);
}
CPublicTrainGenerator::Frequency freq;
if(unit.name[unit.name.length()-1] == 'W')
freq = CPublicTrainGenerator::FREQ_WEEKLY;
else
freq = CPublicTrainGenerator::FREQ_DAILY;
pStationFrom->AddPublicTrain(unit.name,
unit.appears, *pStationTo,
freq, unit.disappears, unit.late,
!unit.through.empty(), unit.comes, unit.leaves);
/*std::cout << "Train " << unit.name << " schedule: \t\t"
<< CTimeInterval::MinutesToTime(unit.appears)
<< ", from " << unit.from
<< " to " << unit.to << std::endl;*/
continue;
}
else
return false;
}
return true;
}
CSchedule::~CSchedule()
{
// TODO Auto-generated destructor stub
}