#include "config.h" #include #include #include #include // realpath #include // dirname #include using json = nlohmann::json; #include #include #include "config_json.cpp" // json parsing in diffrent file const uint8_t Config::ButtonConfig::DEFAULTWIDTH = 6; Config::Config(const std::string binaryArgument) { #if __unix__ 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"; } #else (void) binaryArgument; binaryPath = "./"; #endif } Config::~Config() { } bool Config::SampleConfig::isValid() const { return !file.empty(); } bool Config::ButtonConfig::isValid() const { bool validfound = std::any_of(samples.begin(), samples.end(), [](const SampleConfig& sc){ return sc.isValid(); }); return !samples.empty() && !name.empty() && validfound; } 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"; // just save the current config to create a file save(); } } void Config::save() { std::ofstream stream(binaryPath + file); if(stream) { json j = rootConfig; stream << j.dump(1, '\t'); } else { Log::error << "could not write configfile"; } }