FreeBASIC  0.91.0
dev_file_encod_readline_wstr.c
Go to the documentation of this file.
1 /* UTF-encoded file device LINE INPUT for wstrings */
2 
3 #include "fb.h"
4 
5 int fb_DevFileReadLineEncodWstr( FB_FILE *handle, FB_WCHAR *dst, ssize_t max_chars )
6 {
7  int res;
8 
9  FB_LOCK();
10 
11  FILE* fp = (FILE *)handle->opaque;
12  if( fp == stdout || fp == stderr )
13  fp = stdin;
14 
15  if( fp == NULL ) {
16  FB_UNLOCK();
18  }
19 
20  /* Clear string first, we're only using += concat assign below... */
21  dst[0] = _LC('\0');
22 
23  /* Read one byte at a time until CR and/or LF is found.
24  The fb_FileGetDataEx() will handle the decoding. The length to read
25  is specified in wchars, not bytes, because we're passing TRUE for
26  is_unicode. */
27  while( TRUE ) {
28  FB_WCHAR c[2];
29  size_t len;
30 
31  res = fb_FileGetDataEx( handle, 0, c, 1, &len, FALSE, TRUE );
32  if( (res != FB_RTERROR_OK) || (len == 0) )
33  break;
34 
35  /* CR? Check for following LF too, and skip it if it's there */
36  if( c[0] == _LC('\r') ) {
37  res = fb_FileGetDataEx( handle, 0, c, 1, &len, FALSE, TRUE );
38  if( (res != FB_RTERROR_OK) || (len == 0) )
39  break;
40 
41  /* No LF? Ok then, don't skip it yet */
42  if( c[0] != _LC('\n') )
43  fb_FilePutBackEx( handle, c, 1 );
44 
45  break;
46  }
47 
48  /* LF? */
49  if( c[0] == _LC('\n') ) {
50  break;
51  }
52 
53  /* Any other char? Append to string, and continue... */
54  c[1] = _LC('\0');
55  fb_WstrConcatAssign( dst, max_chars, c );
56  }
57 
58  FB_UNLOCK();
59 
60  return res;
61 }