From 4f4d7cde7e8a52cacb43b89d973ac3b188b6c3b1 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Wed, 6 Jul 2016 01:29:05 -0700 Subject: [PATCH] libobs/util: Add os_get_program_data_path* functions Allows getting the system-local program data path. Typically: Windows: C:\ProgramData Mac: /Library/Application Support Linux: /usr/local/share --- libobs/util/platform-cocoa.m | 32 +++++++++++++++++++++++++++----- libobs/util/platform-nix.c | 14 ++++++++++++++ libobs/util/platform-windows.c | 31 ++++++++++++++++++++++++++----- libobs/util/platform.h | 3 +++ 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/libobs/util/platform-cocoa.m b/libobs/util/platform-cocoa.m index 7d8b1054d..29076d6aa 100644 --- a/libobs/util/platform-cocoa.m +++ b/libobs/util/platform-cocoa.m @@ -69,11 +69,12 @@ uint64_t os_gettime_ns(void) return f(); } -/* gets the location ~/Library/Application Support/[name] */ -int os_get_config_path(char *dst, size_t size, const char *name) +/* gets the location [domain mask]/Library/Application Support/[name] */ +static int os_get_path_internal(char *dst, size_t size, const char *name, + NSSearchPathDomainMask domainMask) { NSArray *paths = NSSearchPathForDirectoriesInDomains( - NSApplicationSupportDirectory, NSUserDomainMask, YES); + NSApplicationSupportDirectory, domainMask, YES); if([paths count] == 0) bcrash("Could not get home directory (platform-cocoa)"); @@ -87,10 +88,11 @@ int os_get_config_path(char *dst, size_t size, const char *name) return snprintf(dst, size, "%s/%s", base_path, name); } -char *os_get_config_path_ptr(const char *name) +static char *os_get_path_ptr_internal(const char *name, + NSSearchPathDomainMask domainMask) { NSArray *paths = NSSearchPathForDirectoriesInDomains( - NSApplicationSupportDirectory, NSUserDomainMask, YES); + NSApplicationSupportDirectory, domainMask, YES); if([paths count] == 0) bcrash("Could not get home directory (platform-cocoa)"); @@ -113,6 +115,26 @@ char *os_get_config_path_ptr(const char *name) return path.array; } +int os_get_config_path(char *dst, size_t size, const char *name) +{ + return os_get_path_internal(dst, size, name, NSUserDomainMask); +} + +char *os_get_config_path_ptr(const char *name) +{ + return os_get_path_ptr_internal(name, NSUserDomainMask); +} + +int os_get_program_data_path(char *dst, size_t size, const char *name) +{ + return os_get_path_internal(dst, size, name, NSLocalDomainMask); +} + +char *os_get_program_data_path_ptr(const char *name) +{ + return os_get_path_ptr_internal(name, NSLocalDomainMask); +} + struct os_cpu_usage_info { int64_t last_cpu_time; int64_t last_sys_time; diff --git a/libobs/util/platform-nix.c b/libobs/util/platform-nix.c index 3cded14d3..6c7b4ba60 100644 --- a/libobs/util/platform-nix.c +++ b/libobs/util/platform-nix.c @@ -241,6 +241,20 @@ char *os_get_config_path_ptr(const char *name) #endif } +int os_get_program_data_path(char *dst, size_t size, const char *name) +{ + return snprintf(dst, size, "/usr/local/share/%s", !!name ? name : ""); +} + +char *os_get_program_data_path_ptr(const char *name) +{ + size_t len = snprintf(NULL, 0, "/usr/local/share/%s", !!name ? name : ""); + char *str = bmalloc(len + 1); + snprintf(str, len + 1, "/usr/local/share/%s", !!name ? name : ""); + str[len] = 0; + return str; +} + #endif bool os_file_exists(const char *path) diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 119426c77..2e33cfaf1 100644 --- a/libobs/util/platform-windows.c +++ b/libobs/util/platform-windows.c @@ -208,12 +208,13 @@ uint64_t os_gettime_ns(void) return (uint64_t)time_val; } -/* returns %appdata%\[name] on windows */ -int os_get_config_path(char *dst, size_t size, const char *name) +/* returns [folder]\[name] on windows */ +static int os_get_path_internal(char *dst, size_t size, const char *name, + int folder) { wchar_t path_utf16[MAX_PATH]; - SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, + SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path_utf16); if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) { @@ -231,13 +232,13 @@ int os_get_config_path(char *dst, size_t size, const char *name) return -1; } -char *os_get_config_path_ptr(const char *name) +static char *os_get_path_ptr_internal(const char *name, int folder) { char *ptr; wchar_t path_utf16[MAX_PATH]; struct dstr path; - SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, + SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path_utf16); os_wcs_to_utf8_ptr(path_utf16, 0, &ptr); @@ -247,6 +248,26 @@ char *os_get_config_path_ptr(const char *name) return path.array; } +int os_get_config_path(char *dst, size_t size, const char *name) +{ + return os_get_path_internal(dst, size, name, CSIDL_APPDATA); +} + +char *os_get_config_path_ptr(const char *name) +{ + return os_get_path_ptr_internal(name, CSIDL_APPDATA); +} + +int os_get_program_data_path(char *dst, size_t size, const char *name) +{ + return os_get_path_internal(dst, size, name, CSIDL_COMMON_APPDATA); +} + +char *os_get_program_data_path_ptr(const char *name) +{ + return os_get_path_ptr_internal(name, CSIDL_COMMON_APPDATA); +} + bool os_file_exists(const char *path) { WIN32_FIND_DATAW wfd; diff --git a/libobs/util/platform.h b/libobs/util/platform.h index 78fc40df3..b08c40a47 100644 --- a/libobs/util/platform.h +++ b/libobs/util/platform.h @@ -108,6 +108,9 @@ EXPORT uint64_t os_gettime_ns(void); EXPORT int os_get_config_path(char *dst, size_t size, const char *name); EXPORT char *os_get_config_path_ptr(const char *name); +EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name); +EXPORT char *os_get_program_data_path_ptr(const char *name); + EXPORT bool os_file_exists(const char *path); EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);