os_resolve_shortcut: cleanup

This commit is contained in:
Justin M. Keyes 2016-05-29 05:18:05 -04:00
parent 618aad3811
commit d96b359410

View File

@ -935,41 +935,40 @@ bool os_fileid_equal_fileinfo(const FileID *file_id,
# define CP_UTF8 65001 /* magic number from winnls.h */ # define CP_UTF8 65001 /* magic number from winnls.h */
#endif #endif
static int utf8_to_utf16(const char *path, const WCHAR **pathw) static int utf8_to_utf16(const char *str, WCHAR **strw)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
ssize_t buf_sz = 0, path_len, pathw_len = 0; ssize_t wchar_len = 0;
// Compute the length needed to store the converted widechar string. // Compute the length needed to store the converted widechar string.
pathw_len = MultiByteToWideChar(CP_UTF8, wchar_len = MultiByteToWideChar(CP_UTF8,
0, // dwFlags: must be 0 for utf8 0, // dwFlags: must be 0 for utf8
path, // lpMultiByteStr: string to convert str, // lpMultiByteStr: string to convert
-1, -1, // -1 => process up to NUL
NULL, // lpWideCharStr: converted string NULL, // lpWideCharStr: converted string
0); // 0 => return length, don't convert 0); // 0 => return length, don't convert
if (pathw_len == 0) { if (wchar_len == 0) {
return GetLastError(); return GetLastError();
} }
buf_sz += pathw_len * sizeof(WCHAR); ssize_t buf_sz = wchar_len * sizeof(WCHAR);
if (buf_sz == 0) { if (buf_sz == 0) {
*pathw = NULL; *strw = NULL;
return 0; return 0;
} }
char* buf = xmalloc(buf_sz); char* buf = xmalloc(buf_sz);
char* pos = buf; char* pos = buf;
DWORD r = MultiByteToWideChar(CP_UTF8, int r = MultiByteToWideChar(CP_UTF8,
0, 0,
path, str,
-1, -1,
(WCHAR*) pos, (WCHAR*) pos,
pathw_len); wchar_len);
assert(r == (DWORD) pathw_len); assert(r == wchar_len);
*pathw = (WCHAR*) pos; *strw = (WCHAR*) pos;
pos += r * sizeof(WCHAR);
return 0; return 0;
} }
@ -982,12 +981,10 @@ char_u * os_resolve_shortcut(char_u *fname)
HRESULT hr; HRESULT hr;
IPersistFile *ppf = NULL; IPersistFile *ppf = NULL;
OLECHAR wsz[MAX_PATH]; OLECHAR wsz[MAX_PATH];
WIN32_FIND_DATA ffd; // we get those free of charge
CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
char_u *rfname = NULL; char_u *rfname = NULL;
int len; int len;
IShellLinkW *pslw = NULL; IShellLinkW *pslw = NULL;
WIN32_FIND_DATAW ffdw; // we get those free of charge WIN32_FIND_DATAW ffdw;
// Check if the file name ends in ".lnk". Avoid calling CoCreateInstance(), // Check if the file name ends in ".lnk". Avoid calling CoCreateInstance(),
// it's quite slow. // it's quite slow.
@ -1001,14 +998,13 @@ char_u * os_resolve_shortcut(char_u *fname)
CoInitialize(NULL); CoInitialize(NULL);
# ifdef FEAT_MBYTE
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) {
// create a link manager object and request its interface // create a link manager object and request its interface
hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
&IID_IShellLinkW, (void**)&pslw); &IID_IShellLinkW, (void**)&pslw);
if (hr == S_OK) { if (hr == S_OK) {
WCHAR **p; WCHAR **p;
int len = utf8_to_utf16((char *)fname, p); //TODO(jkeyes): if this returns non-zero, report the error
(void)utf8_to_utf16((char *)fname, p);
if (p != NULL) { if (p != NULL) {
// Get a pointer to the IPersistFile interface. // Get a pointer to the IPersistFile interface.
@ -1043,19 +1039,15 @@ shortcut_errorw:
goto shortcut_end; goto shortcut_end;
} }
} }
}
# endif
shortcut_end: shortcut_end:
// Release all interface pointers (both belong to the same object) // Release all interface pointers (both belong to the same object)
if (ppf != NULL) { if (ppf != NULL) {
ppf->lpVtbl->Release(ppf); ppf->lpVtbl->Release(ppf);
} }
# ifdef FEAT_MBYTE
if (pslw != NULL) { if (pslw != NULL) {
pslw->lpVtbl->Release(pslw); pslw->lpVtbl->Release(pslw);
} }
# endif
CoUninitialize(); CoUninitialize();
return rfname; return rfname;