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