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