2006-11-20 22:03:30 +08:00
|
|
|
/* $PostgreSQL: pgsql/contrib/tsearch2/dict_ex.c,v 1.9 2006/11/20 14:03:30 teodor Exp $ */
|
2006-03-11 12:38:42 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
/*
|
|
|
|
* example of dictionary
|
2003-07-21 18:27:44 +08:00
|
|
|
* Teodor Sigaev <teodor@sigaev.ru>
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "dict.h"
|
|
|
|
#include "common.h"
|
2005-12-12 19:10:12 +08:00
|
|
|
#include "ts_locale.h"
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2003-07-21 18:27:44 +08:00
|
|
|
StopList stoplist;
|
2003-08-04 08:43:34 +08:00
|
|
|
} DictExample;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(dex_init);
|
2003-08-04 08:43:34 +08:00
|
|
|
Datum dex_init(PG_FUNCTION_ARGS);
|
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
PG_FUNCTION_INFO_V1(dex_lexize);
|
2003-08-04 08:43:34 +08:00
|
|
|
Datum dex_lexize(PG_FUNCTION_ARGS);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
Datum
|
|
|
|
dex_init(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DictExample *d = (DictExample *) malloc(sizeof(DictExample));
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!d)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
2003-08-04 08:43:34 +08:00
|
|
|
memset(d, 0, sizeof(DictExample));
|
|
|
|
|
|
|
|
d->stoplist.wordop = lowerstr;
|
|
|
|
|
|
|
|
if (!PG_ARGISNULL(0) && PG_GETARG_POINTER(0) != NULL)
|
|
|
|
{
|
|
|
|
text *in = PG_GETARG_TEXT_P(0);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
readstoplist(in, &(d->stoplist));
|
|
|
|
sortstoplist(&(d->stoplist));
|
|
|
|
PG_FREE_IF_COPY(in, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2003-08-04 08:43:34 +08:00
|
|
|
dex_lexize(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DictExample *d = (DictExample *) PG_GETARG_POINTER(0);
|
|
|
|
char *in = (char *) PG_GETARG_POINTER(1);
|
2006-11-20 22:03:30 +08:00
|
|
|
char *utxt = pnstrdup(in, PG_GETARG_INT32(2));
|
2005-01-25 23:24:38 +08:00
|
|
|
TSLexeme *res = palloc(sizeof(TSLexeme) * 2);
|
2006-11-20 22:03:30 +08:00
|
|
|
char *txt = lowerstr(utxt);
|
2005-01-25 23:24:38 +08:00
|
|
|
|
2006-11-20 22:03:30 +08:00
|
|
|
pfree(utxt);
|
2005-10-15 10:49:52 +08:00
|
|
|
memset(res, 0, sizeof(TSLexeme) * 2);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
|
|
|
|
{
|
2003-07-21 18:27:44 +08:00
|
|
|
pfree(txt);
|
2003-08-04 08:43:34 +08:00
|
|
|
}
|
|
|
|
else
|
2005-01-25 23:24:38 +08:00
|
|
|
res[0].lexeme = txt;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|