-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapLoader.cpp
More file actions
463 lines (373 loc) · 14 KB
/
Copy pathMapLoader.cpp
File metadata and controls
463 lines (373 loc) · 14 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// Created by Claudia on 2020-10-05.
//
#include "MapLoader.h"
#include <string.h>
#include <errno.h>
using namespace std;
/////////////////////////////////////////////////////////////////////////////
/// MAPLOADER ///
/////////////////////////////////////////////////////////////////////////////
MapLoader::MapLoader()= default;
//load all files in testing directory
void MapLoader::loadMaps() {
string name = "";
mainPath = "";
cout << "In order to search for the Maps in your computer, please enter your first name" << endl;
cin >> name;
if (name == "clau") {
// can use relative paths on mac
mainPath = "../testing/";
} else if (name == "james") {
mainPath = "C://Users/James/source/repos/TestWarzone1/testing/";
} else if ( name == "gini") {
mainPath = "C:/Users/gini/source/repos/claudiiafg/Warzone/testing/";
} else if (name == "ian") {
mainPath = "/home/ian/Warzone/Warzone/testing/";
} else if (name == "ta") {
mainPath = "../testing/";
} else {
cout << "no option for you, please go look into MapLoader::loadMaps() and get yourself an personalized option";
}
vector<string> fileNames = {
"brasil.cards",
"brasil.map",
"brasil_map.gif",
"brasil_pic.gif",
"canada.cards",
"canada.map",
"canada.gif",
"canada.gif",
"europe.cards",
"europe.map",
"europe.gif",
"europe.gif",
"Andorra.map",
"Andorra.bmp",
"Atlantis.map",
"Atlantis.bmp"
};
for(auto &fileName : fileNames) {
string parendFolder = fileName.substr(0,fileName.find_last_of('.'));
string path_name = mainPath + parendFolder + "/" + fileName;
// check for file with map data and create MapFiles
if(isMapType(path_name)) {
cout << "->VALID File: " << fileName << endl;
vector<string> content = getContent(path_name);
string name = fileName;
maps.push_back(new MapFile(name, content));
} else {
cout << "Invalid: " << fileName << endl;
}
}
}
// validate map type
bool MapLoader::isMapType(const string& path) {
//get file extension
string ext = path.substr(path.find_last_of('.'));
// only look for files with .map extension
if(ext != ".map") {
return false;
}
ifstream myFile (path);
// check for errors opening the file
if (!myFile.is_open()) {
cout<<"It failed\n"<<strerror(errno)<<endl;
return false;
}
// close the file
myFile.close();
return true;
}
// extract map data
vector<string> MapLoader::getContent(const string& path) {
ifstream myFile (path);
string lineContent;
vector<string> vecOfStr;
// push each line of file into vector of strings (easier to find / parse information)
while ( getline(myFile, lineContent)) {
vecOfStr.push_back(lineContent);
}
// Close the file
myFile.close();
return vecOfStr;
}
// prints and returns maps available
vector<MapFile*> MapLoader::getMaps() {
return maps;
}
// streams insertion operator
ostream& operator<<(ostream &os, const MapLoader& n) {
return os << "Files loaded: " << n.maps.size() << endl;
}
// assignment operator
MapLoader& MapLoader::operator = (const MapLoader& loader){
maps = loader.maps;
return *this;
}
/////////////////////////////////////////////////////////////////////////////
/// MAPFILE ///
/////////////////////////////////////////////////////////////////////////////
// MapFile constructor -> creates valid map files to be turned into Graphs
MapFile::MapFile(string _name, vector<string> fullContent) {
name = _name;
content = fullContent;
}
// copy constructor
MapFile::MapFile(const MapFile& _file) {
name =_file.name;
content = _file.content;
}
// streams insertion operator
ostream& operator<<(ostream &os, const MapFile& n) {
return os << n.name << endl;
}
// assignment operator
MapFile& MapFile::operator = (const MapFile& _file) {
name = _file.name;
content = _file.content;
return *this;
}
// destructor
MapFile::~MapFile() = default;
/////////////////////////////////////////////////////////////////////////////
/// CONQUEST-FILE-READER ///
/////////////////////////////////////////////////////////////////////////////
ConquestFileReader::ConquestFileReader() = default;
ConquestFileReader::ConquestFileReader(const ConquestFileReader &_file) {
map = _file.map;
}
ConquestFileReader::~ConquestFileReader() {
delete map;
map = nullptr;
}
ConquestFileReader::ConquestFileReader(MapFile *otherMap) {
map = otherMap;
}
vector<string> ConquestFileReader::getContent(const string &path) {
ifstream myFile (path);
string lineContent;
vector<string> vecOfStr;
// push each line of file into vector of strings (easier to find / parse information)
while ( getline(myFile, lineContent)) {
vecOfStr.push_back(lineContent);
}
// Close the file
myFile.close();
// conquest identifiers
const string CONT_ID_CQ = "Continents";
const string TERR_ID_CQ = "Territories";
// get indexes of each section
int contI = 0;
int terrI = 0;
for (size_t i = 0; i < vecOfStr.size(); ++i){
if(vecOfStr[i].find(CONT_ID_CQ) < vecOfStr.size() && vecOfStr[i].find(CONT_ID_CQ) > 0) {
contI = i;
} else if(vecOfStr[i].find(TERR_ID_CQ) < vecOfStr.size() && vecOfStr[i].find(TERR_ID_CQ) > 0) {
terrI = i;
}
}
// get all continent strings from map data
vector<string> tempContinents;
for (int i = (contI + 1); i < (terrI); ++i){
tempContinents.push_back(vecOfStr[i]);
}
// get all territories strings from map data
vector<string> tempTerritories;
for (int i = (terrI + 1); i < (vecOfStr.size()); ++i){
tempTerritories.push_back(vecOfStr[i]);
}
// get each sections of map adapted
vector<string> finalContinents = adaptContinents(tempContinents);
vector<string> finalTerritories = adaptTerritories(tempTerritories);
vector<string> finalBorders = adaptBorders(tempTerritories, finalTerritories);
// add empty lines between sections
vector<string> finalVecStr;
finalContinents.emplace_back("\n");
finalTerritories.emplace_back("\n");
finalBorders.emplace_back("\n");
// merge all sections together
finalVecStr.insert( finalVecStr.end(), finalContinents.begin(), finalContinents.end());
finalVecStr.insert( finalVecStr.end(), finalTerritories.begin(), finalTerritories.end() );
finalVecStr.insert( finalVecStr.end(), finalBorders.begin(), finalBorders.end() );
return finalVecStr;
}
string ConquestFileReader::findIdOfTerritory(string terrName, const vector<string> finalTerritories) {
string name = terrName.substr(0, terrName.length() - 1);
for_each(name.begin(), name.end(), [name](char & c) {
c = ::tolower(c);
});
for(auto & line: finalTerritories) {
if(line != "[countries]") {
string tempLine = line;
for_each(tempLine.begin(), tempLine.end(), [tempLine](char & c) {
c = ::tolower(c);
});
if (tempLine.find(name) != std::string::npos) {
string substr;
string tempName;
stringstream ss(tempLine);
getline( ss, substr, ' ');
getline( ss, tempName, ' ');
if(ss.good()) {
if(name.size() - tempName.size() < 2 || tempName.size() - name.size() < 2) {
return substr;
}
}
}
}
}
return "not found";
}
int ConquestFileReader::nthOccurrence(const string &str, const string &findMe, int nth) {
size_t pos = 0;
int counter = 0;
while(counter != nth) {
pos += 1;
pos = str.find(findMe, pos);
if (pos == string::npos) return -1;
counter++;
}
return pos;
}
ostream &operator<<(ostream &os, const ConquestFileReader &l) {
return os << l.map << endl;
}
ConquestFileReader &ConquestFileReader::operator=(const ConquestFileReader &mapToAssign) {
map = mapToAssign.map;
return *this;
}
vector<string> ConquestFileReader::adaptTerritories(const vector<string> tempTerritories) {
//flags
int continentCounter = 1;
int territoryCounter = 0;
bool previousWasEmpty = false;
//constants
const string COMMA = ",";
const string TERR_ID_WZ = "[countries]";
//vars
string terrName;
size_t pos;
vector<string> finalTerritories;
finalTerritories.push_back(TERR_ID_WZ);
for(auto & terr: tempTerritories) {
// two consecutive new lines means nothing more to search through
if(previousWasEmpty && (terr.size() == 1 || terr.size() == 0)) {
break;
}
// if current new line next territories are from another continent
if(terr.size() == 1 || terr.size() == 0) {
continentCounter += 1;
previousWasEmpty = true;
// if territory data push to vector in right format
} else {
territoryCounter += 1;
pos = terr.find(COMMA);
terrName = terr.substr (0, pos);
replace(terrName.begin(), terrName.end(), ' ', '-');
finalTerritories.push_back(to_string(territoryCounter) + " " + terrName + " " + to_string(continentCounter));
previousWasEmpty = false;
}
}
return finalTerritories;
}
vector<string> ConquestFileReader::adaptBorders(const vector<string> tempTerritories, const vector<string> finalTerritories) {
// flags
bool previousWasEmpty = false;
int territoryCounter = 0;
// constacts
const string COMMA = ",";
const string ADJ_ID_WZ = "[borders]";
// vars
size_t initIndex;
vector<string> finalAdjacents;
finalAdjacents.push_back(ADJ_ID_WZ);
for(auto & terr: tempTerritories) {
// two consecutive new lines means nothing more to search through
if (previousWasEmpty && (terr.size() == 1 || terr.size() == 0)) {
break;
}
if (terr.size() == 1 || terr.size() == 0) {
previousWasEmpty = true;
} else {
previousWasEmpty = false;
territoryCounter += 1;
// start at 4th comma
initIndex = nthOccurrence(terr, COMMA, 4);
string adjacentList = terr.substr (initIndex);
stringstream ss(adjacentList);
vector<string> adjacentVector;
// start territory adjacent's line with territory node id
string finalAdjLine = to_string(territoryCounter);
vector<string> tempFinal;
// split line by commas
while(ss.good()) {
string substr;
getline( ss, substr, ',');
// push only unique adjacent territories
if (find(adjacentVector.begin(), adjacentVector.end(), substr) == adjacentVector.end()) {
replace(substr.begin(), substr.end(), ' ', '-');
adjacentVector.push_back(substr);
}
}
// loop through adjacent territories and find their id -> push to territory adjacent's line
for (int i = 1; i < adjacentVector.size(); i++) {
string nodeId = findIdOfTerritory(adjacentVector[i], finalTerritories);
if(nodeId != "not found" && (!(std::find(tempFinal.begin(), tempFinal.end(), nodeId) != tempFinal.end()))) {
finalAdjLine += " " + nodeId;
tempFinal.push_back(nodeId);
}
}
// push to territory adjacent's line'' vector
finalAdjacents.push_back(finalAdjLine);
}
}
return finalAdjacents;
}
vector<string> ConquestFileReader::adaptContinents(const vector<string> tempContinents) {
const string CONT_ID_WZ = "[continents]";
const string EQUAL = "=";
vector<string> finalContinents;
size_t pos;
string continentName;
string continentBonus;
finalContinents.push_back(CONT_ID_WZ);
for(auto & continent: tempContinents) {
// if it is not newl line character
if(continent.size() != 1) {
pos = continent.find(EQUAL);
continentName = continent.substr (0, pos);
replace(continentName.begin(), continentName.end(), ' ', '-');
continentBonus = continent.substr (pos + 1);
finalContinents.push_back(continentName + " " + continentBonus);
}
}
return finalContinents;
}
/////////////////////////////////////////////////////////////////////////////
/// CONQUEST-FILE-READER-ADAPTER ///
/////////////////////////////////////////////////////////////////////////////
ConquestFileReaderAdapter::ConquestFileReaderAdapter() = default;
ConquestFileReaderAdapter::ConquestFileReaderAdapter(ConquestFileReader *toAdapt) {
conquestFileReader = toAdapt;
}
ConquestFileReaderAdapter::ConquestFileReaderAdapter(const ConquestFileReaderAdapter &_file) {
conquestFileReader = _file.conquestFileReader;
}
ConquestFileReaderAdapter::~ConquestFileReaderAdapter() {
delete conquestFileReader;
conquestFileReader = nullptr;
}
vector<string> ConquestFileReaderAdapter::getContent(const string& path) {
string fileName = conquestFileReader->map->name;
string parendFolder = fileName.substr(0,fileName.find_last_of('.'));
string path_name = path + parendFolder + "/" + fileName;
return conquestFileReader->getContent(path_name);
}
ostream &operator<<(ostream &os, const ConquestFileReaderAdapter &l) {
return os << l.conquestFileReader << endl;
}
ConquestFileReaderAdapter &ConquestFileReaderAdapter::operator=(const ConquestFileReaderAdapter &adapter) {
conquestFileReader = adapter.conquestFileReader;
return *this;
}