| 1 | /* |
| 2 | Copyright (C) 2014-2015 Brazil |
| 3 | Copyright (C) 2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "grn.h" |
| 23 | #include "grn_db.h" |
| 24 | #include <groonga/token_filter.h> |
| 25 | |
| 26 | grn_rc |
| 27 | grn_token_filter_register(grn_ctx *ctx, |
| 28 | const char *plugin_name_ptr, |
| 29 | int plugin_name_length, |
| 30 | grn_token_filter_init_func *init, |
| 31 | grn_token_filter_filter_func *filter, |
| 32 | grn_token_filter_fin_func *fin) |
| 33 | { |
| 34 | if (plugin_name_length == -1) { |
| 35 | plugin_name_length = (int)strlen(plugin_name_ptr); |
| 36 | } |
| 37 | |
| 38 | { |
| 39 | grn_obj *token_filter_object = grn_proc_create(ctx, |
| 40 | plugin_name_ptr, |
| 41 | plugin_name_length, |
| 42 | GRN_PROC_TOKEN_FILTER, |
| 43 | NULL, NULL, NULL, 0, NULL); |
| 44 | if (token_filter_object == NULL) { |
| 45 | GRN_PLUGIN_ERROR(ctx, GRN_TOKEN_FILTER_ERROR, |
| 46 | "[token-filter][%.*s] failed to grn_proc_create()", |
| 47 | plugin_name_length, plugin_name_ptr); |
| 48 | return ctx->rc; |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | grn_proc *token_filter = (grn_proc *)token_filter_object; |
| 53 | token_filter->callbacks.token_filter.init = init; |
| 54 | token_filter->callbacks.token_filter.filter = filter; |
| 55 | token_filter->callbacks.token_filter.fin = fin; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return GRN_SUCCESS; |
| 60 | } |
| 61 | |
| 62 | grn_obj * |
| 63 | grn_token_filter_create(grn_ctx *ctx, const char *name, int length) |
| 64 | { |
| 65 | grn_obj *token_filter; |
| 66 | |
| 67 | GRN_API_ENTER; |
| 68 | token_filter = grn_proc_create(ctx, |
| 69 | name, |
| 70 | length, |
| 71 | GRN_PROC_TOKEN_FILTER, |
| 72 | NULL, |
| 73 | NULL, |
| 74 | NULL, |
| 75 | 0, |
| 76 | NULL); |
| 77 | if (!token_filter) { |
| 78 | if (length < 0) { |
| 79 | length = (int)strlen(name); |
| 80 | } |
| 81 | GRN_PLUGIN_ERROR(ctx, |
| 82 | GRN_TOKEN_FILTER_ERROR, |
| 83 | "[token-filter][create] failed to create: <%.*s>", |
| 84 | length, name); |
| 85 | } |
| 86 | |
| 87 | GRN_API_RETURN(token_filter); |
| 88 | } |
| 89 | |
| 90 | grn_rc |
| 91 | grn_token_filter_set_init_func(grn_ctx *ctx, |
| 92 | grn_obj *token_filter, |
| 93 | grn_token_filter_init_query_func *init) |
| 94 | { |
| 95 | GRN_API_ENTER; |
| 96 | if (token_filter) { |
| 97 | ((grn_proc *)token_filter)->callbacks.token_filter.init_query = init; |
| 98 | } else { |
| 99 | GRN_PLUGIN_ERROR(ctx, |
| 100 | GRN_INVALID_ARGUMENT, |
| 101 | "[token-filter][init][set] token filter is NULL"); |
| 102 | } |
| 103 | GRN_API_RETURN(ctx->rc); |
| 104 | } |
| 105 | |
| 106 | grn_rc |
| 107 | grn_token_filter_set_filter_func(grn_ctx *ctx, |
| 108 | grn_obj *token_filter, |
| 109 | grn_token_filter_filter_func *filter) |
| 110 | { |
| 111 | GRN_API_ENTER; |
| 112 | if (token_filter) { |
| 113 | ((grn_proc *)token_filter)->callbacks.token_filter.filter = filter; |
| 114 | } else { |
| 115 | GRN_PLUGIN_ERROR(ctx, |
| 116 | GRN_INVALID_ARGUMENT, |
| 117 | "[token-filter][filter][set] token filter is NULL"); |
| 118 | } |
| 119 | GRN_API_RETURN(ctx->rc); |
| 120 | } |
| 121 | |
| 122 | grn_rc |
| 123 | grn_token_filter_set_fin_func(grn_ctx *ctx, |
| 124 | grn_obj *token_filter, |
| 125 | grn_token_filter_fin_func *fin) |
| 126 | { |
| 127 | GRN_API_ENTER; |
| 128 | if (token_filter) { |
| 129 | ((grn_proc *)token_filter)->callbacks.token_filter.fin = fin; |
| 130 | } else { |
| 131 | GRN_PLUGIN_ERROR(ctx, |
| 132 | GRN_INVALID_ARGUMENT, |
| 133 | "[token-filter][fin][set] token filter is NULL"); |
| 134 | } |
| 135 | GRN_API_RETURN(ctx->rc); |
| 136 | } |