2013-07-27 11:40:16 -04:00
|
|
|
/*
|
2013-08-07 21:00:09 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
2013-07-27 11:40:16 -04:00
|
|
|
*/
|
2013-12-29 21:12:32 -05:00
|
|
|
#include <error.h>
|
2013-07-27 11:40:16 -04:00
|
|
|
#include <file.h>
|
2013-07-28 19:57:07 -04:00
|
|
|
#include <print.h>
|
2013-07-27 11:40:16 -04:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_TEST
|
2014-04-26 13:31:22 -04:00
|
|
|
const std::string OCARINA_DIR = "ocarina-test";
|
2013-07-27 11:40:16 -04:00
|
|
|
#elif CONFIG_DEBUG
|
2014-04-26 13:31:22 -04:00
|
|
|
const std::string OCARINA_DIR = "ocarina-debug";
|
2013-07-27 11:40:16 -04:00
|
|
|
#else
|
2014-04-26 13:31:22 -04:00
|
|
|
const std::string OCARINA_DIR = "ocarina";
|
2013-07-27 11:40:16 -04:00
|
|
|
#endif
|
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
File :: File(const std::string &name)
|
|
|
|
: mode(NOT_OPEN), filename(name), version(FILE_VERSION)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2013-07-27 11:40:16 -04:00
|
|
|
}
|
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
File :: ~File()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
const char *File :: find_dir()
|
|
|
|
{
|
|
|
|
std::string res = g_get_user_data_dir();
|
|
|
|
res += "/" + OCARINA_DIR;
|
|
|
|
return res.c_str();
|
|
|
|
}
|
|
|
|
|
2013-07-27 11:40:16 -04:00
|
|
|
const char *File :: get_filepath()
|
|
|
|
{
|
2014-04-26 13:31:22 -04:00
|
|
|
std::string res;
|
|
|
|
|
|
|
|
if (filename != "") {
|
|
|
|
res = find_dir();
|
|
|
|
res += "/" + filename;
|
|
|
|
}
|
|
|
|
return res.c_str();
|
2013-07-27 11:40:16 -04:00
|
|
|
}
|
2013-07-28 19:57:07 -04:00
|
|
|
|
2013-07-28 22:33:40 -04:00
|
|
|
const unsigned int File :: get_version()
|
|
|
|
{
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
bool File :: exists()
|
|
|
|
{
|
2014-04-26 13:31:22 -04:00
|
|
|
return g_file_test(filename.c_str(), G_FILE_TEST_EXISTS);
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2014-03-09 10:34:06 -04:00
|
|
|
bool File :: open_read()
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
|
|
|
if (!exists()) {
|
2014-03-02 14:44:24 -05:00
|
|
|
dprint("ERROR: File does not exist\n");
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
std::fstream::open(get_filepath(), std::fstream::in);
|
2013-07-28 22:33:40 -04:00
|
|
|
if (std::fstream::fail()) {
|
2014-03-02 14:44:24 -05:00
|
|
|
dprint("ERROR: File could not be opened for reading\n");
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mode = OPEN_READ;
|
2013-07-28 22:33:40 -04:00
|
|
|
std::fstream::operator>>(version);
|
|
|
|
getline();
|
2014-03-09 10:34:06 -04:00
|
|
|
return true;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2014-03-09 10:34:06 -04:00
|
|
|
bool File :: open_write()
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2014-04-26 13:31:22 -04:00
|
|
|
if (g_mkdir_with_parents(find_dir(), 0755) != 0) {
|
2014-03-02 14:44:24 -05:00
|
|
|
dprint("ERROR: Could not make directory\n");
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
std::fstream::open(get_filepath(), std::fstream::out);
|
2013-07-28 22:33:40 -04:00
|
|
|
if (std::fstream::fail()) {
|
2014-03-02 14:44:24 -05:00
|
|
|
dprint("ERROR: Could not open file for writing\n");
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mode = OPEN_WRITE;
|
2013-07-28 22:33:40 -04:00
|
|
|
std::fstream::operator<<(FILE_VERSION) << std::endl;
|
2014-03-09 10:34:06 -04:00
|
|
|
return true;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2014-03-09 10:34:06 -04:00
|
|
|
bool File :: open(OpenMode m)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2014-04-26 13:31:22 -04:00
|
|
|
if (filename == "") {
|
|
|
|
dprint("ERROR: No filename specified\n");
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2014-03-02 14:44:24 -05:00
|
|
|
} else if (m == NOT_OPEN) {
|
|
|
|
dprint("ERROR: NOT_OPEN is not a legal OpenMode\n");
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2014-03-02 14:44:24 -05:00
|
|
|
} else if (mode != NOT_OPEN) {
|
|
|
|
dprint("ERROR: File is already open\n");
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2014-03-02 14:44:24 -05:00
|
|
|
else if (m == OPEN_READ)
|
2014-03-09 10:34:06 -04:00
|
|
|
return open_read();
|
2013-07-28 19:57:07 -04:00
|
|
|
else /* m == OPEN_WRITE */
|
2014-03-09 10:34:06 -04:00
|
|
|
return open_write();
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2013-12-29 21:12:32 -05:00
|
|
|
void File :: close()
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
|
|
|
if (mode != NOT_OPEN)
|
2013-07-28 22:33:40 -04:00
|
|
|
std::fstream::close();
|
2013-07-28 19:57:07 -04:00
|
|
|
mode = NOT_OPEN;
|
2013-07-28 22:33:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string File :: getline()
|
|
|
|
{
|
2014-01-22 19:21:11 -05:00
|
|
|
char c;
|
2013-07-28 22:33:40 -04:00
|
|
|
std::string res;
|
2014-01-22 19:21:11 -05:00
|
|
|
|
|
|
|
/* Ignore leading whitespace */
|
|
|
|
while (peek() == ' ')
|
|
|
|
read(&c, 1);
|
|
|
|
|
2013-07-28 22:33:40 -04:00
|
|
|
std::getline(*static_cast<std::fstream *>(this), res);
|
|
|
|
return res;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|