libbluray | branch: master | hpi1 <[email protected]> | Fri Nov 15 09:49:44 2013 +0200| [33501aa84d92c0447af8a1099aa4461c492291a8] | committer: hpi1
Updated system directory utils and moved function definitions from file.h to dirs.h > http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=33501aa84d92c0447af8a1099aa4461c492291a8 --- src/Makefile.am | 1 + src/file/dirs.h | 38 +++++++++++++++++++++++++++ src/file/dirs_darwin.c | 44 ++++++++++++++++++++++++++++++++ src/file/dirs_win32.c | 46 +++++++++++++++++++++++++++++++-- src/file/dirs_xdg.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++- src/file/file.h | 7 ----- src/libbluray/bdj/bdj.c | 2 +- 7 files changed, 192 insertions(+), 11 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 8fd3a8c..ed45f31 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -71,6 +71,7 @@ libbluray_la_SOURCES = \ libbluray/hdmv/mobj_print.c \ file/file.h \ file/file_posix.c \ + file/dirs.h \ file/dl.h \ file/filesystem.h \ file/filesystem.c \ diff --git a/src/file/dirs.h b/src/file/dirs.h new file mode 100644 index 0000000..bb098af --- /dev/null +++ b/src/file/dirs.h @@ -0,0 +1,38 @@ +/* + * This file is part of libbluray + * Copyright (C) 2011-2013 VideoLAN + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#ifndef BLURAY_DIRS_H +#define BLURAY_DIRS_H + +#include <util/attributes.h> + +#ifdef _WIN32 +BD_PRIVATE int win32_mkdir(const char *dir); +#endif + +/* + * 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); + +#endif diff --git a/src/file/dirs_darwin.c b/src/file/dirs_darwin.c index fe4dc6b..851fc0d 100644 --- a/src/file/dirs_darwin.c +++ b/src/file/dirs_darwin.c @@ -32,10 +32,31 @@ #include "util/strutl.h" #include "util/logging.h" +#define USER_CFG_DIR "Library/Preferences" #define USER_CACHE_DIR "Library/Caches" #define USER_DATA_DIR "Library" +#define SYSTEM_CFG_DIR "/Library/Preferences" +const 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"); + } + + return dir; +} + const char *file_get_data_home(void) { static char *dir = NULL; @@ -73,3 +94,26 @@ const char *file_get_cache_home(void) return dir; } + +const char *file_get_config_system(const char *dir) +{ + static char *dirs = NULL; // "dir1\0dir2\0...\0dirN\0\0" + + if (!dirs) { + dirs = str_printf("%s%c%c", SYSTEM_CFG_DIR, 0, 0); + } + + if (!dir) { + // first call + dir = dirs; + } else { + // next call + dir += strlen(dir) + 1; + if (!*dir) { + // end of list + dir = NULL; + } + } + + return dir; +} diff --git a/src/file/dirs_win32.c b/src/file/dirs_win32.c index 0ae832e..c34d5a2 100644 --- a/src/file/dirs_win32.c +++ b/src/file/dirs_win32.c @@ -21,10 +21,9 @@ # include "config.h" #endif -#include "file.h" +#include "dirs.h" #include "util/logging.h" -#include "util/macro.h" #include <stdio.h> #include <string.h> @@ -35,6 +34,19 @@ #include <direct.h> +int win32_mkdir(const char *dir) +{ + wchar_t wdir[MAX_PATH]; + + MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH); + return _wmkdir(wdir); +} + +const char *file_get_config_home(void) +{ + return file_get_data_home(); +} + const char *file_get_data_home(void) { static char *appdir = NULL; @@ -60,3 +72,33 @@ const char *file_get_cache_home(void) { return file_get_data_home(); } + +const char *file_get_config_system(const char *dir) +{ + static char *appdir = NULL; + wchar_t wdir[MAX_PATH]; + + if (!dir) { + // first call + + if (*appdir) + return appdir; + + /* Get the "Application Data" folder for all users */ + if (S_OK == SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, + NULL, SHGFP_TYPE_CURRENT, wdir)) { + int len = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL); + appdir = malloc(len); + WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL); + return appdir; + } else { + BD_DEBUG(DBG_FILE, "Can't find common configuration directory !\n"); + return NULL; + } + } else { + // next call + return NULL; + } + + return dir; +} diff --git a/src/file/dirs_xdg.c b/src/file/dirs_xdg.c index 7a41ece..df5d81b 100644 --- a/src/file/dirs_xdg.c +++ b/src/file/dirs_xdg.c @@ -21,7 +21,7 @@ # include "config.h" #endif -#include "file.h" +#include "dirs.h" #include "util/strutl.h" #include "util/logging.h" @@ -35,10 +35,36 @@ * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html */ +#define USER_CFG_DIR ".config" #define USER_CACHE_DIR ".cache" #define USER_DATA_DIR ".local/share" +#define SYSTEM_CFG_DIR "/etc/xdg" +const 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); + } + + BD_DEBUG(DBG_FILE, "Can't find user home directory ($HOME) !\n"); + } + + return dir; +} + const char *file_get_data_home(void) { static char *dir = NULL; @@ -86,3 +112,40 @@ const char *file_get_cache_home(void) return dir; } + +const char *file_get_config_system(const char *dir) +{ + static char *dirs = NULL; // "dir1\0dir2\0...\0dirN\0\0" + + if (!dirs) { + const char *xdg_sys = getenv("XDG_CONFIG_DIRS"); + + if (xdg_sys && *xdg_sys) { + + dirs = calloc(1, strlen(xdg_sys) + 2); + strcpy(dirs, xdg_sys); + + char *pt = dirs; + while (NULL != (pt = strchr(pt, ':'))) { + *pt++ = 0; + } + + } else { + dirs = str_printf("%s%c%c", SYSTEM_CFG_DIR, 0, 0); + } + } + + if (!dir) { + // first call + dir = dirs; + } else { + // next call + dir += strlen(dir) + 1; + if (!*dir) { + // end of list + dir = NULL; + } + } + + return dir; +} diff --git a/src/file/file.h b/src/file/file.h index 080485b..5ee8c01 100644 --- a/src/file/file.h +++ b/src/file/file.h @@ -55,11 +55,4 @@ BD_PRIVATE BD_FILE_OPEN file_open_default(void); BD_PRIVATE extern BD_DIR_H* (*dir_open)(const char* dirname); -/* - * User cache and data dirs - */ - -BD_PRIVATE const char *file_get_data_home(void); -BD_PRIVATE const char *file_get_cache_home(void); - #endif /* FILE_H_ */ diff --git a/src/libbluray/bdj/bdj.c b/src/libbluray/bdj/bdj.c index ccb4fe3..d60d6fc 100644 --- a/src/libbluray/bdj/bdj.c +++ b/src/libbluray/bdj/bdj.c @@ -29,7 +29,7 @@ #include "bdj_util.h" #include "common.h" #include "libbluray/register.h" -#include "file/file.h" +#include "file/dirs.h" #include "file/dl.h" #include "util/strutl.h" #include "util/macro.h" _______________________________________________ libbluray-devel mailing list [email protected] https://mailman.videolan.org/listinfo/libbluray-devel
