Make sure file from a recent menu exists before using it.

Otherwise it can result in a very confusing error that does not
suggest at all that the file is missing.
This commit is contained in:
whitequark 2019-05-24 15:57:43 +00:00
parent beea4444ab
commit eb7e12b829
4 changed files with 15 additions and 3 deletions

View File

@ -345,8 +345,13 @@ static void PopulateMenuWithPathnames(Platform::MenuRef menu,
menuItem->SetEnabled(false); menuItem->SetEnabled(false);
} else { } else {
for(Platform::Path pathname : pathnames) { for(Platform::Path pathname : pathnames) {
Platform::MenuItemRef menuItem = menu->AddItem(pathname.raw, Platform::MenuItemRef menuItem = menu->AddItem(pathname.raw, [=]() {
[=]() { onTrigger(pathname); }, /*mnemonics=*/false); if(FileExists(pathname)) {
onTrigger(pathname);
} else {
Error(_("File '%s' does not exist."), pathname.raw.c_str());
}
}, /*mnemonics=*/false);
} }
} }
} }

View File

@ -405,6 +405,13 @@ FILE *OpenFile(const Platform::Path &filename, const char *mode) {
#endif #endif
} }
bool FileExists(const Platform::Path &filename) {
FILE *f = OpenFile(filename, "rb");
if(f == NULL) return false;
fclose(f);
return true;
}
void RemoveFile(const Platform::Path &filename) { void RemoveFile(const Platform::Path &filename) {
ssassert(filename.raw.length() == strlen(filename.raw.c_str()), ssassert(filename.raw.length() == strlen(filename.raw.c_str()),
"Unexpected null byte in middle of a path"); "Unexpected null byte in middle of a path");

View File

@ -55,6 +55,7 @@ struct PathLess {
}; };
// File manipulation functions. // File manipulation functions.
bool FileExists(const Platform::Path &filename);
FILE *OpenFile(const Platform::Path &filename, const char *mode); FILE *OpenFile(const Platform::Path &filename, const char *mode);
bool ReadFile(const Platform::Path &filename, std::string *data); bool ReadFile(const Platform::Path &filename, std::string *data);
bool WriteFile(const Platform::Path &filename, const std::string &data); bool WriteFile(const Platform::Path &filename, const std::string &data);

View File

@ -138,7 +138,6 @@ enum class Command : uint32_t;
#include "platform/gui.h" #include "platform/gui.h"
const size_t MAX_RECENT = 8; const size_t MAX_RECENT = 8;
extern Platform::Path RecentFile[MAX_RECENT];
#define AUTOSAVE_EXT "slvs~" #define AUTOSAVE_EXT "slvs~"