From 22dc1b9e72a5e63e12e19532fd657c013c2a0f2f Mon Sep 17 00:00:00 2001
From: Twinaphex <autechre1024@hotmail.com>
Date: Sun, 29 Jul 2012 18:17:53 +0200
Subject: [PATCH] (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

---
 conf/config_file.c             | 24 +++++++++++++++++++-----
 console/rarch_console_config.c | 25 +++++--------------------
 console/rarch_console_config.h |  1 -
 3 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/conf/config_file.c b/conf/config_file.c
index 90eb1d2c0d..302bb9184a 100644
--- a/conf/config_file.c
+++ b/conf/config_file.c
@@ -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)
diff --git a/console/rarch_console_config.c b/console/rarch_console_config.c
index fd20dc8c34..376aaf1e73 100644
--- a/console/rarch_console_config.c
+++ b/console/rarch_console_config.c
@@ -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);
-   }
 }
diff --git a/console/rarch_console_config.h b/console/rarch_console_config.h
index 1ed691b422..6d010ac50e 100644
--- a/console/rarch_console_config.h
+++ b/console/rarch_console_config.h
@@ -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);