xref: /csrg-svn/old/refer/inv/inv2.c (revision 12293)
1*12293Stut #ifndef lint
2*12293Stut static char *sccsid = "@(#)inv2.c	4.1 (Berkeley) 05/06/83";
3*12293Stut #endif
4*12293Stut 
5*12293Stut #include <stdio.h>
6*12293Stut #include <assert.h>
7*12293Stut #define LINESIZ 1250
8*12293Stut 
9*12293Stut newkeys (outf, inf, recf, nhash, fd, iflong)
10*12293Stut FILE *outf, *inf, *recf, *fd;
11*12293Stut int *iflong;
12*12293Stut {
13*12293Stut 	/* reads key lines from inf; hashes and writes on outf;
14*12293Stut 	 * writes orig key on recf, records pointer on outf too.
15*12293Stut 	 * format of outf is : hash code space record pointer
16*12293Stut 	 */
17*12293Stut 
18*12293Stut 	long lp, ftell();
19*12293Stut 	long ld = 0;
20*12293Stut 	int ll = 0, lt = 0;
21*12293Stut 	char line[LINESIZ];
22*12293Stut 	char key[30], bkeys[40];
23*12293Stut 	char *p, *s;
24*12293Stut 	char *keyv[500];
25*12293Stut 	int i, nk, ndoc = 0, more = 0, c;
26*12293Stut 
27*12293Stut 	lp = ftell (recf);
28*12293Stut 	while (fgets(line, LINESIZ, inf))
29*12293Stut 	{
30*12293Stut 		p = line;
31*12293Stut 		while (*p != '\t') p++;
32*12293Stut 		*p++ =0;
33*12293Stut 		fputs(line, recf);
34*12293Stut 		if (fd)
35*12293Stut 		{
36*12293Stut 			sprintf(bkeys, ";%ld", ld);
37*12293Stut 			ll = strlen(p);
38*12293Stut 			lt = strlen(bkeys);
39*12293Stut 			fputs(bkeys, recf);
40*12293Stut 			sprintf(bkeys, ",%d", ll);
41*12293Stut 			lt += strlen(bkeys);
42*12293Stut 			fputs(bkeys, recf);
43*12293Stut 			ld += ll;
44*12293Stut 			fputs(p, fd);
45*12293Stut 		}
46*12293Stut 		putc('\n',recf);
47*12293Stut 		for(s=p; *s; s++);
48*12293Stut 		if (*--s == '\n')
49*12293Stut 		{
50*12293Stut 			more=0;
51*12293Stut 			*s=0;
52*12293Stut 		}
53*12293Stut 		else
54*12293Stut 			more=1;
55*12293Stut 		_assert (fd==0 || more==0);
56*12293Stut 		nk = getargs(p, keyv);
57*12293Stut 		if (more)
58*12293Stut 			nk--;
59*12293Stut 		for(i=0; i<nk; i++)
60*12293Stut 			fprintf(outf,"%04d %06ld\n",hash(keyv[i])%nhash, lp);
61*12293Stut # if D1
62*12293Stut 		for(i=0; i<nk; i++)
63*12293Stut 			printf("key %s hash %d\n",keyv[i],hash(keyv[i])%nhash);
64*12293Stut # endif
65*12293Stut 		if (more) /* allow more than LINESIZ keys */
66*12293Stut 		{
67*12293Stut 			strcpy(key, keyv[nk]);
68*12293Stut 			for(s=key; *s; s++);
69*12293Stut 			while ( (c=getc(inf)) != '\n')
70*12293Stut 			{
71*12293Stut 				if (c != ' ')
72*12293Stut 				{
73*12293Stut 					*s++ = c;
74*12293Stut 					continue;
75*12293Stut 				}
76*12293Stut 				*s=0;
77*12293Stut 				if (s>key)
78*12293Stut 					fprintf(outf, "%04d %06ld\n",hash(key)%nhash, lp);
79*12293Stut 				s = key;
80*12293Stut 			}
81*12293Stut 		}
82*12293Stut 		lp += (strlen(line)+lt+1);
83*12293Stut 		ndoc++;
84*12293Stut 	}
85*12293Stut 	*iflong = (lp>=65536L);
86*12293Stut 	if (sizeof(int)>2) *iflong=1; /* force long on VAX */
87*12293Stut 	fclose(recf);
88*12293Stut 	return(ndoc);
89*12293Stut }
90*12293Stut 
91*12293Stut trimnl(p)
92*12293Stut char *p;
93*12293Stut {
94*12293Stut 	while (*p) p++;
95*12293Stut 	p--;
96*12293Stut 	if (*p == '\n') *p=0;
97*12293Stut }
98