xref: /titanic_41/usr/src/cmd/refer/inv2.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1980 Regents of the University of California.
7  * All rights reserved. The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13  * All Rights Reserved.
14  */
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 #include <stdio.h>
19 #include <assert.h>
20 #define LINESIZ 1250
21 
22 newkeys (outf, inf, recf, nhash, fd, iflong)
23 FILE *outf, *inf, *recf, *fd;
24 int *iflong;
25 {
26 	/* reads key lines from inf; hashes and writes on outf;
27 	 * writes orig key on recf, records pointer on outf too.
28 	 * format of outf is : hash code space record pointer
29 	 */
30 
31 	long lp, ftell();
32 	long ld = 0;
33 	int ll = 0, lt = 0;
34 	char line[LINESIZ];
35 	char key[30], bkeys[40];
36 	char *p, *s;
37 	char *keyv[500];
38 	int i, nk, ndoc = 0, more = 0, c;
39 
40 	lp = ftell (recf);
41 	while (fgets(line, LINESIZ, inf))
42 	{
43 		p = line;
44 		while (*p != '\t') p++;
45 		*p++ =0;
46 		fputs(line, recf);
47 		if (fd)
48 		{
49 			sprintf(bkeys, ";%ld", ld);
50 			ll = strlen(p);
51 			lt = strlen(bkeys);
52 			fputs(bkeys, recf);
53 			sprintf(bkeys, ",%d", ll);
54 			lt += strlen(bkeys);
55 			fputs(bkeys, recf);
56 			ld += ll;
57 			fputs(p, fd);
58 		}
59 		putc('\n',recf);
60 		for(s=p; *s; s++);
61 		if (*--s == '\n')
62 		{
63 			more=0;
64 			*s=0;
65 		}
66 		else
67 			more=1;
68 		assert (fd==0 || more==0);
69 		nk = getargs(p, keyv);
70 		if (more)
71 			nk--;
72 		for(i=0; i<nk; i++)
73 			fprintf(outf,"%04d %06ld\n",hash(keyv[i])%nhash, lp);
74 # if D1
75 		for(i=0; i<nk; i++)
76 			printf("key %s hash %d\n",keyv[i],hash(keyv[i])%nhash);
77 # endif
78 		if (more) /* allow more than LINESIZ keys */
79 		{
80 			strcpy(key, keyv[nk]);
81 			for(s=key; *s; s++);
82 			while ( (c=getc(inf)) != '\n')
83 			{
84 				if (c != ' ')
85 				{
86 					*s++ = c;
87 					continue;
88 				}
89 				*s=0;
90 				if (s>key)
91 					fprintf(outf, "%04d %06ld\n",hash(key)%nhash, lp);
92 				s = key;
93 			}
94 		}
95 		lp += (strlen(line)+lt+1);
96 		ndoc++;
97 	}
98 	*iflong = (lp>=65536L);
99 	if (sizeof(int)>2) *iflong=1; /* force long on VAX */
100 	fclose(recf);
101 	return(ndoc);
102 }
103 
104 trimnl(p)
105 char *p;
106 {
107 	while (*p) p++;
108 	p--;
109 	if (*p == '\n') *p=0;
110 }
111