source: trunk/minix/commands/advent/database.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.4 KB
Line 
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
9static 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*/
18void rdupto(fdi, uptoc, print, string)
19FILE *fdi;
20int uptoc, print;
21char *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*/
51void rdskip(fdi, skipc, n)
52FILE *fdi;
53int 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*/
67boolean yes(msg1, msg2, msg3)
68int 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*/
95void rspeak(msg)
96int 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*/
110void pspeak(item, state)
111int 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*/
124void desclg(loc)
125int 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*/
135void descsh(loc)
136int loc;
137{
138 fseek(fd2, idx2[loc - 1], 0);
139 rdupto(fd2, '#', 1, 0);
140 return;
141}
Note: See TracBrowser for help on using the repository browser.