#include "config.h" #include #include #include #include using json = nlohmann::json; 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() { } Config::~Config() { } void from_json(const json& j, Config::RootConfig::AudioConfig& ac) { readVector(ac.devices, j.value("devices", json::array())); } void from_json(const json& j, Config::RootConfig::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); } 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()) { readVector(rc.buttons, barr); } rc.audioPath = j.value("audioPath", ""); } bool Config::RootConfig::ButtonConfig::isValid() const { return !file.empty(); } void Config::load() { std::ifstream stream(file); json json; if(stream) { try { stream >> json; } catch(nlohmann::detail::parse_error& pe) { std::cout << "json error: " << pe.what() << std::endl; return; } } rootConfig = json; }