~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/tsearch2/dict_ispell.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ISpell interface
 
3
 * Teodor Sigaev <teodor@sigaev.ru>
 
4
 */
 
5
#include <stdlib.h>
 
6
#include <string.h>
 
7
#include <ctype.h>
 
8
 
 
9
#include "postgres.h"
 
10
 
 
11
#include "dict.h"
 
12
#include "common.h"
 
13
#include "ispell/spell.h"
 
14
 
 
15
typedef struct
 
16
{
 
17
        StopList        stoplist;
 
18
        IspellDict      obj;
 
19
}       DictISpell;
 
20
 
 
21
PG_FUNCTION_INFO_V1(spell_init);
 
22
Datum           spell_init(PG_FUNCTION_ARGS);
 
23
 
 
24
PG_FUNCTION_INFO_V1(spell_lexize);
 
25
Datum           spell_lexize(PG_FUNCTION_ARGS);
 
26
 
 
27
static void
 
28
freeDictISpell(DictISpell * d)
 
29
{
 
30
        NIFree(&(d->obj));
 
31
        freestoplist(&(d->stoplist));
 
32
        free(d);
 
33
}
 
34
 
 
35
Datum
 
36
spell_init(PG_FUNCTION_ARGS)
 
37
{
 
38
        DictISpell *d;
 
39
        Map                *cfg,
 
40
                           *pcfg;
 
41
        text       *in;
 
42
        bool            affloaded = false,
 
43
                                dictloaded = false,
 
44
                                stoploaded = false;
 
45
 
 
46
        if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
 
47
                ereport(ERROR,
 
48
                                (errcode(ERRCODE_CONFIG_FILE_ERROR),
 
49
                                 errmsg("ISpell confguration error")));
 
50
 
 
51
        d = (DictISpell *) malloc(sizeof(DictISpell));
 
52
        if (!d)
 
53
                ereport(ERROR,
 
54
                                (errcode(ERRCODE_OUT_OF_MEMORY),
 
55
                                 errmsg("out of memory")));
 
56
        memset(d, 0, sizeof(DictISpell));
 
57
        d->stoplist.wordop = lowerstr;
 
58
 
 
59
        in = PG_GETARG_TEXT_P(0);
 
60
        parse_cfgdict(in, &cfg);
 
61
        PG_FREE_IF_COPY(in, 0);
 
62
        pcfg = cfg;
 
63
        while (pcfg->key)
 
64
        {
 
65
                if (pg_strcasecmp("DictFile", pcfg->key) == 0)
 
66
                {
 
67
                        if (dictloaded)
 
68
                        {
 
69
                                freeDictISpell(d);
 
70
                                ereport(ERROR,
 
71
                                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
 
72
                                           errmsg("dictionary already loaded")));
 
73
                        }
 
74
                        if (NIImportDictionary(&(d->obj), pcfg->value))
 
75
                        {
 
76
                                freeDictISpell(d);
 
77
                                ereport(ERROR,
 
78
                                                (errcode(ERRCODE_CONFIG_FILE_ERROR),
 
79
                                                 errmsg("could not load dictionary file \"%s\"",
 
80
                                                                pcfg->value)));
 
81
                        }
 
82
                        dictloaded = true;
 
83
                }
 
84
                else if (pg_strcasecmp("AffFile", pcfg->key) == 0)
 
85
                {
 
86
                        if (affloaded)
 
87
                        {
 
88
                                freeDictISpell(d);
 
89
                                ereport(ERROR,
 
90
                                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
 
91
                                           errmsg("affixes already loaded")));
 
92
                        }
 
93
                        if (NIImportAffixes(&(d->obj), pcfg->value))
 
94
                        {
 
95
                                freeDictISpell(d);
 
96
                                ereport(ERROR,
 
97
                                                (errcode(ERRCODE_CONFIG_FILE_ERROR),
 
98
                                                 errmsg("could not load affix file \"%s\"",
 
99
                                                                pcfg->value)));
 
100
                        }
 
101
                        affloaded = true;
 
102
                }
 
103
                else if (pg_strcasecmp("StopFile", pcfg->key) == 0)
 
104
                {
 
105
                        text       *tmp = char2text(pcfg->value);
 
106
 
 
107
                        if (stoploaded)
 
108
                        {
 
109
                                freeDictISpell(d);
 
110
                                ereport(ERROR,
 
111
                                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
 
112
                                           errmsg("stop words already loaded")));
 
113
                        }
 
114
                        readstoplist(tmp, &(d->stoplist));
 
115
                        sortstoplist(&(d->stoplist));
 
116
                        pfree(tmp);
 
117
                        stoploaded = true;
 
118
                }
 
119
                else
 
120
                {
 
121
                        freeDictISpell(d);
 
122
                        ereport(ERROR,
 
123
                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
124
                                         errmsg("unrecognized option: %s => %s",
 
125
                                                        pcfg->key, pcfg->value)));
 
126
                }
 
127
                pfree(pcfg->key);
 
128
                pfree(pcfg->value);
 
129
                pcfg++;
 
130
        }
 
131
        pfree(cfg);
 
132
 
 
133
        if (affloaded && dictloaded)
 
134
        {
 
135
                NISortDictionary(&(d->obj));
 
136
                NISortAffixes(&(d->obj));
 
137
        }
 
138
        else if (!affloaded)
 
139
        {
 
140
                freeDictISpell(d);
 
141
                ereport(ERROR,
 
142
                                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
 
143
                                 errmsg("no affixes")));
 
144
        }
 
145
        else
 
146
        {
 
147
                freeDictISpell(d);
 
148
                ereport(ERROR,
 
149
                                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
 
150
                                 errmsg("no dictionary")));
 
151
        }
 
152
 
 
153
        PG_RETURN_POINTER(d);
 
154
}
 
155
 
 
156
Datum
 
157
spell_lexize(PG_FUNCTION_ARGS)
 
158
{
 
159
        DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0);
 
160
        char       *in = (char *) PG_GETARG_POINTER(1);
 
161
        char       *txt;
 
162
        char      **res;
 
163
        char      **ptr,
 
164
                          **cptr;
 
165
 
 
166
        if (!PG_GETARG_INT32(2))
 
167
                PG_RETURN_POINTER(NULL);
 
168
 
 
169
        res = palloc(sizeof(char *) * 2);
 
170
        txt = pnstrdup(in, PG_GETARG_INT32(2));
 
171
        res = NINormalizeWord(&(d->obj), txt);
 
172
        pfree(txt);
 
173
 
 
174
        if (res == NULL)
 
175
                PG_RETURN_POINTER(NULL);
 
176
 
 
177
        ptr = cptr = res;
 
178
        while (*ptr)
 
179
        {
 
180
                if (searchstoplist(&(d->stoplist), *ptr))
 
181
                {
 
182
                        pfree(*ptr);
 
183
                        *ptr = NULL;
 
184
                        ptr++;
 
185
                }
 
186
                else
 
187
                {
 
188
                        *cptr = *ptr;
 
189
                        cptr++;
 
190
                        ptr++;
 
191
                }
 
192
        }
 
193
        *cptr = NULL;
 
194
 
 
195
        PG_RETURN_POINTER(res);
 
196
}