Navigation Menu

Skip to content

Commit

Permalink
mrb: add LocaleOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jun 21, 2018
1 parent 97071c6 commit f8b1916
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ctx_impl_mrb.c
@@ -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
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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");

Expand Down
82 changes: 82 additions & 0 deletions lib/mrb/mrb_locale_output.c
@@ -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
32 changes: 32 additions & 0 deletions lib/mrb/mrb_locale_output.h
@@ -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

2 changes: 2 additions & 0 deletions lib/mrb/scripts/initialize/post.rb
Expand Up @@ -29,6 +29,8 @@

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|
Expand Down
28 changes: 28 additions & 0 deletions lib/mrb/scripts/locale_output.rb
@@ -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
1 change: 1 addition & 0 deletions lib/mrb/scripts/sources.am
Expand Up @@ -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 \
Expand Down
2 changes: 2 additions & 0 deletions lib/mrb/sources.am
Expand Up @@ -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 \
Expand Down

0 comments on commit f8b1916

Please sign in to comment.