86 lines
2.0 KiB
C++
86 lines
2.0 KiB
C++
#include "config.h"
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
|
|
#include <stdlib.h> // realpath
|
|
#include <libgen.h> // dirname
|
|
|
|
#include <nlohmann/json.hpp>
|
|
using json = nlohmann::json;
|
|
|
|
#include <Log.h>
|
|
|
|
template<typename T>
|
|
static void readVector(std::vector<T>& v, const json& j) {
|
|
v.clear();
|
|
v.reserve(j.size());
|
|
std::copy(j.begin(), j.end(), std::insert_iterator<std::vector<T>>(v, v.begin()));
|
|
}
|
|
|
|
Config::Config(const std::string binaryArgument) {
|
|
char* buff = realpath(binaryArgument.c_str(), NULL); // buff needs free !
|
|
//ssize_t read = readlink(binaryArgument.c_str(), buf, MAXBUF);
|
|
if(buff) {
|
|
// success
|
|
char* binPath = dirname(buff);
|
|
binaryPath = std::string(binPath) + '/';
|
|
free(buff);
|
|
} else {
|
|
// error
|
|
Log::error << "unable to read path of the binaryFile";
|
|
}
|
|
}
|
|
|
|
Config::~Config() {
|
|
}
|
|
|
|
void from_json(const json& j, Config::AudioConfig& ac) {
|
|
readVector(ac.devices, j.value("devices", json::array()));
|
|
}
|
|
|
|
void from_json(const json& j, Config::ButtonConfig& bc) {
|
|
bc.name = j.value("name", "");
|
|
bc.file = j.value("file", "");
|
|
bc.key = j.value("key", "");
|
|
bc.offset = j.value("offset", 0);
|
|
bc.length = j.value("length", 0);
|
|
bc.volume = j.value("volume", 1.f);
|
|
bc.width = j.value("width", 6);
|
|
}
|
|
|
|
void from_json(const json& j, Config::RootConfig& rc) {
|
|
rc.audio = j.value<Config::AudioConfig>("audio", {});
|
|
json barr = j.value("buttons", json::array());
|
|
if(barr.is_array() && !barr.empty()) {
|
|
rc.buttons.reserve(barr.size());
|
|
for(const json& line : barr) {
|
|
std::vector<Config::ButtonConfig> btns;
|
|
readVector(btns, line);
|
|
rc.buttons.push_back(btns);
|
|
}
|
|
}
|
|
rc.audioPath = j.value("audioPath", "");
|
|
}
|
|
|
|
bool Config::ButtonConfig::isValid() const {
|
|
return !file.empty();
|
|
}
|
|
|
|
void Config::load() {
|
|
std::ifstream stream(binaryPath + file);
|
|
json json;
|
|
if(stream) {
|
|
try {
|
|
stream >> json;
|
|
rootConfig = json;
|
|
} catch(nlohmann::detail::parse_error& pe) {
|
|
std::cout << "json error: " << pe.what() << std::endl;
|
|
return;
|
|
}
|
|
} else {
|
|
Log::error << "config File not found";
|
|
}
|
|
}
|