diff --git a/core/file.cpp b/core/file.cpp index 608c5da0..da2da58f 100644 --- a/core/file.cpp +++ b/core/file.cpp @@ -32,7 +32,7 @@ static bool __file_mkdir() void file_init(struct file *file, const std::string &name, unsigned int version) { - file->f_mode = NOT_OPEN; + file->f_mode = OPEN_READ; file->f_version = version; file->f_prev = 0; file->f_file = NULL; @@ -52,7 +52,7 @@ const std::string file_path(struct file *file) const unsigned int file_version(struct file *file) { - if (file->f_mode == OPEN_READ) + if (file->f_file && (file->f_mode == OPEN_READ)) return file->f_prev; return file->f_version; } @@ -94,8 +94,7 @@ static bool __file_open_write(struct file *file) bool file_open(struct file *file, OpenMode mode) { - if ((file->f_name == "") || (mode == NOT_OPEN) || - (file->f_mode != NOT_OPEN)) + if ((file->f_name == "") || (file->f_file != NULL)) return false; if (mode == OPEN_READ) @@ -105,12 +104,9 @@ bool file_open(struct file *file, OpenMode mode) void file_close(struct file *file) { - if (file->f_mode != NOT_OPEN) { - if (file->f_file) - fclose(file->f_file); - file->f_file = NULL; - } - file->f_mode = NOT_OPEN; + if (file->f_file) + fclose(file->f_file); + file->f_file = NULL; } int file_readf(struct file *file, const char *fmt, ...) diff --git a/include/core/file.h b/include/core/file.h index 78ded272..c2bd54e7 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -16,7 +16,6 @@ enum OpenMode { OPEN_READ, /**< File is open for reading. */ OPEN_WRITE, /**< File is open for writing. */ - NOT_OPEN, /**< File is not open. */ }; @@ -81,7 +80,7 @@ bool file_exists(struct file *); */ bool file_open(struct file *, OpenMode); -/* Close an open file, setting file->mode to NOT_OPEN. */ +/* Close an open file, setting file->f_file to NULL. */ void file_close(struct file *); /* Read an entire line from the file and return it to the caller. */ diff --git a/tests/core/file.cpp b/tests/core/file.cpp index ecf76d48..cdc605d8 100644 --- a/tests/core/file.cpp +++ b/tests/core/file.cpp @@ -10,7 +10,7 @@ static void test_verify_constructor(struct file *file, const std::string &fpath) { test_equal((void *)file->f_file, NULL); test_equal(file_version(file), 0); - test_equal(file->f_mode, NOT_OPEN); + test_equal(file->f_mode, OPEN_READ); test_equal(file_path(file), fpath); } @@ -21,13 +21,10 @@ static void test_empty() file_init(&fempty, "", 0); test_verify_constructor(&fempty, ""); - test_equal(file_open(&fempty, NOT_OPEN), (bool)false); test_equal(file_open(&fempty, OPEN_READ), (bool)false); test_equal((void *)fempty.f_file, NULL); - test_equal(fempty.f_mode, NOT_OPEN); test_equal(file_open(&fempty, OPEN_WRITE), (bool)false); test_equal((void *)fempty.f_file, NULL); - test_equal(fempty.f_mode, NOT_OPEN); test_equal(file_exists(&fempty), (bool)false); test_equal(test_data_file_exists(NULL), (bool)false); @@ -43,7 +40,6 @@ static void test_file() test_equal(file_exists(&file), (bool)false); test_equal(test_data_file_exists(NULL), (bool)false); - test_equal(file_open(&file, NOT_OPEN), (bool)false); test_equal(file_open(&file, OPEN_READ), (bool)false); test_equal(file_open(&file, OPEN_WRITE), (bool)true); test_not_equal((void *)file.f_file, NULL); @@ -52,7 +48,7 @@ static void test_file() file_close(&file); test_equal((void *)file.f_file, NULL); - test_equal(file.f_mode, NOT_OPEN); + test_equal(file.f_mode, OPEN_WRITE); test_equal(file_exists(&file), (bool)true); test_equal(test_data_file_exists("file.txt"), (bool)true);