mirror of
https://github.com/kichikuou/system3-sdl2.git
synced 2026-09-29 02:18:09 +03:00
Improve system3.ini parser
- keys are case-insensitive - spaces are allowed in values
This commit is contained in:
+42
-15
@@ -1,6 +1,8 @@
|
||||
#include "config.h"
|
||||
|
||||
#include "common.h"
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
@@ -32,6 +34,25 @@ bool is_empty_line(const char *s)
|
||||
return true;
|
||||
}
|
||||
|
||||
char *trim(char *s)
|
||||
{
|
||||
while (isspace(*s))
|
||||
s++;
|
||||
char *p = s + strlen(s) - 1;
|
||||
while (p > s && isspace(*p))
|
||||
*p-- = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
std::pair<char*, char*> parse_keyval(char *line)
|
||||
{
|
||||
char *eq = strchr(line, '=');
|
||||
if (!eq)
|
||||
return {nullptr, nullptr};
|
||||
*eq = '\0';
|
||||
return { trim(line), trim(eq + 1) };
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Config::Config(int argc, char *argv[])
|
||||
@@ -89,22 +110,28 @@ void Config::load_ini()
|
||||
else
|
||||
WARNING(INIFILENAME ":%d Unknown section \"%s\"", lineno, val);
|
||||
} else if (current_section == CONFIG) {
|
||||
if (sscanf(line, "noantialias = %s", val))
|
||||
no_antialias = to_bool(val, lineno);
|
||||
else if (sscanf(line, "savedir = %s", val))
|
||||
save_dir = val;
|
||||
else if (sscanf(line, "fontfile = %s", val))
|
||||
font_file = val;
|
||||
else if (sscanf(line, "playlist = %s", val))
|
||||
playlist = val;
|
||||
else if (sscanf(line, "fm = %s", val))
|
||||
use_fm = to_bool(val, lineno);
|
||||
else if (sscanf(line, "game = %s", val))
|
||||
game_id = val;
|
||||
else if (sscanf(line, "encoding = %s", val))
|
||||
encoding = val;
|
||||
else if (!is_empty_line(line))
|
||||
char *key, *val;
|
||||
std::tie(key, val) = parse_keyval(line);
|
||||
if (!key && !is_empty_line(line)) {
|
||||
WARNING(INIFILENAME ":%d parse error", lineno);
|
||||
continue;
|
||||
}
|
||||
if (!strcasecmp(key, "noantialias"))
|
||||
no_antialias = to_bool(val, lineno);
|
||||
else if (!strcasecmp(key, "savedir"))
|
||||
save_dir = val;
|
||||
else if (!strcasecmp(key, "fontfile"))
|
||||
font_file = val;
|
||||
else if (!strcasecmp(key, "playlist"))
|
||||
playlist = val;
|
||||
else if (!strcasecmp(key, "fm"))
|
||||
use_fm = to_bool(val, lineno);
|
||||
else if (!strcasecmp(key, "game"))
|
||||
game_id = val;
|
||||
else if (!strcasecmp(key, "encoding"))
|
||||
encoding = val;
|
||||
else
|
||||
WARNING(INIFILENAME ":%d unknown key '%s'", lineno, key);
|
||||
} else if (!is_empty_line(line)) {
|
||||
WARNING(INIFILENAME ":%d parse error", lineno);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user