FreeBASIC  0.91.0
dev_file_eof.c
Go to the documentation of this file.
1 /* detects EOF for file device */
2 
3 #include "fb.h"
4 
6 {
7  int res;
8  FILE *fp;
9 
10  FB_LOCK();
11 
12  fp = (FILE*) handle->opaque;
13 
14  if( fp == NULL )
15  {
16  FB_UNLOCK();
17  return FB_TRUE;
18  }
19 
20  res = FB_FALSE;
21 
22  switch( handle->mode )
23  {
24  /* non-text mode? */
27  /* note: handle->putback_size will be checked by fb_FileEofEx() */
28  res = ( ftell( fp ) >= handle->size? FB_TRUE : FB_FALSE );
29  break;
30 
31  /* text-mode (INPUT, OUTPUT or APPEND) */
32  default:
33  /* try feof() first, because the EOF char (27) */
34  if( feof( fp ) )
35  {
36  res = FB_TRUE;
37  }
38  /* if in input mode, feof() won't return TRUE if file_pos == file_size */
39  else if( handle->mode == FB_FILE_MODE_INPUT )
40  {
41  int has_size = handle->hooks->pfnTell!=NULL && handle->hooks->pfnSeek!=NULL;
42  /* note: fseek() is unreliable in text-mode, size must be calculated
43  re-opening the file in binary mode */
44  if( has_size )
45  if( ftell( fp ) >= handle->size )
46  res = FB_TRUE;
47  }
48  }
49 
50  FB_UNLOCK();
51 
52  return res;
53 }