home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / dv_x / dvix.zip / PATH_OPE.C < prev    next >
C/C++ Source or Header  |  1992-10-02  |  6KB  |  188 lines

  1. /*
  2.  *    $Source: /user/bruce/dvix/RCS/path_open.c,v $
  3.  *    $Author: root $
  4.  *    $Locker:  $
  5.  *    $Header: /user/bruce/dvix/RCS/path_open.c,v 1.1 1991/11/04 00:57:29 root Exp $
  6.  *
  7.  *    $Log: path_open.c,v $
  8.  * Revision 1.1  1991/11/04  00:57:29  root
  9.  * Initial revision
  10.  *
  11.  * Revision 1.4  88/07/28  11:47:00  eichin
  12.  * fixed next_path bugs. it has been tested now.
  13.  * 
  14.  * Revision 1.3  88/07/28  09:59:56  eichin
  15.  * replace execat (which was really proprietary) with next_path, which
  16.  * although much slower, is quite clear about what it is doing. Making it
  17.  * efficient would waste a lot of time, as other real xdvi stuff needs it
  18.  * more.
  19.  * 
  20.  * Revision 1.2  88/07/28  09:48:25  eichin
  21.  * added stuff from lcs for font subdirectories.
  22.  * 
  23.  * Revision 1.1  87/05/14  11:00:44  eichin
  24.  * Initial revision
  25.  * 
  26.  * 
  27.  */
  28. #ifndef lint
  29. static char *rcsid_path_open_c = "$Header: /user/bruce/dvix/RCS/path_open.c,v 1.1 1991/11/04 00:57:29 root Exp $";
  30. #endif    lint
  31. /*
  32.  * path open - search for a file in a given path.
  33.  *        -Mark Eichin...from my other random toolkits.5/14/1987
  34.  */
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #include <sys/dir.h>
  38. #include "local.h"    /* mod for DV/X, MS-DOS (GBP) */
  39.  
  40. char *index(), *rindex();
  41.  
  42. /*
  43.  * next_path
  44.  * returns the name relative to the first component of 'path' in
  45.  * 'fullname' and returns the path without the first component.
  46.  */
  47. char *next_path(path, name, fullname)
  48.      char *path, *name, *fullname;
  49. {
  50.   char *scan;
  51.  
  52.   scan = index(path,';');  /* : to ; mod for DV/X and MS-DOS (GBP) */
  53.   if(scan)
  54.     {
  55.       /* not the last component of the path, only take the first one */
  56.       strncpy(fullname, path, scan-path);
  57.       fullname[scan-path] = '\0';
  58.     }
  59.   else
  60.     {
  61.       /* the last path component, take the whole thing */
  62.       strcpy(fullname, path);
  63.     }
  64.   strcat(fullname, "/");    /* append a slash, they can be redundant */
  65.   strcat(fullname, name);    /* and then the name */
  66.  
  67.   return(scan?(scan+1):NULL);
  68. }
  69.  
  70. FILE *fopen_path(pathvar,name,stat)
  71.      register char *pathvar, *name, *stat;
  72. {
  73.   char tname[128];
  74.   register char *cp = index(name,'/')?"":pathvar;
  75.   FILE *fil;
  76.  
  77.   do {
  78.     cp=next_path(cp, name, tname);
  79.     if((fil=fopen(tname,stat))!=NULL)
  80.       {
  81.     return(fil);
  82.       }
  83.   } while(cp);
  84.   return(NULL);
  85. }
  86.  
  87. FILE *fopen_font_path(pathvar,name,stat,pksize,size) /* mod for DV/X,
  88.                                                         MS-DOS (GBP) */
  89.      register char *pathvar, *name, *stat;
  90.      register int pksize, size;
  91. {
  92.   /* The goal here is to try to open SOMETHING, whatever it really is */
  93.   char tname[128], family[128], *dotsplit;
  94.   char subname[128]; /* mod for DV/X, MS-DOS (GBP) */
  95.   int point, mag, len;
  96.   register char *cp = index(name,'/')?"":pathvar;
  97.   FILE *fil;
  98.   DIR *dirstr;
  99.   struct direct *dirrec;
  100.   char qf[128], bestfil[128]; int qpt, qmag;
  101.   int delp, delm;
  102.   int mindelp=99999999, mindelm=99999999; int bestpt, bestmag;
  103.  
  104.   sprintf(subname,FONT_SUBDIR_PREFIX,pksize); /* mod for DV/X, MS-DOS (GBP) */
  105.   strcat(subname,name);                  /* mod for DV/X, MS-DOS (GBP) */
  106.   do {
  107. /*    cp=next_path(cp, name, tname);        try normal path extension */
  108.     cp=next_path(cp, subname, tname);    /* mod for DV/X, MS-DOS (GBP) */
  109.     if((fil=fopen(tname,stat))!=NULL)
  110.       {
  111.     return(fil);
  112.       }
  113.     /*
  114.      * Some systems store the fonts in directories by family, and then
  115.      * each size/scale within that directory... so we strip off the
  116.      * extension, then put the whole file name back on. This should
  117.      * really be done with something like fontdesc files.
  118.      * (from rgcote and shep@lcs.mit.edu, fixed and installed by
  119.      * eichin@athena.mit.edu)
  120.      */
  121.     dotsplit = rindex(tname, '.');
  122.     *dotsplit++ = '/';
  123.     *dotsplit = '\0';
  124.     strcat(tname, name);
  125.     if ((fil = fopen(tname,stat)) != NULL)
  126.       {
  127.     return(fil);
  128.       }
  129.   } while(cp);
  130.  
  131.   if(index(name,'/'))
  132.     return(NULL);        /* never try again, if absolute */
  133.   /* if we get here, we must look real hard... */
  134.  
  135.   cp = pathvar;
  136.   point = -1;
  137.   mag = -1;
  138.   (void) sscanf(name,"%[^0123456789.]%d.%d",family,&point,&mag);
  139.  
  140.  
  141.   len=strlen(family);
  142.   do {
  143.     cp=next_path(cp,"",tname);
  144.     dirstr=opendir(tname);
  145. #ifdef DEBUG
  146.     fprintf(stderr,"This dir:%s\n",tname);
  147.     fprintf(stderr,"dirstr=%x,dir=<%s>\n",dirstr,tname);
  148.     if(!dirstr) perror(tname);    /* for discovering too many open files. */
  149. #endif DEBUG
  150.     if(dirstr)
  151.       {
  152.     for(dirrec=readdir(dirstr); dirrec!=NULL; dirrec=readdir(dirstr))
  153.       {
  154.         if(dirrec->d_namlen>=len && !strncmp(dirrec->d_name,family,len))
  155.           {
  156. #ifdef DEBUG
  157.         fprintf(stderr,"Font:%s\n",dirrec->d_name);
  158. #endif DEBUG
  159.         qpt=(-1); qmag=(-1);
  160.         (void) sscanf(dirrec->d_name,"%[^0123456789.]%d.%d",
  161.                   qf,&qpt,&qmag);
  162.         delp=abs(point-qpt); delm=abs(mag-qmag); /* mag from??? */
  163.         if ((delp < mindelp) || (delp == mindelp && delm < mindelm))
  164.           {
  165.             mindelp=delp; mindelm=delm;
  166.             bestpt=qpt; bestmag=qmag;
  167.             strcpy(bestfil,tname);
  168.             strcat(bestfil,"/");
  169.             strcat(bestfil,dirrec->d_name);
  170. #ifdef DEBUG
  171.             fprintf(stderr,"Current best:%s\n",bestfil);
  172. #endif DEBUG
  173.           }
  174.           }
  175.       }
  176.     closedir(dirstr);
  177.       }
  178.   } while(cp);
  179.   if(mindelp==99999999) return(NULL);
  180.   else 
  181.     {
  182.       fprintf(stderr,"Using <%s> instead of <%s>, continuing.\n",
  183.           rindex(bestfil,'/'),
  184.           name);
  185.       return(fopen(bestfil,stat));
  186.     }
  187. }
  188.