-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.cpp
118 lines (100 loc) · 2.83 KB
/
configuration.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
106
107
108
109
110
111
112
113
114
115
116
117
118
// © Copyright (c) 2018 SqYtCO
#include "configuration.h"
#include <string>
#include <fstream>
#if __cplusplus < 201703L
#include <experimental/filesystem>
namespace std
{
namespace filesystem = experimental::filesystem;
}
#else
#include <filesystem>
#endif
Configuration::Configuration() : config_saved(true)
{
// try reading in configuration; on failure set default values
if(!read_config())
reset_config();
}
void Configuration::reset_config()
{
// set all default values
size_x = Default_Values::SIZE_X;
size_y = Default_Values::SIZE_Y;
num_of_threads = Default_Values::NUM_OF_THREADS;
relation_dead = Default_Values::RELATION_DEAD;
relation_alive = Default_Values::RELATION_ALIVE;
border_behavior = Default_Values::BORDER_BEHAVIOR;
start_random = Default_Values::START_RANDOM;
survival_rules = Default_Values::SURVIVAL_RULES;
rebirth_rules = Default_Values::REBORN_RULES;
}
bool Configuration::read_config()
{
std::ifstream in(config_path + CONFIG_FILE);
// return on error
if(!in)
return false;
// set default values if properties are missing in config_file
reset_config();
std::string property, value;
// read in property name
while(std::getline(in, property, '='))
{
// read in value of property
std::getline(in, value);
if(property == "size_x")
size_x = std::stoul(value);
else if(property == "size_y")
size_y = std::stoul(value);
else if(property == "num_of_threads")
num_of_threads = std::stoul(value);
else if(property == "relation_dead")
relation_dead = std::stoul(value);
else if(property == "relation_alive")
relation_alive = std::stoul(value);
else if(property == "border_behavior")
border_behavior = static_cast<Border_Behavior>(std::stoul(value));
else if(property == "start_random")
start_random = std::stoul(value);
else if(property == "survival_rules")
survival_rules = std::stoul(value);
else if(property == "rebirth_rules")
rebirth_rules = std::stoul(value);
}
config_saved = true;
// return on success
return true;
}
bool Configuration::write_config()
{
try
{
// check if path exists and create it if not
if(!std::filesystem::exists(config_path))
std::filesystem::create_directories(config_path);
}
catch(...) // if write permission is not granted
{
return false;
}
std::ofstream out(config_path + CONFIG_FILE);
// return on error
if(!out)
return false;
out << "size_x=" << size_x << '\n'
<< "size_y=" << size_y << '\n'
<< "num_of_threads=" << num_of_threads << '\n'
<< "relation_dead=" << relation_dead << '\n'
<< "relation_alive=" << relation_alive << '\n'
<< "border_behavior=" << static_cast<int>(border_behavior) << '\n'
<< "start_random=" << start_random << '\n'
<< "survival_rules=" << survival_rules << '\n'
<< "rebirth_rules=" << rebirth_rules;
// return on error
if(!out)
return false;
config_saved = true;
return true;
}