From cf6364f5d1d2f38e85509685532f2a9008426c5d Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 9 Oct 2015 13:55:34 -0400 Subject: [PATCH] core/file.c: Convert file to C Signed-off-by: Anna Schumaker --- core/{file.cpp => file.c} | 6 ++-- include/core/database.h | 2 ++ include/core/file.h | 53 +++++++++++++-------------------- include/core/queue.h | 2 ++ tests/core/Sconscript | 2 +- tests/core/{file.cpp => file.c} | 5 ++-- 6 files changed, 31 insertions(+), 39 deletions(-) rename core/{file.cpp => file.c} (95%) rename tests/core/{file.cpp => file.c} (97%) diff --git a/core/file.cpp b/core/file.c similarity index 95% rename from core/file.cpp rename to core/file.c index 118ed09d..3b122868 100644 --- a/core/file.cpp +++ b/core/file.c @@ -1,4 +1,4 @@ -/** +/* * Copyright 2013 (c) Anna Schumaker. */ #include @@ -66,7 +66,7 @@ bool file_exists(struct file *file) return ret; } -static bool __file_open_common(struct file *file, OpenMode mode) +static bool __file_open_common(struct file *file, enum open_mode mode) { gchar *path = file_path(file); file->f_file = g_fopen(path, (mode == OPEN_READ) ? "r" : "w"); @@ -99,7 +99,7 @@ static bool __file_open_write(struct file *file) return file_writef(file, "%d\n", file->f_version) > 0; } -bool file_open(struct file *file, OpenMode mode) +bool file_open(struct file *file, enum open_mode mode) { if ((strlen(file->f_name) == 0) || (file->f_file != NULL)) return false; diff --git a/include/core/database.h b/include/core/database.h index aa7140b4..faa57c3f 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -4,7 +4,9 @@ #ifndef OCARINA_CORE_DATABASE_H #define OCARINA_CORE_DATABASE_H +extern "C" { #include +} #include #include diff --git a/include/core/file.h b/include/core/file.h index e3e53b27..8ecde903 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -1,28 +1,6 @@ -/** +/* * Copyright 2013 (c) Anna Schumaker. - */ -#ifndef OCARINA_CORE_FILE_H -#define OCARINA_CORE_FILE_H - -#include -#include - -#include - - -#define FILE_MAX_LEN 16 - - -/** - * Constants defining how files can be opened. - */ -enum OpenMode { - OPEN_READ, /**< File is open for reading. */ - OPEN_WRITE, /**< File is open for writing. */ -}; - - -/** + * * File data is store in the user's home directory according to the * XDG / freedesktop.org specification, meaning all of our data will * be stored in a subdirectory of $XDG_DATA_HOME. The actual subdirectory @@ -35,19 +13,30 @@ enum OpenMode { * CONFIG_DEBUG | $XDG_DATA_HOME/ocarina-debug/ * CONFIG_TESTING | $XDG_DATA_HOME/ocarina-test/ * - * Data should be written to files using either a space or newline as a - * delimiter. The beginning of every file is the current file version - * followed by a newline. For example: + * The beginning of every file is the current file version followed by a + * newline. For example: * * 42 * * - * - * Data should be written to files using either a space or newline as - * a delimiter. */ +#ifndef OCARINA_CORE_FILE_H +#define OCARINA_CORE_FILE_H + +#include +#include +#include + + +#define FILE_MAX_LEN 16 + +enum open_mode { + OPEN_READ, + OPEN_WRITE, +}; + struct file { - OpenMode f_mode; /* The file's current open mode. */ + enum open_mode f_mode; /* The file's current open mode. */ 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. */ @@ -84,7 +73,7 @@ bool file_exists(struct file *); * * Returns true if the open was successful and false otherwise. */ -bool file_open(struct file *, OpenMode); +bool file_open(struct file *, enum open_mode); /* Close an open file, setting file->f_file to NULL. */ void file_close(struct file *); diff --git a/include/core/queue.h b/include/core/queue.h index 8d243302..0b7265f5 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -4,7 +4,9 @@ #ifndef OCARINA_CORE_QUEUE_H #define OCARINA_CORE_QUEUE_H +extern "C" { #include +} #include #include diff --git a/tests/core/Sconscript b/tests/core/Sconscript index 966c2cbe..df72254b 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -16,7 +16,7 @@ Export("core_objs") res = [ CoreTest("version", "version.c") ] res += [ CoreTest("string", "string.c") ] res += [ CoreTest("random", "random.c") ] -res += [ CoreTest("file", "file.cpp") ] +res += [ CoreTest("file", "file.c") ] res += [ CoreTest("database", "database.cpp") ] res += [ CoreTest("index", "index.cpp") ] res += [ CoreTest("filter", "filter.cpp") ] diff --git a/tests/core/file.cpp b/tests/core/file.c similarity index 97% rename from tests/core/file.cpp rename to tests/core/file.c index 498a3c98..da828fea 100644 --- a/tests/core/file.cpp +++ b/tests/core/file.c @@ -2,8 +2,8 @@ * Copyright 2014 (c) Anna Schumaker. */ #include +#include #include -#include "test.h" static void test_verify_constructor(struct file *file, gchar *fpath) @@ -20,8 +20,7 @@ static void test_verify_constructor(struct file *file, gchar *fpath) static void test_invalid_file(struct file *file) { - gchar path[] = ""; - test_verify_constructor(file, path); + test_verify_constructor(file, ""); test_equal(file_open(file, OPEN_READ), (bool)false); test_equal((void *)file->f_file, NULL);