core/file: Switch file_path() to return a gchar *
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
d306c809b6
commit
471c773bd5
|
@ -6,27 +6,31 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#ifdef CONFIG_TESTING
|
#ifdef CONFIG_TESTING
|
||||||
const std::string OCARINA_DIR = "ocarina-test";
|
const gchar *OCARINA_DIR = "ocarina-test";
|
||||||
#elif CONFIG_DEBUG
|
#elif CONFIG_DEBUG
|
||||||
const std::string OCARINA_DIR = "ocarina-debug";
|
const gchar *OCARINA_DIR = "ocarina-debug";
|
||||||
#else
|
#else
|
||||||
const std::string OCARINA_DIR = "ocarina";
|
const gchar *OCARINA_DIR = "ocarina";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define REPORT_ERROR() \
|
#define REPORT_ERROR() \
|
||||||
printf("%s (%s:%d): %s\n", __func__, __FILE__, __LINE__, strerror(errno))
|
printf("%s (%s:%d): %s\n", __func__, __FILE__, __LINE__, strerror(errno))
|
||||||
|
|
||||||
|
|
||||||
static const std::string find_ocarina_dir()
|
static gchar *__file_path(gchar *name)
|
||||||
{
|
{
|
||||||
std::string res(g_get_user_data_dir());
|
return g_strjoin("/", g_get_user_data_dir(), OCARINA_DIR, name, NULL);
|
||||||
res += "/" + OCARINA_DIR;
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool __file_mkdir()
|
static bool __file_mkdir()
|
||||||
{
|
{
|
||||||
return g_mkdir_with_parents(find_ocarina_dir().c_str(), 0755) == 0;
|
gchar *dir = __file_path(NULL);
|
||||||
|
int ret = g_mkdir_with_parents(dir, 0755);
|
||||||
|
|
||||||
|
g_free(dir);
|
||||||
|
if (ret != 0)
|
||||||
|
REPORT_ERROR();
|
||||||
|
return ret == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,15 +43,11 @@ void file_init(struct file *file, const gchar *name, unsigned int version)
|
||||||
g_strlcpy(file->f_name, (name == NULL) ? "" : name, FILE_MAX_LEN);
|
g_strlcpy(file->f_name, (name == NULL) ? "" : name, FILE_MAX_LEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string file_path(struct file *file)
|
gchar *file_path(struct file *file)
|
||||||
{
|
{
|
||||||
std::string res = "";
|
if (strlen(file->f_name) != 0)
|
||||||
|
return __file_path(file->f_name);
|
||||||
if (strlen(file->f_name) != 0) {
|
return g_strdup("");
|
||||||
res = find_ocarina_dir() + "/";
|
|
||||||
res += file->f_name;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned int file_version(struct file *file)
|
const unsigned int file_version(struct file *file)
|
||||||
|
@ -59,12 +59,19 @@ const unsigned int file_version(struct file *file)
|
||||||
|
|
||||||
bool file_exists(struct file *file)
|
bool file_exists(struct file *file)
|
||||||
{
|
{
|
||||||
return g_file_test(file_path(file).c_str(), G_FILE_TEST_EXISTS);
|
gchar *path = file_path(file);
|
||||||
|
bool ret = g_file_test(path, G_FILE_TEST_EXISTS);
|
||||||
|
|
||||||
|
g_free(path);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool __file_open_common(struct file *file, OpenMode mode)
|
static bool __file_open_common(struct file *file, OpenMode mode)
|
||||||
{
|
{
|
||||||
file->f_file = g_fopen(file_path(file).c_str(), (mode == OPEN_READ) ? "r" : "w");
|
gchar *path = file_path(file);
|
||||||
|
file->f_file = g_fopen(path, (mode == OPEN_READ) ? "r" : "w");
|
||||||
|
g_free(path);
|
||||||
|
|
||||||
if (!file->f_file) {
|
if (!file->f_file) {
|
||||||
REPORT_ERROR();
|
REPORT_ERROR();
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -58,8 +58,11 @@ struct file {
|
||||||
/* Initialize a new file object. */
|
/* Initialize a new file object. */
|
||||||
void file_init(struct file *, const gchar *, unsigned int);
|
void file_init(struct file *, const gchar *, unsigned int);
|
||||||
|
|
||||||
/* Returns the full path of the file or an empty string if filename is not set. */
|
/*
|
||||||
const std::string file_path(struct file *);
|
* Returns the full path of the file or an empty string if filename is not set.
|
||||||
|
* This function allocates a new string that MUST be freed with g_free().
|
||||||
|
*/
|
||||||
|
gchar *file_path(struct 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 *);
|
||||||
|
|
|
@ -6,17 +6,22 @@
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
|
|
||||||
static void test_verify_constructor(struct file *file, const std::string &fpath)
|
static void test_verify_constructor(struct file *file, gchar *fpath)
|
||||||
{
|
{
|
||||||
|
gchar *path = file_path(file);
|
||||||
|
|
||||||
test_equal((void *)file->f_file, NULL);
|
test_equal((void *)file->f_file, NULL);
|
||||||
test_equal(file_version(file), 0);
|
test_equal(file_version(file), 0);
|
||||||
test_equal(file->f_mode, OPEN_READ);
|
test_equal(file->f_mode, OPEN_READ);
|
||||||
test_equal(file_path(file), fpath);
|
test_equal(path, fpath);
|
||||||
|
|
||||||
|
g_free(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_invalid_file(struct file *file)
|
static void test_invalid_file(struct file *file)
|
||||||
{
|
{
|
||||||
test_verify_constructor(file, "");
|
gchar path[] = "";
|
||||||
|
test_verify_constructor(file, path);
|
||||||
|
|
||||||
test_equal(file_open(file, OPEN_READ), (bool)false);
|
test_equal(file_open(file, OPEN_READ), (bool)false);
|
||||||
test_equal((void *)file->f_file, NULL);
|
test_equal((void *)file->f_file, NULL);
|
||||||
|
@ -65,11 +70,11 @@ static void test_file()
|
||||||
test_equal(file_exists(&file), (bool)true);
|
test_equal(file_exists(&file), (bool)true);
|
||||||
test_equal(test_data_file_exists("file.txt"), (bool)true);
|
test_equal(test_data_file_exists("file.txt"), (bool)true);
|
||||||
|
|
||||||
g_chmod(file_path(&file).c_str(), 0444);
|
g_chmod(filepath, 0444);
|
||||||
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
|
test_equal(file_open(&file, OPEN_WRITE), (bool)false);
|
||||||
g_chmod(file_path(&file).c_str(), 0200);
|
g_chmod(filepath, 0200);
|
||||||
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
||||||
g_chmod(file_path(&file).c_str(), 0644);
|
g_chmod(filepath, 0644);
|
||||||
|
|
||||||
test_equal(file_open(&file, OPEN_READ), (bool)true);
|
test_equal(file_open(&file, OPEN_READ), (bool)true);
|
||||||
test_not_equal((void *)file.f_file, NULL);
|
test_not_equal((void *)file.f_file, NULL);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user