postgresql/contrib/tsearch2/dict_ex.c

71 lines
1.3 KiB
C
Raw Normal View History

/* $PostgreSQL: pgsql/contrib/tsearch2/dict_ex.c,v 1.9 2006/11/20 14:03:30 teodor Exp $ */
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"
#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)
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);
char *utxt = pnstrdup(in, PG_GETARG_INT32(2));
TSLexeme *res = palloc(sizeof(TSLexeme) * 2);
char *txt = lowerstr(utxt);
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
res[0].lexeme = txt;
2003-07-21 18:27:44 +08:00
PG_RETURN_POINTER(res);
}