home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume14 / cat2deskjet / part01 / ftbl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-30  |  2.6 KB  |  143 lines

  1. /*
  2.  *    ftbl -- font table management routines
  3.  *  Copyright (C) 1990 Vassilis Prevelakis
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation and included with this distribution in the
  8.  *  file named LICENSE.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  Bugs fixes, comments etc. to:
  20.  *       Vasilis Prevelakis
  21.  *       Centre Universitaire d'Informatique (CUI)
  22.  *       12 Rue du Lac, Geneva, Switzerland CH-1207
  23.  *  email: vp@cui.unige.ch
  24.  *  uucp:  ...!mcsun!cui!vp
  25.  */
  26.  
  27. #include <stdio.h>
  28.  
  29. #define FT_SIZE 16
  30.  
  31. static FILE* fd;
  32.  
  33. static struct ft_ent {
  34.     short fid;
  35.     char name[256];
  36. } ft_tbl[FT_SIZE];
  37. static char fname[256];
  38. static int nextent;
  39.  
  40. ft_init(fontdir)
  41. char* fontdir;
  42. {
  43.     char buf[256];
  44.     int c, i, ent;
  45.     char *vp;
  46.     int fid;
  47.  
  48.     /* initialize table */
  49.     for (ent=0; ent<FT_SIZE; ent++)
  50.         ft_tbl[ent].fid = -1;
  51.     nextent = 0;
  52.         
  53.     sprintf(fname, "%s/font.tbl", fontdir);
  54.     if ((fd = fopen(fname, "rt")) == NULL)
  55.     {
  56.         fprintf(stderr,
  57.             "Couldn't open cache file: %s, ignored\n", fname);
  58.         return;
  59.     }
  60.  
  61.     /* load table */
  62.     for (ent=0; ent<FT_SIZE; ent++)
  63.     {
  64.         if (fscanf(fd, "%d: %s", &fid, buf) != 2)
  65.             break;
  66.         ft_tbl[ent].fid = fid;
  67.         strcpy(ft_tbl[ent].name, buf);
  68.     }
  69.     fclose(fd);
  70. }
  71.  
  72. short ft_getnextent()
  73. {
  74.     return(ft_tbl[nextent++].fid);
  75. }
  76.  
  77. char* ft_getname(fid)
  78. short fid;
  79. {
  80.     int i;
  81.  
  82.     for (i=0; i < FT_SIZE; i++)
  83.         if (ft_tbl[i].fid == fid)
  84.             return(ft_tbl[i].name);
  85.     return(NULL);
  86. }
  87.  
  88. int ft_isthere(fid)
  89. short fid;
  90. {
  91.     int i;
  92.  
  93.     for (i=0; i < FT_SIZE; i++)
  94.         if (ft_tbl[i].fid == fid)
  95.             return(1);
  96.     return(0);
  97. }
  98.  
  99. ft_delent(fid)
  100. short fid;
  101. {
  102.     int i;
  103.  
  104.     for (i=0; i < FT_SIZE; i++)
  105.         if (ft_tbl[i].fid == fid)
  106.             ft_tbl[i].fid = -1;
  107. }
  108.  
  109. int ft_addent(fid, s)
  110. short fid;
  111. char* s;
  112. {
  113.     int i;
  114.  
  115.     for (i=0; i < FT_SIZE; i++)
  116.         if (ft_tbl[i].fid < 0)
  117.         {
  118.             ft_tbl[i].fid = fid;
  119.             strcpy(ft_tbl[i].name, s);
  120.             return(i);
  121.         }
  122.     return(0);
  123. }
  124.  
  125. ft_close()
  126. {
  127.     int i;
  128.  
  129.     unlink(fname);
  130.     if ((fd = fopen(fname, "wt")) == NULL)
  131.     {
  132.         fprintf(stderr, "Couldn't open font cache file: %s\n", fname);
  133.         exit(1);
  134.     }
  135.     
  136.     for (i=0; i < FT_SIZE; i++)
  137.         if (ft_tbl[i].fid > 0)
  138.             fprintf(fd, "%d: %s\n", ft_tbl[i].fid, ft_tbl[i].name);
  139.     fclose(fd);
  140. }
  141.  
  142.  
  143.