From bce7f5e4c2ab3026bbd0d179d628e4db7f0772bc Mon Sep 17 00:00:00 2001 From: kichikuou Date: Wed, 8 Sep 2021 17:09:49 +0900 Subject: [PATCH] Improve system3.ini parser - keys are case-insensitive - spaces are allowed in values --- src/config.cpp | 57 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 1bd8e7a..88f3841 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,6 +1,8 @@ #include "config.h" #include "common.h" +#include +#include #include #include #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 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); }