aboutsummaryrefslogtreecommitdiff
path: root/lib/strat.c
blob: b2ec3af695e007402057f9eba6aa7cf8112c52ab (plain)
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
/* This file is part of GNU Dico.
   Copyright (C) 2008, 2010, 2012 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/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <dico.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

/* List of configured matching strategies */
static dico_list_t /* of struct dico_strategy */ strategy_list;
static dico_strategy_t default_strategy;

#define DEFSTRATNAME(s) ((s)[0] == '.' && (s)[1] == 0)

int
dico_strat_name_cmp(const void *item, void *data)
{
    dico_strategy_t strat = (dico_strategy_t) item;
    const char *name = data;
    return strcmp(strat->name, name);
}

dico_strategy_t
dico_strategy_create(const char *name, const char *descr)
{
    dico_strategy_t np;
    size_t size = sizeof(*np) + strlen(name) + strlen(descr) + 2;
    np = malloc(size);
    if (np) {
	memset(np, 0, size);
	np->name = (char*)(np + 1);
	strcpy(np->name, name);
	np->descr = np->name + strlen(np->name) + 1;
	strcpy(np->descr, descr);
    }
    return np;
}

int
dico_strat_free(void *item, void *data)
{
    dico_strategy_t strat = item;
    dico_list_destroy(&strat->stratcl);
    free(strat);
    return 0;
}

dico_strategy_t
dico_strategy_dup(const dico_strategy_t strat)
{
    dico_strategy_t np = dico_strategy_create(strat->name, strat->descr);
    if (np) {
	np->sel = strat->sel;
	np->closure = strat->closure;
    }
    return np;
}

dico_strategy_t
dico_strategy_find(const char *name)
{
    if (DEFSTRATNAME(name)) 
	return default_strategy;
    return dico_list_locate(strategy_list, (void*)name);
}

int
dico_strategy_add(const dico_strategy_t strat)
{
    if (!strategy_list) {
	strategy_list = dico_list_create();
	if (!strategy_list)
	    return 1;
	dico_list_set_comparator(strategy_list, dico_strat_name_cmp);
	dico_list_set_free_item(strategy_list, dico_strat_free, NULL);
    }
    if (!dico_strategy_find(strat->name)) {
	dico_strategy_t new_strat = dico_strategy_dup(strat);
	if (!new_strat)
	    return 1;
	dico_list_append(strategy_list, new_strat);
    }
    return 0;
}

dico_iterator_t 
dico_strategy_iterator()
{
    return dico_list_iterator(strategy_list);
}

size_t
dico_strategy_count()
{
    return dico_list_count(strategy_list);
}

void
dico_strategy_iterate(dico_list_iterator_t itr, void *data)
{
    return dico_list_iterate(strategy_list, itr, data);
}

int
dico_set_default_strategy(const char *name)
{
    dico_strategy_t sp;

    if (DEFSTRATNAME(name) || (sp = dico_strategy_find(name)) == NULL) {
	errno = EINVAL;
	return 1;
    }
    if (default_strategy)
	default_strategy->is_default = 0;
    sp->is_default = 1;
    default_strategy = sp;
    return 0;
}

dico_strategy_t
dico_get_default_strategy()
{
    return default_strategy;
}

    

Return to:

Send suggestions and report system problems to the System administrator.