A private doorbell project to serve an Android app with webcam pictures and microphone recordings when someone presses the button. Information is being spread via MQTT. https://www.taibsu.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
heimdall/config.cpp

91 lines
2.7 KiB

#include "config.h"
namespace tai {
config::config(const std::vector<std::string>& options)
{
/*
configuration readout
*/
_config = config4cpp::Configuration::create();
std::string scope {};
std::string filename { "heimdall.cfg" };
try {
// parse our file
_config->parse(filename.c_str());
LOG_INF("[CONF] Trying to parse " + std::to_string(options.size()) + " options...");
// retain variable values
for (auto const& option : options) {
// config4cpp doesn't support c++17
if (config4cpp::Configuration::Type::CFG_STRING == _config->type(scope.c_str(), option.c_str())) {
_options.insert({ option, _config->lookupString(scope.c_str(), option.c_str()) });
LOG_INF("\t- Option " + option + " parsed.");
}
else {
LOG_WRN("\t- Option " + option + " NOT parsed since it is not a string. Parse it using the particular function.");
}
}
LOG_INF("[CONF] Parsed " + std::to_string(_options.size()) + " config options.");
LOG_INF("[CONF] Configuration parsing complete.");
if (_options.size() == options.size()) {
LOG_INF("[CONF] All options were successfully parsed.");
}
else {
// We assume a total maximum of 256 entries in our config file.
// If you want to use more, use int or bigger instead.
uint8_t amount { static_cast<uint8_t>(options.size() - _options.size()) };
LOG_INF("[CONF] Didn't parse " + std::to_string(amount) + " options.");
}
}
catch (config4cpp::ConfigurationException& ce) {
LOG_FTL("[CONF] Unable to read from config file or option: " + std::string(ce.c_str()));
LOG_FTL("[CONF] Exiting.");
// Just to make it easy for the dev to see what's wrong when debugging.
assert(false);
_config->destroy();
exit(-1);
}
}
auto config::options(const std::string& o) -> const std::string&
{
if (_options.find(o) != _options.end()) {
return _options.at(o);
}
return {};
}
auto config::optionsVec(const std::string& o) -> std::vector<std::string>
{
// @TODO rework when there's time. useless memory overload
config4cpp::StringVector vec;
try {
_config->lookupList("", o.c_str(), vec);
}
catch (config4cpp::ConfigurationException& ce) {
LOG_FTL("[CONF] Unable to read from config option vector: " + std::string(ce.c_str()));
LOG_FTL("[CONF] Exiting.");
assert(false);
_config->destroy();
exit(-1);
}
return std::vector<std::string>(vec.c_array(), vec.c_array() + vec.length());
}
} // namespace tai