FreeBASIC  0.91.0
con_lineinp.c
Go to the documentation of this file.
1 /* console line input function */
2 
3 #include "fb.h"
4 
5 static const char *pszDefaultQuestion = "? ";
6 
7 #if defined( HOST_WIN32 ) || defined( HOST_DOS ) || defined( HOST_LINUX )
8 
10  (
11  FBSTRING *text,
12  void *dst,
13  ssize_t dst_len,
14  int fillrem,
15  int addquestion,
16  int addnewline
17  )
18 {
19  FBSTRING *tmp_result;
20 
21  FB_LOCK();
22 
24 
25  if( text != NULL )
26  {
27  if( text->data != NULL )
28  {
29  fb_PrintString( 0, text, 0 );
30  }
31  /* del if temp */
32  else
33  {
34  fb_hStrDelTemp( text );
35  }
36 
37  if( addquestion != FB_FALSE )
38  {
40  }
41  }
42 
43  FB_UNLOCK();
44 
45  tmp_result = fb_ConReadLine( FALSE );
46 
47  if( addnewline ) {
49  }
50 
51  if( tmp_result!=NULL ) {
52  fb_StrAssign( dst, dst_len, tmp_result, -1, fillrem );
53  return fb_ErrorSetNum( FB_RTERROR_OK );
54  }
55 
57 }
58 
59 #else
60 
61 static char *hWrapper( char *buffer, size_t count, FILE *fp )
62 {
63  return fb_ReadString( buffer, count, fp );
64 }
65 
67  (
68  FBSTRING *text,
69  void *dst,
70  ssize_t dst_len,
71  int fillrem,
72  int addquestion,
73  int addnewline
74  )
75 {
76  int res, old_x, old_y;
77  size_t len;
78 
80  fb_GetXY( &old_x, &old_y );
81 
82  FB_LOCK();
83 
84  if( text != NULL )
85  {
86  if( text->data != NULL )
87  {
88  fb_PrintString( 0, text, 0 );
89  }
90  /* del if temp */
91  else
92  {
93  fb_hStrDelTemp( text );
94  }
95 
96  if( addquestion != FB_FALSE )
97  {
99  }
100  }
101 
102  {
103  /* create temporary string */
104  FBSTRING str_result = { 0 };
105 
106  res = fb_DevFileReadLineDumb( stdin, &str_result, hWrapper );
107 
108  len = FB_STRSIZE(&str_result);
109 
110  /* We have to handle the NEWLINE stuff here because we *REQUIRE*
111  * the *COMPLETE* temporary input string for the correct position
112  * adjustment. */
113  if( !addnewline ) {
114  /* This is the easy and dumb method to do the position adjustment.
115  * The problem is that it doesn't take TAB's into account. */
116  int cols, rows, old_y;
117 
118  fb_GetSize( &cols, &rows );
119  fb_GetXY( NULL, &old_y );
120 
121  old_x += len - 1;
122  old_x %= cols;
123  old_x += 1;
124  old_y -= 1;
125 
126  fb_Locate( old_y, old_x, -1, 0, 0 );
127  }
128 
129 
130  /* add contents of tempporary string to result buffer */
131  fb_StrAssign( dst, dst_len, (void *)&str_result, -1, fillrem );
132 
133  fb_StrDelete( &str_result );
134  }
135 
136  FB_UNLOCK();
137 
138  return res;
139 }
140 
141 #endif