change os_get_home_path to a better and more clear function, os_get_config_path

This commit is contained in:
jp9000 2013-12-23 18:59:54 -07:00
parent 02f9647b6c
commit bb53a39aee
5 changed files with 32 additions and 40 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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);

View file

@ -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"