From c308ba7f8ec8d4a8a53c3d0c712dbd116b8e9535 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 30 Mar 2016 10:17:42 -0400 Subject: [PATCH] core/file: Rewrite file opening There are enough differences in the read and write paths that __file_open_common() was still trying to account for. Let's make a simpler wrapper around g_fopen() and move differing code into __file_open_read() and __file_open_write(). Signed-off-by: Anna Schumaker --- core/file.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/core/file.c b/core/file.c index ea27adc2..7a78379d 100644 --- a/core/file.c +++ b/core/file.c @@ -14,6 +14,16 @@ static gchar *__file_path(const gchar *name) return g_strjoin("/", g_get_user_data_dir(), OCARINA_NAME, name, NULL); } +static FILE *__file_open(gchar *path, const gchar *mode) +{ + FILE *ret = g_fopen(path, mode); + + if (!ret) + REPORT_ERROR(path); + g_free(path); + return ret; +} + static bool __file_mkdir() { gchar *dir = __file_path(NULL); @@ -58,27 +68,16 @@ bool file_exists(struct file *file) return ret; } -static bool __file_open_common(struct file *file, enum open_mode mode) -{ - gchar *path = file_path(file); - file->f_file = g_fopen(path, (mode == OPEN_READ) ? "r" : "w"); - g_free(path); - - if (!file->f_file) { - REPORT_ERROR(file->f_name); - return false; - } - - file->f_mode = mode; - return true; -} - static bool __file_open_read(struct file *file) { if (!file_exists(file)) return false; - if (!__file_open_common(file, OPEN_READ)) + + file->f_file = __file_open(file_path(file), "r"); + if (!file->f_file) return false; + + file->f_mode = OPEN_READ; return file_readf(file, "%u\n", &file->f_prev) == 1; } @@ -86,8 +85,12 @@ static bool __file_open_write(struct file *file) { if (!__file_mkdir()) return false; - if (!__file_open_common(file, OPEN_WRITE)) + + file->f_file = __file_open(file_path(file), "w"); + if (!file->f_file) return false; + + file->f_mode = OPEN_WRITE; return file_writef(file, "%d\n", file->f_version) > 0; }