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 <Anna@OcarinaProject.net>
This commit is contained in:
parent
224290d4eb
commit
c308ba7f8e
37
core/file.c
37
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue