Quote a settings value on save when reading it back would change it.

cfg_load trims blanks around a value and takes one pair of quotes off it, so a mods_order list whose first mod's
folder name starts with a space, or a value that is itself quoted, came back different at the next start and the mod
lost its place and switch. Such values are now written in quotes; others are written as before. --probe-cfg checks it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-08 08:48:14 -05:00
parent 7f9ace35fb
commit b142b7a71a
2 changed files with 26 additions and 2 deletions

View File

@@ -547,6 +547,17 @@ static const char *section_of(const char *key) {
return NULL;
}
// A value as cfg_load reads it back: quoted when it starts or ends with a blank (cfg_load trims those) or is itself
// quoted (cfg_load takes one pair of quotes off). A mod's folder name in mods_order can start with a space.
static void put_value(Out *o, const char *v) {
size_t n = strlen(v);
int quote = n && (v[0] == ' ' || v[0] == '\t' || v[n - 1] == ' ' || v[n - 1] == '\t' ||
(n >= 2 && v[0] == '"' && v[n - 1] == '"'));
if (quote) puts_(o, "\"");
puts_(o, v);
if (quote) puts_(o, "\"");
}
// "key = value" + eol for entry e (key lower-case)
static void put_entry(Out *o, const CfgEntry *e, const char *eol) {
char k[64];
@@ -554,7 +565,7 @@ static void put_entry(Out *o, const CfgEntry *e, const char *eol) {
for (char *p = k; *p; p++) *p = (char)tolower((unsigned char)*p);
puts_(o, k);
puts_(o, " = ");
puts_(o, e->value);
put_value(o, e->value);
puts_(o, eol);
}
@@ -639,7 +650,7 @@ int cfg_save_to(const char *path) {
while (kn && (s[kn - 1] == ' ' || s[kn - 1] == '\t')) kn--;
put(&o, s, kn);
puts_(&o, " = ");
puts_(&o, entries[replace[i]].value);
put_value(&o, entries[replace[i]].value);
int had_eol = len && s[len - 1] == '\n';
if (had_eol) puts_(&o, eol);
} else {

View File

@@ -129,6 +129,19 @@ int probe_cfg(void) {
got = plat_file_read(path, &got_n);
CHECK(got && !strcmp((const char *)got, "[audio]\nvolume = 90\n"), "new file: the section and the key");
free(got);
// values cfg_load would trim or unquote: mod folder names in the order list start with a space or are quoted
cfg_set("NX2NAVI_MODS_ORDER", " Lead|Tail ");
cfg_set("NX2NAVI_MODS_DISABLED", "\"Q\"");
CHECK(cfg_save_to(path) == 1 && cfg_load(path) == 1 && EQ(cfg_get("NX2NAVI_MODS_ORDER"), " Lead|Tail ") &&
EQ(cfg_get("NX2NAVI_MODS_DISABLED"), "\"Q\""), "blanks at the ends and quotes survive a save");
cfg_set("NX2NAVI_MODS_ORDER", "A|B");
CHECK(cfg_save_to(path) == 1 && cfg_load(path) == 1 && EQ(cfg_get("NX2NAVI_MODS_ORDER"), "A|B"),
"a plain value replacing a quoted one");
got = plat_file_read(path, &got_n);
CHECK(got && strstr((const char *)got, "mods_order = A|B\n") &&
strstr((const char *)got, "mods_disabled = \"\"Q\"\"\n"),
"only the values that need it are quoted");
free(got);
// Defaults table: the menu's "default" marks and Reset
const CfgMeta *m = cfg_meta("FPS60");