xref: /original-bsd/games/boggle/mkdict/mkdict.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[] = "@(#)mkdict.c	8.1 (Berkeley) 06/11/93";
19 #endif /* not lint */
20 
21 /*
22  * Filter out words that:
23  *	1) Are not completely made up of lower case letters
24  *	2) Contain a 'q' not immediately followed by a 'u'
25  *	3) Are less that 3 characters long
26  *	4) Are greater than MAXWORDLEN characters long
27  */
28 
29 #include <ctype.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "bog.h"
35 
36 int
37 main(argc, argv)
38 	int argc;
39 	char *argv[];
40 {
41 	register char *p, *q;
42 	register int ch, common, n, nwords;
43 	int current, len, prev, qcount;
44 	char buf[2][MAXWORDLEN + 1];
45 
46 	prev = 0;
47 	current = 1;
48 	buf[prev][0] = '\0';
49 	if (argc == 2)
50 		n = atoi(argv[1]);
51 
52 	for (nwords = 1;
53 	    fgets(buf[current], MAXWORDLEN + 1, stdin) != NULL; ++nwords) {
54 		if ((p = index(buf[current], '\n')) == NULL) {
55 			fprintf(stderr,
56 			    "mkdict: word too long: %s\n", buf[current]);
57 			while ((ch = getc(stdin)) != EOF && ch != '\n')
58 				;
59 			if (ch == EOF)
60 				break;
61 			continue;
62 		}
63 		len = 0;
64 		for (p = buf[current]; *p != '\n'; p++) {
65 			if (!islower(*p))
66 				break;
67 			if (*p == 'q') {
68 				q = p + 1;
69 				if (*q != 'u')
70 					break;
71 				else {
72 					while (*q = *(q + 1))
73 						q++;
74 				}
75 				len++;
76 			}
77 			len++;
78 		}
79 		if (*p != '\n' || len < 3 || len > MAXWORDLEN)
80 			continue;
81 		if (argc == 2 && nwords % n)
82 			continue;
83 
84 		*p = '\0';
85 		p = buf[current];
86 		q = buf[prev];
87 		qcount = 0;
88 		while ((ch = *p++) == *q++ && ch != '\0')
89 			if (ch == 'q')
90 				qcount++;
91 		common = p - buf[current] - 1;
92 		printf("%c%s", common + qcount, p - 1);
93 		prev = !prev;
94 		current = !current;
95 	}
96 	fprintf(stderr, "%d words\n", nwords);
97 	exit(0);
98 }
99