FreeBASIC
0.91.0
Main Page
Data Structures
Files
File List
Globals
file_input_tok_wstr.c
Go to the documentation of this file.
1
/* input function core */
2
3
#include "
fb.h
"
4
5
static
FB_WCHAR
hReadChar
(
FB_INPUTCTX
*
ctx
)
6
{
7
/* device? */
8
if
(
FB_HANDLE_USED
(ctx->
handle
) )
9
{
10
int
res;
11
FB_WCHAR
c;
12
13
size_t
len;
14
res =
fb_FileGetDataEx
( ctx->
handle
, 0, &c, 1, &len,
FALSE
,
TRUE
);
15
if
( (res !=
FB_RTERROR_OK
) || (len == 0) )
16
return
FB_WEOF
;
17
18
return
c;
19
}
20
/* console.. */
21
else
22
{
23
if
( ctx->
index
>=
FB_STRSIZE
( &ctx->
str
.
len
) )
24
return
FB_WEOF
;
25
else
26
return
(
unsigned
char
)ctx->
str
.
data
[ctx->
index
++];
27
}
28
29
}
30
31
static
int
hUnreadChar
(
FB_INPUTCTX
*
ctx
,
FB_WCHAR
c )
32
{
33
/* device? */
34
if
(
FB_HANDLE_USED
(ctx->
handle
) )
35
{
36
return
fb_FilePutBackWstrEx
( ctx->
handle
, &c, 1 );
37
}
38
/* console .. */
39
else
40
{
41
if
( ctx->
index
<= 0 )
42
return
0;
43
else
44
{
45
--ctx->
index
;
46
return
1;
47
}
48
}
49
}
50
51
static
FB_WCHAR
hSkipWhiteSpc
(
FB_INPUTCTX
*
ctx
)
52
{
53
FB_WCHAR
c;
54
55
/* skip white space */
56
do
57
{
58
c =
hReadChar
( ctx );
59
if
( c ==
FB_WEOF
)
60
break
;
61
}
while
( (c ==
_LC
(
' '
)) || (c ==
_LC
(
'\t'
)) );
62
63
return
c;
64
}
65
66
static
void
hSkipDelimiter
(
FB_INPUTCTX
*
ctx
,
FB_WCHAR
c )
67
{
68
/* skip white space */
69
while
( (c ==
_LC
(
' '
)) || (c ==
_LC
(
'\t'
)) )
70
c =
hReadChar
( ctx );
71
72
switch
( c )
73
{
74
case
_LC
(
','
):
75
case
FB_WEOF
:
76
break
;
77
78
case
_LC
(
'\n'
):
79
break
;
80
81
case
_LC
(
'\r'
):
82
if
( (c =
hReadChar
( ctx )) !=
_LC
(
'\n'
) )
83
hUnreadChar
( ctx, c );
84
break
;
85
86
default
:
87
hUnreadChar
( ctx, c );
88
break
;
89
}
90
}
91
92
void
fb_FileInputNextTokenWstr
(
FB_WCHAR
*
buffer
, ssize_t max_chars,
int
is_string )
93
{
94
/* max_chars does not include the null terminator, the buffer is
95
assumed to be big enough to hold at least the null terminator */
96
97
ssize_t len;
98
int
isquote, skipdelim;
99
FB_WCHAR
c;
100
FB_INPUTCTX
*
ctx
=
FB_TLSGETCTX
( INPUT );
101
102
/* */
103
skipdelim =
TRUE
;
104
isquote = 0;
105
len = 0;
106
107
c =
hSkipWhiteSpc
( ctx );
108
109
while
( (c !=
FB_WEOF
) && (len < max_chars) )
110
{
111
switch
( c )
112
{
113
case
_LC
(
'\n'
):
114
skipdelim =
FALSE
;
115
goto
exit;
116
117
case
_LC
(
'\r'
):
118
if
( (c =
hReadChar
( ctx )) !=
_LC
(
'\n'
) )
119
hUnreadChar
( ctx, c );
120
121
skipdelim =
FALSE
;
122
goto
exit;
123
124
case
_LC
(
'"'
):
125
if
( !isquote )
126
{
127
if
( len == 0 )
128
isquote = 1;
129
else
130
goto
savechar;
131
}
132
else
133
{
134
isquote = 0;
135
if
( is_string )
136
{
137
c =
hReadChar
( ctx );
138
goto
exit;
139
}
140
}
141
142
break
;
143
144
case
_LC
(
','
):
145
if
( !isquote )
146
{
147
skipdelim =
FALSE
;
148
goto
exit;
149
}
150
151
goto
savechar;
152
153
case
_LC
(
'\t'
):
154
case
_LC
(
' '
):
155
if
( !isquote )
156
{
157
if
( !is_string )
158
{
159
goto
exit;
160
}
161
}
162
163
default
:
164
savechar:
165
*buffer++ = c;
166
++len;
167
break
;
168
}
169
170
c =
hReadChar
( ctx );
171
}
172
173
exit:
174
*buffer =
_LC
(
'\0'
);
175
176
/* skip comma or newline */
177
if
( skipdelim )
178
hSkipDelimiter
( ctx, c );
179
}
rtlib
file_input_tok_wstr.c
Generated on Thu Jan 23 2014 19:40:11 for FreeBASIC by
1.8.4