f30069a63ad79139708278ff0da25e3c587addb3
[chise/ruby.git] / ext / rbchise.c
1 /* Ruby/CHISE ext by eto 2003-0308 */
2
3 #include "ruby.h"
4 #include "chise.h"
5
6 static VALUE mCHISE, cDS, cDT, cFT, cVALUE;
7
8 typedef struct {
9   CHISE_DS ds;
10 } RB_CHISE_DS;
11
12 typedef struct {
13   CHISE_Decoding_Table *dt;
14 } RB_CHISE_DT;
15
16 typedef struct {
17   CHISE_Feature_Table *ft;
18 } RB_CHISE_FT;
19
20 typedef struct {
21   CHISE_Value value;
22 } RB_CHISE_VALUE;
23
24 static VALUE fds_new(VALUE klass, VALUE type, VALUE location){
25   RB_CHISE_DS *rds;
26   VALUE tdata = Data_Make_Struct(klass, RB_CHISE_DS, 0, free, rds);
27   chise_open_data_source (&rds->ds, (CHISE_DS_Type)NUM2INT(type), RSTRING(rb_str_to_str(location))->ptr);
28   return tdata;
29 }
30
31 static VALUE fds_close(VALUE obj){
32   RB_CHISE_DS *rds;
33   Data_Get_Struct(obj, RB_CHISE_DS, rds);
34   chise_close_data_source (&rds->ds);
35   return Qnil;
36 }
37
38 static VALUE fds_open_dt(VALUE obj, VALUE ccs){
39   RB_CHISE_DS *rds;
40   RB_CHISE_DT *rdt;
41   VALUE vdt;
42   int status;
43   Data_Get_Struct(obj, RB_CHISE_DS, rds);
44   vdt = Data_Make_Struct(cDT, RB_CHISE_DT, 0, free, rdt);
45   status = chise_open_decoding_table (&rdt->dt, &rds->ds, RSTRING(rb_str_to_str(ccs))->ptr, DB_HASH, DB_RDONLY, 0755);
46   if (status){
47     chise_close_decoding_table (rdt->dt);
48     chise_close_data_source (&rds->ds);
49     return Qnil;
50   }
51   return vdt;
52 }
53
54 static VALUE fds_open_ft(VALUE obj, VALUE feature){
55   RB_CHISE_DS *rds;
56   RB_CHISE_FT *rft;
57   VALUE vft;
58   int status;
59   Data_Get_Struct(obj, RB_CHISE_DS, rds);
60   vft = Data_Make_Struct(cFT, RB_CHISE_FT, 0, free, rft);
61   status = chise_open_feature_table (&rft->ft, &rds->ds, RSTRING(rb_str_to_str(feature))->ptr, DB_HASH, DB_RDONLY, 0755);
62   if (status){
63     chise_close_feature_table (rft->ft);
64     chise_close_data_source (&rds->ds);
65     return Qnil;
66   }
67   return vft;
68 }
69
70 static VALUE fdt_get_char(VALUE obj, VALUE code_point){
71   RB_CHISE_DT *rdt;
72   Data_Get_Struct(obj, RB_CHISE_DT, rdt);
73   CHISE_Char_ID char_id;
74   char_id = chise_dt_get_char (rdt->dt, NUM2INT(code_point));
75   return INT2NUM(char_id);
76 }
77
78 static VALUE fdt_close(VALUE obj){
79   RB_CHISE_DT *rdt;
80   Data_Get_Struct(obj, RB_CHISE_DT, rdt);
81   chise_close_decoding_table (rdt->dt);
82   return Qnil;
83 }
84
85 static VALUE fft_get_value(VALUE obj, VALUE char_id){
86   RB_CHISE_FT *rft;
87   RB_CHISE_VALUE *rvalue;
88   VALUE vvalue;
89   int status;
90   Data_Get_Struct(obj, RB_CHISE_FT, rft);
91   vvalue = Data_Make_Struct(cVALUE, RB_CHISE_VALUE, 0, free, rvalue);
92   status = chise_ft_get_value (rft->ft, (CHISE_Char_ID)NUM2INT(char_id), &rvalue->value);
93   return vvalue;
94 }
95
96 static VALUE fft_close(VALUE obj){
97   RB_CHISE_FT *rft;
98   Data_Get_Struct(obj, RB_CHISE_FT, rft);
99   chise_close_feature_table (rft->ft);
100   return Qnil;
101 }
102
103 static VALUE fvalue_to_s(VALUE obj){
104   RB_CHISE_VALUE *rvalue;
105   Data_Get_Struct(obj, RB_CHISE_VALUE, rvalue);
106   return rb_str_new(chise_value_to_c_string(&rvalue->value), chise_value_size(&rvalue->value));
107 }
108
109 void Init_chise(){
110   mCHISE = rb_define_module("CHISE");
111
112   cDS = rb_define_class_under(mCHISE, "DataSource", rb_cObject);
113   rb_define_singleton_method(cDS, "new", fds_new, 2);
114   rb_define_method(cDS, "close", fds_close, 0);
115   rb_define_const(cDS, "NONE",  INT2FIX(CHISE_DS_NONE));
116   rb_define_const(cDS, "Berkeley_DB",  INT2FIX(CHISE_DS_Berkeley_DB));
117   rb_define_method(cDS, "open_decoding_table", fds_open_dt, 1);
118   rb_define_method(cDS, "open_feature_table", fds_open_ft, 1);
119
120   cDT = rb_define_class_under(mCHISE, "DecodingTable", rb_cObject);
121   rb_define_method(cDT, "get_char", fdt_get_char, 1);
122   rb_define_method(cDT, "close", fdt_close, 0);
123
124   cFT = rb_define_class_under(mCHISE, "FeatureTable", rb_cObject);
125   rb_define_method(cFT, "get_value", fft_get_value, 1);
126   rb_define_method(cFT, "close", fft_close, 0);
127
128   cVALUE = rb_define_class_under(mCHISE, "Value", rb_cObject);
129   rb_define_method(cVALUE, "to_s", fvalue_to_s, 0);
130 }