home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xdvi / part03 / pxl_open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-07  |  3.3 KB  |  133 lines

  1. /*
  2.  *    pxl_open.c(path, font, pxlmag, pkmag, name, read_font_index)
  3.  *    Find and open gf, pk, or pxl files in the given path, having the given
  4.  *    name and magnification.  It tries gf files first, followed by pk and pxl
  5.  *    files..  The path variable should be of the form path1:path2:...:pathn,
  6.  *    and each of the paths will be tried successively.  Strings in pathi of
  7.  *    the form %f, %p, and %d will be replaced by the font name, "gf" or "pk"
  8.  *    or "pxl", and the magnification, respectively.  If no %f appears in a
  9.  *    path specifier, then the string "/%f.%d%p" is added on the end.  If
  10.  *    the file is found, then a file pointer is returned, and *name is set to
  11.  *    a string giving the file name.  If the file is not found, then NULL is
  12.  *    returned.  This procedure also returns a pointer to the glyph-reading
  13.  *    procedure associated with the file format.
  14.  * 
  15.  *    Often there are so many fonts that we need to manage the number of
  16.  *    simultaneously open files.  In that case, the variable n_fonts_left
  17.  *    gives the number of open files that are left, (initially MAXINT, set
  18.  *    dynamically) and when it is necessary to close a file, these routines
  19.  *    call close_a_file() which should free up a file descriptor.
  20.  * 
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26.  
  27. #define    PATH_SEP    ':'
  28. #define    DEFAULT_TAIL    "/%f.%d%p"
  29.  
  30. #ifndef    __STDC__
  31. #define    OPEN_MODE    "r"
  32. #else    __STDC__
  33. #define    OPEN_MODE    "rb"
  34. #endif    __STDC__
  35.  
  36. extern    int    n_fonts_left;
  37.  
  38.     /* the corresponding read_char procedures are handled in xdvi.h */
  39. typedef    void (*read_font_index_proc)();
  40.     /* struct font *fontp; */
  41.  
  42. read_font_index_proc read_GF_index, read_PK_index, read_PXL_index;
  43.  
  44. char *malloc(), *index(), *sprintf();
  45.  
  46. #define    Strcpy    (void) strcpy
  47. #define    Sprintf    (void) sprintf
  48.  
  49. static FILE *formatted_open(path, font, pxl, mag, name)
  50. char *path, *font, *pxl;
  51. int mag;
  52. char **name;
  53. {
  54.     char    *p = path,
  55.         nm[128],
  56.         *n = nm,
  57.         c;
  58.     int    f_used = 0;
  59.     FILE    *f;
  60.  
  61.     for (;;) {
  62.         c = *p++;
  63.         if (c==PATH_SEP || c=='\0') {
  64.         if (f_used) break;
  65.         p = DEFAULT_TAIL;
  66.         continue;
  67.         }
  68.         if (c=='%') {
  69.         c = *p++;
  70.         switch (c) {
  71.             case 'f':
  72.             f_used=1;
  73.             Strcpy(n, font);
  74.             break;
  75.             case 'p':
  76.             Strcpy(n, pxl);
  77.             break;
  78.             case 'd':
  79.             Sprintf(n, "%d", mag);
  80.             break;
  81.             default:
  82.             *n++ = c;
  83.             *n = '\0';
  84.         }
  85.         n += strlen(n);
  86.         }
  87.         else *n++ = c;
  88.     }
  89.     *n = '\0';
  90.     f = fopen(nm, OPEN_MODE);
  91.     if (f == NULL && errno == EMFILE) {
  92.         n_fonts_left = 0;
  93.         close_a_file();
  94.         f = fopen(nm, OPEN_MODE);
  95.     }
  96.     if (f != NULL) {
  97.         *name = malloc((unsigned) (n - nm));
  98.         Strcpy(*name, nm);
  99.     }
  100.     return(f);
  101. }
  102.  
  103. FILE *pxl_open(path, font, pxlmag, pkmag, name, read_font_index)
  104. char *path, *font;
  105. int pxlmag, pkmag;
  106. char **name;
  107. read_font_index_proc *read_font_index;
  108. {
  109.     char    *p = path;
  110.     FILE    *f;
  111.  
  112.     for (;;) {
  113.         if (read_GF_index &&
  114.         (f=formatted_open(p, font, "gf", pkmag, name)) != NULL) {
  115.             *read_font_index = read_GF_index;
  116.             return(f);
  117.         }
  118.         if (read_PK_index &&
  119.         (f=formatted_open(p, font, "pk", pkmag, name)) != NULL) {
  120.             *read_font_index = read_PK_index;
  121.             return(f);
  122.         }
  123.         if (read_PXL_index &&
  124.         (f=formatted_open(p, font, "pxl", pxlmag, name)) != NULL) {
  125.             *read_font_index = read_PXL_index;
  126.             return(f);
  127.         }
  128.         p = index(p, PATH_SEP);
  129.         if (p == NULL) return(NULL);
  130.         ++p;
  131.     }
  132. }
  133.