mirror of
https://github.com/minetest/minetestmapper.git
synced 2024-11-13 22:20:29 +01:00
Rewrite config file parser
I noticed it didn't work correctly in some cases...
This commit is contained in:
parent
04b9dffb11
commit
ecc2b31f78
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
80
util.cpp
80
util.cpp
|
@ -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;
|
||||
char linebuf[512];
|
||||
while (is.good()) {
|
||||
is.getline(linebuf, sizeof(linebuf));
|
||||
|
||||
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;
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user