Module | NativeSyllableComposer |
In: |
lib/ext/native_syllable_composer/native_syllable_composer.c
|
// cf. www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html // cf. cesare.seesaa.net/article/32850625.html include "ruby.h" include "compose.h" VALUE syllable_composer = Qnil;
void Init_native_syllable_composer(); VALUE native_syllable_composer_compose(VALUE self, VALUE rInputType, VALUE rOutputType, VALUE rSyllable);
void Init_native_syllable_composer() {
syllable_composer = rb_define_module("NativeSyllableComposer"); rb_define_singleton_method(syllable_composer, "compose", native_syllable_composer_compose, 3);
}
VALUE native_syllable_composer_compose(VALUE self, VALUE rInputType, VALUE rOutputType, VALUE rSyllable) {
int inputType = NUM2INT(rInputType); int outputType = NUM2INT(rOutputType); VALUE rStr = StringValue(rSyllable); size_t rStrLen = RSTRING(rStr)->len; char *rStrPtr = RSTRING(rStr)->ptr; if (!rStrPtr) return Qnil; char *string = (char*)calloc(1, rStrLen); memcpy(string, rStrPtr,rStrLen); VALUE result = ComposeTLSyllable(inputType, outputType, string); free(string); return result;
}