xref: /dragonfly/games/boggle/mkindex/mkindex.c (revision abf903a5)
1 /*	$OpenBSD: mkindex.c,v 1.9 2016/01/07 16:00:31 tb 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 <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include "bog.h"
42 
43 char *nextword(FILE *, char *, int *, int *);
44 
45 int
46 main(void)
47 {
48 	int clen, rlen, prev;
49 	long off, start;
50 	char buf[MAXWORDLEN + 1];
51 
52 	prev = '\0';
53 	off = start = 0L;
54 	while (nextword(stdin, buf, &clen, &rlen) != NULL) {
55 		if (*buf != prev) {
56 			if (prev != '\0')
57 				printf("%c %6ld %6ld\n", prev, start, off - 1);
58 			prev = *buf;
59 			start = off;
60 		}
61 		off += clen + 1;
62 	}
63 	printf("%c %6ld %6ld\n", prev, start, off - 1);
64 	return 0;
65 }
66 
67 /*
68  * Return the next word in the compressed dictionary in 'buffer' or
69  * NULL on end-of-file
70  * Also set clen to the length of the compressed word (for mkindex) and
71  * rlen to the strlen() of the real word
72  */
73 char *
74 nextword(FILE *fp, char *buffer, int *clen, int *rlen)
75 {
76 	int ch, pcount;
77 	char *p, *q;
78 	static char buf[MAXWORDLEN + 1];
79 	static int first = 1;
80 	static int lastch = 0;
81 
82 	if (first) {
83 		if ((pcount = getc(fp)) == EOF)
84 			return (NULL);
85 		first = 0;
86 	}
87 	else if ((pcount = lastch) == EOF)
88 		return (NULL);
89 
90 	p = buf + (*clen = pcount);
91 
92 	while ((ch = getc(fp)) != EOF && ch >= 'a')
93 			*p++ = ch;
94 		lastch = ch;
95 	*p = '\0';
96 
97 	*rlen = (int) (p - buf);
98 	*clen = *rlen - *clen;
99 
100 	p = buf;
101 	q = buffer;
102 	while ((*q++ = *p) != '\0') {
103 		if (*p++ == 'q')
104 			*q++ = 'u';
105 	}
106 	return (buffer);
107 }
108