提交 037f2843 authored 作者: blu's avatar blu

init

上级 5e1278e1
...@@ -13,6 +13,7 @@ update: 2019/08/23 ...@@ -13,6 +13,7 @@ update: 2019/08/23
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <cstdlib> #include <cstdlib>
#include <iomanip>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
using namespace std; using namespace std;
...@@ -206,47 +207,37 @@ int saveLocalConfigration(json &config, string fileName) ...@@ -206,47 +207,37 @@ int saveLocalConfigration(json &config, string fileName)
spdlog::error("saveLocalConfigration failed to mv file: {}", mv); spdlog::error("saveLocalConfigration failed to mv file: {}", mv);
return -1; return -1;
} }
// store new
ofstream file; // write prettified JSON file
file.open(fileName, ios::out|ios::trunc); try{
if(!file) { std::ofstream o(fileName);
spdlog::error("saveLocalConfigration failed to open file: {}", fileName); o << std::setw(4) << config << std::endl;
}catch(exception &e) {
spdlog::error("saveLocalConfigration failed to write configuration to file {}: {}\n{}", fileName, e.what(), config.dump());
return -2; return -2;
} }
file << config.dump();
file.close();
return ret; return ret;
} }
int loadLocalConfigration(json &config, string fileName) int loadLocalConfigration(json &config, string fileName)
{ {
int ret = 0; int ret = 0;
string body;
ifstream file;
file.open(fileName, ios::in);
if(!file) {
spdlog::error("loadLocalConfigration failed to open file: {}", fileName);
return -2;
}
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
try { try {
config = json::parse(str); std::ifstream i(fileName);
i >> config;
} }
catch(exception &e) { catch(exception &e) {
spdlog::error("loadLocalConfigration failed to parse config {}: {}", fileName, str); spdlog::error("loadLocalConfigration failed to parse config {}: {}", fileName, e.what());
return -3; return -2;
} }
return ret; return ret;
} }
// INFO: deprecated since configuration is stored in json // INFO: deprecated since configuration is stored in json
// modules: id integer, pid integer, iid integer, cls text, sn text, config text, version text, online integer, enabled integer, updatetime datetime, lastboot datetime, active integer // modules: id integer, pid integer, iid integer, cls text, sn text, config text, version text, online integer, enabled integer, updatetime datetime, lastboot datetime, active integer
// eg: 2, 0, NULL, evmgr, ILSEVMGR1, "xxxx", 1, 1, ts // eg: 2, 0, NULL, evmgr, ILSEVMGR1, "xxxx", 1, 1, ts
......
...@@ -17,6 +17,7 @@ extern "C" { ...@@ -17,6 +17,7 @@ extern "C" {
#define EV_DB_FILENAME_GENERAL "general.db" #define EV_DB_FILENAME_GENERAL "general.db"
#define EV_DB_FILENAME_LOG "log.db" #define EV_DB_FILENAME_LOG "log.db"
#define EV_DB_FILENAME_CONFIG "config.json" #define EV_DB_FILENAME_CONFIG "config.json"
#define EV_DB_FILENAME_SLICES "slices.json"
using namespace std; using namespace std;
using json = nlohmann::json; using json = nlohmann::json;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论