2013-07-27 11:40:16 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2013 (c) Bryan Schumaker.
|
|
|
|
*/
|
|
|
|
#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
|
|
|
|
#define OCARINA_DIR "ocarina-test"
|
|
|
|
#elif CONFIG_DEBUG
|
|
|
|
#define OCARINA_DIR "ocarina-debug"
|
|
|
|
#else
|
|
|
|
#define OCARINA_DIR "ocarina"
|
|
|
|
#endif
|
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
void File :: find_dir(std::string &res)
|
2013-07-27 11:40:16 -04:00
|
|
|
{
|
2013-07-28 19:57:07 -04:00
|
|
|
switch (hint) {
|
2013-07-27 11:40:16 -04:00
|
|
|
case FILE_TYPE_CONFIG:
|
2013-07-28 19:57:07 -04:00
|
|
|
res = g_get_user_config_dir();
|
2013-07-27 11:40:16 -04:00
|
|
|
break;
|
|
|
|
case FILE_TYPE_DATA:
|
2013-07-28 19:57:07 -04:00
|
|
|
res = g_get_user_data_dir();
|
2013-07-27 11:40:16 -04:00
|
|
|
break;
|
|
|
|
case FILE_TYPE_LEGACY:
|
2013-07-28 19:57:07 -04:00
|
|
|
res = g_get_home_dir();
|
2013-07-27 11:40:16 -04:00
|
|
|
}
|
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
res += "/";
|
|
|
|
switch (hint) {
|
2013-07-27 11:40:16 -04:00
|
|
|
case FILE_TYPE_CONFIG:
|
|
|
|
case FILE_TYPE_DATA:
|
2013-07-28 19:57:07 -04:00
|
|
|
res += OCARINA_DIR;
|
2013-07-27 11:40:16 -04:00
|
|
|
break;
|
|
|
|
case FILE_TYPE_LEGACY:
|
2013-07-28 19:57:07 -04:00
|
|
|
res += ".";
|
|
|
|
res += OCARINA_DIR;
|
2013-07-27 11:40:16 -04:00
|
|
|
}
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
File :: File(std::string path, FileLocHint file_hint)
|
|
|
|
: mode(NOT_OPEN), hint(file_hint), version(FILE_VERSION)
|
|
|
|
{
|
|
|
|
std::string dir;
|
2013-07-27 11:40:16 -04:00
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
find_dir(dir);
|
2013-07-27 11:40:16 -04:00
|
|
|
filepath = dir + "/" + path;
|
|
|
|
}
|
|
|
|
|
2013-07-28 19:57:07 -04:00
|
|
|
File :: ~File()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2013-07-27 11:40:16 -04:00
|
|
|
const char *File :: get_filepath()
|
|
|
|
{
|
|
|
|
return filepath.c_str();
|
|
|
|
}
|
2013-07-28 19:57:07 -04:00
|
|
|
|
|
|
|
bool File :: exists()
|
|
|
|
{
|
|
|
|
return g_file_test(filepath.c_str(), G_FILE_TEST_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File :: open_read()
|
|
|
|
{
|
|
|
|
if (!exists()) {
|
|
|
|
dprint("ERROR: File does not exist (%s)\n", filepath.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.open(filepath.c_str(), std::fstream::in);
|
|
|
|
if (stream.fail()) {
|
|
|
|
dprint("ERROR: Could not open file for reading (%s)\n", filepath.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mode = OPEN_READ;
|
|
|
|
stream >> version;
|
|
|
|
return stream.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File :: open_write()
|
|
|
|
{
|
|
|
|
std::string dir;
|
|
|
|
if (hint == FILE_TYPE_LEGACY) {
|
|
|
|
dprint("ERROR: Cannot write to legacy files (%s)\n", filepath.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_dir(dir);
|
|
|
|
if (g_mkdir_with_parents(dir.c_str(), 0755) != 0) {
|
|
|
|
dprint("ERROR: Could not make directory (%s)\n", dir.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.open(filepath.c_str(), std::fstream::out);
|
|
|
|
if (stream.fail()) {
|
|
|
|
dprint("ERROR: Could not open file for writing (%s)\n", filepath.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mode = OPEN_WRITE;
|
|
|
|
stream << version << std::endl;
|
|
|
|
return stream.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File :: open(OpenMode m)
|
|
|
|
{
|
|
|
|
if (mode != NOT_OPEN) {
|
|
|
|
dprint("ERROR: File is already open (%s)\n", filepath.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m == NOT_OPEN) {
|
|
|
|
dprint("ERROR: NOT_OPEN is not a legal OpenMode (%s)\n", filepath.c_str());
|
|
|
|
return false;
|
|
|
|
} else if (m == OPEN_READ)
|
|
|
|
return open_read();
|
|
|
|
else /* m == OPEN_WRITE */
|
|
|
|
return open_write();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool File :: close()
|
|
|
|
{
|
|
|
|
if (mode != NOT_OPEN)
|
|
|
|
stream.close();
|
|
|
|
mode = NOT_OPEN;
|
|
|
|
return stream.good();
|
|
|
|
}
|