| 1 |
#include "../src/ffindex.h" |
| 2 |
#include "ruby.h" |
| 3 |
|
| 4 |
VALUE FFindex = Qnil; |
| 5 |
|
| 6 |
void Init_ffindex(); |
| 7 |
|
| 8 |
VALUE method_ffindex_initialize(VALUE self, VALUE ffdata_filename, VALUE ffindex_filename); |
| 9 |
|
| 10 |
static VALUE ffindex_db_allocate(VALUE klass); |
| 11 |
|
| 12 |
static void ffindex_db_deallocate(void * ffindex_db); |
| 13 |
|
| 14 |
VALUE method_ffindex_get_data_by_index(VALUE self, VALUE key); |
| 15 |
|
| 16 |
VALUE method_ffindex_get_data_by_name(VALUE self, VALUE key); |
| 17 |
|
| 18 |
VALUE method_ffindex_each_data(VALUE self, VALUE key); |
| 19 |
|
| 20 |
VALUE method_ffindex_collect_data(VALUE self, VALUE key); |
| 21 |
|
| 22 |
|
| 23 |
void Init_ffindex() |
| 24 |
{ |
| 25 |
FFindex = rb_define_class("FFindex", rb_cObject); |
| 26 |
rb_define_alloc_func(FFindex, ffindex_db_allocate); |
| 27 |
rb_define_method(FFindex, "initialize", method_ffindex_initialize, 2); |
| 28 |
rb_define_method(FFindex, "get_data_by_index", method_ffindex_get_data_by_index, 1); |
| 29 |
rb_define_method(FFindex, "get_data_by_name", method_ffindex_get_data_by_name, 1); |
| 30 |
rb_define_method(FFindex, "each_data", method_ffindex_each_data, 0); |
| 31 |
rb_define_method(FFindex, "collect_data", method_ffindex_collect_data, 0); |
| 32 |
} |
| 33 |
|
| 34 |
static void ffindex_db_deallocate(void * ffindex_db) |
| 35 |
{ |
| 36 |
free((ffindex_db_t *)ffindex_db); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
static VALUE ffindex_db_allocate(VALUE klass) |
| 41 |
{ |
| 42 |
ffindex_db_t * ffindex_db = (ffindex_db_t *)calloc(1, sizeof(ffindex_db_t)); |
| 43 |
return Data_Wrap_Struct(klass, NULL, ffindex_db_deallocate, ffindex_db); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
VALUE method_ffindex_initialize(VALUE self, VALUE ffdata_filename, VALUE ffindex_filename) |
| 48 |
{ |
| 49 |
Check_Type(ffdata_filename, T_STRING); |
| 50 |
Check_Type(ffindex_filename, T_STRING); |
| 51 |
|
| 52 |
ffindex_db_t * ffindex_db; |
| 53 |
Data_Get_Struct(self, ffindex_db_t, ffindex_db); |
| 54 |
|
| 55 |
ffindex_db->ffdata_filename = calloc(RSTRING_LEN(ffdata_filename) + 1, sizeof(char)); |
| 56 |
memcpy(ffindex_db->ffdata_filename, StringValuePtr(ffdata_filename), RSTRING_LEN(ffdata_filename)); |
| 57 |
|
| 58 |
ffindex_db->ffindex_filename = calloc(RSTRING_LEN(ffindex_filename) + 1, sizeof(char)); |
| 59 |
memcpy(ffindex_db->ffindex_filename, StringValuePtr(ffindex_filename), RSTRING_LEN(ffindex_filename)); |
| 60 |
|
| 61 |
ffindex_db->mode[0] = 'r'; |
| 62 |
|
| 63 |
ffindex_db = ffindex_index_db_open(ffindex_db); |
| 64 |
|
| 65 |
return self; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
VALUE method_ffindex_get_data_by_index(VALUE self, VALUE key) |
| 70 |
{ |
| 71 |
ffindex_db_t * ffindex_db; |
| 72 |
Data_Get_Struct(self, ffindex_db_t, ffindex_db); |
| 73 |
|
| 74 |
size_t index = FIX2INT(key); |
| 75 |
ffindex_entry_t * entry = ffindex_get_entry_by_index(ffindex_db->ffindex, index); |
| 76 |
if(entry) |
| 77 |
{ |
| 78 |
char * data = ffindex_get_data_by_entry(ffindex_db->ffdata, entry); |
| 79 |
return rb_str_new2(data); |
| 80 |
} |
| 81 |
else |
| 82 |
return Qnil; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
VALUE method_ffindex_get_data_by_name(VALUE self, VALUE key) |
| 87 |
{ |
| 88 |
Check_Type(key, T_STRING); |
| 89 |
char * name = calloc(RSTRING_LEN(key) + 1, sizeof(char)); |
| 90 |
memcpy(name, StringValuePtr(key), RSTRING_LEN(key)); |
| 91 |
|
| 92 |
ffindex_db_t * ffindex_db; |
| 93 |
Data_Get_Struct(self, ffindex_db_t, ffindex_db); |
| 94 |
ffindex_entry_t * entry = ffindex_get_entry_by_name(ffindex_db->ffindex, name); |
| 95 |
if(entry) |
| 96 |
{ |
| 97 |
char * data = ffindex_get_data_by_entry(ffindex_db->ffdata, entry); |
| 98 |
return rb_str_new2(data); |
| 99 |
} |
| 100 |
else |
| 101 |
return Qnil; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
VALUE method_ffindex_each_data(VALUE self, VALUE key) |
| 106 |
{ |
| 107 |
if(!rb_block_given_p()) |
| 108 |
rb_raise(rb_eArgError, "a block is required"); |
| 109 |
|
| 110 |
ffindex_db_t * ffindex_db; |
| 111 |
Data_Get_Struct(self, ffindex_db_t, ffindex_db); |
| 112 |
|
| 113 |
size_t num_entries = ffindex_db->ffindex->n_entries; |
| 114 |
size_t i; |
| 115 |
for(i = 0; i < num_entries; i++) |
| 116 |
{ |
| 117 |
rb_yield(rb_funcall(self, rb_intern("get_data_by_index"), 1, INT2FIX(i))); |
| 118 |
} |
| 119 |
|
| 120 |
return INT2FIX(i); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
VALUE method_ffindex_collect_data(VALUE self, VALUE key) |
| 125 |
{ |
| 126 |
if(!rb_block_given_p()) |
| 127 |
rb_raise(rb_eArgError, "a block is required"); |
| 128 |
|
| 129 |
ffindex_db_t * ffindex_db; |
| 130 |
Data_Get_Struct(self, ffindex_db_t, ffindex_db); |
| 131 |
|
| 132 |
size_t num_entries = ffindex_db->ffindex->n_entries; |
| 133 |
size_t i; |
| 134 |
VALUE arr = rb_ary_new(); |
| 135 |
for(i = 0; i < num_entries; i++) |
| 136 |
{ |
| 137 |
rb_ary_push(arr, rb_yield(rb_funcall(self, rb_intern("get_data_by_index"), 1, INT2FIX(i)))); |
| 138 |
} |
| 139 |
|
| 140 |
return arr; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|