diff options
author | Peng Wu <alexepico@gmail.com> | 2011-12-20 12:50:43 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2011-12-20 13:01:13 +0800 |
commit | 29f9a74d45deffa61a58c6395418bdff79979996 (patch) | |
tree | 722caaa6fb59e534dc995c15591c9bf947717a5f | |
parent | 66c80265f40d48519caba75e6e37a5e4ac9b83cd (diff) | |
download | libpinyin-29f9a74d45deffa61a58c6395418bdff79979996.tar.gz libpinyin-29f9a74d45deffa61a58c6395418bdff79979996.tar.xz libpinyin-29f9a74d45deffa61a58c6395418bdff79979996.zip |
add format version check
-rw-r--r-- | src/pinyin.cpp | 52 | ||||
-rw-r--r-- | src/pinyin.h | 7 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/pinyin.cpp b/src/pinyin.cpp index 0fee2a8..87f61fc 100644 --- a/src/pinyin.cpp +++ b/src/pinyin.cpp @@ -21,6 +21,7 @@ #include "pinyin.h" +#include <glib/gstdio.h> #include "pinyin_internal.h" /* a glue layer for input method integration. */ @@ -47,6 +48,53 @@ struct _pinyin_context_t{ }; +static bool check_format(const char * userdir){ + gchar * filename = g_build_filename + (userdir, "version", NULL); + + MemoryChunk chunk; + bool exists = chunk.load(filename); + + if (exists) { + exists = (0 == memcmp + (LIBPINYIN_FORMAT_VERSION, chunk.begin(), + strlen(LIBPINYIN_FORMAT_VERSION) + 1)); + } + g_free(filename); + + if (exists) + return exists; + + /* clean up files, if version mis-matches. */ + filename = g_build_filename + (userdir, "gb_char.dbin", NULL); + g_unlink(filename); + g_free(filename); + + filename = g_build_filename + (userdir, "gbk_char.dbin", NULL); + g_unlink(filename); + g_free(filename); + + filename = g_build_filename + (userdir, "user.db", NULL); + g_unlink(filename); + g_free(filename); + + return exists; +} + +static bool mark_version(const char * userdir){ + gchar * filename = g_build_filename + (userdir, "version", NULL); + MemoryChunk chunk; + chunk.set_content(0, LIBPINYIN_FORMAT_VERSION, + strlen(LIBPINYIN_FORMAT_VERSION) + 1); + bool retval = chunk.save(filename); + g_free(filename); + return retval; +} + pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir){ pinyin_context_t * context = new pinyin_context_t; @@ -56,6 +104,8 @@ pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir){ context->m_user_dir = g_strdup(userdir); context->m_modified = false; + check_format(context->m_user_dir); + context->m_pinyin_table = new ChewingLargeTable(context->m_options); MemoryChunk * chunk = new MemoryChunk; gchar * filename = g_build_filename @@ -168,6 +218,8 @@ bool pinyin_save(pinyin_context_t * context){ context->m_user_bigram->save_db(filename); g_free(filename); + mark_version(context->m_user_dir); + context->m_modified = false; return true; } diff --git a/src/pinyin.h b/src/pinyin.h index b85ea53..7bed656 100644 --- a/src/pinyin.h +++ b/src/pinyin.h @@ -101,6 +101,13 @@ bool pinyin_translate_token(pinyin_instance_t * instance, bool pinyin_train(pinyin_instance_t * instance); bool pinyin_reset(pinyin_instance_t * instance); + +/* hack here. */ +typedef ChewingKey PinyinKey; +typedef ChewingKeyRest PinyinKeyPos; + +#define LIBPINYIN_FORMAT_VERSION "0.4.91" + }; #endif |