From f01eb59dc41a9bde62c6fdab4b9e6f5491985ca0 Mon Sep 17 00:00:00 2001 From: Kenichi Maruyama <25722430+kemaruya@users.noreply.github.com> Date: Wed, 1 Mar 2023 14:07:51 +0900 Subject: [PATCH] Update win32-utf8.c Reflects mgkuhn's comment. --- contrib/win32/win32compat/win32-utf8.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/contrib/win32/win32compat/win32-utf8.c b/contrib/win32/win32compat/win32-utf8.c index cef67337b..30d9bc5d1 100644 --- a/contrib/win32/win32compat/win32-utf8.c +++ b/contrib/win32/win32compat/win32-utf8.c @@ -11,7 +11,7 @@ UINT g_previous_codepage = 0; void -mresetlocale(void); +mrestorelocale(void); int vfmprintf(FILE *stream, const char *fmt, va_list ap) @@ -100,19 +100,22 @@ asmprintf(char **outp, size_t sz, int *written, const char *fmt, ...) void msetlocale(void) { - // allow console output of unicode characters - g_previous_codepage = SetConsoleOutputCP(CP_UTF8); + // save previous codepage + g_previous_codepage = GetConsoleOutputCP(); - // register reset function at exit - atexit(mresetlocale); + // allow console output of unicode characters + SetConsoleOutputCP(CP_UTF8); + + // register a restore function at exit + atexit(mrestorelocale); } void -mresetlocale(void) +mrestorelocale(void) { if( 0 != g_previous_codepage ) { - // restore console output codepage for previous one + // restore console output codepage to previous one SetConsoleOutputCP(g_previous_codepage); } } \ No newline at end of file