xref: /original-bsd/games/boggle/mkindex/mkindex.c (revision 3705696b)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Barry Brachman.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char copyright[] =
13 "@(#) Copyright (c) 1993\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)mkindex.c	8.1 (Berkeley) 06/11/93";
19 #endif /* not lint */
20 
21 #include <stdio.h>
22 
23 #include "bog.h"
24 
25 char *nextword __P((FILE *, char *, int *, int *));
26 
27 int
28 main(argc, argv)
29 	int argc;
30 	char *argv[];
31 {
32 	int clen, rlen, prev;
33 	long off, start;
34 	char buf[MAXWORDLEN + 1];
35 
36 	prev = '\0';
37 	off = start = 0L;
38 	while (nextword(stdin, buf, &clen, &rlen) != NULL) {
39 		if (*buf != prev) {
40 			if (prev != '\0')
41 				printf("%c %6ld %6ld\n", prev, start, off - 1);
42 			prev = *buf;
43 			start = off;
44 		}
45 		off += clen + 1;
46 	}
47 	printf("%c %6ld %6ld\n", prev, start, off - 1);
48 	exit(0);
49 }
50 
51 /*
52  * Return the next word in the compressed dictionary in 'buffer' or
53  * NULL on end-of-file
54  * Also set clen to the length of the compressed word (for mkindex) and
55  * rlen to the strlen() of the real word
56  */
57 char *
58 nextword(fp, buffer, clen, rlen)
59 	FILE *fp;
60 	char *buffer;
61 	int *clen, *rlen;
62 {
63 	register int ch, pcount;
64 	register char *p, *q;
65 	static char buf[MAXWORDLEN + 1];
66 	static int first = 1;
67 	static int lastch = 0;
68 
69    	if (first) {
70 		if ((pcount = getc(fp)) == EOF)
71 			return (NULL);
72 		first = 0;
73 	}
74 	else if ((pcount = lastch) == EOF)
75 		return (NULL);
76 
77 	p = buf + (*clen = pcount);
78 
79 	while ((ch = getc(fp)) != EOF && ch >= 'a')
80 			*p++ = ch;
81 		lastch = ch;
82 	*p = '\0';
83 
84 	*rlen = (int) (p - buf);
85 	*clen = *rlen - *clen;
86 
87 	p = buf;
88 	q = buffer;
89 	while ((*q++ = *p) != '\0') {
90 		if (*p++ == 'q')
91 			*q++ = 'u';
92 	}
93 	return (buffer);
94 }
95