فهرست منبع

change os_get_home_path to a better and more clear function, os_get_config_path

jp9000 11 سال پیش
والد
کامیت
bb53a39aee
5فایلهای تغییر یافته به همراه32 افزوده شده و 40 حذف شده
  1. 10 5
      libobs/util/platform-cocoa.m
  2. 7 6
      libobs/util/platform-nix.c
  3. 9 5
      libobs/util/platform-windows.c
  4. 1 1
      libobs/util/platform.h
  5. 5 23
      obs/obs-app.cpp

+ 10 - 5
libobs/util/platform-cocoa.m

@@ -97,7 +97,8 @@ uint64_t os_gettime_ns(void)
 	return *(uint64_t*) &nano;
 }
 
-char *os_get_home_path(void)
+/* gets the location ~/Library/Application Support/[name] */
+char *os_get_config_path(const char *name)
 {
 	NSArray *paths = NSSearchPathForDirectoriesInDomains(
 			NSApplicationSupportDirectory, NSUserDomainMask, YES);
@@ -110,13 +111,17 @@ char *os_get_home_path(void)
 	NSUInteger len = [application_support
 		lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
 
-	char *path = bmalloc(len+1);
+	char *path_ptr = bmalloc(len+1);
 
-	path[len] = 0;
+	path_ptr[len] = 0;
 
-	memcpy(path, [application_support UTF8String], len);
+	memcpy(path_ptr, [application_support UTF8String], len);
 
-	return path;
+	struct dstr path;
+	dstr_init_move_array(&path, path_ptr);
+	dstr_cat(&path, "/");
+	dstr_cat(&path, name);
+	return path.array;
 }
 
 bool os_file_exists(const char *path)

+ 7 - 6
libobs/util/platform-nix.c

@@ -89,17 +89,18 @@ uint64_t os_gettime_ns(void)
 	return tp.tv_nsec;
 }
 
-/* should return $HOME/ */
-char *os_get_home_path(void)
+/* should return $HOME/.[name] */
+char *os_get_config_path(const char *name)
 {
 	char *path_ptr = getenv("HOME");
 	if (path_ptr == NULL)
 		bcrash("Could not get $HOME\n");
 
-	char *path = bmalloc(strlen(path_ptr)+1);
-
-	strcpy(path, path_ptr);
-	return path;
+	struct dstr path;
+	dstr_init_copy(&path, path_ptr);
+	dstr_cat(&path, ".");
+	dstr_cat(&path, name);
+	return path.array;
 }
 
 bool os_file_exists(const char *path)

+ 9 - 5
libobs/util/platform-windows.c

@@ -135,17 +135,21 @@ uint64_t os_gettime_ns(void)
 	return (uint64_t)time_val;
 }
 
-/* returns %appdata% on windows */
-char *os_get_home_path(void)
+/* returns %appdata%/[name] on windows */
+char *os_get_config_path(const char *name)
 {
-	char *out;
+	char *ptr;
 	wchar_t path_utf16[MAX_PATH];
+	struct dstr path;
 
 	SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
 			path_utf16);
 
-	os_wcs_to_utf8(path_utf16, 0, &out);
-	return out;
+	os_wcs_to_utf8(path_utf16, 0, &ptr);
+	dstr_init_move_array(&path, ptr);
+	dstr_cat(&path, "\\");
+	dstr_cat(&path, name);
+	return path.array;
 }
 
 bool os_file_exists(const char *path)

+ 1 - 1
libobs/util/platform.h

@@ -69,7 +69,7 @@ EXPORT void os_sleep_ms(uint32_t duration);
 
 EXPORT uint64_t os_gettime_ns(void);
 
-EXPORT char *os_get_home_path(void);
+EXPORT char *os_get_config_path(const char *name);
 
 EXPORT bool os_file_exists(const char *path);
 

+ 5 - 23
obs/obs-app.cpp

@@ -111,39 +111,21 @@ static bool do_mkdir(const char *path)
 
 static bool MakeUserDirs()
 {
-	BPtr<char> homePath(os_get_home_path());
-	stringstream str;
-
-	str << homePath << "/obs-studio";
-	if (!do_mkdir(str.str().c_str()))
-		return false;
-
-	return true;
+	BPtr<char> configPath(os_get_config_path("obs-studio"));
+	return do_mkdir(configPath);
 }
 
 bool OBSApp::InitGlobalConfig()
 {
-	BPtr<char> homePath(os_get_home_path());
-	stringstream str;
+	BPtr<char> path(os_get_config_path("obs-studio/global.ini"));
 
-	if (!homePath) {
-		OBSErrorBox(NULL, "Failed to get home path");
-		return false;
-	}
-
-	str << homePath << "/obs-studio/global.ini";
-	string path = move(str.str());
-
-	int errorcode = globalConfig.Open(path.c_str(), CONFIG_OPEN_ALWAYS);
+	int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
 	if (errorcode != CONFIG_SUCCESS) {
 		OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
 		return false;
 	}
 
-	if (!InitGlobalConfigDefaults())
-		return false;
-
-	return true;
+	return InitGlobalConfigDefaults();
 }
 
 #define DEFAULT_LANG "en"