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

  1. /* Copyright (C) 1992, 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. /* gp_ntfs.c */
  20. /* file system stuff for MS-Windows WIN32 and MS-Windows NT*/
  21. /* hacked from gp_dosfs.c by Russell Lang */
  22.  
  23. #include "stdio_.h"
  24. #include <fcntl.h>
  25. #include "dos_.h"
  26. #include "memory_.h"
  27. #include "string_.h"
  28. #include "gstypes.h"
  29. #include "gsmemory.h"
  30. #include "gsstruct.h"
  31. #include "gp.h"
  32. #include "gsutil.h"
  33. #include "windows_.h"
  34.  
  35. /* ------ Printer accessing ------ */
  36.  
  37. /* Put a printer file (which might be stdout) into binary or text mode. */
  38. /* This is not a standard gp procedure, */
  39. /* but all MS-DOS configurations need it. */
  40. void
  41. gp_set_printer_binary(int prnfno, int binary)
  42. {
  43.     /* UNIMPLEMENTED */
  44. }
  45.  
  46. /* ------ File names ------ */
  47.  
  48. /* Define the character used for separating file names in a list. */
  49. const char gp_file_name_list_separator = ';';
  50.  
  51. /* Define the string to be concatenated with the file mode */
  52. /* for opening files without end-of-line conversion. */
  53. const char gp_fmode_binary_suffix[] = "b";
  54. /* Define the file modes for binary reading or writing. */
  55. const char gp_fmode_rb[] = "rb";
  56. const char gp_fmode_wb[] = "wb";
  57.  
  58. /* Answer whether a file name contains a directory/device specification, */
  59. /* i.e. is absolute (not directory- or device-relative). */
  60. bool
  61. gp_file_name_is_absolute(const char *fname, uint len)
  62. {    /* A file name is absolute if it contains a drive specification */
  63.     /* (second character is a :) or if it start with / or \. */
  64.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  65.         (len >= 2 && fname[1] == ':')) );
  66. }
  67.  
  68. /* Answer the string to be used for combining a directory/device prefix */
  69. /* with a base file name.  The file name is known to not be absolute. */
  70. const char *
  71. gp_file_name_concat_string(const char *prefix, uint plen,
  72.   const char *fname, uint len)
  73. {    if ( plen > 0 )
  74.       switch ( prefix[plen - 1] )
  75.        {    case ':': case '/': case '\\': return "";
  76.        };
  77.     return "\\";
  78. }
  79.  
  80. /* ------ File enumeration ------ */
  81.  
  82. struct file_enum_s {
  83.     WIN32_FIND_DATA find_data;
  84.     HANDLE find_handle;
  85.     char *pattern;            /* orig pattern + modified pattern */
  86.     int patlen;            /* orig pattern length */
  87.     int pat_size;            /* allocate space for pattern */
  88.     int first_time;
  89.     gs_memory_t *memory;
  90. };
  91. gs_private_st_ptrs1(st_file_enum, struct file_enum_s, "file_enum",
  92.   file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern);
  93.  
  94. /* Initialize an enumeration.  Note that * and ? in a directory */
  95. /* don't work, and \ is taken literally unless a second \ follows. */
  96. file_enum *
  97. gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t *mem)
  98. {    file_enum *pfen = gs_alloc_struct(mem, file_enum, &st_file_enum, "gp_enumerate_files");
  99.     int pat_size = 2 * patlen + 1;
  100.     char *pattern;
  101.     if ( pfen == 0 ) return 0;
  102.  
  103.     /* pattern could be allocated as a string, */
  104.     /* but it's simpler for GC and freeing to allocate it as bytes. */
  105.  
  106.     pattern = (char *)gs_alloc_bytes(mem, pat_size,
  107.                      "gp_enumerate_files(pattern)");
  108.     if ( pattern == 0 ) return 0;
  109.     memcpy(pattern, pat, patlen);
  110.     pattern[patlen] = 0;
  111.     pfen->pattern = pattern;
  112.     pfen->patlen = patlen;
  113.     pfen->pat_size = pat_size;
  114.     pfen->memory = mem;
  115.     pfen->first_time = 1;
  116.     memset(&pfen->find_data, 0, sizeof(pfen->find_data));
  117.     pfen->find_handle = INVALID_HANDLE_VALUE;
  118.     return pfen;
  119. }
  120.  
  121. /* Enumerate the next file. */
  122. uint
  123. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  124. {    int code = 0;
  125.     uint len;
  126. top:    if ( pfen->first_time )
  127.        {
  128.         pfen->find_handle = FindFirstFile(pfen->pattern, &(pfen->find_data));
  129.         if (pfen->find_handle == INVALID_HANDLE_VALUE)
  130.             code = -1;
  131.             pfen->first_time = 0;
  132.        }
  133.     else
  134.        {
  135.         if (!FindNextFile(pfen->find_handle, &(pfen->find_data)))
  136.             code = -1;
  137.        }
  138.     if ( code != 0 )
  139.        {    /* All done, clean up. */
  140.         gp_enumerate_files_close(pfen);
  141.         return ~(uint)0;
  142.        }
  143.     len = strlen(pfen->find_data.cFileName);
  144.     memcpy(ptr, pfen->find_data.cFileName, len);
  145.     return len;
  146. }
  147.  
  148. /* Clean up the file enumeration. */
  149. void
  150. gp_enumerate_files_close(file_enum *pfen)
  151. {    gs_memory_t *mem = pfen->memory;
  152.     if (pfen->find_handle != INVALID_HANDLE_VALUE)
  153.         FindClose(pfen->find_handle);
  154.     gs_free_object(mem, pfen->pattern,
  155.                "gp_enumerate_files_close(pattern)");
  156.     gs_free_object(mem, pfen, "gp_enumerate_files_close");
  157. }
  158.