From 6c980f046a165c9b9c88682c2c3671ce302acf45 Mon Sep 17 00:00:00 2001 From: Toni Wilen Date: Sun, 8 Nov 2020 16:33:17 +0200 Subject: [PATCH] Only save ini if it has been modified. --- include/ini.h | 1 + ini.cpp | 27 +++++++++++++++++++++++++-- od-win32/registry.cpp | 7 +++++-- od-win32/win32.cpp | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/include/ini.h b/include/ini.h index c4f32dbb..571ee24e 100644 --- a/include/ini.h +++ b/include/ini.h @@ -10,6 +10,7 @@ struct ini_data { struct ini_line **inidata; int inilines; + bool modified; }; struct ini_context diff --git a/ini.cpp b/ini.cpp index 864bd83c..14413c49 100644 --- a/ini.cpp +++ b/ini.cpp @@ -83,6 +83,8 @@ void ini_addnewcomment(struct ini_data *ini, const TCHAR *section, const TCHAR * void ini_addnewstring(struct ini_data *ini, const TCHAR *section, const TCHAR *key, const TCHAR *val) { struct ini_line *il = xcalloc(struct ini_line, 1); + if (!il) + return; il->section = my_strdup(section); if (!_tcsicmp(section, _T("WinUAE"))) il->section_order = 1; @@ -99,12 +101,19 @@ void ini_addnewstring(struct ini_data *ini, const TCHAR *section, const TCHAR *k if (cnt == ini->inilines) { ini->inilines += 10; ini->inidata = xrealloc(struct ini_line*, ini->inidata, ini->inilines); + if (!ini->inidata) { + xfree(il->key); + xfree(il->value); + xfree(il); + return; + } int cnt2 = cnt; while (cnt2 < ini->inilines) { ini->inidata[cnt2++] = NULL; } } ini->inidata[cnt] = il; + ini->modified = true; } void ini_addnewval(struct ini_data *ini, const TCHAR *section, const TCHAR *key, uae_u32 v) @@ -124,6 +133,8 @@ void ini_addnewval64(struct ini_data *ini, const TCHAR *section, const TCHAR *ke void ini_addnewdata(struct ini_data *ini, const TCHAR *section, const TCHAR *key, const uae_u8 *data, int len) { TCHAR *s = xcalloc(TCHAR, len * 3); + if (!s) + return; _tcscpy(s, _T("\\\n")); int w = 32; for (int i = 0; i < len; i += w) { @@ -238,7 +249,10 @@ struct ini_data *ini_load(const TCHAR *path, bool sort) if (sort) ini_sort(&ini); struct ini_data *iniout = xcalloc(ini_data, 1); - memcpy(iniout, &ini, sizeof(struct ini_data)); + if (iniout) { + memcpy(iniout, &ini, sizeof(struct ini_data)); + iniout->modified = false; + } return iniout; } @@ -284,6 +298,7 @@ bool ini_save(struct ini_data *ini, const TCHAR *path) } } fclose(f); + ini->modified = false; return true; } @@ -394,6 +409,8 @@ bool ini_getdata_multi(struct ini_data *ini, const TCHAR *section, const TCHAR * len = _tcslen(out2); outp = xcalloc(uae_u8, len); + if (!outp) + goto err; int j = 0; for (int i = 0; i < len; ) { @@ -531,13 +548,18 @@ bool ini_addstring(struct ini_data *ini, const TCHAR *section, const TCHAR *key, if (il->key == NULL) return true; if (!_tcsicmp(key, il->key)) { + TCHAR* v = val ? val : _T(""); + if (il->value && !_tcscmp(il->value, v)) + return true; xfree(il->value); - il->value = my_strdup(val ? val : _T("")); + il->value = my_strdup(v); + ini->modified = true; return true; } } } ini_addnewstring(ini, section, key, val); + ini->modified = true; return true; } @@ -551,6 +573,7 @@ bool ini_delete(struct ini_data *ini, const TCHAR *section, const TCHAR *key) xfree(il->value); xfree(il); ini->inidata[c] = NULL; + ini->modified = true; return true; } } diff --git a/od-win32/registry.cpp b/od-win32/registry.cpp index 82d90cf4..4f3d3f35 100644 --- a/od-win32/registry.cpp +++ b/od-win32/registry.cpp @@ -379,10 +379,13 @@ UAEREG *regcreatetree (UAEREG *root, const TCHAR *name) void regclosetree (UAEREG *key) { + if (inimode) { + if (inidata->modified) { + ini_save(inidata, inipath); + } + } if (!key) return; - if (inimode) - ini_save(inidata, inipath); if (key->fkey) RegCloseKey (key->fkey); xfree (key->inipath); diff --git a/od-win32/win32.cpp b/od-win32/win32.cpp index 82a7544c..ff769911 100644 --- a/od-win32/win32.cpp +++ b/od-win32/win32.cpp @@ -7113,6 +7113,7 @@ end: //_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); #endif close_console(); + regclosetree(NULL); if (hWinUAEKey) RegCloseKey(hWinUAEKey); _fcloseall(); -- 2.47.3