| 1 | /* |
| 2 | Copyright(C) 2018 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 | #include <mruby/variable.h> |
| 26 | |
| 27 | #include "../grn_mrb.h" |
| 28 | #include "../grn_encoding.h" |
| 29 | |
| 30 | static mrb_value |
| 31 | mrb_grn_locale_output_initialize(mrb_state *mrb, mrb_value self) |
| 32 | { |
| 33 | mrb_value mrb_output; |
| 34 | |
| 35 | mrb_get_args(mrb, "o", &mrb_output); |
| 36 | mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@output"), mrb_output); |
| 37 | |
| 38 | return self; |
| 39 | } |
| 40 | |
| 41 | static mrb_value |
| 42 | mrb_grn_locale_output_write(mrb_state *mrb, mrb_value self) |
| 43 | { |
| 44 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 45 | mrb_value mrb_output; |
| 46 | char *utf8_message; |
| 47 | mrb_int utf8_message_size; |
| 48 | const char *locale_message; |
| 49 | size_t locale_message_size; |
| 50 | mrb_value mrb_locale_message; |
| 51 | mrb_value mrb_written; |
| 52 | |
| 53 | mrb_get_args(mrb, "s", &utf8_message, &utf8_message_size); |
| 54 | locale_message = |
| 55 | grn_encoding_convert_to_locale_from_utf8(ctx, |
| 56 | utf8_message, |
| 57 | (ssize_t)utf8_message_size, |
| 58 | &locale_message_size); |
| 59 | mrb_locale_message = mrb_str_new(mrb, locale_message, locale_message_size); |
| 60 | grn_encoding_converted_free(ctx, locale_message); |
| 61 | |
| 62 | mrb_output = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@output")); |
| 63 | mrb_written = mrb_funcall(mrb, mrb_output, "write", 1, mrb_locale_message); |
| 64 | |
| 65 | return mrb_written; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | grn_mrb_locale_output_init(grn_ctx *ctx) |
| 70 | { |
| 71 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 72 | mrb_state *mrb = data->state; |
| 73 | struct RClass *module = data->module; |
| 74 | struct RClass *klass; |
| 75 | |
| 76 | klass = mrb_define_class_under(mrb, module, "LocaleOutput", mrb->object_class); |
| 77 | |
| 78 | mrb_define_method(mrb, klass, "initialize", |
| 79 | mrb_grn_locale_output_initialize, MRB_ARGS_REQ(1)); |
| 80 | mrb_define_method(mrb, klass, "write", |
| 81 | mrb_grn_locale_output_write, MRB_ARGS_REQ(1)); |
| 82 | } |
| 83 | #endif |