129 lines
3.5 KiB
C++
129 lines
3.5 KiB
C++
|
|
#pragma once
|
|
|
|
#include <libconfig.h++>
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <iostream>
|
|
#include <unordered_map>
|
|
|
|
struct LoggerConfig {
|
|
std::string id;
|
|
std::vector<std::string> flagsEnabled;
|
|
bool showTime = false;
|
|
bool showSourceTrace = false;
|
|
bool coutEnabled = false;
|
|
bool fileEnabled = false;
|
|
std::string filePath;
|
|
|
|
bool parseConfig(const libconfig::Setting& setting) {
|
|
|
|
if (!setting.lookupValue("Id", id)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const libconfig::Setting& flags = setting.lookup("FlagsEnabled");
|
|
for (int index = 0; index < flags.getLength(); ++index) {
|
|
flagsEnabled.push_back(static_cast<const char*>(flags[index]));
|
|
}
|
|
} catch (const libconfig::SettingException&) {
|
|
flagsEnabled.clear();
|
|
}
|
|
|
|
setting.lookupValue("ShowTime", showTime);
|
|
setting.lookupValue("ShowSourceTrace", showSourceTrace);
|
|
setting.lookupValue("CoutEnabled", coutEnabled);
|
|
setting.lookupValue("FileEnabled", fileEnabled);
|
|
setting.lookupValue("FilePath", filePath);
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
struct AudioConfig {
|
|
uint32_t sampleRate;
|
|
uint32_t channels;
|
|
uint32_t stereoMode;
|
|
uint32_t bufferSize;
|
|
float pitchStandard;
|
|
int32_t midiHome;
|
|
int32_t notesPerOctave;
|
|
};
|
|
|
|
struct KeymapConfig {
|
|
std::string id;
|
|
std::unordered_map<int32_t, uint8_t> keymap;
|
|
|
|
bool parseConfig(const libconfig::Setting& setting) {
|
|
|
|
if (!setting.lookupValue("Id", id)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const libconfig::Setting& keymapGroup = setting.lookup("Keymap");
|
|
const libconfig::Setting& notesGroup = setting.lookup("Notes");
|
|
const libconfig::Setting& keysGroup = setting.lookup("Keys");
|
|
|
|
for(size_t i = 0; i < keymapGroup.getLength(); i++) {
|
|
libconfig::Setting& item = keymapGroup[i];
|
|
std::string key = item.getName();
|
|
std::string note = "";
|
|
if(item.getType() == libconfig::Setting::TypeString) note = std::string(item.c_str());
|
|
|
|
int8_t noteId = static_cast<int>(notesGroup[note]) % INT8_MAX;
|
|
int32_t keyId = static_cast<int>(keysGroup[key]) % INT32_MAX;
|
|
|
|
keymap.emplace(keyId, noteId);
|
|
}
|
|
|
|
} catch (const libconfig::SettingException&) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
class ConfigService {
|
|
|
|
public:
|
|
|
|
ConfigService(const std::string& filePath);
|
|
~ConfigService() = default;
|
|
|
|
bool loadFromFile(const std::string& filePath);
|
|
|
|
template<typename T>
|
|
bool getConfig(const std::string& type, const std::string& id, T* config) const {
|
|
try {
|
|
const libconfig::Setting& configs = config_.lookup(type);
|
|
for (int index = 0; index < configs.getLength(); ++index) {
|
|
const libconfig::Setting& configSetting = configs[index];
|
|
|
|
std::string configId;
|
|
if (!configSetting.lookupValue("Id", configId) || configId != id) {
|
|
continue;
|
|
}
|
|
|
|
return config->parseConfig(configSetting);
|
|
}
|
|
} catch (const libconfig::SettingException& ex) {
|
|
std::cout << "libconfig setting exception: " << ex.what() << std::endl;
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
const std::string& lastError() const;
|
|
|
|
private:
|
|
|
|
libconfig::Config config_;
|
|
std::string lastError_;
|
|
};
|