1
/* This file is part of GNU Dico.
2
Copyright (C) 2008 Sergey Poznyakoff
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)
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.
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/>. */
25
/* List of configured matching strategies */
26
static dico_list_t /* of struct dico_strategy */ strategy_list;
27
static dico_strategy_t default_strategy;
29
#define DEFSTRATNAME(s) ((s)[0] == '.' && (s)[1] == 0)
32
dico_strat_name_cmp(const void *item, void *data)
34
dico_strategy_t strat = (dico_strategy_t) item;
35
const char *name = data;
36
return strcmp(strat->name, name);
40
dico_strategy_create(const char *name, const char *descr)
43
size_t size = sizeof(*np) + strlen(name) + strlen(descr) + 2;
47
np->name = (char*)(np + 1);
48
strcpy(np->name, name);
49
np->descr = np->name + strlen(np->name) + 1;
50
strcpy(np->descr, descr);
56
dico_strat_free(void *item, void *data)
58
dico_strategy_t strat = item;
59
dico_list_destroy(&strat->stratcl);
65
dico_strategy_dup(const dico_strategy_t strat)
67
dico_strategy_t np = dico_strategy_create(strat->name, strat->descr);
70
np->closure = strat->closure;
76
dico_strategy_find(const char *name)
78
if (DEFSTRATNAME(name))
79
return default_strategy;
80
return dico_list_locate(strategy_list, (void*)name);
84
dico_strategy_add(const dico_strategy_t strat)
87
strategy_list = dico_list_create();
90
dico_list_set_comparator(strategy_list, dico_strat_name_cmp);
91
dico_list_set_free_item(strategy_list, dico_strat_free, NULL);
93
if (!dico_strategy_find(strat->name)) {
94
dico_strategy_t new_strat = dico_strategy_dup(strat);
97
dico_list_append(strategy_list, new_strat);
103
dico_strategy_iterator()
105
return dico_list_iterator(strategy_list);
109
dico_strategy_count()
111
return dico_list_count(strategy_list);
115
dico_strategy_iterate(dico_list_iterator_t itr, void *data)
117
return dico_list_iterate(strategy_list, itr, data);
121
dico_set_default_strategy(const char *name)
125
if (DEFSTRATNAME(name) || (sp = dico_strategy_find(name)) == NULL) {
129
if (default_strategy)
130
default_strategy->is_default = 0;
132
default_strategy = sp;
136
const dico_strategy_t
137
dico_get_default_strategy()
139
return default_strategy;