diff --git a/Makefile b/Makefile index dc3f806679..f55a87b233 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include config.mk TARGET = ssnes tools/ssnes-joyconfig OBJ = ssnes.o file.o driver.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o ups.o bps.o strl.o screenshot.o -JOYCONFIG_OBJ = tools/ssnes-joyconfig.o conf/config_file.o +JOYCONFIG_OBJ = tools/ssnes-joyconfig.o conf/config_file.o strl.o HEADERS = $(wildcard */*.h) $(wildcard *.h) LIBS = -lm diff --git a/Makefile.win32 b/Makefile.win32 index 2b2c2f91b8..d8fb1bb099 100644 --- a/Makefile.win32 +++ b/Makefile.win32 @@ -1,7 +1,7 @@ TARGET = ssnes.exe JTARGET = ssnes-joyconfig.exe OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o bps.o ups.o strl.o screenshot.o audio/hermite.o -JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o +JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o strl.o CC = gcc CXX = g++ diff --git a/Makefile.win64 b/Makefile.win64 index d43ac62606..e24a684c0c 100644 --- a/Makefile.win64 +++ b/Makefile.win64 @@ -1,7 +1,7 @@ TARGET = ssnes.exe JTARGET = ssnes-joyconfig.exe OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o bps.o ups.o strl.o screenshot.o audio/hermite.o -JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o +JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o strl.o CC = gcc CXX = g++ diff --git a/conf/config_file.c b/conf/config_file.c index 9dbdc773ce..d6bf82fa92 100644 --- a/conf/config_file.c +++ b/conf/config_file.c @@ -21,6 +21,7 @@ #include #include #include +#include "strl.h" struct entry_list { @@ -247,7 +248,7 @@ bool config_get_char(config_file_t *conf, const char *key, char *in) { if (strcmp(key, list->key) == 0) { - if (strlen(list->value) > 1) + if (list->value[0] && list->value[1]) return false; *in = *list->value; return true; @@ -273,6 +274,22 @@ bool config_get_string(config_file_t *conf, const char *key, char **str) return false; } +bool config_get_array(config_file_t *conf, const char *key, char *buf, size_t size) +{ + struct entry_list *list = conf->entries; + + while (list != NULL) + { + if (strcmp(key, list->key) == 0) + { + strlcpy(buf, list->value, size); + return true; + } + list = list->next; + } + return false; +} + bool config_get_bool(config_file_t *conf, const char *key, bool *in) { struct entry_list *list = conf->entries; diff --git a/conf/config_file.h b/conf/config_file.h index c413ab7185..757d1e3934 100644 --- a/conf/config_file.h +++ b/conf/config_file.h @@ -22,6 +22,7 @@ #include #include #include +#include typedef struct config_file config_file_t; @@ -48,6 +49,8 @@ bool config_get_hex(config_file_t *conf, const char *entry, unsigned *in); bool config_get_char(config_file_t *conf, const char *entry, char *in); // Extracts an allocated string in *in. This must be free()-d if this function succeeds. bool config_get_string(config_file_t *conf, const char *entry, char **in); +// Extracts a string to a preallocated buffer. Avoid memory allocation. +bool config_get_array(config_file_t *conf, const char *entry, char *in, size_t size); // Extracts a boolean from config. Valid boolean true are "true" and "1". Valid false are "false" and "0". Other values will be treated as an error. bool config_get_bool(config_file_t *conf, const char *entry, bool *in); diff --git a/settings.c b/settings.c index 5bb7cf4345..9e5544d944 100644 --- a/settings.c +++ b/settings.c @@ -261,10 +261,9 @@ static config_file_t *open_default_config_file(void) g_settings.var = tmp_double #define CONFIG_GET_STRING(var, key) do { \ - if (config_get_string(conf, key, &tmp_str)) \ + if (config_get_array(conf, key, tmp_str, sizeof(tmp_str))) \ { \ strlcpy(g_settings.var, tmp_str, sizeof(g_settings.var)); \ - free(tmp_str); \ } \ } while(0) @@ -294,7 +293,7 @@ static void parse_config_file(void) int tmp_int; double tmp_double; bool tmp_bool; - char *tmp_str; + char tmp_str[MAXPATHLEN]; CONFIG_GET_DOUBLE(video.xscale, "video_xscale"); CONFIG_GET_DOUBLE(video.yscale, "video_yscale"); @@ -339,7 +338,7 @@ static void parse_config_file(void) #endif #if defined(HAVE_CG) || defined(HAVE_XML) - if (config_get_string(conf, "video_shader_type", &tmp_str)) + if (config_get_array(conf, "video_shader_type", tmp_str, sizeof(tmp_str))) { if (strcmp("cg", tmp_str) == 0) g_settings.video.shader_type = SSNES_SHADER_CG; @@ -349,8 +348,6 @@ static void parse_config_file(void) g_settings.video.shader_type = SSNES_SHADER_AUTO; else if (strcmp("none", tmp_str) == 0) g_settings.video.shader_type = SSNES_SHADER_NONE; - - free(tmp_str); } #endif