21f9ea4119c21e7cbc4ca64da1c02685af8f4ce5
[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     bool retval = system_table_info.load("table.conf");
51     if (!retval) {
52         fprintf(stderr, "load table.conf failed.\n");
53         exit(ENOENT);
54     }
55
56     /* generate pinyin index*/
57     pinyin_option_t options = USE_TONE;
58     ChewingLargeTable chewing_table(options);
59     PhraseLargeTable2 phrase_table;
60
61     /* generate phrase index */
62     FacadePhraseIndex phrase_index;
63
64     const pinyin_table_info_t * phrase_files =
65         system_table_info.get_table_info();
66
67     for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
68         const pinyin_table_info_t * table_info = phrase_files + i;
69         assert(table_info->m_dict_index == i);
70
71         if (SYSTEM_FILE != table_info->m_file_type &&
72             DICTIONARY != table_info->m_file_type)
73             continue;
74
75         const char * tablename = table_info->m_table_filename;
76
77         gchar * filename = g_build_filename(table_dir, tablename, NULL);
78         FILE * tablefile = fopen(filename, "r");
79
80         if (NULL == tablefile) {
81             fprintf(stderr, "open %s failed!\n", tablename);
82             exit(ENOENT);
83         }
84
85         chewing_table.load_text(tablefile);
86         fseek(tablefile, 0L, SEEK_SET);
87         phrase_table.load_text(tablefile);
88         fseek(tablefile, 0L, SEEK_SET);
89         phrase_index.load_text(i, tablefile);
90         fclose(tablefile);
91         g_free(filename);
92     }
93
94     MemoryChunk * new_chunk = new MemoryChunk;
95     chewing_table.store(new_chunk);
96     new_chunk->save("pinyin_index.bin");
97     chewing_table.load(new_chunk);
98     
99     new_chunk = new MemoryChunk;
100     phrase_table.store(new_chunk);
101     new_chunk->save("phrase_index.bin");
102     phrase_table.load(new_chunk);
103
104     phrase_index.compact();
105
106     if (!save_phrase_index(phrase_files, &phrase_index))
107         exit(ENOENT);
108
109     if (!save_dictionary(phrase_files, &phrase_index))
110         exit(ENOENT);
111
112     return 0;
113 }