FreeBASIC  0.91.0
fb_file.h
Go to the documentation of this file.
1 #define FB_FILE_MODE_BINARY 0
2 #define FB_FILE_MODE_RANDOM 1
3 #define FB_FILE_MODE_INPUT 2
4 #define FB_FILE_MODE_OUTPUT 3
5 #define FB_FILE_MODE_APPEND 4
6 
7 #define FB_FILE_ACCESS_ANY 0
8 #define FB_FILE_ACCESS_READ 1
9 #define FB_FILE_ACCESS_WRITE 2
10 #define FB_FILE_ACCESS_READWRITE 3
11 
12 #define FB_FILE_LOCK_SHARED 0
13 #define FB_FILE_LOCK_READ 1
14 #define FB_FILE_LOCK_WRITE 2
15 #define FB_FILE_LOCK_READWRITE 3
16 
17 #define FB_FILE_TYPE_NORMAL 0
18 #define FB_FILE_TYPE_CONSOLE 1
19 #define FB_FILE_TYPE_ERR 2
20 #define FB_FILE_TYPE_PIPE 3
21 #define FB_FILE_TYPE_VFS 4
22 #define FB_FILE_TYPE_PRINTER 5
23 #define FB_FILE_TYPE_SERIAL 6
24 
25 typedef enum _FB_FILE_ENCOD {
31 
32 #define FB_FILE_ENCOD_DEFAULT FB_FILE_ENCOD_ASCII
33 
34 
35 #define FB_FILE_FROM_HANDLE(handle) \
36  (((handle) - __fb_ctx.fileTB) + 1 - FB_RESERVED_FILES)
37 #define FB_FILE_INDEX_VALID(index) \
38  ((index)>=1 && ((index)<=(FB_MAX_FILES-FB_RESERVED_FILES)))
39 
40 #define FB_INDEX_IS_SPECIAL(index) \
41  (((index) < 1) && (((index) > (-FB_RESERVED_FILES))
42 
43 #define FB_HANDLE_IS_SCREEN(handle) \
44  ((handle)!=NULL && FB_HANDLE_DEREF(handle)==FB_HANDLE_SCREEN)
45 
46 #define FB_HANDLE_USED(handle) \
47  ((handle)!=NULL && ((handle)->hooks!=NULL))
48 
49 #define FB_HANDLE_SCREEN __fb_ctx.fileTB
50 #define FB_HANDLE_PRINTER (__fb_ctx.fileTB+1)
51 
52 struct _FB_FILE;
53 
54 typedef int (*FnFileSetWidth) ( struct _FB_FILE *handle, int new_width );
55 typedef int (*FnFileTest) ( struct _FB_FILE *handle, const char *filename,
56  size_t filename_len );
57 typedef int (*FnFileOpen) ( struct _FB_FILE *handle, const char *filename,
58  size_t filename_len );
59 typedef int (*FnFileEof) ( struct _FB_FILE *handle );
60 typedef int (*FnFileClose) ( struct _FB_FILE *handle );
61 typedef int (*FnFileSeek) ( struct _FB_FILE *handle, fb_off_t offset, int whence );
62 typedef int (*FnFileTell) ( struct _FB_FILE *handle, fb_off_t *pOffset );
63 typedef int (*FnFileRead) ( struct _FB_FILE *handle, void *value,
64  size_t *pValuelen );
65 typedef int (*FnFileReadWstr) ( struct _FB_FILE *handle, FB_WCHAR *value,
66  size_t *pValuelen );
67 typedef int (*FnFileWrite) ( struct _FB_FILE *handle, const void *value,
68  size_t valuelen );
69 typedef int (*FnFileWriteWstr) ( struct _FB_FILE *handle, const FB_WCHAR *value,
70  size_t valuelen );
71 typedef int (*FnFileLock) ( struct _FB_FILE *handle, fb_off_t position,
72  fb_off_t size );
73 typedef int (*FnFileUnlock) ( struct _FB_FILE *handle, fb_off_t position,
74  fb_off_t size );
75 typedef int (*FnFileReadLine) ( struct _FB_FILE *handle, FBSTRING *dst );
76 typedef int (*FnFileReadLineWstr) ( struct _FB_FILE *handle, FB_WCHAR *dst, ssize_t dst_chars );
77 typedef int (*FnFileFlush) ( struct _FB_FILE *handle );
78 
79 typedef struct _FB_FILE_HOOKS {
95 
96 typedef struct _FB_FILE {
97  int mode;
98  int len;
101  int type;
102  int access;
103  int lock;
104  unsigned line_length;
105  unsigned width;
106 
107  /* for a device-independent put back feature */
108  char putback_buffer[4];
109  size_t putback_size;
110 
112  /* an i/o handler might store additional (handler specific) data here */
113  void *opaque;
114  /* used when opening SCRN: to create an redirection handle */
116 } FB_FILE;
117 
118 typedef struct {
120  int status;
122  int index;
123 } FB_INPUTCTX;
124 
125 
126 #define FB_FILE_TO_HANDLE_VALID( index ) \
127  ((FB_FILE *)(__fb_ctx.fileTB + (index) - 1 + FB_RESERVED_FILES))
128 
129 #define FB_FILE_TO_HANDLE( index ) \
130  ( (index) == 0? \
131  ((FB_FILE *)FB_HANDLE_SCREEN) : \
132  ( (index) == -1? \
133  ((FB_FILE *)FB_HANDLE_PRINTER) : \
134  ( FB_FILE_INDEX_VALID( (index) )? \
135  FB_FILE_TO_HANDLE_VALID( (index) ) : \
136  ((FB_FILE *)(NULL)) \
137  ) \
138  ) \
139  )
140 
141 static __inline__ FB_FILE *FB_HANDLE_DEREF( FB_FILE *handle )
142 {
143  if( handle != NULL ) {
144  FB_LOCK();
145  while( handle->redirection_to != NULL ) {
146  handle = handle->redirection_to;
147  }
148  FB_UNLOCK();
149  }
150  return handle;
151 }
152 
153  int fb_FilePutData ( int fnum, fb_off_t pos, const void *data,
154  size_t length, int adjust_rec_pos,
155  int checknewline );
156  int fb_FilePutDataEx ( FB_FILE *handle, fb_off_t pos, const void *data,
157  size_t length, int adjust_rec_pos,
158  int checknewline, int isunicode );
159  int fb_FileGetData ( int fnum, fb_off_t pos, void *data,
160  size_t length, int adjust_rec_pos );
161  int fb_FileGetDataEx ( FB_FILE *handle, fb_off_t pos, void *data,
162  size_t length, size_t *bytesread,
163  int adjust_rec_pos, int isunicode );
164 
165  int fb_FileOpenVfsRawEx ( FB_FILE *handle, const char *filename,
166  size_t filename_length,
167  unsigned int mode, unsigned int access,
168  unsigned int lock, int len, FB_FILE_ENCOD encoding,
169  FnFileOpen pfnOpen );
170  int fb_FileOpenVfsEx ( FB_FILE *handle, FBSTRING *str_filename,
171  unsigned int mode, unsigned int access,
172  unsigned int lock, int len, FB_FILE_ENCOD encoding,
173  FnFileOpen pfnOpen );
174 FBCALL int fb_FileOpenCons ( FBSTRING *str_filename, unsigned int mode,
175  unsigned int access, unsigned int lock,
176  int fnum, int len, const char *encoding );
177 FBCALL int fb_FileOpenErr ( FBSTRING *str_filename, unsigned int mode,
178  unsigned int access, unsigned int lock,
179  int fnum, int len, const char *encoding );
180 FBCALL int fb_FileOpenPipe ( FBSTRING *str_filename, unsigned int mode,
181  unsigned int access, unsigned int lock,
182  int fnum, int len, const char *encoding );
183 FBCALL int fb_FileOpenScrn ( FBSTRING *str_filename, unsigned int mode,
184  unsigned int access, unsigned int lock,
185  int fnum, int len, const char *encoding );
186 
187 FBCALL int fb_FileOpenLpt ( FBSTRING *str_filename, unsigned int mode,
188  unsigned int access, unsigned int lock,
189  int fnum, int len, const char *encoding );
190 
191 FBCALL int fb_FileOpenCom ( FBSTRING *str_filename, unsigned int mode,
192  unsigned int access, unsigned int lock,
193  int fnum, int len, const char *encoding );
194 
196  (
197  FBSTRING *str,
198  unsigned int mode,
199  unsigned int access,
200  unsigned int lock,
201  int fnum,
202  int len
203  );
204 
205 FBCALL int fb_FileFree ( void );
206 FBCALL int fb_FileOpen ( FBSTRING *str, unsigned int mode,
207  unsigned int access, unsigned int lock,
208  int fnum, int len );
209 FBCALL int fb_FileOpenEncod ( FBSTRING *str, unsigned int mode,
210  unsigned int access, unsigned int lock,
211  int fnum, int len, const char *encoding );
212  int fb_FileOpenEx ( FB_FILE *handle, FBSTRING *str,
213  unsigned int mode, unsigned int access,
214  unsigned int lock, int len );
215 FBCALL int fb_FileOpenShort ( FBSTRING *str_file_mode, int fnum,
216  FBSTRING *filename, int len,
217  FBSTRING *str_access_mode,
218  FBSTRING *str_lock_mode);
219  int fb_FileCloseEx ( FB_FILE *handle );
220 FBCALL int fb_FileClose ( int fnum );
221 FBCALL int fb_FileCloseAll ( void );
222 
223 FBCALL int fb_FilePut ( int fnum, int pos, void* value, size_t valuelen );
224 FBCALL int fb_FilePutLarge ( int fnum, long long pos, void *value, size_t valuelen );
225  int fb_FilePutEx ( FB_FILE *handle, fb_off_t pos, void* value, size_t valuelen );
226 FBCALL int fb_FilePutStr ( int fnum, int pos, void *str, ssize_t str_len );
227 FBCALL int fb_FilePutStrLarge ( int fnum, long long pos, void *str, ssize_t str_len );
228  int fb_FilePutStrEx ( FB_FILE *handle, fb_off_t pos, void *str, ssize_t str_len );
229 FBCALL int fb_FilePutArray ( int fnum, int pos, FBARRAY *src );
230 FBCALL int fb_FilePutArrayLarge( int fnum, long long pos, FBARRAY *src );
231 
232 FBCALL int fb_FileGet ( int fnum, int pos, void* value, size_t valuelen );
233 FBCALL int fb_FileGetLarge ( int fnum, long long pos, void *dst, size_t chars );
234 FBCALL int fb_FileGetIOB ( int fnum, int pos, void *dst, size_t chars, size_t *bytesread );
235 FBCALL int fb_FileGetLargeIOB ( int fnum, long long pos, void *dst, size_t chars, size_t *bytesread );
236  int fb_FileGetEx ( FB_FILE *handle, fb_off_t pos, void* value, size_t valuelen );
237 FBCALL int fb_FileGetStr ( int fnum, int pos, void *str, ssize_t str_len );
238 FBCALL int fb_FileGetStrLarge ( int fnum, long long pos, void *str, ssize_t str_len );
239 FBCALL int fb_FileGetStrIOB ( int fnum, int pos, void *str, ssize_t str_len, size_t *bytesread );
240 FBCALL int fb_FileGetStrLargeIOB( int fnum, long long pos, void *str, ssize_t str_len, size_t *bytesread );
241  int fb_FileGetStrEx ( FB_FILE *handle, fb_off_t pos, void *str, ssize_t str_len, size_t *bytesread );
242 FBCALL int fb_FileGetArray ( int fnum, int pos, FBARRAY *dst );
243 FBCALL int fb_FileGetArrayLarge( int fnum, long long pos, FBARRAY *dst );
244 FBCALL int fb_FileGetArrayIOB ( int fnum, int pos, FBARRAY *dst, size_t *bytesread );
245 FBCALL int fb_FileGetArrayLargeIOB( int fnum, long long pos, FBARRAY *dst, size_t *bytesread );
246 
247 FBCALL int fb_FileEof ( int fnum );
248  int fb_FileEofEx ( FB_FILE *handle );
249 FBCALL long long fb_FileTell ( int fnum );
250  fb_off_t fb_FileTellEx ( FB_FILE *handle );
251 FBCALL int fb_FileSeek ( int fnum, int newpos );
252 FBCALL int fb_FileSeekLarge ( int fnum, long long newpos );
253  int fb_FileSeekEx ( FB_FILE *handle, fb_off_t newpos );
254 FBCALL long long fb_FileLocation ( int fnum );
255  fb_off_t fb_FileLocationEx ( FB_FILE *handle );
256 FBCALL int fb_FileKill ( FBSTRING *str );
257 FBCALL void fb_FileReset ( void );
258 FBCALL void fb_FileResetEx ( int streamno );
259  int fb_hFileResetEx ( int streamno );
260 FBCALL long long fb_FileSize ( int fnum );
261  fb_off_t fb_FileSizeEx ( FB_FILE *handle );
262 FBCALL int fb_FilePutBack ( int fnum, const void *data, size_t length );
263 FBCALL int fb_FilePutBackWstr ( int fnum, const FB_WCHAR *src, size_t chars );
264  int fb_FilePutBackEx ( FB_FILE *handle, const void *data, size_t length );
265  int fb_FilePutBackWstrEx( FB_FILE *handle, const FB_WCHAR *src, size_t chars );
266 
267 FBCALL int fb_FileInput ( int fnum );
268 FBCALL FBSTRING *fb_FileStrInput ( ssize_t bytes, int fnum );
269 FBCALL FB_WCHAR *fb_FileWstrInput ( ssize_t chars, int fnum );
270 FBCALL int fb_FileLineInput ( int fnum, void *dst, ssize_t dst_len, int fillrem );
271 FBCALL int fb_FileLineInputWstr( int fnum, FB_WCHAR *dst, ssize_t max_chars );
272 
273 FBCALL int fb_InputByte ( char *dst );
274 FBCALL int fb_InputUbyte ( unsigned char *dst );
275 FBCALL int fb_InputShort ( short *dst );
276 FBCALL int fb_InputUshort ( unsigned short *dst );
277 FBCALL int fb_InputInt ( int *dst );
278 FBCALL int fb_InputUint ( unsigned int *dst );
279 FBCALL int fb_InputLongint ( long long *dst );
280 FBCALL int fb_InputUlongint ( unsigned long long *dst );
281 FBCALL int fb_InputSingle ( float *dst );
282 FBCALL int fb_InputDouble ( double *dst );
283 FBCALL int fb_InputString ( void *dst, ssize_t strlen, int fillrem );
284 FBCALL int fb_InputWstr ( FB_WCHAR *str, ssize_t length );
285 
286 FBCALL int fb_FileLock ( int fnum, unsigned int inipos, unsigned int endpos );
287 FBCALL int fb_FileLockLarge ( int fnum, long long inipos, long long endpos );
288 FBCALL int fb_FileUnlock ( int fnum, unsigned int inipos, unsigned int endpos );
289 FBCALL int fb_FileUnlockLarge ( int fnum, long long inipos, long long endpos );
290 
291  int fb_hFilePrintBuffer ( int fnum, const char *buffer );
292  int fb_hFilePrintBufferWstr ( int fnum, const FB_WCHAR *buffer );
293  int fb_hFilePrintBufferEx( FB_FILE *handle, const void *buffer, size_t len );
294  int fb_hFilePrintBufferWstrEx( FB_FILE *handle, const FB_WCHAR *buffer, size_t len );
295 
296  int fb_hFileLock ( FILE *f, fb_off_t inipos, fb_off_t size );
297  int fb_hFileUnlock ( FILE *f, fb_off_t inipos, fb_off_t size );
298  void fb_hConvertPath ( char *path );
299 
300  FB_FILE_ENCOD fb_hFileStrToEncoding( const char *encoding );
301 
302 FBCALL int fb_SetPos ( FB_FILE *handle, int line_length );
303 
304  int fb_FileInputNextToken( char *buffer, ssize_t maxlen, int isstring, int *isfp );
305  void fb_FileInputNextTokenWstr( FB_WCHAR *buffer, ssize_t max_chars, int is_string );
306 
307 FBCALL FBSTRING *fb_Dir ( FBSTRING *filespec, int attrib, int *out_attrib );
308 FBCALL FBSTRING *fb_Dir64 ( FBSTRING *filespec, int attrib, long long *outattrib );
309 FBCALL FBSTRING *fb_DirNext ( int *outattrib );
310 FBCALL FBSTRING *fb_DirNext64 ( long long *outattrib );
311 
312  /* Maximum length that can safely be parsed as INTEGER */
313 #define FB_INPUT_MAXINTLEN 9
314 
315  /* Maximum length that can safely be parsed as LONGINT */
316 #define FB_INPUT_MAXLONGLEN 18
317 
318  /* Maximum length of a DOUBLE printed in FB ("-1.345678901234567e+100") */
319 #define FB_INPUT_MAXDBLLEN (1 + 17 + 1 + 1 + 3)
320 
321  /* Maximum length that can represent a LONGINT ("&B" + 64 digits) */
322 #define FB_INPUT_MAXLONGBINLEN (2 + 64)
323 
324  /* Numeric input max buffer length (max numeric length + delimiter) */
325 #define FB_INPUT_MAXNUMERICLEN (FB_INPUT_MAXLONGBINLEN+1)
326 
327  /* String input max buffer length */
328 #define FB_INPUT_MAXSTRINGLEN 4096
329 
330 
331 /**************************************************************************************************
332  * UTF Encoding
333  **************************************************************************************************/
334 
335 extern const UTF_8 __fb_utf8_bmarkTb[7];
336 
337  void fb_hCharToUTF8 ( const char *src, ssize_t chars, char *dst, ssize_t *bytes );
338  char *fb_CharToUTF ( FB_FILE_ENCOD encod, const char *src, ssize_t chars, char *dst, ssize_t *bytes );
339  char *fb_WCharToUTF ( FB_FILE_ENCOD encod, const FB_WCHAR *src, ssize_t chars, char *dst, ssize_t *bytes );
340  ssize_t fb_hFileRead_UTFToChar( FILE *fp, FB_FILE_ENCOD encod, char *dst, ssize_t max_chars );
341  ssize_t fb_hFileRead_UTFToWchar( FILE *fp, FB_FILE_ENCOD encod, FB_WCHAR *dst, ssize_t max_chars );
342 
343 /**************************************************************************************************
344  * VB-compatible functions
345  **************************************************************************************************/
346 
347 #define FB_FILE_ATTR_MODE_INPUT 1
348 #define FB_FILE_ATTR_MODE_OUTPUT 2
349 #define FB_FILE_ATTR_MODE_RANDOM 4
350 #define FB_FILE_ATTR_MODE_APPEND 8
351 #define FB_FILE_ATTR_MODE_BINARY 32
352 
353 #define FB_FILE_ATTR_MODE 1
354 #define FB_FILE_ATTR_HANDLE 2
355 #define FB_FILE_ATTR_ENCODING 3
356 
357 FBCALL int fb_FileCopy ( const char *source, const char *destination );
358 FBCALL int fb_CrtFileCopy ( const char *source, const char *destination );