Commit 872d8f2f authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Win32: simplify and fix a warning

parent a5fded6e
...@@ -57,7 +57,9 @@ int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap ) ...@@ -57,7 +57,9 @@ int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
return vfprintf (stream, fmt, ap); return vfprintf (stream, fmt, ap);
#else #else
char *str; char *str;
int res; int res = vasprintf (&str, fmt, ap);
if (unlikely(res == -1))
return -1;
# ifndef UNDER_CE # ifndef UNDER_CE
/* Writing to the console is a lot of fun on Microsoft Windows. /* Writing to the console is a lot of fun on Microsoft Windows.
...@@ -67,44 +69,33 @@ int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap ) ...@@ -67,44 +69,33 @@ int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
int fd = _fileno (stream); int fd = _fileno (stream);
if (likely(fd != -1) && _isatty (fd)) if (likely(fd != -1) && _isatty (fd))
{ {
res = vasprintf (&str, fmt, ap); wchar_t *wide = ToWide (str);
if (unlikely(res == -1))
return -1;
size_t wlen = 2 * (res + 1);
wchar_t *wide = malloc (wlen);
if (likely(wide != NULL)) if (likely(wide != NULL))
{ {
wlen = MultiByteToWideChar (CP_UTF8, 0, str, res + 1, wide, wlen); HANDLE h = (HANDLE)((uintptr_t)_get_osfhandle (fd));
if (wlen > 0) DWORD out;
{
HANDLE h = (HANDLE)(intptr_t)_get_osfhandle (fd); /* XXX: It is not clear whether WriteConsole() wants the number of
DWORD out; * Unicode characters or the size of the wchar_t array. */
WriteConsoleW (h, wide, wcslen (wide), &out, NULL);
WriteConsoleW (h, wide, wlen - 1, &out, NULL);
}
else
res = -1;
free (wide); free (wide);
} }
else else
res = -1; res = -1;
free (str);
return res;
} }
else
# endif # endif
{
res = vasprintf (&str, fmt, ap); char *ansi = ToANSI (str);
if (unlikely(res == -1)) if (ansi != NULL)
return -1; {
fputs (ansi, stream);
char *ansi = ToLocaleDup (str); free (ansi);
}
else
res = -1;
}
free (str); free (str);
if (ansi == NULL)
return -1;
fputs (ansi, stream);
free (ansi);
return res; return res;
#endif #endif
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment