https://github.com/libsdl-org/Maelstrom/commit/8b66208d36d024ca5c6b363f2965169335443627
From 8b66208d36d024ca5c6b363f2965169335443627 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 7 Nov 2011 20:49:32 -0500
Subject: [PATCH] Only hit the disk to write preferences if something has
changed.
---
utils/prefs.cpp | 16 ++++++++++++++--
utils/prefs.h | 3 ++-
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/utils/prefs.cpp b/utils/prefs.cpp
index bf2c5e81..984342a3 100644
--- a/utils/prefs.cpp
+++ b/utils/prefs.cpp
@@ -34,6 +34,7 @@ Prefs::Prefs(const char *file)
{
m_file = SDL_strdup(file);
m_values = hash_create(NULL, hash_hash_string, hash_keymatch_string, hash_nuke_strings);
+ m_dirty = false;
}
Prefs::~Prefs()
@@ -84,7 +85,7 @@ Prefs::Load()
*next++ = '\0';
}
- SetString(key, value);
+ SetString(key, value, false);
key = next;
while (*key && (*key == '\r' || *key == '\n')) {
@@ -110,6 +111,11 @@ Prefs::Save()
const char *key, *value;
void *iter;
+ // Only hit the disk if something actually changed.
+ if (!m_dirty) {
+ return true;
+ }
+
fp = PHYSFS_openWrite(m_file);
if (!fp) {
fprintf(stderr, "Warning: Couldn't open %s: %s\n",
@@ -131,11 +137,13 @@ Prefs::Save()
}
PHYSFS_close(fp);
+ m_dirty = false;
+
return true;
}
void
-Prefs::SetString(const char *key, const char *value)
+Prefs::SetString(const char *key, const char *value, bool dirty)
{
const char *lastValue;
@@ -149,6 +157,10 @@ Prefs::SetString(const char *key, const char *value)
hash_remove(m_values, key);
}
hash_insert(m_values, SDL_strdup(key), SDL_strdup(value));
+
+ if (dirty) {
+ m_dirty = true;
+ }
}
void
diff --git a/utils/prefs.h b/utils/prefs.h
index 8baf1d33..368922d5 100644
--- a/utils/prefs.h
+++ b/utils/prefs.h
@@ -30,7 +30,7 @@ class Prefs
bool Load();
bool Save();
- void SetString(const char *key, const char *value);
+ void SetString(const char *key, const char *value, bool dirty = true);
void SetNumber(const char *key, int value);
void SetBool(const char *key, bool value);
void Set(const char *key, const char *value) {
@@ -59,6 +59,7 @@ class Prefs
protected:
char *m_file;
HashTable *m_values;
+ bool m_dirty;
};
template <typename T>