diff options
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/python_swig/Makefile | 22 | ||||
-rw-r--r-- | bindings/python_swig/hangul.i | 78 | ||||
-rw-r--r-- | bindings/python_swig/test_hangul.py | 20 | ||||
-rw-r--r-- | bindings/python_swig/test_hanja.py | 21 | ||||
-rwxr-xr-x | bindings/python_swig/valgrind-test.sh | 3 |
5 files changed, 144 insertions, 0 deletions
diff --git a/bindings/python_swig/Makefile b/bindings/python_swig/Makefile new file mode 100644 index 0000000..759128a --- /dev/null +++ b/bindings/python_swig/Makefile @@ -0,0 +1,22 @@ +TARGET=_hangul.so +SWIG=swig +CC=gcc +PYTHON_CFLAGS=`python-config --cflags` +PYTHON_LIBS=`python-config --libs` +HANGUL_CFLAGS=`pkg-config --cflags libhangul` +HANGUL_LIBS=`pkg-config --libs libhangul` +CFLAGS+=-O2 -Wall + +all: $(TARGET) + +hangul_wrap.c: hangul.i + $(SWIG) $(HANGUL_CFLAGS) -python hangul.i + +hangul_wrap.o: hangul_wrap.c + $(CC) $(CFLAGS) $(PYTHON_CFLAGS) $(HANGUL_CFLAGS) -c -fPIC -o $@ $< + +_hangul.so: hangul_wrap.o + $(CC) -shared -o $@ $< $(HANGUL_LIBS) + +clean: + rm -f *.o *_wrap.c hangul.py* $(TARGET) diff --git a/bindings/python_swig/hangul.i b/bindings/python_swig/hangul.i new file mode 100644 index 0000000..8b21020 --- /dev/null +++ b/bindings/python_swig/hangul.i @@ -0,0 +1,78 @@ +%module hangul +%{ +#include "hangul.h" +#include <iconv.h> + +/* FIXME - to avoid undifined symbol error */ +ucschar hangul_choseong_to_jamo(ucschar ch) { return 0; } +ucschar hangul_jungseong_to_jamo(ucschar ch) { return 0; } +ucschar hangul_jongseong_to_jamo(ucschar ch) { return 0; } +int hanja_table_txt_to_bin(const char* txtfilename, const char* binfilename) +{ return 0; } + +#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'; +} + + +%} + +%typemap(out) const ucschar* { + char commit[32] = { '\0', }; + ucs4_to_utf8(commit, $1, sizeof(commit)); + $result = PyString_FromString(commit); +} + +%typemap(out) ucschar { + $result = Py_BuildValue("I", $1); +} + +%typemap(in) ucschar { + if(!PyInt_Check($input)) { + PyErr_SetString(PyExc_ValueError, "Expected a int"); + return NULL; + } + $1 = (ucschar)PyInt_AS_LONG($input); +} + + + +%include "hangul.h" diff --git a/bindings/python_swig/test_hangul.py b/bindings/python_swig/test_hangul.py new file mode 100644 index 0000000..240e244 --- /dev/null +++ b/bindings/python_swig/test_hangul.py @@ -0,0 +1,20 @@ +from hangul import * +import sys + +hic = hangul_ic_new("2") + +for ascii in raw_input(): + ret = hangul_ic_process(hic, ord(ascii)) + commit = hangul_ic_get_commit_string(hic) + if len(commit)>0: + sys.stdout.write(commit) + if not ret: + sys.stdout.write(ascii) + +if not hangul_ic_is_empty(hic): + commit = hangul_ic_flush(hic) + if len(commit)>0: + sys.stdout.write(commit) + +hangul_ic_delete(hic); +sys.stdout.write('\n') diff --git a/bindings/python_swig/test_hanja.py b/bindings/python_swig/test_hanja.py new file mode 100644 index 0000000..0c2a87b --- /dev/null +++ b/bindings/python_swig/test_hanja.py @@ -0,0 +1,21 @@ +from hangul import * +import sys + +hanja_table_file = None +if len(sys.argv)>2: + hanja_table_file = sys.argv[1] + +table = hanja_table_load(hanja_table_file); +while True: + buf = sys.stdin.read(1024) + if len(buf)==0: + break + list = hanja_table_match_prefix(table, buf); + n = hanja_list_get_size(list); + for i in range(n): + key = hanja_list_get_nth_key(list, i) + value = hanja_list_get_nth_value(list, i) + comment = hanja_list_get_nth_comment(list, i) + print "%s:%s:%s" % (key, value, comment) + +hanja_table_delete(table) diff --git a/bindings/python_swig/valgrind-test.sh b/bindings/python_swig/valgrind-test.sh new file mode 100755 index 0000000..a361677 --- /dev/null +++ b/bindings/python_swig/valgrind-test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +exec valgrind --tool=memcheck --leak-check=full python test_hangul.py $@ + |