[9] | 1 | /* program DATABASE.C */
|
---|
| 2 |
|
---|
| 3 | #include <string.h>
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 | #include "advent.h"
|
---|
| 6 | #include "advdec.h"
|
---|
| 7 | #include "advtext.h"
|
---|
| 8 |
|
---|
| 9 | static char oline[256];
|
---|
| 10 |
|
---|
| 11 | _PROTOTYPE(void rdupto, (FILE *, int, int, char *));
|
---|
| 12 | _PROTOTYPE(void rdskip, (FILE *, int, int));
|
---|
| 13 |
|
---|
| 14 | /*
|
---|
| 15 | Function to scan a file up to a specified
|
---|
| 16 | point and either print or return a string.
|
---|
| 17 | */
|
---|
| 18 | void rdupto(fdi, uptoc, print, string)
|
---|
| 19 | FILE *fdi;
|
---|
| 20 | int uptoc, print;
|
---|
| 21 | char *string;
|
---|
| 22 | {
|
---|
| 23 | int c, i;
|
---|
| 24 | static _CONST unsigned char key[4] = {'c' | 0x80, 'L' | 0x80,
|
---|
| 25 | 'y' | 0x80, 'D' | 0x80};
|
---|
| 26 |
|
---|
| 27 | i = 1;
|
---|
| 28 | while ((c = getc(fdi)) != uptoc && c != EOF) {
|
---|
| 29 | if (c == '\n')
|
---|
| 30 | i = 1;
|
---|
| 31 | if (c >= 0x80)
|
---|
| 32 | c ^= key[i++ & 3];
|
---|
| 33 | if (c == '\r')
|
---|
| 34 | continue;
|
---|
| 35 | if (print)
|
---|
| 36 | putchar(c);
|
---|
| 37 | else
|
---|
| 38 | *string++ = (char) c;
|
---|
| 39 | }
|
---|
| 40 | if (!print)
|
---|
| 41 | *string = '\0';
|
---|
| 42 | return;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /*
|
---|
| 46 | Function to read a file skipping
|
---|
| 47 | a given character a specified number
|
---|
| 48 | of times, with or without repositioning
|
---|
| 49 | the file.
|
---|
| 50 | */
|
---|
| 51 | void rdskip(fdi, skipc, n)
|
---|
| 52 | FILE *fdi;
|
---|
| 53 | int skipc, n;
|
---|
| 54 | {
|
---|
| 55 | int c;
|
---|
| 56 |
|
---|
| 57 | while (n--)
|
---|
| 58 | while ((c = getc(fdi)) != skipc)
|
---|
| 59 | if (c == EOF)
|
---|
| 60 | bug(32);
|
---|
| 61 | return;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /*
|
---|
| 65 | Routine to request a yes or no answer to a question.
|
---|
| 66 | */
|
---|
| 67 | boolean yes(msg1, msg2, msg3)
|
---|
| 68 | int msg1, msg2, msg3;
|
---|
| 69 | {
|
---|
| 70 | char answer[INPUTBUFLEN];
|
---|
| 71 |
|
---|
| 72 | if (msg1)
|
---|
| 73 | rspeak(msg1);
|
---|
| 74 | do {
|
---|
| 75 | switch (*ask("\n> ", answer, sizeof(answer))) {
|
---|
| 76 | case 'n':
|
---|
| 77 | case 'N':
|
---|
| 78 | if (msg3)
|
---|
| 79 | rspeak(msg3);
|
---|
| 80 | return (FALSE);
|
---|
| 81 | case 'y':
|
---|
| 82 | case 'Y':
|
---|
| 83 | if (msg2)
|
---|
| 84 | rspeak(msg2);
|
---|
| 85 | return (TRUE);
|
---|
| 86 | default:
|
---|
| 87 | fputs("Please answer Y (yes) or N (no).", stdout);
|
---|
| 88 | }
|
---|
| 89 | } while (TRUE);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /*
|
---|
| 93 | Print a location description from "advent4.txt"
|
---|
| 94 | */
|
---|
| 95 | void rspeak(msg)
|
---|
| 96 | int msg;
|
---|
| 97 | {
|
---|
| 98 | if (msg == 54)
|
---|
| 99 | printf("ok.\n");
|
---|
| 100 | else {
|
---|
| 101 | fseek(fd4, idx4[msg - 1], 0);
|
---|
| 102 | rdupto(fd4, '#', 1, 0);
|
---|
| 103 | }
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /*
|
---|
| 108 | Print an item message for a given state from "advent3.txt"
|
---|
| 109 | */
|
---|
| 110 | void pspeak(item, state)
|
---|
| 111 | int item, state;
|
---|
| 112 | {
|
---|
| 113 | fseek(fd3, idx3[item - 1], 0);
|
---|
| 114 | rdskip(fd3, '/', state + 2);
|
---|
| 115 | rdupto(fd3, '/', FALSE, oline);
|
---|
| 116 | if (strncmp(oline, "<$$<", 4) != 0)
|
---|
| 117 | printf("%s", oline);
|
---|
| 118 | return;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /*
|
---|
| 122 | Print a long location description from "advent1.txt"
|
---|
| 123 | */
|
---|
| 124 | void desclg(loc)
|
---|
| 125 | int loc;
|
---|
| 126 | {
|
---|
| 127 | fseek(fd1, idx1[loc - 1], 0);
|
---|
| 128 | rdupto(fd1, '#', 1, 0);
|
---|
| 129 | return;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /*
|
---|
| 133 | Print a short location description from "advent2.txt"
|
---|
| 134 | */
|
---|
| 135 | void descsh(loc)
|
---|
| 136 | int loc;
|
---|
| 137 | {
|
---|
| 138 | fseek(fd2, idx2[loc - 1], 0);
|
---|
| 139 | rdupto(fd2, '#', 1, 0);
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|