core/file: Add cache_file_write_path() function
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
5dcf681e48
commit
79ecaa11fb
30
core/file.c
30
core/file.c
|
@ -25,6 +25,21 @@ static gchar *__file_build_path(const gchar *base, const gchar *dir,
|
||||||
return __file_path(base, dir, name);
|
return __file_path(base, dir, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gchar *__file_build_tmp(const gchar *base, const gchar *dir,
|
||||||
|
const gchar *name)
|
||||||
|
{
|
||||||
|
gchar *tmp, *res;
|
||||||
|
|
||||||
|
if (string_length(name) == 0)
|
||||||
|
return g_strdup("");
|
||||||
|
|
||||||
|
tmp = g_strdup_printf(".%s.tmp", name);
|
||||||
|
res = __file_path(base, dir, tmp);
|
||||||
|
g_free(tmp);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static FILE *__file_open(gchar *path, const gchar *mode)
|
static FILE *__file_open(gchar *path, const gchar *mode)
|
||||||
{
|
{
|
||||||
FILE *ret = g_fopen(path, mode);
|
FILE *ret = g_fopen(path, mode);
|
||||||
|
@ -101,16 +116,13 @@ gchar *cache_file_path(struct cache_file *file)
|
||||||
|
|
||||||
gchar *file_write_path(struct file *file)
|
gchar *file_write_path(struct file *file)
|
||||||
{
|
{
|
||||||
gchar *fname, *res;
|
return __file_build_tmp(g_get_user_data_dir(), NULL, file->f_name);
|
||||||
|
}
|
||||||
|
|
||||||
if (string_length(file->f_name) == 0)
|
gchar *cache_file_write_path(struct cache_file *file)
|
||||||
return g_strdup("");
|
{
|
||||||
|
return __file_build_tmp(g_get_user_cache_dir(), file->cf_subdir,
|
||||||
fname = g_strdup_printf(".%s.tmp", file->f_name);
|
file->cf_name);
|
||||||
res = __file_path(g_get_user_data_dir(), NULL, fname);
|
|
||||||
g_free(fname);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned int file_version(struct file *file)
|
const unsigned int file_version(struct file *file)
|
||||||
|
|
|
@ -82,6 +82,7 @@ gchar *cache_file_path(struct cache_file *);
|
||||||
* This function allocates a new string that MUST be freed with g_free().
|
* This function allocates a new string that MUST be freed with g_free().
|
||||||
*/
|
*/
|
||||||
gchar *file_write_path(struct file *);
|
gchar *file_write_path(struct file *);
|
||||||
|
gchar *cache_file_write_path(struct cache_file *);
|
||||||
|
|
||||||
/* Returns the version number of the file. */
|
/* Returns the version number of the file. */
|
||||||
const unsigned int file_version(struct file *);
|
const unsigned int file_version(struct file *);
|
||||||
|
|
|
@ -146,16 +146,18 @@ static void test_versioning()
|
||||||
static void test_cache()
|
static void test_cache()
|
||||||
{
|
{
|
||||||
struct cache_file file = CACHE_FILE_INIT("dir", "file.txt");
|
struct cache_file file = CACHE_FILE_INIT("dir", "file.txt");
|
||||||
gchar *basepath, *filepath;
|
gchar *basepath, *filepath, *writepath;
|
||||||
|
|
||||||
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
|
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
|
||||||
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
|
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
|
||||||
|
writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", NULL);
|
||||||
|
|
||||||
test_equal((void *)file.cf_file, NULL);
|
test_equal((void *)file.cf_file, NULL);
|
||||||
test_equal(file.cf_name, "file.txt");
|
test_equal(file.cf_name, "file.txt");
|
||||||
test_equal(file.cf_subdir, "dir");
|
test_equal(file.cf_subdir, "dir");
|
||||||
|
|
||||||
test_str_equal(cache_file_path(&file), filepath);
|
test_str_equal(cache_file_path(&file), filepath);
|
||||||
|
test_str_equal(cache_file_write_path(&file), writepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
DECLARE_UNIT_TESTS(
|
DECLARE_UNIT_TESTS(
|
||||||
|
|
Loading…
Reference in New Issue