From a0093df2f962b34289c545d85a2baad036b1bcc5 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 17 Mar 2019 12:41:15 +0100 Subject: A more general fix for resource loading when pointing to a directory. --- plugingui/resource.cc | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'plugingui') diff --git a/plugingui/resource.cc b/plugingui/resource.cc index 432f25d..b70f84a 100644 --- a/plugingui/resource.cc +++ b/plugingui/resource.cc @@ -30,12 +30,37 @@ #include #include +#include + +#if DG_PLATFORM != DG_PLATFORM_WINDOWS +#include +#include +#include +#endif + // rcgen generated file containing rc_data declaration. #include "resource_data.h" namespace GUI { +// TODO: Replace with std::filesystem::is_regular_file once we update the +// compiler to require C++17 +static bool pathIsFile(const std::string& path) +{ +#if DG_PLATFORM == DG_PLATFORM_WINDOWS + return (GetFileAttributesA(path.data()) & FILE_ATTRIBUTE_DIRECTORY) == 0; +#else + struct stat s; + if(stat(path.data(), &s) != 0) + { + return false; // error + } + + return (s.st_mode & S_IFREG) != 0; // s.st_mode & S_IFDIR => dir +#endif +} + // Internal resources start with a colon. static bool nameIsInternal(const std::string& name) { @@ -74,8 +99,13 @@ Resource::Resource(const std::string& name) } else { + if(!pathIsFile(name)) + { + return; + } + // Read from file: - std::FILE *fp = std::fopen(name.c_str(), "rb"); + std::FILE *fp = std::fopen(name.data(), "rb"); if(!fp) { return; -- cgit v1.2.3