FreeBASIC  0.91.0
io_readstr.c
Go to the documentation of this file.
1 /* console line input function */
2 
3 #include "../fb.h"
4 #include "fb_private_console.h"
5 
6 char *fb_ConsoleReadStr( char *buffer, ssize_t len )
7 {
8  int k, x, y, cols;
9  ssize_t pos = 0;
10  char ch[2] = { 0, '\0' };
11 
12  if (!__fb_con.inited)
13  return fgets(buffer, len, stdin);
14 
15  fb_ConsoleGetSize(&cols, NULL);
16 
17  do {
18  while( ((k = fb_hGetCh(TRUE)) == -1) || (k > 0xFF) )
19  fb_Delay( 10 );
20 
21  fb_ConsoleGetXY(&x, &y);
22 
23  if (k == 8) {
24  if (pos > 0) {
25  x--;
26  if (x <= 0) {
27  x = cols;
28  y--;
29  if (y <= 0)
30  x = y = 1;
31  }
32  fb_hTermOut(SEQ_LOCATE, x-1, y-1);
34  pos--;
35  }
36  } else if (k != '\t') {
37  if (pos < len - 1) {
38  buffer[pos++] = ch[0] = k;
39  fb_ConsolePrintBuffer(ch, 0);
40  if (x == cols)
41  fputc( '\n', stdout );
42  }
43  }
44  } while (k != '\r');
45 
46  fputc( '\n', stdout );
47  buffer[pos] = '\0';
48 
49  return buffer;
50 }