diff options
Diffstat (limited to 'modules/gcide/gcide.c')
-rw-r--r-- | modules/gcide/gcide.c | 91 |
1 files changed, 89 insertions, 2 deletions
diff --git a/modules/gcide/gcide.c b/modules/gcide/gcide.c index fa10c02..fbbd788 100644 --- a/modules/gcide/gcide.c +++ b/modules/gcide/gcide.c | |||
@@ -296,7 +296,94 @@ gcide_free_db(dico_handle_t hp) | |||
296 | free_db(db); | 296 | free_db(db); |
297 | return 0; | 297 | return 0; |
298 | } | 298 | } |
299 | |||
300 | static int | ||
301 | _is_nl_or_punct(int c) | ||
302 | { | ||
303 | return !!strchr("\r\n!,-./:;?", c); | ||
304 | } | ||
305 | |||
306 | static char * | ||
307 | read_info_file(const char *fname, int first_line) | ||
308 | { | ||
309 | dico_stream_t stream; | ||
310 | int rc; | ||
311 | char *bufptr = NULL; | ||
312 | size_t bufsize = 0; | ||
313 | |||
314 | stream = dico_mapfile_stream_create(fname, DICO_STREAM_READ); | ||
315 | if (!stream) { | ||
316 | dico_log(L_NOTICE, errno, _("cannot create stream `%s'"), fname); | ||
317 | return NULL; | ||
318 | } | ||
319 | |||
320 | rc = dico_stream_open(stream); | ||
321 | if (rc) { | ||
322 | dico_log(L_ERR, 0, | ||
323 | _("cannot open stream `%s': %s"), | ||
324 | fname, dico_stream_strerror(stream, rc)); | ||
325 | dico_stream_destroy(&stream); | ||
326 | return NULL; | ||
327 | } | ||
328 | |||
329 | if (first_line) { | ||
330 | size_t n; | ||
331 | |||
332 | rc = dico_stream_getline(stream, &bufptr, &bufsize, &n); | ||
333 | if (rc) { | ||
334 | dico_log(L_ERR, 0, | ||
335 | _("read error in stream `%s': %s"), | ||
336 | fname, dico_stream_strerror(stream, rc)); | ||
337 | } else | ||
338 | dico_string_trim(bufptr, n, _is_nl_or_punct); | ||
339 | } else { | ||
340 | off_t size; | ||
341 | rc = dico_stream_size(stream, &size); | ||
342 | if (rc) { | ||
343 | dico_log(L_ERR, 0, | ||
344 | _("cannot get size of stream `%s': %s"), | ||
345 | fname, dico_stream_strerror(stream, rc)); | ||
346 | } else { | ||
347 | bufsize = size; | ||
348 | bufptr = malloc (bufsize + 1); | ||
349 | if (!bufptr) { | ||
350 | dico_log(L_ERR, errno, | ||
351 | _("cannot allocate dictionary information buffer")); | ||
352 | } else if ((rc = dico_stream_read(stream, bufptr, bufsize, NULL))) { | ||
353 | dico_log(L_ERR, 0, | ||
354 | _("read error in stream `%s': %s"), | ||
355 | fname, dico_stream_strerror(stream, rc)); | ||
356 | free(bufptr); | ||
357 | bufptr = NULL; | ||
358 | } else | ||
359 | bufptr[bufsize] = 0; | ||
360 | } | ||
361 | } | ||
362 | |||
363 | dico_stream_destroy(&stream); | ||
364 | return bufptr; | ||
365 | } | ||
366 | |||
367 | static char * | ||
368 | read_dictionary_info(struct gcide_db *db, int first_line) | ||
369 | { | ||
370 | char *fname = dico_full_file_name(db->db_dir, "INFO"); | ||
371 | char *info = read_info_file(fname, first_line); | ||
372 | free(fname); | ||
373 | return info; | ||
374 | } | ||
375 | |||
376 | char * | ||
377 | gcide_info(dico_handle_t hp) | ||
378 | { | ||
379 | return read_dictionary_info((struct gcide_db *) hp, 0); | ||
380 | } | ||
299 | 381 | ||
382 | char * | ||
383 | gcide_descr(dico_handle_t hp) | ||
384 | { | ||
385 | return read_dictionary_info((struct gcide_db *) hp, 1); | ||
386 | } | ||
300 | 387 | ||
301 | static gcide_iterator_t | 388 | static gcide_iterator_t |
302 | exact_match(struct gcide_db *db, const char *hw) | 389 | exact_match(struct gcide_db *db, const char *hw) |
@@ -748,8 +835,8 @@ struct dico_database_module DICO_EXPORT(gcide, module) = { | |||
748 | gcide_free_db, | 835 | gcide_free_db, |
749 | NULL, | 836 | NULL, |
750 | NULL, | 837 | NULL, |
751 | NULL, | 838 | gcide_info, |
752 | NULL, | 839 | gcide_descr, |
753 | NULL, | 840 | NULL, |
754 | gcide_match, | 841 | gcide_match, |
755 | gcide_define, | 842 | gcide_define, |