-
Notifications
You must be signed in to change notification settings - Fork 0
/
words.c
executable file
·346 lines (288 loc) · 6.69 KB
/
words.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Sarien - A Sierra AGI resource interpreter engine
* Copyright (C) 1999-2002 Stuart George and Claudio Matsuoka
*
* $Id: words.c,v 1.35 2003/08/26 00:53:16 cmatsuoka Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; see docs/COPYING for further details.
*/
/*
* New find_word algorithm by Thomas Akesson <tapilot@home.se>
*/
#include <stdio.h>
#include <string.h>
#include "sarien.h"
#include "agi.h"
#include "keyboard.h" /* for clean_input() */
static UINT8 *words; /* words in the game */
static UINT32 words_flen; /* length of word memory */
/*
* Local implementation to avoid problems with strndup() used by
* gcc 3.2 Cygwin (see #635984)
*/
static char *my_strndup (char* src, int n)
{
char *tmp = strncpy (malloc(n + 1), src, n);
tmp[n] = 0;
return tmp;
}
int load_words (char *fname)
{
#ifndef PALMOS
FILE *fp = NULL;
UINT32 flen;
UINT8 *mem = NULL;
char *path = NULL;
words = NULL;
path = fixpath (NO_GAMEDIR, fname);
if ((fp = fopen(path, "rb")) == NULL) {
report ("Warning: can't open %s\n", path);
return err_OK /*err_BadFileOpen*/;
}
report ("Loading dictionary: %s\n", path);
fseek (fp, 0, SEEK_END);
flen = ftell (fp);
fseek (fp, 0, SEEK_SET);
words_flen = flen;
if ((mem = (UINT8*)calloc(1, flen + 32)) == NULL) {
fclose (fp);
return err_NotEnoughMemory;
}
fread (mem, 1, flen, fp);
fclose (fp);
words = mem;
return err_OK;
#endif
}
void unload_words ()
{
if (words != NULL) {
free (words);
words = NULL;
}
}
/**
* Find a word in the dictionary
* Uses an algorithm hopefully like the one Sierra used. Returns the ID
* of the word and the length in flen. Returns -1 if not found.
*
* Thomas Åkesson, November 2001
*/
int find_word (char *word, int *flen)
{
int mchr = 0; /* matched chars */
int len, fchr, id = -1;
UINT8* p = words;
UINT8* q = words + words_flen;
*flen = 0;
_D ("%s", word);
if (word[0] >= 'a' && word[0] <= 'z')
fchr = word[0] - 'a';
else
return -1;
len = strlen (word);
/* Get the offset to the first word beginning with the
* right character
*/
p += hilo_getword (p + 2 * fchr);
while (p[0] >= mchr) {
if (p[0] == mchr) {
p++;
/* Loop through all matching characters */
while ((p[0] ^ word[mchr]) == 0x7F && mchr < len) {
mchr++;
p++;
}
/* Check if this is the last character of the word
* and if it matches
*/
if ((p[0] ^ word[mchr]) == 0xFF && mchr < len) {
mchr++;
if (word[mchr] == 0 || word[mchr] == 0x20) {
id = hilo_getword (p+1);
*flen = mchr;
}
}
}
if (p >= q)
return -1;
/* Step to the next word */
while (p[0] < 0x80)
p++;
p += 3;
}
return id;
}
void dictionary_words (char *msg)
{
char *p = NULL;
char *q = NULL;
int wid, wlen;
_D ("msg = \"%s\"", msg);
clean_input ();
for (p = msg; p && *p && getvar (V_word_not_found) == 0; ) {
if (*p == 0x20)
p++;
if (*p == 0)
break;
wid = find_word(p, &wlen);
_D ("find_word(p) == %d", wid);
switch (wid) {
case -1:
_D (_D_WARN "unknown word");
game.ego_words[game.num_ego_words].word = strdup(p);
q = game.ego_words[game.num_ego_words].word;
game.ego_words[game.num_ego_words].id = 19999;
setvar(V_word_not_found, 1 + game.num_ego_words);
game.num_ego_words++;
p += strlen (p);
break;
case 0:
/* ignore this word */
_D (_D_WARN "ignore word");
p += wlen;
q = NULL;
break;
default:
/* an OK word */
/* _D (_D_WARN "ok word (%d)", wc1); */
game.ego_words[game.num_ego_words].id = wid;
game.ego_words[game.num_ego_words].word = my_strndup(p, wlen);
game.num_ego_words++;
p += wlen;
break;
}
if (p != NULL && *p) {
_D ("p = %s", p);
*p = 0;
p++;
}
if (q != NULL) {
for (; (*q!=0 && *q!=0x20); q++);
if (*q) {
*q=0;
q++;
}
}
}
_D (_D_WARN "num_ego_words = %d", game.num_ego_words);
if (game.num_ego_words > 0) {
setflag (F_entered_cli, TRUE);
setflag (F_said_accepted_input, FALSE);
}
}
#ifdef OPT_LIST_DICT
int show_words ()
{
/*
decode *words into words!
*/
int sc, wc, woff, wid;
unsigned char x[128];
unsigned char c;
int num_words;
num_words=0;
/* scan for first entry with words */
for (wc = woff = 0; woff == 0 && woff < words_flen; wc += 2)
woff = hilo_getword(words+wc);
/* AGDS cludge for bad word file :( */
if(woff > words_flen)
return err_OK;
/* count all the words in the list */
for(sc=0, wc=0; woff<words_flen; ) {
c = hilo_getbyte (words+woff);
woff++;
if(c > 0x80) {
wc++;
wid = hilo_getword (words+woff);
woff += 2;
if(wid > sc && wid != 9999)
sc = wid;
}
}
num_words = wc;
/* scan for frist words entry */
for(wc=0, woff=0; woff==0; wc+=2)
woff=hilo_getword(words+wc);
/* build word list */
for(wc=0; wc<num_words; wc++)
{
c=hilo_getbyte(words+woff); woff++;
wid=c;
while(c<0x80)
{
c=hilo_getbyte(words+woff); woff++;
x[wid]=((c^0x7F)&0x7F); wid++;
}
x[wid]=0x0;
printf("id=%-4i %s\n", hilo_getword(words+woff), x);
woff+=2;
}
return err_OK;
}
#endif
#if defined(CIBYL)
#include <s9.h>
extern s9_t sarien_s9;
void add_s9_words ()
{
/*
from show_words
*/
int sc, wc, woff, wid;
unsigned char x[128];
unsigned char c;
int num_words;
static char **allocated_words;
static int num_allocated_words;
num_words=0;
/* scan for first entry with words */
for (wc = woff = 0; woff == 0 && woff < words_flen; wc += 2)
woff = hilo_getword(words+wc);
/* AGDS cludge for bad word file :( */
if(woff > words_flen)
return;
/* count all the words in the list */
for(sc=0, wc=0; woff<words_flen; ) {
c = hilo_getbyte (words+woff);
woff++;
if(c > 0x80) {
wc++;
wid = hilo_getword (words+woff);
woff += 2;
if(wid > sc && wid != 9999)
sc = wid;
}
}
num_words = wc;
if (allocated_words != NULL)
{
int i;
for (i = 0; i < num_allocated_words; i++)
free( allocated_words[i] );
free(allocated_words);
}
allocated_words = malloc(sizeof(char*) * num_words);
num_allocated_words = num_words;
/* scan for frist words entry */
for(wc=0, woff=0; woff==0; wc+=2)
woff=hilo_getword(words+wc);
/* build word list */
for(wc=0; wc<num_words; wc++)
{
c=hilo_getbyte(words+woff); woff++;
wid=c;
while(c<0x80)
{
c=hilo_getbyte(words+woff); woff++;
x[wid]=((c^0x7F)&0x7F); wid++;
}
x[wid]=0x0;
/* FIXME! This can turn into a memory leak */
allocated_words[wc] = strdup((const char*)x);
s9_add_word(&sarien_s9, allocated_words[wc]);
woff+=2;
}
}
#endif /* CIBYL */