file: Do not open files with an empty filepath

Instead mark them as invalid to keep from opening a directory instead of
a file.

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-08-10 23:52:52 -04:00 committed by Anna Schumaker
parent fee08b1f94
commit f7a3eec35c
8 changed files with 45 additions and 8 deletions

View File

@ -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.

View File

@ -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.

View File

@ -13,6 +13,7 @@ enum FileLocHint {
FILE_TYPE_CONFIG,
FILE_TYPE_DATA,
FILE_TYPE_LEGACY,
FILE_TYPE_INVALID,
};
enum OpenMode {

View File

@ -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;
}

View File

@ -5,16 +5,17 @@
#include <file.h>
#include <print.h>
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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -17,3 +17,6 @@ Test 4b: Passed
Test 4c: Passed
Test 5: Passed
ERROR: File does not exist (INVALID)
Test 6: Passed