diff options
author | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2012-12-09 15:05:21 +0100 |
---|---|---|
committer | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2012-12-15 17:38:00 +0100 |
commit | fab9febdc3a863c157a56cc4de2418cbb9665844 (patch) | |
tree | 8c442f1f04d67587d91f7f5427d5c90d7a99ff0e /core/path.c | |
parent | c7bf5111c7db05dc5b1750ff285e343849293dd3 (diff) | |
download | mpv-fab9febdc3a863c157a56cc4de2418cbb9665844.tar.bz2 mpv-fab9febdc3a863c157a56cc4de2418cbb9665844.tar.xz |
path: add mp_find_config_file and reorganize some of the code
Add `mp_find_config_file` to search different known paths and use that in
ass_mp to look for the fontconfig configuration file.
Some incidental changes spawned by this feature where:
* Buffer allocation for the strings containing the paths is now performed
with talloc. All of the allocations are done on a NULL context, but it still
improves readability of the code.
* Move the OSX function for lookup inside of a bundle: this code path was
currently not used by the bundle generated with `make osxbundle`. The plan
is to use it again in a future commit to get a fontconfig config file.
Diffstat (limited to 'core/path.c')
-rw-r--r-- | core/path.c | 130 |
1 files changed, 54 insertions, 76 deletions
diff --git a/core/path.c b/core/path.c index 3a2d4c09ba..ed53d5d465 100644 --- a/core/path.c +++ b/core/path.c @@ -23,6 +23,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -33,51 +34,63 @@ #include "config.h" #include "core/mp_msg.h" #include "core/path.h" +#include "talloc.h" +#include "osdep/io.h" -#ifdef CONFIG_MACOSX_BUNDLE -#include <CoreFoundation/CoreFoundation.h> -#include <unistd.h> -#elif defined(__MINGW32__) +#if defined(__MINGW32__) #include <windows.h> #elif defined(__CYGWIN__) #include <windows.h> #include <sys/cygwin.h> #endif -#include "talloc.h" +#ifdef CONFIG_MACOSX_BUNDLE +#include "osdep/macosx_bundle.h" +#endif -#include "osdep/io.h" -char *get_path(const char *filename) +typedef char *(*lookup_fun)(const char *); +static const lookup_fun config_lookup_functions[] = { + mp_find_user_config_file, +#ifdef CONFIG_MACOSX_BUNDLE + get_bundled_path, +#endif + mp_find_global_config_file, + NULL +}; + +char *mp_find_config_file(const char *filename) { - char *homedir; - char *buff; + for (int i = 0; config_lookup_functions[i] != NULL; i++) { + char *path = config_lookup_functions[i](filename); + if (!path) continue; + + if (mp_path_exists(path)) + return path; + + talloc_free(path); + } + return NULL; +} + +char *mp_find_user_config_file(const char *filename) +{ + char *homedir = NULL, *buff = NULL; #ifdef __MINGW32__ - static char *config_dir = "/mpv"; + static char *config_dir = "mpv"; #else - static char *config_dir = "/.mpv"; + static char *config_dir = ".mpv"; #endif #if defined(__MINGW32__) || defined(__CYGWIN__) char exedir[260]; #endif - int len; -#ifdef CONFIG_MACOSX_BUNDLE - struct stat dummy; - CFIndex maxlen = 256; - CFURLRef res_url_ref = NULL; - CFURLRef bdl_url_ref = NULL; - char *res_url_path = NULL; - char *bdl_url_path = NULL; -#endif - - if ((homedir = getenv("MPV_HOME")) != NULL) + if ((homedir = getenv("MPV_HOME")) != NULL) { config_dir = ""; - else if ((homedir = getenv("HOME")) == NULL) + } else if ((homedir = getenv("HOME")) == NULL) { #if defined(__MINGW32__) || defined(__CYGWIN__) /* Hack to get fonts etc. loaded outside of Cygwin environment. */ - { int i, imax = 0; - len = (int)GetModuleFileNameA(NULL, exedir, 260); + int len = (int)GetModuleFileNameA(NULL, exedir, 260); for (i = 0; i < len; i++) if (exedir[i] == '\\') { exedir[i] = '/'; @@ -85,67 +98,32 @@ char *get_path(const char *filename) } exedir[imax] = '\0'; homedir = exedir; - } #else return NULL; #endif - len = strlen(homedir) + strlen(config_dir) + 1; - if (filename == NULL) { - if ((buff = malloc(len)) == NULL) - return NULL; - sprintf(buff, "%s%s", homedir, config_dir); - } else { - len += strlen(filename) + 1; - if ((buff = malloc(len)) == NULL) - return NULL; - sprintf(buff, "%s%s/%s", homedir, config_dir, filename); } -#ifdef CONFIG_MACOSX_BUNDLE - if (stat(buff, &dummy)) { - - res_url_ref = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()); - bdl_url_ref = CFBundleCopyBundleURL(CFBundleGetMainBundle()); - - if (res_url_ref && bdl_url_ref) { - - res_url_path = malloc(maxlen); - bdl_url_path = malloc(maxlen); - - while (!CFURLGetFileSystemRepresentation(res_url_ref, true, - res_url_path, maxlen)) { - maxlen *= 2; - res_url_path = realloc(res_url_path, maxlen); - } - CFRelease(res_url_ref); - - while (!CFURLGetFileSystemRepresentation(bdl_url_ref, true, - bdl_url_path, maxlen)) { - maxlen *= 2; - bdl_url_path = realloc(bdl_url_path, maxlen); - } - CFRelease(bdl_url_ref); - - if (strcmp(res_url_path, bdl_url_path) == 0) - res_url_path = NULL; - } - - if (res_url_path && filename) { - if ((strlen(filename) + strlen(res_url_path) + 2) > maxlen) - maxlen = strlen(filename) + strlen(res_url_path) + 2; - free(buff); - buff = malloc(maxlen); - strcpy(buff, res_url_path); - - strcat(buff, "/"); - strcat(buff, filename); - } + if (filename) { + char * temp = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir)); + buff = mp_path_join(NULL, bstr0(temp), bstr0(filename)); + talloc_free(temp); + } else { + buff = mp_path_join(NULL, bstr0(homedir), bstr0(config_dir)); } -#endif + mp_msg(MSGT_GLOBAL, MSGL_V, "get_path('%s') -> '%s'\n", filename, buff); return buff; } +char *mp_find_global_config_file(const char *filename) +{ + if (filename) { + return mp_path_join(NULL, bstr0(MPLAYER_CONFDIR), bstr0(filename)); + } else { + return talloc_strdup(NULL, MPLAYER_CONFDIR); + } +} + char *mp_basename(const char *path) { char *s; |