current revision
This commit is contained in:
104
ipin/Config.h
Normal file
104
ipin/Config.h
Normal file
@@ -0,0 +1,104 @@
|
||||
#ifndef EVALCONFIG_H
|
||||
#define EVALCONFIG_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <unordered_map>
|
||||
#include "IPINHelper.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 "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 "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
|
||||
Reference in New Issue
Block a user