bl/print.c
changeset 11 4a80c6053a66
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/bl/print.c	Mon Jan 30 23:32:47 2012 +0000
     1.3 @@ -0,0 +1,64 @@
     1.4 +#ifdef WIN32
     1.5 +#include <windows.h>
     1.6 +#endif
     1.7 +#include <stdlib.h>
     1.8 +#include <stdio.h>
     1.9 +#include <glib.h>
    1.10 +
    1.11 +/*
    1.12 + * Handlers for g_print() and g_printerr() which will output via
    1.13 + * WriteConsoleW when run under MS-Windows and the corresponding
    1.14 + * stream has not been re-directed. In all other cases, output
    1.15 + * via stdout and stderr respectively.
    1.16 + */
    1.17 +
    1.18 +#ifdef WIN32
    1.19 +static HANDLE bl_console=0;
    1.20 +
    1.21 +static void bl_print_handler_console(const char *string)
    1.22 +{
    1.23 +    long len;
    1.24 +    DWORD dummy;
    1.25 +    gunichar2 *string2;
    1.26 +    string2=g_utf8_to_utf16(string,-1,NULL,&len,NULL);
    1.27 +    if (string2)
    1.28 +    {
    1.29 +	WriteConsoleW(bl_console,string2,len,&dummy,NULL);
    1.30 +	g_free(string2);
    1.31 +    }
    1.32 +}
    1.33 +#endif
    1.34 +
    1.35 +static void bl_print_handler_stdout(const char *string)
    1.36 +{
    1.37 +    fputs(string,stdout);
    1.38 +}
    1.39 +
    1.40 +static void bl_print_handler_stderr(const char *string)
    1.41 +{
    1.42 +    fputs(string,stderr);
    1.43 +}
    1.44 +
    1.45 +void bl_set_print_handlers(void)
    1.46 +{
    1.47 +#ifdef WIN32
    1.48 +    DWORD dummy;
    1.49 +    if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),&dummy))
    1.50 +    {
    1.51 +	bl_console=GetStdHandle(STD_OUTPUT_HANDLE);
    1.52 +	g_set_print_handler(bl_print_handler_console);
    1.53 +    }
    1.54 +    else
    1.55 +#endif
    1.56 +	g_set_print_handler(bl_print_handler_stdout);
    1.57 +#ifdef WIN32
    1.58 +    if (GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE),&dummy))
    1.59 +    {
    1.60 +	if (!bl_console)
    1.61 +	    bl_console=GetStdHandle(STD_ERROR_HANDLE);
    1.62 +	g_set_printerr_handler(bl_print_handler_console);
    1.63 +    }
    1.64 +    else
    1.65 +#endif
    1.66 +	g_set_printerr_handler(bl_print_handler_stderr);
    1.67 +}