summaryrefslogtreecommitdiff
path: root/bindings/ruby/hangul.c
blob: 6c641d0ddc629aa73d96b0d8796425f4dd87dec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 *  Ruby extenstion library for libhangul.
 *
 *  * Author: Gyoung-Yoon Noh <nohmad@sub-port.net>
 *  * License: Same as libhangul.
 */

#include <locale.h>

#include "ruby.h"
#include "hangul.h"

static void
rbhic_free(HangulInputContext *hic)
{
    hangul_ic_delete(hic);
}

static VALUE
rbhic_alloc(VALUE klass)
{
    setlocale(LC_CTYPE, "");
    HangulInputContext *hic = hangul_ic_new("2");
    return Data_Wrap_Struct(klass, 0, rbhic_free, hic);
}

static VALUE
rbhic_initialize(int argc, VALUE *argv, VALUE self)
{
    HangulInputContext *hic;
    Data_Get_Struct(self, HangulInputContext, hic);
    VALUE keyboard;
    rb_scan_args(argc, argv, "01", &keyboard);
    if (argc > 0) {
        Check_Type(keyboard, T_STRING);
        hangul_ic_select_keyboard(hic, RSTRING_PTR(keyboard));
    }
    return self;
}

static VALUE
rbhic_process(VALUE self, VALUE ch)
{
    Check_Type(ch, T_FIXNUM);
    HangulInputContext *hic;
    Data_Get_Struct(self, HangulInputContext, hic);

    bool ret = hangul_ic_process(hic, NUM2CHR(ch));
    return ret ? Qtrue : Qfalse;
}

static VALUE
rbhic_preedit_string(VALUE self)
{
}

static VALUE
rbhic_commit_string(VALUE self)
{
    HangulInputContext *hic;
    Data_Get_Struct(self, HangulInputContext, hic);

    char cbuf[64] = { '\0', };
    wchar_t *wstr = (wchar_t *) hangul_ic_get_commit_string(hic);
    int len = wcstombs(cbuf, wstr, sizeof(cbuf));
    if (strlen(cbuf) > 0)
        return rb_str_new(cbuf, len);
    else
        return Qnil;
}

static VALUE
rbhic_backspace(VALUE self)
{
}

static VALUE
rbhic_reset(VALUE self)
{
    HangulInputContext *hic;
    Data_Get_Struct(self, HangulInputContext, hic);
    hangul_ic_reset(hic);
    return Qnil;
}

static VALUE
rbhic_flush(VALUE self)
{
    HangulInputContext *hic;
    Data_Get_Struct(self, HangulInputContext, hic);
    hangul_ic_flush(hic);
    return Qnil;
}

void
Init_hangul(void)
{
    /* ::Hangul module. */
    VALUE rb_mHangul;
    rb_mHangul = rb_define_module("Hangul");

    /* Hangul::InputContext class. */
    VALUE rb_cInputContext;
    rb_cInputContext = rb_define_class_under(rb_mHangul, "InputContext", rb_cObject);
    rb_define_alloc_func(rb_cInputContext, rbhic_alloc);

    /* Hangul::InputContext methods. */
    rb_define_method(rb_cInputContext, "initialize", rbhic_initialize, -1);
    rb_define_method(rb_cInputContext, "process", rbhic_process, 1);
    rb_define_method(rb_cInputContext, "commit_string", rbhic_commit_string, 0);
    rb_define_method(rb_cInputContext, "preedit_string", rbhic_preedit_string, 0);
    rb_define_method(rb_cInputContext, "backspace", rbhic_backspace, 0);
    rb_define_method(rb_cInputContext, "flush", rbhic_flush, 0);
    rb_define_method(rb_cInputContext, "reset", rbhic_reset, 0);
}