FreeBASIC  0.91.0
vfs_open.c
Go to the documentation of this file.
1 /* open file (vfs) functions */
2 
3 #include "fb.h"
4 
5 /*::::::*/
7  (
9  )
10 {
11  fb_off_t size = 0;
12 
13  if( handle->hooks->pfnSeek == NULL || handle->hooks->pfnTell == NULL )
14  return size;
15 
16  switch( handle->mode )
17  {
20  case FB_FILE_MODE_INPUT:
21  if( handle->hooks->pfnSeek( handle, 0, SEEK_END ) != 0 )
22  return -1;
23 
24  handle->hooks->pfnTell( handle, &size );
25 
26  handle->hooks->pfnSeek( handle, 0, SEEK_SET );
27  break;
28 
30  handle->hooks->pfnTell( handle, &size );
31  break;
32  }
33 
34  return size;
35 }
36 
37 /*::::::*/
39  (
40  FB_FILE *handle,
41  const char *filename,
42  size_t filename_length,
43  unsigned int mode,
44  unsigned int access,
45  unsigned int lock,
46  int len,
47  FB_FILE_ENCOD encoding,
48  FnFileOpen pfnOpen
49  )
50 {
51  int result;
52 
53  FB_LOCK();
54 
55  if (handle->hooks!=NULL) {
56  FB_UNLOCK();
58  }
59 
61 
62  /* clear handle */
63  memset(handle, 0, sizeof(FB_FILE));
64 
65  /* specific file/device handles are stored in the member "opaque" */
66  handle->mode = mode;
67  handle->encod = encoding;
68  handle->size = 0;
69  handle->type = FB_FILE_TYPE_VFS;
70  handle->access = access;
71  handle->lock = lock; /* lock mode not supported yet */
72  handle->line_length = 0;
73 
74  /* reclen */
75  switch( handle->mode )
76  {
78  case FB_FILE_MODE_INPUT:
80  if( len <= 0 )
81  len = 128;
82  handle->len = len;
83  break;
84 
85  default:
86  handle->len = 0;
87  break;
88  }
89 
90  if (pfnOpen==NULL)
91  {
92  /* unknown protocol! */
93  FB_UNLOCK();
95  }
96 
97  /* assume size won't be calculated by dev::open */
98  handle->size = -1;
99 
100  result = pfnOpen(handle, filename, filename_length);
101 
102  DBG_ASSERT(result!=FB_RTERROR_OK || handle->hooks!=NULL);
103 
104  if( result == 0 )
105  {
106  /* if size wasn't calculated yet, do it now */
107  if( handle->size == -1 )
108  handle->size = hFileGetSize( handle );
109  }
110  else
111  {
112  memset(handle, 0, sizeof(FB_FILE));
113  }
114 
115  FB_UNLOCK();
116 
117  return result;
118 }
119 
120 /*::::::*/
122  (
123  FB_FILE *handle,
124  FBSTRING *str_filename,
125  unsigned int mode,
126  unsigned int access,
127  unsigned int lock,
128  int len,
129  FB_FILE_ENCOD encoding,
130  FnFileOpen pfnOpen
131  )
132 {
133  char *filename;
134  size_t filename_length;
135 
136  /* copy file name */
137  filename_length = FB_STRSIZE( str_filename );
138  filename = (char*) alloca( filename_length + 1 );
139  fb_hStrCopy( filename, str_filename->data, filename_length );
140  filename[filename_length] = 0;
141 
142  /* del if temp */
143  fb_hStrDelTemp( str_filename );
144 
145  return fb_FileOpenVfsRawEx( handle, filename, filename_length,
146  mode, access, lock, len, encoding, pfnOpen );
147 }