36 lines
934 B
C++
36 lines
934 B
C++
|
|
#include "ConfigService.hpp"
|
|
|
|
#include <exception>
|
|
|
|
ConfigService::ConfigService(const std::string& filePath) {
|
|
|
|
if(!loadFromFile(filePath)) {
|
|
std::cout << "Error loading file " << filePath << std::endl;
|
|
}
|
|
|
|
}
|
|
|
|
bool ConfigService::loadFromFile(const std::string& filePath) {
|
|
try {
|
|
config_.clear();
|
|
config_.readFile(filePath.c_str());
|
|
lastError_.clear();
|
|
return true;
|
|
} catch (const libconfig::FileIOException&) {
|
|
lastError_ = "Unable to read config file: " + filePath;
|
|
} catch (const libconfig::ParseException& error) {
|
|
lastError_ = std::string("Parse error in ") + error.getFile() + ":" +
|
|
std::to_string(error.getLine()) + " - " + error.getError();
|
|
} catch (const std::exception& error) {
|
|
lastError_ = error.what();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
const std::string& ConfigService::lastError() const {
|
|
return lastError_;
|
|
}
|
|
|