Home | History | Annotate | Line # | Download | only in mkindex
mkindex.c revision 1.10
      1 /* $NetBSD: mkindex.c,v 1.10 2005/07/01 16:38:24 jmc 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.10 2005/07/01 16:38:24 jmc Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 
     50 #include "bog.h"
     51 
     52 char *nextword(FILE *, char *, int *, int *);
     53 
     54 int
     55 main(void)
     56 {
     57 	int clen, rlen, prev, i;
     58 	long off, start;
     59 	char buf[MAXWORDLEN + 1];
     60 
     61 	prev = '\0';
     62 	off = start = 0L;
     63 	while (nextword(stdin, buf, &clen, &rlen) != NULL) {
     64 		if (*buf != prev) {
     65 			/*
     66 			 * Boggle expects a full index even if the dictionary
     67 			 * had no words beginning with some letters.
     68 			 * So we write out entries for every letter from prev
     69 			 * to *buf.
     70 			 */
     71 			if (prev != '\0')
     72 				printf("%c %6ld %6ld\n", prev, start, off - 1);
     73 			for (i = (prev ? prev + 1 : 'a'); i < *buf; i++)
     74 				printf("%c %6ld %6ld\n", i, off, off - 1);
     75 			prev = *buf;
     76 			start = off;
     77 		}
     78 		off += clen + 1;
     79 	}
     80 	printf("%c %6ld %6ld\n", prev, start, off - 1);
     81 	for (i = prev + 1; i <= 'z'; i++)
     82 		printf("%c %6ld %6ld\n", i, off, off - 1);
     83 	fflush(stdout);
     84 	if (ferror(stdout)) {
     85 		perror("error writing standard output");
     86 		exit(1);
     87 	}
     88 	exit(0);
     89 }
     90 
     91 /*
     92  * Return the next word in the compressed dictionary in 'buffer' or
     93  * NULL on end-of-file
     94  * Also set clen to the length of the compressed word (for mkindex) and
     95  * rlen to the strlen() of the real word
     96  */
     97 char *
     98 nextword(FILE *fp, char *buffer, int *clen, int *rlen)
     99 {
    100 	int ch, pcount;
    101 	char *p, *q;
    102 	static char buf[MAXWORDLEN + 1];
    103 	static int first = 1;
    104 	static int lastch = 0;
    105 
    106    	if (first) {
    107 		if ((pcount = getc(fp)) == EOF)
    108 			return (NULL);
    109 		first = 0;
    110 	}
    111 	else if ((pcount = lastch) == EOF)
    112 		return (NULL);
    113 
    114 	p = buf + (*clen = pcount);
    115 
    116 	while ((ch = getc(fp)) != EOF && ch >= 'a')
    117 			*p++ = ch;
    118 		lastch = ch;
    119 	*p = '\0';
    120 
    121 	*rlen = (int) (p - buf);
    122 	*clen = *rlen - *clen;
    123 
    124 	p = buf;
    125 	q = buffer;
    126 	while ((*q++ = *p) != '\0') {
    127 		if (*p++ == 'q')
    128 			*q++ = 'u';
    129 	}
    130 	return (buffer);
    131 }
    132