Debian Codegraph @

lib/mrb/mrb_command_input.c

raw symbols SCIP
1 /*
2 Copyright(C) 2015 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 #include <groonga/command.h>
22
23 #ifdef GRN_WITH_MRUBY
24 #include <mruby.h>
25 #include <mruby/class.h>
26 #include <mruby/data.h>
27 #include <mruby/value.h>
28 #include <mruby/string.h>
29
30 #include "mrb_ctx.h"
31 #include "mrb_converter.h"
32 #include "mrb_command_input.h"
33
34 static struct mrb_data_type mrb_grn_command_input_type = {
35 "Groonga::CommandInput",
36 NULL
37 };
38
39 static mrb_value
40 mrb_grn_command_input_initialize(mrb_state *mrb, mrb_value self)
41 {
42 mrb_value mrb_command_input_ptr;
43
44 mrb_get_args(mrb, "o", &mrb_command_input_ptr);
45 DATA_TYPE(self) = &mrb_grn_command_input_type;
46 DATA_PTR(self) = mrb_cptr(mrb_command_input_ptr);
47 return self;
48 }
49
50 static mrb_value
51 mrb_grn_command_input_array_reference(mrb_state *mrb, mrb_value self)
52 {
53 grn_ctx *ctx = (grn_ctx *)mrb->ud;
54 grn_command_input *input;
55 mrb_value mrb_key_or_offset;
56 grn_obj *argument;
57
58 input = DATA_PTR(self);
59 mrb_get_args(mrb, "o", &mrb_key_or_offset);
60
61 switch (mrb_type(mrb_key_or_offset)) {
62 case MRB_TT_INTEGER :
63 {
64 mrb_int offset = mrb_integer(mrb_key_or_offset);
65 argument = grn_command_input_at(ctx, input, (unsigned int)offset);
66 }
67 break;
68 case MRB_TT_SYMBOL :
69 {
70 mrb_sym mrb_key_symbol;
71 const char *key;
72 mrb_int key_length;
73
74 mrb_key_symbol = mrb_symbol(mrb_key_or_offset);
75 key = mrb_sym2name_len(mrb, mrb_key_symbol, &key_length);
76 argument = grn_command_input_get(ctx, input, key, (int)key_length);
77 }
78 break;
79 case MRB_TT_STRING :
80 {
81 mrb_value mrb_key = mrb_key_or_offset;
82 argument = grn_command_input_get(ctx, input,
83 RSTRING_PTR(mrb_key),
84 RSTRING_LEN(mrb_key));
85 }
86 break;
87 default :
88 mrb_raisef(mrb, E_ARGUMENT_ERROR,
89 "must be offset (as integer) or key (as symbol or string): %S",
90 mrb_key_or_offset);
91 break;
92 }
93
94 if (!argument) {
95 return mrb_nil_value();
96 }
97
98 if (GRN_TEXT_LEN(argument) == 0) {
99 return mrb_nil_value();
100 }
101
102 return mrb_str_new_static(mrb,
103 GRN_TEXT_VALUE(argument),
104 GRN_TEXT_LEN(argument));
105 }
106
107 static mrb_value
108 mrb_grn_command_input_get_arguments(mrb_state *mrb, mrb_value self)
109 {
110 grn_ctx *ctx = (grn_ctx *)mrb->ud;
111 grn_command_input *input;
112 grn_obj *arguments;
113
114 input = DATA_PTR(self);
115 arguments = grn_command_input_get_arguments(ctx, input);
116
117 return grn_mrb_value_from_grn_obj(mrb, arguments);
118 }
119
120 void
121 grn_mrb_command_input_init(grn_ctx *ctx)
122 {
123 grn_mrb_data *data = &(ctx->impl->mrb);
124 mrb_state *mrb = data->state;
125 struct RClass *module = data->module;
126 struct RClass *klass;
127
128 klass = mrb_define_class_under(mrb, module, "CommandInput", mrb->object_class);
129 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
130
131 mrb_define_method(mrb, klass, "initialize",
132 mrb_grn_command_input_initialize, MRB_ARGS_REQ(1));
133
134 mrb_define_method(mrb, klass, "[]",
135 mrb_grn_command_input_array_reference, MRB_ARGS_REQ(1));
136
137 mrb_define_method(mrb, klass, "arguments",
138 mrb_grn_command_input_get_arguments, MRB_ARGS_NONE());
139 }
140 #endif