Debian Codegraph @

KasumiDic.cxx

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*********************************************************************
2 *
3 * KasumiDic.cxx
4 *
5 * Kasumi - a management tool for a private dictionary of anthy
6 *
7 * Copyright (C) 2004-2006 Takashi Nakamoto
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
23 *
24 *********************************************************************/
25
26 #include "KasumiDic.hxx"
27 #include "KasumiWord.hxx"
28 #include "KasumiString.hxx"
29 #include "KasumiException.hxx"
30 #include "KasumiConfiguration.hxx"
31 extern "C"{ // ad-hoc solution for a defect of Anthy
32 #include <anthy/dicutil.h>
33 }
34 #include <anthy/anthy.h>
35 #include <stdio.h>
36 #include <cstdlib>
37 #include <iostream>
38 #include <fstream>
39 #include <sstream>
40 #include <string>
41 #include <vector>
42 #include <cassert>
43
44 using namespace std;
45
46 #define OptionOutput( Word, OptionName ) (string(OptionName) + " = " + (Word->getOption(OptionName) ? "y" : "n"))
47 #define BUFFER_SIZE (255)
48
49 KasumiDic::KasumiDic(KasumiConfiguration *conf)
50 throw(KasumiException){
51
52 try{
53 load(conf);
54 }catch(KasumiException e){
55 throw e;
56 }
57 }
58
59 KasumiDic::~KasumiDic()
60 {
61 list<KasumiWord*>::iterator p;
62
63 while(!mWordList.empty())
64 {
65 p = mWordList.begin();
66 mWordList.pop_front();
67 delete (*p);
68 }
69 }
70
71 void KasumiDic::load(KasumiConfiguration *conf)
72 throw(KasumiException){
73
74 const int FREQ_LBOUND = conf->getPropertyValueByInt("MinFrequency");
75 const int FREQ_UBOUND = conf->getPropertyValueByInt("MaxFrequency");
76
•77 // "anthy" on Debian uses forked upstream source based on 9100h-25
78 // with its upstream version numbered as 0.3 on buster/sid as of 2018/July
79 // while handling version order issue with "epoch" trick.
80 //
81 // Debian anthy packages as of 2018/July
82 // o-o-stable: 9100h-16 wheezy
83 // oldstable: 9100h-24 jessie
84 // stable: 9100h-25 stretch
85 // testing: 1:0.3-7 buster
86 // unstable: 1:0.3-7 sid
87 //
88 // "kasumi" assumes version number is like 9100 and address issues with very
89 // old version before 7716.
90 // When conversting version string to number, "kasumi" assumes it is an
91 // integer which can be conversted by atoi(). Also, "kasumi" performed
92 // assertion operation to check this atoi() produced real number other than
93 // 0. With the recent upstream version of Debian forked source, version 0.3
94 // becomes 0 and its assertion fails as reported on BTS: #885918
95 // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=885918
96 //
97 // Since pre-7716 version of "anthy" is non-existing, it is most simple to
98 // remove code addressing re-7716 version of "anthy". In order to keep this
99 // somewhat invasive change, I keep the pertinent lines commented out by this
100 // patch.
101
102 try{
103 if(anthy_priv_dic_select_first_entry() == -1){
104 // no word
105 return;
106 }
107 else if(anthy_priv_dic_select_first_entry() == -3)
108 {
•109 // always through exception since anthy is post 7716 version.
110 string message = string("Failed to read private dictionary. This problem might be a problem of Anthy.\n");
111 throw KasumiException(message, STDERR, KILL);
112 }
113
114 char sound[BUFFER_SIZE], wt[BUFFER_SIZE], spelling[BUFFER_SIZE];
115 int freq;
116
117 do{
118 if (anthy_priv_dic_get_index(sound, BUFFER_SIZE) &&
119 anthy_priv_dic_get_wtype(wt, BUFFER_SIZE) &&
120 anthy_priv_dic_get_word(spelling, BUFFER_SIZE)) {
121 freq = anthy_priv_dic_get_freq();
122
123 // corret frequency value crossing the bounds
124 if(FREQ_LBOUND > freq)
125 freq = FREQ_LBOUND;
126 if(FREQ_UBOUND < freq)
127 freq = FREQ_UBOUND;
128
129 KasumiWord *newWord = KasumiWord::createNewWord(conf);
130
131 newWord->setSound(string(sound));
•132 // anthy is post 7710 version.
133 newWord->setSpelling(string(spelling));
134 newWord->setFrequency(freq);
135 newWord->setWordType(KasumiWordType::getWordTypeFromCannaTab(string(wt)));
136 appendWord(newWord);
137 }
138 }while(anthy_priv_dic_select_next_entry() == 0);
139 }catch(KasumiException e){
140 throw e;
141 }
142 }
143
144 void KasumiDic::appendWord(KasumiWord *word){
145 // check duplication
146 list<KasumiWord*>::iterator p = mWordList.begin();
147 while(p != mWordList.end() )
148 {
149 if((*p)->getID() == word->getID())
150 return; // nothing to do
151 p++;
152 }
153
154 word->registerEventListener(this);
155 mWordList.push_back(word);
156
157 // raise event
158 for(size_t i=0;i<EventListeners.size();i++){
159 EventListeners[i]->appendedWord(word);
160 }
161 }
162
163 void KasumiDic::removeWord(unsigned int id)
164 {
165 int flag = 0;
166
167 list<KasumiWord*>::iterator p = mWordList.begin();
168 while(p != mWordList.end() )
169 {
170 if((*p)->getID() == id)
171 {
172 mWordList.erase(p);
173 (*p)->removeEventListener(this);
174 free(*p);
175 flag = 1;
176 break;
177 }
178 p++;
179 }
180
181 // raise event
182 if(flag)
183 for(size_t i=0;i<EventListeners.size();i++){
184 EventListeners[i]->removedWord(id);
185 }
186 }
187
188 void KasumiDic::store()
189 throw(KasumiException)
190 {
191 list<KasumiWord*>::iterator p = mWordList.begin();
192
193 anthy_priv_dic_delete();
194
195 while( p != mWordList.end() )
196 {
197 string spelling = (*p)->getSpelling();
198 string sound = (*p)->getSound();
199 string wt = (*p)->getWordType()->getCannaTab();
200 int freq = (*p)->getFrequency();
201
202 int ret = anthy_priv_dic_add_entry(sound.c_str(),
203 spelling.c_str(),
204 wt.c_str(),
205 freq);
206
207 if (ret == -1)
208 {
209 throw KasumiException(string("Failed to register") + sound,
210 ERR_DIALOG,
211 KILL);
212 }
213
214 p++;
215 }
216 }
217
218 void KasumiDic::registerEventListener(KasumiDicEventListener *listener){
219 int i,size;
220
221 size = EventListeners.size();
222
223 // if the listener have been already registered, nothing to do
224 // assuring no duplication
225 for(i=0;i<size;i++){
226 if(EventListeners[i] == listener){
227 return;
228 }
229 }
230
231 EventListeners.push_back(listener);
232 }
233
234 void KasumiDic::removeEventListener(KasumiDicEventListener *listener){
235 vector<KasumiDicEventListener*>::iterator i;
236 KasumiDicEventListener *p;
237
238 for(i=EventListeners.begin();i!=EventListeners.end();i++){
239 p = *i;
240 if(p == listener){
241 EventListeners.erase(i);
242 return;
243 }
244 }
245 }
246
247 void KasumiDic::changedFrequency(KasumiWord *word)
248 {
249 for(size_t i=0;i<EventListeners.size();i++){
250 EventListeners[i]->modifiedWord(word);
251 }
252 }
253
254 void KasumiDic::changedSpelling(KasumiWord *word)
255 {
256 for(size_t i=0;i<EventListeners.size();i++){
257 EventListeners[i]->modifiedWord(word);
258 }
259 }
260
261 void KasumiDic::changedSound(KasumiWord *word)
262 {
263 for(size_t i=0;i<EventListeners.size();i++){
264 EventListeners[i]->modifiedWord(word);
265 }
266 }
267
268 void KasumiDic::changedWordType(KasumiWord *word)
269 {
270 for(size_t i=0;i<EventListeners.size();i++){
271 EventListeners[i]->modifiedWord(word);
272 }
273 }
274
275 /*
276 // for debug
277 //
278 // % g++ -g KasumiDic.cxx KasumiWord.cxx KasumiException.cxx KasumiConfiguration.cxx KasumiWordType.cxx KasumiString.cxx `pkg-config --libs --cflags anthy gtk+-2.0`
279 void output(KasumiDic *dic)
280 {
281 list<KasumiWord*>::iterator p = dic->firstWordIter();
282 while(p != dic->endWordIter()){
283 cout << (*p)->getID() << " " << (*p)->getSpelling() << endl;
284 p++;
285 }
286 }
287
288 int main(int argc, char *argv[])
289 {
290 anthy_dic_util_init();
291
292 KasumiWordType::initWordTypeList();
293 KasumiConfiguration *conf = new KasumiConfiguration(argc, argv);
294 KasumiDic *dic = new KasumiDic(conf);
295 KasumiWord *word = KasumiWord::createNewWord(conf);
296 word->setSpellingByUTF8("テスト");
297 word->setSoundByUTF8("てすと");
298 dic->appendWord(word);
299 output(dic);
300
301 while(1);
302
303 dic->removeWord(1);
304 output(dic);
305 }
306
307 */