forked from MrBesen/soundboard
61 lines
1.3 KiB
C++
61 lines
1.3 KiB
C++
#include "config.h"
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
using json = nlohmann::json;
|
|
|
|
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() {
|
|
}
|
|
|
|
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<Config::RootConfig::AudioConfig>("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;
|
|
}
|