-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.cpp
60 lines (58 loc) · 1.33 KB
/
files.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
#include "engine.h"
void engine::load(std::string filename)
{
std::fstream file;
file.open(filename, std::fstream::in);
if (!file.is_open())
{
std::cout << "Coudn't open " << filename << std::endl;
return;
}
clear();
int vertex_count;
file >> vertex_count;
for (int i = 0; i < vertex_count; ++i)
{
int id;
float x, y;
std::string name;
file >> id >> name >> x >> y;
add_vertex(id, x, y, name);
}
for (int i = 0; i < vertex_count; ++i)
{
int id, current;
std::string line;
std::getline(file,line);
std::stringstream ss(line);
ss >> id;
while (ss >> current)
{
add_edge(get_vertex(current), get_vertex(id));
}
}
file.close();
}
void engine::save(std::string filename)
{
std::fstream file;
file.open(filename, std::fstream::out | std::fstream::trunc);
if (!file.is_open())
{
std::cout << "Coudn't open " << filename << std::endl;
return;
}
file << vertices.size() << std::endl;
for (auto& vertex : vertices)
{
file << vertex->get_id() << " " << vertex->get_name() << " " << vertex->get_position().x << " " << vertex->get_position().y << std::endl;
}
for (auto& vertex : vertices)
{
file << vertex->get_id() << " ";
for (auto adj : vertex->get_adjacents())
file << adj->get_id() << " ";
file << std::endl;
}
file.close();
}