2018-03-25 14:32:11 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-07-08 14:54:27 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2020-03-27 19:32:30 +01:00
|
|
|
static inline std::string trim(const std::string &s)
|
2014-07-08 14:54:27 +02:00
|
|
|
{
|
2020-03-27 19:32:30 +01:00
|
|
|
auto isspace = [] (char c) -> bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; };
|
2014-07-08 14:54:27 +02:00
|
|
|
|
2020-03-27 19:32:30 +01:00
|
|
|
size_t front = 0;
|
|
|
|
while(isspace(s[front]))
|
|
|
|
++front;
|
|
|
|
size_t back = s.size() - 1;
|
|
|
|
while(back > front && isspace(s[back]))
|
|
|
|
--back;
|
2014-07-08 14:54:27 +02:00
|
|
|
|
2020-03-27 19:32:30 +01:00
|
|
|
return s.substr(front, back - front + 1);
|
2014-07-08 14:54:27 +02:00
|
|
|
}
|
|
|
|
|
2018-03-25 14:32:11 +02:00
|
|
|
std::string read_setting(const std::string &name, std::istream &is)
|
2014-07-08 14:54:27 +02:00
|
|
|
{
|
2020-03-27 19:32:30 +01:00
|
|
|
char linebuf[512];
|
|
|
|
while (is.good()) {
|
|
|
|
is.getline(linebuf, sizeof(linebuf));
|
|
|
|
|
|
|
|
for(char *p = linebuf; *p; p++) {
|
|
|
|
if(*p != '#')
|
|
|
|
continue;
|
|
|
|
*p = '\0'; // Cut off at the first #
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::string line(linebuf);
|
|
|
|
|
|
|
|
auto pos = line.find('=');
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
continue;
|
|
|
|
auto key = trim(line.substr(0, pos));
|
|
|
|
if (key != name)
|
|
|
|
continue;
|
|
|
|
return trim(line.substr(pos+1));
|
|
|
|
}
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "Setting '" << name << "' not found";
|
|
|
|
throw std::runtime_error(oss.str());
|
2014-07-08 14:54:27 +02:00
|
|
|
}
|