
1: %module anthy 2: %include typemaps.i 3: 4: %{ 5: #include "include/anthy.h" 6: %} 7: 8: %init %{ 9: anthy_init(); 10: %} 11: 12: %define OUTPUT_TYPEMAP(type, klass) 13: %typemap(in,numinputs=0) type *OUTPUT(VALUE temp) 14: { 15: temp = rb_funcall(klass, rb_intern("new"), 0); 16: Data_Get_Struct(temp, $*ltype, $1); 17: } 18: 19: %typemap(argout) type *OUTPUT 20: { 21: if ($result < 0) 22: $result = Qnil; 23: else 24: $result = temp$argnum; 25: } 26: %enddef 27: 28: OUTPUT_TYPEMAP(struct anthy_conv_stat, cConvStat.klass); 29: OUTPUT_TYPEMAP(struct anthy_segment_stat, cSegmentStat.klass); 30: OUTPUT_TYPEMAP(struct anthy_prediction_stat, cPredictionStat.klass); 31: 32: #undef OUTPUT_TYPEMAP 33: 34: %rename(ConvStat) anthy_conv_stat; 35: %rename(SegmentStat) anthy_segment_stat; 36: %rename(PredictionStat) anthy_prediction_stat; 37: 38: %rename(COMPILED_ENCODING) ANTHY_COMPILED_ENCODING; 39: %rename(EUC_JP_ENCODING) ANTHY_EUC_JP_ENCODING; 40: %rename(UTF8_ENCODING) ANTHY_UTF8_ENCODING; 41: 42: %rename(init) anthy_init; 43: %rename(quit) anthy_quit; 44: %rename(conf_override) anthy_conf_override; 45: %rename(set_personality) anthy_set_personality; 46: 47: %ignore anthy_create_context; 48: %ignore anthy_reset_context; 49: %ignore anthy_release_context; 50: 51: %ignore anthy_set_string; 52: %ignore anthy_resize_segment; 53: %ignore anthy_get_stat; 54: %ignore anthy_get_segment_stat; 55: %ignore anthy_get_segment; 56: %ignore anthy_commit_segment; 57: 58: %ignore anthy_set_prediction_string; 59: %ignore anthy_get_prediction_stat; 60: %ignore anthy_get_prediction; 61: 62: %ignore anthy_print_context; 63: %ignore anthy_context_set_encoding; 64: 65: %{ 66: #define anthy_context_set_string anthy_set_string 67: #define anthy_context_resize_segment anthy_resize_segment 68: #define anthy_context_get_stat anthy_get_stat 69: #define anthy_context_get_segment_stat anthy_get_segment_stat 70: /* #define anthy_context_get_segment anthy_get_segment */ 71: /* #define anthy_context_commit_segment anthy_commit_segment */ 72: #define anthy_context_print anthy_print_context 73: 74: #define anthy_context_set_prediction_string anthy_set_prediction_string 75: #define anthy_context_get_prediction_stat anthy_get_prediction_stat 76: /* #define anthy_context_get_prediction anthy_get_prediction */ 77: %} 78: 79: %rename(Context) anthy_context; 80: 81: %rename(get_version_string) anthy_get_version_string; 82: /* SWIG says "Segmentation fault" */ 83: /* %alias anthy_get_version_string "version_string"; */ 84: %inline %{ 85: static char * 86: version_string(void) 87: { 88: return anthy_get_version_string(); 89: } 90: %} 91: 92: /* logger */ 93: %ignore anthy_set_logger; 94: 95: %header %{ 96: static VALUE logger = Qnil; 97: %} 98: 99: %init %{ 100: rb_gc_register_address(&logger); 101: %} 102: 103: %{ 104: #if 0 105: } /* for c-mode indentation */ 106: #endif 107: 108: typedef struct logger_info 109: { 110: VALUE level; 111: VALUE message; 112: } logger_info_t; 113: 114: static VALUE 115: invoke_logger(VALUE data) 116: { 117: logger_info_t *info = (logger_info_t *)data; 118: return rb_funcall(logger, rb_intern("call"), 2, info->level, info->message); 119: } 120: 121: static void 122: rb_logger(int lv, const char *str) 123: { 124: int state = 0; 125: logger_info_t info; 126: 127: info.level = INT2NUM(lv); 128: info.message = rb_str_new2(str); 129: rb_protect(invoke_logger, (VALUE)&info, &state); 130: if (state && !NIL_P(ruby_errinfo)) { 131: VALUE klass, message, backtrace; 132: int i, len; 133: 134: klass = rb_funcall(rb_funcall(ruby_errinfo, rb_intern("class"), 0), 135: rb_intern("to_s"), 0); 136: message = rb_funcall(ruby_errinfo, rb_intern("message"), 0); 137: backtrace = rb_funcall(ruby_errinfo, rb_intern("backtrace"), 0); 138: rb_warning("%s: %s\n", StringValueCStr(klass), StringValueCStr(message)); 139: 140: len = RARRAY(backtrace)->len; 141: for (i = 0; i < len; i++) { 142: rb_warning("%s\n", StringValueCStr(RARRAY(backtrace)->ptr[i])); 143: } 144: } 145: } 146: 147: #if 0 148: { /* for c-mode indentation */ 149: #endif 150: %} 151: 152: %inline %{ 153: #if 0 154: } /* for c-mode indentation */ 155: #endif 156: 157: static void 158: set_logger(int level) 159: { 160: if (rb_block_given_p()) { 161: logger = Qnil; 162: anthy_set_logger(NULL, level); 163: } else { 164: logger = rb_block_proc(); 165: anthy_set_logger(rb_logger, level); 166: } 167: } 168: 169: #if 0 170: { /* for c-mode indentation */ 171: #endif 172: %} 173: 174: %include include/anthy.h 175: 176: 177: %freefunc anthy_context "anthy_release_context"; 178: 179: struct anthy_context 180: { 181: %extend 182: { 183: anthy_context(void) 184: { 185: return anthy_create_context(); 186: }; 187: 188: void reset(void) 189: { 190: anthy_reset_context(self); 191: }; 192: 193: %alias set_string "string="; 194: int set_string(char *); 195: 196: void resize_segment(int, int); 197: 198: %alias get_stat "stat"; 199: int get_stat(struct anthy_conv_stat *OUTPUT); 200: 201: %alias get_segment_stat "segment_stat"; 202: int get_segment_stat(int, struct anthy_segment_stat *OUTPUT); 203: 204: %alias get_segment "segment"; 205: VALUE get_segment(int nth_seg, int nth_cand) 206: { 207: int len; 208: char *buffer; 209: len = anthy_get_segment(self, nth_seg, nth_cand, NULL, 0); 210: buffer = alloca(len + 1); 211: len = anthy_get_segment(self, nth_seg, nth_cand, buffer, len + 1); 212: if (len < 0) { 213: return Qnil; 214: } else { 215: return rb_str_new2(buffer); 216: } 217: }; 218: 219: VALUE commit_segment(int s, int c) 220: { 221: return (anthy_commit_segment(self, s, c) < 0) ? Qfalse : Qtrue; 222: } 223: 224: %alias set_prediction_string "prediction_string="; 225: int set_prediction_string(const char *); 226: 227: %alias get_prediction_stat "prediction_stat"; 228: int get_prediction_stat(struct anthy_prediction_stat *OUTPUT); 229: 230: %alias get_prediction "prediction"; 231: int get_prediction(int nth_pre) 232: { 233: int len; 234: char *buffer; 235: len = anthy_get_prediction(self, nth_pre, NULL, 0); 236: buffer = alloca(len + 1); 237: len = anthy_get_prediction(self, nth_pre, buffer, len + 1); 238: if (len < 0) { 239: return Qnil; 240: } else { 241: return rb_str_new2(buffer); 242: } 243: }; 244: 245: 246: void print(); 247: 248: %alias set_encoding "encoding="; 249: void set_encoding(int encoding); 250: } 251: };