cellar/src/bottles/config/save_load.cpp
2017-03-24 18:45:50 -07:00

63 lines
1.4 KiB
C++

#include <fstream>
#include <sstream>
#include <string>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include "json.hpp"
#include "bottles.hpp"
using namespace std;
using namespace cellar::bottles;
namespace fs = boost::filesystem;
using json = nlohmann::json;
bool Bottle::load_config() {
string jsonpath = this->canonical_path + "/cellar.json";
if (fs::exists(jsonpath)) {
json config;
ifstream configstream(jsonpath);
stringstream sstr_config;
sstr_config << configstream.rdbuf();
config = json::parse(sstr_config.str());
this->config = config;
return true;
}
else {
return false;
}
}
bool Bottle::save_config() {
string jsonpath = this->canonical_path + "/cellar.json";
// backup the old config in case something bad happens
if (fs::exists(jsonpath)) {
fs::copy_file(jsonpath, jsonpath + ".old");
// now make room for the new one
fs::remove(jsonpath);
}
ofstream configstream(jsonpath);
configstream << this->config.dump(4);
return true;
}
string Bottle::get_config(string key) {
if (this->config.find(key) != this->config.end()) {
return this->config[key];
} else {
return "";
}
}
bool Bottle::set_config(string key, string value) {
this->config[key] = value;
return true;
}