summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2015-05-21 13:12:49 +0800
committerPeng Wu <alexepico@gmail.com>2015-05-21 13:12:49 +0800
commit2fd097b32236f156aa18e7ee077ef34dc36d2d52 (patch)
tree50f9f169e51d726ae5deac667c3efc81487e186e
parentfad72be7fbcc4f52bfc1ce28c2caaa32ac5f9188 (diff)
downloadlibpinyin-2fd097b32236f156aa18e7ee077ef34dc36d2d52.tar.gz
libpinyin-2fd097b32236f156aa18e7ee077ef34dc36d2d52.tar.xz
libpinyin-2fd097b32236f156aa18e7ee077ef34dc36d2d52.zip
re-factor table info
-rw-r--r--src/include/novel_types.h5
-rw-r--r--src/storage/table_info.cpp44
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
@@ -143,6 +143,11 @@ typedef GArray * CandidateConstraints;
typedef guint32 pinyin_option_t;
typedef enum {
+ DEFAULT_TABLE,
+ ADDON_TABLE,
+} TABLE_TARGET;
+
+typedef enum {
/* for default tables. */
RESERVED = 0,
GB_DICTIONARY = 1,
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);