diff --git a/design.txt b/design.txt index 4d2d29af..418448b5 100644 --- a/design.txt +++ b/design.txt @@ -132,6 +132,7 @@ On-disk files: (lib/file.cpp) FILE_TYPE_CONFIG, FILE_TYPE_DATA, FILE_TYPE_LEGACY, + FILE_TYPE_INVALID, } - Open mode: @@ -173,6 +174,9 @@ On-disk files: (lib/file.cpp) $HOME/.ocarina/ $HOME/.ocarina-debug/ + If filepath is an empty string, set the file hint to + FILE_TYPE_INVALID and do not set the filepath field. + File : ~File() Close the file stream if it is open. @@ -199,6 +203,7 @@ On-disk files: (lib/file.cpp) - Write version information to the start of the file - Return true + Return false if hint == FILE_TYPE_INVALID. Return false if the file is already open. Return false if there are any other errors. diff --git a/design/file.txt b/design/file.txt index ceb5b93e..233e4b02 100644 --- a/design/file.txt +++ b/design/file.txt @@ -36,6 +36,7 @@ On-disk files: (lib/file.cpp) FILE_TYPE_CONFIG, FILE_TYPE_DATA, FILE_TYPE_LEGACY, + FILE_TYPE_INVALID, } - Open mode: @@ -77,6 +78,9 @@ On-disk files: (lib/file.cpp) $HOME/.ocarina/ $HOME/.ocarina-debug/ + If filepath is an empty string, set the file hint to + FILE_TYPE_INVALID and do not set the filepath field. + File : ~File() Close the file stream if it is open. @@ -103,6 +107,7 @@ On-disk files: (lib/file.cpp) - Write version information to the start of the file - Return true + Return false if hint == FILE_TYPE_INVALID. Return false if the file is already open. Return false if there are any other errors. diff --git a/include/file.h b/include/file.h index 1e02476c..64cf9542 100644 --- a/include/file.h +++ b/include/file.h @@ -13,6 +13,7 @@ enum FileLocHint { FILE_TYPE_CONFIG, FILE_TYPE_DATA, FILE_TYPE_LEGACY, + FILE_TYPE_INVALID, }; enum OpenMode { diff --git a/lib/file.cpp b/lib/file.cpp index 29a94d60..d84e0054 100644 --- a/lib/file.cpp +++ b/lib/file.cpp @@ -16,6 +16,9 @@ void File :: find_dir(std::string &res) { + if (hint == FILE_TYPE_INVALID) + return; + switch (hint) { case FILE_TYPE_CONFIG: res = g_get_user_config_dir(); @@ -23,8 +26,9 @@ void File :: find_dir(std::string &res) case FILE_TYPE_DATA: res = g_get_user_data_dir(); break; - case FILE_TYPE_LEGACY: + default: /* FILE_TYPE_LEGACY */ res = g_get_home_dir(); + break; } res += "/"; @@ -33,7 +37,7 @@ void File :: find_dir(std::string &res) case FILE_TYPE_DATA: res += OCARINA_DIR; break; - case FILE_TYPE_LEGACY: + default: /* FILE_TYPE_LEGACY */ res += "."; res += OCARINA_DIR; } @@ -44,6 +48,12 @@ File :: File(std::string path, FileLocHint file_hint) { std::string dir; + if (path == "") { + hint = FILE_TYPE_INVALID; + filepath = "INVALID"; + return; + } + find_dir(dir); filepath = dir + "/" + path; } diff --git a/tests/file/compile.cpp b/tests/file/compile.cpp index 6e110eae..921d4a73 100644 --- a/tests/file/compile.cpp +++ b/tests/file/compile.cpp @@ -5,16 +5,17 @@ #include #include -void test_filepath(FileLocHint hint) +void test_filepath(std::string file, FileLocHint hint) { - File file("test.file", hint); - print("File path is: %s\n", file.get_filepath()); + File f(file, hint); + print("File path is: %s\n", f.get_filepath()); } int main(int argc, char **argv) { - test_filepath(FILE_TYPE_CONFIG); - test_filepath(FILE_TYPE_DATA); - test_filepath(FILE_TYPE_LEGACY); + test_filepath("test.file", FILE_TYPE_CONFIG); + test_filepath("test.file", FILE_TYPE_DATA); + test_filepath("test.file", FILE_TYPE_LEGACY); + test_filepath("", FILE_TYPE_DATA); return 0; } diff --git a/tests/file/compile.good b/tests/file/compile.good index c8893370..70993883 100644 --- a/tests/file/compile.good +++ b/tests/file/compile.good @@ -1,3 +1,4 @@ File path is: /home/anna/.config/ocarina-test/test.file File path is: /home/anna/.local/share/ocarina-test/test.file File path is: /home/anna/.ocarina-test/test.file +File path is: INVALID diff --git a/tests/file/open.cpp b/tests/file/open.cpp index 7426e0f6..d70228bc 100644 --- a/tests/file/open.cpp +++ b/tests/file/open.cpp @@ -91,6 +91,16 @@ int test_5() return test_result("5", file.close(), true); } +/* + * Attempt to open an invalid file + */ +int test_6() +{ + File file("", FILE_TYPE_DATA); + print("\n"); + return test_result("6", file.open(OPEN_READ), false); +} + int main(int argc, char **argv) { int failed = 0; @@ -102,6 +112,7 @@ int main(int argc, char **argv) failed += test_3(); failed += test_4(); failed += test_5(); + failed += test_6(); return failed; } diff --git a/tests/file/open.good b/tests/file/open.good index 664bd6db..dfbe73c8 100644 --- a/tests/file/open.good +++ b/tests/file/open.good @@ -17,3 +17,6 @@ Test 4b: Passed Test 4c: Passed Test 5: Passed + +ERROR: File does not exist (INVALID) +Test 6: Passed