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