bl/print.c
author ali <ali@juiblex.co.uk>
Sat May 25 20:29:36 2013 +0100 (2013-05-25)
changeset 47 7522c36859d0
permissions -rw-r--r--
Break check_for_spaced_emdash() and check_for_spaced_dash() out
     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 }