From: DeaDDooMER Date: Fri, 17 Apr 2020 08:20:20 +0000 (+0400) Subject: config: allow multiple cfg structures for config update X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=49c7dbd3920be7bb57a9ec39779d9695fbe8f0c5;p=flatwaifu.git config: allow multiple cfg structures for config update --- diff --git a/src/config.c b/src/config.c index 885a0e4..ed611c3 100644 --- a/src/config.c +++ b/src/config.c @@ -209,10 +209,12 @@ static int CFG_write_entry (FILE *f, const cfg_t *entry) { return entry->t == 0 ? 0 : 1; } -int CFG_update_config (const char *old, const char *new, const cfg_t *cfg, const char *msg) { +int CFG_update_config (const char *old, const char *new, int n, const cfg_t **cfg, const char *msg) { assert(old != NULL); assert(new != NULL); + assert(n >= 0); assert(cfg != NULL); + int i, j; char key[64]; char value[64]; FILE *nf = fopen(new, "wb"); @@ -224,15 +226,21 @@ int CFG_update_config (const char *old, const char *new, const cfg_t *cfg, const } if (CFG_open_iterator(old)) { while (CFG_scan_iterator(key, 64, value, 64)) { - if (CFG_find_entry(key, cfg) == NULL) { + i = 0; + while (i < n && CFG_find_entry(key, cfg[i]) == NULL) { + i++; + } + if (i >= n) { CFG_write_key_value(nf, key, value); } } CFG_close_iterator(); } - int i = 0; - while (CFG_write_entry(nf, &cfg[i])) { - i++; + for (j = 0; j < n; j++) { + i = 0; + while (CFG_write_entry(nf, &cfg[j][i])) { + i++; + } } fclose(nf); } diff --git a/src/config.h b/src/config.h index 29d17d7..c666722 100644 --- a/src/config.h +++ b/src/config.h @@ -33,6 +33,6 @@ int CFG_scan_iterator (char *key, int keylen, char *value, int valuelen); void CFG_close_iterator (void); int CFG_read_config (const char *name, const cfg_t *cfg); -int CFG_update_config (const char *old, const char *new, const cfg_t *cfg, const char *msg); +int CFG_update_config (const char *old, const char *new, int n, const cfg_t **cfg, const char *msg); #endif /* CONFIG_H_INCLUDED */ diff --git a/src/sdl/main.c b/src/sdl/main.c index 2df46b3..a440844 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -131,7 +131,8 @@ static void CFG_load (void) { } static void CFG_save (void) { - CFG_update_config("doom2d.cfg", "doom2d.cfg", cfg, "generated by doom2d, do not modify"); + const cfg_t *list[] = { &cfg, NULL }; + CFG_update_config("doom2d.cfg", "doom2d.cfg", 1, list, "generated by doom2d, do not modify"); //CFG_update_config("doom2d.cfg", "doom2d.tmp", cfg, "temporary file"); //CFG_update_config("doom2d.tmp", "doom2d.cfg", cfg, "generated by doom2d, do not modify"); //remove("doom2d.tmp"); diff --git a/src/sdl2/main.c b/src/sdl2/main.c index ae80fe9..37b4c26 100644 --- a/src/sdl2/main.c +++ b/src/sdl2/main.c @@ -110,7 +110,8 @@ static void CFG_load (void) { } static void CFG_save (void) { - CFG_update_config("doom2d.cfg", "doom2d.cfg", cfg, "generated by doom2d, do not modify"); + const cfg_t *list[] = { &cfg, NULL }; + CFG_update_config("doom2d.cfg", "doom2d.cfg", 1, list, "generated by doom2d, do not modify"); //CFG_update_config("doom2d.cfg", "doom2d.tmp", cfg, "temporary file"); //CFG_update_config("doom2d.tmp", "doom2d.cfg", cfg, "generated by doom2d, do not modify"); //remove("doom2d.tmp");