💾 Archived View for runjimmyrunrunyoufuckerrun.com › src › dictidx.c captured on 2021-12-17 at 13:26:06.

View Raw

More Information

-=-=-=-=-=-=-

#include <u.h>
#include <libc.h>
#include <bio.h>

/*
	Creates an index for a dictionary.
	Headwords match from ⇒ to newline.
	Usage: dictidx dict | sort | uniq > dict.idx


int
find⇒(Biobuf *b)
{
	long c;

	while((c = Bgetrune(b)) != 0x21d2)
		if(c < 0)
			return 0;
	return 1;
}

void
main(int argc, char **argv)
{
	char *hw;
	Biobuf *in, *out;
	vlong off, len;
	int n, finished;

	if(argc != 2)
		sysfatal("Usage: %s dict | sort | uniq > dict.idx", argv[0]);

	in = Bopen(argv[1], OREAD);
	if(in == nil) sysfatal("Bopen: %r");
	out = Bfdopen(1, OWRITE);
	if(out == nil) sysfatal("Bopen: %r");

	if(!find⇒(in))
		sysfatal("no headwords found");

	for(finished = 0; !finished; free(hw)){
		hw = Brdstr(in, '\n', 1);
		if(hw == nil)
			sysfatal("eof in headword");
		off = Boffset(in);
		if(!find⇒(in))
			finished++;
		len = Boffset(in) - off - 3;
		n = Bprint(out, "%s\t%lld\t%lld\n", hw, off, len);
		if(n < 0) sysfatal("Bprint: %r");
	}
}