3 * Library to deal with pinyin.
5 * Copyright (C) 2011 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.
22 #ifndef FACADE_CHEWING_TABLE_H
23 #define FACADE_CHEWING_TABLE_H
25 #include "novel_types.h"
26 #include "chewing_large_table.h"
33 * The facade class of chewing large table.
37 class FacadeChewingTable{
39 ChewingLargeTable * m_system_chewing_table;
40 ChewingLargeTable * m_user_chewing_table;
44 * FacadeChewingTable::FacadeChewingTable:
46 * The constructor of the FacadeChewingTable.
49 FacadeChewingTable() {
50 m_system_chewing_table = NULL;
51 m_user_chewing_table = NULL;
55 * FacadeChewingTable::set_options:
56 * @options: the pinyin options.
57 * @returns: whether the setting options is successful.
59 * Set the options of the system and user chewing table.
62 bool set_options(pinyin_option_t options) {
64 if (m_system_chewing_table)
65 result = m_system_chewing_table->set_options(options) || result;
66 if (m_user_chewing_table)
67 result = m_user_chewing_table->set_options(options) || result;
72 * FacadeChewingTable::load:
73 * @options: the pinyin options.
74 * @system: the memory chunk of the system chewing table.
75 * @user: the memory chunk of the user chewing table.
76 * @returns: whether the load operation is successful.
78 * Load the system or user chewing table from the memory chunks.
81 bool load(pinyin_option_t options, MemoryChunk * system,
85 m_system_chewing_table = new ChewingLargeTable(options);
86 result = m_system_chewing_table->load(system) || result;
89 m_user_chewing_table = new ChewingLargeTable(options);
90 result = m_user_chewing_table->load(user) || result;
96 * FacadeChewingTable::store:
97 * @new_user: the memory chunk to store the user chewing table.
98 * @returns: whether the store operation is successful.
100 * Store the user chewing table to the memory chunk.
103 bool store(MemoryChunk * new_user) {
104 if (NULL == m_user_chewing_table)
106 return m_user_chewing_table->store(new_user);
110 * FacadeChewingTable::search:
111 * @phrase_length: the length of the phrase to be searched.
112 * @keys: the pinyin key of the phrase to be searched.
113 * @ranges: the array of GArrays to store the matched phrase token.
114 * @returns: the search result of enum SearchResult.
116 * Search the phrase tokens according to the pinyin keys.
119 int search(int phrase_length, /* in */ ChewingKey keys[],
120 /* out */ PhraseIndexRanges ranges) const {
123 for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
125 g_array_set_size(ranges[i], 0);
128 int result = SEARCH_NONE;
130 if (NULL != m_system_chewing_table)
131 result |= m_system_chewing_table->search
132 (phrase_length, keys, ranges);
134 if (NULL != m_user_chewing_table)
135 result |= m_user_chewing_table->search
136 (phrase_length, keys, ranges);
142 * FacadeChewingTable::add_index:
143 * @phrase_length: the length of the phrase to be added.
144 * @keys: the pinyin keys of the phrase to be added.
145 * @token: the token of the phrase to be added.
146 * @returns: the add result of enum ErrorResult.
148 * Add the phrase token to the user chewing table.
151 int add_index(int phrase_length, /* in */ ChewingKey keys[],
152 /* in */ phrase_token_t token) {
153 if (NULL == m_user_chewing_table)
155 return m_user_chewing_table->add_index(phrase_length, keys, token);
159 * FacadeChewingTable::remove_index:
160 * @phrase_length: the length of the phrase to be removed.
161 * @keys: the pinyin keys of the phrase to be removed.
162 * @token: the token of the phrase to be removed.
163 * @returns: the remove result of enum ErrorResult.
165 * Remove the phrase token from the user chewing table.
168 int remove_index(int phrase_length, /* in */ ChewingKey keys[],
169 /* in */ phrase_token_t token) {
170 if (NULL == m_user_chewing_table)
172 return m_user_chewing_table->remove_index(phrase_length, keys, token);