diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2016-05-29 06:09:49 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2016-07-30 15:28:13 -0400 |
commit | 23aadb69486fbfc6739dfd8861278f99315023ba (patch) | |
tree | 8ad17bd523beaac071fc966525e800c0b7fde893 | |
parent | d96b359410ef59683575ee00f209860f7a879395 (diff) | |
download | rneovim-23aadb69486fbfc6739dfd8861278f99315023ba.tar.gz rneovim-23aadb69486fbfc6739dfd8861278f99315023ba.tar.bz2 rneovim-23aadb69486fbfc6739dfd8861278f99315023ba.zip |
utf16_to_utf8
-rw-r--r-- | src/nvim/os/fs.c | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 4833ef868e..ce43eb9422 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -973,6 +973,41 @@ static int utf8_to_utf16(const char *str, WCHAR **strw) return 0; } +static int utf16_to_utf8(const WCHAR *strw, char **str) + FUNC_ATTR_NONNULL_ALL +{ + // Compute the space required to store the string as UTF-8. + ssize_t utf8_len = WideCharToMultiByte(CP_UTF8, + 0, + strw, + -1, + NULL, + 0, + NULL, + NULL); + if (utf8_len == 0) { + return GetLastError(); + } + + ssize_t buf_sz = utf8_len * sizeof(char); + char* buf = xmalloc(buf_sz); + char* pos = buf; + + // Convert string to UTF-8. + int r = WideCharToMultiByte(CP_UTF8, + 0, + strw, + -1, + (LPSTR*) pos, + utf8_len, + NULL, + NULL); + assert(r == utf8_len); + *str = pos; + + return 0; +} + /// When "fname" is the name of a shortcut (*.lnk) resolve the file it points /// to and return that name in allocated memory. /// Otherwise NULL is returned. @@ -1002,9 +1037,9 @@ char_u * os_resolve_shortcut(char_u *fname) hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, (void**)&pslw); if (hr == S_OK) { - WCHAR **p; + WCHAR *p; //TODO(jkeyes): if this returns non-zero, report the error - (void)utf8_to_utf16((char *)fname, p); + (void)utf8_to_utf16((char *)fname, &p); if (p != NULL) { // Get a pointer to the IPersistFile interface. @@ -1031,7 +1066,8 @@ char_u * os_resolve_shortcut(char_u *fname) ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR)); hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0); if (hr == S_OK && wsz[0] != NUL) { - rfname = utf16_to_enc(wsz, NULL); + //TODO(jkeyes): if this returns non-zero, report the error + (void)utf16_to_utf8(wsz, &rfname); } shortcut_errorw: |