Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add swig interface for python and test codes

git-svn-id: http://kldp.net/svn/hangul/libhangul/trunk@197 8f00fcd2-89fc-0310-932e-b01be5b65e01
  • Loading branch information...
commit c35fb187594b34f054894133b40de95de6f8ddd5 1 parent c46cd8e
@jooncheol jooncheol authored
View
22 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)
View
78 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"
View
20 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')
View
21 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)
View
3  bindings/python_swig/valgrind-test.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+exec valgrind --tool=memcheck --leak-check=full python test_hangul.py $@
+
Please sign in to comment.
Something went wrong with that request. Please try again.