define filenames
[platform/upstream/libpinyin.git] / src / storage / table_info.cpp
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2013 Peng Wu <alexepico@gmail.com>
6  *  
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.
11  * 
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.
16  *  
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.
20  */
21
22 #include "table_info.h"
23 #include <stdio.h>
24 #include <assert.h>
25 #include <string.h>
26
27 using namespace pinyin;
28
29
30 static const pinyin_table_info_t reserved_tables[] = {
31     {RESERVED, NULL, NULL, NULL, NOT_USED},
32     {GB_DICTIONARY, "gb_char.table", "gb_char.bin", "gb_char.dbin", SYSTEM_FILE},
33     {GBK_DICTIONARY, "gbk_char.table", "gbk_char.bin", "gbk_char.dbin", SYSTEM_FILE},
34
35     {MERGED_DICTIONARY, "merged.table", "merged.bin", "merged.dbin", SYSTEM_FILE},
36
37     {USER_DICTIONARY, NULL, NULL, "user.bin", USER_FILE}
38 };
39
40
41 SystemTableInfo::SystemTableInfo() {
42     m_binary_format_version = 0;
43     m_model_data_version = 0;
44     m_lambda = 0.;
45
46     size_t i;
47     for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
48         pinyin_table_info_t * tableinfo = &m_table_info[i];
49
50         tableinfo->m_dict_index = i;
51         tableinfo->m_table_filename = NULL;
52         tableinfo->m_system_filename = NULL;
53         tableinfo->m_user_filename = NULL;
54         tableinfo->m_file_type = NOT_USED;
55     }
56 }
57
58 SystemTableInfo::~SystemTableInfo() {
59     reset();
60 }
61
62 void SystemTableInfo::reset() {
63     m_binary_format_version = 0;
64     m_model_data_version = 0;
65     m_lambda = 0.;
66
67     size_t i;
68     for (i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
69         pinyin_table_info_t * tableinfo = &m_table_info[i];
70
71         g_free((gchar *)tableinfo->m_table_filename);
72         tableinfo->m_table_filename = NULL;
73         g_free((gchar *)tableinfo->m_system_filename);
74         tableinfo->m_system_filename = NULL;
75         g_free((gchar *)tableinfo->m_user_filename);
76         tableinfo->m_user_filename = NULL;
77
78         tableinfo->m_file_type = NOT_USED;
79     }
80 }
81
82 void SystemTableInfo::postfix_tables() {
83     size_t i;
84     for (i = 0; i < G_N_ELEMENTS(reserved_tables); ++i) {
85         const pinyin_table_info_t * postfix = &reserved_tables[i];
86
87         guint8 index = postfix->m_dict_index;
88         pinyin_table_info_t * tableinfo = &m_table_info[index];
89         assert(tableinfo->m_dict_index == index);
90
91         tableinfo->m_table_filename = g_strdup(postfix->m_table_filename);
92         tableinfo->m_system_filename = g_strdup(postfix->m_system_filename);
93         tableinfo->m_user_filename = g_strdup(postfix->m_user_filename);
94         tableinfo->m_file_type = postfix->m_file_type;
95     }
96 }
97
98 static gchar * to_string(const char * str) {
99     if (0 == strcmp(str, "NULL"))
100         return NULL;
101
102     return g_strdup(str);
103 }
104
105 static PHRASE_FILE_TYPE to_file_type(const char * str) {
106 #define HANDLE(x) {                             \
107         if (0 == strcmp(str, #x))               \
108             return x;                           \
109     }
110
111     HANDLE(NOT_USED);
112     HANDLE(SYSTEM_FILE);
113     HANDLE(DICTIONARY);
114     HANDLE(USER_FILE);
115
116     assert(false);
117
118 #undef HANDLE
119 }
120
121 bool SystemTableInfo::load(const char * filename) {
122     reset();
123
124     FILE * input = fopen(filename, "r");
125     if (NULL == input) {
126         fprintf(stderr, "open %s failed.\n", filename);
127         return false;
128     }
129
130     int binver = 0, modelver = 0;
131     gfloat lambda = 0.;
132
133     int num = fscanf(input, "binary format version:%d", &binver);
134     if (1 != num)
135         return false;
136
137     num = fscanf(input, "model data version:%d", &modelver);
138     if (1 != num)
139         return false;
140
141     num = fscanf(input, "lambda parameter:%f", &lambda);
142     if (1 != num)
143         return false;
144
145 #if 0
146     printf("binver:%d modelver:%d lambda:%f\n", binver, modelver, lambda);
147 #endif
148
149     m_binary_format_version = binver;
150     m_model_data_version = modelver;
151     m_lambda = lambda;
152
153     int index = 0;
154     char tablefile[256], sysfile[256], userfile[256], filetype[256];
155     while (!feof(input)) {
156         num = fscanf(input, "%d %s %s %s %s",
157                      &index, tablefile, sysfile, userfile, filetype);
158
159         if (5 != num)
160             continue;
161
162         if (!(0 <= index && index < PHRASE_INDEX_LIBRARY_COUNT))
163             continue;
164
165         /* save into m_table_info. */
166         pinyin_table_info_t * tableinfo = &m_table_info[index];
167         assert(index == tableinfo->m_dict_index);
168
169         tableinfo->m_table_filename = to_string(tablefile);
170         tableinfo->m_system_filename = to_string(sysfile);
171         tableinfo->m_user_filename = to_string(userfile);
172
173         tableinfo->m_file_type = to_file_type(filetype);
174     }
175
176     fclose(input);
177
178     /* postfix reserved tables. */
179     postfix_tables();
180     return true;
181 }
182
183 const pinyin_table_info_t * SystemTableInfo::get_table_info() {
184     return m_table_info;
185 }
186
187 gfloat SystemTableInfo::get_lambda() {
188     return m_lambda;
189 }
190
191
192 UserTableInfo::UserTableInfo() {
193     m_binary_format_version = 0;
194     m_model_data_version = 0;
195 }
196
197 void UserTableInfo::reset() {
198     m_binary_format_version = 0;
199     m_model_data_version = 0;
200 }
201
202 bool UserTableInfo::load(const char * filename) {
203     reset();
204
205     FILE * input = fopen(filename, "r");
206     if (NULL == input) {
207         fprintf(stderr, "open %s failed.", filename);
208         return false;
209     }
210
211     int binver = 0, modelver = 0;
212
213     int num = fscanf(input, "binary format version:%d", &binver);
214     if (1 != num)
215         return false;
216
217     num = fscanf(input, "model data version:%d", &modelver);
218     if (1 != num)
219         return false;
220
221 #if 0
222     printf("binver:%d modelver:%d\n", binver, modelver);
223 #endif
224
225     m_binary_format_version = binver;
226     m_model_data_version = modelver;
227
228     fclose(input);
229
230     return true;
231 }
232
233 bool UserTableInfo::save(const char * filename) {
234     FILE * output = fopen(filename, "w");
235     if (NULL == output) {
236         fprintf(stderr, "write %s failed.\n", filename);
237         return false;
238     }
239
240     fprintf(output, "binary format version:%d\n", m_binary_format_version);
241     fprintf(output, "model data version:%d\n", m_model_data_version);
242
243     fclose(output);
244
245     return true;
246 }
247
248 bool UserTableInfo::is_conform(const SystemTableInfo * sysinfo) {
249     if (sysinfo->m_binary_format_version != m_binary_format_version)
250         return false;
251
252     if (sysinfo->m_model_data_version != m_model_data_version)
253         return false;
254
255     return true;
256 }
257
258 bool UserTableInfo::make_conform(const SystemTableInfo * sysinfo) {
259     m_binary_format_version = sysinfo->m_binary_format_version;
260     m_model_data_version = sysinfo->m_model_data_version;
261     return true;
262 }