| 1 | /* |
| 2 | Copyright(C) 2015-2016 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 "../grn_ctx_impl.h" |
| 21 | |
| 22 | #ifdef GRN_WITH_MRUBY |
| 23 | #include <mruby.h> |
| 24 | #include <mruby/string.h> |
| 25 | |
| 26 | #include "../grn_mrb.h" |
| 27 | #include "mrb_config.h" |
| 28 | #include "mrb_ctx.h" |
| 29 | |
| 30 | static mrb_value |
| 31 | config_array_reference(mrb_state *mrb, mrb_value self) |
| 32 | { |
| 33 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 34 | char *key; |
| 35 | mrb_int key_size; |
| 36 | const char *value; |
| 37 | uint32_t value_size; |
| 38 | grn_rc rc; |
| 39 | |
| 40 | mrb_get_args(mrb, "s", &key, &key_size); |
| 41 | |
| 42 | rc = grn_config_get(ctx, key, (int32_t)key_size, &value, &value_size); |
| 43 | if (rc != GRN_SUCCESS) { |
| 44 | grn_mrb_ctx_check(mrb); |
| 45 | } |
| 46 | |
| 47 | if (!value) { |
| 48 | return mrb_nil_value(); |
| 49 | } else { |
| 50 | return mrb_str_new(mrb, value, value_size); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static mrb_value |
| 55 | config_array_set(mrb_state *mrb, mrb_value self) |
| 56 | { |
| 57 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 58 | char *key; |
| 59 | mrb_int key_size; |
| 60 | mrb_value mrb_value_; |
| 61 | grn_rc rc; |
| 62 | |
| 63 | mrb_get_args(mrb, "sS", &key, &key_size, &mrb_value_); |
| 64 | |
| 65 | rc = grn_config_set(ctx, |
| 66 | key, (int32_t)key_size, |
| 67 | RSTRING_PTR(mrb_value_), RSTRING_LEN(mrb_value_)); |
| 68 | if (rc != GRN_SUCCESS) { |
| 69 | grn_mrb_ctx_check(mrb); |
| 70 | } |
| 71 | |
| 72 | return mrb_value_; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | grn_mrb_config_init(grn_ctx *ctx) |
| 77 | { |
| 78 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 79 | mrb_state *mrb = data->state; |
| 80 | struct RClass *module; |
| 81 | |
| 82 | module = mrb_define_module_under(mrb, data->module, "Config"); |
| 83 | |
| 84 | mrb_define_singleton_method(mrb, (struct RObject *)module, |
| 85 | "[]", config_array_reference, |
| 86 | MRB_ARGS_REQ(1)); |
| 87 | mrb_define_singleton_method(mrb, (struct RObject *)module, |
| 88 | "[]=", config_array_set, |
| 89 | MRB_ARGS_REQ(2)); |
| 90 | } |
| 91 | #endif |