| 1 | /* |
| 2 | Copyright(C) 2018 Kouhei Sutou <[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_ctx_impl.h" |
| 20 | #include <string.h> |
| 21 | |
| 22 | #ifdef GRN_WITH_MRUBY |
| 23 | #include <mruby.h> |
| 24 | #include <mruby/class.h> |
| 25 | #include <mruby/data.h> |
| 26 | #include <mruby/hash.h> |
| 27 | #include <mruby/array.h> |
| 28 | #include <mruby/string.h> |
| 29 | |
| 30 | #include "mrb_converter.h" |
| 31 | #include "mrb_ctx.h" |
| 32 | #include "mrb_output_columns.h" |
| 33 | |
| 34 | static struct mrb_data_type mrb_grn_output_columns_type = { |
| 35 | "Groonga::OutputColumns", |
| 36 | NULL |
| 37 | }; |
| 38 | |
| 39 | static mrb_value |
| 40 | mrb_grn_output_columns_initialize(mrb_state *mrb, mrb_value self) |
| 41 | { |
| 42 | mrb_value mrb_output_columns_ptr; |
| 43 | |
| 44 | mrb_get_args(mrb, "o", &mrb_output_columns_ptr); |
| 45 | DATA_TYPE(self) = &mrb_grn_output_columns_type; |
| 46 | DATA_PTR(self) = mrb_cptr(mrb_output_columns_ptr); |
| 47 | return self; |
| 48 | } |
| 49 | |
| 50 | static mrb_value |
| 51 | mrb_grn_output_columns_apply(mrb_state *mrb, mrb_value self) |
| 52 | { |
| 53 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 54 | mrb_value *mrb_columns; |
| 55 | mrb_int n_mrb_columns; |
| 56 | grn_obj *output_columns; |
| 57 | grn_obj columns; |
| 58 | mrb_value mrb_exception; |
| 59 | |
| 60 | mrb_get_args(mrb, "a", &mrb_columns, &n_mrb_columns); |
| 61 | |
| 62 | output_columns = DATA_PTR(self); |
| 63 | GRN_PTR_INIT(&columns, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 64 | { |
| 65 | mrb_int i; |
| 66 | for (i = 0; i < n_mrb_columns; i++) { |
| 67 | grn_obj *column = GRN_MRB_DATA_PTR(mrb_columns[i]); |
| 68 | GRN_PTR_PUT(ctx, &columns, column); |
| 69 | } |
| 70 | } |
| 71 | grn_output_columns_apply(ctx, output_columns, &columns); |
| 72 | mrb_exception = grn_mrb_ctx_to_exception(mrb); |
| 73 | GRN_OBJ_FIN(ctx, &columns); |
| 74 | if (!mrb_nil_p(mrb_exception)) { |
| 75 | mrb_exc_raise(mrb, mrb_exception); |
| 76 | } |
| 77 | |
| 78 | return mrb_nil_value(); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | grn_mrb_output_columns_init(grn_ctx *ctx) |
| 83 | { |
| 84 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 85 | mrb_state *mrb = data->state; |
| 86 | struct RClass *module = data->module; |
| 87 | struct RClass *object_class = data->object_class; |
| 88 | struct RClass *klass; |
| 89 | |
| 90 | klass = mrb_define_class_under(mrb, module, "OutputColumns", object_class); |
| 91 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
| 92 | |
| 93 | mrb_define_method(mrb, klass, "initialize", |
| 94 | mrb_grn_output_columns_initialize, |
| 95 | MRB_ARGS_REQ(1)); |
| 96 | |
| 97 | mrb_define_method(mrb, klass, "apply", |
| 98 | mrb_grn_output_columns_apply, |
| 99 | MRB_ARGS_REQ(1)); |
| 100 | } |
| 101 | #endif |