file: More cleanups after removing legacy file support
Looks like I missed updating a few places in the File class. I do that now, and I also began updating the unit test to the new system. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
8aa121f291
commit
55f3f06ded
|
@ -6,3 +6,4 @@
|
||||||
share/ocarina/#*
|
share/ocarina/#*
|
||||||
bin/
|
bin/
|
||||||
.sconsign.dblite
|
.sconsign.dblite
|
||||||
|
*.patch
|
||||||
|
|
37
DESIGN
37
DESIGN
|
@ -98,12 +98,6 @@ On-disk files:
|
||||||
- File version:
|
- File version:
|
||||||
#define FILE_VERSION 0
|
#define FILE_VERSION 0
|
||||||
|
|
||||||
- Hint where the file is located:
|
|
||||||
enum FileLocHint {
|
|
||||||
FILE_TYPE_DATA,
|
|
||||||
FILE_TYPE_INVALID,
|
|
||||||
}
|
|
||||||
|
|
||||||
- Open mode:
|
- Open mode:
|
||||||
enum OpenMode {
|
enum OpenMode {
|
||||||
OPEN_READ,
|
OPEN_READ,
|
||||||
|
@ -116,13 +110,12 @@ On-disk files:
|
||||||
private:
|
private:
|
||||||
unsigned int version;
|
unsigned int version;
|
||||||
OpenMode mode;
|
OpenMode mode;
|
||||||
FileLocHint hint;
|
string filename;
|
||||||
string filepath;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
File(string, FileLocHint);
|
File(const std::string &);
|
||||||
~File();
|
~File();
|
||||||
const char *get_filepath();
|
const std::string get_filepath();
|
||||||
const unsigned int get_version();
|
const unsigned int get_version();
|
||||||
bool exists();
|
bool exists();
|
||||||
void open(OpenMode);
|
void open(OpenMode);
|
||||||
|
@ -135,20 +128,22 @@ On-disk files:
|
||||||
File << <OTHER_DATA>;
|
File << <OTHER_DATA>;
|
||||||
|
|
||||||
- API:
|
- API:
|
||||||
File :: File(string filepath, FileLocHint hint);
|
File :: File(const std::string &filename);
|
||||||
Resolve filepath to one of:
|
Store the name of the file.
|
||||||
XDG_DATA_HOME/ocarina/filepath
|
|
||||||
XDG_DATA_HOME/ocarina-debug/filepath
|
|
||||||
XDG_DATA_HOME/ocarina-test/filepath
|
|
||||||
|
|
||||||
If filepath is an empty string, set the file hint to
|
|
||||||
FILE_TYPE_INVALID and do not set the filepath field.
|
|
||||||
|
|
||||||
File :: ~File();
|
File :: ~File();
|
||||||
Close the file stream if it is open.
|
Close the file stream if it is open.
|
||||||
|
|
||||||
const char *File :: get_filepath();
|
const std::string File :: get_filepath();
|
||||||
Return the full filepath to the file.
|
If filename == "":
|
||||||
|
return ""
|
||||||
|
|
||||||
|
Otherwise, resolve filepath to one of:
|
||||||
|
XDG_DATA_HOME/ocarina/filepath
|
||||||
|
XDG_DATA_HOME/ocarina-debug/filepath
|
||||||
|
XDG_DATA_HOME/ocarina-test/filepath
|
||||||
|
|
||||||
|
and return the result.
|
||||||
|
|
||||||
const unsigned int File :: get_version();
|
const unsigned int File :: get_version();
|
||||||
Return the file version number.
|
Return the file version number.
|
||||||
|
@ -159,7 +154,7 @@ On-disk files:
|
||||||
|
|
||||||
bool File :: open(OpenMode mode);
|
bool File :: open(OpenMode mode);
|
||||||
Return false if:
|
Return false if:
|
||||||
- hint == FILE_TYPE_INVALID
|
- filename == ""
|
||||||
- mode == NOT_OPEN
|
- mode == NOT_OPEN
|
||||||
- The file is already open
|
- The file is already open
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
Database<T> :: Database(std::string filepath, bool autosave)
|
Database<T> :: Database(std::string filepath, bool autosave)
|
||||||
: _size(0), _autosave(autosave), _file(filepath, FILE_TYPE_DATA)
|
: _size(0), _autosave(autosave), _file(filepath)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,11 +9,6 @@
|
||||||
|
|
||||||
#define FILE_VERSION 0
|
#define FILE_VERSION 0
|
||||||
|
|
||||||
enum FileLocHint {
|
|
||||||
FILE_TYPE_DATA,
|
|
||||||
FILE_TYPE_INVALID,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum OpenMode {
|
enum OpenMode {
|
||||||
OPEN_READ,
|
OPEN_READ,
|
||||||
OPEN_WRITE,
|
OPEN_WRITE,
|
||||||
|
@ -23,16 +18,15 @@ enum OpenMode {
|
||||||
class File : public std::fstream {
|
class File : public std::fstream {
|
||||||
private:
|
private:
|
||||||
OpenMode mode;
|
OpenMode mode;
|
||||||
FileLocHint hint;
|
std::string filename;
|
||||||
std::string filepath;
|
|
||||||
unsigned int version;
|
unsigned int version;
|
||||||
|
|
||||||
std::string find_dir();
|
const char *find_dir();
|
||||||
bool open_read();
|
bool open_read();
|
||||||
bool open_write();
|
bool open_write();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
File(const std::string &, FileLocHint);
|
File(const std::string &);
|
||||||
~File();
|
~File();
|
||||||
const char *get_filepath();
|
const char *get_filepath();
|
||||||
const unsigned int get_version();
|
const unsigned int get_version();
|
||||||
|
|
|
@ -20,7 +20,7 @@ static unsigned int o_pause_count = 0;
|
||||||
static bool o_should_pause = false;
|
static bool o_should_pause = false;
|
||||||
|
|
||||||
static Queue o_recently_played(Q_ENABLED | Q_REPEAT | Q_NO_SORT | Q_DISABLE_CHANGED_SIZE);
|
static Queue o_recently_played(Q_ENABLED | Q_REPEAT | Q_NO_SORT | Q_DISABLE_CHANGED_SIZE);
|
||||||
static File f_cur_track("cur_track", FILE_TYPE_DATA);
|
static File f_cur_track("cur_track");
|
||||||
|
|
||||||
static void parse_error(GstMessage *error)
|
static void parse_error(GstMessage *error)
|
||||||
{
|
{
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
static std::list<Queue> playqueue_deck;
|
static std::list<Queue> playqueue_deck;
|
||||||
static Queue library_playqueue(Q_ENABLED);
|
static Queue library_playqueue(Q_ENABLED);
|
||||||
static File deck_file("deck", FILE_TYPE_DATA);
|
static File deck_file("deck");
|
||||||
|
|
||||||
static void add_library_track(unsigned int id)
|
static void add_library_track(unsigned int id)
|
||||||
{
|
{
|
||||||
|
|
63
lib/file.cpp
63
lib/file.cpp
|
@ -8,23 +8,16 @@
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
#ifdef CONFIG_TEST
|
#ifdef CONFIG_TEST
|
||||||
#define OCARINA_DIR "ocarina-test"
|
const std::string OCARINA_DIR = "ocarina-test";
|
||||||
#elif CONFIG_DEBUG
|
#elif CONFIG_DEBUG
|
||||||
#define OCARINA_DIR "ocarina-debug"
|
const std::string OCARINA_DIR = "ocarina-debug";
|
||||||
#else
|
#else
|
||||||
#define OCARINA_DIR "ocarina"
|
const std::string OCARINA_DIR = "ocarina";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
File :: File(const std::string &path, FileLocHint file_hint)
|
File :: File(const std::string &name)
|
||||||
: mode(NOT_OPEN), hint(file_hint), version(FILE_VERSION)
|
: mode(NOT_OPEN), filename(name), version(FILE_VERSION)
|
||||||
{
|
{
|
||||||
if (path.size() == 0)
|
|
||||||
hint = FILE_TYPE_INVALID;
|
|
||||||
|
|
||||||
if (hint == FILE_TYPE_INVALID)
|
|
||||||
filepath = "INVALID";
|
|
||||||
else
|
|
||||||
filepath = find_dir() + "/" + path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
File :: ~File()
|
File :: ~File()
|
||||||
|
@ -32,9 +25,22 @@ File :: ~File()
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *File :: find_dir()
|
||||||
|
{
|
||||||
|
std::string res = g_get_user_data_dir();
|
||||||
|
res += "/" + OCARINA_DIR;
|
||||||
|
return res.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
const char *File :: get_filepath()
|
const char *File :: get_filepath()
|
||||||
{
|
{
|
||||||
return filepath.c_str();
|
std::string res;
|
||||||
|
|
||||||
|
if (filename != "") {
|
||||||
|
res = find_dir();
|
||||||
|
res += "/" + filename;
|
||||||
|
}
|
||||||
|
return res.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned int File :: get_version()
|
const unsigned int File :: get_version()
|
||||||
|
@ -44,25 +50,7 @@ const unsigned int File :: get_version()
|
||||||
|
|
||||||
bool File :: exists()
|
bool File :: exists()
|
||||||
{
|
{
|
||||||
return g_file_test(filepath.c_str(), G_FILE_TEST_EXISTS);
|
return g_file_test(filename.c_str(), G_FILE_TEST_EXISTS);
|
||||||
}
|
|
||||||
|
|
||||||
std::string File :: find_dir()
|
|
||||||
{
|
|
||||||
std::string res;
|
|
||||||
|
|
||||||
if (hint == FILE_TYPE_DATA) {
|
|
||||||
res = g_get_user_data_dir();
|
|
||||||
res += "/";
|
|
||||||
res += OCARINA_DIR;
|
|
||||||
} else /* FILE_TYPE_LEGACY */ {
|
|
||||||
res = g_get_home_dir();
|
|
||||||
res += "/.";
|
|
||||||
res += OCARINA_DIR;
|
|
||||||
res += "/library";
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File :: open_read()
|
bool File :: open_read()
|
||||||
|
@ -72,7 +60,7 @@ bool File :: open_read()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::fstream::open(filepath.c_str(), std::fstream::in);
|
std::fstream::open(get_filepath(), std::fstream::in);
|
||||||
if (std::fstream::fail()) {
|
if (std::fstream::fail()) {
|
||||||
dprint("ERROR: File could not be opened for reading\n");
|
dprint("ERROR: File could not be opened for reading\n");
|
||||||
return false;
|
return false;
|
||||||
|
@ -86,13 +74,12 @@ bool File :: open_read()
|
||||||
|
|
||||||
bool File :: open_write()
|
bool File :: open_write()
|
||||||
{
|
{
|
||||||
std::string dir = find_dir();
|
if (g_mkdir_with_parents(find_dir(), 0755) != 0) {
|
||||||
if (g_mkdir_with_parents(dir.c_str(), 0755) != 0) {
|
|
||||||
dprint("ERROR: Could not make directory\n");
|
dprint("ERROR: Could not make directory\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::fstream::open(filepath.c_str(), std::fstream::out);
|
std::fstream::open(get_filepath(), std::fstream::out);
|
||||||
if (std::fstream::fail()) {
|
if (std::fstream::fail()) {
|
||||||
dprint("ERROR: Could not open file for writing\n");
|
dprint("ERROR: Could not open file for writing\n");
|
||||||
return false;
|
return false;
|
||||||
|
@ -105,8 +92,8 @@ bool File :: open_write()
|
||||||
|
|
||||||
bool File :: open(OpenMode m)
|
bool File :: open(OpenMode m)
|
||||||
{
|
{
|
||||||
if (hint == FILE_TYPE_INVALID) {
|
if (filename == "") {
|
||||||
dprint("ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened\n");
|
dprint("ERROR: No filename specified\n");
|
||||||
return false;
|
return false;
|
||||||
} else if (m == NOT_OPEN) {
|
} else if (m == NOT_OPEN) {
|
||||||
dprint("ERROR: NOT_OPEN is not a legal OpenMode\n");
|
dprint("ERROR: NOT_OPEN is not a legal OpenMode\n");
|
||||||
|
|
|
@ -14,7 +14,8 @@ for arg in sys.argv:
|
||||||
|
|
||||||
src = SConscript("src/Sconscript")
|
src = SConscript("src/Sconscript")
|
||||||
|
|
||||||
tests = [ "version" ] #, "file", "database", "index", "filter", "idle", "tag_db",
|
tests = [ "version" , "file" ]
|
||||||
|
#, "file", "database", "index", "filter", "idle", "tag_db",
|
||||||
# "queue" ]
|
# "queue" ]
|
||||||
#scripts = [ "playlist", "library", "deck", "audio", "gui" ]
|
#scripts = [ "playlist", "library", "deck", "audio", "gui" ]
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ int main(int argc, char **argv)
|
||||||
std::string key = argv[optind];
|
std::string key = argv[optind];
|
||||||
|
|
||||||
|
|
||||||
File f("db_entry.txt", FILE_TYPE_DATA);
|
File f("db_entry.txt");
|
||||||
IntEntry ient(i, key);
|
IntEntry ient(i, key);
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
|
|
|
@ -18,8 +18,32 @@
|
||||||
*/
|
*/
|
||||||
#include <file.h>
|
#include <file.h>
|
||||||
#include <print.h>
|
#include <print.h>
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
|
static void test_filepath()
|
||||||
|
{
|
||||||
|
File a("");
|
||||||
|
File b("file.txt");
|
||||||
|
|
||||||
|
test_equal(a.get_version(), (unsigned)0);
|
||||||
|
test_equal((std::string)a.get_filepath(), (std::string)"");
|
||||||
|
|
||||||
|
test_equal(b.get_version(), (unsigned)0);
|
||||||
|
test_equal((std::string)b.get_filepath(), test :: data_file("file.txt"));
|
||||||
|
|
||||||
|
test_equal(a.exists(), false);
|
||||||
|
test_equal(b.exists(), false);
|
||||||
|
test_equal(test :: data_dir_exists(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
run_test("File Constructor Test", test_filepath);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*#include <unistd.h>
|
||||||
|
|
||||||
enum action_t { CLOSE, OPEN, PATHS, READ, VERSION, WRITE };
|
enum action_t { CLOSE, OPEN, PATHS, READ, VERSION, WRITE };
|
||||||
|
|
||||||
|
@ -141,4 +165,4 @@ int main(int argc, char **argv)
|
||||||
f << data << std::endl;
|
f << data << std::endl;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
|
@ -27,7 +27,7 @@ void to_lowercase(const std::string &text)
|
||||||
|
|
||||||
void read_file(unsigned int n)
|
void read_file(unsigned int n)
|
||||||
{
|
{
|
||||||
File f("filter.txt", FILE_TYPE_DATA);
|
File f("filter.txt");
|
||||||
if (f.open(OPEN_READ)) {
|
if (f.open(OPEN_READ)) {
|
||||||
for (unsigned int i = 0; i < n; i++) {
|
for (unsigned int i = 0; i < n; i++) {
|
||||||
std::string text = f.getline();
|
std::string text = f.getline();
|
||||||
|
|
|
@ -29,7 +29,7 @@ void test_results(bool success, unsigned int line)
|
||||||
template <class T>
|
template <class T>
|
||||||
void save_tag(const std::string &file, T &tag)
|
void save_tag(const std::string &file, T &tag)
|
||||||
{
|
{
|
||||||
File f(file, FILE_TYPE_DATA);
|
File f(file);
|
||||||
f.open(OPEN_WRITE);
|
f.open(OPEN_WRITE);
|
||||||
tag.write(f);
|
tag.write(f);
|
||||||
f.close();
|
f.close();
|
||||||
|
@ -38,7 +38,7 @@ void save_tag(const std::string &file, T &tag)
|
||||||
template <class T>
|
template <class T>
|
||||||
void load_tag(const std::string &file, T &tag)
|
void load_tag(const std::string &file, T &tag)
|
||||||
{
|
{
|
||||||
File f(file, FILE_TYPE_DATA);
|
File f(file);
|
||||||
f.open(OPEN_READ);
|
f.open(OPEN_READ);
|
||||||
tag.read(f);
|
tag.read(f);
|
||||||
f.close();
|
f.close();
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
namespace test
|
namespace test
|
||||||
{
|
{
|
||||||
|
@ -36,7 +37,7 @@ namespace test
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
void assert_equal(const T &lhs, const T &rhs, unsigned int line)
|
void check_equal(const T &lhs, const T &rhs, unsigned int line)
|
||||||
{
|
{
|
||||||
if (lhs == rhs)
|
if (lhs == rhs)
|
||||||
std::cout << "Success!" << std::endl;
|
std::cout << "Success!" << std::endl;
|
||||||
|
@ -48,9 +49,29 @@ namespace test
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define equal(lhs, rhs) \
|
template <class T>
|
||||||
begin(); \
|
void equal(const T &lhs, const T &rhs, unsigned int line = 0)
|
||||||
test :: assert_equal(lhs, rhs, __LINE__)
|
{
|
||||||
|
begin();
|
||||||
|
check_equal(lhs, rhs, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string data_dir()
|
||||||
|
{
|
||||||
|
std::string res = g_get_user_data_dir();
|
||||||
|
res += "/ocarina-test";
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string data_file(const std::string &name)
|
||||||
|
{
|
||||||
|
return data_dir() + "/" + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool data_dir_exists()
|
||||||
|
{
|
||||||
|
return g_file_test(data_dir().c_str(), G_FILE_TEST_IS_DIR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define run_test(name, func, ...) \
|
#define run_test(name, func, ...) \
|
||||||
|
@ -60,8 +81,7 @@ namespace test
|
||||||
test :: end(); \
|
test :: end(); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define test_equal(lhs, rhs) \
|
#define test_equal(lhs, rhs) \
|
||||||
do { \
|
do { \
|
||||||
test :: begin(); \
|
test :: equal(lhs, rhs, __LINE__); \
|
||||||
test :: assert_equal(lhs, rhs, __LINE__); \
|
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
Loading…
Reference in New Issue