xref: /original-bsd/contrib/bib/src/makekey.c (revision 0999a820)
1 #ifndef lint
2 static char sccsid[] = "@(#)makekey.c	2.3	05/27/93";
3 #endif not lint
4 #
5 
6 # include "stdio.h"
7 # include "ctype.h"
8 # include "bib.h"
9 
10 char    commlist[MAXCOMM]=   /*  list of strings of common words         */
11      "";
12 int firsttime = 1;
13 
14 /*  makekey(p,max_klen,common):  compresses *p into a key
15         folds upper to lower case.  ignores non-alphanumeric
16         drops keys of length <= 1.
17         drops words in common (name of file of words, one per line)
18             (first call determines common for all later calls)
19 */
20 makekey(p,max_klen,common)
21 char *p;
22 int  max_klen;          /* max key length */
23 char *common;
24 {   register char *from, *to, *stop;
25 
26     if (firsttime) {firsttime= 0; load_comm(common); }
27 
28     from= p; to= p; stop= max_klen+p;
29     while (*from != NULL  &&  to < stop)
30     {   if      (islower(*from))      *to++ = *from++;
31         else if (isdigit(*from))      *to++ = *from++;
32         else if (isupper(*from))    { *to++ = tolower(*from);  from++; }
33         else                          from++;
34     }
35     *to= NULL;
36 
37     if (to<=p+1 ||
38         lookup(commlist, p) )  *p= NULL;
39 }
40 
41 /*  list is a string of null terminated strings, final string is null.
42     p is a null terminated string.
43     return 1 if p is a string in list, 0 ow.
44 */
45 int lookup(list,p)
46 char *list, *p;
47 {   int len;
48     len= strlen(list);
49     while (len!=0 && strcmp(list,p)!=0)
50     {   list += (len+1);
51         len= strlen(list);
52     }
53     return(len!=0);
54 }
55 
56 /*  read file common into commlist
57 */
58 load_comm(common)
59 char *common;
60 {   FILE    *commfile;          /*  stream of common words                  */
61     char *p, *stop;
62     commfile= fopen(common,"r");
63     if (commfile==NULL) fprintf(stderr,"cannot open '%s'\n", common);
64     else
65     {   /* read commfile into commlist  */
66             p= commlist;    stop= commlist+MAXCOMM-1;
67             while (p<stop && ((*p= getc(commfile))!=EOF))
68             {   if (*p=='\n')   *p= NULL;
69                 p++;
70             }
71             if  (*p==EOF)  *p= NULL;
72             else
73             {   fprintf(stderr,"invert: too many common words\n");
74                 commlist[0]= NULL;
75             }
76         fclose(commfile);
77     }
78 }
79 
80