From 169f867f3c7a556e04db4aa3a3214e4f3495b0d8 Mon Sep 17 00:00:00 2001 From: James Zern Date: Mon, 20 Jun 2022 10:15:24 -0700 Subject: [PATCH] unicode.h: set console mode before using wprintf This fixes incorrect character display in the file name in e.g., stats output from cwebp.exe in visual studio builds. In mingw further changes will be needed as using %s in wfprintf() for wchar_t pointers is a Microsoft extension that doesn't seem to be supported with gcc -fms-extensions. %ls works, but will require updating format strings and wrapping concatenations with TO_W_CHAR() as visual studio will reject merging string constants of different width. https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode Change-Id: I57d24c3815f7b5aa3971ac315c65c4458258d706 --- examples/unicode.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/unicode.h b/examples/unicode.h index 61199ce7..0831e23c 100644 --- a/examples/unicode.h +++ b/examples/unicode.h @@ -16,11 +16,15 @@ #ifndef WEBP_EXAMPLES_UNICODE_H_ #define WEBP_EXAMPLES_UNICODE_H_ +#include + #if defined(_WIN32) && defined(_UNICODE) // wchar_t is used instead of TCHAR because we only perform additional work when // Unicode is enabled and because the output of CommandLineToArgvW() is wchar_t. +#include +#include #include #include #include @@ -55,8 +59,16 @@ #define WFOPEN(ARG, OPT) _wfopen((const W_CHAR*)ARG, TO_W_CHAR(OPT)) -#define WPRINTF(STR, ...) wprintf(TO_W_CHAR(STR), __VA_ARGS__) -#define WFPRINTF(STDERR, STR, ...) fwprintf(STDERR, TO_W_CHAR(STR), __VA_ARGS__) +#define WFPRINTF(STREAM, STR, ...) \ + do { \ + int prev_mode; \ + fflush(STREAM); \ + prev_mode = _setmode(_fileno(STREAM), _O_U8TEXT); \ + fwprintf(STREAM, TO_W_CHAR(STR), __VA_ARGS__); \ + fflush(STREAM); \ + (void)_setmode(_fileno(STREAM), prev_mode); \ + } while (0) +#define WPRINTF(STR, ...) WFPRINTF(stdout, STR, __VA_ARGS__) #define WSTRLEN(FILENAME) wcslen((const W_CHAR*)FILENAME) #define WSTRCMP(FILENAME, STR) wcscmp((const W_CHAR*)FILENAME, TO_W_CHAR(STR)) @@ -65,6 +77,8 @@ #else +#include + // Unicode file paths work as is on Unix platforms, and no extra work is done on // Windows either if Unicode is disabled. @@ -83,7 +97,7 @@ #define WFOPEN(ARG, OPT) fopen(ARG, OPT) #define WPRINTF(STR, ...) printf(STR, __VA_ARGS__) -#define WFPRINTF(STDERR, STR, ...) fprintf(STDERR, STR, __VA_ARGS__) +#define WFPRINTF(STREAM, STR, ...) fprintf(STREAM, STR, __VA_ARGS__) #define WSTRLEN(FILENAME) strlen(FILENAME) #define WSTRCMP(FILENAME, STR) strcmp(FILENAME, STR)