| 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 |
#include <config.h> |
| 18 |
#include <dico.h> |
| 19 |
#include <string.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <ctype.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <appi18n.h> |
| 25 |
|
| 26 |
static int |
| 27 |
substr_sel(int cmd, struct dico_key *key, const char *dict_word) |
| 28 |
{ |
| 29 |
unsigned *sample; |
| 30 |
unsigned *tmp; |
| 31 |
int res; |
| 32 |
|
| 33 |
switch (cmd) { |
| 34 |
case DICO_SELECT_BEGIN: |
| 35 |
if (utf8_mbstr_to_wc(key->word, &sample, NULL)) |
| 36 |
return 1; |
| 37 |
utf8_wc_strupper(sample); |
| 38 |
key->call_data = sample; |
| 39 |
break; |
| 40 |
|
| 41 |
case DICO_SELECT_RUN: |
| 42 |
sample = key->call_data; |
| 43 |
if (utf8_mbstr_to_wc(dict_word, &tmp, NULL)) |
| 44 |
return 0; |
| 45 |
utf8_wc_strupper(tmp); |
| 46 |
res = !!utf8_wc_strstr(tmp, sample); |
| 47 |
free(tmp); |
| 48 |
return res; |
| 49 |
|
| 50 |
case DICO_SELECT_END: |
| 51 |
free(key->call_data); |
| 52 |
break; |
| 53 |
} |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
static struct dico_strategy substr_strat = { |
| 58 |
"substr", |
| 59 |
"Match a substring anywhere in the headword", |
| 60 |
substr_sel |
| 61 |
}; |
| 62 |
|
| 63 |
static int |
| 64 |
substr_init(int argc, char **argv) |
| 65 |
{ |
| 66 |
dico_strategy_add(&substr_strat); |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
static unsigned * |
| 71 |
strtowc(const char *str) |
| 72 |
{ |
| 73 |
unsigned *buf; |
| 74 |
if (utf8_mbstr_to_wc(str, &buf, NULL)) { |
| 75 |
dico_log(L_ERR, errno, "cannot convert \"%s\"", str); |
| 76 |
abort(); |
| 77 |
} |
| 78 |
return buf; |
| 79 |
} |
| 80 |
|
| 81 |
static int |
| 82 |
substr_run_test(int argc, char **argv) |
| 83 |
{ |
| 84 |
unsigned *wa, *wb; |
| 85 |
unsigned const *p; |
| 86 |
int ret; |
| 87 |
|
| 88 |
argc--; |
| 89 |
argv++; |
| 90 |
|
| 91 |
if (argc != 2) { |
| 92 |
dico_log(L_ERR, 0, "bad argument list for substr test"); |
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
wa = strtowc(argv[0]); |
| 97 |
wb = strtowc(argv[1]); |
| 98 |
p = utf8_wc_strstr(wa, wb); |
| 99 |
if (p) { |
| 100 |
ret = 0; |
| 101 |
printf("%td\n", p - wa); |
| 102 |
} else |
| 103 |
ret = 1; |
| 104 |
free(wa); |
| 105 |
free(wb); |
| 106 |
return ret; |
| 107 |
} |
| 108 |
|
| 109 |
struct dico_database_module DICO_EXPORT(substr, module) = { |
| 110 |
.dico_version = DICO_MODULE_VERSION, |
| 111 |
.dico_capabilities = DICO_CAPA_NODB, |
| 112 |
.dico_init = substr_init, |
| 113 |
.dico_run_test = substr_run_test |
| 114 |
}; |