FreeBASIC
0.91.0
Main Page
Data Structures
Files
File List
Globals
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
9
int
fb_ConsoleLineInput
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
23
fb_PrintBufferEx
(
NULL
, 0,
FB_PRINT_FORCE_ADJUST
);
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
{
39
fb_PrintFixString
( 0,
pszDefaultQuestion
, 0 );
40
}
41
}
42
43
FB_UNLOCK
();
44
45
tmp_result =
fb_ConReadLine
(
FALSE
);
46
47
if
( addnewline ) {
48
fb_PrintVoid
( 0,
FB_PRINT_NEWLINE
);
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
56
return
fb_ErrorSetNum
(
FB_RTERROR_OUTOFMEM
);
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
66
int
fb_ConsoleLineInput
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
79
fb_PrintBufferEx
(
NULL
, 0,
FB_PRINT_FORCE_ADJUST
);
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
{
98
fb_PrintFixString
( 0,
pszDefaultQuestion
, 0 );
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
rtlib
con_lineinp.c
Generated on Thu Jan 23 2014 19:40:07 for FreeBASIC by
1.8.4