#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));
}
|