14
|
1 |
#include <stdio.h>
|
|
2 |
#include <sys/stat.h>
|
|
3 |
#include <sys/types.h>
|
|
4 |
#include <sys/mman.h>
|
|
5 |
#include <fcntl.h>
|
|
6 |
#include "dictre.h"
|
|
7 |
|
17
|
8 |
const static char indexext[] = ".index";
|
|
9 |
const static char dictext[] = ".dict";
|
14
|
10 |
|
|
11 |
int get_filesize(const char *fname)
|
|
12 |
{
|
|
13 |
struct stat st;
|
|
14 |
int res;
|
|
15 |
res = stat(fname, &st);
|
|
16 |
if (res == -1)
|
|
17 |
{
|
|
18 |
fprintf(stderr, "Problem stating the file %s\n", fname);
|
|
19 |
perror("Error:");
|
|
20 |
exit(-1);
|
|
21 |
}
|
|
22 |
|
|
23 |
return st.st_size;
|
|
24 |
}
|
|
25 |
|
17
|
26 |
void init_dictionary(struct Dict *d, const char *base)
|
14
|
27 |
{
|
17
|
28 |
char *filename;
|
|
29 |
|
|
30 |
filename = (char *) malloc(strlen(base) + 10);
|
|
31 |
|
|
32 |
/* Prepare .index filename and open it*/
|
|
33 |
strcpy(filename, base);
|
|
34 |
strcat(filename, indexext);
|
|
35 |
|
|
36 |
d->indexsize = get_filesize(filename);
|
|
37 |
d->indexfd = open(filename, O_RDONLY);
|
|
38 |
if (d->indexfd == -1)
|
14
|
39 |
{
|
17
|
40 |
fprintf(stderr, "Problem opening the file %s\n", filename);
|
14
|
41 |
perror("Error:");
|
|
42 |
exit(-1);
|
|
43 |
}
|
17
|
44 |
d->index = (unsigned char *) mmap(0, d->indexsize, PROT_READ, MAP_SHARED,
|
|
45 |
d->indexfd, 0);
|
14
|
46 |
|
17
|
47 |
/* Prepare .dict filename and open it*/
|
|
48 |
strcpy(filename, base);
|
|
49 |
strcat(filename, dictext);
|
|
50 |
d->defs = fopen(filename, "r");
|
|
51 |
if (d->defs == 0)
|
14
|
52 |
{
|
17
|
53 |
fprintf(stderr, "Problem opening the file %s\n", filename);
|
14
|
54 |
perror("Error:");
|
|
55 |
exit(-1);
|
|
56 |
}
|
|
57 |
|
17
|
58 |
free(filename);
|
14
|
59 |
}
|
|
60 |
|
17
|
61 |
void end_dictionary(struct Dict *d)
|
14
|
62 |
{
|
17
|
63 |
munmap(d->index, d->indexsize);
|
|
64 |
close(d->indexfd);
|
|
65 |
fclose(d->defs);
|
14
|
66 |
}
|
|
67 |
|
17
|
68 |
static void fill_def(struct Dict *d, int offset, int length, char * def)
|
14
|
69 |
{
|
17
|
70 |
fseek(d->defs, offset, SEEK_SET);
|
|
71 |
fread(def, 1, length, d->defs);
|
|
72 |
}
|
|
73 |
|
|
74 |
static int pointer_at_end(struct Dict *d, unsigned char *ptr)
|
|
75 |
{
|
|
76 |
if (ptr >= (d->index + d->indexsize))
|
14
|
77 |
return 1;
|
|
78 |
return 0;
|
|
79 |
}
|
|
80 |
|
17
|
81 |
static char * skip_until_newline(struct Dict *d, char *from)
|
14
|
82 |
{
|
17
|
83 |
if (pointer_at_end(d, from))
|
14
|
84 |
return 0;
|
|
85 |
while(*from != '\n' && *from != 0)
|
|
86 |
{
|
|
87 |
++from;
|
17
|
88 |
if(pointer_at_end(d, from))
|
14
|
89 |
return 0;
|
|
90 |
}
|
|
91 |
return from;
|
|
92 |
}
|
|
93 |
|
|
94 |
static int compare(const unsigned char *word, const unsigned char *test)
|
|
95 |
{
|
|
96 |
int i;
|
|
97 |
|
|
98 |
/*printf("Comparing %s to %.20s\n", word, test);*/
|
|
99 |
for(i=0; word[i] != 0 && test[i] != 0; ++i)
|
|
100 |
{
|
|
101 |
if (word[i] != test[i])
|
|
102 |
{
|
|
103 |
break;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
if (word[i] == 0 && test[i] == '\t')
|
|
107 |
return 0;
|
|
108 |
else if (word[i] == 0)
|
|
109 |
return -1;
|
|
110 |
else if (test[i] == '\t')
|
|
111 |
return 1;
|
|
112 |
else if (word[i] > test[i])
|
|
113 |
return 1;
|
|
114 |
else if (word[i] < test[i])
|
|
115 |
return -1;
|
|
116 |
|
|
117 |
/* It should never reach this. */
|
|
118 |
return -1;
|
|
119 |
}
|
|
120 |
|
17
|
121 |
static char * bin_search(struct Dict *d, const char *word)
|
14
|
122 |
{
|
|
123 |
int step, pivot;
|
|
124 |
|
17
|
125 |
pivot = d->indexsize / 2;
|
|
126 |
step = d->indexsize / 2;
|
14
|
127 |
|
|
128 |
do
|
|
129 |
{
|
|
130 |
char *test;
|
|
131 |
int comparision;
|
17
|
132 |
test = d->index + pivot;
|
|
133 |
test = skip_until_newline(d, test);
|
14
|
134 |
if (test == 0)
|
|
135 |
return 0;
|
|
136 |
test += 1; /* skip exactly the new line */
|
|
137 |
|
|
138 |
comparision = compare(word, test);
|
|
139 |
if (comparision == 0)
|
|
140 |
{
|
|
141 |
return test + strlen(word) + 1; /* skip word and \n */
|
|
142 |
} else if (comparision < 0)
|
|
143 |
{
|
|
144 |
step = step / 2;
|
|
145 |
pivot = pivot - step;
|
|
146 |
} else if (comparision > 0)
|
|
147 |
{
|
|
148 |
step = step / 2;
|
|
149 |
pivot = pivot + step;
|
|
150 |
}
|
|
151 |
} while(step > 0);
|
|
152 |
return 0;
|
|
153 |
}
|
|
154 |
|
|
155 |
static int my_get_int(char **pos)
|
|
156 |
{
|
|
157 |
int i;
|
|
158 |
char *start;
|
|
159 |
int val;
|
|
160 |
|
|
161 |
start = *pos;
|
|
162 |
for(i=0; start[i] != '\t' && start[i] != '\n'; ++i)
|
|
163 |
;
|
|
164 |
val = str2int_len(start, i);
|
|
165 |
*pos += i + 1;
|
|
166 |
return val;
|
|
167 |
}
|
|
168 |
|
17
|
169 |
void find_def(struct Dict *d, const char *word, char * def)
|
14
|
170 |
{
|
|
171 |
int offset, len;
|
|
172 |
char *pos;
|
|
173 |
|
17
|
174 |
pos = bin_search(d, word); /* pos points to the offset already. */
|
14
|
175 |
if (pos == 0)
|
|
176 |
{
|
|
177 |
def[0] = 0;
|
|
178 |
/*fprintf(stderr, "Cannot find %s\n", word);*/
|
|
179 |
return;
|
|
180 |
}
|
|
181 |
offset = my_get_int(&pos); /* increments pos */
|
|
182 |
len = my_get_int(&pos); /* increments pos */
|
17
|
183 |
fill_def(d, offset, len, def);
|
14
|
184 |
}
|