-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandeling.cpp
More file actions
138 lines (137 loc) · 3.7 KB
/
FileHandeling.cpp
File metadata and controls
138 lines (137 loc) · 3.7 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
// Functions Loading Data into Data Structures
#include "Graph.cpp"
#include "fstream"
using namespace std;
LinkedList<Intersection> loadIntersections()
{
LinkedList<Intersection> l;
for (int i = 'A'; i <= 'Z'; i++)
{
l.append(Intersection(i));
}
return l;
}
LinkedList<Road> readRoad(string filename = "road_network.csv")
{
ifstream file(filename);
string line;
LinkedList<Road> l;
getline(file, line);
while (getline(file, line))
{
char inter_from = line[0];
char inter_to = line[2];
int cost = stoi(line.substr(4));
l.append(Road(inter_from, inter_to, cost));
}
return l;
}
LinkedList<Vehicle> readEVehicle(string filename = "emergency_vehicles.csv")
{
ifstream file(filename);
string line, sub = "blank";
LinkedList<Vehicle> l;
getline(file, line);
while (getline(file, line))
{
string id, priority;
char inter_from, inter_to;
int count = -1;
for (int i = 0; i < line.length() + 1; i++)
{
if (line[i] == ',' || line[i] == '\n' || line[i] == '\r')
{
count++;
sub = line.substr(0, i);
line = line.substr(i + 1);
i = -1;
}
if (i != -1 && line[i] == '\000')
{
sub = line.substr(0, i);
count++;
}
if (sub == "")
{
return l;
}
switch (count)
{
case 0: // string read
id = sub;
break;
case 1: // 1st inter
inter_from = sub[0];
break;
case 2: // 2nd inter
inter_to = sub[0];
break;
case 3: // 2nd inter
priority = sub;
break;
default:
break;
}
}
l.append(Vehicle(id, inter_from, inter_to, priority));
}
return l;
}
LinkedList<Vehicle> readVehicle(string filename = "vehicles.csv")
{
ifstream file(filename);
string line, sub;
LinkedList<Vehicle> l;
getline(file, line);
while (getline(file, line))
{
string id;
char inter_from, inter_to;
int count = -1;
for (int i = 0; i < line.length(); i++)
{
if (line[i] == ',' || line[i] == '\n' || line[i] == '\r' || line[i] == '\000')
{
count++;
sub = line.substr(0, i);
line = line.substr(i + 1);
i = 0;
}
switch (count)
{
case 0: // string read
id = sub;
break;
case 1: // 1st inter
inter_from = sub[0];
break;
case 2: // 2nd inter
inter_to = sub[0];
break;
default:
break;
}
}
l.append(Vehicle(id, inter_from, inter_to));
}
return l;
}
// int main()
// {
// LinkedList<Vehicle> vl = readVehicle();
// cout << "Vehicles: \n";
// for (int i = 0; i < vl.size; i++)
// {
// cout << vl[i].id << " " << vl[i].source << " " << vl[i].destination << " " << vl[i].priority << endl;
// }
// LinkedList<Vehicle> evl = readEVehicle();
// for (int i = 0; i < evl.size; i++)
// {
// cout << evl[i].id << " " << evl[i].source << " " << evl[i].destination << " " << evl[i].priority << endl;
// }
// LinkedList<Road> rl = readRoad();
// for (int i = 0; i < rl.size; i++)
// {
// cout << rl[i].from << " " << rl[i].to << " " << rl[i].travel_time << endl;
// }
// }