mpg123: Import upstream release 1.32.2.
[wine.git] / libs / mpg123 / src / compat / wpathconv.h
blob167440bc33431cdd42c326e11f02b0c36a356b48
1 /*
2 wpathconv: static functions for windows file path conversions
4 This file is intended to be included in libcompat sources for internal use.
5 It is separated out to be able to split off the dlopen functions into a
6 separate libcompat. It is just a code fragment.
8 copyright 2007-2019 by the mpg123 project - free software under the terms of the LGPL 2.1
9 see COPYING and AUTHORS files in distribution or http://mpg123.org
10 initially written by JonY
13 #ifndef WINDOWS_UWP
15 #ifdef WANT_WIN32_UNICODE
17 /* Convert unix UTF-8 (or ASCII) paths to Windows wide character paths. */
18 static wchar_t* u2wpath(const char *upath)
20 wchar_t* wpath, *p;
21 if(!upath || INT123_win32_utf8_wide(upath, &wpath, NULL) < 1)
22 return NULL;
23 for(p=wpath; *p; ++p)
24 if(*p == L'/')
25 *p = L'\\';
26 return wpath;
29 #ifndef HIDE_w2upath
30 /* Convert Windows wide character paths to unix UTF-8. */
31 static char* w2upath(const wchar_t *wpath)
33 char* upath, *p;
34 if(!wpath || INT123_win32_wide_utf8(wpath, &upath, NULL) < 1)
35 return NULL;
36 for(p=upath; *p; ++p)
37 if(*p == '\\')
38 *p = '/';
39 return upath;
41 #endif
43 /* An absolute path that is too long and not already marked with
44 \\?\ can be marked as a long one and still work. */
45 static int wpath_need_elongation(wchar_t *wpath)
47 if( wpath && !PathIsRelativeW(wpath)
48 && wcslen(wpath) > MAX_PATH-1
49 && wcsncmp(L"\\\\?\\", wpath, 4) )
50 return 1;
51 else
52 return 0;
55 /* Take any wide windows path and turn it into a path that is allowed
56 to be longer than MAX_PATH, if it is not already. */
57 static wchar_t* wlongpath(wchar_t *wpath)
59 size_t len, plen;
60 const wchar_t *prefix = L"";
61 wchar_t *wlpath = NULL;
62 if(!wpath)
63 return NULL;
65 /* Absolute paths that do not start with \\?\ get that prepended
66 to allow them being long. */
67 if(!PathIsRelativeW(wpath) && wcsncmp(L"\\\\?\\", wpath, 4))
69 if(wcslen(wpath) >= 2 && PathIsUNCW(wpath))
71 /* \\server\path -> \\?\UNC\server\path */
72 prefix = L"\\\\?\\UNC";
73 ++wpath; /* Skip the first \. */
75 else /* c:\some/path -> \\?\c:\some\path */
76 prefix = L"\\\\?\\";
78 plen = wcslen(prefix);
79 len = plen + wcslen(wpath);
80 wlpath = malloc(len+1*sizeof(wchar_t));
81 if(wlpath)
83 /* Brute force memory copying, swprintf is too dandy. */
84 memcpy(wlpath, prefix, sizeof(wchar_t)*plen);
85 memcpy(wlpath+plen, wpath, sizeof(wchar_t)*(len-plen));
86 wlpath[len] = 0;
88 return wlpath;
91 /* Convert unix path to wide windows path, optionally marking
92 it as long path if necessary. */
93 static wchar_t* u2wlongpath(const char *upath)
95 wchar_t *wpath = NULL;
96 wchar_t *wlpath = NULL;
97 wpath = u2wpath(upath);
98 if(wpath_need_elongation(wpath))
100 wlpath = wlongpath(wpath);
101 free(wpath);
102 wpath = wlpath;
104 return wpath;
107 #endif
109 #else
111 static wchar_t* u2wlongpath(const char *upath)
113 wchar_t* wpath, *p;
114 if (!upath || INT123_win32_utf8_wide(upath, &wpath, NULL) < 1)
115 return NULL;
116 for (p = wpath; *p; ++p)
117 if (*p == L'/')
118 *p = L'\\';
119 return wpath;
122 #endif