Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rewrite config file parser
I noticed it didn't work correctly in some cases...
  • Loading branch information
sfan5 committed Mar 27, 2020
1 parent 04b9dff commit ecc2b31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 51 deletions.
2 changes: 1 addition & 1 deletion include/util.h
Expand Up @@ -10,7 +10,7 @@ inline std::string read_setting_default(const std::string &name, std::istream &i
{
try {
return read_setting(name, is);
} catch(std::runtime_error &e) {
} catch(const std::runtime_error &e) {
return def;
}
}
Expand Down
82 changes: 32 additions & 50 deletions util.cpp
Expand Up @@ -3,61 +3,43 @@

#include "util.h"

inline std::string trim(const std::string &s)
static inline std::string trim(const std::string &s)
{
size_t front = 0;
while(s[front] == ' ' ||
s[front] == '\t' ||
s[front] == '\r' ||
s[front] == '\n'
)
++front;
auto isspace = [] (char c) -> bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; };

size_t back = s.size();
while(back > front &&
(s[back-1] == ' ' ||
s[back-1] == '\t' ||
s[back-1] == '\r' ||
s[back-1] == '\n'
)
)
--back;
size_t front = 0;
while(isspace(s[front]))
++front;
size_t back = s.size() - 1;
while(back > front && isspace(s[back]))
--back;

return s.substr(front, back - front);
return s.substr(front, back - front + 1);
}

#define EOFCHECK() do { \
if (is.eof()) { \
std::ostringstream oss; \
oss << "Setting '" << name << "' not found."; \
throw std::runtime_error(oss.str()); \
} \
} while(0)

std::string read_setting(const std::string &name, std::istream &is)
{
char c;
char s[256];
std::string nm, value;

next:
while((c = is.get()) == ' ' || c == '\t' || c == '\r' || c == '\n')
;
EOFCHECK();
if(c == '#') // Ignore comments
is.ignore(0xffff, '\n');
EOFCHECK();
s[0] = c; // The current char belongs to the name too
is.get(&s[1], 255, '=');
is.ignore(1); // Jump over the =
EOFCHECK();
nm = trim(std::string(s));
is.get(s, 256, '\n');
value = trim(std::string(s));
if(name == nm)
return value;
else
goto next;
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());
}

#undef EOFCHECK

0 comments on commit ecc2b31

Please sign in to comment.