add get_shengmu/yunmu_string
[platform/upstream/libpinyin.git] / src / storage / facade_phrase_table2.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 #ifndef FACADE_PHRASE_TABLE2_H
23 #define FACADE_PHRASE_TABLE2_H
24
25 #include "phrase_large_table2.h"
26
27 namespace pinyin{
28
29 /**
30  * FacadePhraseTable2:
31  *
32  * The facade class of phrase large table2.
33  *
34  */
35
36 class FacadePhraseTable2{
37 private:
38     PhraseLargeTable2 * m_system_phrase_table;
39     PhraseLargeTable2 * m_user_phrase_table;
40
41     void reset(){
42         if (m_system_phrase_table) {
43             delete m_system_phrase_table;
44             m_system_phrase_table = NULL;
45         }
46
47         if (m_user_phrase_table) {
48             delete m_user_phrase_table;
49             m_user_phrase_table = NULL;
50         }
51     }
52
53 public:
54     /**
55      * FacadePhraseTable2::FacadePhraseTable2:
56      *
57      * The constructor of the FacadePhraseTable2.
58      *
59      */
60     FacadePhraseTable2() {
61         m_system_phrase_table = NULL;
62         m_user_phrase_table = NULL;
63     }
64
65     /**
66      * FacadePhraseTable2::~FacadePhraseTable2:
67      *
68      * The destructor of the FacadePhraseTable2.
69      *
70      */
71     ~FacadePhraseTable2() {
72         reset();
73     }
74
75     /**
76      * FacadePhraseTable2::load:
77      * @system: the memory chunk of the system phrase table.
78      * @user: the memory chunk of the user phrase table.
79      * @returns: whether the load operation is successful.
80      *
81      * Load the system or user phrase table from the memory chunks.
82      *
83      */
84     bool load(MemoryChunk * system, MemoryChunk * user) {
85         reset();
86
87         bool result = false;
88         if (system) {
89             m_system_phrase_table = new PhraseLargeTable2;
90             result = m_system_phrase_table->load(system) || result;
91         }
92         if (user) {
93             m_user_phrase_table = new PhraseLargeTable2;
94             result = m_user_phrase_table->load(user) || result;
95         }
96         return result;
97     }
98
99     /**
100      * FacadePhraseTable2::store:
101      * @new_user: the memory chunk to store the user phrase table.
102      * @returns: whether the store operation is successful.
103      *
104      * Store the user phrase table to the memory chunk.
105      *
106      */
107     bool store(MemoryChunk * new_user) {
108         if (NULL == m_user_phrase_table)
109             return false;
110         return m_user_phrase_table->store(new_user);
111     }
112
113     /**
114      * FacadePhraseTable2::search:
115      * @phrase_length: the length of the phrase to be searched.
116      * @phrase: the ucs4 characters of the phrase to be searched.
117      * @tokens: the GArray of tokens to store the matched phrases.
118      * @returns: the search result of enum SearchResult.
119      *
120      * Search the phrase tokens according to the ucs4 characters.
121      *
122      */
123     int search(int phrase_length, /* in */ ucs4_t phrase[],
124                /* out */ PhraseTokens tokens) const {
125         /* clear tokens. */
126         for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
127             if (tokens[i])
128                 g_array_set_size(tokens[i], 0);
129         }
130
131         int result = SEARCH_NONE;
132
133         if (NULL != m_system_phrase_table)
134             result |= m_system_phrase_table->search
135                 (phrase_length, phrase, tokens);
136
137         if (NULL != m_user_phrase_table)
138             result |= m_user_phrase_table->search
139                 (phrase_length, phrase, tokens);
140
141         return result;
142     }
143
144     /**
145      * FacadePhraseTable2::add_index:
146      * @phrase_length: the length of the phrase to be added.
147      * @phrase: the ucs4 characters of the phrase to be added.
148      * @token: the token of the phrase to be added.
149      * @returns: the add result of enum ErrorResult.
150      *
151      * Add the phrase token to the user phrase table.
152      *
153      */
154     int add_index(int phrase_length, /* in */ ucs4_t phrase[],
155                   /* in */ phrase_token_t token) {
156         if (NULL == m_user_phrase_table)
157             return ERROR_NO_USER_TABLE;
158
159         return m_user_phrase_table->add_index
160             (phrase_length, phrase, token);
161     }
162
163     /**
164      * FacadePhraseTable2::remove_index:
165      * @phrase_length: the length of the phrase to be removed.
166      * @phrase: the ucs4 characters of the phrase to be removed.
167      * @token: the token of the phrase to be removed.
168      * @returns: the remove result of enum ErrorResult.
169      *
170      * Remove the phrase token from the user phrase table.
171      *
172      */
173     int remove_index(int phrase_length, /* in */ ucs4_t phrase[],
174                      /* in */ phrase_token_t token) {
175         if (NULL == m_user_phrase_table)
176             return ERROR_NO_USER_TABLE;
177
178         return m_user_phrase_table->remove_index
179             (phrase_length, phrase, token);
180     }
181
182     /**
183      * FacadePhraseTable2::mask_out:
184      * @mask: the mask.
185      * @value: the value.
186      * @returns: whether the mask out operation is successful.
187      *
188      * Mask out the matched phrase index.
189      *
190      */
191     bool mask_out(phrase_token_t mask, phrase_token_t value) {
192         if (NULL == m_user_phrase_table)
193             return false;
194
195         return m_user_phrase_table->mask_out
196             (mask, value);
197     }
198 };
199
200 };
201
202
203 #endif