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/#*
|
||||
bin/
|
||||
.sconsign.dblite
|
||||
*.patch
|
||||
|
|
37
DESIGN
37
DESIGN
|
@ -98,12 +98,6 @@ On-disk files:
|
|||
- File version:
|
||||
#define FILE_VERSION 0
|
||||
|
||||
- Hint where the file is located:
|
||||
enum FileLocHint {
|
||||
FILE_TYPE_DATA,
|
||||
FILE_TYPE_INVALID,
|
||||
}
|
||||
|
||||
- Open mode:
|
||||
enum OpenMode {
|
||||
OPEN_READ,
|
||||
|
@ -116,13 +110,12 @@ On-disk files:
|
|||
private:
|
||||
unsigned int version;
|
||||
OpenMode mode;
|
||||
FileLocHint hint;
|
||||
string filepath;
|
||||
string filename;
|
||||
|
||||
public:
|
||||
File(string, FileLocHint);
|
||||
File(const std::string &);
|
||||
~File();
|
||||
const char *get_filepath();
|
||||
const std::string get_filepath();
|
||||
const unsigned int get_version();
|
||||
bool exists();
|
||||
void open(OpenMode);
|
||||
|
@ -135,20 +128,22 @@ On-disk files:
|
|||
File << <OTHER_DATA>;
|
||||
|
||||
- API:
|
||||
File :: File(string filepath, FileLocHint hint);
|
||||
Resolve filepath to one of:
|
||||
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(const std::string &filename);
|
||||
Store the name of the file.
|
||||
|
||||
File :: ~File();
|
||||
Close the file stream if it is open.
|
||||
|
||||
const char *File :: get_filepath();
|
||||
Return the full filepath to the file.
|
||||
const std::string File :: get_filepath();
|
||||
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();
|
||||
Return the file version number.
|
||||
|
@ -159,7 +154,7 @@ On-disk files:
|
|||
|
||||
bool File :: open(OpenMode mode);
|
||||
Return false if:
|
||||
- hint == FILE_TYPE_INVALID
|
||||
- filename == ""
|
||||
- mode == NOT_OPEN
|
||||
- The file is already open
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
template <class T>
|
||||
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
|
||||
|
||||
enum FileLocHint {
|
||||
FILE_TYPE_DATA,
|
||||
FILE_TYPE_INVALID,
|
||||
};
|
||||
|
||||
enum OpenMode {
|
||||
OPEN_READ,
|
||||
OPEN_WRITE,
|
||||
|
@ -23,16 +18,15 @@ enum OpenMode {
|
|||
class File : public std::fstream {
|
||||
private:
|
||||
OpenMode mode;
|
||||
FileLocHint hint;
|
||||
std::string filepath;
|
||||
std::string filename;
|
||||
unsigned int version;
|
||||
|
||||
std::string find_dir();
|
||||
const char *find_dir();
|
||||
bool open_read();
|
||||
bool open_write();
|
||||
|
||||
public:
|
||||
File(const std::string &, FileLocHint);
|
||||
File(const std::string &);
|
||||
~File();
|
||||
const char *get_filepath();
|
||||
const unsigned int get_version();
|
||||
|
|
|
@ -20,7 +20,7 @@ static unsigned int o_pause_count = 0;
|
|||
static bool o_should_pause = false;
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
static std::list<Queue> playqueue_deck;
|
||||
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)
|
||||
{
|
||||
|
|
63
lib/file.cpp
63
lib/file.cpp
|
@ -8,23 +8,16 @@
|
|||
#include <glib.h>
|
||||
|
||||
#ifdef CONFIG_TEST
|
||||
#define OCARINA_DIR "ocarina-test"
|
||||
const std::string OCARINA_DIR = "ocarina-test";
|
||||
#elif CONFIG_DEBUG
|
||||
#define OCARINA_DIR "ocarina-debug"
|
||||
const std::string OCARINA_DIR = "ocarina-debug";
|
||||
#else
|
||||
#define OCARINA_DIR "ocarina"
|
||||
const std::string OCARINA_DIR = "ocarina";
|
||||
#endif
|
||||
|
||||
File :: File(const std::string &path, FileLocHint file_hint)
|
||||
: mode(NOT_OPEN), hint(file_hint), version(FILE_VERSION)
|
||||
File :: File(const std::string &name)
|
||||
: 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()
|
||||
|
@ -32,9 +25,22 @@ File :: ~File()
|
|||
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()
|
||||
{
|
||||
return filepath.c_str();
|
||||
std::string res;
|
||||
|
||||
if (filename != "") {
|
||||
res = find_dir();
|
||||
res += "/" + filename;
|
||||
}
|
||||
return res.c_str();
|
||||
}
|
||||
|
||||
const unsigned int File :: get_version()
|
||||
|
@ -44,25 +50,7 @@ const unsigned int File :: get_version()
|
|||
|
||||
bool File :: exists()
|
||||
{
|
||||
return g_file_test(filepath.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;
|
||||
return g_file_test(filename.c_str(), G_FILE_TEST_EXISTS);
|
||||
}
|
||||
|
||||
bool File :: open_read()
|
||||
|
@ -72,7 +60,7 @@ bool File :: open_read()
|
|||
return false;
|
||||
}
|
||||
|
||||
std::fstream::open(filepath.c_str(), std::fstream::in);
|
||||
std::fstream::open(get_filepath(), std::fstream::in);
|
||||
if (std::fstream::fail()) {
|
||||
dprint("ERROR: File could not be opened for reading\n");
|
||||
return false;
|
||||
|
@ -86,13 +74,12 @@ bool File :: open_read()
|
|||
|
||||
bool File :: open_write()
|
||||
{
|
||||
std::string dir = find_dir();
|
||||
if (g_mkdir_with_parents(dir.c_str(), 0755) != 0) {
|
||||
if (g_mkdir_with_parents(find_dir(), 0755) != 0) {
|
||||
dprint("ERROR: Could not make directory\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::fstream::open(filepath.c_str(), std::fstream::out);
|
||||
std::fstream::open(get_filepath(), std::fstream::out);
|
||||
if (std::fstream::fail()) {
|
||||
dprint("ERROR: Could not open file for writing\n");
|
||||
return false;
|
||||
|
@ -105,8 +92,8 @@ bool File :: open_write()
|
|||
|
||||
bool File :: open(OpenMode m)
|
||||
{
|
||||
if (hint == FILE_TYPE_INVALID) {
|
||||
dprint("ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened\n");
|
||||
if (filename == "") {
|
||||
dprint("ERROR: No filename specified\n");
|
||||
return false;
|
||||
} else if (m == NOT_OPEN) {
|
||||
dprint("ERROR: NOT_OPEN is not a legal OpenMode\n");
|
||||
|
|
|
@ -14,7 +14,8 @@ for arg in sys.argv:
|
|||
|
||||
src = SConscript("src/Sconscript")
|
||||
|
||||
tests = [ "version" ] #, "file", "database", "index", "filter", "idle", "tag_db",
|
||||
tests = [ "version" , "file" ]
|
||||
#, "file", "database", "index", "filter", "idle", "tag_db",
|
||||
# "queue" ]
|
||||
#scripts = [ "playlist", "library", "deck", "audio", "gui" ]
|
||||
|
||||
|
|
|
@ -81,7 +81,7 @@ int main(int argc, char **argv)
|
|||
std::string key = argv[optind];
|
||||
|
||||
|
||||
File f("db_entry.txt", FILE_TYPE_DATA);
|
||||
File f("db_entry.txt");
|
||||
IntEntry ient(i, key);
|
||||
|
||||
switch (action) {
|
||||
|
|
|
@ -18,8 +18,32 @@
|
|||
*/
|
||||
#include <file.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 };
|
||||
|
||||
|
@ -141,4 +165,4 @@ int main(int argc, char **argv)
|
|||
f << data << std::endl;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
|
|
@ -27,7 +27,7 @@ void to_lowercase(const std::string &text)
|
|||
|
||||
void read_file(unsigned int n)
|
||||
{
|
||||
File f("filter.txt", FILE_TYPE_DATA);
|
||||
File f("filter.txt");
|
||||
if (f.open(OPEN_READ)) {
|
||||
for (unsigned int i = 0; i < n; i++) {
|
||||
std::string text = f.getline();
|
||||
|
|
|
@ -29,7 +29,7 @@ void test_results(bool success, unsigned int line)
|
|||
template <class T>
|
||||
void save_tag(const std::string &file, T &tag)
|
||||
{
|
||||
File f(file, FILE_TYPE_DATA);
|
||||
File f(file);
|
||||
f.open(OPEN_WRITE);
|
||||
tag.write(f);
|
||||
f.close();
|
||||
|
@ -38,7 +38,7 @@ void save_tag(const std::string &file, T &tag)
|
|||
template <class T>
|
||||
void load_tag(const std::string &file, T &tag)
|
||||
{
|
||||
File f(file, FILE_TYPE_DATA);
|
||||
File f(file);
|
||||
f.open(OPEN_READ);
|
||||
tag.read(f);
|
||||
f.close();
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <glib.h>
|
||||
|
||||
namespace test
|
||||
{
|
||||
|
@ -36,7 +37,7 @@ namespace test
|
|||
}
|
||||
|
||||
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)
|
||||
std::cout << "Success!" << std::endl;
|
||||
|
@ -48,9 +49,29 @@ namespace test
|
|||
}
|
||||
}
|
||||
|
||||
#define equal(lhs, rhs) \
|
||||
begin(); \
|
||||
test :: assert_equal(lhs, rhs, __LINE__)
|
||||
template <class T>
|
||||
void equal(const T &lhs, const T &rhs, unsigned int line = 0)
|
||||
{
|
||||
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, ...) \
|
||||
|
@ -60,8 +81,7 @@ namespace test
|
|||
test :: end(); \
|
||||
} while (0)
|
||||
|
||||
#define test_equal(lhs, rhs) \
|
||||
do { \
|
||||
test :: begin(); \
|
||||
test :: assert_equal(lhs, rhs, __LINE__); \
|
||||
#define test_equal(lhs, rhs) \
|
||||
do { \
|
||||
test :: equal(lhs, rhs, __LINE__); \
|
||||
} while (0)
|
||||
|
|
Loading…
Reference in New Issue