[x265] CLI: fix Unicode output in Windows for old mingw-w64

Mateusz mateuszb at poczta.onet.pl
Fri Apr 29 10:23:23 CEST 2016


-------------- next part --------------
# HG changeset patch
# User Ma0 <mateuszb at poczta.onet.pl>
# Date 1461917775 -7200
#      Fri Apr 29 10:16:15 2016 +0200
# Node ID 6a5c3dbd556d9dcc320439a9c601f06523ec79fb
# Parent  00ea3784bd36c164c5f799c998d7a09f2cb244bf
CLI: fix Unicode output in Windows for old mingw-w64

diff -r 00ea3784bd36 -r 6a5c3dbd556d source/common/common.cpp
--- a/source/common/common.cpp	Thu Apr 28 09:59:30 2016 +0200
+++ b/source/common/common.cpp	Fri Apr 29 10:16:15 2016 +0200
@@ -181,13 +181,17 @@
     vsnprintf(buffer + p, bufferSize - p, fmt, arg);
     va_end(arg);
 
-    wchar_t buf_utf16[bufferSize];
-    MultiByteToWideChar(CP_UTF8, 0, buffer, -1, buf_utf16, sizeof(buf_utf16)/sizeof(wchar_t));
-    fflush(stderr);
-    int oldmode = _setmode(_fileno(stderr), _O_U8TEXT);
-    fwprintf(stderr, L"%ls", buf_utf16);               // WARNING: due to bug in msvcrt.dll fputws doesn't work in mingw/gcc 
-    fflush(stderr);
-    _setmode(_fileno(stderr), oldmode);
+    HANDLE console = GetStdHandle(STD_ERROR_HANDLE);
+    DWORD mode;
+    if (GetConsoleMode(console, &mode))
+    {
+        wchar_t buf_utf16[bufferSize];
+        int length_utf16 = MultiByteToWideChar(CP_UTF8, 0, buffer, -1, buf_utf16, sizeof(buf_utf16)/sizeof(wchar_t)) - 1;
+        if (length_utf16 > 0)
+            WriteConsoleW(console, buf_utf16, length_utf16, &mode, NULL);
+    }
+    else
+        fputs(buffer, stderr);
 }
 
 FILE* x265_fopen(const char* fileName, const char* mode)


More information about the x265-devel mailing list