-
Notifications
You must be signed in to change notification settings - Fork 0
/
librubyocamlsearchr.c
64 lines (51 loc) · 1.36 KB
/
librubyocamlsearchr.c
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
#include "ocamlsearchr.h"
#include "ruby.h"
VALUE cOcamlSearchR;
static VALUE t_init(VALUE self, VALUE file, VALUE word)
{
int search = init(STR2CSTR(file),STR2CSTR(word));
rb_iv_set(self,"@nb", (INT2FIX(search)));
return self;
}
static VALUE t_reset(VALUE self)
{
reset(FIX2INT(rb_iv_get(self,"@nb")));
}
static VALUE t_search(VALUE self)
{
result * result;
VALUE res;
VALUE bytes;
int position;
bytechain * match;
char * chain;
res = rb_ary_new();
bytes = rb_ary_new();
result = ocamlsearch(FIX2INT(rb_iv_get(self,"@nb")));
if (result_equals(result, &Not_found))
rb_raise(rb_eException, "Results not found");
position = result->position;
match = result->match;
chain = match->chain;
int i;
for(i=0; i < match->length; i++) {
rb_ary_push(bytes, INT2FIX(chain[i]));
}
delete_struct(result);
rb_ary_push(res, INT2FIX(position));
rb_ary_push(res, bytes);
return res;
}
static VALUE t_pos(VALUE self)
{
int pos;
pos = get_position(FIX2INT(rb_iv_get(self, "@nb")));
return INT2FIX(pos);
}
void Init_librubyocamlsearchr() {
cOcamlSearchR = rb_define_class("OcamlSearchR", rb_cObject);
rb_define_method(cOcamlSearchR, "initialize", t_init, 2);
rb_define_method(cOcamlSearchR, "reset", t_reset, 0);
rb_define_method(cOcamlSearchR, "pos", t_pos, 0);
rb_define_method(cOcamlSearchR, "search", t_search, 0);
}