FreeBASIC  0.91.0
dev_file_encod_readline.c
Go to the documentation of this file.
1 /* UTF-encoded file device LINE INPUT for strings */
2 
3 #include "fb.h"
4 
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  fb_StrDelete( dst );
22 
23  /* Read one byte at a time until CR and/or LF is found.
24  The fb_FileGetDataEx() will handle the decoding. */
25  while( TRUE ) {
26  char c[2];
27  size_t len;
28 
29  res = fb_FileGetDataEx( handle, 0, c, 1, &len, FALSE, FALSE );
30  if( (res != FB_RTERROR_OK) || (len == 0) )
31  break;
32 
33  /* CR? Check for following LF too, and skip it if it's there */
34  if( c[0] == '\r' ) {
35  res = fb_FileGetDataEx( handle, 0, c, 1, &len, FALSE, FALSE );
36  if( (res != FB_RTERROR_OK) || (len == 0) )
37  break;
38 
39  /* No LF? Ok then, don't skip it yet */
40  if( c[0] != '\n' )
41  fb_FilePutBackEx( handle, c, 1 );
42 
43  break;
44  }
45 
46  /* LF? */
47  if( c[0] == '\n' ) {
48  break;
49  }
50 
51  /* Any other char? Append to string, and continue... */
52  c[1] = 0;
53  fb_StrConcatAssign( (void *)dst, -1, c, 1, FB_FALSE );
54  }
55 
56  FB_UNLOCK();
57 
58  return res;
59 }