Add support for user-defined I/O conversion casts.
[PostgreSQL.git] / src / backend / tsearch / dict_simple.c
blob10bf619dc705a02953ed77f6c2fef1330acaa5aa
1 /*-------------------------------------------------------------------------
3 * dict_simple.c
4 * Simple dictionary: just lowercase and check for stopword
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
9 * IDENTIFICATION
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
14 #include "postgres.h"
16 #include "commands/defrem.h"
17 #include "tsearch/ts_locale.h"
18 #include "tsearch/ts_public.h"
19 #include "tsearch/ts_utils.h"
20 #include "utils/builtins.h"
23 typedef struct
25 StopList stoplist;
26 bool accept;
27 } DictSimple;
30 Datum
31 dsimple_init(PG_FUNCTION_ARGS)
33 List *dictoptions = (List *) PG_GETARG_POINTER(0);
34 DictSimple *d = (DictSimple *) palloc0(sizeof(DictSimple));
35 bool stoploaded = false,
36 acceptloaded = false;
37 ListCell *l;
39 d->accept = true; /* default */
41 foreach(l, dictoptions)
43 DefElem *defel = (DefElem *) lfirst(l);
45 if (pg_strcasecmp("StopWords", defel->defname) == 0)
47 if (stoploaded)
48 ereport(ERROR,
49 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
50 errmsg("multiple StopWords parameters")));
51 readstoplist(defGetString(defel), &d->stoplist, lowerstr);
52 stoploaded = true;
54 else if (pg_strcasecmp("Accept", defel->defname) == 0)
56 if (acceptloaded)
57 ereport(ERROR,
58 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
59 errmsg("multiple Accept parameters")));
60 d->accept = defGetBoolean(defel);
61 acceptloaded = true;
63 else
65 ereport(ERROR,
66 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
67 errmsg("unrecognized simple dictionary parameter: \"%s\"",
68 defel->defname)));
72 PG_RETURN_POINTER(d);
75 Datum
76 dsimple_lexize(PG_FUNCTION_ARGS)
78 DictSimple *d = (DictSimple *) PG_GETARG_POINTER(0);
79 char *in = (char *) PG_GETARG_POINTER(1);
80 int32 len = PG_GETARG_INT32(2);
81 char *txt;
82 TSLexeme *res;
84 txt = lowerstr_with_len(in, len);
86 if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
88 /* reject as stopword */
89 pfree(txt);
90 res = palloc0(sizeof(TSLexeme) * 2);
91 PG_RETURN_POINTER(res);
93 else if (d->accept)
95 /* accept */
96 res = palloc0(sizeof(TSLexeme) * 2);
97 res[0].lexeme = txt;
98 PG_RETURN_POINTER(res);
100 else
102 /* report as unrecognized */
103 pfree(txt);
104 PG_RETURN_POINTER(NULL);