FreeBASIC  0.91.0
io_printbuff_wstr.c
Go to the documentation of this file.
1 /* low-level print to console function */
2 
3 #include "../fb.h"
4 #include "fb_private_console.h"
5 
6 #define ENTER_UTF8 "\e%G"
7 #define EXIT_UTF8 "\e%@"
8 
9 void fb_ConsolePrintBufferWstrEx( const FB_WCHAR *buffer, size_t chars, int mask )
10 {
11  size_t avail, avail_len;
12  char *temp;
13 
14  if( !__fb_con.inited )
15  {
16  /* !!!FIXME!!! is this ok or should it be converted to UTF-8 too? */
17  fwrite( buffer, sizeof( FB_WCHAR ), chars, stdout );
18  fflush( stdout );
19  return;
20  }
21 
22  temp = alloca( chars * 4 + 1 );
23 
24  BG_LOCK( );
26  BG_UNLOCK( );
27 
28  /* ToDo: handle scrolling for internal characters/attributes buffer? */
29  avail = (__fb_con.w * __fb_con.h) - (((__fb_con.cur_y - 1) * __fb_con.w) + __fb_con.cur_x - 1);
30  avail_len = chars;
31  if (avail < avail_len)
32  avail_len = avail;
33 
34  /* !!!FIXME!!! to support unicode the char_buffer would have to be a wchar_t,
35  slowing down non-unicode printing.. */
36  fb_wstr_ConvToA( temp, buffer, avail_len );
37 
38  memcpy( __fb_con.char_buffer + ((__fb_con.cur_y - 1) * __fb_con.w) + __fb_con.cur_x - 1,
39  temp,
40  avail_len );
41 
42  memset( __fb_con.attr_buffer + ((__fb_con.cur_y - 1) * __fb_con.w) + __fb_con.cur_x - 1,
43  __fb_con.fg_color | (__fb_con.bg_color << 4),
44  avail_len );
45 
46  /* convert wchar_t to UTF-8 */
47  ssize_t bytes;
48 
49  fb_WCharToUTF( FB_FILE_ENCOD_UTF8, buffer, chars, temp, &bytes );
50  /* add null-term */
51  temp[bytes] = '\0';
52 
53  fputs( ENTER_UTF8, stdout );
54 
55  fputs( temp, stdout );
56 
57  fputs( EXIT_UTF8, stdout );
58 
59  /* update x and y coordinates.. */
60  for( ; chars; chars--, buffer++ )
61  {
62  ++__fb_con.cur_x;
63  if( (*buffer == _LC('\n')) || (__fb_con.cur_x >= __fb_con.w) )
64  {
65  __fb_con.cur_x = 1;
66  ++__fb_con.cur_y;
67  if( __fb_con.cur_y > __fb_con.h )
68  __fb_con.cur_y = __fb_con.h;
69  }
70  }
71 
72  fflush( stdout );
73 }
74 
75 void fb_ConsolePrintBufferWstr( const FB_WCHAR *buffer, int mask )
76 {
77  return fb_ConsolePrintBufferWstrEx( buffer, fb_wstr_Len( buffer ), mask );
78 }