From 55f3f06ded39e2422a0e61c04f75b2cc27168529 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 26 Apr 2014 13:31:22 -0400 Subject: [PATCH] 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 --- .gitignore | 1 + DESIGN | 37 +++++++++++-------------- include/database.hpp | 2 +- include/file.h | 12 ++------ lib/audio.cpp | 2 +- lib/deck.cpp | 2 +- lib/file.cpp | 63 +++++++++++++++++------------------------- tests/Sconscript | 3 +- tests/src/db_entry.cpp | 2 +- tests/src/file.cpp | 28 +++++++++++++++++-- tests/src/filter.cpp | 2 +- tests/src/tags.cpp | 4 +-- tests/src/test.h | 36 ++++++++++++++++++------ 13 files changed, 108 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index e42436a8..93b1a6b2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ share/ocarina/#* bin/ .sconsign.dblite +*.patch diff --git a/DESIGN b/DESIGN index ad1e9f09..e329d720 100644 --- a/DESIGN +++ b/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 << ; - 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 diff --git a/include/database.hpp b/include/database.hpp index 348013f4..40ab7e5d 100644 --- a/include/database.hpp +++ b/include/database.hpp @@ -11,7 +11,7 @@ template Database :: Database(std::string filepath, bool autosave) - : _size(0), _autosave(autosave), _file(filepath, FILE_TYPE_DATA) + : _size(0), _autosave(autosave), _file(filepath) { } diff --git a/include/file.h b/include/file.h index a88db254..a4073b9f 100644 --- a/include/file.h +++ b/include/file.h @@ -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(); diff --git a/lib/audio.cpp b/lib/audio.cpp index 2a9f316f..051f49d7 100644 --- a/lib/audio.cpp +++ b/lib/audio.cpp @@ -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) { diff --git a/lib/deck.cpp b/lib/deck.cpp index 3631be74..0d3d31af 100644 --- a/lib/deck.cpp +++ b/lib/deck.cpp @@ -11,7 +11,7 @@ static std::list 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) { diff --git a/lib/file.cpp b/lib/file.cpp index 025bdbdc..be560e3b 100644 --- a/lib/file.cpp +++ b/lib/file.cpp @@ -8,23 +8,16 @@ #include #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"); diff --git a/tests/Sconscript b/tests/Sconscript index 3b8e361b..d05abdba 100644 --- a/tests/Sconscript +++ b/tests/Sconscript @@ -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" ] diff --git a/tests/src/db_entry.cpp b/tests/src/db_entry.cpp index 213f712d..311bb483 100644 --- a/tests/src/db_entry.cpp +++ b/tests/src/db_entry.cpp @@ -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) { diff --git a/tests/src/file.cpp b/tests/src/file.cpp index 64a11dcd..8462d2a7 100644 --- a/tests/src/file.cpp +++ b/tests/src/file.cpp @@ -18,8 +18,32 @@ */ #include #include +#include "test.h" -#include + +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 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; } -} +}*/ diff --git a/tests/src/filter.cpp b/tests/src/filter.cpp index a2dfb97d..4233edbf 100644 --- a/tests/src/filter.cpp +++ b/tests/src/filter.cpp @@ -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(); diff --git a/tests/src/tags.cpp b/tests/src/tags.cpp index d42e837e..5d89bfe6 100644 --- a/tests/src/tags.cpp +++ b/tests/src/tags.cpp @@ -29,7 +29,7 @@ void test_results(bool success, unsigned int line) template 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 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(); diff --git a/tests/src/test.h b/tests/src/test.h index af4dfe46..857c6c9e 100644 --- a/tests/src/test.h +++ b/tests/src/test.h @@ -4,6 +4,7 @@ #include #include +#include namespace test { @@ -36,7 +37,7 @@ namespace test } template - 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 + 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)