Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/refer/index.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "refer.h"
#include <errno.h>

void initindex(Index *ip)
{
	ip->n = 0;
	initlist(&ip->hash);
	initlist(&ip->freq);
}

void growindex(Index *ip, long size)
{
	growlist(&ip->hash, size);
	growlist(&ip->freq, size);
}

void emptyindex(Index *ip)
{
	ip->n = 0;
	emptylist(&ip->hash);
	emptylist(&ip->freq);
}

static void badform(char *s)
{
	err("%s index file -- rerun pubindex?", s);
}

int getindex(Index *ip, FILE *fp)
{
	long nhash;
	int i, c;

	if (getl(fp) != REFMAGIC)
		badform("old format");
	nhash = getl(fp);
	if (nhash&~0x7fff) /* probably byte swapped */
		badform("improbable hash table size in");
	growindex(ip, nhash);
	ip->n = nhash;
	for (i=0; i<nhash; i++)
		ip->hash.el[i] = getl(fp);
	if ((c = getc(fp)) != EOF) {
		ungetc(c, fp);
		for (i=0; i<nhash; i++)
			ip->freq.el[i] = getl(fp);
	} else
		emptylist(&ip->freq);
	return ip->n;
}

void putindex(Index *ip, FILE *fp)
{
	int i;

	putl(REFMAGIC, fp);
	putl((long)ip->n, fp);
	if (ip->hash.el)
		for (i=0; i<ip->n; i++)
			putl(ip->hash.el[i], fp);
	if (ip->freq.el)
		for (i=0; i<ip->n; i++)
			putl(ip->freq.el[i], fp);
	fflush(fp);
	if (ferror(fp))
		err("error writing index file.  %s", strerror(errno));
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.