Skip to content

Commit

Permalink
Make sure file from a recent menu exists before using it.
Browse files Browse the repository at this point in the history
Otherwise it can result in a very confusing error that does not
suggest at all that the file is missing.
  • Loading branch information
whitequark committed May 24, 2019
1 parent beea444 commit eb7e12b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/graphicswin.cpp
Expand Up @@ -345,8 +345,13 @@ static void PopulateMenuWithPathnames(Platform::MenuRef menu,
menuItem->SetEnabled(false);
} else {
for(Platform::Path pathname : pathnames) {
Platform::MenuItemRef menuItem = menu->AddItem(pathname.raw,
[=]() { onTrigger(pathname); }, /*mnemonics=*/false);
Platform::MenuItemRef menuItem = menu->AddItem(pathname.raw, [=]() {
if(FileExists(pathname)) {
onTrigger(pathname);
} else {
Error(_("File '%s' does not exist."), pathname.raw.c_str());
}
}, /*mnemonics=*/false);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/platform/platform.cpp
Expand Up @@ -405,6 +405,13 @@ FILE *OpenFile(const Platform::Path &filename, const char *mode) {
#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) {
ssassert(filename.raw.length() == strlen(filename.raw.c_str()),
"Unexpected null byte in middle of a path");
Expand Down
1 change: 1 addition & 0 deletions src/platform/platform.h
Expand Up @@ -55,6 +55,7 @@ struct PathLess {
};

// File manipulation functions.
bool FileExists(const Platform::Path &filename);
FILE *OpenFile(const Platform::Path &filename, const char *mode);
bool ReadFile(const Platform::Path &filename, std::string *data);
bool WriteFile(const Platform::Path &filename, const std::string &data);
Expand Down
1 change: 0 additions & 1 deletion src/solvespace.h
Expand Up @@ -138,7 +138,6 @@ enum class Command : uint32_t;
#include "platform/gui.h"

const size_t MAX_RECENT = 8;
extern Platform::Path RecentFile[MAX_RECENT];

#define AUTOSAVE_EXT "slvs~"

Expand Down

0 comments on commit eb7e12b

Please sign in to comment.