From bb53a39aee0334c7412d967a1ef85e44e5e14bd6 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Mon, 23 Dec 2013 18:59:54 -0700 Subject: [PATCH] change os_get_home_path to a better and more clear function, os_get_config_path --- libobs/util/platform-cocoa.m | 15 ++++++++++----- libobs/util/platform-nix.c | 13 +++++++------ libobs/util/platform-windows.c | 14 +++++++++----- libobs/util/platform.h | 2 +- obs/obs-app.cpp | 28 +++++----------------------- 5 files changed, 32 insertions(+), 40 deletions(-) diff --git a/libobs/util/platform-cocoa.m b/libobs/util/platform-cocoa.m index 7d300010a..c77c5d1bd 100644 --- a/libobs/util/platform-cocoa.m +++ b/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) diff --git a/libobs/util/platform-nix.c b/libobs/util/platform-nix.c index cbee63e78..7a77814db 100644 --- a/libobs/util/platform-nix.c +++ b/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) diff --git a/libobs/util/platform-windows.c b/libobs/util/platform-windows.c index 2a0a4ed07..64ff36384 100644 --- a/libobs/util/platform-windows.c +++ b/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) diff --git a/libobs/util/platform.h b/libobs/util/platform.h index f13f722ef..553de6ae8 100644 --- a/libobs/util/platform.h +++ b/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); diff --git a/obs/obs-app.cpp b/obs/obs-app.cpp index 9922c5599..fbdc10bf7 100644 --- a/obs/obs-app.cpp +++ b/obs/obs-app.cpp @@ -111,39 +111,21 @@ static bool do_mkdir(const char *path) static bool MakeUserDirs() { - BPtr homePath(os_get_home_path()); - stringstream str; - - str << homePath << "/obs-studio"; - if (!do_mkdir(str.str().c_str())) - return false; - - return true; + BPtr configPath(os_get_config_path("obs-studio")); + return do_mkdir(configPath); } bool OBSApp::InitGlobalConfig() { - BPtr homePath(os_get_home_path()); - stringstream str; + BPtr 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"