| 1 | /* |
| 2 | Copyright (C) 2026 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "grn.h" |
| 22 | #include "grn_db.h" |
| 23 | #include "grn_extractor.h" |
| 24 | |
| 25 | grn_rc |
| 26 | grn_extract_data_init(grn_ctx *ctx, grn_extract_data *data) |
| 27 | { |
| 28 | data->table = NULL; |
| 29 | data->index = 0; |
| 30 | data->value = NULL; |
| 31 | return GRN_SUCCESS; |
| 32 | } |
| 33 | |
| 34 | grn_obj * |
| 35 | grn_extract_data_get_value(grn_ctx *ctx, grn_extract_data *data) |
| 36 | { |
| 37 | return data->value; |
| 38 | } |
| 39 | |
| 40 | grn_obj * |
| 41 | grn_extract_data_get_table(grn_ctx *ctx, grn_extract_data *data) |
| 42 | { |
| 43 | return data->table; |
| 44 | } |
| 45 | |
| 46 | uint32_t |
| 47 | grn_extract_data_get_index(grn_ctx *ctx, grn_extract_data *data) |
| 48 | { |
| 49 | return data->index; |
| 50 | } |
| 51 | |
| 52 | grn_obj * |
| 53 | grn_extractor_create(grn_ctx *ctx, const char *name, int length) |
| 54 | { |
| 55 | GRN_API_ENTER; |
| 56 | grn_obj *extractor = grn_proc_create(ctx, |
| 57 | name, |
| 58 | length, |
| 59 | GRN_PROC_EXTRACTOR, |
| 60 | NULL, |
| 61 | NULL, |
| 62 | NULL, |
| 63 | 0, |
| 64 | NULL); |
| 65 | if (!extractor) { |
| 66 | if (length < 0) { |
| 67 | length = (int)strlen(name); |
| 68 | } |
| 69 | GRN_PLUGIN_ERROR(ctx, |
| 70 | GRN_EXTRACTOR_ERROR, |
| 71 | "[extractor][create] failed to create: <%.*s>", |
| 72 | length, |
| 73 | name); |
| 74 | } |
| 75 | |
| 76 | GRN_API_RETURN(extractor); |
| 77 | } |
| 78 | |
| 79 | grn_rc |
| 80 | grn_extractor_set_extract_func(grn_ctx *ctx, |
| 81 | grn_obj *extractor, |
| 82 | grn_extractor_extract_func *extract) |
| 83 | { |
| 84 | GRN_API_ENTER; |
| 85 | if (extractor) { |
| 86 | ((grn_proc *)extractor)->callbacks.extractor.extract = extract; |
| 87 | } else { |
| 88 | GRN_PLUGIN_ERROR(ctx, |
| 89 | GRN_INVALID_ARGUMENT, |
| 90 | "[extractor][extract][set] extractor is NULL"); |
| 91 | } |
| 92 | GRN_API_RETURN(ctx->rc); |
| 93 | } |
| 94 | |
| 95 | grn_obj * |
| 96 | grn_extractor_extract(grn_ctx *ctx, grn_obj *extractor, grn_extract_data *data) |
| 97 | { |
| 98 | GRN_API_ENTER; |
| 99 | grn_obj *value = |
| 100 | ((grn_proc *)extractor)->callbacks.extractor.extract(ctx, extractor, data); |
| 101 | GRN_API_RETURN(value); |
| 102 | } |