From 29f9a74d45deffa61a58c6395418bdff79979996 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 20 Dec 2011 12:50:43 +0800 Subject: add format version check --- src/pinyin.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pinyin.h | 7 +++++++ 2 files changed, 59 insertions(+) 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 #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 -- cgit