~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/tsearch2/stopword.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * stopword library
 
3
 * Teodor Sigaev <teodor@sigaev.ru>
 
4
 */
 
5
#include "postgres.h"
 
6
 
 
7
#include <ctype.h>
 
8
 
 
9
#include "miscadmin.h"
 
10
 
 
11
#include "common.h"
 
12
#include "dict.h"
 
13
 
 
14
#define STOPBUFLEN      4096
 
15
 
 
16
char *
 
17
lowerstr(char *str)
 
18
{
 
19
        char       *ptr = str;
 
20
 
 
21
        while (*ptr)
 
22
        {
 
23
                *ptr = tolower(*(unsigned char *) ptr);
 
24
                ptr++;
 
25
        }
 
26
        return str;
 
27
}
 
28
 
 
29
void
 
30
freestoplist(StopList * s)
 
31
{
 
32
        char      **ptr = s->stop;
 
33
 
 
34
        if (ptr)
 
35
                while (*ptr && s->len > 0)
 
36
                {
 
37
                        free(*ptr);
 
38
                        ptr++;
 
39
                        s->len--;
 
40
                        free(s->stop);
 
41
                }
 
42
        memset(s, 0, sizeof(StopList));
 
43
}
 
44
 
 
45
void
 
46
readstoplist(text *in, StopList * s)
 
47
{
 
48
        char      **stop = NULL;
 
49
 
 
50
        s->len = 0;
 
51
        if (in && VARSIZE(in) - VARHDRSZ > 0)
 
52
        {
 
53
                char       *filename = text2char(in);
 
54
                FILE       *hin;
 
55
                char            buf[STOPBUFLEN];
 
56
                int                     reallen = 0;
 
57
 
 
58
                /* if path is relative, take it as relative to share dir */
 
59
                if (!is_absolute_path(filename))
 
60
                {
 
61
                        char    sharepath[MAXPGPATH];
 
62
                        char   *absfn;
 
63
 
 
64
                        get_share_path(my_exec_path, sharepath);
 
65
                        absfn = palloc(strlen(sharepath) + strlen(filename) + 2);
 
66
                        sprintf(absfn, "%s/%s", sharepath, filename);
 
67
                        pfree(filename);
 
68
                        filename = absfn;
 
69
                }
 
70
 
 
71
                if ((hin = fopen(filename, "r")) == NULL)
 
72
                        ereport(ERROR,
 
73
                                        (errcode(ERRCODE_CONFIG_FILE_ERROR),
 
74
                                         errmsg("could not open file \"%s\": %m",
 
75
                                                        filename)));
 
76
 
 
77
                while (fgets(buf, STOPBUFLEN, hin))
 
78
                {
 
79
                        buf[strlen(buf) - 1] = '\0';
 
80
                        if (*buf == '\0')
 
81
                                continue;
 
82
 
 
83
                        if (s->len >= reallen)
 
84
                        {
 
85
                                char      **tmp;
 
86
 
 
87
                                reallen = (reallen) ? reallen * 2 : 16;
 
88
                                tmp = (char **) realloc((void *) stop, sizeof(char *) * reallen);
 
89
                                if (!tmp)
 
90
                                {
 
91
                                        freestoplist(s);
 
92
                                        fclose(hin);
 
93
                                        ereport(ERROR,
 
94
                                                        (errcode(ERRCODE_OUT_OF_MEMORY),
 
95
                                                         errmsg("out of memory")));
 
96
                                }
 
97
                                stop = tmp;
 
98
                        }
 
99
 
 
100
                        stop[s->len] = strdup(buf);
 
101
                        if (!stop[s->len])
 
102
                        {
 
103
                                freestoplist(s);
 
104
                                fclose(hin);
 
105
                                ereport(ERROR,
 
106
                                                (errcode(ERRCODE_OUT_OF_MEMORY),
 
107
                                                 errmsg("out of memory")));
 
108
                        }
 
109
                        if (s->wordop)
 
110
                                stop[s->len] = (s->wordop) (stop[s->len]);
 
111
 
 
112
                        (s->len)++;
 
113
                }
 
114
                fclose(hin);
 
115
                pfree(filename);
 
116
        }
 
117
        s->stop = stop;
 
118
}
 
119
 
 
120
static int
 
121
comparestr(const void *a, const void *b)
 
122
{
 
123
        return strcmp(*(char **) a, *(char **) b);
 
124
}
 
125
 
 
126
void
 
127
sortstoplist(StopList * s)
 
128
{
 
129
        if (s->stop && s->len > 0)
 
130
                qsort(s->stop, s->len, sizeof(char *), comparestr);
 
131
}
 
132
 
 
133
bool
 
134
searchstoplist(StopList * s, char *key)
 
135
{
 
136
        if (s->wordop)
 
137
                key = (*(s->wordop)) (key);
 
138
        return (s->stop && s->len > 0 && bsearch(&key, s->stop, s->len, sizeof(char *), comparestr)) ? true : false;
 
139
}