diff --git a/libretro-common/file/config_file.c b/libretro-common/file/config_file.c index d4a0535dbd..bf2a14e14f 100644 --- a/libretro-common/file/config_file.c +++ b/libretro-common/file/config_file.c @@ -462,10 +462,10 @@ static bool config_file_parse_line(config_file_t *conf, /* Check whether entire line is a comment */ if (comment) { + config_file_t sub_conf; char real_path[PATH_MAX_LENGTH]; char *path = NULL; char *include_line = NULL; - config_file_t *sub_conf = NULL; /* Starting a line with an 'include' directive * appends a sub-config file @@ -501,12 +501,13 @@ static bool config_file_parse_line(config_file_t *conf, config_file_add_sub_conf(conf, path, real_path, sizeof(real_path), cb); - sub_conf = config_file_new_alloc(); + config_file_initialize(&sub_conf); - if (config_file_load_internal(sub_conf, real_path, + if (config_file_load_internal(&sub_conf, real_path, conf->include_depth + 1, cb) == 0) - config_file_add_child_list(conf, sub_conf); /* Pilfer internal list. */ - config_file_free(sub_conf); + config_file_add_child_list(conf, &sub_conf); /* Pilfer internal list. */ + + config_file_deinitialize(&sub_conf); free(path); return true; @@ -560,12 +561,12 @@ static bool config_file_parse_line(config_file_t *conf, return true; } -void config_file_free(config_file_t *conf) +bool config_file_deinitialize(config_file_t *conf) { struct config_include_list *inc_tmp = NULL; struct config_entry_list *tmp = NULL; if (!conf) - return; + return false; tmp = conf->entries; while (tmp) @@ -600,6 +601,13 @@ void config_file_free(config_file_t *conf) if (conf->path) free(conf->path); + return true; +} + +void config_file_free(config_file_t *conf) +{ + if (!config_file_deinitialize(conf)) + return; free(conf); } @@ -741,6 +749,21 @@ config_file_t *config_file_new(const char *path) return conf; } +void config_file_initialize(struct config_file *conf) +{ + if (!conf) + return; + + conf->path = NULL; + conf->entries = NULL; + conf->tail = NULL; + conf->last = NULL; + conf->includes = NULL; + conf->include_depth = 0; + conf->guaranteed_no_duplicates = false; + conf->modified = false; +} + config_file_t *config_file_new_alloc(void) { struct config_file *conf = (struct config_file*)malloc(sizeof(*conf)); diff --git a/libretro-common/include/file/config_file.h b/libretro-common/include/file/config_file.h index b19539f18a..c803bb8101 100644 --- a/libretro-common/include/file/config_file.h +++ b/libretro-common/include/file/config_file.h @@ -88,6 +88,8 @@ config_file_t *config_file_new(const char *path); config_file_t *config_file_new_alloc(void); +void config_file_initialize(struct config_file *conf); + /* Loads a config file. Returns NULL if file doesn't exist. * NULL path will create an empty config file. * Includes cb callbacks to run custom code during config file processing.*/