summaryrefslogtreecommitdiff
path: root/test/hangul.c
blob: 4023759ff336e0fa4b694dc8b694ae72093510d6 (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iconv.h>

#include "../hangul/hangul.h"

#ifdef WORDS_BIGENDIAN
#define UCS4 "UCS-4BE"
#else
#define UCS4 "UCS-4LE"
#endif

void ucs4_to_utf8(char *buf, const ucschar *ucs4, size_t bufsize)
{
    size_t n;
    char*  inbuf;
    size_t inbytesleft;
    char*  outbuf;
    size_t outbytesleft;
    size_t ret;
    iconv_t cd;

    for (n = 0; ucs4[n] != 0; n++)
	continue;

    if (n == 0) {
	buf[0] = '\0';
	return;
    }

    cd = iconv_open("UTF-8", UCS4);
    if (cd == (iconv_t)(-1))
	return;

    inbuf = (char*)ucs4;
    inbytesleft = n * 4;
    outbuf = buf;
    outbytesleft = bufsize;
    ret = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);

    iconv_close(cd);

    if (outbytesleft > 0)
	*outbuf = '\0';
    else
	buf[bufsize - 1] = '\0';
}

int
main(int argc, char *argv[])
{
    int ascii;
    const char* keyboard = "2";
    char commit[32] = { '\0', };
    HangulInputContext *hic;

    if (argc > 1) {
	keyboard = argv[1];
    }

    hic = hangul_ic_new(keyboard);
    if (hic == NULL) {
	printf("hic is null\n");
	return -1;
    }

    for (ascii = getchar(); ascii != EOF; ascii = getchar()) {
	int ret = hangul_ic_process(hic, ascii);
	ucs4_to_utf8(commit, hangul_ic_get_commit_string(hic), sizeof(commit));
	if (strlen(commit) > 0) {
	    printf("%s", commit);
	}
	if (!ret) {
	    printf("%c", ascii);
	}
    } 

    if (!hangul_ic_is_empty(hic)) {
	ucs4_to_utf8(commit, hangul_ic_flush(hic), sizeof(commit));
	if (strlen(commit) > 0) {
	    printf("%s", commit);
	}
    }

    hangul_ic_delete(hic);

    return 0;
}