| 1 |
/* This file is part of GNU Dico. |
| 2 |
Copyright (C) 2008-2024 Sergey Poznyakoff |
| 3 |
|
| 4 |
GNU Dico is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 3, or (at your option) |
| 7 |
any later version. |
| 8 |
|
| 9 |
GNU Dico is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* This module provides support for databases in Emacs outline format. |
| 18 |
It is intended to work as a testbed for Dico's database module system. |
| 19 |
Do not use it in production environment. |
| 20 |
|
| 21 |
An outline database has the following structure: |
| 22 |
|
| 23 |
* Description |
| 24 |
<text> |
| 25 |
|
| 26 |
* Info |
| 27 |
<text> |
| 28 |
|
| 29 |
* Languages |
| 30 |
<lang> [<lang>...] [: <lang> [<lang>...]] |
| 31 |
|
| 32 |
* MIME |
| 33 |
<Header>: <value> |
| 34 |
[<Header>: <value>...] |
| 35 |
|
| 36 |
* Dictionary |
| 37 |
** <entry> |
| 38 |
<text> |
| 39 |
[any number of entries follows] |
| 40 |
|
| 41 |
Any other text is ignored. |
| 42 |
|
| 43 |
To use this module, add the following to your dictd.conf file: |
| 44 |
|
| 45 |
handler outline { |
| 46 |
type loadable; |
| 47 |
command "outline"; |
| 48 |
} |
| 49 |
database { |
| 50 |
handler "outline <filename>"; |
| 51 |
... |
| 52 |
} |
| 53 |
*/ |
| 54 |
|
| 55 |
|
| 56 |
#include <config.h> |
| 57 |
#include <dico.h> |
| 58 |
#include <string.h> |
| 59 |
#include <stdlib.h> |
| 60 |
#include <stdio.h> |
| 61 |
#include <ctype.h> |
| 62 |
#include <errno.h> |
| 63 |
#include <appi18n.h> |
| 64 |
#include <wordsplit.h> |
| 65 |
|
| 66 |
static size_t compare_count; |
| 67 |
|
| 68 |
struct entry { |
| 69 |
char *word; /* Word */ |
| 70 |
size_t length; /* Its length in bytes */ |
| 71 |
size_t wordlen; /* Its length in characters */ |
| 72 |
off_t offset; /* Offset of the corresponding article in file */ |
| 73 |
size_t size; /* Size of the article */ |
| 74 |
struct entry *peer; |
| 75 |
}; |
| 76 |
|
| 77 |
struct outline_file { |
| 78 |
char *name; |
| 79 |
FILE *fp; |
| 80 |
size_t count; |
| 81 |
struct entry *index; |
| 82 |
struct entry *suf_index; |
| 83 |
|
| 84 |
struct entry *info_entry, *descr_entry, *lang_entry, *mime_entry; |
| 85 |
}; |
| 86 |
|
| 87 |
#define STATE_INITIAL 0 |
| 88 |
#define STATE_DICT 1 |
| 89 |
|
| 90 |
static size_t |
| 91 |
trimnl(char *buf) |
| 92 |
{ |
| 93 |
size_t len = strlen(buf); |
| 94 |
if (len > 0 && buf[len - 1] == '\n') |
| 95 |
buf[--len] = 0; |
| 96 |
return len; |
| 97 |
} |
| 98 |
|
| 99 |
static size_t |
| 100 |
trimws(char *buf) |
| 101 |
{ |
| 102 |
size_t len = strlen(buf); |
| 103 |
while (len > 0 && isspace(buf[len - 1])) |
| 104 |
buf[--len] = 0; |
| 105 |
return len; |
| 106 |
} |
| 107 |
|
| 108 |
static int |
| 109 |
find_header(struct outline_file *file, char *buf, size_t size, size_t *pread) |
| 110 |
{ |
| 111 |
while (fgets(buf, size, file->fp)) { |
| 112 |
size_t rdbytes = strlen(buf); |
| 113 |
size_t len = trimnl(buf); |
| 114 |
|
| 115 |
if (len > 0) { |
| 116 |
int level; |
| 117 |
|
| 118 |
for (level = 0; buf[level] == '*' && level < len; level++) |
| 119 |
; |
| 120 |
if (level) { |
| 121 |
*pread = rdbytes; |
| 122 |
return level; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
static off_t |
| 130 |
skipws(struct outline_file *file, char *buf, size_t size) |
| 131 |
{ |
| 132 |
while (fgets(buf, size, file->fp)) { |
| 133 |
size_t len = strlen(buf); |
| 134 |
if (!(len == 1 && buf[0] == '\n')) { |
| 135 |
fseek(file->fp, -(off_t)len, SEEK_CUR); |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
return ftell(file->fp); |
| 140 |
} |
| 141 |
|
| 142 |
static struct entry * |
| 143 |
alloc_entry(const char *text, size_t len) |
| 144 |
{ |
| 145 |
struct entry *ep = malloc(sizeof(*ep)); |
| 146 |
if (ep) { |
| 147 |
memset(ep, 0, sizeof(*ep)); |
| 148 |
ep->word = malloc(len + 1); |
| 149 |
if (!ep->word) { |
| 150 |
free(ep); |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
memcpy(ep->word, text, len); |
| 154 |
ep->word[len] = 0; |
| 155 |
ep->length = len; |
| 156 |
ep->wordlen = utf8_strlen(ep->word); |
| 157 |
} |
| 158 |
return ep; |
| 159 |
} |
| 160 |
|
| 161 |
static struct entry * |
| 162 |
read_entry(struct outline_file *file, int *plevel) |
| 163 |
{ |
| 164 |
char buf[128], *p; |
| 165 |
struct entry *ep; |
| 166 |
int level; |
| 167 |
size_t rdbytes, len; |
| 168 |
|
| 169 |
level = find_header(file, buf, sizeof(buf), &rdbytes); |
| 170 |
|
| 171 |
if (level == 0) |
| 172 |
return NULL; |
| 173 |
|
| 174 |
*plevel = level; |
| 175 |
for (p = buf + level; *p && isspace(*p); p++) |
| 176 |
; |
| 177 |
|
| 178 |
len = trimws(p); |
| 179 |
|
| 180 |
ep = alloc_entry(p, len); |
| 181 |
if (!ep) |
| 182 |
return NULL; |
| 183 |
|
| 184 |
ep->offset = skipws(file, buf, sizeof(buf)); |
| 185 |
|
| 186 |
find_header(file, buf, sizeof(buf), &rdbytes); |
| 187 |
fseek(file->fp, -(off_t)rdbytes, SEEK_CUR); |
| 188 |
ep->size = ftell(file->fp) - ep->offset; |
| 189 |
|
| 190 |
return ep; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
enum result_type { |
| 195 |
result_match, |
| 196 |
result_match_list, |
| 197 |
result_define |
| 198 |
}; |
| 199 |
|
| 200 |
struct result { |
| 201 |
struct outline_file *file; |
| 202 |
enum result_type type; |
| 203 |
size_t count; |
| 204 |
size_t compare_count; |
| 205 |
union { |
| 206 |
const struct entry *ep; |
| 207 |
dico_list_t list; |
| 208 |
} v; |
| 209 |
}; |
| 210 |
|
| 211 |
typedef int (*entry_match_t) (struct outline_file *file, |
| 212 |
const char *word, |
| 213 |
struct result *res); |
| 214 |
|
| 215 |
struct strategy_def { |
| 216 |
struct dico_strategy strat; |
| 217 |
entry_match_t match; |
| 218 |
}; |
| 219 |
|
| 220 |
static int exact_match(struct outline_file *, const char *, struct result *); |
| 221 |
static int prefix_match(struct outline_file *, const char *, struct result *); |
| 222 |
static int suffix_match(struct outline_file *file, const char *word, |
| 223 |
struct result *res); |
| 224 |
|
| 225 |
static struct strategy_def strat_tab[] = { |
| 226 |
{ { "exact", "Match words exactly" }, exact_match }, |
| 227 |
{ { "prefix", "Match word prefixes" }, prefix_match }, |
| 228 |
{ { "suffix", "Match word suffixes" }, suffix_match } |
| 229 |
}; |
| 230 |
|
| 231 |
static entry_match_t |
| 232 |
find_matcher(const char *strat) |
| 233 |
{ |
| 234 |
int i; |
| 235 |
for (i = 0; i < DICO_ARRAY_SIZE(strat_tab); i++) |
| 236 |
if (strcmp(strat, strat_tab[i].strat.name) == 0) |
| 237 |
return strat_tab[i].match; |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
static int |
| 242 |
outline_init(int argc, char **argv) |
| 243 |
{ |
| 244 |
int i; |
| 245 |
for (i = 0; i < DICO_ARRAY_SIZE(strat_tab); i++) |
| 246 |
dico_strategy_add(&strat_tab[i].strat); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
static int |
| 252 |
compare_entry(const void *a, const void *b) |
| 253 |
{ |
| 254 |
const struct entry *epa = a; |
| 255 |
const struct entry *epb = b; |
| 256 |
compare_count++; |
| 257 |
return utf8_strcasecmp(epa->word, epb->word); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
static void |
| 262 |
revert_word(char *dst, const char *src, size_t len) |
| 263 |
{ |
| 264 |
struct utf8_iterator itr; |
| 265 |
char *p = dst + len; |
| 266 |
|
| 267 |
*p = 0; |
| 268 |
for (utf8_iter_init(&itr, (char *)src, len); |
| 269 |
!utf8_iter_end_p(&itr); |
| 270 |
utf8_iter_next(&itr)) { |
| 271 |
p -= itr.curwidth; |
| 272 |
if (p < dst) |
| 273 |
break; |
| 274 |
memcpy(p, itr.curptr, itr.curwidth); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
static int |
| 279 |
init_suffix_index(struct outline_file *file) |
| 280 |
{ |
| 281 |
if (!file->suf_index) { |
| 282 |
size_t i; |
| 283 |
|
| 284 |
file->suf_index = calloc(file->count, sizeof(file->suf_index[0])); |
| 285 |
if (!file->suf_index) |
| 286 |
return 1; |
| 287 |
for (i = 0; i < file->count; i++) { |
| 288 |
char *p = malloc(file->index[i].length + 1); |
| 289 |
if (!p) { |
| 290 |
while (i > 0) |
| 291 |
free(file->suf_index[--i].word); |
| 292 |
free(file->suf_index); |
| 293 |
return 1; |
| 294 |
} |
| 295 |
revert_word(p, file->index[i].word, file->index[i].length); |
| 296 |
file->suf_index[i] = file->index[i]; |
| 297 |
file->suf_index[i].word = p; |
| 298 |
file->suf_index[i].peer = &file->index[i]; |
| 299 |
} |
| 300 |
} |
| 301 |
qsort(file->suf_index, file->count, sizeof(file->suf_index[0]), |
| 302 |
compare_entry); |
| 303 |
compare_count = 0; |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static int |
| 310 |
exact_match(struct outline_file *file, const char *word, struct result *res) |
| 311 |
{ |
| 312 |
struct entry x, *ep; |
| 313 |
x.word = (char*) word; |
| 314 |
x.length = strlen(word); |
| 315 |
x.wordlen = utf8_strlen(word); |
| 316 |
ep = bsearch(&x, file->index, file->count, sizeof(file->index[0]), |
| 317 |
compare_entry); |
| 318 |
if (ep) { |
| 319 |
res->type = result_match; |
| 320 |
res->v.ep = ep; |
| 321 |
res->count = 1; |
| 322 |
return 0; |
| 323 |
} |
| 324 |
return 1; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
static int |
| 329 |
compare_prefix(const void *a, const void *b) |
| 330 |
{ |
| 331 |
const struct entry *pkey = a; |
| 332 |
const struct entry *pelt = b; |
| 333 |
size_t wordlen = pkey->wordlen; |
| 334 |
if (pelt->wordlen < wordlen) |
| 335 |
wordlen = pelt->wordlen; |
| 336 |
compare_count++; |
| 337 |
return utf8_strncasecmp(pkey->word, pelt->word, wordlen); |
| 338 |
} |
| 339 |
|
| 340 |
static int |
| 341 |
prefix_match(struct outline_file *file, const char *word, struct result *res) |
| 342 |
{ |
| 343 |
struct entry x, *ep; |
| 344 |
x.word = (char*) word; |
| 345 |
x.length = strlen(word); |
| 346 |
x.wordlen = utf8_strlen(word); |
| 347 |
ep = bsearch(&x, file->index, file->count, sizeof(file->index[0]), |
| 348 |
compare_prefix); |
| 349 |
if (ep) { |
| 350 |
size_t count = 1; |
| 351 |
struct entry *p; |
| 352 |
for (p = ep - 1; p > file->index && compare_prefix(&x, p) == 0; p--) |
| 353 |
count++; |
| 354 |
for (ep++; ep < file->index + file->count |
| 355 |
&& compare_prefix(&x, ep) == 0; ep++) |
| 356 |
count++; |
| 357 |
res->type = result_match; |
| 358 |
res->v.ep = p + 1; |
| 359 |
res->count = count; |
| 360 |
return 0; |
| 361 |
} |
| 362 |
return 1; |
| 363 |
} |
| 364 |
|
| 365 |
static int |
| 366 |
compare_entry_ptr(const void *a, const void *b) |
| 367 |
{ |
| 368 |
struct entry *const *epa = a; |
| 369 |
struct entry *const *epb = b; |
| 370 |
compare_count++; |
| 371 |
return utf8_strcasecmp((*epa)->word, (*epb)->word); |
| 372 |
} |
| 373 |
|
| 374 |
static int |
| 375 |
suffix_match(struct outline_file *file, const char *word, struct result *res) |
| 376 |
{ |
| 377 |
struct entry x, *ep; |
| 378 |
int rc; |
| 379 |
|
| 380 |
if (init_suffix_index(file)) { |
| 381 |
dico_log(L_ERR, 0, "not enough memory"); |
| 382 |
return 1; |
| 383 |
} |
| 384 |
|
| 385 |
x.length = strlen(word); |
| 386 |
x.word = malloc(x.length + 1); |
| 387 |
if (!x.word) { |
| 388 |
dico_log(L_ERR, 0, "not enough memory"); |
| 389 |
return 1; |
| 390 |
} |
| 391 |
x.wordlen = utf8_strlen(word); |
| 392 |
|
| 393 |
revert_word(x.word, word, x.length); |
| 394 |
|
| 395 |
ep = bsearch(&x, file->suf_index, file->count, sizeof(file->suf_index[0]), |
| 396 |
compare_prefix); |
| 397 |
if (ep) { |
| 398 |
struct entry *p, **epp; |
| 399 |
size_t count = 1; |
| 400 |
|
| 401 |
for (p = ep - 1; p > file->suf_index && compare_prefix(&x, p) == 0; |
| 402 |
p--) |
| 403 |
count++; |
| 404 |
for (ep++; ep < file->suf_index + file->count |
| 405 |
&& compare_prefix(&x, ep) == 0; ep++) |
| 406 |
count++; |
| 407 |
p++; |
| 408 |
|
| 409 |
epp = calloc(count, sizeof(*epp)); |
| 410 |
if (!epp) { |
| 411 |
dico_log(L_ERR, 0, "not enough memory"); |
| 412 |
rc = 1; |
| 413 |
} else { |
| 414 |
res->type = result_match_list; |
| 415 |
res->v.list = dico_list_create(); |
| 416 |
if (!res->v.list) { |
| 417 |
dico_log(L_ERR, 0, "not enough memory"); |
| 418 |
rc = 1; |
| 419 |
} else { |
| 420 |
size_t i; |
| 421 |
|
| 422 |
for (i = 0; i < count; i++) |
| 423 |
epp[i] = p[i].peer; |
| 424 |
qsort(epp, count, sizeof(epp[0]), compare_entry_ptr); |
| 425 |
|
| 426 |
for (i = 0; i < count; i++) |
| 427 |
dico_list_append(res->v.list, epp[i]); |
| 428 |
res->count = dico_list_count(res->v.list); |
| 429 |
rc = 0; |
| 430 |
} |
| 431 |
free(epp); |
| 432 |
} |
| 433 |
} else |
| 434 |
rc = 1; |
| 435 |
free(x.word); |
| 436 |
return rc; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
static int |
| 441 |
outline_free_db (dico_handle_t hp) |
| 442 |
{ |
| 443 |
size_t i; |
| 444 |
struct outline_file *file = (struct outline_file *) hp; |
| 445 |
|
| 446 |
fclose(file->fp); |
| 447 |
free(file->name); |
| 448 |
free(file->info_entry); |
| 449 |
free(file->descr_entry); |
| 450 |
free(file->lang_entry); |
| 451 |
free(file->mime_entry); |
| 452 |
for (i = 0; i < file->count; i++) { |
| 453 |
free(file->index[i].word); |
| 454 |
if (file->suf_index) |
| 455 |
free(file->suf_index[i].word); |
| 456 |
} |
| 457 |
free(file->index); |
| 458 |
free(file->suf_index); |
| 459 |
free(file); |
| 460 |
return 0; |
| 461 |
} |
| 462 |
|
| 463 |
static dico_handle_t |
| 464 |
outline_init_db(const char *dbname, int argc, char **argv) |
| 465 |
{ |
| 466 |
FILE *fp; |
| 467 |
struct outline_file *file; |
| 468 |
dico_list_t list; |
| 469 |
struct entry *ep; |
| 470 |
int level; |
| 471 |
int state; |
| 472 |
size_t i, count; |
| 473 |
dico_iterator_t itr; |
| 474 |
|
| 475 |
if (argc != 2) { |
| 476 |
dico_log(L_ERR, 0, _("outline_open: wrong number of arguments")); |
| 477 |
return NULL; |
| 478 |
} |
| 479 |
|
| 480 |
fp = fopen(argv[1], "r"); |
| 481 |
if (!fp) { |
| 482 |
dico_log(L_ERR, errno, _("cannot open file %s"), argv[1]); |
| 483 |
return NULL; |
| 484 |
} |
| 485 |
file = malloc(sizeof(*file)); |
| 486 |
if (!file) { |
| 487 |
dico_log(L_ERR, 0, "not enough memory"); |
| 488 |
fclose(fp); |
| 489 |
return NULL; |
| 490 |
} |
| 491 |
|
| 492 |
memset(file, 0, sizeof(*file)); |
| 493 |
file->name = strdup(argv[1]); |
| 494 |
file->fp = fp; |
| 495 |
|
| 496 |
list = dico_list_create(); |
| 497 |
if (!list) { |
| 498 |
dico_log(L_ERR, 0, "not enough memory"); |
| 499 |
fclose(fp); |
| 500 |
free(file); |
| 501 |
return NULL; |
| 502 |
} |
| 503 |
|
| 504 |
state = STATE_INITIAL; |
| 505 |
while ((ep = read_entry(file, &level))) { |
| 506 |
switch (state) { |
| 507 |
case STATE_DICT: |
| 508 |
if (level == 2) { |
| 509 |
dico_list_append(list, ep); |
| 510 |
break; |
| 511 |
} else if (level == 1) { |
| 512 |
state = STATE_INITIAL; |
| 513 |
/* FALL THROUGH */ |
| 514 |
} else { |
| 515 |
free(ep); |
| 516 |
break; |
| 517 |
} |
| 518 |
|
| 519 |
case STATE_INITIAL: |
| 520 |
if (level == 1) { |
| 521 |
if (strcasecmp(ep->word, "info") == 0) { |
| 522 |
file->info_entry = ep; |
| 523 |
break; |
| 524 |
} else if (strcasecmp(ep->word, "description") == 0) { |
| 525 |
file->descr_entry = ep; |
| 526 |
break; |
| 527 |
} else if (strcasecmp(ep->word, "languages") == 0) { |
| 528 |
file->lang_entry = ep; |
| 529 |
break; |
| 530 |
} else if (strcasecmp(ep->word, "mime") == 0) { |
| 531 |
file->mime_entry = ep; |
| 532 |
break; |
| 533 |
} else if (strcasecmp(ep->word, "dictionary") == 0) |
| 534 |
state = STATE_DICT; |
| 535 |
} |
| 536 |
free(ep); |
| 537 |
break; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
file->count = count = dico_list_count(list); |
| 542 |
file->index = calloc(count, sizeof(file->index[0])); |
| 543 |
if (!file->index) { |
| 544 |
dico_log(L_ERR, 0, "not enough memory"); |
| 545 |
outline_free_db((dico_handle_t)file); |
| 546 |
return NULL; |
| 547 |
} |
| 548 |
|
| 549 |
itr = dico_list_iterator(list); |
| 550 |
for (i = 0, ep = dico_iterator_first(itr); ep; |
| 551 |
i++, ep = dico_iterator_next(itr)) { |
| 552 |
file->index[i] = *ep; |
| 553 |
free(ep); |
| 554 |
} |
| 555 |
dico_iterator_destroy(&itr); |
| 556 |
dico_list_destroy(&list); |
| 557 |
qsort(file->index, count, sizeof(file->index[0]), compare_entry); |
| 558 |
|
| 559 |
return (dico_handle_t) file; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
static inline int |
| 564 |
isws(int c) |
| 565 |
{ |
| 566 |
return c == ' ' || c == '\t' || c == '\r' || c == '\f'; |
| 567 |
} |
| 568 |
|
| 569 |
static char * |
| 570 |
read_buf(struct outline_file *file, struct entry *ep, int trim) |
| 571 |
{ |
| 572 |
size_t size; |
| 573 |
char *buf = malloc(ep->size + 1); |
| 574 |
if (!buf) |
| 575 |
return NULL; |
| 576 |
fseek(file->fp, ep->offset, SEEK_SET); |
| 577 |
size = fread(buf, 1, ep->size, file->fp); |
| 578 |
if (trim && size > 0 && buf[size-1] == '\n') { |
| 579 |
while (size > 0 && buf[size-1] == '\n') { |
| 580 |
--size; |
| 581 |
while (size > 0 && isws(buf[size-1])) |
| 582 |
--size; |
| 583 |
} |
| 584 |
buf[size++] = '\n'; |
| 585 |
} |
| 586 |
buf[size] = 0; |
| 587 |
return buf; |
| 588 |
} |
| 589 |
|
| 590 |
static char * |
| 591 |
outline_info(dico_handle_t hp) |
| 592 |
{ |
| 593 |
struct outline_file *file = (struct outline_file *) hp; |
| 594 |
if (file->info_entry) |
| 595 |
return read_buf(file, file->info_entry, 0); |
| 596 |
return NULL; |
| 597 |
} |
| 598 |
|
| 599 |
static char * |
| 600 |
outline_descr(dico_handle_t hp) |
| 601 |
{ |
| 602 |
struct outline_file *file = (struct outline_file *) hp; |
| 603 |
if (file->descr_entry) { |
| 604 |
char *buf = read_buf(file, file->descr_entry, 0); |
| 605 |
char *p = strchr(buf, '\n'); |
| 606 |
if (p) |
| 607 |
*p = 0; |
| 608 |
return buf; |
| 609 |
} |
| 610 |
return NULL; |
| 611 |
} |
| 612 |
|
| 613 |
static int |
| 614 |
outline_lang(dico_handle_t hp, dico_list_t list[2]) |
| 615 |
{ |
| 616 |
struct outline_file *file = (struct outline_file *) hp; |
| 617 |
|
| 618 |
list[0] = list[1] = NULL; |
| 619 |
if (file->lang_entry) { |
| 620 |
int n = 0; |
| 621 |
struct wordsplit ws; |
| 622 |
char *buf = read_buf(file, file->lang_entry, 0); |
| 623 |
|
| 624 |
ws.ws_delim = "\n"; |
| 625 |
if (wordsplit(buf, &ws, WRDSF_DEFFLAGS|WRDSF_DELIM) == 0) { |
| 626 |
if (ws.ws_wordc) { |
| 627 |
int i; |
| 628 |
for (i = 0; i < ws.ws_wordc; i++) { |
| 629 |
if (n == 0 && strcmp(ws.ws_wordv[i], ":") == 0) { |
| 630 |
n = 1; |
| 631 |
free(ws.ws_wordv[i]); |
| 632 |
} else { |
| 633 |
if (!list[n]) |
| 634 |
list[n] = dico_list_create(); |
| 635 |
dico_list_append(list[n], ws.ws_wordv[i]); |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
ws.ws_wordc = 0; |
| 640 |
wordsplit_free(&ws); |
| 641 |
} else { |
| 642 |
dico_log(L_ERR, 0, _("outline_lang: not enough memory")); |
| 643 |
return 1; |
| 644 |
} |
| 645 |
} |
| 646 |
return 0; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
static dico_result_t |
| 651 |
outline_match0(dico_handle_t hp, entry_match_t match, const char *word) |
| 652 |
{ |
| 653 |
struct outline_file *file = (struct outline_file *) hp; |
| 654 |
struct result *res; |
| 655 |
|
| 656 |
compare_count = 0; |
| 657 |
res = malloc(sizeof(*res)); |
| 658 |
if (!res) |
| 659 |
return NULL; |
| 660 |
res->file = file; |
| 661 |
if (match(file, word, res)) { |
| 662 |
free(res); |
| 663 |
res = NULL; |
| 664 |
} else |
| 665 |
res->compare_count = compare_count; |
| 666 |
return (dico_result_t) res; |
| 667 |
} |
| 668 |
|
| 669 |
static dico_result_t |
| 670 |
outline_match_all(dico_handle_t hp, dico_strategy_t strat, const char *word) |
| 671 |
{ |
| 672 |
struct outline_file *file = (struct outline_file *) hp; |
| 673 |
dico_list_t list; |
| 674 |
size_t count, i; |
| 675 |
struct result *res; |
| 676 |
struct dico_key key; |
| 677 |
|
| 678 |
list = dico_list_create(); |
| 679 |
|
| 680 |
if (!list) { |
| 681 |
dico_log(L_ERR, 0, _("outline_match_all: not enough memory")); |
| 682 |
return NULL; |
| 683 |
} |
| 684 |
|
| 685 |
if (dico_key_init(&key, strat, word)) { |
| 686 |
dico_log(L_ERR, 0, _("outline_match_all: key initialization failed")); |
| 687 |
return NULL; |
| 688 |
} |
| 689 |
|
| 690 |
for (i = 0; i < file->count; i++) { |
| 691 |
if (dico_key_match(&key, file->index[i].word)) |
| 692 |
dico_list_append(list, &file->index[i]); |
| 693 |
} |
| 694 |
|
| 695 |
dico_key_deinit(&key); |
| 696 |
|
| 697 |
compare_count = file->count; |
| 698 |
|
| 699 |
count = dico_list_count(list); |
| 700 |
if (count == 0) { |
| 701 |
dico_list_destroy(&list); |
| 702 |
return NULL; |
| 703 |
} |
| 704 |
|
| 705 |
res = malloc(sizeof(*res)); |
| 706 |
if (!res) |
| 707 |
return NULL; |
| 708 |
res->file = file; |
| 709 |
res->type = result_match_list; |
| 710 |
res->count = count; |
| 711 |
res->v.list = list; |
| 712 |
res->compare_count = compare_count; |
| 713 |
return (dico_result_t) res; |
| 714 |
} |
| 715 |
|
| 716 |
static dico_result_t |
| 717 |
outline_match(dico_handle_t hp, const dico_strategy_t strat, const char *word) |
| 718 |
{ |
| 719 |
entry_match_t match = find_matcher(strat->name); |
| 720 |
if (match) |
| 721 |
return outline_match0(hp, match, word); |
| 722 |
else if (strat->sel) |
| 723 |
return outline_match_all(hp, strat, word); |
| 724 |
return NULL; |
| 725 |
} |
| 726 |
|
| 727 |
static dico_result_t |
| 728 |
outline_define(dico_handle_t hp, const char *word) |
| 729 |
{ |
| 730 |
struct outline_file *file = (struct outline_file *) hp; |
| 731 |
struct result *res; |
| 732 |
|
| 733 |
compare_count = 0; |
| 734 |
res = malloc(sizeof(*res)); |
| 735 |
if (!res) |
| 736 |
return NULL; |
| 737 |
res->file = file; |
| 738 |
if (exact_match(file, word, res)) { |
| 739 |
free(res); |
| 740 |
res = NULL; |
| 741 |
} else { |
| 742 |
res->type = result_define; |
| 743 |
res->compare_count = compare_count; |
| 744 |
} |
| 745 |
return (dico_result_t) res; |
| 746 |
} |
| 747 |
|
| 748 |
static void |
| 749 |
printdef(dico_stream_t str, struct outline_file *file, const struct entry *ep) |
| 750 |
{ |
| 751 |
FILE *fp = file->fp; |
| 752 |
size_t size = ep->size; |
| 753 |
char buf[128]; |
| 754 |
|
| 755 |
fseek(fp, ep->offset, SEEK_SET); |
| 756 |
while (size) { |
| 757 |
size_t rdsize = size; |
| 758 |
if (rdsize > sizeof(buf)) |
| 759 |
rdsize = sizeof(buf); |
| 760 |
rdsize = fread(buf, 1, rdsize, fp); |
| 761 |
if (rdsize == 0) |
| 762 |
break; |
| 763 |
dico_stream_write(str, buf, rdsize); |
| 764 |
size -= rdsize; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
static int |
| 769 |
outline_output_result (dico_result_t rp, size_t n, dico_stream_t str) |
| 770 |
{ |
| 771 |
struct result *res = (struct result *) rp; |
| 772 |
const struct entry *ep; |
| 773 |
|
| 774 |
switch (res->type) { |
| 775 |
case result_match: |
| 776 |
ep = res->v.ep + n; |
| 777 |
dico_stream_write(str, ep->word, strlen(ep->word)); |
| 778 |
break; |
| 779 |
|
| 780 |
case result_match_list: |
| 781 |
ep = dico_list_item(res->v.list, n); |
| 782 |
dico_stream_write(str, ep->word, strlen(ep->word)); |
| 783 |
break; |
| 784 |
|
| 785 |
case result_define: |
| 786 |
ep = res->v.ep + n; |
| 787 |
printdef(str, res->file, ep); |
| 788 |
} |
| 789 |
return 0; |
| 790 |
} |
| 791 |
|
| 792 |
static size_t |
| 793 |
outline_result_count (dico_result_t rp) |
| 794 |
{ |
| 795 |
struct result *res = (struct result *) rp; |
| 796 |
return res->count; |
| 797 |
} |
| 798 |
|
| 799 |
static size_t |
| 800 |
outline_compare_count (dico_result_t rp) |
| 801 |
{ |
| 802 |
struct result *res = (struct result *) rp; |
| 803 |
return res->compare_count; |
| 804 |
} |
| 805 |
|
| 806 |
static void |
| 807 |
outline_free_result(dico_result_t rp) |
| 808 |
{ |
| 809 |
struct result *res = (struct result *) rp; |
| 810 |
if (res->type == result_match_list) |
| 811 |
dico_list_destroy(&res->v.list); |
| 812 |
free(rp); |
| 813 |
} |
| 814 |
|
| 815 |
static char * |
| 816 |
outline_db_mime_header(dico_handle_t hp) |
| 817 |
{ |
| 818 |
struct outline_file *file = (struct outline_file *) hp; |
| 819 |
if (file->mime_entry) |
| 820 |
return read_buf(file, file->mime_entry, 1); |
| 821 |
return NULL; |
| 822 |
} |
| 823 |
|
| 824 |
struct dico_database_module DICO_EXPORT(outline, module) = { |
| 825 |
.dico_version = DICO_MODULE_VERSION, |
| 826 |
.dico_capabilities = DICO_CAPA_NONE, |
| 827 |
.dico_init = outline_init, |
| 828 |
.dico_init_db = outline_init_db, |
| 829 |
.dico_free_db = outline_free_db, |
| 830 |
.dico_db_info = outline_info, |
| 831 |
.dico_db_descr = outline_descr, |
| 832 |
.dico_db_lang = outline_lang, |
| 833 |
.dico_match = outline_match, |
| 834 |
.dico_define = outline_define, |
| 835 |
.dico_output_result = outline_output_result, |
| 836 |
.dico_result_count = outline_result_count, |
| 837 |
.dico_compare_count = outline_compare_count, |
| 838 |
.dico_free_result = outline_free_result, |
| 839 |
.dico_db_mime_header = outline_db_mime_header |
| 840 |
}; |