FreeBASIC  0.91.0
dev_lpt.c
Go to the documentation of this file.
1 /* LPTx device */
2 
3 #include "fb.h"
4 
5 static FB_FILE *fb_DevLptFindDeviceByName( int iPort, char * filename, int no_redir )
6 {
7  size_t i;
8  /* Test if the printer is already open. */
9  for( i=0; i<FB_MAX_FILES; ++i )
10  {
12  if( handle->type == FB_FILE_TYPE_PRINTER )
13  {
14  if( no_redir==FALSE || handle->redirection_to==NULL )
15  {
16  DEV_LPT_INFO *devInfo = (DEV_LPT_INFO*) handle->opaque;
17  if( devInfo )
18  {
19  if( iPort == 0 || iPort == devInfo->iPort )
20  {
21  if( strcmp(devInfo->pszDevice, filename)==0 ) {
22  /* bugcheck */
24  && handle!=FB_HANDLE_PRINTER );
25  return handle;
26  }
27  }
28  }
29  }
30  }
31  }
32  return NULL;
33 }
34 
35 static char *fb_DevLptMakeDeviceName( DEV_LPT_PROTOCOL *lpt_proto )
36 {
37  if( lpt_proto )
38  {
39  char * p = calloc( strlen(lpt_proto->proto) + strlen(lpt_proto->name) + 3, 1 );
40  strcpy( p, lpt_proto->proto );
41  strcat( p, ":" );
42  strcat( p, lpt_proto->name );
43  return p;
44  }
45  return NULL;
46 }
47 
49  NULL,
51  NULL,
52  NULL,
53  NULL,
54  NULL,
57  NULL,
58  NULL,
59  NULL,
60  NULL
61 };
62 
63 /*:::::*/
64 int fb_DevLptOpen( FB_FILE *handle, const char *filename, size_t filename_len )
65 {
66  DEV_LPT_PROTOCOL *lpt_proto;
67  DEV_LPT_INFO *devInfo;
68  FB_FILE *redir_handle = NULL;
69  FB_FILE *tmp_handle = NULL;
70  int res;
71 
72  if (!fb_DevLptParseProtocol( &lpt_proto, filename, filename_len , TRUE) )
73  {
74  if( lpt_proto )
75  free( lpt_proto );
77  }
78 
79  FB_LOCK();
80 
81  /* Determine the port number and a normalized device name */
82  devInfo = (DEV_LPT_INFO*) calloc(1, sizeof(DEV_LPT_INFO));
83  devInfo->uiRefCount = 1;
84  devInfo->iPort = lpt_proto->iPort;
85  devInfo->pszDevice = fb_DevLptMakeDeviceName( lpt_proto );
86  devInfo->driver_opaque = NULL;
87 
88  /* Test if the printer is already open. */
89  tmp_handle = fb_DevLptFindDeviceByName( devInfo->iPort, devInfo->pszDevice, FALSE );
90  if( tmp_handle )
91  {
92  free(devInfo);
93  redir_handle = tmp_handle;
94  devInfo = (DEV_LPT_INFO*) tmp_handle->opaque;
95  ++devInfo->uiRefCount;
96  }
97 
98  /* Open the printer if not opened already */
99  if( devInfo->driver_opaque == NULL ) {
100  res = fb_PrinterOpen( devInfo, devInfo->iPort, filename );
101  } else {
102  res = fb_ErrorSetNum( FB_RTERROR_OK );
103  if( FB_HANDLE_USED(redir_handle) ) {
104  /* We only allow redirection between OPEN "LPT1:" and LPRINT */
105  if( handle==FB_HANDLE_PRINTER ) {
106  redir_handle->redirection_to = handle;
107  handle->width = redir_handle->width;
108  handle->line_length = redir_handle->line_length;
109  } else {
110  handle->redirection_to = redir_handle;
111  }
112  } else {
113  handle->width = 80;
114  }
115  }
116 
117  if( res == FB_RTERROR_OK ) {
118  handle->hooks = &hooks_dev_lpt;
119  handle->opaque = devInfo;
120  handle->type = FB_FILE_TYPE_PRINTER;
121  } else {
122  if( devInfo->pszDevice )
123  free( devInfo->pszDevice );
124  free( devInfo );
125  }
126 
127  if( lpt_proto )
128  free( lpt_proto );
129 
130  FB_UNLOCK();
131 
132  return res;
133 }
134 
135 int fb_DevPrinterSetWidth( const char *pszDevice, int width, int default_width )
136 {
137  FB_FILE *tmp_handle = NULL;
138  int cur = ((default_width==-1) ? 80 : default_width);
139  char *pszDev;
140  DEV_LPT_PROTOCOL *lpt_proto;
141 
142  if (!fb_DevLptParseProtocol( &lpt_proto, pszDevice, strlen(pszDevice), TRUE) )
143  {
144  if( lpt_proto )
145  free( lpt_proto );
147  }
148 
149  pszDev = fb_DevLptMakeDeviceName( lpt_proto );
150 
151  /* Test all printers. */
152  tmp_handle = fb_DevLptFindDeviceByName( lpt_proto->iPort, pszDev, TRUE );
153  if( tmp_handle )
154  {
155  if( width!=-1 )
156  tmp_handle->width = width;
157  cur = tmp_handle->width;
158  }
159 
160  if( lpt_proto )
161  free( lpt_proto );
162  free(pszDev);
163 
164  return cur;
165 }
166 
167 int fb_DevPrinterGetOffset( const char *pszDevice )
168 {
169  FB_FILE *tmp_handle = NULL;
170  int cur = 0;
171  char *pszDev;
172  DEV_LPT_PROTOCOL *lpt_proto;
173 
174  if (!fb_DevLptParseProtocol( &lpt_proto, pszDevice, strlen(pszDevice), TRUE) )
175  {
176  if( lpt_proto )
177  free( lpt_proto );
179  }
180 
181  pszDev = fb_DevLptMakeDeviceName( lpt_proto );
182 
183  /* Test all printers. */
184  tmp_handle = fb_DevLptFindDeviceByName( lpt_proto->iPort, pszDev, TRUE );
185  if( tmp_handle )
186  cur = tmp_handle->line_length;
187 
188  if( lpt_proto )
189  free( lpt_proto );
190  free(pszDev);
191 
192  return cur;
193 
194 }