You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postgres/contrib/tsearch2/stopword.c

127 lines
2.1 KiB

23 years ago
/*
23 years ago
* stopword library
* Teodor Sigaev <teodor@sigaev.ru>
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "postgres.h"
#include "common.h"
#include "dict.h"
#define STOPBUFLEN 4096
23 years ago
char *
lowerstr(char *str)
{
char *ptr = str;
while (*ptr)
{
*ptr = tolower(*(unsigned char *) ptr);
23 years ago
ptr++;
}
return str;
}
void
23 years ago
freestoplist(StopList * s)
{
char **ptr = s->stop;
if (ptr)
while (*ptr && s->len > 0)
{
23 years ago
free(*ptr);
23 years ago
ptr++;
s->len--;
free(s->stop);
}
memset(s, 0, sizeof(StopList));
23 years ago
}
void
23 years ago
readstoplist(text *in, StopList * s)
{
char **stop = NULL;
s->len = 0;
if (in && VARSIZE(in) - VARHDRSZ > 0)
{
char *filename = text2char(in);
FILE *hin = NULL;
char buf[STOPBUFLEN];
int reallen = 0;
if ((hin = fopen(filename, "r")) == NULL)
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("could not open file \"%s\": %m",
23 years ago
filename)));
23 years ago
while (fgets(buf, STOPBUFLEN, hin))
{
buf[strlen(buf) - 1] = '\0';
if (*buf == '\0')
continue;
23 years ago
23 years ago
if (s->len >= reallen)
{
char **tmp;
reallen = (reallen) ? reallen * 2 : 16;
tmp = (char **) realloc((void *) stop, sizeof(char *) * reallen);
if (!tmp)
{
23 years ago
freestoplist(s);
23 years ago
fclose(hin);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
23 years ago
}
23 years ago
stop = tmp;
23 years ago
}
23 years ago
stop[s->len] = strdup(buf);
if (!stop[s->len])
{
23 years ago
freestoplist(s);
23 years ago
fclose(hin);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
23 years ago
}
23 years ago
if (s->wordop)
stop[s->len] = (s->wordop) (stop[s->len]);
23 years ago
23 years ago
(s->len)++;
23 years ago
}
fclose(hin);
23 years ago
pfree(filename);
23 years ago
}
23 years ago
s->stop = stop;
}
23 years ago
static int
23 years ago
comparestr(const void *a, const void *b)
{
return strcmp(*(char **) a, *(char **) b);
23 years ago
}
void
23 years ago
sortstoplist(StopList * s)
{
if (s->stop && s->len > 0)
qsort(s->stop, s->len, sizeof(char *), comparestr);
23 years ago
}
bool
23 years ago
searchstoplist(StopList * s, char *key)
{
if (s->wordop)
key = (*(s->wordop)) (key);
return (s->stop && s->len > 0 && bsearch(&key, s->stop, s->len, sizeof(char *), comparestr)) ? true : false;
23 years ago
}