forked from joaoslva/DA_Project_1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsvReader.cpp
106 lines (86 loc) · 2.83 KB
/
CsvReader.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
#include "CsvReader.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
std::vector<Station> CsvReader::readStations(const std::string &filename) {
std::vector<Station> stations;
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error opening file: " << filename << std::endl;
return stations;
}
std::string line;
std::string name, district, municipality, township, lineName;
int id = 1;
getline(file, line); // Skip header line
while (getline(file, line)) {
std::istringstream iss(line);
if(iss.peek() == '"'){
iss.ignore();
std::string temp;
getline(iss, temp, '"');
getline(iss, name, ',');
name = temp + name;
} else {
getline(iss, name, ',');
}
getline(iss, district, ',');
getline(iss, municipality, ',');
if(iss.peek() == '"'){
iss.ignore();
std::string temp;
getline(iss, temp, '"');
getline(iss, township, ',');
township = temp + township;
} else {
getline(iss, township, ',');
}
getline(iss, lineName, ',');
stations.emplace_back(id, name, district, municipality, township, lineName);
id++;
}
file.close();
return stations;
}
std::vector<Railway> CsvReader::readNetwork(const std::string &filename){
std::vector<Railway> networkSegments;
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error opening file: " << filename << std::endl;
return networkSegments;
}
std::string line;
getline(file, line); // Skip header line
std::string sourceStation, destinyStation, capacityString, service;
int capacity;
while (getline(file, line))
{
std::istringstream line_stream(line);
if(line_stream.peek() == '"'){
line_stream.ignore();
std::string temp;
getline(line_stream, temp, '"');
getline(line_stream, sourceStation, ',');
sourceStation = temp + sourceStation;
} else {
getline(line_stream, sourceStation, ',');
}
if(line_stream.peek() == '"'){
line_stream.ignore();
std::string temp;
getline(line_stream, temp, '"');
getline(line_stream, destinyStation, ',');
destinyStation = temp + destinyStation;
} else {
getline(line_stream, destinyStation, ',');
}
getline(line_stream, capacityString, ',');
getline(line_stream, service, ',');
capacity = stoi(capacityString);
networkSegments.emplace_back(sourceStation, destinyStation, capacity, service);
}
file.close();
return networkSegments;
}