Home | History | Annotate | Line # | Download | only in mkindex
mkindex.c revision 1.9
      1 /* $NetBSD: mkindex.c,v 1.9 2003/08/07 09:37:06 agc Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Barry Brachman.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #ifndef lint
     36 static char copyright[] = "@(#) Copyright (c) 1993\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 
     39 #if 0
     40 static char sccsid[] = "@(#)mkindex.c	8.1 (Berkeley) 6/11/93";
     41 #else
     42 static char rcsid[] =
     43     "$NetBSD: mkindex.c,v 1.9 2003/08/07 09:37:06 agc Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 
     50 #include "bog.h"
     51 
     52 int main(void);
     53 char *nextword(FILE *, char *, int *, int *);
     54 
     55 int
     56 main(void)
     57 {
     58 	int clen, rlen, prev, i;
     59 	long off, start;
     60 	char buf[MAXWORDLEN + 1];
     61 
     62 	prev = '\0';
     63 	off = start = 0L;
     64 	while (nextword(stdin, buf, &clen, &rlen) != NULL) {
     65 		if (*buf != prev) {
     66 			/*
     67 			 * Boggle expects a full index even if the dictionary
     68 			 * had no words beginning with some letters.
     69 			 * So we write out entries for every letter from prev
     70 			 * to *buf.
     71 			 */
     72 			if (prev != '\0')
     73 				printf("%c %6ld %6ld\n", prev, start, off - 1);
     74 			for (i = (prev ? prev + 1 : 'a'); i < *buf; i++)
     75 				printf("%c %6ld %6ld\n", i, off, off - 1);
     76 			prev = *buf;
     77 			start = off;
     78 		}
     79 		off += clen + 1;
     80 	}
     81 	printf("%c %6ld %6ld\n", prev, start, off - 1);
     82 	for (i = prev + 1; i <= 'z'; i++)
     83 		printf("%c %6ld %6ld\n", i, off, off - 1);
     84 	fflush(stdout);
     85 	if (ferror(stdout)) {
     86 		perror("error writing standard output");
     87 		exit(1);
     88 	}
     89 	exit(0);
     90 }
     91 
     92 /*
     93  * Return the next word in the compressed dictionary in 'buffer' or
     94  * NULL on end-of-file
     95  * Also set clen to the length of the compressed word (for mkindex) and
     96  * rlen to the strlen() of the real word
     97  */
     98 char *
     99 nextword(fp, buffer, clen, rlen)
    100 	FILE *fp;
    101 	char *buffer;
    102 	int *clen, *rlen;
    103 {
    104 	int ch, pcount;
    105 	char *p, *q;
    106 	static char buf[MAXWORDLEN + 1];
    107 	static int first = 1;
    108 	static int lastch = 0;
    109 
    110    	if (first) {
    111 		if ((pcount = getc(fp)) == EOF)
    112 			return (NULL);
    113 		first = 0;
    114 	}
    115 	else if ((pcount = lastch) == EOF)
    116 		return (NULL);
    117 
    118 	p = buf + (*clen = pcount);
    119 
    120 	while ((ch = getc(fp)) != EOF && ch >= 'a')
    121 			*p++ = ch;
    122 		lastch = ch;
    123 	*p = '\0';
    124 
    125 	*rlen = (int) (p - buf);
    126 	*clen = *rlen - *clen;
    127 
    128 	p = buf;
    129 	q = buffer;
    130 	while ((*q++ = *p) != '\0') {
    131 		if (*p++ == 'q')
    132 			*q++ = 'u';
    133 	}
    134 	return (buffer);
    135 }
    136