support two letter yunmu from full pinyin
[platform/upstream/libpinyin.git] / utils / utils_helper.h
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2012 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
23 #ifndef UTILS_HELPER_H
24 #define UTILS_HELPER_H
25
26
27 #define TAGLIB_GET_TOKEN(var, index)                                    \
28     phrase_token_t var = null_token;                                    \
29     {                                                                   \
30         const char * string = (const char *) g_ptr_array_index          \
31             (values, index);                                            \
32         var = atoi(string);                                             \
33     }
34
35 #define TAGLIB_GET_PHRASE_STRING(var, index)                            \
36     const char * var = NULL;                                            \
37     {                                                                   \
38         var = (const char *) g_ptr_array_index                          \
39             (values, index);                                            \
40     }
41
42 #define TAGLIB_GET_TAGVALUE(type, var, conv)                            \
43     type var;                                                           \
44     {                                                                   \
45         gpointer value = NULL;                                          \
46         assert(g_hash_table_lookup_extended                             \
47                (required, #var, NULL, &value));                         \
48         var = conv((const char *)value);                                \
49     }
50
51 #define TAGLIB_PARSE_SEGMENTED_LINE(phrase_index, var, line)            \
52     phrase_token_t var = null_token;                                    \
53     do {                                                                \
54         if (0 == strlen(line))                                          \
55             break;                                                      \
56                                                                         \
57         gchar ** strs = g_strsplit_set(line, " \t", 2);                 \
58         if (2 != g_strv_length(strs))                                   \
59             assert(false);                                              \
60                                                                         \
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));                     \
66                                                                         \
67         var = _token;                                                   \
68                                                                         \
69         g_strfreev(strs);                                               \
70     } while(false);
71
72
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;
78
79         if (SYSTEM_FILE != table_info->m_file_type)
80             continue;
81
82         const char * binfile = table_info->m_system_filename;
83
84         chunk = new MemoryChunk;
85         bool retval = chunk->load(binfile);
86         if (!retval) {
87             fprintf(stderr, "load %s failed!\n", binfile);
88             return false;
89         }
90
91         phrase_index->load(i, chunk);
92     }
93     return true;
94 }
95
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;
101
102         if (SYSTEM_FILE != table_info->m_file_type)
103             continue;
104
105         const char * binfile = table_info->m_system_filename;
106
107         new_chunk = new MemoryChunk;
108         phrase_index->store(i, new_chunk);
109         bool retval = new_chunk->save(binfile);
110         if (!retval) {
111             fprintf(stderr, "save %s failed.", binfile);
112             return false;
113         }
114
115         phrase_index->load(i, new_chunk);
116     }
117     return true;
118 }
119
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;
125
126         if (DICTIONARY != table_info->m_file_type)
127             continue;
128
129         const char * binfile = table_info->m_system_filename;
130
131         new_chunk = new MemoryChunk;
132         phrase_index->store(i, new_chunk);
133         bool retval = new_chunk->save(binfile);
134         if (!retval) {
135             fprintf(stderr, "save %s failed.", binfile);
136             return false;
137         }
138
139         phrase_index->load(i, new_chunk);
140     }
141     return true;
142 }
143
144 #endif