3 * Library to deal with pinyin.
5 * Copyright (C) 2012 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.
23 #ifndef UTILS_HELPER_H
24 #define UTILS_HELPER_H
27 #define TAGLIB_GET_TOKEN(var, index) \
28 phrase_token_t var = null_token; \
30 const char * string = (const char *) g_ptr_array_index \
35 #define TAGLIB_GET_PHRASE_STRING(var, index) \
36 const char * var = NULL; \
38 var = (const char *) g_ptr_array_index \
42 #define TAGLIB_GET_TAGVALUE(type, var, conv) \
45 gpointer value = NULL; \
46 assert(g_hash_table_lookup_extended \
47 (required, #var, NULL, &value)); \
48 var = conv((const char *)value); \
51 #define TAGLIB_PARSE_SEGMENTED_LINE(phrase_index, var, line) \
52 phrase_token_t var = null_token; \
54 if (0 == strlen(line)) \
57 gchar ** strs = g_strsplit_set(line, " \t", 2); \
58 if (2 != g_strv_length(strs)) \
61 phrase_token_t _token = atoi(strs[0]); \
62 const char * phrase = strs[1]; \
63 if (null_token != _token) \
64 assert(taglib_validate_token_with_string \
65 (phrase_index, _token, phrase)); \
73 static bool load_phrase_index(const pinyin_table_info_t * phrase_files,
74 FacadePhraseIndex * phrase_index) {
75 MemoryChunk * chunk = NULL;
76 for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
77 const pinyin_table_info_t * table_info = phrase_files + i;
79 if (SYSTEM_FILE != table_info->m_file_type)
82 const char * binfile = table_info->m_system_filename;
84 chunk = new MemoryChunk;
85 bool retval = chunk->load(binfile);
87 fprintf(stderr, "load %s failed!\n", binfile);
91 phrase_index->load(i, chunk);
96 static bool save_phrase_index(const pinyin_table_info_t * phrase_files,
97 FacadePhraseIndex * phrase_index) {
98 MemoryChunk * new_chunk = NULL;
99 for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
100 const pinyin_table_info_t * table_info = phrase_files + i;
102 if (SYSTEM_FILE != table_info->m_file_type)
105 const char * binfile = table_info->m_system_filename;
107 new_chunk = new MemoryChunk;
108 phrase_index->store(i, new_chunk);
109 bool retval = new_chunk->save(binfile);
111 fprintf(stderr, "save %s failed.", binfile);
115 phrase_index->load(i, new_chunk);
120 static bool save_dictionary(const pinyin_table_info_t * phrase_files,
121 FacadePhraseIndex * phrase_index) {
122 MemoryChunk * new_chunk = NULL;
123 for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
124 const pinyin_table_info_t * table_info = phrase_files + i;
126 if (DICTIONARY != table_info->m_file_type)
129 const char * binfile = table_info->m_system_filename;
131 new_chunk = new MemoryChunk;
132 phrase_index->store(i, new_chunk);
133 bool retval = new_chunk->save(binfile);
135 fprintf(stderr, "save %s failed.", binfile);
139 phrase_index->load(i, new_chunk);