(RARCH_CONSOLE) If a config file does not exist, try to create it

first before entering out of config_file_new function - reduces
some code bloat in console ports
This commit is contained in:
Twinaphex 2012-07-29 18:17:53 +02:00
parent 6aa0eea7bf
commit 22dc1b9e72
3 changed files with 24 additions and 26 deletions

View File

@ -43,6 +43,12 @@
#endif
#endif
#ifdef RARCH_CONSOLE
#define CREATE_FILE_IF_NOT_EXISTS 1
#else
#define CREATE_FILE_IF_NOT_EXISTS 0
#endif
#define MAX_INCLUDE_DEPTH 16
struct entry_list
@ -69,7 +75,7 @@ struct config_file
struct include_list *includes;
};
static config_file_t *config_file_new_internal(const char *path, unsigned depth);
static config_file_t *config_file_new_internal(const char *path, unsigned depth, unsigned create_if_not_exists);
static char *getaline(FILE *file)
{
@ -249,7 +255,7 @@ static void add_sub_conf(config_file_t *conf, char *line)
}
#endif
config_file_t *sub_conf = config_file_new_internal(real_path, conf->include_depth + 1);
config_file_t *sub_conf = config_file_new_internal(real_path, conf->include_depth + 1, CREATE_FILE_IF_NOT_EXISTS);
if (!sub_conf)
{
free(path);
@ -314,7 +320,7 @@ static bool parse_line(config_file_t *conf, struct entry_list *list, char *line)
return true;
}
static config_file_t *config_file_new_internal(const char *path, unsigned depth)
static config_file_t *config_file_new_internal(const char *path, unsigned depth, unsigned create_if_not_exists)
{
struct config_file *conf = (struct config_file*)calloc(1, sizeof(*conf));
if (!conf)
@ -333,7 +339,15 @@ static config_file_t *config_file_new_internal(const char *path, unsigned depth)
conf->include_depth = depth;
FILE *file = fopen(path, "r");
if (!file)
if (!file && create_if_not_exists)
{
file = fopen(path, "w");
fclose(file);
file = fopen(path, "r"); // try again to open
}
if(!file)
{
free(conf->path);
free(conf);
@ -374,7 +388,7 @@ static config_file_t *config_file_new_internal(const char *path, unsigned depth)
config_file_t *config_file_new(const char *path)
{
return config_file_new_internal(path, 0);
return config_file_new_internal(path, 0, CREATE_FILE_IF_NOT_EXISTS);
}
void config_file_free(config_file_t *conf)

View File

@ -22,22 +22,13 @@
#include "rarch_console_config.h"
#include "rarch_console_libretro_mgmt.h"
void rarch_config_create_default(const char * conf_name)
{
FILE * f;
RARCH_WARN("Config file \"%s\" doesn't exist. Creating...\n", conf_name);
f = fopen(conf_name, "w");
fclose(f);
}
void rarch_config_load(const char * conf_name, const char * libretro_dir_path, const char * exe_ext, bool find_libretro_path)
{
if(!path_file_exists(conf_name))
rarch_config_create_default(conf_name);
else
{
config_file_t * conf = config_file_new(conf_name);
if(!conf)
return;
// g_settings
#ifdef HAVE_LIBRETRO_MANAGEMENT
@ -113,19 +104,14 @@ void rarch_config_load(const char * conf_name, const char * libretro_dir_path, c
// g_extern
CONFIG_GET_INT_EXTERN(state_slot, "state_slot");
CONFIG_GET_INT_EXTERN(audio_data.mute, "audio_mute");
}
}
void rarch_config_save(const char * conf_name)
{
if(!path_file_exists(conf_name))
rarch_config_create_default(conf_name);
else
{
config_file_t * conf = config_file_new(conf_name);
if(conf == NULL)
conf = config_file_new(NULL);
if(!conf)
return;
// g_settings
config_set_string(conf, "libretro_path", g_settings.libretro);
@ -191,5 +177,4 @@ void rarch_config_save(const char * conf_name)
RARCH_ERR("Failed to write config file to \"%s\". Check permissions.\n", conf_name);
free(conf);
}
}

View File

@ -24,7 +24,6 @@ enum
INPUT_PRESET_FILE
};
void rarch_config_create_default(const char * conf_name);
void rarch_config_load(const char * conf_name, const char * libretro_dir_path, const char * exe_ext, bool find_libretro_path);
void rarch_config_save(const char * conf_name);