pans
2017-08-18 5cf652629fb40796cd2e0ab17c3617ed52365473
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
//
// Created by ke.chen on 2017/8/7.
//
 
#include "AppConfig.h"
 
AppConfig::AppConfig()
{
}
 
AppConfig::AppConfig(string path)
{
    loadConfig(path);
}
 
AppConfig::~AppConfig()
{
}
 
string AppConfig::getStringValue(string key)
{
    return string(root[key].asString());
}
 
int AppConfig::getIntValue(string key)
{
    return root[key].asInt();
}
 
void AppConfig::setStringValue(string key, string value)
{
    root[key] = value;
}
 
void AppConfig::setIntValue(string key, int value)
{
    root[key] = value;
}
 
void AppConfig::loadConfig(string path)
{
    fstream fs(path);
    if (!fs.is_open())
    {
        save(path);
        return;
    }
    Json::CharReaderBuilder builder;
    auto reader = builder.newCharReader();
    JSONCPP_STRING doc;
    std::getline(fs, doc, (char) EOF);
    reader->parse(doc.data(), doc.data() + doc.size(), &root, false);
}
 
void AppConfig::save(string path)
{
    ofstream ofs(path);
    ofs << root.toStyledString();
    ofs.close();
}
 
void AppConfig::clear()
{
    root.clear();
}