2014-09-16 08:22:34 -04:00
|
|
|
/**
|
|
|
|
* @file
|
2013-08-07 21:00:09 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
2013-07-27 11:40:16 -04:00
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/file.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-05-26 12:10:36 -04:00
|
|
|
File :: File(const std::string &name, unsigned int vers)
|
|
|
|
: mode(NOT_OPEN), filename(name), version(vers), prev_version(0)
|
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-05-18 10:15:51 -04:00
|
|
|
const std::string File :: find_dir()
|
2014-04-26 13:31:22 -04:00
|
|
|
{
|
2014-05-18 10:15:51 -04:00
|
|
|
std::string res(g_get_user_data_dir());
|
2014-04-26 13:31:22 -04:00
|
|
|
res += "/" + OCARINA_DIR;
|
2014-05-18 10:15:51 -04:00
|
|
|
return res;
|
2014-04-26 13:31:22 -04:00
|
|
|
}
|
|
|
|
|
2014-05-18 10:15:51 -04:00
|
|
|
const std::string File :: get_filepath()
|
2013-07-27 11:40:16 -04:00
|
|
|
{
|
2014-05-18 10:15:51 -04:00
|
|
|
std::string res = "";
|
2014-04-26 13:31:22 -04:00
|
|
|
|
|
|
|
if (filename != "") {
|
|
|
|
res = find_dir();
|
|
|
|
res += "/" + filename;
|
|
|
|
}
|
2014-05-18 10:15:51 -04:00
|
|
|
return res;
|
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()
|
|
|
|
{
|
2014-05-26 12:10:36 -04:00
|
|
|
if (mode == OPEN_READ)
|
|
|
|
return prev_version;
|
2013-07-28 22:33:40 -04:00
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
bool File :: exists()
|
|
|
|
{
|
2014-05-18 10:15:51 -04:00
|
|
|
return g_file_test(get_filepath().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
|
|
|
{
|
2014-04-26 14:06:20 -04:00
|
|
|
if (!exists())
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
|
2014-05-18 10:15:51 -04:00
|
|
|
std::fstream::open(get_filepath().c_str(), std::fstream::in);
|
2014-04-26 14:06:20 -04:00
|
|
|
if (std::fstream::fail())
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
|
|
|
|
mode = OPEN_READ;
|
2014-05-26 12:10:36 -04:00
|
|
|
std::fstream::operator>>(prev_version);
|
2013-07-28 22:33:40 -04:00
|
|
|
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-05-18 10:15:51 -04:00
|
|
|
if (g_mkdir_with_parents(find_dir().c_str(), 0755) != 0)
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
|
2014-05-18 10:15:51 -04:00
|
|
|
std::fstream::open(get_filepath().c_str(), std::fstream::out);
|
2014-04-26 14:06:20 -04:00
|
|
|
if (std::fstream::fail())
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
|
|
|
|
mode = OPEN_WRITE;
|
2014-05-26 12:10:36 -04:00
|
|
|
std::fstream::operator<<(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 14:06:20 -04:00
|
|
|
if ((filename == "") || (m == NOT_OPEN) || (mode != NOT_OPEN))
|
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
|
|
|
}
|