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,61 +998,56 @@ char_u * os_resolve_shortcut(char_u *fname)
CoInitialize(NULL); CoInitialize(NULL);
# ifdef FEAT_MBYTE // create a link manager object and request its interface
if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) { hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
// create a link manager object and request its interface &IID_IShellLinkW, (void**)&pslw);
hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, if (hr == S_OK) {
&IID_IShellLinkW, (void**)&pslw); WCHAR **p;
if (hr == S_OK) { //TODO(jkeyes): if this returns non-zero, report the error
WCHAR **p; (void)utf8_to_utf16((char *)fname, p);
int len = 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.
hr = pslw->lpVtbl->QueryInterface( hr = pslw->lpVtbl->QueryInterface(
pslw, &IID_IPersistFile, (void**)&ppf); pslw, &IID_IPersistFile, (void**)&ppf);
if (hr != S_OK) { if (hr != S_OK) {
goto shortcut_errorw; goto shortcut_errorw;
} }
// "load" the name and resolve the link // "load" the name and resolve the link
hr = ppf->lpVtbl->Load(ppf, p, STGM_READ); hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
if (hr != S_OK) { if (hr != S_OK) {
goto shortcut_errorw; goto shortcut_errorw;
} }
# if 0 // This makes Vim wait a long time if the target does not exist. # if 0 // This makes Vim wait a long time if the target does not exist.
hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI); hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
if (hr != S_OK) { if (hr != S_OK) {
goto shortcut_errorw; goto shortcut_errorw;
} }
# endif # endif
// Get the path to the link target. // Get the path to the link target.
ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR)); ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0); hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
if (hr == S_OK && wsz[0] != NUL) { if (hr == S_OK && wsz[0] != NUL) {
rfname = utf16_to_enc(wsz, NULL); rfname = utf16_to_enc(wsz, NULL);
} }
shortcut_errorw: shortcut_errorw:
xfree(p); xfree(p);
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;