Browse Source

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
jp9000 9 years ago
parent
commit
4f4d7cde7e
4 changed files with 70 additions and 10 deletions
  1. 27 5
      libobs/util/platform-cocoa.m
  2. 14 0
      libobs/util/platform-nix.c
  3. 26 5
      libobs/util/platform-windows.c
  4. 3 0
      libobs/util/platform.h

+ 27 - 5
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;

+ 14 - 0
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)

+ 26 - 5
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;

+ 3 - 0
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);