forked from mbwilgus/comp345-winter2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapLoader.cpp
More file actions
109 lines (98 loc) · 2.76 KB
/
Copy pathMapLoader.cpp
File metadata and controls
109 lines (98 loc) · 2.76 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
#include <fstream>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
#include "MapLoader.h"
#include "Map.h"
#include "Resource.h"
/* Resource getResourceByName(string r); */
Map* MapLoader::load(string& fname)
{
UndirectedGraph<City> graph;
ifstream input;
input.open(fname);
string sentinal;
string infoLine;
if(input.eof()) return nullptr;
input >> sentinal;
if(sentinal != "--CITIES--") return nullptr;
unordered_map<string, City> cities;
while(input >> infoLine)
{
if(infoLine == sentinal) break;
size_t start = 0;
size_t end = infoLine.find(":");
string name = infoLine.substr(start, end - start);
start = end + 1;
int region = stoi(infoLine.substr(start));
City city(name, region);
graph.addVertex(city);
cities.insert({name, city});
}
if(input.eof()) return nullptr;
input >> sentinal;
if(sentinal != "--CONNECTIONS--") return nullptr;
while(input >> infoLine)
{
if(infoLine == sentinal) break;
size_t start = 0;
size_t end = infoLine.find(":", start);
string source = infoLine.substr(start, end - start);
start = end + 1;
end = infoLine.find(":", start);
string dest = infoLine.substr(start, end - start);
start = end + 1;
int cost = stoi(infoLine.substr(start));
graph.addEdge(cities[source], cities[dest], cost);
}
Map* powerGrid = new Map(graph);
// if(!input.eof())
// {
// input >> sentinal;
// if(sentinal != "--MARKET--")
// {
// delete powerGrid;
// return nullptr;
// }
// while(input >> infoLine)
// {
// powerGrid->addResourceToMarket(getResourceByName(infoLine));
// }
// }
// if(!input.eof())
// {
// input >> sentinal;
// if(sentinal != "--POOL--")
// {
// delete powerGrid;
// return nullptr;
// }
// while(input >> infoLine)
// {
// powerGrid->addResourceToPool(getResourceByName(infoLine));
// }
// }
// if(!input.eof())
// {
// input >> sentinal;
// if(sentinal != "--BANK--")
// {
// delete powerGrid;
// return nullptr;
// }
// input >> infoLine;
// powerGrid->addElektroToBank(stoi(infoLine));
// }
input.close();
return powerGrid;
}
/* Resource getResourceByName(string r) */
/* { */
/* if(r == "coal") return COAL; */
/* if(r == "oil") return OIL; */
/* if(r == "garbage") return GARBAGE; */
/* if(r == "uranium") return URANIUM; */
/* return COAL; // keep compiler happy */
/* } */