core/file: Add a C-style initializer macro

I eventually intend to remove the file_init() function, once everything
has been converted to C.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-12 08:30:01 -04:00
parent cf6364f5d1
commit 08394c3168
2 changed files with 11 additions and 6 deletions

View File

@ -43,6 +43,14 @@ struct file {
gchar f_name[FILE_MAX_LEN]; /* The file's basename. */
};
#define FILE_INIT(fname, version) \
{ \
.f_mode = OPEN_READ, \
.f_version = version, \
.f_prev = 0, .f_file = NULL, \
.f_name = fname, \
}
/* Initialize a new file object. */
void file_init(struct file *, const gchar *, unsigned int);

View File

@ -48,10 +48,9 @@ static void test_empty()
static void test_file()
{
struct file file;
struct file file = FILE_INIT("file.txt", 0);
gchar *filepath = test_data_file("file.txt");
file_init(&file, "file.txt", 0);
test_verify_constructor(&file, filepath);
test_equal(file_exists(&file), (bool)false);
test_equal(test_data_file_exists(NULL), (bool)false);
@ -86,13 +85,11 @@ static void test_file()
static void test_io()
{
struct file fout, fin;
struct file fout = FILE_INIT("file.txt", 1);
struct file fin = FILE_INIT("file.txt", 0);
char *res = NULL;
unsigned int i;
file_init(&fout, "file.txt", 1);
file_init(&fin, "file.txt", 0);
test_equal(file_open(&fout, OPEN_WRITE), (bool)true);
file_writef(&fout, "1 ABCDE\n");
file_writef(&fout, "2 FGHIJ KLMNO\n");