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.

128 lines
3.3 KiB

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