| 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 | #ifdef GRN_EMBEDDED |
| 20 | # define GRN_PLUGIN_FUNCTION_TAG functions_binary |
| 21 | #endif |
| 22 | |
| 23 | #include <groonga/plugin.h> |
| 24 | |
| 25 | static grn_obj * |
| 26 | func_binary_length(grn_ctx *ctx, |
| 27 | int n_args, |
| 28 | grn_obj **args, |
| 29 | grn_user_data *user_data) |
| 30 | { |
| 31 | if (n_args != 1) { |
| 32 | GRN_PLUGIN_ERROR(ctx, |
| 33 | GRN_INVALID_ARGUMENT, |
| 34 | "binary_length(): wrong number of arguments (%d for 1)", |
| 35 | n_args); |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | grn_obj *target = args[0]; |
| 40 | if (target->header.type != GRN_BULK) { |
| 41 | grn_obj inspected; |
| 42 | |
| 43 | GRN_TEXT_INIT(&inspected, 0); |
| 44 | grn_inspect(ctx, &inspected, target); |
| 45 | GRN_PLUGIN_ERROR(ctx, |
| 46 | GRN_INVALID_ARGUMENT, |
| 47 | "binary_length(): target object must be a bulk: " |
| 48 | "<%.*s>", |
| 49 | (int)GRN_TEXT_LEN(&inspected), |
| 50 | GRN_TEXT_VALUE(&inspected)); |
| 51 | GRN_OBJ_FIN(ctx, &inspected); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | size_t length = GRN_BULK_VSIZE(target); |
| 56 | grn_obj *grn_length = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_UINT32, 0); |
| 57 | if (!grn_length) { |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | GRN_UINT32_SET(ctx, grn_length, length); |
| 62 | |
| 63 | return grn_length; |
| 64 | } |
| 65 | |
| 66 | grn_rc |
| 67 | GRN_PLUGIN_INIT(grn_ctx *ctx) |
| 68 | { |
| 69 | return ctx->rc; |
| 70 | } |
| 71 | |
| 72 | grn_rc |
| 73 | GRN_PLUGIN_REGISTER(grn_ctx *ctx) |
| 74 | { |
| 75 | grn_rc rc = GRN_SUCCESS; |
| 76 | |
| 77 | grn_proc_create(ctx, |
| 78 | "binary_length", |
| 79 | -1, |
| 80 | GRN_PROC_FUNCTION, |
| 81 | func_binary_length, |
| 82 | NULL, |
| 83 | NULL, |
| 84 | 0, |
| 85 | NULL); |
| 86 | |
| 87 | return rc; |
| 88 | } |
| 89 | |
| 90 | grn_rc |
| 91 | GRN_PLUGIN_FIN(grn_ctx *ctx) |
| 92 | { |
| 93 | return GRN_SUCCESS; |
| 94 | } |