Debian Codegraph @

modules/word/word.c

raw
1 /* This file is part of GNU Dico.
2 Copyright (C) 2010-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 <stdlib.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <appi18n.h>
23
24 static int
25 word_sel(int cmd, dico_key_t key, const char *dict_word)
26 {
27 int rc = 0;
28 char const *key_word = key->word;
29 struct dico_tokbuf tb;
30 int i;
31
32 switch (cmd) {
33 case DICO_SELECT_BEGIN:
34 break;
35
36 case DICO_SELECT_RUN:
37 dico_tokenize_begin(&tb);
38 if (dico_tokenize_string(&tb, (char*) dict_word)) {
39 dico_tokenize_end(&tb);
40 return 0;
41 }
42 for (i = 0; i < tb.tb_tokc; i++)
43 if (utf8_strcasecmp(tb.tb_tokv[i], (char*) key_word) == 0) {
44 rc = 1;
45 break;
46 }
47 dico_tokenize_end(&tb);
48 break;
49
50 case DICO_SELECT_END:
51 break;
52 }
53 return rc;
54 }
55
56 static int
57 first_sel(int cmd, dico_key_t key, const char *dict_word)
58 {
59 int rc = 0;
60 char const *key_word = key->word;
61 struct dico_tokbuf tb;
62
63 switch (cmd) {
64 case DICO_SELECT_BEGIN:
65 break;
66
67 case DICO_SELECT_RUN:
68 dico_tokenize_begin(&tb);
69 if (dico_tokenize_string(&tb, (char*) dict_word) == 0 && tb.tb_tokc)
70 rc = utf8_strcasecmp(tb.tb_tokv[0], (char*) key_word) == 0;
71 dico_tokenize_end(&tb);
72 break;
73
74 case DICO_SELECT_END:
75 break;
76 }
77 return rc;
78 }
79
80 static int
81 last_sel(int cmd, dico_key_t key, const char *dict_word)
82 {
83 int rc = 0;
84 char const *key_word = key->word;
85 struct dico_tokbuf tb;
86
87 switch (cmd) {
88 case DICO_SELECT_BEGIN:
89 break;
90
91 case DICO_SELECT_RUN:
92 dico_tokenize_begin(&tb);
93 if (dico_tokenize_string(&tb, (char*) dict_word) == 0 && tb.tb_tokc)
94 rc = utf8_strcasecmp(tb.tb_tokv[tb.tb_tokc-1],
95 (char*) key_word) == 0;
96 dico_tokenize_end(&tb);
97 break;
98
99 case DICO_SELECT_END:
100 break;
101 }
102 return rc;
103 }
104
105 static struct dico_strategy strats[] = {
106 { "word", "Match a word anywhere in the headword", word_sel },
107 { "first", "Match the first word within headwords", first_sel },
108 { "last", "Match the last word within headwords", last_sel },
109 { NULL }
110 };
111
112 static struct dico_strategy *
113 findstrat(const char *name)
114 {
115 struct dico_strategy *sp;
116 for (sp = strats; sp->name; sp++)
117 if (strcmp(sp->name, name) == 0)
118 return sp;
119 dico_log(L_ERR, 0, _("unknown strategy: %s"), name);
120 return NULL;
121 }
122
123 static int
124 word_init(int argc, char **argv)
125 {
126 struct dico_strategy *sp;
127
128 if (argc == 1) {
129 for (sp = strats; sp->name; sp++)
130 dico_strategy_add(sp);
131 } else {
132 int i;
133
134 for (i = 1; i < argc; i++)
135 if ((sp = findstrat(argv[i])))
136 dico_strategy_add(sp);
137 }
138 return 0;
139 }
140
141 struct dico_database_module DICO_EXPORT(word, module) = {
142 DICO_MODULE_VERSION,
143 DICO_CAPA_NODB,
144 word_init,
145 };