postgresql/contrib/test_parser/test_parser.c

139 lines
2.8 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* test_parser.c
* Simple example of a text search parser
*
2008-01-02 04:31:21 +08:00
* Copyright (c) 2007-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
2008-01-02 04:31:21 +08:00
* $PostgreSQL: pgsql/contrib/test_parser/test_parser.c,v 1.4 2008/01/01 20:31:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
PG_MODULE_MAGIC;
/*
* types
*/
/* self-defined type */
2007-11-16 05:14:46 +08:00
typedef struct
{
char *buffer; /* text to parse */
int len; /* length of the text in buffer */
int pos; /* position of the parser */
} ParserState;
/* copy-paste from wparser.h of tsearch2 */
2007-11-16 05:14:46 +08:00
typedef struct
{
int lexid;
char *alias;
char *descr;
} LexDescr;
/*
* prototypes
*/
PG_FUNCTION_INFO_V1(testprs_start);
2007-11-16 05:14:46 +08:00
Datum testprs_start(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(testprs_getlexeme);
2007-11-16 05:14:46 +08:00
Datum testprs_getlexeme(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(testprs_end);
2007-11-16 05:14:46 +08:00
Datum testprs_end(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(testprs_lextype);
2007-11-16 05:14:46 +08:00
Datum testprs_lextype(PG_FUNCTION_ARGS);
/*
* functions
*/
2007-11-16 05:14:46 +08:00
Datum
testprs_start(PG_FUNCTION_ARGS)
{
ParserState *pst = (ParserState *) palloc0(sizeof(ParserState));
2007-11-16 05:14:46 +08:00
pst->buffer = (char *) PG_GETARG_POINTER(0);
pst->len = PG_GETARG_INT32(1);
pst->pos = 0;
PG_RETURN_POINTER(pst);
}
2007-11-16 05:14:46 +08:00
Datum
testprs_getlexeme(PG_FUNCTION_ARGS)
{
2007-11-16 05:14:46 +08:00
ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
char **t = (char **) PG_GETARG_POINTER(1);
int *tlen = (int *) PG_GETARG_POINTER(2);
int type;
*tlen = pst->pos;
2007-11-16 05:14:46 +08:00
*t = pst->buffer + pst->pos;
if ((pst->buffer)[pst->pos] == ' ')
{
/* blank type */
type = 12;
/* go to the next non-white-space character */
while ((pst->buffer)[pst->pos] == ' ' &&
pst->pos < pst->len)
(pst->pos)++;
2007-11-16 05:14:46 +08:00
}
else
{
/* word type */
type = 3;
/* go to the next white-space character */
while ((pst->buffer)[pst->pos] != ' ' &&
pst->pos < pst->len)
(pst->pos)++;
}
*tlen = pst->pos - *tlen;
/* we are finished if (*tlen == 0) */
if (*tlen == 0)
2007-11-16 05:14:46 +08:00
type = 0;
PG_RETURN_INT32(type);
}
2007-11-16 05:14:46 +08:00
Datum
testprs_end(PG_FUNCTION_ARGS)
{
ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
2007-11-16 05:14:46 +08:00
pfree(pst);
PG_RETURN_VOID();
}
2007-11-16 05:14:46 +08:00
Datum
testprs_lextype(PG_FUNCTION_ARGS)
{
/*
2007-11-16 05:14:46 +08:00
* Remarks: - we have to return the blanks for headline reason - we use
* the same lexids like Teodor in the default word parser; in this way we
* can reuse the headline function of the default word parser.
*/
2007-11-16 05:14:46 +08:00
LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (2 + 1));
/* there are only two types in this parser */
descr[0].lexid = 3;
descr[0].alias = pstrdup("word");
descr[0].descr = pstrdup("Word");
descr[1].lexid = 12;
descr[1].alias = pstrdup("blank");
descr[1].descr = pstrdup("Space symbols");
descr[2].lexid = 0;
PG_RETURN_POINTER(descr);
}