core/file: Remove NOT_OPEN open mode
I can use the file->f_file pointer for the same thing. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
dbd27d1297
commit
d783c6b479
|
@ -32,7 +32,7 @@ static bool __file_mkdir()
|
||||||
|
|
||||||
void file_init(struct file *file, const std::string &name, unsigned int version)
|
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_version = version;
|
||||||
file->f_prev = 0;
|
file->f_prev = 0;
|
||||||
file->f_file = NULL;
|
file->f_file = NULL;
|
||||||
|
@ -52,7 +52,7 @@ const std::string file_path(struct file *file)
|
||||||
|
|
||||||
const unsigned int file_version(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_prev;
|
||||||
return file->f_version;
|
return file->f_version;
|
||||||
}
|
}
|
||||||
|
@ -94,8 +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 == "") || (mode == NOT_OPEN) ||
|
if ((file->f_name == "") || (file->f_file != NULL))
|
||||||
(file->f_mode != NOT_OPEN))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (mode == OPEN_READ)
|
if (mode == OPEN_READ)
|
||||||
|
@ -105,12 +104,9 @@ bool file_open(struct file *file, OpenMode mode)
|
||||||
|
|
||||||
void file_close(struct file *file)
|
void file_close(struct file *file)
|
||||||
{
|
{
|
||||||
if (file->f_mode != NOT_OPEN) {
|
if (file->f_file)
|
||||||
if (file->f_file)
|
fclose(file->f_file);
|
||||||
fclose(file->f_file);
|
file->f_file = NULL;
|
||||||
file->f_file = NULL;
|
|
||||||
}
|
|
||||||
file->f_mode = NOT_OPEN;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int file_readf(struct file *file, const char *fmt, ...)
|
int file_readf(struct file *file, const char *fmt, ...)
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
enum OpenMode {
|
enum OpenMode {
|
||||||
OPEN_READ, /**< File is open for reading. */
|
OPEN_READ, /**< File is open for reading. */
|
||||||
OPEN_WRITE, /**< File is open for writing. */
|
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);
|
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 *);
|
void file_close(struct file *);
|
||||||
|
|
||||||
/* Read an entire line from the file and return it to the caller. */
|
/* Read an entire line from the file and return it to the caller. */
|
||||||
|
|
|
@ -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((void *)file->f_file, NULL);
|
||||||
test_equal(file_version(file), 0);
|
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);
|
test_equal(file_path(file), fpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,13 +21,10 @@ static void test_empty()
|
||||||
file_init(&fempty, "", 0);
|
file_init(&fempty, "", 0);
|
||||||
test_verify_constructor(&fempty, "");
|
test_verify_constructor(&fempty, "");
|
||||||
|
|
||||||
test_equal(file_open(&fempty, NOT_OPEN), (bool)false);
|
|
||||||
test_equal(file_open(&fempty, OPEN_READ), (bool)false);
|
test_equal(file_open(&fempty, OPEN_READ), (bool)false);
|
||||||
test_equal((void *)fempty.f_file, NULL);
|
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(file_open(&fempty, OPEN_WRITE), (bool)false);
|
||||||
test_equal((void *)fempty.f_file, NULL);
|
test_equal((void *)fempty.f_file, NULL);
|
||||||
test_equal(fempty.f_mode, NOT_OPEN);
|
|
||||||
|
|
||||||
test_equal(file_exists(&fempty), (bool)false);
|
test_equal(file_exists(&fempty), (bool)false);
|
||||||
test_equal(test_data_file_exists(NULL), (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(file_exists(&file), (bool)false);
|
||||||
test_equal(test_data_file_exists(NULL), (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_READ), (bool)false);
|
||||||
test_equal(file_open(&file, OPEN_WRITE), (bool)true);
|
test_equal(file_open(&file, OPEN_WRITE), (bool)true);
|
||||||
test_not_equal((void *)file.f_file, NULL);
|
test_not_equal((void *)file.f_file, NULL);
|
||||||
|
@ -52,7 +48,7 @@ static void test_file()
|
||||||
|
|
||||||
file_close(&file);
|
file_close(&file);
|
||||||
test_equal((void *)file.f_file, NULL);
|
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(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);
|
||||||
|
|
Loading…
Reference in New Issue