Skip to content

Commit

Permalink
audtool: Print UTF-8 correctly on Windows. Closes: #823.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlindgren90 committed Oct 16, 2018
1 parent 0b1b60e commit dec82a8
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/audtool/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include <string.h>
#include <locale.h>

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

#include "audtool.h"

const struct commandhandler handlers[] =
Expand Down Expand Up @@ -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;
Expand All @@ -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])
{
Expand Down

0 comments on commit dec82a8

Please sign in to comment.