From eb7e12b829fbc31f5edd9d27656579183190cafd Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 24 May 2019 15:57:43 +0000 Subject: [PATCH] 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. --- src/graphicswin.cpp | 9 +++++++-- src/platform/platform.cpp | 7 +++++++ src/platform/platform.h | 1 + src/solvespace.h | 1 - 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/graphicswin.cpp b/src/graphicswin.cpp index 0571027..9274439 100644 --- a/src/graphicswin.cpp +++ b/src/graphicswin.cpp @@ -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); } } } diff --git a/src/platform/platform.cpp b/src/platform/platform.cpp index 6eec06c..ab08828 100644 --- a/src/platform/platform.cpp +++ b/src/platform/platform.cpp @@ -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"); diff --git a/src/platform/platform.h b/src/platform/platform.h index 06ae19f..011dfcc 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -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); diff --git a/src/solvespace.h b/src/solvespace.h index 9ba97d6..4e18a74 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -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~"