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