| 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 "../grn_proc.h" |
| 20 | #include "../grn_ctx.h" |
| 21 | #include "../grn_extractor.h" |
| 22 | |
| 23 | #include <groonga/plugin.h> |
| 24 | |
| 25 | static grn_obj * |
| 26 | command_extract(grn_ctx *ctx, |
| 27 | int nargs, |
| 28 | grn_obj **args, |
| 29 | grn_user_data *user_data) |
| 30 | { |
| 31 | const char *context_tag = "[extract]"; |
| 32 | |
| 33 | grn_raw_string extractors_raw; |
| 34 | extractors_raw.value = |
| 35 | grn_plugin_proc_get_var_string(ctx, |
| 36 | user_data, |
| 37 | "extractors", |
| 38 | -1, |
| 39 | &(extractors_raw.length)); |
| 40 | grn_obj *value = grn_plugin_proc_get_var(ctx, user_data, "value", -1); |
| 41 | |
| 42 | if (extractors_raw.length == 0) { |
| 43 | GRN_PLUGIN_ERROR(ctx, |
| 44 | GRN_INVALID_ARGUMENT, |
| 45 | "%s extractor names are missing", |
| 46 | context_tag); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | grn_obj *lexicon = grn_proc_lexicon_open(ctx, |
| 52 | NULL, |
| 53 | NULL, |
| 54 | NULL, |
| 55 | &extractors_raw, |
| 56 | context_tag); |
| 57 | if (!lexicon) { |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | int n_elements = 1; |
| 62 | grn_ctx_output_map_open(ctx, "RESULT", n_elements); |
| 63 | { |
| 64 | grn_obj *extracted_value = grn_table_extract(ctx, lexicon, value); |
| 65 | grn_ctx_output_cstr(ctx, "extracted"); |
| 66 | if (extracted_value) { |
| 67 | grn_ctx_output_obj(ctx, extracted_value, NULL); |
| 68 | if (extracted_value != value) { |
| 69 | GRN_OBJ_FIN(ctx, extracted_value); |
| 70 | } |
| 71 | } else { |
| 72 | grn_ctx_output_null(ctx); |
| 73 | } |
| 74 | } |
| 75 | grn_ctx_output_map_close(ctx); |
| 76 | |
| 77 | grn_obj_unlink(ctx, lexicon); |
| 78 | } |
| 79 | |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | grn_proc_init_extract(grn_ctx *ctx) |
| 85 | { |
| 86 | grn_expr_var vars[2]; |
| 87 | grn_plugin_expr_var_init(ctx, &(vars[0]), "extractors", -1); |
| 88 | grn_plugin_expr_var_init(ctx, &(vars[1]), "value", -1); |
| 89 | grn_plugin_command_create(ctx, "extract", -1, command_extract, 2, vars); |
| 90 | } |