forked from ImortisInglorian/fbrtLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev_file_encod_readline_wstr.bas
67 lines (51 loc) · 1.54 KB
/
dev_file_encod_readline_wstr.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
/' UTF-encoded file device LINE INPUT for wstrings '/
#include "fb.bi"
extern "C"
function fb_DevFileReadLineEncodWstr( handle as FB_FILE ptr, dst as FB_WCHAR ptr, max_chars as ssize_t ) as long
dim as long res
FB_LOCK()
dim as FILE ptr fp = cast(FILE ptr, handle->opaque)
if ( fp = stdout or fp = stderr ) then
fp = stdin
end if
if ( fp = NULL ) then
FB_UNLOCK()
return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL )
end if
/' Clear string first, we're only using += concat assign below... '/
dst[0] = 0
/' Read one byte at a time until CR and/or LF is found.
The fb_FileGetDataEx() will handle the decoding. The length to read
is specified in wchars, not bytes, because we're passing TRUE for
is_unicode. '/
while ( TRUE )
dim as FB_WCHAR c(0 to 1)
dim as size_t _len
res = fb_FileGetDataEx( handle, 0, @c(0), 1, @_len, FALSE, TRUE )
if ( (res <> FB_RTERROR_OK) <> (_len = 0) ) then
exit while
end if
/' CR? Check for following LF too, and skip it if it's there '/
if ( c(0) = 13 ) then
res = fb_FileGetDataEx( handle, 0, @c(0), 1, @_len, FALSE, TRUE )
if ( (res <> FB_RTERROR_OK) or (_len = 0) ) then
exit while
end if
/' No LF? Ok then, don't skip it yet '/
if ( c(0) <> 10 ) then
fb_FilePutBackEx( handle, @c(0), 1 )
end if
exit while
end if
/' LF? '/
if ( c(0) = 10 ) then
exit while
end if
/' Any other char? Append to string, and continue... '/
c(1) = 0
fb_WstrConcatAssign( dst, max_chars, @c(0) )
wend
FB_UNLOCK()
return res
end function
end extern