[libbluray-devel] Remove static variables from file_get_[cache|data|config]_home()
hpi1
git at videolan.org
Wed Jan 7 12:06:22 CET 2015
libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Wed Jan 7 12:35:49 2015 +0200| [06acffbfc511f9a97bf96cbe84c0a6e18a3d15dd] | committer: hpi1
Remove static variables from file_get_[cache|data|config]_home()
> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=06acffbfc511f9a97bf96cbe84c0a6e18a3d15dd
---
src/file/dirs.h | 7 ++--
src/file/dirs_darwin.c | 60 ++++++++++-----------------------
src/file/dirs_win32.c | 12 +++----
src/file/dirs_xdg.c | 84 +++++++++++++++++------------------------------
src/libbluray/bdj/bdj.c | 18 +++++-----
5 files changed, 64 insertions(+), 117 deletions(-)
diff --git a/src/file/dirs.h b/src/file/dirs.h
index ed2f526..841559f 100644
--- a/src/file/dirs.h
+++ b/src/file/dirs.h
@@ -31,9 +31,10 @@ BD_PRIVATE char *win32_get_font_dir(const char *font_file);
* Config, cache and data dirs
*/
-BD_PRIVATE const char *file_get_config_home(void);
BD_PRIVATE const char *file_get_config_system(const char *dir);
-BD_PRIVATE const char *file_get_cache_home(void);
-BD_PRIVATE const char *file_get_data_home(void);
+
+BD_PRIVATE char *file_get_config_home(void);
+BD_PRIVATE char *file_get_cache_home(void);
+BD_PRIVATE char *file_get_data_home(void);
#endif
diff --git a/src/file/dirs_darwin.c b/src/file/dirs_darwin.c
index b7fcc29..8ff32e4 100644
--- a/src/file/dirs_darwin.c
+++ b/src/file/dirs_darwin.c
@@ -38,61 +38,37 @@
#define SYSTEM_CFG_DIR "/Library/Preferences"
-const char *file_get_config_home(void)
+char *file_get_config_home(void)
{
- static char *dir = NULL;
- static int init_done = 0;
-
- if (!init_done) {
- init_done = 1;
-
- const char *user_home = getenv("HOME");
- if (user_home && *user_home) {
- return dir = str_printf("%s/%s", user_home, USER_CFG_DIR);
- }
-
- BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ const char *user_home = getenv("HOME");
+ if (user_home && *user_home) {
+ return str_printf("%s/%s", user_home, USER_CFG_DIR);
}
- return dir;
+ BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ return NULL;
}
-const char *file_get_data_home(void)
+char *file_get_data_home(void)
{
- static char *dir = NULL;
- static int init_done = 0;
-
- if (!init_done) {
- init_done = 1;
-
- const char *user_home = getenv("HOME");
- if (user_home && *user_home) {
- return dir = str_printf("%s/%s", user_home, USER_DATA_DIR);
- }
-
- BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ const char *user_home = getenv("HOME");
+ if (user_home && *user_home) {
+ return str_printf("%s/%s", user_home, USER_DATA_DIR);
}
- return dir;
+ BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ return NULL;
}
-const char *file_get_cache_home(void)
+char *file_get_cache_home(void)
{
- static char *dir = NULL;
- static int init_done = 0;
-
- if (!init_done) {
- init_done = 1;
-
- const char *user_home = getenv("HOME");
- if (user_home && *user_home) {
- return dir = str_printf("%s/%s", user_home, USER_CACHE_DIR);
- }
-
- BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ const char *user_home = getenv("HOME");
+ if (user_home && *user_home) {
+ return str_printf("%s/%s", user_home, USER_CACHE_DIR);
}
- return dir;
+ BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ return NULL;
}
const char *file_get_config_system(const char *dir)
diff --git a/src/file/dirs_win32.c b/src/file/dirs_win32.c
index c581a48..4d1e51d 100644
--- a/src/file/dirs_win32.c
+++ b/src/file/dirs_win32.c
@@ -59,24 +59,20 @@ char *win32_get_font_dir(const char *font_file)
return path;
}
-const char *file_get_config_home(void)
+char *file_get_config_home(void)
{
return file_get_data_home();
}
-const char *file_get_data_home(void)
+char *file_get_data_home(void)
{
- static char *appdir = NULL;
wchar_t wdir[MAX_PATH];
- if (appdir)
- return appdir;
-
/* Get the "Application Data" folder for the user */
if (S_OK == SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
NULL, SHGFP_TYPE_CURRENT, wdir)) {
int len = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL);
- appdir = malloc(len);
+ char *appdir = malloc(len);
WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
return appdir;
}
@@ -85,7 +81,7 @@ const char *file_get_data_home(void)
return NULL;
}
-const char *file_get_cache_home(void)
+char *file_get_cache_home(void)
{
return file_get_data_home();
}
diff --git a/src/file/dirs_xdg.c b/src/file/dirs_xdg.c
index df5d81b..c38f710 100644
--- a/src/file/dirs_xdg.c
+++ b/src/file/dirs_xdg.c
@@ -41,76 +41,52 @@
#define SYSTEM_CFG_DIR "/etc/xdg"
-const char *file_get_config_home(void)
+char *file_get_config_home(void)
{
- static char *dir = NULL;
- static int init_done = 0;
-
- if (!init_done) {
- init_done = 1;
-
- const char *xdg_home = getenv("XDG_CONFIG_HOME");
- if (xdg_home && *xdg_home) {
- return dir = str_printf("%s", xdg_home);
- }
-
- const char *user_home = getenv("HOME");
- if (user_home && *user_home) {
- return dir = str_printf("%s/%s", user_home, USER_CFG_DIR);
- }
+ const char *xdg_home = getenv("XDG_CONFIG_HOME");
+ if (xdg_home && *xdg_home) {
+ return str_printf("%s", xdg_home);
+ }
- BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ const char *user_home = getenv("HOME");
+ if (user_home && *user_home) {
+ return str_printf("%s/%s", user_home, USER_CFG_DIR);
}
- return dir;
+ BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ return NULL;
}
-const char *file_get_data_home(void)
+char *file_get_data_home(void)
{
- static char *dir = NULL;
- static int init_done = 0;
-
- if (!init_done) {
- init_done = 1;
-
- const char *xdg_home = getenv("XDG_DATA_HOME");
- if (xdg_home && *xdg_home) {
- return dir = str_printf("%s", xdg_home);
- }
-
- const char *user_home = getenv("HOME");
- if (user_home && *user_home) {
- return dir = str_printf("%s/%s", user_home, USER_DATA_DIR);
- }
+ const char *xdg_home = getenv("XDG_DATA_HOME");
+ if (xdg_home && *xdg_home) {
+ return str_printf("%s", xdg_home);
+ }
- BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ const char *user_home = getenv("HOME");
+ if (user_home && *user_home) {
+ return str_printf("%s/%s", user_home, USER_DATA_DIR);
}
- return dir;
+ BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ return NULL;
}
-const char *file_get_cache_home(void)
+char *file_get_cache_home(void)
{
- static char *dir = NULL;
- static int init_done = 0;
-
- if (!init_done) {
- init_done = 1;
-
- const char *xdg_cache = getenv("XDG_CACHE_HOME");
- if (xdg_cache && *xdg_cache) {
- return dir = str_printf("%s", xdg_cache);
- }
-
- const char *user_home = getenv("HOME");
- if (user_home && *user_home) {
- return dir = str_printf("%s/%s", user_home, USER_CACHE_DIR);
- }
+ const char *xdg_cache = getenv("XDG_CACHE_HOME");
+ if (xdg_cache && *xdg_cache) {
+ return str_printf("%s", xdg_cache);
+ }
- BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ const char *user_home = getenv("HOME");
+ if (user_home && *user_home) {
+ return str_printf("%s/%s", user_home, USER_CACHE_DIR);
}
- return dir;
+ BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n");
+ return NULL;
}
const char *file_get_config_system(const char *dir)
diff --git a/src/libbluray/bdj/bdj.c b/src/libbluray/bdj/bdj.c
index aed4174..e1c1adb 100644
--- a/src/libbluray/bdj/bdj.c
+++ b/src/libbluray/bdj/bdj.c
@@ -264,6 +264,7 @@ static const char *_find_libbluray_jar(void)
static const char *_bdj_persistent_root(BDJ_STORAGE *storage)
{
const char *root;
+ char *data_home;
if (storage->persistent_root) {
return storage->persistent_root;
@@ -274,11 +275,9 @@ static const char *_bdj_persistent_root(BDJ_STORAGE *storage)
return root;
}
- root = file_get_data_home();
- if (!root) {
- root = "";
- }
- storage->persistent_root = str_printf("%s/bluray/dvb.persistent.root/", root);
+ data_home = file_get_data_home();
+ storage->persistent_root = str_printf("%s/bluray/dvb.persistent.root/", data_home ? data_home : "");
+ X_FREE(data_home);
BD_DEBUG(DBG_BDJ, "LIBBLURAY_PERSISTENT_ROOT not set, using %s\n", storage->persistent_root);
@@ -288,6 +287,7 @@ static const char *_bdj_persistent_root(BDJ_STORAGE *storage)
static const char *_bdj_buda_root(BDJ_STORAGE *storage)
{
const char *root;
+ char *cache_home;
if (storage->cache_root) {
return storage->cache_root;
@@ -298,11 +298,9 @@ static const char *_bdj_buda_root(BDJ_STORAGE *storage)
return root;
}
- root = file_get_cache_home();
- if (!root) {
- root = "";
- }
- storage->cache_root = str_printf("%s/bluray/bluray.bindingunit.root/", root);
+ cache_home = file_get_cache_home();
+ storage->cache_root = str_printf("%s/bluray/bluray.bindingunit.root/", cache_home ? cache_home : "");
+ X_FREE(cache_home);
BD_DEBUG(DBG_BDJ, "LIBBLURAY_CACHE_ROOT not set, using %s\n", storage->cache_root);
More information about the libbluray-devel
mailing list