Skip to content

Commit 2dd2bb1

Browse files
committed
Improve config settings
1 parent 0116f62 commit 2dd2bb1

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);
@@ -158,10 +158,20 @@ static void set_defaults(void)
158158

159159
void config_init(void)
160160
{
161+
const char* root_path = NULL;
162+
161163
if (check_portable())
162-
config_root_path = SDL_GetBasePath();
164+
root_path = SDL_GetBasePath();
163165
else
164-
config_root_path = SDL_GetPrefPath("Geardome", GG_TITLE);
166+
root_path = SDL_GetPrefPath("Geardome", GG_TITLE);
167+
168+
if (root_path == NULL)
169+
{
170+
Log("Unable to determine config path. Falling back to current directory.");
171+
root_path = SDL_strdup("./");
172+
}
173+
174+
config_root_path = root_path;
165175

166176
strncpy_fit(config_temp_path, config_root_path, sizeof(config_temp_path));
167177
strncat_fit(config_temp_path, "tmp/", sizeof(config_temp_path));
@@ -180,7 +190,8 @@ void config_init(void)
180190

181191
void config_destroy(void)
182192
{
183-
SafeDelete(config_ini_file)
193+
SafeDelete(config_ini_file);
194+
SDL_free((void*)config_root_path);
184195
}
185196

186197
void config_load_defaults(void)
@@ -759,6 +770,10 @@ void config_write(void)
759770
{
760771
Debug("Settings saved");
761772
}
773+
else
774+
{
775+
Error("Unable to save settings to %s", config_emu_file_path);
776+
}
762777
}
763778

764779
static bool check_portable(void)
@@ -767,9 +782,11 @@ static bool check_portable(void)
767782
char portable_file_path[260];
768783

769784
base_path = SDL_GetBasePath();
785+
if (base_path == NULL)
786+
return false;
770787

771-
strcpy(portable_file_path, base_path);
772-
strcat(portable_file_path, "portable.ini");
788+
snprintf(portable_file_path, sizeof(portable_file_path), "%sportable.ini", base_path);
789+
SDL_free((void*)base_path);
773790

774791
FILE* file = fopen_utf8(portable_file_path, "r");
775792

@@ -784,14 +801,16 @@ static bool check_portable(void)
784801

785802
static int read_int(const char* group, const char* key, int default_value)
786803
{
787-
int ret = 0;
804+
int ret = default_value;
788805

789806
std::string value = config_ini_data[group][key];
790807

791-
if(value.empty())
792-
ret = default_value;
793-
else
794-
ret = std::stoi(value);
808+
if (!value.empty())
809+
{
810+
std::istringstream iss(value);
811+
if (!(iss >> ret))
812+
ret = default_value;
813+
}
795814

796815
Debug("Load integer setting: [%s][%s]=%d", group, key, ret);
797816
return ret;
@@ -806,17 +825,15 @@ static void write_int(const char* group, const char* key, int integer)
806825

807826
static float read_float(const char* group, const char* key, float default_value)
808827
{
809-
float ret = 0.0f;
828+
float ret = default_value;
810829

811830
std::string value = config_ini_data[group][key];
812831

813-
if(value.empty())
814-
ret = default_value;
815-
else
832+
if (!value.empty())
816833
{
817-
std::istringstream iss(value);
818-
iss.imbue(std::locale::classic());
819-
if (!(iss >> ret))
834+
std::istringstream converter(value);
835+
converter.imbue(std::locale::classic());
836+
if (!(converter >> ret))
820837
ret = default_value;
821838
}
822839

@@ -830,20 +847,22 @@ static void write_float(const char* group, const char* key, float value)
830847
oss.imbue(std::locale::classic());
831848
oss << std::fixed << std::setprecision(2) << value;
832849
std::string value_str = oss.str();
833-
config_ini_data[group][key] = oss.str();
850+
config_ini_data[group][key] = value_str;
834851
Debug("Save float setting: [%s][%s]=%s", group, key, value_str.c_str());
835852
}
836853

837854
static bool read_bool(const char* group, const char* key, bool default_value)
838855
{
839-
bool ret;
856+
bool ret = default_value;
840857

841858
std::string value = config_ini_data[group][key];
842859

843-
if(value.empty())
844-
ret = default_value;
845-
else
846-
std::istringstream(value) >> std::boolalpha >> ret;
860+
if (!value.empty())
861+
{
862+
std::istringstream converter(value);
863+
if (!(converter >> std::boolalpha >> ret))
864+
ret = default_value;
865+
}
847866

848867
Debug("Load bool setting: [%s][%s]=%s", group, key, ret ? "true" : "false");
849868
return ret;
@@ -866,7 +885,7 @@ static std::string read_string(const char* group, const char* key)
866885
return ret;
867886
}
868887

869-
static void write_string(const char* group, const char* key, std::string value)
888+
static void write_string(const char* group, const char* key, const std::string& value)
870889
{
871890
config_ini_data[group][key] = value;
872891
Debug("Save string setting: [%s][%s]=%s", group, key, value.c_str());

0 commit comments

Comments
 (0)