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