win: os_get_hostname() #5416 (#6413)

This commit is contained in:
Justin M. Keyes
2017-04-07 19:46:33 +02:00
committed by GitHub
parent 1813076c44
commit 13352c00f1
4 changed files with 63 additions and 29 deletions

View File

@@ -1304,6 +1304,7 @@ static int utf_strnicmp(const char_u *s1, const char_u *s2, size_t n1,
# define CP_UTF8 65001 /* magic number from winnls.h */
#endif
/// Reassigns `strw` to a new, allocated pointer to a UTF16 string.
int utf8_to_utf16(const char *str, WCHAR **strw)
FUNC_ATTR_NONNULL_ALL
{
@@ -1345,40 +1346,40 @@ int utf8_to_utf16(const char *str, WCHAR **strw)
return 0;
}
/// Reassigns `str` to a new, allocated pointer to a UTF8 string.
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);
DWORD 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;
*str = xmalloc(utf8_len);
// Convert string to UTF-8.
int r = WideCharToMultiByte(CP_UTF8,
0,
strw,
-1,
pos,
utf8_len,
NULL,
NULL);
assert(r == utf8_len);
if (r != utf8_len) {
EMSG2("WideCharToMultiByte failed: %d", r);
// Convert to UTF-8.
utf8_len = WideCharToMultiByte(CP_UTF8,
0,
strw,
-1,
*str,
utf8_len,
NULL,
NULL);
if (utf8_len == 0) {
free(*str);
*str = NULL;
return GetLastError();
}
*str = pos;
(*str)[utf8_len] = '\0';
return 0;
}