Class: OpenCC::Converter
- Inherits:
-
Object
- Object
- OpenCC::Converter
- Defined in:
- ext/opencc/opencc.c
Instance Method Summary collapse
Constructor Details
#initialize(*args) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'ext/opencc/opencc.c', line 39
static VALUE opencc_converter_t_initialize(int argc, VALUE* argv, VALUE self) {
opencc_converter_t *ptr;
const char *cfg = NULL;
if (argv[0] != Qnil) {
switch (TYPE(argv[0])) {
case T_SYMBOL:
cfg = RSTRING_PTR(rb_sym_to_s(argv[0]));
break;
case T_STRING:
cfg = RSTRING_PTR(argv[0]);
break;
}
}
TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
ptr->opencc = opencc_open(cfg);
// On error the return value will be (opencc_t) -1.
if (ptr->opencc == (opencc_t) - 1) {
free(ptr);
rb_raise(rb_eException, "%s", "(opencc_open) failed to allocate instance of opencc");
}
return self;
}
|
Instance Method Details
#close ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'ext/opencc/opencc.c', line 81
static VALUE opencc_converter_t_close(VALUE self) {
opencc_converter_t *ptr;
TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
if (ptr->opencc != NULL && opencc_close(ptr->opencc) == 0) {
ptr->opencc = NULL;
return Qtrue;
} else {
return Qfalse;
}
}
|
#convert(input) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'ext/opencc/opencc.c', line 66
static VALUE opencc_converter_t_convert(VALUE self, VALUE input) {
opencc_converter_t *ptr;
TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
if (ptr->opencc != NULL) {
char * buff = opencc_convert_utf8(ptr->opencc, RSTRING_PTR(input), RSTRING_LEN(input));
VALUE result = rb_utf8_str_new_cstr(buff);
opencc_convert_utf8_free(buff);
return result;
} else {
rb_warn("%s", "opencc has been closed");
return Qnil;
}
}
|