-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathParser.cpp
More file actions
276 lines (177 loc) · 6.21 KB
/
Copy pathParser.cpp
File metadata and controls
276 lines (177 loc) · 6.21 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* File: Parser.cpp
* Author: michael
*
* Created on 31. mai 2013, 14:44
*/
#include <cctype> // For isalpha() and isdigit().
#include <exception>
#include <iostream>
#include <stdexcept>
#include "Airplane.h"
#include "Cloud.h"
#include "Entity.h"
#include "ForbiddenZone.h"
#include "Parser.h"
namespace ATCsim { // Project ATCsim namespace.
Parser::Parser(std::string configFileName)
:
// Open the configuration file for read operations.
configFile_(configFileName, std::ios::in)
{
// Check if the file is open.
if (!configFile_.is_open()) {
throw std::runtime_error("Unable to open configuation file.");
}
}
void Parser::extract(std::vector< std::unique_ptr<Entity> >& entities) {
try {
if (extractNextTagName() == "System") {
extractSystem(entities);
} else {
throw std::runtime_error("First level should be <System>.");
}
if (extractNextTagName() != "/System") {
throw std::runtime_error("First level should be <System>.");
}
// entities.push_back(std::unique_ptr<Entity> (
// new ForbiddenZone({260, 360, 360, 260},
// {150, 150, 250, 250})));
// entities.push_back(std::unique_ptr<Entity> (
// new ForbiddenZone({480, 670, 690, 780, 670, 650},
// {150, 130, 20, 120, 130, 330})));
// For the moment only forbidden zones are read in the configuration files.
// Directly add clouds and airplanes.
entities.push_back(std::unique_ptr<Entity > (
new Cloud({540, 620, 770, 790, 700, 580},
{400, 350, 400, 500, 540, 500}, 124.2f, 200)));
entities.push_back(std::unique_ptr<Entity > (
new Airplane(1, "AA293", 8000, 260.4f, 800, Airplane::CardinalPoint(Airplane::N), Airplane::CardinalPoint(Airplane::S))));
entities.push_back(std::unique_ptr<Entity > (
new Airplane(2, "LX8829", 9811, 90.3f, 600, Airplane::CardinalPoint(Airplane::S), Airplane::CardinalPoint(Airplane::W))));
entities.push_back(std::unique_ptr<Entity > (
new Airplane(3, "DLH22", 7510, 166.8f, 700, Airplane::CardinalPoint(Airplane::E), Airplane::CardinalPoint(Airplane::S))));
entities.push_back(std::unique_ptr<Entity > (
new Airplane(4, "BER120", 7440, 20.3f, 400, Airplane::CardinalPoint(Airplane::W), Airplane::CardinalPoint(Airplane::E))));
// tag = parseTag();
//
// switch (tag) {
//
// case Avion:
// extraireAvion(/* ici des arguments utiles */);
// break;
//
// default:
// // traitement d'une balise inconnue
// }
} catch (std::exception& e) {
throw std::runtime_error("Configuration extraction error: " + std::string(e.what()));
// std::cerr << "Configuration extraction error: " << e.what() << std::endl;
}
// configFile_.close();
}
bool Parser::findTag() {
char c;
do {
configFile_.get(c);
} while (c != '<' && std::isspace(c));
if (c == '<') {
return true;
// Return false if there is still data to be read.
} else {
configFile_.seekg(-1, std::ios::cur);
return false;
}
}
bool Parser::skipComment() {
char c;
configFile_.get(c);
// If it's a comment tag, skip it.
if (c == '!') {
do {
configFile_.get(c);
} while (c != '>');
return true;
// Else decrement get pointer (we have read the tag name first character)
} else {
configFile_.seekg(-1, std::ios::cur);
return false;
}
}
std::string Parser::readString(int(*firstCharCheck)(int), int(*lastCharCheck)(int)) {
std::string string;
char c;
do {
configFile_.get(c);
} while (!firstCharCheck(c));
while (!lastCharCheck(c)) {
string += c;
configFile_.get(c);
}
return string;
}
int tagFirsCharCheck(int c) {return c == '/' || std::isalpha(c);}
int tagLastCharCheck(int c) {return c == '>';}
std::string Parser::extractNextTagName() {
// Skip all the comments.
do {
// Return an empty string if there is still data to be read.
if (!findTag())
return "";
} while (skipComment());
// Read the tag name.
return readString(&tagFirsCharCheck, &tagLastCharCheck);
}
void Parser::extractSystem(std::vector< std::unique_ptr<Entity> >& entities) {
std::string tagName;
tagName = extractNextTagName();
if (tagName == "ForbiddenZones") {
if (extractForbiddenZones(entities) != "/ForbiddenZones") {
throw std::runtime_error("<ForbiddenZones> must be closed.");
}
} else {
throw std::runtime_error("There must be a <ForbiddenZones> second level.");
}
// if (tagName == "Clouds") {
// if (extractForbiddenZones(entities) != "/ForbiddenZones") {
// throw std::runtime_error("<Clouds> must be closed.");
// }
// } else {
// throw std::runtime_error("There must be a <Clouds> second level.");
// }
}
std::string Parser::extractForbiddenZones(std::vector< std::unique_ptr<Entity> >& entities) {
std::string tagName(extractNextTagName());
while (tagName == "ForbiddenZone") {
std::vector<int16_t> xPoints, yPoints;
if (extractNextTagName() == "xPoints") {
extractPoints(xPoints);
} else {
throw std::runtime_error("ForbiddenZone must contain an xPoints tag.");
}
if (extractNextTagName() == "yPoints") {
extractPoints(yPoints);
} else {
throw std::runtime_error("ForbiddenZone must contain an yPoints tag.");
}
if (extractNextTagName() != "/ForbiddenZone") {
throw std::runtime_error("<ForbiddenZone> must be closed.");
}
Entity* entity = new ForbiddenZone(xPoints,yPoints);
entities.push_back(std::unique_ptr<Entity>(entity));
tagName = extractNextTagName();
}
return tagName;
}
void Parser::extractPoints(std::vector<int16_t>& points) {
std::string string, tagName(extractNextTagName());
while (tagName == "") {
string = readString(&std::isdigit, &std::isblank);
points.push_back(std::atoi(string.c_str()));
tagName = extractNextTagName();
}
if (tagName != "/xPoints" && tagName != "/yPoints") {
throw std::runtime_error("<xPoints> and <yPoints> must be closed.");
}
}
} // End of project ATCsim namespace.