-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.cpp
More file actions
81 lines (61 loc) · 1.61 KB
/
loader.cpp
File metadata and controls
81 lines (61 loc) · 1.61 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
#include "loader.h"
using namespace std;
Loader::Loader() : nom_fichier("")
{}
Loader::Loader(string nom_fichier) : nom_fichier(nom_fichier)
{}
string Loader::get_file_name() const
{
return nom_fichier;
}
void Loader::set_file_name(string nom_fichier)
{
this->nom_fichier = nom_fichier;
}
City Loader::read_line(const string& line, char sep)
{
int x, y, id;
stringstream ss(line);
string tmp;
getline(ss, tmp, sep); // lecture de l'id
id = atoi(tmp.c_str());
getline(ss, tmp, sep); // lecture de l'abscisse
x = atoi(tmp.c_str());
getline(ss, tmp, sep); // lecture de l'ordonnée
y = atoi(tmp.c_str());
City ville(id, x, y);
return ville;
}
void Loader::read_first_line(const string& line)
{
stringstream ss(line);
string tmp, tmp2;
getline(ss, tmp);
tmp2 = tmp.substr(tmp.size() - 3, 3);
nb_ville = atoi(tmp2.c_str());
}
vector<City> Loader::load()
{
ifstream fichier(nom_fichier.c_str());
if (fichier) {
string ligne;
getline(fichier, ligne);
read_first_line(ligne);
tableau_ville.reserve(nb_ville);
tableau_ville.resize(nb_ville);
for (int i = 0; i < 5; ++i) getline(fichier, ligne);
for (int i = 0; i < nb_ville; ++i) {
getline(fichier, ligne);
City ville = read_line(ligne, ' ');
tableau_ville[i] = ville;
}
}
else cerr << "Impossible d'ouvrir le fichier : " << nom_fichier << " !" << endl;
return tableau_ville;
}
void Loader::print()
{
for (int i = 0; i < tableau_ville.size(); ++i) {
cout << "ville : " << tableau_ville[i].get_id() << " " << tableau_ville[i].get_x() << " " << tableau_ville[i].get_y() << endl;
}
}