|
root / base / usr / src / cmd / refer / inv2.c
inv2.c C 108 lines 2.1 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved. The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#include <stdio.h>
#include <assert.h>
#define	LINESIZ 1250

extern int hash();

int
newkeys(FILE *outf, FILE *inf, FILE *recf, int nhash, FILE *fd, int *iflong)
{
	/*
	 * reads key lines from inf; hashes and writes on outf;
	 * writes orig key on recf, records pointer on outf too.
	 * format of outf is : hash code space record pointer
	 */

	long lp, ftell();
	long ld = 0;
	int ll = 0, lt = 0;
	char line[LINESIZ];
	char key[30], bkeys[40];
	char *p, *s;
	char *keyv[500];
	int i, nk, ndoc = 0, more = 0, c;

	lp = ftell(recf);
	while (fgets(line, LINESIZ, inf)) {
		p = line;
		while (*p != '\t') p++;
		*p++ = 0;
		fputs(line, recf);
		if (fd) {
			sprintf(bkeys, ";%ld", ld);
			ll = strlen(p);
			lt = strlen(bkeys);
			fputs(bkeys, recf);
			sprintf(bkeys, ",%d", ll);
			lt += strlen(bkeys);
			fputs(bkeys, recf);
			ld += ll;
			fputs(p, fd);
		}
		putc('\n', recf);
		for (s = p; *s; s++)
			;
		if (*--s == '\n') {
			more = 0;
			*s = 0;
		} else
			more = 1;
		assert(fd == 0 || more == 0);
		nk = getargs(p, keyv);
		if (more)
			nk--;
		for (i = 0; i < nk; i++)
			fprintf(outf, "%04d %06ld\n", hash(keyv[i])%nhash, lp);
#if D1
		for (i = 0; i < nk; i++)
			printf("key %s hash %d\n",
			    keyv[i], hash(keyv[i])%nhash);
#endif
		if (more) {	/* allow more than LINESIZ keys */
			strcpy(key, keyv[nk]);
			for (s = key; *s; s++)
				;
			while ((c = getc(inf)) != '\n') {
				if (c != ' ') {
					*s++ = c;
					continue;
				}
				*s = 0;
				if (s > key)
					fprintf(outf, "%04d %06ld\n",
					    hash(key)%nhash, lp);
				s = key;
			}
		}
		lp += (strlen(line)+lt+1);
		ndoc++;
	}
	*iflong = (lp >= 65536L);
	if (sizeof (int) > 2) *iflong = 1; /* force long on VAX */
	fclose(recf);
	return (ndoc);
}


void
trimnl(char *p)
{
	while (*p) p++;
	p--;
	if (*p == '\n') *p = 0;
}