core/file.c: Convert file to C

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-09 13:55:34 -04:00
parent 7529ccd4cc
commit cf6364f5d1
6 changed files with 31 additions and 39 deletions

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/file.h>
@ -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;

View File

@ -4,7 +4,9 @@
#ifndef OCARINA_CORE_DATABASE_H
#define OCARINA_CORE_DATABASE_H
extern "C" {
#include <core/file.h>
}
#include <map>
#include <string>

View File

@ -1,28 +1,6 @@
/**
/*
* Copyright 2013 (c) Anna Schumaker.
*/
#ifndef OCARINA_CORE_FILE_H
#define OCARINA_CORE_FILE_H
#include <glib.h>
#include <glib/gstdio.h>
#include <string>
#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> <more data>
* <even more data>
*
* 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 <glib.h>
#include <glib/gstdio.h>
#include <stdbool.h>
#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 *);

View File

@ -4,7 +4,9 @@
#ifndef OCARINA_CORE_QUEUE_H
#define OCARINA_CORE_QUEUE_H
extern "C" {
#include <core/file.h>
}
#include <core/tags/track.h>
#include <vector>

View File

@ -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") ]

View File

@ -2,8 +2,8 @@
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/file.h>
#include <tests/test.h>
#include <stdlib.h>
#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);