2014-09-16 08:22:34 -04:00
|
|
|
/**
|
2013-08-07 21:00:09 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
2013-07-27 11:40:16 -04:00
|
|
|
*/
|
2014-06-21 09:37:47 -04:00
|
|
|
#ifndef OCARINA_CORE_FILE_H
|
|
|
|
#define OCARINA_CORE_FILE_H
|
2013-07-27 11:40:16 -04:00
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
#include <fstream>
|
2013-07-27 11:40:16 -04:00
|
|
|
#include <string>
|
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
|
2014-09-16 08:22:34 -04:00
|
|
|
/**
|
2014-11-01 21:30:06 -04:00
|
|
|
* Constants defining how files can be opened.
|
2014-09-16 08:22:34 -04:00
|
|
|
*/
|
2013-07-28 19:57:07 -04:00
|
|
|
enum OpenMode {
|
2014-11-01 21:30:06 -04:00
|
|
|
OPEN_READ, /**< File is open for reading. */
|
|
|
|
OPEN_WRITE, /**< File is open for writing. */
|
|
|
|
NOT_OPEN, /**< File is not open. */
|
2013-07-28 19:57:07 -04:00
|
|
|
};
|
|
|
|
|
2014-05-26 12:10:36 -04:00
|
|
|
|
2014-09-16 08:22:34 -04:00
|
|
|
/**
|
2014-11-01 21:30:06 -04:00
|
|
|
* 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
|
|
|
|
* changes based on what configuration values the user has set when Ocarina
|
|
|
|
* is compiled.
|
|
|
|
*
|
2015-08-24 08:31:00 -04:00
|
|
|
* Config Value | Ocarina Directory
|
|
|
|
* ---------------|--------------------
|
|
|
|
* default | $XDG_DATA_HOME/ocarina/
|
|
|
|
* CONFIG_DEBUG | $XDG_DATA_HOME/ocarina-debug/
|
|
|
|
* CONFIG_TESTING | $XDG_DATA_HOME/ocarina-test/
|
2014-11-01 21:30:06 -04:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* 42
|
|
|
|
* <data> <more data>
|
|
|
|
* <even more data>
|
|
|
|
*
|
|
|
|
* Data should be written to files using either a space or newline as
|
|
|
|
* a delimiter.
|
2014-09-16 08:22:34 -04:00
|
|
|
*/
|
2015-09-10 08:00:42 -04:00
|
|
|
struct file : public std::fstream {
|
|
|
|
OpenMode 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. */
|
|
|
|
std::string f_name; /* The file's basename. */
|
2013-07-27 11:40:16 -04:00
|
|
|
};
|
|
|
|
|
2015-09-10 08:10:38 -04:00
|
|
|
|
2015-09-10 10:33:24 -04:00
|
|
|
/* Initialize a new file object. */
|
|
|
|
void file_init(struct file *, const std::string &, unsigned int);
|
|
|
|
|
2015-09-10 08:17:53 -04:00
|
|
|
/* Returns the full path of the file or an empty string if filename is not set. */
|
|
|
|
const std::string file_path(struct file *);
|
|
|
|
|
2015-09-10 08:10:38 -04:00
|
|
|
/* Returns the version number of the file. */
|
|
|
|
const unsigned int file_version(struct file *);
|
|
|
|
|
2015-09-10 08:23:09 -04:00
|
|
|
/* Returns true if the file exists on disk and false otherwise. */
|
|
|
|
bool file_exists(struct file *);
|
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
/*
|
|
|
|
* Call to open a file for either reading or writing. Callers
|
|
|
|
* are expected to call file_close() when IO is completed.
|
|
|
|
*
|
|
|
|
* When opening a file for reading (OPEN_READ):
|
|
|
|
* - Check if the file exists.
|
|
|
|
* - Read in file->_prev_version from the start of the file.
|
|
|
|
*
|
|
|
|
* When opening a file for writing (OPEN_WRITE):
|
|
|
|
* - Create missing directories as needed.
|
|
|
|
* - Write file->_version to the start of the file.
|
|
|
|
*
|
|
|
|
* Returns true if the open was successful and false otherwise.
|
|
|
|
*/
|
|
|
|
bool file_open(struct file *, OpenMode);
|
|
|
|
|
|
|
|
|
2015-09-10 09:12:48 -04:00
|
|
|
/* Close an open file, setting file->mode to NOT_OPEN. */
|
|
|
|
void file_close(struct file *);
|
|
|
|
|
2015-10-08 15:40:42 -04:00
|
|
|
/* Read an entire line from the file and return it to the caller. */
|
|
|
|
std::string file_readl(struct file *);
|
2015-09-10 08:10:38 -04:00
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_FILE_H */
|