mkindex.c 3.9 KB

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