summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/mruby-input-stream/src/mruby_input_stream.c
blob: a5f90b9814b8b6282f50b6d79c940350e88dec1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <string.h>
#include "mruby.h"
#include "mruby/value.h"
#include "mruby/data.h"
#include "mruby/string.h"
#include "mruby_input_stream.h"

typedef struct mrb_input_stream_t {
  const char *base;
  mrb_int len;
  mrb_int pos;
  mrb_input_stream_free_callback free_cb;
  void *free_cb_data;
} mrb_input_stream_t;

static mrb_input_stream_t*
mrb_input_stream_create(mrb_state *mrb, const char *base, mrb_int len, mrb_input_stream_free_callback cb, void *cb_data);

static void
mrb_mruby_input_stream_free(mrb_state *mrb, void *ptr);

static mrb_int
seek_char(mrb_input_stream_t *stream, const char chr);

const static struct mrb_data_type mrb_input_stream_type = {
  "InputStream",
  mrb_mruby_input_stream_free,
};

static void
assert_is_open(mrb_state *mrb, mrb_input_stream_t *stream)
{
  if (stream->len < 0) {
    struct RClass *klass = mrb_class_get(mrb, "IOError");
    if (klass == NULL)
      klass = E_RUNTIME_ERROR;
    mrb_raise(mrb, klass, "stream closed");
  }
}

static mrb_value
mrb_input_stream_init(mrb_state *mrb, mrb_value self)
{
  mrb_value str;
  mrb_int len;
  char *ptr;
  mrb_input_stream_t *stream;
  mrb_int n = mrb_get_args(mrb, "|S", &str);
  if (n > 1) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1)", mrb_fixnum_value(n));
  }

  if (n == 1) {
    len = RSTRING_LEN(str);
    ptr = RSTRING_PTR(str);
    stream = mrb_input_stream_create(mrb, ptr, len, NULL, NULL);
  } else {
    stream = mrb_input_stream_create(mrb, NULL, 0, NULL, NULL);
  }

  DATA_TYPE(self) = &mrb_input_stream_type;
  DATA_PTR(self) = stream;
  return self;
}

static void
default_free_cb(mrb_state *mrb, const char *base, mrb_int len, void *cb_data)
{
  if (base != NULL)
    mrb_free(mrb, (void *)base);
}

static void
mrb_mruby_input_stream_free(mrb_state *mrb, void *ptr)
{
  mrb_input_stream_t *stream = (mrb_input_stream_t *)ptr;
  if (stream->free_cb != NULL)
    stream->free_cb(mrb, stream->base, stream->len, stream->free_cb_data);
  mrb_free(mrb, stream);
}

static void setup_stream(mrb_state *mrb, mrb_input_stream_t *stream, const char *base, mrb_int len, mrb_int pos, mrb_input_stream_free_callback free_cb, void *free_cb_data)
{
  if (free_cb == NULL) {
    if (len > 0) {
      char *dst_base = (char *)mrb_malloc(mrb, sizeof(char)*len);
      memcpy(dst_base, base, len);
      stream->base = dst_base;
      stream->len = len;
    } else {
      stream->base = NULL;
      stream->len = len;
    }
    stream->free_cb = default_free_cb;
    stream->free_cb_data = NULL;
  } else {
    stream->base = base;
    stream->len = len;
    stream->free_cb = free_cb;
    stream->free_cb_data = free_cb_data;
  }

  stream->pos = pos;
}

mrb_input_stream_t*
mrb_input_stream_create(mrb_state *mrb, const char *base, mrb_int len, mrb_input_stream_free_callback free_cb, void *free_cb_data)
{
  mrb_input_stream_t *stream = (mrb_input_stream_t *)mrb_malloc(mrb, sizeof(mrb_input_stream_t));

  setup_stream(mrb, stream, base, len, 0, free_cb, free_cb_data);
  return stream;
}

mrb_value
mrb_input_stream_value(mrb_state *mrb, const char *base, mrb_int len)
{
  mrb_input_stream_t *stream = mrb_input_stream_create(mrb, base, len, NULL, NULL);
  struct RClass *c = mrb_class_get(mrb, "InputStream");
  struct RData *d = mrb_data_object_alloc(mrb, c, stream, &mrb_input_stream_type);

  return mrb_obj_value(d);
}

void
mrb_input_stream_get_data(mrb_state *mrb, mrb_value self, const char **base, mrb_int *len, mrb_int *pos, mrb_input_stream_free_callback *free_cb, void **free_cb_data)
{
  mrb_input_stream_t *stream = DATA_PTR(self);

  if (base != NULL)
    *base = stream->base;
  if (len != NULL)
    *len = stream->len;
  if (pos != NULL)
    *pos = stream->pos;
  if (free_cb != NULL)
    *free_cb = stream->free_cb;
  if (free_cb_data != NULL)
    *free_cb_data = stream->free_cb_data;
}

void
mrb_input_stream_set_data(mrb_state *mrb, mrb_value self, const char *base, mrb_int len, mrb_int pos, mrb_input_stream_free_callback free_cb, void *free_cb_data)
{
  mrb_input_stream_t *stream = DATA_PTR(self);

  if (stream->free_cb != NULL)
    stream->free_cb(mrb, stream->base, stream->len, stream->free_cb_data);
  setup_stream(mrb, stream, base, len, pos, free_cb, free_cb_data);
}

static mrb_value
mrb_input_stream_gets(mrb_state *mrb, mrb_value self)
{
  mrb_input_stream_t *stream = DATA_PTR(self);
  mrb_int pos, len;

  assert_is_open(mrb, stream);

  pos = stream->pos;
  len = seek_char(stream, '\n');
  if (len < 0) {
    return mrb_nil_value();
  }
  if (stream->pos + len < stream->len) {
    len++;
  }
  stream->pos += len;
  return mrb_str_new(mrb, (stream->base + pos), len);
}

static mrb_int
seek_char(mrb_input_stream_t *stream, char chr){
  const char *base = stream->base;
  size_t len = stream->len;
  mrb_int pos = stream->pos;
  const char *end = base + len;
  const char *start = base + pos;
  const char *s = start;

  if (pos >= len) {
    return -1;
  }

  while (s < end) {
    if (*s == chr) {
      break;
    }
    s++;
  }
  return (s - start);
}

static mrb_value
mrb_input_stream_read(mrb_state *mrb, mrb_value self)
{
  mrb_int len;
  mrb_value buf;
  mrb_int n = mrb_get_args(mrb, "|iS", &len, &buf), pos;
  mrb_input_stream_t *stream = DATA_PTR(self);
  const char *start;

  assert_is_open(mrb, stream);

  pos = stream->pos;
  start = stream->base + pos;

  if (pos >= stream->len) {
    return mrb_nil_value();
  }
  if (n == 0) {
    stream->pos = stream->len;
    return mrb_str_new(mrb, start, stream->len - pos);
  } else {
    mrb_int newpos = pos + len;
    if (newpos > stream->len) {
      newpos = stream->len;
    }
    stream->pos = newpos;
    if (n == 1) {
      return mrb_str_new(mrb, start, newpos - pos);
    } else {
      return mrb_str_cat(mrb, buf, start, newpos - pos);
    }
  }
}

static mrb_value
mrb_input_stream_rewind(mrb_state *mrb, mrb_value self)
{
  mrb_input_stream_t *stream = DATA_PTR(self);
  assert_is_open(mrb, stream);
  stream->pos = 0;
  return self;
}


static mrb_value
mrb_input_stream_byteindex(mrb_state *mrb, mrb_value self)
{
  mrb_input_stream_t *stream = DATA_PTR(self);
  mrb_int chr, n, len;

  assert_is_open(mrb, stream);

  n = mrb_get_args(mrb, "i", &chr);
  if (n != 1) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1)", mrb_fixnum_value(n));
  }
  if (chr < 0 || chr > 255) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "index should be a byte (0 - 255)");
  }

  len = seek_char(stream, chr);
  if (len < 0) {
    return mrb_nil_value();
  }

  return mrb_fixnum_value(len);
}


void
mrb_mruby_input_stream_gem_init(mrb_state* mrb)
{
  struct RClass * c = mrb_define_class(mrb, "InputStream", mrb->object_class);

  mrb_define_method(mrb, c, "gets",  mrb_input_stream_gets,  MRB_ARGS_NONE());
  mrb_define_method(mrb, c, "read",  mrb_input_stream_read,  MRB_ARGS_ANY());
  mrb_define_method(mrb, c, "initialize",  mrb_input_stream_init,  MRB_ARGS_BLOCK());
  mrb_define_method(mrb, c, "rewind",  mrb_input_stream_rewind,  MRB_ARGS_NONE());
  mrb_define_method(mrb, c, "byteindex",  mrb_input_stream_byteindex,  MRB_ARGS_ANY());
}

void
mrb_mruby_input_stream_gem_final(mrb_state* mrb)
{
}