Debian Codegraph @

modules/nprefix/nprefix.c

raw
1 /* This file is part of GNU Dico.
2 Copyright (C) 2012-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 <string.h>
21 #include <ctype.h>
22 #include <errno.h>
23
24 /* MATCH <dict> nprefix <skip>#<count>#<string>
25
26 Returns at most <count> headwords whose prefix matches <string>,
27 skipping <skip> first unique matches.
28 */
29
30 struct nprefix {
31 char *prefix; /* Prefix being looked for */
32 size_t pfxlen; /* Its length in characters */
33 int lim; /* If true, use the limits below */
34 size_t skip; /* Skip this number of matches */
35 size_t count; /* Display at most this number of matches */
36 size_t n; /* Number of the current match */
37 char *last_match_str; /* Last matched string */
38 size_t last_match_len; /* Size of last_match_str */
39 };
40
41 static int
42 nprefix_sel(int cmd, dico_key_t key, const char *dict_word)
43 {
44 char const *key_word = key->word;
45 struct nprefix *np;
46 size_t wordlen;
47
48 switch (cmd) {
49 case DICO_SELECT_BEGIN: {
50 char *p;
51 size_t skip, count;
52
53 np = calloc(1, sizeof(np[0]));
54 if (!np) {
55 DICO_LOG_MEMERR();
56 return 1;
57 }
58 np->prefix = (char*)key_word;
59 np->lim = 0;
60 skip = strtoul(key_word, &p, 10);
61 if (*p == '#') {
62 count = strtoul(p + 1, &p, 10);
63 if (*p == '#') {
64 np->prefix = p + 1;
65 np->skip = skip;
66 np->count = count;
67 np->lim = 1;
68 }
69 }
70 np->pfxlen = utf8_strlen(np->prefix);
71 key->call_data = np;
72 break;
73 }
74
75 case DICO_SELECT_RUN:
76 np = key->call_data;
77 if (np->last_match_str &&
78 utf8_strcasecmp((char*)dict_word, np->last_match_str) == 0)
79 return 0;
80 if (np->lim && np->n > np->skip + np->count)
81 return 0;
82 wordlen = utf8_strlen(dict_word);
83 if (wordlen >= np->pfxlen &&
84 utf8_strncasecmp((char*)dict_word, np->prefix, np->pfxlen) == 0) {
85 size_t s = strlen(dict_word) + 1;
86 if (np->last_match_len < s) {
87 char *p = realloc(np->last_match_str, s);
88 if (!p)
89 return 0;
90
91 np->last_match_str = p;
92 np->last_match_len = s;
93 }
94 strcpy(np->last_match_str, dict_word);
95 if (!np->lim)
96 return 1;
97 np->n++;
98 return np->n > np->skip && np->n <= np->skip + np->count;
99 }
100 break;
101
102 case DICO_SELECT_END:
103 np = key->call_data;
104 free(np->last_match_str);
105 free(np);
106 break;
107 }
108 return 0;
109 }
110
111 static struct dico_strategy nprefix_strat[] = {
112 { "nprefix", "Match prefixes, [skip#count#]prefix", nprefix_sel },
113 { NULL }
114 };
115
116 static int
117 nprefix_init(int argc, char **argv)
118 {
119 dico_strategy_add(nprefix_strat);
120 return 0;
121 }
122
123 struct dico_database_module DICO_EXPORT(nprefix, module) = {
124 DICO_MODULE_VERSION,
125 DICO_CAPA_NODB,
126 nprefix_init,
127 };