core/file: Switch file_init() to take a gchar *

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-09 09:55:23 -04:00
parent d783c6b479
commit d306c809b6
4 changed files with 35 additions and 19 deletions

View File

@ -30,22 +30,22 @@ static bool __file_mkdir()
} }
void file_init(struct file *file, const std::string &name, unsigned int version) void file_init(struct file *file, const gchar *name, unsigned int version)
{ {
file->f_mode = OPEN_READ; file->f_mode = OPEN_READ;
file->f_version = version; file->f_version = version;
file->f_prev = 0; file->f_prev = 0;
file->f_file = NULL; file->f_file = NULL;
file->f_name = name; g_strlcpy(file->f_name, (name == NULL) ? "" : name, FILE_MAX_LEN);
} }
const std::string file_path(struct file *file) const std::string file_path(struct file *file)
{ {
std::string res = ""; std::string res = "";
if (file->f_name != "") { if (strlen(file->f_name) != 0) {
res = find_ocarina_dir(); res = find_ocarina_dir() + "/";
res += "/" + file->f_name; res += file->f_name;
} }
return res; return res;
} }
@ -94,7 +94,7 @@ static bool __file_open_write(struct file *file)
bool file_open(struct file *file, OpenMode mode) bool file_open(struct file *file, OpenMode mode)
{ {
if ((file->f_name == "") || (file->f_file != NULL)) if ((strlen(file->f_name) == 0) || (file->f_file != NULL))
return false; return false;
if (mode == OPEN_READ) if (mode == OPEN_READ)

View File

@ -11,7 +11,7 @@ template <class T>
Database<T> :: Database(std::string filepath, bool autosave) Database<T> :: Database(std::string filepath, bool autosave)
: _size(0), _autosave(autosave) : _size(0), _autosave(autosave)
{ {
file_init(&_file, filepath, 0); file_init(&_file, filepath.c_str(), 0);
} }
template <class T> template <class T>

View File

@ -10,6 +10,9 @@
#include <string> #include <string>
#define FILE_MAX_LEN 16
/** /**
* Constants defining how files can be opened. * Constants defining how files can be opened.
*/ */
@ -48,12 +51,12 @@ struct file {
unsigned int f_version; /* The file's current data version. */ unsigned int f_version; /* The file's current data version. */
unsigned int f_prev; /* The file's on-disk data version. */ unsigned int f_prev; /* The file's on-disk data version. */
FILE *f_file; /* The file's IO stream. */ FILE *f_file; /* The file's IO stream. */
std::string f_name; /* The file's basename. */ gchar f_name[FILE_MAX_LEN]; /* The file's basename. */
}; };
/* Initialize a new file object. */ /* Initialize a new file object. */
void file_init(struct file *, const std::string &, 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. */ /* Returns the full path of the file or an empty string if filename is not set. */
const std::string file_path(struct file *); const std::string file_path(struct file *);

View File

@ -14,20 +14,32 @@ static void test_verify_constructor(struct file *file, const std::string &fpath)
test_equal(file_path(file), fpath); test_equal(file_path(file), fpath);
} }
static void test_invalid_file(struct file *file)
{
test_verify_constructor(file, "");
test_equal(file_open(file, OPEN_READ), (bool)false);
test_equal((void *)file->f_file, NULL);
test_equal(file_open(file, OPEN_WRITE), (bool)false);
test_equal((void *)file->f_file, NULL);
test_equal(file->f_mode, OPEN_READ);
test_equal(file_exists(file), (bool)false);
test_equal(test_data_file_exists(NULL), (bool)false);
}
static void test_null()
{
struct file fnull;
file_init(&fnull, NULL, 0);
test_invalid_file(&fnull);
}
static void test_empty() static void test_empty()
{ {
struct file fempty; struct file fempty;
file_init(&fempty, "", 0); file_init(&fempty, "", 0);
test_verify_constructor(&fempty, ""); test_invalid_file(&fempty);
test_equal(file_open(&fempty, OPEN_READ), (bool)false);
test_equal((void *)fempty.f_file, NULL);
test_equal(file_open(&fempty, OPEN_WRITE), (bool)false);
test_equal((void *)fempty.f_file, NULL);
test_equal(file_exists(&fempty), (bool)false);
test_equal(test_data_file_exists(NULL), (bool)false);
} }
static void test_file() static void test_file()
@ -112,6 +124,7 @@ static void test_io()
} }
DECLARE_UNIT_TESTS( DECLARE_UNIT_TESTS(
UNIT_TEST("File (Path = NULL)", test_null),
UNIT_TEST("File (Path = \"\")", test_empty), UNIT_TEST("File (Path = \"\")", test_empty),
UNIT_TEST("File (Path = \"file.txt\")", test_file), UNIT_TEST("File (Path = \"file.txt\")", test_file),
UNIT_TEST("File I/O", test_io), UNIT_TEST("File I/O", test_io),