Files
totk-pc-recomp/runtime/cfg_probe.c
T

58 lines
2.4 KiB
C

// --probe-cfg: totk.ini parsing, the environment override and a missing file (cfg.c). Needs no game files.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#include "host.h"
#include "platform/platform.h"
static int fails;
#define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL cfg: %s\n", what); } } while (0)
#define EQ(a, b) ((a) && !strcmp((a), (b)))
int probe_cfg(void) {
char path[1024];
if (!plat_exe_path(path, sizeof path)) return 1;
char *cut = strrchr(path, '\\');
if (!cut) return 1;
strcpy(cut + 1, "cfg-probe.ini");
static const char ini[] =
"\xEF\xBB\xBF# comment\r\n"
"; another comment\r\n"
"[frame rate]\r\n"
" fps60 = 0 \r\n"
"Realtime=0\r\n"
"[Display]\r\n"
"GPU_name = \"My GPU 1\"\r\n"
"TOTK_Locale = de-DE\r\n"
"lang =\r\n"
"trace = a\r\n"
"trace = b\r\n"
"no_such_setting = 1\r\n"
"no equals sign\r\n"
"last = no newline at the end";
_putenv_s("TOTK_LOCALE", ""); // an empty variable is how _putenv_s spells "remove" on Windows
_putenv_s("TOTK_FPS60", "");
CHECK(plat_file_write(path, ini, sizeof ini - 1), "write the probe ini");
CHECK(cfg_load(path) == 1, "load the probe ini");
CHECK(EQ(cfg_get("TOTK_FPS60"), "0"), "value read, whitespace trimmed, lower-case key");
CHECK(EQ(cfg_get("TOTK_REALTIME"), "0"), "key without spaces, mixed case");
CHECK(EQ(cfg_get("TOTK_GPU_NAME"), "My GPU 1"), "quoted value, section ignored");
CHECK(EQ(cfg_get("TOTK_LOCALE"), "de-DE"), "TOTK_ prefix accepted in the file");
CHECK(cfg_get("TOTK_LANG") == NULL, "empty value is unset");
CHECK(EQ(cfg_get("TOTK_TRACE"), "b"), "later line wins");
CHECK(cfg_get("TOTK_CLOTH_REALTIME") == NULL, "absent key is NULL");
CHECK(cfg_get("TOTK_comment") == NULL, "comment is not a key");
CHECK(EQ(cfg_get("TOTK_NO_SUCH_SETTING"), "1"), "unknown key is still readable");
_putenv_s("TOTK_FPS60", "1");
CHECK(EQ(cfg_get("TOTK_FPS60"), "1"), "environment overrides the file");
_putenv_s("TOTK_FPS60", "");
CHECK(EQ(cfg_get("TOTK_FPS60"), "0"), "file value returns when the variable is removed");
plat_file_delete(path);
CHECK(cfg_load(path) == 0, "missing file is not an error");
CHECK(cfg_get("TOTK_FPS60") == NULL, "missing file leaves no keys");
printf("probe-cfg: %s\n", fails ? "FAIL" : "ok");
return fails ? 1 : 0;
}