From 26b9cb3c7826f7f9806181044edd3b984f7ddda9 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Sat, 13 Jan 2018 00:27:49 -0500 Subject: [PATCH] libretro-common: Clean up dependencies of file_path This moves a few functions that were defined in file_path_special to libretro-common to clean up the dependency tree. --- file_path_special.c | 237 ---------------------- file_path_special.h | 9 - libretro-common/file/file_path.c | 238 +++++++++++++++++++++++ libretro-common/include/file/file_path.h | 9 + 4 files changed, 247 insertions(+), 246 deletions(-) diff --git a/file_path_special.c b/file_path_special.c index 7c24df4571..7817ac4e66 100644 --- a/file_path_special.c +++ b/file_path_special.c @@ -65,118 +65,6 @@ #include "paths.h" #include "verbosity.h" -void fill_pathname_expand_special(char *out_path, - const char *in_path, size_t size) -{ -#if !defined(RARCH_CONSOLE) - if (*in_path == '~') - { - const char *home = getenv("HOME"); - if (home) - { - size_t src_size = strlcpy(out_path, home, size); - retro_assert(src_size < size); - - out_path += src_size; - size -= src_size; - in_path++; - } - } - else if ((in_path[0] == ':') && - ( - (in_path[1] == '/') -#ifdef _WIN32 - || (in_path[1] == '\\') -#endif - ) - ) - { - size_t src_size; - char *application_dir = (char*)malloc(PATH_MAX_LENGTH * sizeof(char)); - - application_dir[0] = '\0'; - - fill_pathname_application_path(application_dir, - PATH_MAX_LENGTH * sizeof(char)); - path_basedir_wrapper(application_dir); - - src_size = strlcpy(out_path, application_dir, size); - retro_assert(src_size < size); - - free(application_dir); - - out_path += src_size; - size -= src_size; - in_path += 2; - } -#endif - - retro_assert(strlcpy(out_path, in_path, size) < size); -} - - -void fill_pathname_abbreviate_special(char *out_path, - const char *in_path, size_t size) -{ -#if !defined(RARCH_CONSOLE) - unsigned i; - const char *candidates[3]; - const char *notations[3]; - char *application_dir = (char*)malloc(PATH_MAX_LENGTH * sizeof(char)); - const char *home = getenv("HOME"); - - application_dir[0] = '\0'; - - /* application_dir could be zero-string. Safeguard against this. - * - * Keep application dir in front of home, moving app dir to a - * new location inside home would break otherwise. */ - - /* ugly hack - use application_dir pointer - * before filling it in. C89 reasons */ - candidates[0] = application_dir; - candidates[1] = home; - candidates[2] = NULL; - - notations [0] = ":"; - notations [1] = "~"; - notations [2] = NULL; - - fill_pathname_application_path(application_dir, - PATH_MAX_LENGTH * sizeof(char)); - path_basedir_wrapper(application_dir); - - for (i = 0; candidates[i]; i++) - { - if (!string_is_empty(candidates[i]) && - strstr(in_path, candidates[i]) == in_path) - { - size_t src_size = strlcpy(out_path, notations[i], size); - - retro_assert(src_size < size); - - out_path += src_size; - size -= src_size; - in_path += strlen(candidates[i]); - - if (!path_char_is_slash(*in_path)) - { - retro_assert(strlcpy(out_path, - path_default_slash(), size) < size); - out_path++; - size--; - } - - break; /* Don't allow more abbrevs to take place. */ - } - } - - free(application_dir); -#endif - - retro_assert(strlcpy(out_path, in_path, size) < size); -} - bool fill_pathname_application_data(char *s, size_t len) { #if defined(_WIN32) && !defined(_XBOX) @@ -240,103 +128,6 @@ bool fill_pathname_application_data(char *s, size_t len) return false; } -#if !defined(RARCH_CONSOLE) -void fill_pathname_application_path(char *s, size_t len) -{ - size_t i; -#ifdef __APPLE__ - CFBundleRef bundle = CFBundleGetMainBundle(); -#endif -#ifdef _WIN32 - DWORD ret; - wchar_t wstr[PATH_MAX_LENGTH] = {0}; -#endif -#ifdef __HAIKU__ - image_info info; - int32_t cookie = 0; -#endif - (void)i; - - if (!len) - return; - -#ifdef _WIN32 -#ifdef LEGACY_WIN32 - ret = GetModuleFileNameA(GetModuleHandle(NULL), s, len); -#else - ret = GetModuleFileNameW(GetModuleHandle(NULL), wstr, ARRAY_SIZE(wstr)); - - if (*wstr) - { - char *str = utf16_to_utf8_string_alloc(wstr); - - if (str) - { - strlcpy(s, str, len); - free(str); - } - } -#endif - s[ret] = '\0'; -#elif defined(__APPLE__) - if (bundle) - { - CFURLRef bundle_url = CFBundleCopyBundleURL(bundle); - CFStringRef bundle_path = CFURLCopyPath(bundle_url); - CFStringGetCString(bundle_path, s, len, kCFStringEncodingUTF8); - CFRelease(bundle_path); - CFRelease(bundle_url); - - retro_assert(strlcat(s, "nobin", len) < len); - return; - } -#elif defined(__HAIKU__) - while (get_next_image_info(0, &cookie, &info) == B_OK) - { - if (info.type == B_APP_IMAGE) - { - strlcpy(s, info.name, len); - return; - } - } -#elif defined(__QNX__) - char *buff = malloc(len); - - if(_cmdname(buff)) - strlcpy(s, buff, len); - - free(buff); -#else - { - pid_t pid; - static const char *exts[] = { "exe", "file", "path/a.out" }; - char link_path[255]; - - link_path[0] = *s = '\0'; - pid = getpid(); - - /* Linux, BSD and Solaris paths. Not standardized. */ - for (i = 0; i < ARRAY_SIZE(exts); i++) - { - ssize_t ret; - - snprintf(link_path, sizeof(link_path), "/proc/%u/%s", - (unsigned)pid, exts[i]); - ret = readlink(link_path, s, len - 1); - - if (ret >= 0) - { - s[ret] = '\0'; - return; - } - } - } - - RARCH_ERR("Cannot resolve application path! This should not happen.\n"); -#endif -} -#endif - #ifdef HAVE_XMB const char* xmb_theme_ident(void); #endif @@ -626,31 +417,3 @@ void fill_short_pathname_representation_wrapper(char* out_rep, fill_short_pathname_representation(out_rep, in_path, size); free(path_short); } - -/** - * path_basedir: - * @path : path - * - * Extracts base directory by mutating path. - * Keeps trailing '/'. - **/ -void path_basedir_wrapper(char *path) -{ - char *last = NULL; - if (strlen(path) < 2) - return; - -#ifdef HAVE_COMPRESSION - /* We want to find the directory with the archive in basedir. */ - last = (char*)path_get_archive_delim(path); - if (last) - *last = '\0'; -#endif - - last = find_last_slash(path); - - if (last) - last[1] = '\0'; - else - snprintf(path, 3, ".%s", path_default_slash()); -} diff --git a/file_path_special.h b/file_path_special.h index 39c4f4ed0c..b0eeee3bdc 100644 --- a/file_path_special.h +++ b/file_path_special.h @@ -130,15 +130,6 @@ enum application_special_type void fill_short_pathname_representation_wrapper(char* out_rep, const char *in_path, size_t size); -/** - * path_basedir: - * @path : path - * - * Extracts base directory by mutating path. - * Keeps trailing '/'. - **/ -void path_basedir_wrapper(char *path); - const char *file_path_str(enum file_path_enum enum_idx); bool fill_pathname_application_data(char *s, size_t len); diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 2572170207..203c2875ee 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -30,6 +30,8 @@ #include #include +#include +#include #ifndef __MACH__ #include @@ -912,3 +914,239 @@ void fill_short_pathname_representation_noext(char* out_rep, fill_short_pathname_representation(out_rep, in_path, size); path_remove_extension(out_rep); } + +void fill_pathname_expand_special(char *out_path, + const char *in_path, size_t size) +{ +#if !defined(RARCH_CONSOLE) + if (*in_path == '~') + { + const char *home = getenv("HOME"); + if (home) + { + size_t src_size = strlcpy(out_path, home, size); + retro_assert(src_size < size); + + out_path += src_size; + size -= src_size; + in_path++; + } + } + else if ((in_path[0] == ':') && + ( + (in_path[1] == '/') +#ifdef _WIN32 + || (in_path[1] == '\\') +#endif + ) + ) + { + size_t src_size; + char *application_dir = (char*)malloc(PATH_MAX_LENGTH * sizeof(char)); + + application_dir[0] = '\0'; + + fill_pathname_application_path(application_dir, + PATH_MAX_LENGTH * sizeof(char)); + path_basedir_wrapper(application_dir); + + src_size = strlcpy(out_path, application_dir, size); + retro_assert(src_size < size); + + free(application_dir); + + out_path += src_size; + size -= src_size; + in_path += 2; + } +#endif + + retro_assert(strlcpy(out_path, in_path, size) < size); +} + +void fill_pathname_abbreviate_special(char *out_path, + const char *in_path, size_t size) +{ +#if !defined(RARCH_CONSOLE) + unsigned i; + const char *candidates[3]; + const char *notations[3]; + char *application_dir = (char*)malloc(PATH_MAX_LENGTH * sizeof(char)); + const char *home = getenv("HOME"); + + application_dir[0] = '\0'; + + /* application_dir could be zero-string. Safeguard against this. + * + * Keep application dir in front of home, moving app dir to a + * new location inside home would break otherwise. */ + + /* ugly hack - use application_dir pointer + * before filling it in. C89 reasons */ + candidates[0] = application_dir; + candidates[1] = home; + candidates[2] = NULL; + + notations [0] = ":"; + notations [1] = "~"; + notations [2] = NULL; + + fill_pathname_application_path(application_dir, + PATH_MAX_LENGTH * sizeof(char)); + path_basedir_wrapper(application_dir); + + for (i = 0; candidates[i]; i++) + { + if (!string_is_empty(candidates[i]) && + strstr(in_path, candidates[i]) == in_path) + { + size_t src_size = strlcpy(out_path, notations[i], size); + + retro_assert(src_size < size); + + out_path += src_size; + size -= src_size; + in_path += strlen(candidates[i]); + + if (!path_char_is_slash(*in_path)) + { + retro_assert(strlcpy(out_path, + path_default_slash(), size) < size); + out_path++; + size--; + } + + break; /* Don't allow more abbrevs to take place. */ + } + } + + free(application_dir); +#endif + + retro_assert(strlcpy(out_path, in_path, size) < size); +} + +/** + * path_basedir: + * @path : path + * + * Extracts base directory by mutating path. + * Keeps trailing '/'. + **/ +void path_basedir_wrapper(char *path) +{ + char *last = NULL; + if (strlen(path) < 2) + return; + +#ifdef HAVE_COMPRESSION + /* We want to find the directory with the archive in basedir. */ + last = (char*)path_get_archive_delim(path); + if (last) + *last = '\0'; +#endif + + last = find_last_slash(path); + + if (last) + last[1] = '\0'; + else + snprintf(path, 3, ".%s", path_default_slash()); +} + +#if !defined(RARCH_CONSOLE) +void fill_pathname_application_path(char *s, size_t len) +{ + size_t i; +#ifdef __APPLE__ + CFBundleRef bundle = CFBundleGetMainBundle(); +#endif +#ifdef _WIN32 + DWORD ret; + wchar_t wstr[PATH_MAX_LENGTH] = {0}; +#endif +#ifdef __HAIKU__ + image_info info; + int32_t cookie = 0; +#endif + (void)i; + + if (!len) + return; + +#ifdef _WIN32 +#ifdef LEGACY_WIN32 + ret = GetModuleFileNameA(GetModuleHandle(NULL), s, len); +#else + ret = GetModuleFileNameW(GetModuleHandle(NULL), wstr, ARRAY_SIZE(wstr)); + + if (*wstr) + { + char *str = utf16_to_utf8_string_alloc(wstr); + + if (str) + { + strlcpy(s, str, len); + free(str); + } + } +#endif + s[ret] = '\0'; +#elif defined(__APPLE__) + if (bundle) + { + CFURLRef bundle_url = CFBundleCopyBundleURL(bundle); + CFStringRef bundle_path = CFURLCopyPath(bundle_url); + CFStringGetCString(bundle_path, s, len, kCFStringEncodingUTF8); + CFRelease(bundle_path); + CFRelease(bundle_url); + + retro_assert(strlcat(s, "nobin", len) < len); + return; + } +#elif defined(__HAIKU__) + while (get_next_image_info(0, &cookie, &info) == B_OK) + { + if (info.type == B_APP_IMAGE) + { + strlcpy(s, info.name, len); + return; + } + } +#elif defined(__QNX__) + char *buff = malloc(len); + + if(_cmdname(buff)) + strlcpy(s, buff, len); + + free(buff); +#else + { + pid_t pid; + static const char *exts[] = { "exe", "file", "path/a.out" }; + char link_path[255]; + + link_path[0] = *s = '\0'; + pid = getpid(); + + /* Linux, BSD and Solaris paths. Not standardized. */ + for (i = 0; i < ARRAY_SIZE(exts); i++) + { + ssize_t ret; + + snprintf(link_path, sizeof(link_path), "/proc/%u/%s", + (unsigned)pid, exts[i]); + ret = readlink(link_path, s, len - 1); + + if (ret >= 0) + { + s[ret] = '\0'; + return; + } + } + } + + /* RARCH_ERR("Cannot resolve application path! This should not happen.\n"); */ +#endif +} +#endif diff --git a/libretro-common/include/file/file_path.h b/libretro-common/include/file/file_path.h index 1ae750c38b..0485821ebb 100644 --- a/libretro-common/include/file/file_path.h +++ b/libretro-common/include/file/file_path.h @@ -389,6 +389,15 @@ void fill_pathname_expand_special(char *out_path, void fill_pathname_abbreviate_special(char *out_path, const char *in_path, size_t size); +/** + * path_basedir: + * @path : path + * + * Extracts base directory by mutating path. + * Keeps trailing '/'. + **/ +void path_basedir_wrapper(char *path); + /** * path_char_is_slash: * @c : character