This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
YASMIN/ipin/Config.h
kazu 3a1cd1bccc fixed fallback step-logger
moved vap-grouping to settings-file
sanity-checks
2016-10-02 18:28:17 +02:00

108 lines
2.0 KiB
C++

#ifndef EVALCONFIG_H
#define EVALCONFIG_H
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "IPINHelper.h"
#include <Indoor/Exception.h>
class EvalConfig {
private:
std::unordered_map<std::string, std::string> keyVal;
public:
EvalConfig() {
;
}
/** get the value for the given key */
const std::string& get(const std::string& key) {
static std::string empty = "";
auto it = keyVal.find(key);
return (it == keyVal.end()) ? (empty) : (it->second);
}
public:
/** load the given configuration file */
void load(const std::string& file) {
std::ifstream inp(file);
if (!inp.good()) {throw Exception("error while opening config file " + file);}
std::string line;
std::string tmp;
while (!inp.eof()) {
// read the next line (might be empty)
std::getline(inp, tmp);
if (tmp.empty()) {continue;}
// in case of \r\n linebreaks, remove the remaining \r (might hereafter be empty)
if (tmp.back() == '\r') {tmp.erase(tmp.end()-1);}
if (tmp.empty()) {continue;}
// multiline?
bool multiline = false;
if (tmp.back() == '\\') {multiline = true; tmp.erase(tmp.end()-1);}
// attach
line += tmp;
// complete line? -> parse it
if (!multiline) {parse(line); line = "";}
}
// trailing data
parse(line);
inp.close();
}
private:
/** parse one line */
void parse(const std::string& line) {
// check
if (line.empty()) {return;}
if (line.front() == '[') {return;}
// split key and value
const size_t pos = line.find(" = ");
if (pos == std::string::npos) {throw Exception("something wrong");}
const std::string key = line.substr(0, pos);
const std::string val = line.substr(pos+3);
// add
keyVal[key] = val;
}
/** get the eval config.ini filename */
std::string getConfigFile() const {
const std::string folder = IPINHelper::getDataFolder();
const std::string file = folder + "it.cnr.isti.steplogger.config.ini";
return file;
}
};
#endif // EVALCONFIG_H