FreeBASIC  0.91.0
io_printbuff.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 CTRL_ALWAYS 0x0800D101
7 #define ENTER_UTF8 "\e%G"
8 #define EXIT_UTF8 "\e%@"
9 
10 void fb_ConsolePrintBufferEx( const void *buffer, size_t len, int mask )
11 {
12  size_t avail, avail_len;
13  const unsigned char *cbuffer = (const unsigned char *) buffer;
14  unsigned int c;
15 
16  if (!__fb_con.inited) {
17  fwrite(buffer, len, 1, stdout);
18  fflush(stdout);
19  return;
20  }
21 
22  BG_LOCK( );
24  BG_UNLOCK( );
25 
26  /* ToDo: handle scrolling for internal characters/attributes buffer? */
27  avail = (__fb_con.w * __fb_con.h) - (((__fb_con.cur_y - 1) * __fb_con.w) + __fb_con.cur_x - 1);
28  avail_len = len;
29  if (avail < avail_len)
30  avail_len = avail;
31  memcpy(__fb_con.char_buffer + ((__fb_con.cur_y - 1) * __fb_con.w) + __fb_con.cur_x - 1, buffer, avail_len);
32  memset(__fb_con.attr_buffer + ((__fb_con.cur_y - 1) * __fb_con.w) + __fb_con.cur_x - 1, __fb_con.fg_color | (__fb_con.bg_color << 4), avail_len);
33 
34  for (; len; len--, cbuffer++) {
35  c = *cbuffer;
36  if( c == 0 )
37  c = 32;
38 
39  if (c < 32) {
40  if ((CTRL_ALWAYS >> c) & 0x1) {
41  /* This character can't be printed, we must use unicode
42  * Enter UTF-8 and start constructing 0xF000 code
43  */
44  fputs( ENTER_UTF8 "\xEF\x80", stdout );
45  /* Set the last 6 bits */
46  fputc( c | 0x80, stdout );
47  /* Escape UTF-8 */
48  fputs( EXIT_UTF8, stdout );
49  } else
50  fputc( c, stdout );
51  } else
52  fputc( c, stdout );
53 
54  __fb_con.cur_x++;
55  if ((c == 10) || (__fb_con.cur_x >= __fb_con.w)) {
56  __fb_con.cur_x = 1;
57  __fb_con.cur_y++;
58  if (__fb_con.cur_y > __fb_con.h)
59  __fb_con.cur_y = __fb_con.h;
60  }
61  }
62 
63  fflush( stdout );
64 }
65 
66 void fb_ConsolePrintBuffer( const char *buffer, int mask )
67 {
68  return fb_ConsolePrintBufferEx( buffer, strlen(buffer), mask );
69 }