3 * Library to deal with pinyin.
5 * Copyright (C) 2013 Peng Wu <alexepico@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "table_info.h"
26 using namespace pinyin;
29 static const pinyin_table_info_t reserved_tables[] = {
30 {RESERVED, NULL, NULL, NULL, NOT_USED},
31 {GB_DICTIONARY, "gb_char.table", "gb_char.bin", "gb_char.dbin", SYSTEM_FILE},
32 {GBK_DICTIONARY, "gbk_char.table", "gbk_char.bin", "gbk_char.dbin", SYSTEM_FILE},
34 {MERGED_DICTIONARY, "merged.table", "merged.bin", "merged.dbin", SYSTEM_FILE},
36 {USER_DICTIONARY, NULL, NULL, "user.bin", USER_FILE}
40 SystemTableInfo::SystemTableInfo() {
41 m_binary_format_version = 0;
42 m_model_data_version = 0;
46 for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
47 pinyin_table_info_t * tableinfo = &m_table_info[i];
49 tableinfo->m_dict_index = i;
50 tableinfo->m_table_filename = NULL;
51 tableinfo->m_system_filename = NULL;
52 tableinfo->m_user_filename = NULL;
53 tableinfo->m_file_type = NOT_USED;
57 SystemTableInfo::~SystemTableInfo() {
61 void SystemTableInfo::reset() {
62 m_binary_format_version = 0;
63 m_model_data_version = 0;
67 for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
68 pinyin_table_info_t * tableinfo = &m_table_info[i];
70 g_free(tableinfo->m_table_filename);
71 tableinfo->m_table_filename = NULL;
72 g_free(tableinfo->m_system_filename);
73 tableinfo->m_system_filename = NULL;
74 g_free(tableinfo->m_user_filename);
75 tableinfo->m_user_filename = NULL;
76 tableinfo->m_file_type = NOT_USED;
80 void SystemTableInfo::postfix_tables() {
82 for (i = 0; i < G_N_ELEMENTS(reserved_tables); ++i) {
83 pinyin_table_info_t * postfix = &reserved_tables[i];
85 guint8 index = postfix->m_dict_index;
86 pinyin_table_info_t * tableinfo = &m_table_info[index];
87 assert(tableinfo->m_dict_index == index);
89 tableinfo->m_table_filename = g_strdup(postfix->m_table_filename);
90 tableinfo->m_system_filename = g_strdup(postfix->m_system_filename);
91 tableinfo->m_user_filename = g_strdup(postfix->m_user_filename);
92 tableinfo->m_file_type = postfix->m_file_type;
96 static gchar * to_string(const char * str) {
97 if (0 == strcmp(str, "NULL"))
100 return g_strdup(str);
103 static PHRASE_FILE_TYPE to_file_type(const char * str) {
104 #define HANDLE(x) { \
105 if (0 == strcmp(str, #x)) \
119 bool SystemTableInfo::load(const char * filename) {
122 FILE * input = fopen(filename, "r");
124 fprintf(stderr, "open %s failed.\n", filename);
128 int binver = 0, modelver = 0;
131 int num = fscanf(input, "binary format version:%d", &binver);
135 num = fscanf(input, "model data version:%d", &modelver);
139 num = fscanf(input, "lambda parameter:%f", &lambda);
144 printf("binver:%d modelver:%d lambda:%f", binver, modelver, lambda);
147 m_binary_format_version = binver;
148 m_model_data_version = modelver;
152 char tablefile[256], sysfile[256], userfile[256], filetype[256];
153 while (!feof(infile)){
154 num = fscanf("%d %s %s %s %s",
155 &index, tablefile, sysfile, userfile, filetype);
160 if (!(0 <= index && index < PHRASE_INDEX_LIBRARY_COUNT))
163 /* save into m_table_info. */
164 pinyin_table_info_t * tableinfo = &m_table_info[index];
165 assert(index == tableinfo->m_dict_index);
167 table_info->m_table_filename = to_string(tablefile);
168 table_info->m_system_filename = to_string(sysfile);
169 table_info->m_user_filename = to_string(userfile);
171 table_info->m_file_type = to_file_type(filetype);