-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataParser.cpp
327 lines (310 loc) · 8.63 KB
/
DataParser.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#pragma warning(disable : 4996)
//================================================================
// DataParser.h
// Purpose: Implementation file for the DataParser utility class.
// Author: Dr. Rick Coleman
//================================================================
#define _CRT_SECURE_NO_DEPRECATE // Shut up MS, about strcpy(), etc.
#define _CRT_SECURE_NO_WARNINGS // just don't say anything.
#include "DataParser.h"
#include <cctype>
using namespace std;
//------------------------------------------------
// Default constructor
//------------------------------------------------
DataParser::DataParser()
{
// Initialize everything
m_iWorldWidth = -1;
m_iWorldHeight = -1;
m_iNumDOCOs = 0;
m_iNextDOCOIndex = 0;
m_iNextObsIndex = 0;
m_iObstacleCount = 0;
}
//------------------------------------------------
// Open data file and init everything
//------------------------------------------------
void DataParser::initParser(const char* fileName)
{
// Initialize everything
char line[128];
strcpy(m_sFileName, fileName); // save file name
inFile = new ifstream();
inFile->open(m_sFileName, fstream::in); // Open the data file
if (inFile->is_open())
{
// Get the basic information
while (getNextLine(line, 127))
{
if (strcmp(line, "<GRID_WIDTH>") == 0)
{
getNextLine(line, 127); // Get grid width
m_iWorldWidth = atoi(line);
}
else if (strcmp(line, "<GRID_HEIGHT>") == 0)
{
getNextLine(line, 127); // Get grid height
m_iWorldHeight = atoi(line);
}
else if (strcmp(line, "<DOCO>") == 0)
{
m_iNumDOCOs++; // Count all DOCOs
}
else if (strcmp(line, "<FOOD>") == 0)
{
getNextLine(line, 127); // Get grid width
m_iFoodCount = atoi(line);
}
else if (strcmp(line, "<OBSTACLE>") == 0)
{
m_iObstacleCount++; // Count all Obstacles
}
}
inFile->close();
}
else
cout << "Failed to open file\n";
}
//------------------------------------------------
// Default destructor
//------------------------------------------------
DataParser::~DataParser()
{
}
//------------------------------------------------
// Get the singleton instance
//------------------------------------------------
DataParser* DataParser::getInstance(const char* fileName)
{
static DataParser* instance = NULL;
if (instance == NULL) // If first time calling
{
instance = new DataParser();
instance->initParser(fileName);
}
return instance;
}
//------------------------------------------------
// Get the width of the world
//------------------------------------------------
int DataParser::getDOCOWorldWidth()
{
return m_iWorldWidth;
}
//------------------------------------------------
// Get the height of the world
//------------------------------------------------
int DataParser::getDOCOWorldHeight()
{
return m_iWorldHeight;
}
//------------------------------------------------
// Get the number of DOCOs in the world
//------------------------------------------------
int DataParser::getDOCOCount()
{
return m_iNumDOCOs;
}
//--------------------------------------------------------------------------
// Get the information on a DOCO
// Args: movement - Pointer to character array to hold movement type string
// xpos - Pointer to int into which starting X position of the DOCO
// will be placed.
// ypos - Pointer to int into which starting Y position of the DOCO
// will be placed.
// Returns: TRUE if data is valid. Returns FALSE when all DOCOs have been
// read.
//--------------------------------------------------------------------------
bool DataParser::getDOCOData(char* movement, int* xpos, int* ypos)
{
int dNum = 0;
char line[128];
// Reopen the file
inFile = new ifstream();
inFile->open(m_sFileName, fstream::in);
if (inFile->is_open())
{
// Read to the the current DOCO count
while (getNextLine(line, 127))
{
if (strcmp(line, "<DOCO>") == 0) // Got one
{
if (dNum == m_iNextDOCOIndex)
{
// Get data on this one
while (getNextLine(line, 127))
{
// Get the type
if (strcmp(line, "<MOVEMENT>") == 0)
{
if (getNextLine(line, 127))
{
strcpy(movement, line); // Set the movement style
}
else
return false; // Oops!
}
else if (strcmp(line, "<XPOS>") == 0)
{
if (getNextLine(line, 127))
{
*xpos = atoi(line); // Set the X position
}
else
return false; // Oops!
}
else if (strcmp(line, "<YPOS>") == 0)
{
if (getNextLine(line, 127))
{
*ypos = atoi(line); // Set the X position
}
else
return false; // Oops!
}
else if (strcmp(line, "</DOCO>") == 0)
{
m_iNextDOCOIndex++; // Increment for next call to this function
return true; // Got it
}
} // end while
} // end if(sNum == nextSensor)
else
{
dNum++; // Check the next one
}
}
}
inFile->close();
} // end if file open
return false; // If we get here we have got all the DOCOs
}
//------------------------------------------------
// Get the information on food
//------------------------------------------------
int DataParser::getFoodCount()
{
return m_iFoodCount;
}
//------------------------------------------------
// Get obstacle count
//------------------------------------------------
int DataParser::getObstacleCount()
{
return m_iObstacleCount;
}
//--------------------------------------------------------------------------
// Get the information on an obstacle
// Args: xpos - Pointer to int into which the X position of the obstacle
// will be placed.
// ypos - Pointer to int into which the Y position of the obstacle
// will be placed.
// Returns: TRUE if data is valid. Returns FALSE when all obstacles have
// been read.
//--------------------------------------------------------------------------
bool DataParser::getObstacleData(int* xpos, int* ypos)
{
int dNum = 0;
char line[128];
// Reopen the file
inFile = new ifstream();
inFile->open(m_sFileName, fstream::in);
if (inFile->is_open())
{
// Read to the the current Obstacle count
while (getNextLine(line, 127))
{
if (strcmp(line, "<OBSTACLE>") == 0) // Got one
{
if (dNum == m_iNextObsIndex)
{
// Get data on this one
while (getNextLine(line, 127))
{
// Get the xPOS
if (strcmp(line, "<XPOS>") == 0)
{
if (getNextLine(line, 127))
{
*xpos = atoi(line); // Set the X position
}
else
return false; // Oops!
}
else if (strcmp(line, "<YPOS>") == 0)
{
if (getNextLine(line, 127))
{
*ypos = atoi(line); // Set the X position
}
else
return false; // Oops!
}
else if (strcmp(line, "</OBSTACLE>") == 0)
{
m_iNextObsIndex++; // Increment for next call to this function
return true; // Got it
}
} // end while
} // end if(sNum == nextSensor)
else
{
dNum++; // Check the next one
}
}
}
inFile->close();
} // end if file open
return false; // If we get here we have got all the DOCOs
}
//------------------------------------------------
// Function: getNextLine()
// Purpose: Reads lines from a file and places
// them in buffer, removing any leading white
// space. Skips blank lines. Ignors comment
// lines starting with <!-- and ending with -->
//
// Args: buffer -- char array to read line into.
// n -- length of buffer.
// Returns: True if a line was successfully read,
// false if the end of file was encountered.
// Notes: Function provided by instructor.
//------------------------------------------------
bool DataParser::getNextLine(char* buffer, int n)
{
bool done = false;
char tempBuf[128];
char* temp;
while (!done)
{
inFile->getline(tempBuf, n); // Read a line from the file
if (inFile->good()) // If a line was successfully read check it
{
if (strlen(tempBuf) == 0) // Skip any blank lines
continue;
else if (strncmp(tempBuf, "<!--", 4) == 0) // Skip comment lines
continue;
else done = true; // Got a valid data line so return with this line
}
else
{
strcpy(buffer, ""); // Clear the buffer array
return false; // Flag end of file
}
} // end while
// Remove white space from end of string
temp = &tempBuf[strlen(tempBuf)]; // point to closing \0
temp--; // back up 1 space
while (isspace(*temp))
{
*temp = '\0'; // Make it another NULL terminator
temp--; // Back up 1 char
}
// Remove white space from front of string
temp = tempBuf;
while (isspace(*temp)) temp++; // Skip leading white space
// Copy remainder of string into the buffer
strcpy(buffer, temp);
return true; // Flag a successful read
}