add config integration for keymap and logger
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
struct LoggerConfig {
|
||||
std::string id;
|
||||
@@ -15,6 +17,30 @@ struct LoggerConfig {
|
||||
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 {
|
||||
@@ -27,6 +53,40 @@ struct AudioConfig {
|
||||
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]) % INT8_MAX;
|
||||
keymap.emplace(keyId, noteId);
|
||||
}
|
||||
|
||||
} catch (const libconfig::SettingException&) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class ConfigService {
|
||||
|
||||
public:
|
||||
@@ -36,11 +96,31 @@ public:
|
||||
|
||||
bool loadFromFile(const std::string& filePath);
|
||||
|
||||
bool getLoggerConfig(const std::string& id, LoggerConfig* loggerConfig) const;
|
||||
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:
|
||||
bool parseLoggerConfig(const libconfig::Setting& loggerSetting, LoggerConfig* loggerConfig) const;
|
||||
|
||||
libconfig::Config config_;
|
||||
std::string lastError_;
|
||||
|
||||
Reference in New Issue
Block a user