FreeBASIC  0.91.0
io_widthdev.c
Go to the documentation of this file.
1 /* set the with for devices */
2 
3 #include "fb.h"
4 #include <ctype.h>
5 
6 typedef struct _DEV_INFO_WIDTH {
8  char *device;
9  int width;
11 
14 static void fb_hListDevInit ( FB_LIST *list )
15 {
16  fb_hListDynInit( list );
17 }
18 
23 static DEV_INFO_WIDTH *fb_hListDevElemAlloc ( FB_LIST *list, const char *device, int width )
24 {
25  DEV_INFO_WIDTH *node = (DEV_INFO_WIDTH*) calloc( 1, sizeof(DEV_INFO_WIDTH) );
26  node->device = strdup(device);
27  node->width = width;
28  fb_hListDynElemAdd( list, &node->elem );
29  return node;
30 }
31 
32 #if 0
33 
35 static void fb_hListDevElemFree ( FB_LIST *list, DEV_INFO_WIDTH *node )
36 {
37  fb_hListDynElemRemove( list, &node->elem );
38  free(node->device);
39  free(node);
40 }
41 #endif
42 
46 
47 /*:::::*/
48 FBCALL int fb_WidthDev( FBSTRING *dev, int width )
49 {
50  int cur = width;
51  DEV_INFO_WIDTH *node;
52  size_t i, size;
53  char *device;
54 
55  FB_LOCK();
56 
57  /* create list of device info nodes (if not created yet) */
58  if( dev_info_widths==NULL ) {
59  dev_info_widths = malloc( sizeof(FB_LIST) );
60  fb_hListDevInit( dev_info_widths );
61  }
62 
63  FB_UNLOCK();
64 
65  /* */
66  size = FB_STRSIZE(dev);
67  device = alloca(size + 1);
68  memcpy( device, dev->data, size );
69  device[size] = 0;
70 
71  /* make the name uppercase */
72  for( i=0; i!=size; ++i ) {
73  unsigned ch = (unsigned) device[i];
74  if( islower(ch) )
75  device[i] = (char) toupper(ch);
76  }
77 
78  FB_LOCK();
79 
80  /* Search list of devices for the requested device name */
81  for (node = (DEV_INFO_WIDTH*) dev_info_widths->head;
82  node != (DEV_INFO_WIDTH*) NULL;
83  node = (DEV_INFO_WIDTH*) node->elem.next)
84  {
85  if( strcmp( device, node->device ) == 0 ) {
86  break;
87  }
88  }
89 
90  if( width != -1 ) {
91  if( node == NULL ) {
92  /* Allocate a new list node if device name not found */
93  node = fb_hListDevElemAlloc ( dev_info_widths, device, width );
94  } else {
95  /* Set device width */
96  node->width = width;
97  }
98  } else if( node != NULL ) {
99  cur = node->width;
100  }
101 
102  /* search the width for all open (and known) devices */
103  if( strcmp( device, "SCRN:" )==0 ) {
104  /* SCREEN device */
105  if( width!=-1 ) {
106  fb_Width( width, -1 );
107  }
108  cur = FB_HANDLE_SCREEN->width;
109 
110  } else if ( fb_DevLptTestProtocol( NULL, device, size ) ) {
111  /* PRINTER device */
112  cur = fb_DevPrinterSetWidth( device, width, cur );
113 
114  } else if ( fb_DevComTestProtocol( NULL, device, size ) ) {
115  /* SERIAL device */
116  cur = fb_DevSerialSetWidth( device, width, cur );
117 
118  } else {
119  /* unknown device */
120  }
121 
122  FB_UNLOCK();
123 
124  if( width==-1 ) {
125  return cur;
126  }
127 
128  return fb_ErrorSetNum( FB_RTERROR_OK );
129 }