summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2013-02-08 11:04:28 +0800
committerPeng Wu <alexepico@gmail.com>2013-02-08 11:04:28 +0800
commit79529dea8225187a0ab1c0d4c87ad558760322d5 (patch)
treebb83fea133ef7138c5c6fa81bb115e49f6f71edc
parent42fdd95eeea6e72f65fe74e356b6ab96bc95666c (diff)
downloadibus-libpinyin-79529dea8225187a0ab1c0d4c87ad558760322d5.tar.gz
ibus-libpinyin-79529dea8225187a0ab1c0d4c87ad558760322d5.tar.xz
ibus-libpinyin-79529dea8225187a0ab1c0d4c87ad558760322d5.zip
write importPinyinDictionary
-rw-r--r--src/PYLibPinyin.cc48
-rw-r--r--src/PYLibPinyin.h4
-rw-r--r--src/PYPConfig.cc6
3 files changed, 54 insertions, 4 deletions
diff --git a/src/PYLibPinyin.cc b/src/PYLibPinyin.cc
index 811400c..285e52c 100644
--- a/src/PYLibPinyin.cc
+++ b/src/PYLibPinyin.cc
@@ -239,7 +239,51 @@ LibPinyinBackEnd::modified (void)
static_cast<gpointer> (this));
}
-bool
+gboolean
+LibPinyinBackEnd::importPinyinDictionary(const char * filename)
+{
+ /* user phrase library should be loaded here. */
+ FILE * dictfile = fopen(filename, "r");
+ if (NULL == dictfile)
+ return FALSE;
+
+ import_iterator_t * iter = pinyin_begin_add_phrases
+ (m_pinyin_context, 15);
+
+ if (NULL == iter)
+ return FALSE;
+
+ char* linebuf = NULL; size_t size = 0; ssize_t read;
+ while ((read = getline(&linebuf, &size, dictfile)) != -1) {
+ if (0 == strlen(linebuf))
+ continue;
+
+ if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
+ linebuf[strlen(linebuf) - 1] = '\0';
+ }
+
+ gchar ** items = g_strsplit_set(linebuf, " \t", 3);
+ guint len = g_strv_length(items);
+
+ gchar * phrase = NULL, * pinyin = NULL;
+ gint count = -1;
+ if (2 == len || 3 == len) {
+ phrase = items[0];
+ pinyin = items[1];
+ if (3 == len)
+ count = atoi(items[2]);
+ } else
+ continue;
+
+ pinyin_iterator_add_phrase(iter, phrase, pinyin, count);
+ }
+
+ pinyin_end_add_phrases(iter);
+ fclose(dictfile);
+}
+
+
+gboolean
LibPinyinBackEnd::clearPinyinUserData (const char * target)
{
if (0 == strcmp("all", target))
@@ -249,6 +293,8 @@ LibPinyinBackEnd::clearPinyinUserData (const char * target)
PHRASE_INDEX_MAKE_TOKEN(15, null_token));
else
g_warning("unknown clear target: %s.\n", target);
+
+ return TRUE;
}
gboolean
diff --git a/src/PYLibPinyin.h b/src/PYLibPinyin.h
index 18fd9d1..2400b0e 100644
--- a/src/PYLibPinyin.h
+++ b/src/PYLibPinyin.h
@@ -50,8 +50,8 @@ public:
void freeChewingInstance (pinyin_instance_t *instance);
void modified (void);
- bool importPinyinDictionary(const char * filename);
- bool clearPinyinUserData(const char * target);
+ gboolean importPinyinDictionary(const char * filename);
+ gboolean clearPinyinUserData(const char * target);
/* use static initializer in C++. */
static LibPinyinBackEnd & instance (void) { return *m_instance; }
diff --git a/src/PYPConfig.cc b/src/PYPConfig.cc
index de78dda..6a70539 100644
--- a/src/PYPConfig.cc
+++ b/src/PYPConfig.cc
@@ -51,8 +51,8 @@ const gchar * const CONFIG_GUIDE_KEY = "GuideKey";
const gchar * const CONFIG_AUXILIARY_SELECT_KEY_F = "AuxiliarySelectKey_F";
const gchar * const CONFIG_AUXILIARY_SELECT_KEY_KP = "AuxiliarySelectKey_KP";
const gchar * const CONFIG_ENTER_KEY = "EnterKey";
-const gchar * const CONFIG_CLEAR_USER_DATA = "ClearUserData";
const gchar * const CONFIG_IMPORT_DICTIONARY = "ImportDictionary";
+const gchar * const CONFIG_CLEAR_USER_DATA = "ClearUserData";
const guint PINYIN_DEFAULT_OPTION =
PINYIN_INCOMPLETE |
@@ -403,6 +403,10 @@ LibPinyinPinyinConfig::valueChanged (const std::string &section,
m_comma_period_page = normalizeGVariant (value, true);
else if (CONFIG_AUTO_COMMIT == name)
m_auto_commit = normalizeGVariant (value, false);
+ else if (CONFIG_IMPORT_DICTIONARY == name) {
+ std::string filename = normalizeGVariant (value, std::string(""));
+ LibPinyinBackEnd::instance ().importPinyinDictionary(filename.c_str ());
+ }
else if (CONFIG_CLEAR_USER_DATA == name) {
std::string target = normalizeGVariant (value, std::string(""));
LibPinyinBackEnd::instance ().clearPinyinUserData(target.c_str ());