From 2fd097b32236f156aa18e7ee077ef34dc36d2d52 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 21 May 2015 13:12:49 +0800 Subject: re-factor table info --- src/include/novel_types.h | 5 +++++ src/storage/table_info.cpp | 44 +++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/include/novel_types.h b/src/include/novel_types.h index 7c545be..8005ed0 100644 --- a/src/include/novel_types.h +++ b/src/include/novel_types.h @@ -142,6 +142,11 @@ typedef GArray * CandidateConstraints; typedef guint32 pinyin_option_t; +typedef enum { + DEFAULT_TABLE, + ADDON_TABLE, +} TABLE_TARGET; + typedef enum { /* for default tables. */ RESERVED = 0, diff --git a/src/storage/table_info.cpp b/src/storage/table_info.cpp index 0e58680..3d57e15 100644 --- a/src/storage/table_info.cpp +++ b/src/storage/table_info.cpp @@ -102,10 +102,31 @@ void SystemTableInfo2::reset() { } #define HANDLE(x) do { \ - if (0 == strcmp(str, #x)) \ + if (0 == strcmp(#x, str)) \ return x; \ } while (0) + +static TABLE_PHONETIC_TYPE to_table_phonetic_type(const char * str) { + if (0 == strcmp("pinyin", str)) + return PINYIN_TABLE; + + if (0 == strcmp("zhuyin", str)) + return ZHUYIN_TABLE; + + assert(FALSE); +} + +static TABLE_TARGET to_table_target(const char * str) { + if (0 == strcmp("default", str)) + return DEFAULT_TABLE; + + if (0 == strcmp("addon", str)) + return ADDON_TABLE; + + assert(FALSE); +} + static guint8 to_index_of_default_tables(const char * str) { HANDLE(RESERVED); HANDLE(GB_DICTIONARY); @@ -119,8 +140,7 @@ static guint8 to_index_of_default_tables(const char * str) { } static gchar * to_string(const char * str) { - if (0 == strcmp(str, "NULL")) - return NULL; + HANDLE(NULL); return g_strdup(str); } @@ -172,12 +192,7 @@ bool SystemTableInfo2::load(const char * filename) { TABLE_PHONETIC_TYPE type = PINYIN_TABLE; char * str = NULL; num = fscanf(input, "source table format:%ms", &str); - if (0 == strcmp("pinyin", str)) - type = PINYIN_TABLE; - else if (0 == strcmp("zhuyin", str)) - type = ZHUYIN_TABLE; - else - assert(FALSE); + type = to_table_phonetic_type(str); free(str); #if 0 @@ -206,14 +221,17 @@ bool SystemTableInfo2::load(const char * filename) { /* decode the table info and the index. */ pinyin_table_info_t * tables = NULL; - if (0 == strcmp("default", tableinfo)) { + TABLE_TARGET target = to_table_target(tableinfo); + + if (DEFAULT_TABLE == target) { tables = m_default_tables; index = to_index_of_default_tables(dictstr); - } else if (0 == strcmp("addon", tableinfo)) { + } + + if (ADDON_TABLE == target) { tables = m_addon_tables; index = atoi(dictstr); - } else - assert(FALSE); + } assert(0 <= index && index < PHRASE_INDEX_LIBRARY_COUNT); -- cgit