forked from ImortisInglorian/fbrtLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdev_file_read_wstr.bas
58 lines (44 loc) · 1.19 KB
/
dev_file_read_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
/' file device '/
#include "fb.bi"
extern "C"
function fb_DevFileReadWstr( handle as FB_FILE ptr, dst as FB_WCHAR ptr, pchars as size_t ptr ) as long
dim as FILE ptr fp
dim as size_t chars
dim as ubyte ptr buffer
FB_LOCK()
if ( handle = NULL ) then
fp = stdin
else
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
end if
chars = *pchars
if ( chars < FB_LOCALBUFF_MAXLEN ) then
buffer = malloc( chars + 1 )
else
buffer = malloc( chars + 1 )
end if
/' do read '/
chars = fread( buffer, 1, chars, fp )
buffer[chars] = 0
/' convert to wchar, file should be opened with the ENCODING option
to allow UTF characters to be read '/
fb_wstr_ConvFromA( dst, chars, buffer )
if ( *pchars >= FB_LOCALBUFF_MAXLEN ) then
free( buffer )
end if
/' fill with nulls if at eof '/
if ( chars <> *pchars ) then
memset( cast(any ptr, @dst[chars]), 0, (*pchars - chars) * sizeof( FB_WCHAR ) )
end if
*pchars = chars
FB_UNLOCK()
return fb_ErrorSetNum( FB_RTERROR_OK )
end function
end extern