[Bf-blender-cvs] [9df13fba69c] master: macOS: add tilde-based path expander

Ankit Meel noreply at git.blender.org
Sat Dec 11 18:00:05 CET 2021


Commit: 9df13fba69c3b0e0e013db198515e6e1a7238f6a
Author: Ankit Meel
Date:   Sat Dec 11 22:29:53 2021 +0530
Branches: master
https://developer.blender.org/rB9df13fba69c3b0e0e013db198515e6e1a7238f6a

macOS: add tilde-based path expander

Replaces `HOME` environment variable usage for user
directories like in D12802.

Reviewed By: #platform_macos, brecht
Differential Revision: https://developer.blender.org/D13212

===================================================================

M	source/blender/blenkernel/intern/appdir.c
M	source/blender/blenlib/BLI_fileops.h
M	source/blender/blenlib/intern/storage_apple.mm

===================================================================

diff --git a/source/blender/blenkernel/intern/appdir.c b/source/blender/blenkernel/intern/appdir.c
index a819459f989..9dd4c7e503a 100644
--- a/source/blender/blenkernel/intern/appdir.c
+++ b/source/blender/blenkernel/intern/appdir.c
@@ -175,10 +175,12 @@ const char *BKE_appdir_folder_default_or_root(void)
 
 const char *BKE_appdir_folder_home(void)
 {
-#ifndef WIN32
-  return BLI_getenv("HOME");
-#else /* Windows */
+#ifdef WIN32
   return BLI_getenv("userprofile");
+#elif defined(__APPLE__)
+  return BLI_expand_tilde("~/");
+#else
+  return BLI_getenv("HOME");
 #endif
 }
 
@@ -248,10 +250,8 @@ bool BKE_appdir_font_folder_default(char *dir)
     BLI_strncpy_wchar_as_utf8(test_dir, wpath, sizeof(test_dir));
   }
 #elif defined(__APPLE__)
-  const char *home = BLI_getenv("HOME");
-  if (home) {
-    BLI_path_join(test_dir, sizeof(test_dir), home, "Library", "Fonts", NULL);
-  }
+  STRNCPY(test_dir, BLI_expand_tilde("~/Library/Fonts/"));
+  BLI_path_slash_ensure(test_dir);
 #else
   STRNCPY(test_dir, "/usr/share/fonts");
 #endif
diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index 932b67866ea..2ef9b8f6c36 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -322,6 +322,14 @@ void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t
  */
 void BLI_file_free_lines(struct LinkNode *lines);
 
+#ifdef __APPLE__
+/**
+ * Expand the leading `~` in the given path to `/Users/$USER`.
+ * This doesn't preserve the trailing path separator.
+ * Giving a path without leading `~` is not an error.
+ */
+const char *BLI_expand_tilde(const char *path_with_tilde);
+#endif
 /* This weirdo pops up in two places. */
 #if !defined(WIN32)
 #  ifndef O_BINARY
diff --git a/source/blender/blenlib/intern/storage_apple.mm b/source/blender/blenlib/intern/storage_apple.mm
index 07091f2b055..b0234b9a093 100644
--- a/source/blender/blenlib/intern/storage_apple.mm
+++ b/source/blender/blenlib/intern/storage_apple.mm
@@ -184,3 +184,20 @@ eFileAttributes BLI_file_attributes(const char *path)
 
   return (eFileAttributes)ret;
 }
+
+const char *BLI_expand_tilde(const char *path_with_tilde)
+{
+  static char path_expanded[FILE_MAX];
+  @autoreleasepool {
+    const NSString *const str_with_tilde = [[NSString alloc] initWithCString:path_with_tilde
+                                                                    encoding:NSUTF8StringEncoding];
+    if (!str_with_tilde) {
+      return nullptr;
+    }
+    const NSString *const str_expanded = [str_with_tilde stringByExpandingTildeInPath];
+    [str_expanded getCString:path_expanded
+                   maxLength:sizeof(path_expanded)
+                    encoding:NSUTF8StringEncoding];
+  }
+  return path_expanded;
+}



More information about the Bf-blender-cvs mailing list