~ubuntu-branches/debian/sid/dico/sid

« back to all changes in this revision

Viewing changes to lib/strat.c

  • Committer: Bazaar Package Importer
  • Author(s): أحمد المحمودي (Ahmed El-Mahmoudy)
  • Date: 2009-04-03 06:28:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090403062825-n4tbn09hv9ve5s2s
Tags: upstream-2.0
Import upstream version 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of GNU Dico.
 
2
   Copyright (C) 2008 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
#ifdef HAVE_CONFIG_H
 
18
# include <config.h>
 
19
#endif
 
20
#include <dico.h>
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <errno.h>
 
24
 
 
25
/* List of configured matching strategies */
 
26
static dico_list_t /* of struct dico_strategy */ strategy_list;
 
27
static dico_strategy_t default_strategy;
 
28
 
 
29
#define DEFSTRATNAME(s) ((s)[0] == '.' && (s)[1] == 0)
 
30
 
 
31
int
 
32
dico_strat_name_cmp(const void *item, void *data)
 
33
{
 
34
    dico_strategy_t strat = (dico_strategy_t) item;
 
35
    const char *name = data;
 
36
    return strcmp(strat->name, name);
 
37
}
 
38
 
 
39
dico_strategy_t
 
40
dico_strategy_create(const char *name, const char *descr)
 
41
{
 
42
    dico_strategy_t np;
 
43
    size_t size = sizeof(*np) + strlen(name) + strlen(descr) + 2;
 
44
    np = malloc(size);
 
45
    if (np) {
 
46
        memset(np, 0, size);
 
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);
 
51
    }
 
52
    return np;
 
53
}
 
54
 
 
55
int
 
56
dico_strat_free(void *item, void *data)
 
57
{
 
58
    dico_strategy_t strat = item;
 
59
    dico_list_destroy(&strat->stratcl);
 
60
    free(strat);
 
61
    return 0;
 
62
}
 
63
 
 
64
dico_strategy_t
 
65
dico_strategy_dup(const dico_strategy_t strat)
 
66
{
 
67
    dico_strategy_t np = dico_strategy_create(strat->name, strat->descr);
 
68
    if (np) {
 
69
        np->sel = strat->sel;
 
70
        np->closure = strat->closure;
 
71
    }
 
72
    return np;
 
73
}
 
74
 
 
75
dico_strategy_t
 
76
dico_strategy_find(const char *name)
 
77
{
 
78
    if (DEFSTRATNAME(name)) 
 
79
        return default_strategy;
 
80
    return dico_list_locate(strategy_list, (void*)name);
 
81
}
 
82
 
 
83
int
 
84
dico_strategy_add(const dico_strategy_t strat)
 
85
{
 
86
    if (!strategy_list) {
 
87
        strategy_list = dico_list_create();
 
88
        if (!strategy_list)
 
89
            return 1;
 
90
        dico_list_set_comparator(strategy_list, dico_strat_name_cmp);
 
91
        dico_list_set_free_item(strategy_list, dico_strat_free, NULL);
 
92
    }
 
93
    if (!dico_strategy_find(strat->name)) {
 
94
        dico_strategy_t new_strat = dico_strategy_dup(strat);
 
95
        if (!new_strat)
 
96
            return 1;
 
97
        dico_list_append(strategy_list, new_strat);
 
98
    }
 
99
    return 0;
 
100
}
 
101
 
 
102
dico_iterator_t 
 
103
dico_strategy_iterator()
 
104
{
 
105
    return dico_list_iterator(strategy_list);
 
106
}
 
107
 
 
108
size_t
 
109
dico_strategy_count()
 
110
{
 
111
    return dico_list_count(strategy_list);
 
112
}
 
113
 
 
114
void
 
115
dico_strategy_iterate(dico_list_iterator_t itr, void *data)
 
116
{
 
117
    return dico_list_iterate(strategy_list, itr, data);
 
118
}
 
119
 
 
120
int
 
121
dico_set_default_strategy(const char *name)
 
122
{
 
123
    dico_strategy_t sp;
 
124
 
 
125
    if (DEFSTRATNAME(name) || (sp = dico_strategy_find(name)) == NULL) {
 
126
        errno = EINVAL;
 
127
        return 1;
 
128
    }
 
129
    if (default_strategy)
 
130
        default_strategy->is_default = 0;
 
131
    sp->is_default = 1;
 
132
    default_strategy = sp;
 
133
    return 0;
 
134
}
 
135
 
 
136
const dico_strategy_t
 
137
dico_get_default_strategy()
 
138
{
 
139
    return default_strategy;
 
140
}
 
141
 
 
142