xref: /csrg-svn/old/refer/inv/inv2.c (revision 48297)
1*48297Sbostic /*-
2*48297Sbostic  * %sccs.include.proprietary.c%
3*48297Sbostic  */
4*48297Sbostic 
512293Stut #ifndef lint
6*48297Sbostic static char sccsid[] = "@(#)inv2.c	4.2 (Berkeley) 04/18/91";
7*48297Sbostic #endif /* not lint */
812293Stut 
912293Stut #include <stdio.h>
1012293Stut #include <assert.h>
1112293Stut #define LINESIZ 1250
1212293Stut 
newkeys(outf,inf,recf,nhash,fd,iflong)1312293Stut newkeys (outf, inf, recf, nhash, fd, iflong)
1412293Stut FILE *outf, *inf, *recf, *fd;
1512293Stut int *iflong;
1612293Stut {
1712293Stut 	/* reads key lines from inf; hashes and writes on outf;
1812293Stut 	 * writes orig key on recf, records pointer on outf too.
1912293Stut 	 * format of outf is : hash code space record pointer
2012293Stut 	 */
2112293Stut 
2212293Stut 	long lp, ftell();
2312293Stut 	long ld = 0;
2412293Stut 	int ll = 0, lt = 0;
2512293Stut 	char line[LINESIZ];
2612293Stut 	char key[30], bkeys[40];
2712293Stut 	char *p, *s;
2812293Stut 	char *keyv[500];
2912293Stut 	int i, nk, ndoc = 0, more = 0, c;
3012293Stut 
3112293Stut 	lp = ftell (recf);
3212293Stut 	while (fgets(line, LINESIZ, inf))
3312293Stut 	{
3412293Stut 		p = line;
3512293Stut 		while (*p != '\t') p++;
3612293Stut 		*p++ =0;
3712293Stut 		fputs(line, recf);
3812293Stut 		if (fd)
3912293Stut 		{
4012293Stut 			sprintf(bkeys, ";%ld", ld);
4112293Stut 			ll = strlen(p);
4212293Stut 			lt = strlen(bkeys);
4312293Stut 			fputs(bkeys, recf);
4412293Stut 			sprintf(bkeys, ",%d", ll);
4512293Stut 			lt += strlen(bkeys);
4612293Stut 			fputs(bkeys, recf);
4712293Stut 			ld += ll;
4812293Stut 			fputs(p, fd);
4912293Stut 		}
5012293Stut 		putc('\n',recf);
5112293Stut 		for(s=p; *s; s++);
5212293Stut 		if (*--s == '\n')
5312293Stut 		{
5412293Stut 			more=0;
5512293Stut 			*s=0;
5612293Stut 		}
5712293Stut 		else
5812293Stut 			more=1;
5912293Stut 		_assert (fd==0 || more==0);
6012293Stut 		nk = getargs(p, keyv);
6112293Stut 		if (more)
6212293Stut 			nk--;
6312293Stut 		for(i=0; i<nk; i++)
6412293Stut 			fprintf(outf,"%04d %06ld\n",hash(keyv[i])%nhash, lp);
6512293Stut # if D1
6612293Stut 		for(i=0; i<nk; i++)
6712293Stut 			printf("key %s hash %d\n",keyv[i],hash(keyv[i])%nhash);
6812293Stut # endif
6912293Stut 		if (more) /* allow more than LINESIZ keys */
7012293Stut 		{
7112293Stut 			strcpy(key, keyv[nk]);
7212293Stut 			for(s=key; *s; s++);
7312293Stut 			while ( (c=getc(inf)) != '\n')
7412293Stut 			{
7512293Stut 				if (c != ' ')
7612293Stut 				{
7712293Stut 					*s++ = c;
7812293Stut 					continue;
7912293Stut 				}
8012293Stut 				*s=0;
8112293Stut 				if (s>key)
8212293Stut 					fprintf(outf, "%04d %06ld\n",hash(key)%nhash, lp);
8312293Stut 				s = key;
8412293Stut 			}
8512293Stut 		}
8612293Stut 		lp += (strlen(line)+lt+1);
8712293Stut 		ndoc++;
8812293Stut 	}
8912293Stut 	*iflong = (lp>=65536L);
9012293Stut 	if (sizeof(int)>2) *iflong=1; /* force long on VAX */
9112293Stut 	fclose(recf);
9212293Stut 	return(ndoc);
9312293Stut }
9412293Stut 
trimnl(p)9512293Stut trimnl(p)
9612293Stut char *p;
9712293Stut {
9812293Stut 	while (*p) p++;
9912293Stut 	p--;
10012293Stut 	if (*p == '\n') *p=0;
10112293Stut }
102