-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.hpp
More file actions
69 lines (59 loc) · 1.56 KB
/
settings.hpp
File metadata and controls
69 lines (59 loc) · 1.56 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
#ifndef SETTINGS_H
#define SETTINGS_H
#include <string>
#include <map>
#include <iostream>
#include <fstream>
using namespace std;
namespace Settings {
using list = map<string, string>;
const string path = string(getenv("HOME")) + "/.kanttiinit";
const string delimiter = "===";
void set(string key, string value);
list get_all();
void set_all(list settings);
pair<bool, string> get(string key);
string get(string key, string default_value);
}
void Settings::set(string key, string value) {
Settings::list all = Settings::get_all();
if (all.find(key) != all.end()) {
all.erase(key);
}
all.emplace(key, value);
Settings::set_all(all);
}
pair<bool, string> Settings::get(string key) {
Settings::list all = Settings::get_all();
if (all.find(key) != all.end()) {
return make_pair(true, all[key]);
}
return make_pair(false, "");
}
string Settings::get(string key, string default_value) {
auto s = Settings::get(key);
return s.first ? s.second : default_value;
}
Settings::list Settings::get_all() {
ifstream file(Settings::path);
Settings::list list;
if (file.is_open()) {
string line;
while (getline(file, line)) {
size_t pos = line.find(Settings::delimiter);
if (pos != string::npos) {
list.emplace(line.substr(0, pos), line.substr(pos + 3, line.length()));
}
}
file.close();
}
return list;
}
void Settings::set_all(Settings::list settings) {
ofstream file(Settings::path);
for (auto& s : settings) {
file << s.first << Settings::delimiter << s.second << endl;
}
file.close();
}
#endif