Permalink
Browse files

audtool: Print UTF-8 correctly on Windows. Closes: #823.

  • Loading branch information...
jlindgren90 committed Oct 16, 2018
1 parent 0b1b60e commit dec82a8d6a23fec13cd92e9761ad63bbe719eb25
Showing with 37 additions and 0 deletions.
  1. +37 −0 src/audtool/main.c
@@ -23,6 +23,10 @@
#include <string.h>
#include <locale.h>

#ifdef _WIN32
#include <windows.h>
#endif

#include "audtool.h"

const struct commandhandler handlers[] =
@@ -182,6 +186,35 @@ static void audtool_connect (int instance)
atexit (audtool_disconnect);
}

#ifdef _WIN32
static void print_utf8 (const char * str)
{
HANDLE h = GetStdHandle (STD_OUTPUT_HANDLE);
DWORD mode;

if (h != INVALID_HANDLE_VALUE && GetConsoleMode (h, & mode))
{
// stdout is the console, which needs special handling to display
// non-ASCII characters correctly (in 2018, the MS runtime *still*
// doesn't handle UTF-8 reliably).
glong n_converted;
gunichar2 * utf16 = g_utf8_to_utf16 (str, -1, NULL, & n_converted, NULL);

if (utf16)
{
DWORD n_written;
WriteConsoleW (h, utf16, n_converted, & n_written, NULL);
g_free (utf16);
}
}
else
{
// fputs() works fine when stdout is redirected.
fputs (str, stdout);
}
}
#endif // _WIN32

int main (int argc, char * * argv)
{
int instance = 1;
@@ -193,6 +226,10 @@ int main (int argc, char * * argv)
g_type_init();
#endif

#ifdef _WIN32
g_set_print_handler (print_utf8);
#endif

// parse instance number (must come first)
if (argc >= 2 && argv[1][0] == '-' && argv[1][1] >= '1' && argv[1][1] <= '9' && ! argv[1][2])
{

0 comments on commit dec82a8

Please sign in to comment.