Skip to content

Commit 065df10

Browse files
Get prefs path+filename before opening file
1 parent e702db4 commit 065df10

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

Diff for: src/MacSrc/Prefs.c

+21-12
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,24 @@ void SetDefaultPrefs(void) {
114114
SetShockGlobals();
115115
}
116116

117-
static FILE *open_prefs(const char *mode) {
118-
FILE *f = fopen(PREFS_FILENAME, mode);
119-
if (f) return f;
120-
121-
char fullname[512];
122-
char *path = SDL_GetPrefPath("Interrupt", "SystemShock");
123-
snprintf(fullname, sizeof(fullname), "%s%s", path, PREFS_FILENAME);
124-
free(path);
125-
return fopen(fullname, mode);
117+
static char *GetPrefsPathFilename(void)
118+
{
119+
static char filename[512];
120+
121+
FILE *f = fopen(PREFS_FILENAME, "r");
122+
if (f != NULL)
123+
{
124+
fclose(f);
125+
strcpy(filename, PREFS_FILENAME);
126+
}
127+
else
128+
{
129+
char *p = SDL_GetPrefPath("Interrupt", "SystemShock");
130+
snprintf(filename, sizeof(filename), "%s%s", p, PREFS_FILENAME);
131+
free(p);
132+
}
133+
134+
return filename;
126135
}
127136

128137
static char *trim(char *s) {
@@ -142,7 +151,7 @@ static bool is_true(const char *s) {
142151
// Locate the preferences file and load them to set our global pref settings.
143152
//--------------------------------------------------------------------
144153
OSErr LoadPrefs(void) {
145-
FILE *f = open_prefs("r");
154+
FILE *f = fopen(GetPrefsPathFilename(), "r");
146155
if (!f) {
147156
// file can't be open, write default preferences
148157
return SavePrefs();
@@ -222,7 +231,7 @@ OSErr LoadPrefs(void) {
222231
OSErr SavePrefs(void) {
223232
INFO("Saving preferences");
224233

225-
FILE *f = open_prefs("w");
234+
FILE *f = fopen(GetPrefsPathFilename(), "w");
226235
if (!f) {
227236
printf("ERROR: Failed to open preferences file\n");
228237
return -1;
@@ -561,7 +570,7 @@ int FireKeys[MAX_FIRE_KEYS+1]; //see input.c
561570

562571

563572

564-
char *GetKeybindsPathFilename(void)
573+
static char *GetKeybindsPathFilename(void)
565574
{
566575
static char filename[512];
567576

0 commit comments

Comments
 (0)