From d306c809b64fc194c0c68389f4546c1aa6164256 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 9 Oct 2015 09:55:23 -0400 Subject: [PATCH] core/file: Switch file_init() to take a gchar * Signed-off-by: Anna Schumaker --- core/file.cpp | 12 ++++++------ include/core/database.hpp | 2 +- include/core/file.h | 7 +++++-- tests/core/file.cpp | 33 +++++++++++++++++++++++---------- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/core/file.cpp b/core/file.cpp index da2da58f..ab6e540e 100644 --- a/core/file.cpp +++ b/core/file.cpp @@ -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_version = version; file->f_prev = 0; 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) { std::string res = ""; - if (file->f_name != "") { - res = find_ocarina_dir(); - res += "/" + file->f_name; + if (strlen(file->f_name) != 0) { + res = find_ocarina_dir() + "/"; + res += file->f_name; } return res; } @@ -94,7 +94,7 @@ static bool __file_open_write(struct file *file) 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; if (mode == OPEN_READ) diff --git a/include/core/database.hpp b/include/core/database.hpp index 49b35a1c..399ea37f 100644 --- a/include/core/database.hpp +++ b/include/core/database.hpp @@ -11,7 +11,7 @@ template Database :: Database(std::string filepath, bool autosave) : _size(0), _autosave(autosave) { - file_init(&_file, filepath, 0); + file_init(&_file, filepath.c_str(), 0); } template diff --git a/include/core/file.h b/include/core/file.h index c2bd54e7..9e5fa094 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -10,6 +10,9 @@ #include +#define FILE_MAX_LEN 16 + + /** * 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_prev; /* The file's on-disk data version. */ 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. */ -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. */ const std::string file_path(struct file *); diff --git a/tests/core/file.cpp b/tests/core/file.cpp index cdc605d8..96e47c02 100644 --- a/tests/core/file.cpp +++ b/tests/core/file.cpp @@ -14,20 +14,32 @@ static void test_verify_constructor(struct file *file, const std::string &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() { struct file fempty; - file_init(&fempty, "", 0); - test_verify_constructor(&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); + test_invalid_file(&fempty); } static void test_file() @@ -112,6 +124,7 @@ static void test_io() } DECLARE_UNIT_TESTS( + UNIT_TEST("File (Path = NULL)", test_null), UNIT_TEST("File (Path = \"\")", test_empty), UNIT_TEST("File (Path = \"file.txt\")", test_file), UNIT_TEST("File I/O", test_io),