#include "config.h" #include #include #include #include // realpath #include // dirname #include using json = nlohmann::json; #include template static void readVector(std::vector& v, const json& j) { v.clear(); v.reserve(j.size()); std::copy(j.begin(), j.end(), std::insert_iterator>(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("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 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"; } }