-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.C
103 lines (82 loc) · 2.33 KB
/
configuration.C
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
# include <configuration.H>
Configuration * Configuration::instance = 0;
Configuration::Configuration()
: grid_width(3), grid_height(3), num_iterations(10), mu(5.0)
{
// Empty
}
Configuration * Configuration::get_instance()
{
if (instance == 0)
instance = new Configuration();
return instance;
}
Configuration::~Configuration()
{
if (instance != 0)
delete instance;
}
const unsigned long & Configuration::get_grid_width() const
{
return grid_width;
}
const unsigned long & Configuration::get_grid_height() const
{
return grid_height;
}
const unsigned long long & Configuration::get_num_iterations() const
{
return num_iterations;
}
const double & Configuration::get_mu() const
{
return mu;
}
void Configuration::set_grid_width(const unsigned long & w)
{
grid_width = w;
}
void Configuration::set_grid_height(const unsigned long & h)
{
grid_height = h;
}
void Configuration::set_num_iterations(const unsigned long long & n)
{
num_iterations = n;
}
void Configuration::set_mu(const double & m)
{
mu = m;
}
void Configuration::save()
{
std::ofstream file("properties.conf", std::ios::out | std::ios::binary);
if (not file)
throw std::logic_error("Cannot open file");
char header[HEADER_SIZE];
strcpy(header, "***ONS***OPTICALNETSIMULATOR***BY_ALEJANDRO_Y_ANNA***");
file.write(header, HEADER_SIZE);
file.write(reinterpret_cast<char *>(&grid_width), sizeof(unsigned long));
file.write(reinterpret_cast<char *>(&grid_height), sizeof(unsigned long));
file.write(reinterpret_cast<char *>(&num_iterations), sizeof(unsigned long long));
file.write(reinterpret_cast<char *>(&mu), sizeof(double));
file.close();
}
void Configuration::load()
{
std::ifstream file("properties.conf", std::ios::in | std::ios::binary);
if (not file)
throw std::logic_error("Cannot open file");
char header[HEADER_SIZE];
file.read(header, HEADER_SIZE);
if (strcmp(header, "***ONS***OPTICALNETSIMULATOR***BY_ALEJANDRO_Y_ANNA***"))
{
file.close();
throw std::logic_error("Invalid file");
}
file.read(reinterpret_cast<char *>(&grid_width), sizeof(unsigned long));
file.read(reinterpret_cast<char *>(&grid_height), sizeof(unsigned long));
file.read(reinterpret_cast<char *>(&num_iterations), sizeof(unsigned long long));
file.read(reinterpret_cast<char *>(&mu), sizeof(double));
file.close();
}