FreeBASIC  0.91.0
file_attr.c
Go to the documentation of this file.
1 /* file open mode and attribs */
2 
3 #include "fb.h"
4 
5 static int file_mode_map[] = { FB_FILE_ATTR_MODE_BINARY, /* FB_FILE_MODE_BINARY = 0 */
6  FB_FILE_ATTR_MODE_RANDOM, /* FB_FILE_MODE_RANDOM = 1 */
7  FB_FILE_ATTR_MODE_INPUT, /* FB_FILE_MODE_INPUT = 2 */
8  FB_FILE_ATTR_MODE_OUTPUT, /* FB_FILE_MODE_OUTPUT = 3 */
9  FB_FILE_ATTR_MODE_APPEND }; /* FB_FILE_MODE_APPEND = 4 */
10 
12  (
13  int handle,
14  int returntype
15  )
16 {
17  int ret = 0;
18  int err = 0;
19  FB_FILE *file;
20 
21  file = FB_FILE_TO_HANDLE( handle );
22 
23  if ( !file )
24  {
25  ret = 0;
27  }
28  else
29  {
30  switch ( returntype )
31  {
32  case FB_FILE_ATTR_MODE:
33  ret = file_mode_map[file->mode];
34  err = FB_RTERROR_OK;
35  break;
36 
37 /* FB_FILE_ATTR_HANDLE only enabled on x86 for now due to return value size issues */
38 #ifdef HOST_X86
39 
40 
42 
43  /* TODO: standardize the DEV_* structs, or provide a device hook function to get OS handle */
44 
45  if( file->type == FB_FILE_TYPE_PRINTER )
46  {
47  if( file->opaque )
48  {
49 #if defined( HOST_WIN32 )
50  if( *((int*)file->opaque) ) /* WARNING: unsafe when sizeof(void *) > sizeof(int) */
51  {
52  ret = **((int**)file->opaque); /* WARNING: unsafe when sizeof(void *) > sizeof(int) */
53  err = FB_RTERROR_OK;
54  }
55 #else
56  ret = *((int*)file->opaque); /* WARNING: unsafe when sizeof(void *) > sizeof(int) */
57  err = FB_RTERROR_OK;
58 #endif
59  }
60  }
61  else if( file->type == FB_FILE_TYPE_SERIAL )
62  {
63  if( file->opaque )
64  {
65  if( *((int*)file->opaque) ) /* WARNING: unsafe when sizeof(void *) > sizeof(int) */
66  {
67  ret = **((int**)file->opaque); /* WARNING: unsafe when sizeof(void *) > sizeof(int) */
68  err = FB_RTERROR_OK;
69  }
70  }
71  }
72  else
73  {
74  ret = (int)file->opaque; /* WARNING: unsafe when sizeof(void *) > sizeof(int) */
75  err = FB_RTERROR_OK;
76  }
77  break;
78 #endif
79 
81  ret = file->encod;
82  err = FB_RTERROR_OK;
83  break;
84 
85  default:
86  ret = 0;
88  break;
89  }
90  }
91 
92  fb_ErrorSetNum( err );
93 
94  return ret;
95 
96 }