From 2c45abbc3bfd7c4963db148b489d9f3468fc801b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 16 Sep 2023 12:46:08 +0300 Subject: [PATCH] Add a function to retrieve temporary directory location MacOS and Android versions are untested. --- source/fs/android/dir_location.cpp | 6 ++++++ source/fs/dir.h | 3 +++ source/fs/osx/cfdir.m | 9 +++++++++ source/fs/osx/dir_location.cpp | 15 +++++++++++++++ source/fs/unix/dir_location.cpp | 8 ++++++++ source/fs/windows/dir_location.cpp | 8 ++++++++ 6 files changed, 49 insertions(+) diff --git a/source/fs/android/dir_location.cpp b/source/fs/android/dir_location.cpp index 155d147..24066f9 100644 --- a/source/fs/android/dir_location.cpp +++ b/source/fs/android/dir_location.cpp @@ -18,5 +18,11 @@ Path get_user_data_dir() return thread->get_internal_data_path(); } +Path get_temp_dir() +{ + // TODO Should use Context.getCacheDir(), but it's a Java function. + return get_home_dir(); +} + } // namespace FS } // namespace Msp diff --git a/source/fs/dir.h b/source/fs/dir.h index 960be2e..e9cccfc 100644 --- a/source/fs/dir.h +++ b/source/fs/dir.h @@ -56,6 +56,9 @@ MSPCORE_API Path get_sys_data_dir(); /// Returns a directory containing system-wide architecture-specific files. MSPCORE_API Path get_sys_lib_dir(); +/// Returns a directory suitable for storing temporary files. +MSPCORE_API Path get_temp_dir(); + /** Looks for a file in a list of paths. Returns the absolute path to the first existing location, or an empty Path if the file is not found at all. */ MSPCORE_API Path path_lookup(const std::string &, const std::vector &); diff --git a/source/fs/osx/cfdir.m b/source/fs/osx/cfdir.m index 4dd4b4b..b21a0ac 100644 --- a/source/fs/osx/cfdir.m +++ b/source/fs/osx/cfdir.m @@ -21,3 +21,12 @@ unsigned get_application_support_dir(char *buf, unsigned size) return [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; } + +unsigned get_temp_dir(char *buf, unsigned size) +{ + NSString *path = NSTemporaryDirectory(); + if(![path getCString:buf maxLength:size encoding:NSUTF8StringEncoding]) + return 0; + + return [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; +} diff --git a/source/fs/osx/dir_location.cpp b/source/fs/osx/dir_location.cpp index 13aa3d2..51ddd4f 100644 --- a/source/fs/osx/dir_location.cpp +++ b/source/fs/osx/dir_location.cpp @@ -7,6 +7,7 @@ using namespace std; extern "C" unsigned get_home_dir(char *, unsigned); extern "C" unsigned get_application_support_dir(char *, unsigned); +extern "C" unsigned get_temp_dir(char *, unsigned); namespace Msp { namespace FS { @@ -38,5 +39,19 @@ Path get_user_data_dir() return get_home_dir()/"Library"/"Application Support"/appname; } +Path get_temp_dir() +{ + string tmp = getenv("TMPDIR"); + if(!tmp.empty()) + return tmp; + + char buf[1024]; + unsigned len = ::get_temp_dir(buf, sizeof(buf)); + if(len) + return string(buf, len); + + return "/tmp"; +} + } // namespace FS } // namespace Msp diff --git a/source/fs/unix/dir_location.cpp b/source/fs/unix/dir_location.cpp index 68d82a2..b66c788 100644 --- a/source/fs/unix/dir_location.cpp +++ b/source/fs/unix/dir_location.cpp @@ -25,5 +25,13 @@ Path get_user_data_dir() return get_home_dir()/("."+appname); } +Path get_temp_dir() +{ + string tmp = getenv("TMPDIR"); + if(!tmp.empty()) + return tmp; + return "/tmp"; +} + } // namespace FS } // namespace Msp diff --git a/source/fs/windows/dir_location.cpp b/source/fs/windows/dir_location.cpp index 85c5c2f..5f47a7c 100644 --- a/source/fs/windows/dir_location.cpp +++ b/source/fs/windows/dir_location.cpp @@ -28,5 +28,13 @@ Path get_user_data_dir() return "."; } +Path get_temp_dir() +{ + char temp[MAX_PATH+1]; + if(GetTempPath(sizeof(temp), temp)) + return temp; + return "."; +} + } // namespace FS } // namespace Msp -- 2.45.2