home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSIODEV.C < prev    next >
C/C++ Source or Header  |  1994-07-31  |  7KB  |  243 lines

  1. /* Copyright (C) 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsiodev.c */
  20. /* IODevice implementation for Ghostscript */
  21. #include "errno_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gp.h"
  26. #include "gsparam.h"
  27. #include "gxiodev.h"
  28.  
  29. /* Import the IODevice table from gconfig.c. */
  30. extern gx_io_device *gx_io_device_table[];
  31. extern uint gx_io_device_table_count;
  32.  
  33. /* Define the OS (%os%) device. */
  34. iodev_proc_fopen(iodev_os_fopen);
  35. iodev_proc_fclose(iodev_os_fclose);
  36. private iodev_proc_delete_file(os_delete);
  37. private iodev_proc_rename_file(os_rename);
  38. private iodev_proc_file_status(os_status);
  39. private iodev_proc_enumerate_files(os_enumerate);
  40. private iodev_proc_get_params(os_get_params);
  41. gx_io_device gs_iodev_os = {
  42.     "%os%", "FileSystem",
  43.     { iodev_no_init, iodev_no_open_device,
  44.       NULL /*iodev_os_open_file*/, iodev_os_fopen, iodev_os_fclose,
  45.       os_delete, os_rename, os_status,
  46.       os_enumerate, gp_enumerate_files_next, gp_enumerate_files_close,
  47.       os_get_params, iodev_no_put_params
  48.     }
  49. };
  50.  
  51. /* ------ Initialization ------ */
  52.  
  53. void
  54. gs_iodev_init(gs_memory_t *mem)
  55. {    /* Run the one-time initialization of each IODevice. */
  56.     gx_io_device **piodev = &gx_io_device_table[0];
  57.     for ( ; *piodev; piodev++ )
  58.         ((*piodev)->procs.init)(*piodev, mem);
  59. }
  60.  
  61. /* ------ Default (unimplemented) IODevice procedures ------ */
  62.  
  63. int
  64. iodev_no_init(gx_io_device *iodev, gs_memory_t *mem)
  65. {    return 0;
  66. }
  67.  
  68. int
  69. iodev_no_open_device(gx_io_device *iodev, const char *access, stream **ps,
  70.   gs_memory_t *mem)
  71. {    return_error(gs_error_invalidfileaccess);
  72. }
  73.  
  74. int
  75. iodev_no_open_file(gx_io_device *iodev, const char *fname, uint namelen,
  76.   const char *access, stream **ps, gs_memory_t *mem)
  77. {    return_error(gs_error_invalidfileaccess);
  78. }
  79.  
  80. int
  81. iodev_no_fopen(gx_io_device *iodev, const char *fname, const char *access,
  82.   FILE **pfile)
  83. {    return_error(gs_error_invalidfileaccess);
  84. }
  85.  
  86. int
  87. iodev_no_fclose(gx_io_device *iodev, FILE *file)
  88. {    return_error(gs_error_ioerror);
  89. }
  90.  
  91. int
  92. iodev_no_delete_file(gx_io_device *iodev, const char *fname)
  93. {    return_error(gs_error_invalidfileaccess);
  94. }
  95.  
  96. int
  97. iodev_no_rename_file(gx_io_device *iodev, const char *from, const char *to)
  98. {    return_error(gs_error_invalidfileaccess);
  99. }
  100.  
  101. int
  102. iodev_no_file_status(gx_io_device *iodev, const char *fname, struct stat *pstat)
  103. {    return_error(gs_error_undefinedfilename);
  104. }
  105.  
  106. file_enum *
  107. iodev_no_enumerate_files(gx_io_device *iodev, const char *pat, uint patlen,
  108.   gs_memory_t *memory)
  109. {    return NULL;
  110. }
  111.  
  112. int
  113. iodev_no_get_params(gx_io_device *iodev, gs_param_list *plist)
  114. {    return 0;
  115. }
  116.  
  117. int
  118. iodev_no_put_params(gx_io_device *iodev, gs_param_list *plist)
  119. {    return 0;
  120. }
  121.  
  122. /* ------ %os% ------ */
  123.  
  124. /* The fopen routine is exported for %null. */
  125. int
  126. iodev_os_fopen(gx_io_device *iodev, const char *fname, const char *access,
  127.   FILE **pfile)
  128. {    errno = 0;
  129.     *pfile = fopen(fname, access);
  130.     if ( *pfile == NULL )
  131.       return_error(gs_fopen_errno_to_code(errno));
  132.     return 0;
  133. }
  134.  
  135. /* The fclose routine is exported for %null. */
  136. int
  137. iodev_os_fclose(gx_io_device *iodev, FILE *file)
  138. {    fclose(file);
  139.     return 0;
  140. }
  141.  
  142. private int
  143. os_delete(gx_io_device *iodev, const char *fname)
  144. {    return (unlink(fname) == 0 ? 0 : gs_error_ioerror);
  145. }
  146.  
  147. private int
  148. os_rename(gx_io_device *iodev, const char *from, const char *to)
  149. {    return (rename(from, to) == 0 ? 0 : gs_error_ioerror);
  150. }
  151.  
  152. private int
  153. os_status(gx_io_device *iodev, const char *fname, struct stat *pstat)
  154. {    /* The RS/6000 prototype for stat doesn't include const, */
  155.     /* so we have to explicitly remove the const modifier. */
  156.     return (stat((char *)fname, pstat) < 0 ? gs_error_undefinedfilename : 0);
  157. }
  158.  
  159. private file_enum *
  160. os_enumerate(gx_io_device *iodev, const char *pat, uint patlen,
  161.   gs_memory_t *mem)
  162. {    return gp_enumerate_files_init(pat, patlen, mem);
  163. }
  164.  
  165. private int
  166. os_get_params(gx_io_device *iodev, gs_param_list *plist)
  167. {    /* We aren't going to implement *all* of the Adobe parameters.... */
  168.     int code;
  169.     bool btrue = true;
  170.     gs_param_string tfs;
  171.     param_string_from_string(tfs, "FileSystem");
  172.     if ( (code = param_write_bool(plist, "HasNames", &btrue)) < 0 )
  173.       return code;
  174.     return param_write_name(plist, "Type", &tfs);
  175. }
  176.  
  177. /* ------ Utilities ------ */
  178.  
  179. /* Get the N'th IODevice from the known device table. */
  180. gx_io_device *
  181. gs_getiodevice(int index)
  182. {    if ( index < 0 || index >= gx_io_device_table_count )
  183.         return 0;        /* index out of range */
  184.     return gx_io_device_table[index];
  185. }
  186.  
  187. /* Look up an IODevice name. */
  188. /* The name may be either %device or %device%. */
  189. gx_io_device *
  190. gs_findiodevice(const byte *str, uint len)
  191. {    gx_io_device **pftab;
  192.     if ( len > 1 && str[len - 1] == '%' )
  193.       len--;
  194.     for ( pftab = gx_io_device_table; *pftab != NULL; pftab++ )
  195.       { const char *dname = (*pftab)->dname;
  196.         if ( strlen(dname) == len + 1 && !memcmp(str, dname, len) )
  197.           return *pftab;
  198.       }
  199.     return 0;
  200. }
  201.  
  202. /* Convert an OS error number to a PostScript error */
  203. /* if opening a file fails. */
  204. int
  205. gs_fopen_errno_to_code(int eno)
  206. {    /* Different OSs vary widely in their error codes. */
  207.     /* We try to cover as many variations as we know about. */
  208.     switch ( eno )
  209.       {
  210. #ifdef ENOENT
  211.       case ENOENT:
  212.         return_error(gs_error_undefinedfilename);
  213. #endif
  214. #ifdef ENOFILE
  215. #  ifndef ENOENT
  216. #    define ENOENT ENOFILE
  217. #  endif
  218. #  if ENOFILE != ENOENT
  219.       case ENOFILE:
  220.         return_error(gs_error_undefinedfilename);
  221. #  endif
  222. #endif
  223. #ifdef ENAMETOOLONG
  224.       case ENAMETOOLONG:
  225.         return_error(gs_error_undefinedfilename);
  226. #endif
  227. #ifdef EACCES
  228.       case EACCES:
  229.         return_error(gs_error_invalidfileaccess);
  230. #endif
  231. #ifdef EMFILE
  232.       case EMFILE:
  233.         return_error(gs_error_limitcheck);
  234. #endif
  235. #ifdef ENFILE
  236.       case ENFILE:
  237.         return_error(gs_error_limitcheck);
  238. #endif
  239.       default:
  240.         return_error(gs_error_ioerror);
  241.       }
  242. }
  243.