-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcon_lineinp.bas
114 lines (81 loc) · 2.95 KB
/
con_lineinp.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/' console line input function '/
#include "fb.bi"
dim shared as ZString Ptr pszDefaultQuestion = sadd("? ")
extern "C"
#if defined( HOST_WIN32 ) or defined( HOST_DOS ) or defined( HOST_LINUX )
function fb_ConsoleLineInput( text as FBSTRING ptr, dst as any ptr, dst_len as ssize_t, fillrem as long, addquestion as long, addnewline as long ) as long
dim as FBSTRING ptr tmp_result
FB_LOCK()
fb_PrintBufferEx( NULL, 0, FB_PRINT_FORCE_ADJUST )
if ( text <> NULL ) then
if ( text->data <> NULL ) then
fb_PrintString( 0, text, 0 )
/' del if temp '/
else
fb_hStrDelTemp( text )
end if
if ( addquestion <> FB_FALSE ) then
fb_PrintFixString( 0, pszDefaultQuestion, 0 )
end if
end if
FB_UNLOCK()
tmp_result = fb_ConReadLine( FALSE )
if ( addnewline <> NULL ) then
fb_PrintVoid( 0, FB_PRINT_NEWLINE )
end if
if ( tmp_result <> NULL ) then
fb_StrAssign( dst, dst_len, tmp_result, -1, fillrem )
return fb_ErrorSetNum( FB_RTERROR_OK )
end if
return fb_ErrorSetNum( FB_RTERROR_OUTOFMEM )
end function
#else
private function hWrapper( buffer as ubyte ptr, count as size_t, fp as FILE ptr ) as ubyte ptr
return fb_ReadString( buffer, count, fp )
end function
function fb_ConsoleLineInput( text as FBSTRING ptr, dst as any ptr, dst_len as ssize_t, fillrem as long, addquestion as long, addnewline as long ) as long
dim as long res, old_x, old_y
dim as size_t _len
fb_PrintBufferEx( NULL, 0, FB_PRINT_FORCE_ADJUST )
fb_GetXY( @old_x, @old_y )
FB_LOCK()
if ( text <> NULL ) then
if( text->data <> NULL ) then
fb_PrintString( 0, text, 0 )
/' del if temp '/
else
fb_hStrDelTemp( text )
end if
if ( addquestion <> FB_FALSE ) then
fb_PrintFixString( 0, pszDefaultQuestion, 0 )
end if
end if
scope
/' create temporary string '/
dim as FBSTRING str_result = ( 0 )
res = fb_DevFileReadLineDumb( stdin, @str_result, hWrapper )
_len = FB_STRSIZE(@str_result)
/' We have to handle the NEWLINE stuff here because we *REQUIRE*
* the *COMPLETE* temporary input string for the correct position
* adjustment. '/
if ( addnewline = 0 ) then
/' This is the easy and dumb method to do the position adjustment.
* The problem is that it doesn't take TAB's into account. '/
dim as long cols, rows, old_y
fb_GetSize( @cols, @rows )
fb_GetXY( NULL, &old_y )
old_x += _len - 1
old_x mod= cols
old_x += 1
old_y -= 1
fb_Locate( old_y, old_x, -1, 0, 0 )
end if
/' add contents of tempporary string to result buffer '/
fb_StrAssign( dst, dst_len, cast(any ptr, @str_result), -1, fillrem )
fb_StrDelete( @str_result )
end scope
FB_UNLOCK()
return res
end function
#endif
end extern