1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/* This file is part of GNU Dico.
Copyright (C) 2018-2024 Sergey Poznyakoff
GNU Dico is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Dico is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
#include <dicod.h>
struct dico_handle_struct {
char *name;
unsigned int initialized:1;
unsigned int is_virtual:1;
size_t vdb_count;
struct vdb_member vdb_memb[1];
};
struct dico_result_struct {
struct dico_handle_struct *vdb;
dicod_db_result_t *vdres[1];
};
int
vdb_free(void *item, void *data)
{
struct vdb_member *memb = item;
free(memb->name);
free(memb);
return 0;
}
dico_list_t
vdb_list_create(void)
{
dico_list_t lst = xdico_list_create();
dico_list_set_free_item(lst, vdb_free, NULL);
return lst;
}
static struct dico_result_struct *
virtual_result_new(struct dico_handle_struct *vdb)
{
struct dico_result_struct *result;
result = xzalloc(sizeof(*result)
+ (vdb->vdb_count - 1) * sizeof(result->vdres[0]));
result->vdb = vdb;
return result;
}
static int virtual_free_db(dico_handle_t hp);
static int
db_name_cmp(const void *item, const void *data, void *closure)
{
const dicod_database_t *db = item;
if (!db->name)
return 1;
return strcmp(db->name, (const char*)data);
}
dicod_database_t *
find_database_all(const char *name)
{
dicod_database_t *res;
dico_list_comp_t oldcmp = dico_list_get_comparator(database_list);
void *olddata = dico_list_get_comparator_data(database_list);
dico_list_set_comparator(database_list, db_name_cmp, NULL);
res = dico_list_locate(database_list, (void*) name);
dico_list_set_comparator(database_list, oldcmp, olddata);
return res;
}
static dico_handle_t
virtual_init_db_ext(const char *dbname, int argc, char **argv, void *extra)
{
struct dico_handle_struct *vdb;
dico_iterator_t itr;
struct vdb_member *mp;
int err = 0;
dico_list_t dblist = extra;
size_t dbcount = dico_list_count(dblist);
size_t i = 0;
vdb = xzalloc(sizeof(*vdb) + (dbcount - 1) * sizeof(vdb->vdb_memb[0]));
vdb->name = xstrdup(dbname);
vdb->vdb_count = dbcount;
itr = xdico_list_iterator(dblist);
for (mp = dico_iterator_first(itr); mp; mp = dico_iterator_next(itr)) {
dicod_database_t *db = find_database_all(mp->name);
if (db) {
vdb->vdb_memb[i] = *mp;
i++;
} else {
dico_log(LOG_ERR, 0, "no such database: %s", mp->name);
err = 1;
}
}
dico_iterator_destroy(&itr);
vdb->vdb_count = i;
if (err) {
virtual_free_db((dico_handle_t)vdb);
vdb = NULL;
}
return (dico_handle_t)vdb;
}
static int
virtual_free_db(dico_handle_t vdb)
{
/* DB list is associated with the dicod_dictionary_t structure and will
be freed along with it. */
free(vdb->name);
free(vdb);
return 0;
}
static int
virtual_open(dico_handle_t vdb)
{
if (!vdb->initialized) {
size_t i, j;
size_t n = vdb->vdb_count;
int is_virtual = 1;
for (i = j = 0; i < n; i++) {
dicod_database_t *db = find_database_all(vdb->vdb_memb[i].name);
if (db) {
vdb->vdb_memb[i].db = db;
if (i > j)
vdb->vdb_memb[j] = vdb->vdb_memb[i];
if (vdb->vdb_memb[j].cond != cond_any)
is_virtual = 0;
j++;
}
}
vdb->vdb_count = j;
vdb->initialized = 1;
vdb->is_virtual = is_virtual;
}
return 0;
}
static int
virtual_db_flags(dico_handle_t vdb)
{
return vdb->is_virtual ? DICO_DBF_VIRTUAL : DICO_DBF_DEFAULT;
}
static int
vdb_member_ok(struct vdb_member const *memb)
{
int res = 0;
if (database_session_visibility(memb->db) == 0)
return 0;
switch (memb->cond) {
case cond_any:
res = 1;
break;
case cond_mime:
res = option_mime;
break;
case cond_nomime:
res = !option_mime;
break;
}
return res;
}
static dico_result_t
virtual_match(dico_handle_t vdb, const dico_strategy_t strat, const char *word)
{
size_t i;
size_t n = vdb->vdb_count;
struct dico_result_struct *result = virtual_result_new(vdb);
for (i = 0; i < n; i++) {
if (vdb_member_ok(&vdb->vdb_memb[i])) {
result->vdres[i] = dicod_database_match(vdb->vdb_memb[i].db,
strat, word);
} else {
result->vdres[i] = NULL;
}
}
return result;
}
static dico_result_t
virtual_define(dico_handle_t vdb, const char *word)
{
size_t i;
size_t n = vdb->vdb_count;
struct dico_result_struct *result = virtual_result_new(vdb);
for (i = 0; i < n; i++) {
if (vdb_member_ok(&vdb->vdb_memb[i])) {
result->vdres[i] = dicod_database_define(vdb->vdb_memb[i].db, word);
} else {
result->vdres[i] = NULL;
}
}
return result;
}
static int
vdres_locate(dico_result_t result, size_t *pn)
{
size_t i = 0;
while (1) {
if (i == result->vdb->vdb_count)
return 1;
if (result->vdres[i]) {
size_t s = dicod_db_result_count(result->vdres[i]);
if (s > *pn)
break;
*pn -= s;
}
i++;
}
return i;
}
static int
virtual_output_result(dico_result_t result, size_t n, dico_stream_t str)
{
size_t i = vdres_locate(result, &n);
return dicod_db_result_output(result->vdres[i], n, str);
}
static size_t
virtual_result_count (dico_result_t result)
{
size_t i;
size_t count = 0;
for (i = 0; i < result->vdb->vdb_count; i++) {
if (result->vdres[i])
count += dicod_db_result_count(result->vdres[i]);
}
return count;
}
static size_t
virtual_compare_count (dico_result_t result)
{
size_t i;
size_t count = 0;
for (i = 0; i < result->vdb->vdb_count; i++) {
if (result->vdres[i])
count += dicod_db_result_compare_count(result->vdres[i]);
}
return count;
}
static void
virtual_free_result(dico_result_t result)
{
size_t i;
for (i = 0; i < result->vdb->vdb_count; i++) {
if (result->vdres[i]) {
dicod_db_result_free(result->vdres[i]);
result->vdres[i] = NULL;
}
}
free(result);
}
static char *
virtual_descr(dico_handle_t vdb)
{
size_t i;
size_t n = vdb->vdb_count;
char *prev = NULL;
for (i = 0; i < n; i++) {
if (vdb_member_ok(&vdb->vdb_memb[i])) {
char *p = dicod_database_get_descr(vdb->vdb_memb[i].db);
if (prev && strcmp(prev, p)) {
/* descriptions differ; too bad */
free(prev);
prev = NULL;
break;
}
prev = xstrdup(p);
dicod_database_free_descr(vdb->vdb_memb[i].db, p);
}
}
return prev;
}
static char *
virtual_info(dico_handle_t vdb)
{
size_t i;
size_t n = vdb->vdb_count;
char *prev = NULL;
for (i = 0; i < n; i++) {
if (vdb_member_ok(&vdb->vdb_memb[i])) {
char *p = dicod_database_get_info(vdb->vdb_memb[i].db);
if (prev && strcmp(prev, p)) {
/* descriptions differ; too bad */
free(prev);
prev = NULL;
break;
}
prev = xstrdup(p);
dicod_database_free_info(vdb->vdb_memb[i].db, p);
}
}
return prev;
}
static dicod_database_t *
virtual_result_db(dico_result_t result, size_t n)
{
size_t i = vdres_locate(result, &n);
return dicod_db_result_db(result->vdres[i], n, result_db_all);
}
struct dico_database_module virtual_builtin_module = {
.dico_version = DICO_MODULE_VERSION,
.dico_capabilities = DICO_CAPA_INIT_EXT,
.dico_init_db_ext = virtual_init_db_ext,
.dico_free_db = virtual_free_db,
.dico_open = virtual_open,
.dico_match = virtual_match,
.dico_define = virtual_define,
.dico_result_count = virtual_result_count,
.dico_compare_count = virtual_compare_count,
.dico_free_result = virtual_free_result,
.dico_db_flags = virtual_db_flags,
.dico_output_result = virtual_output_result,
.dico_db_info = virtual_info,
.dico_db_descr = virtual_descr,
.dico_result_db = virtual_result_db
};
|