diff options
author | Kacper Michajłow <kasper93@gmail.com> | 2024-05-05 18:50:38 +0200 |
---|---|---|
committer | Kacper Michajłow <kasper93@gmail.com> | 2024-05-05 19:37:57 +0200 |
commit | 053d9715076860b393956ca3c1451bcd578bf3f1 (patch) | |
tree | fbc587b2c5491f6a00e15730343adddfdb5800e7 /misc | |
parent | 1d640c9887f97ff0f94f4fb631aea3728589abeb (diff) | |
download | mpv-053d9715076860b393956ca3c1451bcd578bf3f1.tar.bz2 mpv-053d9715076860b393956ca3c1451bcd578bf3f1.tar.xz |
misc/path_utils: normalize win32 paths
Diffstat (limited to 'misc')
-rw-r--r-- | misc/path_utils.c | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/misc/path_utils.c b/misc/path_utils.c index 7fbd1084b8..a1484a9046 100644 --- a/misc/path_utils.c +++ b/misc/path_utils.c @@ -34,6 +34,11 @@ #include "misc/ctype.h" #include "misc/path_utils.h" +#if defined(HAVE_PATHCCH) && HAVE_PATHCCH +#include <windows.h> +#include <pathcch.h> +#endif + char *mp_basename(const char *path) { char *s; @@ -164,7 +169,34 @@ char *mp_normalize_path(void *talloc_ctx, const char *path) path = mp_path_join(talloc_ctx, cwd, path); } -#if HAVE_DOS_PATHS +#if defined(HAVE_PATHCCH) && HAVE_PATHCCH + wchar_t *pathw = mp_from_utf8(NULL, path); + wchar_t *read = pathw, *write = pathw; + wchar_t prev = '\0'; + // preserve leading double backslashes + if (read[0] == '\\' && read[1] == '\\') { + prev = '\\'; + write += 2; + read += 2; + } + wchar_t curr; + while ((curr = *read)) { + if (curr == '/') + curr = '\\'; + if (curr != '\\' || prev != '\\') + *write++ = curr; + prev = curr; + read++; + } + *write = '\0'; + size_t max_size = wcslen(pathw) + 1; + wchar_t *pathc = talloc_array(NULL, wchar_t, max_size); + HRESULT hr = PathCchCanonicalizeEx(pathc, max_size, pathw, PATHCCH_ALLOW_LONG_PATHS); + char *ret = SUCCEEDED(hr) ? mp_to_utf8(talloc_ctx, pathc) : talloc_strdup(talloc_ctx, path); + talloc_free(pathw); + talloc_free(pathc); + return ret; +#elif HAVE_DOS_PATHS return talloc_strdup(talloc_ctx, path); #else char *result = talloc_strdup(talloc_ctx, ""); |