Skip to content

Commit fac940c

Browse files
committed
Improve config settings
1 parent 986f994 commit fac940c

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

platforms/shared/desktop/config.cpp

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void write_float(const char* group, const char* key, float value);
3535
static bool read_bool(const char* group, const char* key, bool default_value);
3636
static void write_bool(const char* group, const char* key, bool boolean);
3737
static std::string read_string(const char* group, const char* key);
38-
static void write_string(const char* group, const char* key, std::string value);
38+
static void write_string(const char* group, const char* key, const std::string& value);
3939
static config_Hotkey read_hotkey(const char* group, const char* key, config_Hotkey default_value);
4040
static void write_hotkey(const char* group, const char* key, config_Hotkey hotkey);
4141
static config_Hotkey make_hotkey(SDL_Scancode key, SDL_Keymod mod);
@@ -106,10 +106,20 @@ static void set_defaults(void)
106106

107107
void config_init(void)
108108
{
109+
const char* root_path = NULL;
110+
109111
if (check_portable())
110-
config_root_path = SDL_GetBasePath();
112+
root_path = SDL_GetBasePath();
111113
else
112-
config_root_path = SDL_GetPrefPath("Geardome", GLYNX_TITLE);
114+
root_path = SDL_GetPrefPath("Geardome", GLYNX_TITLE);
115+
116+
if (root_path == NULL)
117+
{
118+
Log("Unable to determine config path. Falling back to current directory.");
119+
root_path = SDL_strdup("./");
120+
}
121+
122+
config_root_path = root_path;
113123

114124
strncpy_fit(config_emu_file_path, config_root_path, sizeof(config_emu_file_path));
115125
strncat_fit(config_emu_file_path, "config.ini", sizeof(config_emu_file_path));
@@ -124,7 +134,8 @@ void config_init(void)
124134

125135
void config_destroy(void)
126136
{
127-
SafeDelete(config_ini_file)
137+
SafeDelete(config_ini_file);
138+
SDL_free((void*)config_root_path);
128139
}
129140

130141
void config_load_defaults(void)
@@ -490,6 +501,10 @@ void config_write(void)
490501
{
491502
Debug("Settings saved");
492503
}
504+
else
505+
{
506+
Error("Unable to save settings to %s", config_emu_file_path);
507+
}
493508
}
494509

495510
static bool check_portable(void)
@@ -498,9 +513,11 @@ static bool check_portable(void)
498513
char portable_file_path[260];
499514

500515
base_path = SDL_GetBasePath();
516+
if (base_path == NULL)
517+
return false;
501518

502-
strcpy(portable_file_path, base_path);
503-
strcat(portable_file_path, "portable.ini");
519+
snprintf(portable_file_path, sizeof(portable_file_path), "%sportable.ini", base_path);
520+
SDL_free((void*)base_path);
504521

505522
FILE* file = fopen_utf8(portable_file_path, "r");
506523

@@ -515,14 +532,16 @@ static bool check_portable(void)
515532

516533
static int read_int(const char* group, const char* key, int default_value)
517534
{
518-
int ret = 0;
535+
int ret = default_value;
519536

520537
std::string value = config_ini_data[group][key];
521538

522-
if(value.empty())
523-
ret = default_value;
524-
else
525-
ret = std::stoi(value);
539+
if (!value.empty())
540+
{
541+
std::istringstream iss(value);
542+
if (!(iss >> ret))
543+
ret = default_value;
544+
}
526545

527546
Debug("Load integer setting: [%s][%s]=%d", group, key, ret);
528547
return ret;
@@ -537,17 +556,15 @@ static void write_int(const char* group, const char* key, int integer)
537556

538557
static float read_float(const char* group, const char* key, float default_value)
539558
{
540-
float ret = 0.0f;
559+
float ret = default_value;
541560

542561
std::string value = config_ini_data[group][key];
543562

544-
if(value.empty())
545-
ret = default_value;
546-
else
563+
if (!value.empty())
547564
{
548-
std::istringstream iss(value);
549-
iss.imbue(std::locale::classic());
550-
if (!(iss >> ret))
565+
std::istringstream converter(value);
566+
converter.imbue(std::locale::classic());
567+
if (!(converter >> ret))
551568
ret = default_value;
552569
}
553570

@@ -561,20 +578,22 @@ static void write_float(const char* group, const char* key, float value)
561578
oss.imbue(std::locale::classic());
562579
oss << std::fixed << std::setprecision(2) << value;
563580
std::string value_str = oss.str();
564-
config_ini_data[group][key] = oss.str();
581+
config_ini_data[group][key] = value_str;
565582
Debug("Save float setting: [%s][%s]=%s", group, key, value_str.c_str());
566583
}
567584

568585
static bool read_bool(const char* group, const char* key, bool default_value)
569586
{
570-
bool ret;
587+
bool ret = default_value;
571588

572589
std::string value = config_ini_data[group][key];
573590

574-
if(value.empty())
575-
ret = default_value;
576-
else
577-
std::istringstream(value) >> std::boolalpha >> ret;
591+
if (!value.empty())
592+
{
593+
std::istringstream converter(value);
594+
if (!(converter >> std::boolalpha >> ret))
595+
ret = default_value;
596+
}
578597

579598
Debug("Load bool setting: [%s][%s]=%s", group, key, ret ? "true" : "false");
580599
return ret;
@@ -597,7 +616,7 @@ static std::string read_string(const char* group, const char* key)
597616
return ret;
598617
}
599618

600-
static void write_string(const char* group, const char* key, std::string value)
619+
static void write_string(const char* group, const char* key, const std::string& value)
601620
{
602621
config_ini_data[group][key] = value;
603622
Debug("Save string setting: [%s][%s]=%s", group, key, value.c_str());

0 commit comments

Comments
 (0)