bl/print.c
author ali <ali@juiblex.co.uk>
Mon Jan 30 23:32:47 2012 +0000 (2012-01-30)
changeset 11 4a80c6053a66
permissions -rw-r--r--
Use Unicode output on MS-Windows consoles
     1 #ifdef WIN32
     2 #include <windows.h>
     3 #endif
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6 #include <glib.h>
     7 
     8 /*
     9  * Handlers for g_print() and g_printerr() which will output via
    10  * WriteConsoleW when run under MS-Windows and the corresponding
    11  * stream has not been re-directed. In all other cases, output
    12  * via stdout and stderr respectively.
    13  */
    14 
    15 #ifdef WIN32
    16 static HANDLE bl_console=0;
    17 
    18 static void bl_print_handler_console(const char *string)
    19 {
    20     long len;
    21     DWORD dummy;
    22     gunichar2 *string2;
    23     string2=g_utf8_to_utf16(string,-1,NULL,&len,NULL);
    24     if (string2)
    25     {
    26 	WriteConsoleW(bl_console,string2,len,&dummy,NULL);
    27 	g_free(string2);
    28     }
    29 }
    30 #endif
    31 
    32 static void bl_print_handler_stdout(const char *string)
    33 {
    34     fputs(string,stdout);
    35 }
    36 
    37 static void bl_print_handler_stderr(const char *string)
    38 {
    39     fputs(string,stderr);
    40 }
    41 
    42 void bl_set_print_handlers(void)
    43 {
    44 #ifdef WIN32
    45     DWORD dummy;
    46     if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),&dummy))
    47     {
    48 	bl_console=GetStdHandle(STD_OUTPUT_HANDLE);
    49 	g_set_print_handler(bl_print_handler_console);
    50     }
    51     else
    52 #endif
    53 	g_set_print_handler(bl_print_handler_stdout);
    54 #ifdef WIN32
    55     if (GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE),&dummy))
    56     {
    57 	if (!bl_console)
    58 	    bl_console=GetStdHandle(STD_ERROR_HANDLE);
    59 	g_set_printerr_handler(bl_print_handler_console);
    60     }
    61     else
    62 #endif
    63 	g_set_printerr_handler(bl_print_handler_stderr);
    64 }