Kouhei Sutou
null+****@clear*****
Thu Jun 21 16:55:21 JST 2018
Kouhei Sutou 2018-06-21 16:55:21 +0900 (Thu, 21 Jun 2018) New Revision: f8b191650c03bbe3a0ff88f6901574dd5fec5606 https://github.com/groonga/groonga/commit/f8b191650c03bbe3a0ff88f6901574dd5fec5606 Message: mrb: add LocaleOutput Added files: lib/mrb/mrb_locale_output.c lib/mrb/mrb_locale_output.h lib/mrb/scripts/locale_output.rb Modified files: lib/ctx_impl_mrb.c lib/mrb/scripts/initialize/post.rb lib/mrb/scripts/sources.am lib/mrb/sources.am Modified: lib/ctx_impl_mrb.c (+3 -1) =================================================================== --- lib/ctx_impl_mrb.c 2018-06-21 16:49:06 +0900 (fdb25108d) +++ lib/ctx_impl_mrb.c 2018-06-21 16:55:21 +0900 (362877b25) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013-2017 Brazil + Copyright(C) 2013-2018 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -69,6 +69,7 @@ # include "mrb/mrb_eval_context.h" # include "mrb/mrb_thread.h" # include "mrb/mrb_window_definition.h" +# include "mrb/mrb_locale_output.h" # include <mruby/array.h> # include <mruby/string.h> @@ -185,6 +186,7 @@ mrb_groonga_init(mrb_state *mrb, mrb_value self) grn_mrb_eval_context_init(ctx); grn_mrb_thread_init(ctx); grn_mrb_window_definition_init(ctx); + grn_mrb_locale_output_init(ctx); grn_mrb_load(ctx, "initialize/post.rb"); Added: lib/mrb/mrb_locale_output.c (+82 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_locale_output.c 2018-06-21 16:55:21 +0900 (f25e5b60e) @@ -0,0 +1,82 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2018 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "../grn_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/string.h> +#include <mruby/variable.h> + +#include "../grn_mrb.h" +#include "../grn_encoding.h" + +static mrb_value +mrb_grn_locale_output_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_output; + + mrb_get_args(mrb, "o", &mrb_output); + mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@output"), mrb_output); + + return self; +} + +static mrb_value +mrb_grn_locale_output_write(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_output; + char *utf8_message; + mrb_int utf8_message_size; + const char *locale_message; + size_t locale_message_size; + mrb_value mrb_locale_message; + mrb_value mrb_written; + + mrb_get_args(mrb, "s", &utf8_message, &utf8_message_size); + locale_message = + grn_encoding_convert_to_utf8_from_locale(ctx, + utf8_message, + utf8_message_size, + &locale_message_size); + mrb_locale_message = mrb_str_new(mrb, locale_message, locale_message_size); + grn_encoding_converted_free(ctx, locale_message); + + mrb_output = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@output")); + mrb_written = mrb_funcall(mrb, mrb_output, "write", 1, mrb_locale_message); + + return mrb_written; +} + +void +grn_mrb_locale_output_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "LocaleOutput", mrb->object_class); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_locale_output_initialize, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "write", + mrb_grn_locale_output_write, MRB_ARGS_REQ(1)); +} +#endif Added: lib/mrb/mrb_locale_output.h (+32 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_locale_output.h 2018-06-21 16:55:21 +0900 (a87c50395) @@ -0,0 +1,32 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2018 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_locale_output_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + Modified: lib/mrb/scripts/initialize/post.rb (+2 -0) =================================================================== --- lib/mrb/scripts/initialize/post.rb 2018-06-21 16:49:06 +0900 (e94771413) +++ lib/mrb/scripts/initialize/post.rb 2018-06-21 16:55:21 +0900 (9b5e247a7) @@ -29,6 +29,8 @@ require "labeled_arguments" require "command_line_parser" +require "locale_output" + load_path = ENV["GRN_RUBY_LOAD_PATH"] if load_path load_path.split(File::PATH_SEPARATOR).each do |path| Added: lib/mrb/scripts/locale_output.rb (+28 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/locale_output.rb 2018-06-21 16:55:21 +0900 (9eae5d66a) @@ -0,0 +1,28 @@ +module Groonga + class LocaleOutput + def puts(*args) + if args.empty? + write("\n") + return nil + end + + args.each do |arg| + case arg + when String + write(arg) + write("\n") if arg[-1] != "\n" + when Array + next if arg.empty? + puts(*arg) + else + if arg.respond_to?(:to_ary) + puts(arg.to_ary) + else + puts(arg.to_s) + end + end + end + nil + end + end +end Modified: lib/mrb/scripts/sources.am (+1 -0) =================================================================== --- lib/mrb/scripts/sources.am 2018-06-21 16:49:06 +0900 (ae1a95935) +++ lib/mrb/scripts/sources.am 2018-06-21 16:55:21 +0900 (7d6c659b4) @@ -20,6 +20,7 @@ RUBY_SCRIPT_FILES = \ index_cursor.rb \ index_info.rb \ labeled_arguments.rb \ + locale_output.rb \ logger.rb \ object.rb \ operator.rb \ Modified: lib/mrb/sources.am (+2 -0) =================================================================== --- lib/mrb/sources.am 2018-06-21 16:49:06 +0900 (9db167371) +++ lib/mrb/sources.am 2018-06-21 16:55:21 +0900 (b0a6d2c63) @@ -45,6 +45,8 @@ libgrnmrb_la_SOURCES = \ mrb_index_column.h \ mrb_index_cursor.c \ mrb_index_cursor.h \ + mrb_locale_output.c \ + mrb_locale_output.h \ mrb_logger.c \ mrb_logger.h \ mrb_object.c \ -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180621/55859556/attachment-0001.htm