summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2019-01-17 15:27:48 +0800
committerPeng Wu <alexepico@gmail.com>2019-01-17 15:27:48 +0800
commitb44bdf7950cfbb445c799f2c7f1c9747c0b24515 (patch)
tree84443c16f4f6679b89d1a05058982b06353e6f2f
parent35adc8e4fc41dd8c989dc16760e179b63637203a (diff)
downloadlibpinyin-b44bdf7950cfbb445c799f2c7f1c9747c0b24515.zip
libpinyin-b44bdf7950cfbb445c799f2c7f1c9747c0b24515.tar.gz
libpinyin-b44bdf7950cfbb445c799f2c7f1c9747c0b24515.tar.xz
Revert "update pinyin.cpp and zhuyin.cpp"
This reverts commit 97ca682a1099ae6da6ceacd9b15f654562567013.
-rw-r--r--src/pinyin.cpp48
-rw-r--r--src/zhuyin.cpp48
2 files changed, 88 insertions, 8 deletions
diff --git a/src/pinyin.cpp b/src/pinyin.cpp
index 9b45ec5..75eb41b 100644
--- a/src/pinyin.cpp
+++ b/src/pinyin.cpp
@@ -365,7 +365,7 @@ pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir){
context->m_user_bigram = new Bigram;
filename = g_build_filename(context->m_user_dir, USER_BIGRAM, NULL);
- context->m_user_bigram->attach(filename, ATTACH_READWRITE|ATTACH_CREATE);
+ context->m_user_bigram->load_db(filename);
g_free(filename);
gfloat lambda = context->m_system_table_info.get_lambda();
@@ -832,13 +832,53 @@ bool pinyin_save(pinyin_context_t * context){
}
/* save user pinyin table */
- context->m_pinyin_table->sync();
+ gchar * tmpfilename = g_build_filename
+ (context->m_user_dir, USER_PINYIN_INDEX ".tmp", NULL);
+ unlink(tmpfilename);
+ gchar * filename = g_build_filename
+ (context->m_user_dir, USER_PINYIN_INDEX, NULL);
+
+ context->m_pinyin_table->store(tmpfilename);
+
+ int result = rename(tmpfilename, filename);
+ if (0 != result)
+ fprintf(stderr, "rename %s to %s failed.\n",
+ tmpfilename, filename);
+
+ g_free(tmpfilename);
+ g_free(filename);
/* save user phrase table */
- context->m_phrase_table->sync();
+ tmpfilename = g_build_filename
+ (context->m_user_dir, USER_PHRASE_INDEX ".tmp", NULL);
+ unlink(tmpfilename);
+ filename = g_build_filename
+ (context->m_user_dir, USER_PHRASE_INDEX, NULL);
+
+ context->m_phrase_table->store(tmpfilename);
+
+ result = rename(tmpfilename, filename);
+ if (0 != result)
+ fprintf(stderr, "rename %s to %s failed.\n",
+ tmpfilename, filename);
+
+ g_free(tmpfilename);
+ g_free(filename);
/* save user bi-gram */
- context->m_user_bigram->sync();
+ tmpfilename = g_build_filename
+ (context->m_user_dir, USER_BIGRAM ".tmp", NULL);
+ unlink(tmpfilename);
+ filename = g_build_filename(context->m_user_dir, USER_BIGRAM, NULL);
+ context->m_user_bigram->save_db(tmpfilename);
+
+ result = rename(tmpfilename, filename);
+ if (0 != result)
+ fprintf(stderr, "rename %s to %s failed.\n",
+ tmpfilename, filename);
+
+ g_free(tmpfilename);
+ g_free(filename);
mark_version(context);
diff --git a/src/zhuyin.cpp b/src/zhuyin.cpp
index 43745fa..cb3ce70 100644
--- a/src/zhuyin.cpp
+++ b/src/zhuyin.cpp
@@ -340,7 +340,7 @@ zhuyin_context_t * zhuyin_init(const char * systemdir, const char * userdir){
context->m_user_bigram = new Bigram;
filename = g_build_filename(context->m_user_dir, USER_BIGRAM, NULL);
- context->m_user_bigram->attach(filename, ATTACH_READWRITE|ATTACH_CREATE);
+ context->m_user_bigram->load_db(filename);
g_free(filename);
gfloat lambda = context->m_system_table_info.get_lambda();
@@ -644,13 +644,53 @@ bool zhuyin_save(zhuyin_context_t * context){
}
/* save user pinyin table */
- context->m_pinyin_table->sync();
+ gchar * tmpfilename = g_build_filename
+ (context->m_user_dir, USER_PINYIN_INDEX ".tmp", NULL);
+ unlink(tmpfilename);
+ gchar * filename = g_build_filename
+ (context->m_user_dir, USER_PINYIN_INDEX, NULL);
+
+ context->m_pinyin_table->store(tmpfilename);
+
+ int result = rename(tmpfilename, filename);
+ if (0 != result)
+ fprintf(stderr, "rename %s to %s failed.\n",
+ tmpfilename, filename);
+
+ g_free(tmpfilename);
+ g_free(filename);
/* save user phrase table */
- context->m_phrase_table->sync();
+ tmpfilename = g_build_filename
+ (context->m_user_dir, USER_PHRASE_INDEX ".tmp", NULL);
+ unlink(tmpfilename);
+ filename = g_build_filename
+ (context->m_user_dir, USER_PHRASE_INDEX, NULL);
+
+ context->m_phrase_table->store(tmpfilename);
+
+ result = rename(tmpfilename, filename);
+ if (0 != result)
+ fprintf(stderr, "rename %s to %s failed.\n",
+ tmpfilename, filename);
+
+ g_free(tmpfilename);
+ g_free(filename);
/* save user bi-gram */
- context->m_user_bigram->sync();
+ tmpfilename = g_build_filename
+ (context->m_user_dir, USER_BIGRAM ".tmp", NULL);
+ unlink(tmpfilename);
+ filename = g_build_filename(context->m_user_dir, USER_BIGRAM, NULL);
+ context->m_user_bigram->save_db(tmpfilename);
+
+ result = rename(tmpfilename, filename);
+ if (0 != result)
+ fprintf(stderr, "rename %s to %s failed.\n",
+ tmpfilename, filename);
+
+ g_free(tmpfilename);
+ g_free(filename);
mark_version(context);