forked from MrBesen/soundboard
69 lines
1.5 KiB
C++
69 lines
1.5 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 <iomanip>
|
|
|
|
#include <Log.h>
|
|
|
|
#include "config_json.cpp" // json parsing in diffrent file
|
|
|
|
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() {
|
|
}
|
|
|
|
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";
|
|
}
|
|
}
|
|
|
|
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";
|
|
}
|
|
} |