forked from rc0/jbofihe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictaccs.c
216 lines (166 loc) · 3.71 KB
/
dictaccs.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
/***************************************
$Header$
Low level routines for accessing the dictionary.
***************************************/
/* COPYRIGHT */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "functions.h"
#include "canonluj.h"
#include "morf.h"
#ifndef DEFAULT_DICTIONARY
#define DEFAULT_DICTIONARY "smujmaji.dat"
#endif
#include <unistd.h>
#include <sys/stat.h>
#ifdef HAVE_MMAP
#include <sys/types.h>
#include <sys/mman.h>
#endif
typedef struct {
char *key;
char *val;
} Keyval;
#ifdef EMBEDDED_DICTIONARY
#include "dictdata.c"
#else
static Keyval *dict = NULL;
static int n_entries = 0;
#endif
#ifndef EMBEDDED_DICTIONARY
static int inited = 0;
/*++++++++++++++++++++++++++++++++++++++
Read a 'long' integer from file avoiding endianness problems.
static unsigned long get_long
FILE *in
++++++++++++++++++++++++++++++++++++++*/
static unsigned long
get_long(FILE *in)
{
unsigned long a, b, c, d;
/* Avoid endian-ness problem if we were to use fwrite */
a = getc(in);
b = getc(in);
c = getc(in);
d = getc(in);
return (a << 24) | (b << 16) | (c << 8) | (d << 0);
}
/*++++++++++++++++++++++++++++++++++++++
Read the database to build the transaction list.
FILE *in
++++++++++++++++++++++++++++++++++++++*/
static void
read_database(FILE *in)
{
typedef struct {
int klen;
int vlen;
} Entry;
Entry *entries;
int i, len;
struct stat sb;
off_t offset;
int result;
char *dict_base = NULL;
n_entries = get_long(in);
entries = new_array(Entry, n_entries);
dict = new_array(Keyval, n_entries);
for (i=0; i<n_entries; i++) {
len = getc(in);
entries[i].klen = len;
len = getc(in);
entries[i].vlen = len;
}
if (fstat(fileno(in), &sb) < 0) {
fprintf(stderr, "Could not stat the dictionary file\n");
exit(1);
}
offset = ftell(in);
#ifdef HAVE_MMAP
{
char *mmap_base = NULL;
mmap_base = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fileno(in), 0);
result = (int) mmap_base;
if (result < 0) {
perror("Could not mmap the dictionary data\n");
exit(1);
}
dict_base = mmap_base + offset;
}
#else
{
size_t dict_size = sb.st_size - offset;
dict_base = new_array(char, dict_size);
result = fread(dict_base, sizeof(char), dict_size, in);
if (result != dict_size) {
perror("Could not read dictionary contents\n");
exit(1);
}
}
#endif
/* Loop through to build pointer arrays */
for (i=0; i<n_entries; i++) {
if (i == 0) {
dict[i].key = dict_base;
} else {
dict[i].key = dict[i-1].val + entries[i-1].vlen + 1; /* Allow for null termination */
}
dict[i].val = dict[i].key + entries[i].klen + 1; /* Allow for null termination */
}
Free(entries);
}
/* ================================================== */
static void
init(void)
{
char *dname;
FILE *in;
if (!inited) {
inited = 1;
dname = getenv("JBOFIHE_DICTIONARY");
if (!dname) {
dname = DEFAULT_DICTIONARY;
}
in = fopen(dname, "rb");
if (!in) {
inited = -1;
} else {
read_database(in);
fclose(in);
}
}
}
#endif
/* ================================================== */
char *
dict_lookup(char *key)
{
int l, m, h;
int r;
#ifndef EMBEDDED_DICTIONARY
if (!inited) {
init();
}
if (inited < 0) {
/* earlier problem opening dictionary file etc */
return NULL;
}
#endif
/* Bisection search follows. */
l = 0;
h = n_entries;
while (h > l) {
m = (h + l) >> 1;
r = strcmp(key, dict[m].key);
if (r == 0) goto found;
if (m == l) break;
if (r < 0) h = m;
else l = m;
}
return NULL;
found:
return dict[m].val;
}