fixes gen_binary_files.cpp
[platform/upstream/libpinyin.git] / utils / storage / gen_binary_files.cpp
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2010 Peng Wu
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 <stdio.h>
23 #include <locale.h>
24 #include "pinyin_internal.h"
25 #include "utils_helper.h"
26
27 static const gchar * table_dir = ".";
28
29 static GOptionEntry entries[] =
30 {
31     {"table-dir", 0, 0, G_OPTION_ARG_FILENAME, &table_dir, "table directory", NULL},
32     {NULL}
33 };
34
35 int main(int argc, char * argv[]){
36     setlocale(LC_ALL, "");
37
38     GError * error = NULL;
39     GOptionContext * context;
40
41     context = g_option_context_new("- generate binary files");
42     g_option_context_add_main_entries(context, entries, NULL);
43     if (!g_option_context_parse(context, &argc, &argv, &error)) {
44         g_print("option parsing failed:%s\n", error->message);
45         exit(EINVAL);
46     }
47
48     SystemTableInfo system_table_info;
49
50     gchar * filename = g_build_filename(table_dir, "table.conf", NULL);
51     bool retval = system_table_info.load(filename);
52     if (!retval) {
53         fprintf(stderr, "load table.conf failed.\n");
54         exit(ENOENT);
55     }
56     g_free(filename);
57
58     /* generate pinyin index*/
59     pinyin_option_t options = USE_TONE;
60     ChewingLargeTable chewing_table(options);
61     PhraseLargeTable2 phrase_table;
62
63     /* generate phrase index */
64     FacadePhraseIndex phrase_index;
65
66     const pinyin_table_info_t * phrase_files =
67         system_table_info.get_table_info();
68
69     for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
70         const pinyin_table_info_t * table_info = phrase_files + i;
71         assert(table_info->m_dict_index == i);
72
73         if (SYSTEM_FILE != table_info->m_file_type &&
74             DICTIONARY != table_info->m_file_type)
75             continue;
76
77         const char * tablename = table_info->m_table_filename;
78
79         filename = g_build_filename(table_dir, tablename, NULL);
80         FILE * tablefile = fopen(filename, "r");
81
82         if (NULL == tablefile) {
83             fprintf(stderr, "open %s failed!\n", tablename);
84             exit(ENOENT);
85         }
86
87         chewing_table.load_text(tablefile);
88         fseek(tablefile, 0L, SEEK_SET);
89         phrase_table.load_text(tablefile);
90         fseek(tablefile, 0L, SEEK_SET);
91         phrase_index.load_text(i, tablefile);
92         fclose(tablefile);
93         g_free(filename);
94     }
95
96     MemoryChunk * new_chunk = new MemoryChunk;
97     chewing_table.store(new_chunk);
98     new_chunk->save("pinyin_index.bin");
99     chewing_table.load(new_chunk);
100     
101     new_chunk = new MemoryChunk;
102     phrase_table.store(new_chunk);
103     new_chunk->save("phrase_index.bin");
104     phrase_table.load(new_chunk);
105
106     phrase_index.compact();
107
108     if (!save_phrase_index(phrase_files, &phrase_index))
109         exit(ENOENT);
110
111     if (!save_dictionary(phrase_files, &phrase_index))
112         exit(ENOENT);
113
114     return 0;
115 }