xref: /csrg-svn/games/boggle/mkindex/mkindex.c (revision 63301)
163300Sbostic /*-
2*63301Sbostic  * Copyright (c) 1993
3*63301Sbostic  *	The Regents of the University of California.  All rights reserved.
463300Sbostic  *
563300Sbostic  * This code is derived from software contributed to Berkeley by
663300Sbostic  * Barry Brachman.
763300Sbostic  *
863300Sbostic  * %sccs.include.redist.c%
963300Sbostic  */
1063098Sbostic 
1163300Sbostic #ifndef lint
12*63301Sbostic static char copyright[] =
13*63301Sbostic "@(#) Copyright (c) 1993\n\
14*63301Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1563300Sbostic #endif /* not lint */
1663300Sbostic 
1763300Sbostic #ifndef lint
18*63301Sbostic static char sccsid[] = "@(#)mkindex.c	8.1 (Berkeley) 06/11/93";
1963300Sbostic #endif /* not lint */
2063300Sbostic 
2163098Sbostic #include <stdio.h>
2263098Sbostic 
2363098Sbostic #include "bog.h"
2463098Sbostic 
2563300Sbostic char *nextword __P((FILE *, char *, int *, int *));
2663300Sbostic 
2763300Sbostic int
main(argc,argv)2863098Sbostic main(argc, argv)
2963300Sbostic 	int argc;
3063300Sbostic 	char *argv[];
3163098Sbostic {
3263098Sbostic 	int clen, rlen, prev;
3363098Sbostic 	long off, start;
3463300Sbostic 	char buf[MAXWORDLEN + 1];
3563098Sbostic 
3663098Sbostic 	prev = '\0';
3763098Sbostic 	off = start = 0L;
3863300Sbostic 	while (nextword(stdin, buf, &clen, &rlen) != NULL) {
3963098Sbostic 		if (*buf != prev) {
4063098Sbostic 			if (prev != '\0')
4163098Sbostic 				printf("%c %6ld %6ld\n", prev, start, off - 1);
4263098Sbostic 			prev = *buf;
4363098Sbostic 			start = off;
4463098Sbostic 		}
4563098Sbostic 		off += clen + 1;
4663098Sbostic 	}
4763098Sbostic 	printf("%c %6ld %6ld\n", prev, start, off - 1);
4863098Sbostic 	exit(0);
4963098Sbostic }
5063098Sbostic 
5163098Sbostic /*
5263098Sbostic  * Return the next word in the compressed dictionary in 'buffer' or
5363098Sbostic  * NULL on end-of-file
5463098Sbostic  * Also set clen to the length of the compressed word (for mkindex) and
5563098Sbostic  * rlen to the strlen() of the real word
5663098Sbostic  */
5763098Sbostic char *
nextword(fp,buffer,clen,rlen)5863098Sbostic nextword(fp, buffer, clen, rlen)
5963300Sbostic 	FILE *fp;
6063300Sbostic 	char *buffer;
6163300Sbostic 	int *clen, *rlen;
6263098Sbostic {
6363300Sbostic 	register int ch, pcount;
6463098Sbostic 	register char *p, *q;
6563098Sbostic 	static char buf[MAXWORDLEN + 1];
6663098Sbostic 	static int first = 1;
6763098Sbostic 	static int lastch = 0;
6863098Sbostic 
6963098Sbostic    	if (first) {
7063300Sbostic 		if ((pcount = getc(fp)) == EOF)
7163300Sbostic 			return (NULL);
7263098Sbostic 		first = 0;
7363098Sbostic 	}
7463098Sbostic 	else if ((pcount = lastch) == EOF)
7563300Sbostic 		return (NULL);
7663098Sbostic 
7763098Sbostic 	p = buf + (*clen = pcount);
7863098Sbostic 
7963300Sbostic 	while ((ch = getc(fp)) != EOF && ch >= 'a')
8063098Sbostic 			*p++ = ch;
8163300Sbostic 		lastch = ch;
8263300Sbostic 	*p = '\0';
8363098Sbostic 
8463098Sbostic 	*rlen = (int) (p - buf);
8563098Sbostic 	*clen = *rlen - *clen;
8663098Sbostic 
8763098Sbostic 	p = buf;
8863098Sbostic 	q = buffer;
8963098Sbostic 	while ((*q++ = *p) != '\0') {
9063098Sbostic 		if (*p++ == 'q')
9163098Sbostic 			*q++ = 'u';
9263098Sbostic 	}
9363300Sbostic 	return (buffer);
9463098Sbostic }
95