~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to src/backend/tsearch/dict_ispell.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * dict_ispell.c
 
4
 *              Ispell dictionary interface
 
5
 *
 
6
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 
7
 *
 
8
 *
 
9
 * IDENTIFICATION
 
10
 *        src/backend/tsearch/dict_ispell.c
 
11
 *
 
12
 *-------------------------------------------------------------------------
 
13
 */
 
14
#include "postgres.h"
 
15
 
 
16
#include "commands/defrem.h"
 
17
#include "tsearch/dicts/spell.h"
 
18
#include "tsearch/ts_locale.h"
 
19
#include "tsearch/ts_public.h"
 
20
#include "tsearch/ts_utils.h"
 
21
#include "utils/builtins.h"
 
22
 
 
23
 
 
24
typedef struct
 
25
{
 
26
        StopList        stoplist;
 
27
        IspellDict      obj;
 
28
} DictISpell;
 
29
 
 
30
Datum
 
31
dispell_init(PG_FUNCTION_ARGS)
 
32
{
 
33
        List       *dictoptions = (List *) PG_GETARG_POINTER(0);
 
34
        DictISpell *d;
 
35
        bool            affloaded = false,
 
36
                                dictloaded = false,
 
37
                                stoploaded = false;
 
38
        ListCell   *l;
 
39
 
 
40
        d = (DictISpell *) palloc0(sizeof(DictISpell));
 
41
 
 
42
        NIStartBuild(&(d->obj));
 
43
 
 
44
        foreach(l, dictoptions)
 
45
        {
 
46
                DefElem    *defel = (DefElem *) lfirst(l);
 
47
 
 
48
                if (pg_strcasecmp(defel->defname, "DictFile") == 0)
 
49
                {
 
50
                        if (dictloaded)
 
51
                                ereport(ERROR,
 
52
                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
53
                                                 errmsg("multiple DictFile parameters")));
 
54
                        NIImportDictionary(&(d->obj),
 
55
                                                         get_tsearch_config_filename(defGetString(defel),
 
56
                                                                                                                 "dict"));
 
57
                        dictloaded = true;
 
58
                }
 
59
                else if (pg_strcasecmp(defel->defname, "AffFile") == 0)
 
60
                {
 
61
                        if (affloaded)
 
62
                                ereport(ERROR,
 
63
                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
64
                                                 errmsg("multiple AffFile parameters")));
 
65
                        NIImportAffixes(&(d->obj),
 
66
                                                        get_tsearch_config_filename(defGetString(defel),
 
67
                                                                                                                "affix"));
 
68
                        affloaded = true;
 
69
                }
 
70
                else if (pg_strcasecmp(defel->defname, "StopWords") == 0)
 
71
                {
 
72
                        if (stoploaded)
 
73
                                ereport(ERROR,
 
74
                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
75
                                                 errmsg("multiple StopWords parameters")));
 
76
                        readstoplist(defGetString(defel), &(d->stoplist), lowerstr);
 
77
                        stoploaded = true;
 
78
                }
 
79
                else
 
80
                {
 
81
                        ereport(ERROR,
 
82
                                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
83
                                         errmsg("unrecognized Ispell parameter: \"%s\"",
 
84
                                                        defel->defname)));
 
85
                }
 
86
        }
 
87
 
 
88
        if (affloaded && dictloaded)
 
89
        {
 
90
                NISortDictionary(&(d->obj));
 
91
                NISortAffixes(&(d->obj));
 
92
        }
 
93
        else if (!affloaded)
 
94
        {
 
95
                ereport(ERROR,
 
96
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
97
                                 errmsg("missing AffFile parameter")));
 
98
        }
 
99
        else
 
100
        {
 
101
                ereport(ERROR,
 
102
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
103
                                 errmsg("missing DictFile parameter")));
 
104
        }
 
105
 
 
106
        NIFinishBuild(&(d->obj));
 
107
 
 
108
        PG_RETURN_POINTER(d);
 
109
}
 
110
 
 
111
Datum
 
112
dispell_lexize(PG_FUNCTION_ARGS)
 
113
{
 
114
        DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0);
 
115
        char       *in = (char *) PG_GETARG_POINTER(1);
 
116
        int32           len = PG_GETARG_INT32(2);
 
117
        char       *txt;
 
118
        TSLexeme   *res;
 
119
        TSLexeme   *ptr,
 
120
                           *cptr;
 
121
 
 
122
        if (len <= 0)
 
123
                PG_RETURN_POINTER(NULL);
 
124
 
 
125
        txt = lowerstr_with_len(in, len);
 
126
        res = NINormalizeWord(&(d->obj), txt);
 
127
 
 
128
        if (res == NULL)
 
129
                PG_RETURN_POINTER(NULL);
 
130
 
 
131
        ptr = cptr = res;
 
132
        while (ptr->lexeme)
 
133
        {
 
134
                if (searchstoplist(&(d->stoplist), ptr->lexeme))
 
135
                {
 
136
                        pfree(ptr->lexeme);
 
137
                        ptr->lexeme = NULL;
 
138
                        ptr++;
 
139
                }
 
140
                else
 
141
                {
 
142
                        memcpy(cptr, ptr, sizeof(TSLexeme));
 
143
                        cptr++;
 
144
                        ptr++;
 
145
                }
 
146
        }
 
147
        cptr->lexeme = NULL;
 
148
 
 
149
        PG_RETURN_POINTER(res);
 
150
}