-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
156 lines (129 loc) · 2.86 KB
/
config.cpp
File metadata and controls
156 lines (129 loc) · 2.86 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "config.h"
Config::Config(string filePath)
: m_filePath(filePath)
{
}
Config::~Config(){}
void Config::loadDefaultConfiguration()
{
m_data = map<string,string>
{
// Default setting goes here
{"window_width", "800"},
{"window_height", "600"},
};
}
void Config::load()
{
std::ifstream fileStream(m_filePath);
string line;
this->loadDefaultConfiguration();
// Create the file with default configuration if it's not there
if (!fileStream.is_open())
this->save();
else
{
// Read file line by line
while(std::getline(fileStream, line))
{
string key;
string value;
bool keyFound = false;
bool readingValue = false;
// Read character by character
for(char x : line)
{
// Comment
if( x == '#' || x == '!' || x == ';')
break;
// Ignore whitespace if we are reading the key
else if((x == ' ' || x == '\t') && !readingValue)
continue;
else if(x == '=')
keyFound = true;
else if (!keyFound)
key.push_back(x);
else if (keyFound)
{
value.push_back(x);
readingValue = true;
}
}
// Insert {key,value} in the map
if(keyFound)
this->set(key, value);
}
}
fileStream.close();
}
void Config::save() const
{
std::fstream fileStream(m_filePath, std::fstream::out);
fileStream << "# Configuration" << std::endl;
fileStream << std::endl;
for(auto x : m_data)
fileStream << x.first << " = " << x.second << std::endl;
fileStream.close();
}
void Config::print() const
{
for(auto x : m_data)
std::cout << x.first << " = " << x.second << std::endl;
}
void Config::set(const string& key, const string& value)
{
// Check if already in the map
if (m_data.find(key) != m_data.end())
m_data.at(key) = value;
else
m_data.insert({key, value});
}
void Config::set(const string& key, bool value)
{
this->set(key, string((value) ? "true" : "false"));
}
void Config::set(const string& key, float value)
{
this->set(key, std::to_string(value));
}
void Config::set(const string& key, int value)
{
this->set(key, std::to_string(value));
}
void Config::set(const string& key, const char* value)
{
this->set(key, string(value));
}
string Config::get(const string& key) const
{
if (m_data.find(key) != m_data.end())
return m_data.at(key);
else
return "";
}
bool Config::getBool(const string& key) const
{
if(m_data.find(key) != m_data.end())
{
string value = m_data.at(key);
std::transform(value.begin(), value.end(), value.begin(), tolower);
if( value == "true" || value == "1" ||
value == "on" || value == "enable")
return true;
}
return false;
}
float Config::getFloat(const string& key) const
{
if(m_data.find(key) != m_data.end())
return std::atof(m_data.at(key).c_str());
else
return 0.f;
}
int Config::getInt(const string& key) const
{
if(m_data.find(key) != m_data.end())
return std::atoi(m_data.at(key).c_str());
else
return 0;
}