Skip to content

Commit

Permalink
Remove some std::filesystem usage on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Nov 11, 2023
1 parent 60e1e37 commit 7b2404e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/tmbasic/ProgramWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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<const char*>(exeData.data()), exeData.size());
f.close();
Expand All @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions src/vm/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/vm/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ std::vector<std::string> listFiles(const std::string& path);
std::vector<std::string> 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

0 comments on commit 7b2404e

Please sign in to comment.