-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2.c
473 lines (425 loc) · 12.9 KB
/
d2.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*======================================================================
* Implementation of d2 dictionary
*======================================================================*/
#define _GNU_SOURCE /* For asprintf */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include "mem.h"
#include "d2.h"
#include "d2file.h" /* definition of file layout */
#define DEFAULT_DIR "/usr/share/dict/d2"
/*--- Definitions of structures half-declared in d2.h */
struct _Dictl {
int len;
Dict dict; /* Optional parent */
int nhead; /* How many 2-char heads */
D2fsect *hindex; /* Array of head data */
char *tails; /* char[][len-2] */
};
struct _Dict {
char *dir;
Dictl dictl[MAXLEN+1]; /* Children managed */
int used[MAXLEN+1];
};
struct _Dscan {
Dictl dictl;
int len2;
char *word;
int hcount;
int tindex;
int tend;
D2fsect *hptr;
};
struct _Dpool {
unsigned char nlet[26];
unsigned char nwild;
};
/*----------------------------------------------------------------------
* report an error and die
*----------------------------------------------------------------------*/
#define faile(str) (perror(str), exit(1))
static void fail(const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}
/*======================================================================
* class Dictl
* Controls part of a Dict dealing with words of a specific length.
* No per-object data, so just use the reference counts in Dict.
* Reads the data out of the file, which has the format:
* char[2] = "d2" magic number
* short len length of words
* int nhead number of head pairs
* ( char head[2] first two letters of word(s)
* nhead * ( 2 bytes padding
* ( int tend cumulative number of tails
* char tails[ntail][len-2] tails of words
*======================================================================*/
/*----------------------------------------------------------------------
* Destroy a Dictl. Called from Dict_close.
*----------------------------------------------------------------------*/
static void Dictl_delete(Dictl dl)
{
Mem_free(dl->hindex);
Mem_free(dl->tails);
Mem_free(dl);
}
/*----------------------------------------------------------------------
* Access a length dictionary from a file
*----------------------------------------------------------------------*/
Dictl Dictl_fopen(const char *filename)
{
Dictl dl;
FILE *f;
int ntail;
D2fhdr hdr;
dl = NEW(struct _Dictl);
dl->dict = NULL; /* No parent (yet) */
f = fopen(filename, "rb"); /* open it for reading */
if (f==NULL) fail("%s: Unable to open file\n", filename);
if (fread(&hdr, sizeof(D2fhdr), 1, f) < 1)
faile(filename);
if (strncmp(hdr.id, "d2", 2) != 0)
fail("%s: bad magic number", filename);
if ((unsigned)hdr.nhead > 26*26)
fail("%s: bad head count %d", filename, hdr.nhead);
dl->len = hdr.len;
dl->nhead = hdr.nhead;
dl->hindex = NEWARRAY(D2fsect, dl->nhead);
if (fread(dl->hindex, sizeof(D2fsect), dl->nhead, f) < dl->nhead)
faile(filename);
ntail = (dl->nhead == 0) ? 0 : dl->hindex[dl->nhead - 1].tend;
dl->tails = NEWARRAY(char, ntail*(hdr.len-2));
if (fread(dl->tails, sizeof(char), ntail*(hdr.len-2), f) < ntail*(hdr.len-2))
faile(filename);
fclose(f);
return dl;
}
/*----------------------------------------------------------------------
* Access a length part of a Dict
*----------------------------------------------------------------------*/
Dictl Dictl_open(Dict d, int len)
{
Dictl dl;
if (len < 1 || len > MAXLEN)
fail("Dictl_open: length %d is out of range [1:%d]", len, MAXLEN);
if ((dl = d->dictl[len]) == NULL) { /* not already cached */
char *fname = Mem_new(strlen(d->dir)+1+2+1);
sprintf(fname, "%s/%02d", d->dir, len); /* make filename from stub */
dl = d->dictl[len] = Dictl_fopen(fname);
if (dl->len != len) fail("%s: bad word length %d", fname, dl->len);
dl->dict = d;
Mem_free(fname);
}
++ d->used[len];
return dl;
}
/*----------------------------------------------------------------------
* Relinquish use of a Dictl, but hang on to it internally in case
* it's needed again (saves re-reading the file).
*----------------------------------------------------------------------*/
void Dictl_close(Dictl dl)
{
if (dl->dict != NULL)
-- dl->dict->used[dl->len]; /* decrement usage count */
}
/*----------------------------------------------------------------------
* Return the number of items in a Dictl
*----------------------------------------------------------------------*/
int Dictl_length(Dictl dl)
{
return dl->hindex[dl->nhead - 1].tend;
}
/*----------------------------------------------------------------------
* Check if a word is in the given dictionary part
*----------------------------------------------------------------------*/
/*#define c2cmp(s,t) strncmp(s,t,2)*/
#ifdef _GNUC_
#define c2cmp(s,t) (((int)(s)[0]-(t)[0]) ?: ((int)(s)[1]-(int)(t)[1]))
#else
#define c2cmp(s,t) (((int)(s)[0]-(t)[0]) ? ((int)(s)[0]-(int)(t)[0]) : ((int)(s)[1]-(int)(t)[1]))
#endif
int Dictl_contains(Dictl dl, const char *word)
{
int i,j,k, cmp, len2;
len2 = dl->len-2;
if (len2 < 0) { /* Length 1 - special case */
for (i=0; i<dl->nhead; i++) {
if (dl->hindex[i].head[0] == *word) return 1;
}
return 0;
}
for (i=0, j=dl->nhead; i < j; ) {
k = (i+j)/2; /* binary chop */
cmp = c2cmp(word, dl->hindex[k].head);
if (cmp < 0) j = k; /* try in lower half */
else if (cmp > 0) i = k+1; /* try in upper half */
else break; /* exact match */
}
if (i == j) return 0; /* head not found */
if (len2 == 0) return 1; /* len=2: no tail */
for (i=(k==0)?0: dl->hindex[k-1].tend, j=dl->hindex[k].tend; i<j; ) {
k = (i+j)/2; /* binary chop */
cmp = strncmp(word+2, dl->tails+k*len2, len2);
if (cmp < 0) j = k; /* try in lower half */
else if (cmp > 0) i = k+1; /* try in upper half */
else break; /* exact match */
}
return i < j; /* true if tail found */
}
/*======================================================================
* class Dict
* Controls a set of dictionary files (one for each word length).
* The Dictl objects are cached so they can be re-used when needed.
* If memory is restricted, it may be better (a) to delete a Dictl
* when its `used' count has fallen to zero; or (b) delete unused
* Dictls selectively if we run out of space making a new one.
*======================================================================*/
/*--------------------------------------------------------------*
* If we've run out of free memory, de-allocate any Dictl
* entries that are unused at present
*--------------------------------------------------------------*/
static int Dict_freer(void *vdict, size_t size)
{
Dict d = vdict;
int i;
for (i=0; i<=MAXLEN; i++) {
if (d->dictl[i] != NULL && d->used[i] == 0) {
/* fprintf(stderr, "Dict_freer(%d): freeing dictl[%d]\n", size, i);*/
Dictl_delete(d->dictl[i]);
d->dictl[i] = NULL;
return 1;
}
}
return 0;
}
static char *
_dict_default_dir(void)
{
char *dir;
struct stat st;
const char *home = getenv("HOME");
if (home != NULL) {
asprintf(&dir, "%s/.local/share/d2dict", home);
if (stat(dir, &st) == 0 &&
(st.st_mode & S_IFMT) == S_IFDIR) {
return dir;
}
}
if (stat(DEFAULT_DIR, &st) == 0 &&
(st.st_mode & S_IFMT) == S_IFDIR) {
return strdup(DEFAULT_DIR);
}
fail("No directory $HOME/.local/share/d2dict or %s", DEFAULT_DIR);
return 0;
}
/*----------------------------------------------------------------------
* Create a dictionary from a given file stub (NULL => default)
*----------------------------------------------------------------------*/
Dict Dict_open(const char *dir)
{
int i;
Dict d = NEW(struct _Dict);
if (dir == NULL) {
dir = getenv("D2DICT");
}
if (dir == NULL) {
d->dir = _dict_default_dir();
} else {
d->dir = Mem_strdup(dir);
}
for (i=0; i<=MAXLEN; i++) {
d->dictl[i] = NULL; /* no Dictl objects yet */
d->used[i] = 0; /* so not used */
}
Mem_add_freer(Dict_freer, d);
return d;
}
/*----------------------------------------------------------------------
* Finish using a Dict
*----------------------------------------------------------------------*/
void Dict_close(Dict d)
{
int i;
Mem_remove_freer(Dict_freer, d);
for (i=0; i<=MAXLEN; i++) { /* remove all Dictls */
if (d->dictl[i] != NULL) Dictl_delete(d->dictl[i]);
}
Mem_free(d->dir);
Mem_free(d);
}
/*----------------------------------------------------------------------
* Test if a given word is in the dictionary
*----------------------------------------------------------------------*/
int Dict_isword(Dict d, const char *word, int len)
{
int result;
Dictl dl = Dictl_open(d, len); /* open section for this length */
result = Dictl_isword(dl, word); /* check the word */
Dictl_close(dl);
return result;
}
/*======================================================================
* class Dscan
* Allows a serial scan through a Dictl.
*======================================================================*/
Dscan Dscan_open(Dict d, int len)
{
Dscan ds = NEW(struct _Dscan);
ds->dictl = Dictl_open(d, len);
ds->len2 = len - 2; /* length of tails */
ds->word = NEWARRAY(char,len + 1); /* space for words returned */
ds->word[len] = '\0'; /* drop in a terminator */
ds->hcount = ds->dictl->nhead; /* number of heads untried */
ds->hptr = ds->dictl->hindex; /* point to start of `file' */
ds->tindex = 0;
ds->tend = 0; /* need to access next `block' */
return ds;
}
/*----------------------------------------------------------------------
* Return the next word in the scan.
*----------------------------------------------------------------------*/
const char *Dscan_read(Dscan ds)
{
if (ds->tindex == ds->tend) { /* at end of current head */
if (ds->hcount == 0) return NULL; /* no more to read */
ds->word[0] = ds->hptr->head[0]; /* fetch next head */
ds->word[1] = ds->hptr->head[1];
ds->tend = ds->hptr->tend; /* mark end of next block */
ds->hptr++;
ds->hcount--;
}
if (ds->len2 > 0)
strncpy(ds->word+2, ds->dictl->tails+(ds->tindex * ds->len2),
ds->len2); /* copy the tail data */
ds->tindex ++;
return ds->word;
}
/*----------------------------------------------------------------------
* Hint to Dscan_read that we are not interested in words which
* begin with the same <skip> characters as the last word returned.
*----------------------------------------------------------------------*/
void Dscan_skip(Dscan ds, int skip)
{
if (skip < 2) ds->tindex = ds->tend; /* ignore rest of current `block' */
}
/*----------------------------------------------------------------------
* Terminate a scan
*----------------------------------------------------------------------*/
void Dscan_close(Dscan ds)
{
Dictl_close(ds->dictl);
Mem_free(ds->word);
Mem_free(ds);
}
/*=======================================================================
* Pool of letters
*=======================================================================*/
void
(dpool_clear)(Dpool *pPool)
{
memset(pPool,0,sizeof(Dpool));
}
void
dpool_add(Dpool *pPool, const Dpool *pOther)
{
int i;
for (i=0; i<26; i++) {
pPool->nlet[i] += pOther->nlet[i];
}
pPool->nwild += pOther->nwild;
}
void
(dpool_add_char)(Dpool *pPool, int c)
{
if (isalpha(c)) {
pPool->nlet[toupper(c)-'A']++;
} else {
pPool->nwild++;
}
}
void
dpool_add_word(Dpool *pPool, const char *sWord)
{
int c;
while ((c=*sWord++) != '\0') {
if (isalpha(c)) {
pPool->nlet[toupper(c)-'A']++;
} else {
pPool->nwild++;
}
}
}
int
dpool_sub(Dpool *pPool, const Dpool *pOther)
{
int i;
for (i=0; i<26; i++) {
if (pPool->nlet[i] < pOther->nlet[i]) {
if (pPool->nlet[i] + pPool->nwild < pOther->nlet[i]) return -1;
pPool->nwild -= pOther->nlet[i] - pPool->nlet[i];
pPool->nlet[i] = 0;
} else {
pPool->nlet[i] -= pOther->nlet[i];
}
}
if (pPool->nwild < pOther->nwild) return -1;
pPool->nwild -= pOther->nwild;
return 0;
}
int
dpool_sub_char(Dpool *pPool, int c)
{
if (isalpha(c)) {
int i = toupper(c) - 'A';
if (pPool->nlet[i] > 0) {
--pPool->nlet[i];
return 0;
}
}
if (pPool->nwild > 0) {
--pPool->nwild;
return 0;
} else {
return -1;
}
}
int
dpool_sub_word(Dpool *pPool, const char *sWord)
{
int c, i;
while ((c=*sWord++) != '\0') {
if (isalpha(c) &&
(i = toupper(c) - 'A', pPool->nlet[i] > 0)) {
--pPool->nlet[i];
} else if (pPool->nwild > 0) {
--pPool->nwild;
} else {
return -1;
}
}
return 0;
}
void
dpool_string(char *sBuffer, const Dpool *pPool, int cWild)
{
int i, j;
for (i=0; i<26; i++) {
for (j=0; j<pPool->nlet[i]; j++) {
*sBuffer++ = 'A'+i;
}
}
for (j=0; j<pPool->nwild; j++) {
*sBuffer++ = cWild;
}
*sBuffer = '\0';
}