From 7b2404e548f1c3573a0ee88abc7483263ef5138e Mon Sep 17 00:00:00 2001 From: Brian Luft Date: Sat, 11 Nov 2023 23:09:52 +0000 Subject: [PATCH] Remove some std::filesystem usage on macOS --- src/tmbasic/ProgramWindow.cpp | 9 ++++++--- src/vm/filesystem.cpp | 25 +++++++++++++++++++++++++ src/vm/filesystem.h | 2 ++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/tmbasic/ProgramWindow.cpp b/src/tmbasic/ProgramWindow.cpp index 002355ed..c7e06b71 100644 --- a/src/tmbasic/ProgramWindow.cpp +++ b/src/tmbasic/ProgramWindow.cpp @@ -18,6 +18,7 @@ #include "../util/tvutil.h" #include "../vm/Interpreter.h" #include "../vm/Program.h" +#include "../vm/filesystem.h" #include "CodeEditorWindow.h" #include "DesignerWindow.h" #include "GridLayout.h" @@ -38,6 +39,8 @@ using util::Label; using util::ListViewer; using util::ViewPtr; using util::WindowPtr; +using vm::deleteFile; +using vm::getTempFilePath; using vm::Program; namespace tmbasic { @@ -562,7 +565,7 @@ void ProgramWindow::run() { auto pcode = program.vmProgram.serialize(); auto nativePlatform = TargetPlatform::kLinuxArm64; // TODO auto exeData = compiler::makeExeFile(pcode, nativePlatform); - auto tempFilePath = std::filesystem::temp_directory_path() / getTempExeFilename(nativePlatform); + auto tempFilePath = getTempFilePath(getTempExeFilename(nativePlatform)); std::ofstream f{ tempFilePath, std::ios::out | std::ios::binary }; f.write(reinterpret_cast(exeData.data()), exeData.size()); f.close(); @@ -572,11 +575,11 @@ void ProgramWindow::run() { // Execute it TProgram::application->suspend(); - auto args = fmt::format("\"{}\"", tempFilePath.string()); + auto args = fmt::format("\"{}\"", tempFilePath); std::system(args.c_str()); // Delete the temp file - std::filesystem::remove(tempFilePath); + deleteFile(tempFilePath); std::cout << "Press Enter to return to TMBASIC." << std::endl; std::cin.get(); diff --git a/src/vm/filesystem.cpp b/src/vm/filesystem.cpp index 452899a1..72d7fc57 100644 --- a/src/vm/filesystem.cpp +++ b/src/vm/filesystem.cpp @@ -168,4 +168,29 @@ void deleteDirectory(const std::string& path, bool recursive) { } } +std::string getTempFilePath(const std::string& filename) { +#ifdef __APPLE__ + const char* temp_path = getenv("TMPDIR"); + std::ostringstream ss{}; + if (temp_path != nullptr) { + s << temp_path << "/" << filename; + return ss.str(); + } else { + // Default to "/tmp" if TMPDIR is not set + s << "/tmp/" << filename; + return ss.str(); + } +#else + return (std::filesystem::temp_directory_path() / filename).str(); +#endif +} + +void deleteFile(const std::string& path) { +#ifdef __APPLE__ + unlink(path.c_str()); +#else + std::filesystem::remove(path); +#endif +} + } // namespace vm diff --git a/src/vm/filesystem.h b/src/vm/filesystem.h index fb170d69..77836d4d 100644 --- a/src/vm/filesystem.h +++ b/src/vm/filesystem.h @@ -8,5 +8,7 @@ std::vector listFiles(const std::string& path); std::vector listDirectories(const std::string& path); void createDirectory(const std::string& path); void deleteDirectory(const std::string& path, bool recursive); +std::string getTempFilePath(const std::string& filename); +void deleteFile(const std::string& path); } // namespace vm