-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.cpp
68 lines (52 loc) · 1.02 KB
/
Config.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
#include <map>
#include <fstream>
#include "Config.h"
#include "json/json.h"
static std::mutex mtx;
Conf::Conf()
{
std::ifstream file("config.json");
file >> _root;
if (!_root.isObject())
{
printf("ERROR TO READ config.json, type: %d\n", _root.type());
return;
}
file.close();
}
Conf::~Conf()
{
SaveData();
}
void Conf::SetValue(const char* index, const char* value)
{
mtx.lock();
_root[index] = value;
mtx.unlock();
};
void Conf::SetValue(const char* index, int value)
{
mtx.lock();
_root[index] = value;
mtx.unlock();
};
std::string Conf::GetString(const char* index) const
{
if (!_root.isMember(index) || !_root[index].isString())
return "NONE";
return _root[index].asString();
}
int Conf::GetInt(const char* index) const
{
if (!_root.isMember(index) || !_root[index].isInt())
return 0;
return _root[index].asInt();
}
void Conf::SaveData()
{
std::ofstream file;
file.open("config.json");
Json::StreamWriterBuilder styledWriter;
styledWriter.newStreamWriter()->write(_root, &file);
file.close();
}