//
|
// 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();
|
}
|