write facade_phrase_table2.h
[platform/upstream/libpinyin.git] / src / storage / facade_chewing_table.h
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2011 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_CHEWING_TABLE_H
23 #define FACADE_CHEWING_TABLE_H
24
25 #include "novel_types.h"
26 #include "chewing_large_table.h"
27
28 namespace pinyin{
29
30 /**
31  * FacadeChewingTable:
32  *
33  * The facade class of chewing large table.
34  *
35  */
36
37 class FacadeChewingTable{
38 private:
39     ChewingLargeTable * m_system_chewing_table;
40     ChewingLargeTable * m_user_chewing_table;
41
42 public:
43     /**
44      * FacadeChewingTable::FacadeChewingTable:
45      *
46      * The constructor of the FacadeChewingTable.
47      *
48      */
49     FacadeChewingTable() {
50         m_system_chewing_table = NULL;
51         m_user_chewing_table = NULL;
52     }
53
54     /**
55      * FacadeChewingTable::set_options:
56      * @options: the pinyin options.
57      * @returns: whether the setting options is successful.
58      *
59      * Set the options of the system and user chewing table.
60      *
61      */
62     bool set_options(pinyin_option_t options) {
63         bool result = false;
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;
68         return result;
69     }
70
71     /**
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.
77      *
78      * Load the system or user chewing table from the memory chunks.
79      *
80      */
81     bool load(pinyin_option_t options, MemoryChunk * system,
82               MemoryChunk * user){
83         bool result = false;
84         if (system) {
85             m_system_chewing_table = new ChewingLargeTable(options);
86             result = m_system_chewing_table->load(system) || result;
87         }
88         if (user) {
89             m_user_chewing_table = new ChewingLargeTable(options);
90             result = m_user_chewing_table->load(user) || result;
91         }
92         return result;
93     }
94
95     /**
96      * FacadeChewingTable::store:
97      * @new_user: the memory chunk to store the user chewing table.
98      * @returns: whether the store operation is successful.
99      *
100      * Store the user chewing table to the memory chunk.
101      *
102      */
103     bool store(MemoryChunk * new_user) {
104         if (NULL == m_user_chewing_table)
105             return false;
106         return m_user_chewing_table->store(new_user);
107     }
108
109     /**
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.
115      *
116      * Search the phrase tokens according to the pinyin keys.
117      *
118      */
119     int search(int phrase_length, /* in */ ChewingKey keys[],
120                /* out */ PhraseIndexRanges ranges) const {
121
122         /* clear ranges. */
123         for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
124             if (ranges[i])
125                 g_array_set_size(ranges[i], 0);
126         }
127
128         int result = SEARCH_NONE;
129
130         if (NULL != m_system_chewing_table)
131             result |= m_system_chewing_table->search
132                 (phrase_length, keys, ranges);
133
134         if (NULL != m_user_chewing_table)
135             result |= m_user_chewing_table->search
136                 (phrase_length, keys, ranges);
137
138         return result;
139     }
140
141     /**
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.
147      *
148      * Add the phrase token to the user chewing table.
149      *
150      */
151     int add_index(int phrase_length, /* in */ ChewingKey keys[],
152                   /* in */ phrase_token_t token) {
153         if (NULL == m_user_chewing_table)
154             return false;
155         return m_user_chewing_table->add_index(phrase_length, keys, token);
156     }
157
158     /**
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.
164      *
165      * Remove the phrase token from the user chewing table.
166      *
167      */
168     int remove_index(int phrase_length, /* in */ ChewingKey keys[],
169                      /* in */ phrase_token_t token) {
170         if (NULL == m_user_chewing_table)
171             return false;
172         return m_user_chewing_table->remove_index(phrase_length, keys, token);
173     }
174 };
175
176 };
177
178 #endif