~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2005 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* Written by Sergei A. Golubchik, who has a shared copyright to this code */
17
18
#include "ftdefs.h"
19
#include "my_handler.h"
20
21
typedef struct st_ft_stopwords
22
{
23
  const char * pos;
24
  uint   len;
25
} FT_STOPWORD;
26
27
static TREE *stopwords3=NULL;
28
29
static int FT_STOPWORD_cmp(void* cmp_arg __attribute__((unused)),
30
			   FT_STOPWORD *w1, FT_STOPWORD *w2)
31
{
32
  return ha_compare_text(default_charset_info,
33
			 (uchar *)w1->pos,w1->len,
34
			 (uchar *)w2->pos,w2->len,0,0);
35
}
36
37
static void FT_STOPWORD_free(FT_STOPWORD *w, TREE_FREE action,
38
                             void *arg __attribute__((unused)))
39
{
40
  if (action == free_free)
41
    my_free((uchar*) w->pos, MYF(0));
42
}
43
44
static int ft_add_stopword(const char *w)
45
{
46
  FT_STOPWORD sw;
47
  return !w ||
48
         (((sw.len= (uint) strlen(sw.pos=w)) >= ft_min_word_len) &&
49
          (tree_insert(stopwords3, &sw, 0, stopwords3->custom_arg)==NULL));
50
}
51
52
int ft_init_stopwords()
53
{
54
  if (!stopwords3)
55
  {
56
    if (!(stopwords3=(TREE *)my_malloc(sizeof(TREE),MYF(0))))
57
      return -1;
58
    init_tree(stopwords3,0,0,sizeof(FT_STOPWORD),(qsort_cmp2)&FT_STOPWORD_cmp,
59
              0,
60
              (ft_stopword_file ? (tree_element_free)&FT_STOPWORD_free : 0),
61
              NULL);
62
  }
63
64
  if (ft_stopword_file)
65
  {
66
    File fd;
67
    uint len;
68
    uchar *buffer, *start, *end;
69
    FT_WORD w;
70
    int error=-1;
71
72
    if (!*ft_stopword_file)
73
      return 0;
74
75
    if ((fd=my_open(ft_stopword_file, O_RDONLY, MYF(MY_WME))) == -1)
76
      return -1;
77
    len=(uint)my_seek(fd, 0L, MY_SEEK_END, MYF(0));
78
    my_seek(fd, 0L, MY_SEEK_SET, MYF(0));
79
    if (!(start=buffer=my_malloc(len+1, MYF(MY_WME))))
80
      goto err0;
81
    len=my_read(fd, buffer, len, MYF(MY_WME));
82
    end=start+len;
83
    while (ft_simple_get_word(default_charset_info, &start, end, &w, TRUE))
84
    {
85
      if (ft_add_stopword(my_strndup((char*) w.pos, w.len, MYF(0))))
86
        goto err1;
87
    }
88
    error=0;
89
err1:
90
    my_free(buffer, MYF(0));
91
err0:
92
    my_close(fd, MYF(MY_WME));
93
    return error;
94
  }
95
  else
96
  {
97
    /* compatibility mode: to be removed */
98
    char **sws=(char **)ft_precompiled_stopwords;
99
100
    for (;*sws;sws++)
101
    {
102
      if (ft_add_stopword(*sws))
103
        return -1;
104
    }
105
    ft_stopword_file="(built-in)"; /* for SHOW VARIABLES */
106
  }
107
  return 0;
108
}
109
110
int is_stopword(char *word, uint len)
111
{
112
  FT_STOPWORD sw;
113
  sw.pos=word;
114
  sw.len=len;
115
  return tree_search(stopwords3,&sw, stopwords3->custom_arg) != NULL;
116
}
117
118
119
void ft_free_stopwords()
120
{
121
  if (stopwords3)
122
  {
123
    delete_tree(stopwords3); /* purecov: inspected */
124
    my_free((char*) stopwords3,MYF(0));
125
    stopwords3=0;
126
  }
127
  ft_stopword_file= 0;
128
}