FreeBASIC  0.91.0
dev_file_read_wstr.c
Go to the documentation of this file.
1 /* file device */
2 
3 #include "fb.h"
4 
5 int fb_DevFileReadWstr( FB_FILE *handle, FB_WCHAR *dst, size_t *pchars )
6 {
7  FILE *fp;
8  size_t chars;
9  char *buffer;
10 
11  FB_LOCK();
12 
13  if( handle == NULL )
14  fp = stdin;
15  else
16  {
17  fp = (FILE*) handle->opaque;
18  if( fp == stdout || fp == stderr )
19  fp = stdin;
20 
21  if( fp == NULL )
22  {
23  FB_UNLOCK();
25  }
26  }
27 
28  chars = *pchars;
29 
30  if( chars < FB_LOCALBUFF_MAXLEN )
31  buffer = alloca( chars + 1 );
32  else
33  buffer = malloc( chars + 1 );
34 
35  /* do read */
36  chars = fread( buffer, 1, chars, fp );
37  buffer[chars] = '\0';
38 
39  /* convert to wchar, file should be opened with the ENCODING option
40  to allow UTF characters to be read */
41  fb_wstr_ConvFromA( dst, chars, buffer );
42 
43  if( *pchars >= FB_LOCALBUFF_MAXLEN )
44  free( buffer );
45 
46  /* fill with nulls if at eof */
47  if( chars != *pchars )
48  memset( (void *)&dst[chars], 0, (*pchars - chars) * sizeof( FB_WCHAR ) );
49 
50  *pchars = chars;
51 
52  FB_UNLOCK();
53 
54  return fb_ErrorSetNum( FB_RTERROR_OK );
55 }