FreeBASIC  0.91.0
file_openshort.c
Go to the documentation of this file.
1 /* short version of OPEN */
2 
3 #include "fb.h"
4 
5 /*:::::*/
6 FBCALL int fb_FileOpenShort( FBSTRING *str_file_mode,
7  int fnum,
9  int len,
10  FBSTRING *str_access_mode,
11  FBSTRING *str_lock_mode)
12 {
13  unsigned file_mode = 0;
14  int access_mode = -1, lock_mode = -1;
15  size_t file_mode_len, access_mode_len, lock_mode_len;
16  int error_code = FB_RTERROR_OK;
17 
18  if( !FB_FILE_INDEX_VALID( fnum ) )
20 
21  file_mode_len = FB_STRSIZE( str_file_mode );
22  access_mode_len = FB_STRSIZE( str_access_mode );
23  lock_mode_len = FB_STRSIZE( str_lock_mode );
24 
25  if( file_mode_len != 1 || access_mode_len>2 || lock_mode_len>2 ) {
27  }
28 
29  if( error_code==FB_RTERROR_OK ) {
30  if( strcasecmp(str_file_mode->data, "B")==0 ) {
31  file_mode = FB_FILE_MODE_BINARY;
32  } else if( strcasecmp(str_file_mode->data, "I")==0 ) {
33  file_mode = FB_FILE_MODE_INPUT;
34  } else if( strcasecmp(str_file_mode->data, "O")==0 ) {
35  file_mode = FB_FILE_MODE_OUTPUT;
36  } else if( strcasecmp(str_file_mode->data, "A")==0 ) {
37  file_mode = FB_FILE_MODE_APPEND;
38  } else if( strcasecmp(str_file_mode->data, "R")==0 ) {
39  file_mode = FB_FILE_MODE_RANDOM;
40  } else {
42  }
43  }
44 
45  if( access_mode_len!=0 && error_code==FB_RTERROR_OK ) {
46  if ( strcasecmp(str_access_mode->data, "R")==0 ) {
47  access_mode = FB_FILE_ACCESS_READ;
48  } else if ( strcasecmp(str_access_mode->data, "W")==0 ) {
49  access_mode = FB_FILE_ACCESS_WRITE;
50  } else if ( strcasecmp(str_access_mode->data, "RW")==0 ) {
51  access_mode = FB_FILE_ACCESS_READWRITE;
52  } else {
54  }
55  }
56 
57  if( lock_mode_len!=0 && error_code==FB_RTERROR_OK ) {
58  if ( strcasecmp(str_lock_mode->data, "S")==0 ) {
59  lock_mode = FB_FILE_LOCK_SHARED;
60  } else if ( strcasecmp(str_lock_mode->data, "R")==0 ) {
61  lock_mode = FB_FILE_LOCK_READ;
62  } else if ( strcasecmp(str_lock_mode->data, "W")==0 ) {
63  lock_mode = FB_FILE_LOCK_WRITE;
64  } else if ( strcasecmp(str_lock_mode->data, "RW")==0 ) {
65  lock_mode = FB_FILE_LOCK_READWRITE;
66  } else {
68  }
69  }
70 
71  FB_LOCK();
72  fb_hStrDelTemp_NoLock( str_file_mode );
73  fb_hStrDelTemp_NoLock( str_access_mode );
74  fb_hStrDelTemp_NoLock( str_lock_mode );
75  FB_UNLOCK();
76 
77  if( error_code!=FB_RTERROR_OK )
78  return error_code;
79 
80  if( access_mode == -1 ) {
81  /* determine the default access mode for a given file mode */
82  switch (file_mode) {
83  case FB_FILE_MODE_INPUT:
84  access_mode = FB_FILE_ACCESS_READ;
85  break;
88  access_mode = FB_FILE_ACCESS_WRITE;
89  break;
90  default:
91  access_mode = FB_FILE_ACCESS_ANY;
92  break;
93  }
94  }
95 
96  if( lock_mode == -1 ) {
97  /* determine the default lock mode for a given file mode */
98  switch (file_mode) {
99  case FB_FILE_MODE_INPUT:
100  lock_mode = FB_FILE_LOCK_SHARED;
101  break;
102  default:
103  lock_mode = FB_FILE_LOCK_WRITE;
104  break;
105  }
106  }
107 
108  return fb_FileOpen( filename,
109  file_mode,
110  access_mode,
111  lock_mode,
112  fnum,
113  len );
114 }