xref: /openbsd/games/boggle/mkindex/mkindex.c (revision cca36db2)
1 /*	$OpenBSD: mkindex.c,v 1.8 2009/10/27 23:59:24 deraadt Exp $	*/
2 /*	$NetBSD: mkindex.c,v 1.2 1995/03/21 12:14:52 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Barry Brachman.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #include "bog.h"
40 
41 char *nextword(FILE *, char *, int *, int *);
42 
43 int
44 main(int argc, char *argv[])
45 {
46 	int clen, rlen, prev;
47 	long off, start;
48 	char buf[MAXWORDLEN + 1];
49 
50 	prev = '\0';
51 	off = start = 0L;
52 	while (nextword(stdin, buf, &clen, &rlen) != NULL) {
53 		if (*buf != prev) {
54 			if (prev != '\0')
55 				printf("%c %6ld %6ld\n", prev, start, off - 1);
56 			prev = *buf;
57 			start = off;
58 		}
59 		off += clen + 1;
60 	}
61 	printf("%c %6ld %6ld\n", prev, start, off - 1);
62 	exit(0);
63 }
64 
65 /*
66  * Return the next word in the compressed dictionary in 'buffer' or
67  * NULL on end-of-file
68  * Also set clen to the length of the compressed word (for mkindex) and
69  * rlen to the strlen() of the real word
70  */
71 char *
72 nextword(FILE *fp, char *buffer, int *clen, int *rlen)
73 {
74 	int ch, pcount;
75 	char *p, *q;
76 	static char buf[MAXWORDLEN + 1];
77 	static int first = 1;
78 	static int lastch = 0;
79 
80    	if (first) {
81 		if ((pcount = getc(fp)) == EOF)
82 			return (NULL);
83 		first = 0;
84 	}
85 	else if ((pcount = lastch) == EOF)
86 		return (NULL);
87 
88 	p = buf + (*clen = pcount);
89 
90 	while ((ch = getc(fp)) != EOF && ch >= 'a')
91 			*p++ = ch;
92 		lastch = ch;
93 	*p = '\0';
94 
95 	*rlen = (int) (p - buf);
96 	*clen = *rlen - *clen;
97 
98 	p = buf;
99 	q = buffer;
100 	while ((*q++ = *p) != '\0') {
101 		if (*p++ == 'q')
102 			*q++ = 'u';
103 	}
104 	return (buffer);
105 }
106