summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Huang <shawn.p.huang@gmail.com>2010-07-06 10:46:46 +0800
committerPeng Huang <shawn.p.huang@gmail.com>2010-07-06 10:46:46 +0800
commit0d928e4d3300f7dfb4969c7e662d006ff2f73502 (patch)
treee25494edd1a5c1d27f096461e27a254f21bb4cbe
parentf46a3755384fb6dab200d42c059d3d9ec6ebbbf3 (diff)
downloadibus-libpinyin-0d928e4d3300f7dfb4969c7e662d006ff2f73502.tar.gz
ibus-libpinyin-0d928e4d3300f7dfb4969c7e662d006ff2f73502.tar.xz
ibus-libpinyin-0d928e4d3300f7dfb4969c7e662d006ff2f73502.zip
Use opencc
-rw-r--r--debian/control3
-rwxr-xr-xdebian/rules2
-rw-r--r--src/SimpTradConverter.cc74
3 files changed, 45 insertions, 34 deletions
diff --git a/debian/control b/debian/control
index 7abb748..6f3c75a 100644
--- a/debian/control
+++ b/debian/control
@@ -4,7 +4,8 @@ Priority: optional
Maintainer: LI Daobing <lidaobing@debian.org>
Build-Depends: debhelper (>= 7), autotools-dev, pkg-config,
libibus-dev (>= 1.2.99), libsqlite3-dev (>= 3.6), sqlite3, uuid-dev,
- python-support, intltool (>= 0.35.0), libboost-signals-dev (>= 1.39)
+ python-support, intltool (>= 0.35.0), libboost-signals-dev (>= 1.39),
+ libopencc-dev (>= 0.0.4~pre.1)
Standards-Version: 3.8.4
Homepage: http://code.google.com/p/ibus
Vcs-Bzr: lp:~lidaobing/ibus/ibus-pinyin
diff --git a/debian/rules b/debian/rules
index 54e9f22..1a95429 100755
--- a/debian/rules
+++ b/debian/rules
@@ -32,7 +32,7 @@ endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
cp -f /usr/share/misc/config.guess config.guess
endif
- dh_auto_configure
+ dh_auto_configure -- --enable-opencc
build: build-stamp
diff --git a/src/SimpTradConverter.cc b/src/SimpTradConverter.cc
index 877e3c3..6d64167 100644
--- a/src/SimpTradConverter.cc
+++ b/src/SimpTradConverter.cc
@@ -39,42 +39,52 @@ namespace PY {
#ifdef HAVE_OPENCC
-void
-SimpTradConverter::simpToTrad (const gchar *in, String &out)
-{
- const static int BUFFER_SIZE = 64;
- static gunichar buf[BUFFER_SIZE + 1];
- gunichar *in_ucs4;
- wchar_t * pinbuf, * poutbuf;
- size_t inbuf_left, outbuf_left;
-
- in_ucs4 = g_utf8_to_ucs4_fast (in, -1, NULL);
- pinbuf = (wchar_t *)in_ucs4;
- poutbuf = (wchar_t *)buf;
- inbuf_left = std::wcslen ((wchar_t *) pinbuf);
- outbuf_left = BUFFER_SIZE;
-
- opencc_t od = opencc_open(OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD);
-
- size_t retv;
- while ((retv = opencc_convert(od, &pinbuf, &inbuf_left, &poutbuf, &outbuf_left)) > 0)
+class opencc {
+ static const int BUFFER_SIZE = 64;
+public:
+ opencc (void)
{
- if (retv == (size_t) -1)
- {
- g_warning ("SimpTradConverter:");
- opencc_perror("Opencc:");
- g_free (in_ucs4);
- return;
- }
+ m_od = opencc_open (OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD);
+ g_assert (m_od != NULL);
+ }
- *poutbuf = L'\0';
- out << buf;
- outbuf_left = BUFFER_SIZE;
- poutbuf = (wchar_t *)buf;
+ ~opencc (void)
+ {
+ opencc_close(m_od);
}
- opencc_close(od);
- g_free (in_ucs4);
+ void convert (const gchar *in, String &out)
+ {
+ glong n_char;
+ gunichar *in_ucs4 = g_utf8_to_ucs4_fast (in, -1, &n_char);
+
+ wchar_t *pinbuf = (wchar_t *)in_ucs4;
+ size_t inbuf_left = n_char;
+ while (inbuf_left != 0) {
+ wchar_t *poutbuf = (wchar_t *)m_buffer;
+ size_t outbuf_left = BUFFER_SIZE;
+ size_t retval = opencc_convert(m_od, &pinbuf, &inbuf_left, &poutbuf, &outbuf_left);
+ if (retval == (size_t) -1) {
+ /* append left chars in pinbuf */
+ g_warning ("opencc_convert return failed");
+ out << (gunichar *) pinbuf;
+ break;
+ }
+ *poutbuf = L'\0';
+ out << m_buffer;
+ }
+ g_free (in_ucs4);
+ }
+private:
+ opencc_t m_od;
+ gunichar m_buffer[BUFFER_SIZE + 1];
+};
+
+void
+SimpTradConverter::simpToTrad (const gchar *in, String &out)
+{
+ static opencc opencc;
+ opencc.convert (in, out);
}
#else