From 6847eb3a8706f11d9bd9651e1e43b47db3da4f1c Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 17 Apr 2020 18:48:55 -0700 Subject: [PATCH] Add FILESYSTEM_openDirectory() and _openDirectoryEnabled() Have to add some includes and put these behind some ifdefs, of course. I'm pretty sure FreeBSD and OpenBSD and Haiku are POSIX enough that the "open" command will work on them, too. I would've loved to make FILESYSTEM_openDirectoryEnabled a simple bool instead of a function, but I ran into issues with putting it in the FileSystemUtils header file, so I'll just make it a function and call it a day. --- desktop_version/src/FileSystemUtils.cpp | 50 +++++++++++++++++++++++++ desktop_version/src/FileSystemUtils.h | 3 ++ 2 files changed, 53 insertions(+) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 2569630f..6cdfeb9d 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -19,6 +19,7 @@ #if defined(_WIN32) #include #include +#include int mkdir(char* path, int mode) { WCHAR utf16_path[MAX_PATH]; @@ -33,6 +34,9 @@ int mkdir(char* path, int mode) /* These are needed for PLATFORM_* crap */ #include #include +#include +#include +#include #define MAX_PATH PATH_MAX #endif @@ -465,3 +469,49 @@ void PLATFORM_copyFile(const char *oldLocation, const char *newLocation) /* WTF did we just do */ printf("Copied:\n\tOld: %s\n\tNew: %s\n", oldLocation, newLocation); } + +#ifdef _WIN32 +bool FILESYSTEM_openDirectoryEnabled() +{ + return true; +} + +bool FILESYSTEM_openDirectory(const char *dname) +{ + ShellExecute(NULL, "open", dname, NULL, NULL, SW_SHOWMINIMIZED); + return true; +} +#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__) +bool FILESYSTEM_openDirectoryEnabled() +{ + return true; +} + #ifdef __linux__ +const char* open_cmd = "xdg-open"; + #else +const char* open_cmd = "open"; + #endif + +extern "C" char** environ; + +bool FILESYSTEM_openDirectory(const char *dname) +{ + pid_t child; + // This const_cast is legal (ctrl-f "The statement" at https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html + char* argv[3] = {const_cast(open_cmd), const_cast(dname), NULL}; + posix_spawnp(&child, open_cmd, NULL, NULL, argv, environ); + int status = 0; + waitpid(child, &status, 0); + return WIFEXITED(status) && WEXITSTATUS(status) == 0; +} +#else +bool FILESYSTEM_openDirectoryEnabled() +{ + return false; +} + +bool FILESYSTEM_openDirectory(const char *dname) +{ + return false; +} +#endif diff --git a/desktop_version/src/FileSystemUtils.h b/desktop_version/src/FileSystemUtils.h index 88f2b2d4..ddf44d0d 100644 --- a/desktop_version/src/FileSystemUtils.h +++ b/desktop_version/src/FileSystemUtils.h @@ -20,4 +20,7 @@ bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc); std::vector FILESYSTEM_getLevelDirFileNames(); +bool FILESYSTEM_openDirectoryEnabled(); +bool FILESYSTEM_openDirectory(const char *dname); + #endif /* FILESYSTEMUTILS_H */