xref: /csrg-svn/contrib/bib/src/makekey.c (revision 15063)
113111Srrh #ifndef lint
2*15063Sgarrison static char sccsid[] = "@(#)makekey.c	2.2	09/23/83";
313111Srrh #endif not lint
4*15063Sgarrison #
512915Sgarrison 
612915Sgarrison # include "stdio.h"
712915Sgarrison # include "ctype.h"
812915Sgarrison # include "bib.h"
912915Sgarrison 
1012915Sgarrison char    commlist[MAXCOMM]=   /*  list of strings of common words         */
1112915Sgarrison      "";
1212915Sgarrison int firsttime = 1;
1312915Sgarrison 
1412915Sgarrison /*  makekey(p,max_klen,common):  compresses *p into a key
1512915Sgarrison         folds upper to lower case.  ignores non-alphanumeric
1612915Sgarrison         drops keys of length <= 1.
1712915Sgarrison         drops words in common (name of file of words, one per line)
1812915Sgarrison             (first call determines common for all later calls)
1912915Sgarrison */
2012915Sgarrison makekey(p,max_klen,common)
2112915Sgarrison char *p;
2212915Sgarrison int  max_klen;          /* max key length */
2312915Sgarrison char *common;
2412915Sgarrison {   register char *from, *to, *stop;
2512915Sgarrison 
2612915Sgarrison     if (firsttime) {firsttime= 0; load_comm(common); }
2712915Sgarrison 
2812915Sgarrison     from= p; to= p; stop= max_klen+p;
2912915Sgarrison     while (*from != NULL  &&  to < stop)
3012915Sgarrison     {   if      (islower(*from))      *to++ = *from++;
3112915Sgarrison         else if (isdigit(*from))      *to++ = *from++;
3212915Sgarrison         else if (isupper(*from))    { *to++ = tolower(*from);  from++; }
3312915Sgarrison         else                          from++;
3412915Sgarrison     }
3512915Sgarrison     *to= NULL;
3612915Sgarrison 
3712915Sgarrison     if (to<=p+1 ||
3812915Sgarrison         lookup(commlist, p) )  *p= NULL;
3912915Sgarrison }
4012915Sgarrison 
4112915Sgarrison /*  list is a string of null terminated strings, final string is null.
4212915Sgarrison     p is a null terminated string.
4312915Sgarrison     return 1 if p is a string in list, 0 ow.
4412915Sgarrison */
4512915Sgarrison int lookup(list,p)
4612915Sgarrison char *list, *p;
4712915Sgarrison {   int len;
4812915Sgarrison     len= strlen(list);
4912915Sgarrison     while (len!=0 && strcmp(list,p)!=0)
5012915Sgarrison     {   list += (len+1);
5112915Sgarrison         len= strlen(list);
5212915Sgarrison     }
5312915Sgarrison     return(len!=0);
5412915Sgarrison }
5512915Sgarrison 
5612915Sgarrison /*  read file common into commlist
5712915Sgarrison */
5812915Sgarrison load_comm(common)
5912915Sgarrison char *common;
6012915Sgarrison {   FILE    *commfile;          /*  stream of common words                  */
6112915Sgarrison     char *p, *stop;
6212915Sgarrison     commfile= fopen(common,"r");
6312915Sgarrison     if (commfile==NULL) fprintf(stderr, "cannot open '%s'\n", common);
6412915Sgarrison     else
6512915Sgarrison     {   /* read commfile into commlist  */
6612915Sgarrison             p= commlist;    stop= commlist+MAXCOMM-1;
6712915Sgarrison             while (p<stop && ((*p= getc(commfile))!=EOF))
6812915Sgarrison             {   if (*p=='\n')   *p= NULL;
6912915Sgarrison                 p++;
7012915Sgarrison             }
7112915Sgarrison             if  (*p==EOF)  *p= NULL;
7212915Sgarrison             else
7312915Sgarrison             {   fprintf(stderr, "invert: too many common words\n");
7412915Sgarrison                 commlist[0]= NULL;
7512915Sgarrison             }
7612915Sgarrison         fclose(commfile);
7712915Sgarrison     }
7812915Sgarrison }
7912915Sgarrison 
80