mirror of
https://github.com/minetest/minetestmapper.git
synced 2024-11-21 23:13:53 +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 {
|
try {
|
||||||
return read_setting(name, is);
|
return read_setting(name, is);
|
||||||
} catch(std::runtime_error &e) {
|
} catch(const std::runtime_error &e) {
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
76
util.cpp
76
util.cpp
@ -3,61 +3,43 @@
|
|||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
inline std::string trim(const std::string &s)
|
static inline std::string trim(const std::string &s)
|
||||||
{
|
{
|
||||||
size_t front = 0;
|
auto isspace = [] (char c) -> bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; };
|
||||||
while(s[front] == ' ' ||
|
|
||||||
s[front] == '\t' ||
|
|
||||||
s[front] == '\r' ||
|
|
||||||
s[front] == '\n'
|
|
||||||
)
|
|
||||||
++front;
|
|
||||||
|
|
||||||
size_t back = s.size();
|
size_t front = 0;
|
||||||
while(back > front &&
|
while(isspace(s[front]))
|
||||||
(s[back-1] == ' ' ||
|
++front;
|
||||||
s[back-1] == '\t' ||
|
size_t back = s.size() - 1;
|
||||||
s[back-1] == '\r' ||
|
while(back > front && isspace(s[back]))
|
||||||
s[back-1] == '\n'
|
|
||||||
)
|
|
||||||
)
|
|
||||||
--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)
|
std::string read_setting(const std::string &name, std::istream &is)
|
||||||
{
|
{
|
||||||
char c;
|
char linebuf[512];
|
||||||
char s[256];
|
while (is.good()) {
|
||||||
std::string nm, value;
|
is.getline(linebuf, sizeof(linebuf));
|
||||||
|
|
||||||
next:
|
for(char *p = linebuf; *p; p++) {
|
||||||
while((c = is.get()) == ' ' || c == '\t' || c == '\r' || c == '\n')
|
if(*p != '#')
|
||||||
;
|
continue;
|
||||||
EOFCHECK();
|
*p = '\0'; // Cut off at the first #
|
||||||
if(c == '#') // Ignore comments
|
break;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
std::string line(linebuf);
|
||||||
|
|
||||||
#undef EOFCHECK
|
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());
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user