/src/aspell/modules/speller/default/writable.cpp
Line | Count | Source |
1 | | // This file is part of The New Aspell |
2 | | // Copyright (C) 2000,2011 by Kevin Atkinson under the GNU LGPL |
3 | | // license version 2.0 or 2.1. You should have received a copy of the |
4 | | // LGPL license along with this library if you did not you can find it |
5 | | // at http://www.gnu.org/. |
6 | | |
7 | | #include <time.h> |
8 | | |
9 | | #include "hash-t.hpp" |
10 | | #include "data.hpp" |
11 | | #include "data_util.hpp" |
12 | | #include "enumeration.hpp" |
13 | | #include "errors.hpp" |
14 | | #include "file_util.hpp" |
15 | | #include "fstream.hpp" |
16 | | #include "language.hpp" |
17 | | #include "getdata.hpp" |
18 | | |
19 | | #include "gettext.h" |
20 | | |
21 | | namespace { |
22 | | |
23 | | ////////////////////////////////////////////////////////////////////// |
24 | | // |
25 | | // WritableBase |
26 | | // |
27 | | |
28 | | using namespace std; |
29 | | using namespace aspeller; |
30 | | using namespace acommon; |
31 | | |
32 | | typedef const char * Str; |
33 | | typedef unsigned char byte; |
34 | | |
35 | | struct Hash { |
36 | | InsensitiveHash<> f; |
37 | 2.45k | Hash(const Language * l) : f(l) {} |
38 | 22.9M | size_t operator() (Str s) const { |
39 | 22.9M | return f(s); |
40 | 22.9M | } |
41 | | }; |
42 | | |
43 | | struct Equal { |
44 | | InsensitiveEqual f; |
45 | 2.45k | Equal(const Language * l) : f(l) {} |
46 | 0 | bool operator() (Str a, Str b) const { |
47 | 0 | return f(a, b); |
48 | 0 | } |
49 | | }; |
50 | | |
51 | 0 | void write_n_escape(FStream & o, const char * str) { |
52 | 0 | while (*str != '\0') { |
53 | 0 | if (*str == '\n') o << "\\n"; |
54 | 0 | else if (*str == '\r') o << "\\r"; |
55 | 0 | else if (*str == '\\') o << "\\\\"; |
56 | 0 | else o << *str; |
57 | 0 | ++str; |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | 0 | static inline char f_getc(FStream & in) { |
62 | 0 | int c = in.get(); |
63 | 0 | return c == EOF ? '\0' : (char)c; |
64 | 0 | } |
65 | | |
66 | 0 | bool getline_n_unescape(FStream & in, String & str, char delem) { |
67 | 0 | str.clear(); |
68 | 0 | char c = f_getc(in); |
69 | 0 | if (!c) return false; |
70 | 0 | while (c && c != delem) { |
71 | 0 | if (c == '\\') { |
72 | 0 | c = f_getc(in); |
73 | 0 | if (c == 'n') str.append('\n'); |
74 | 0 | else if (c == 'r') str.append('\r'); |
75 | 0 | else if (c == '\\') str.append('\\'); |
76 | 0 | else {str.append('\\'); continue;} |
77 | 0 | } else { |
78 | 0 | str.append(c); |
79 | 0 | } |
80 | 0 | c = f_getc(in); |
81 | 0 | } |
82 | 0 | return true; |
83 | 0 | } |
84 | | |
85 | | bool getline_n_unescape(FStream & in, DataPair & d, String & buf) |
86 | 0 | { |
87 | 0 | if (!getline_n_unescape(in, buf, '\n')) return false; |
88 | 0 | d.value.str = buf.mstr(); |
89 | 0 | d.value.size = buf.size(); |
90 | 0 | return true; |
91 | 0 | } |
92 | | |
93 | | typedef Vector<Str> StrVector; |
94 | | |
95 | | typedef hash_multiset<Str,Hash,Equal> WordLookup; |
96 | | typedef hash_map<Str,StrVector> SoundslikeLookup; |
97 | | |
98 | | class WritableBase : public Dictionary { |
99 | | protected: |
100 | | String suffix; |
101 | | String compatibility_suffix; |
102 | | |
103 | | time_t cur_file_date; |
104 | | |
105 | | String compatibility_file_name; |
106 | | |
107 | | WritableBase(BasicType t, const char * n, const char * s, const char * cs, const Config & cfg) |
108 | 2.45k | : Dictionary(t,n), |
109 | 2.45k | suffix(s), compatibility_suffix(cs), |
110 | 2.45k | use_soundslike(true) { |
111 | 2.45k | fast_lookup = true; |
112 | 2.45k | validate_words = cfg.retrieve_bool("validate-words"); |
113 | 2.45k | } |
114 | 2.45k | virtual ~WritableBase() {} |
115 | | |
116 | | virtual PosibErr<void> save(FStream &, ParmString) = 0; |
117 | | virtual PosibErr<void> merge(FStream &, ParmString, Config * = 0) = 0; |
118 | | |
119 | | PosibErr<void> save2(FStream &, ParmString); |
120 | | PosibErr<void> update(FStream &, ParmString); |
121 | | PosibErr<void> save(bool do_update); |
122 | | PosibErr<void> update_file_date_info(FStream &); |
123 | | PosibErr<void> load(ParmString, Config &, DictList *, SpellerImpl *); |
124 | | PosibErr<void> merge(ParmString); |
125 | | PosibErr<void> save_as(ParmString); |
126 | | PosibErr<void> clear(); |
127 | | |
128 | | String file_encoding; |
129 | | ConvObj iconv; |
130 | | ConvObj oconv; |
131 | | PosibErr<void> set_file_encoding(ParmString, Config & c); |
132 | | |
133 | 0 | PosibErr<void> synchronize() {return save(true);} |
134 | 0 | PosibErr<void> save_noupdate() {return save(false);} |
135 | | |
136 | | bool use_soundslike; |
137 | | StackPtr<WordLookup> word_lookup; |
138 | | SoundslikeLookup soundslike_lookup_; |
139 | | ObjStack buffer; |
140 | | |
141 | 2.45k | void set_lang_hook(Config & c) { |
142 | 2.45k | set_file_encoding(lang()->data_encoding(), c); |
143 | 2.45k | word_lookup.reset(new WordLookup(10, Hash(lang()), Equal(lang()))); |
144 | 2.45k | use_soundslike = lang()->have_soundslike(); |
145 | 2.45k | } |
146 | | }; |
147 | | |
148 | 0 | PosibErr<void> WritableBase::update_file_date_info(FStream & f) { |
149 | 0 | RET_ON_ERR(update_file_info(f)); |
150 | 0 | cur_file_date = get_modification_time(f); |
151 | 0 | return no_err; |
152 | 0 | } |
153 | | |
154 | | PosibErr<void> WritableBase::load(ParmString f0, Config & config, |
155 | | DictList *, SpellerImpl *) |
156 | 1.63k | { |
157 | 1.63k | set_file_name(f0); |
158 | 1.63k | const String f = file_name(); |
159 | 1.63k | FStream in; |
160 | | |
161 | 1.63k | if (file_exists(f)) { |
162 | | |
163 | 3 | RET_ON_ERR(open_file_readlock(in, f)); |
164 | 3 | if (in.peek() == EOF) return make_err(cant_read_file,f); |
165 | | // ^^ FIXME |
166 | 0 | RET_ON_ERR(merge(in, f, &config)); |
167 | | |
168 | 1.63k | } else if (f.size() >= suffix.size() && |
169 | 1.63k | f.substr(f.size()-suffix.size(),suffix.size()) |
170 | 1.63k | == suffix) { |
171 | | |
172 | 1.63k | compatibility_file_name = f.substr(0,f.size() - suffix.size()); |
173 | 1.63k | compatibility_file_name += compatibility_suffix; |
174 | | |
175 | 1.63k | { |
176 | 1.63k | PosibErr<void> pe = open_file_readlock(in, compatibility_file_name); |
177 | 1.63k | if (pe.has_err()) {compatibility_file_name = ""; return pe;} |
178 | 1.63k | } { |
179 | 0 | PosibErr<void> pe = merge(in, compatibility_file_name, &config); |
180 | 0 | if (pe.has_err()) {compatibility_file_name = ""; return pe;} |
181 | 0 | } |
182 | | |
183 | 0 | } else { |
184 | | |
185 | 0 | return make_err(cant_read_file,f); |
186 | | |
187 | 0 | } |
188 | | |
189 | 0 | return update_file_date_info(in); |
190 | 1.63k | } |
191 | | |
192 | 0 | PosibErr<void> WritableBase::merge(ParmString f0) { |
193 | 0 | FStream in; |
194 | 0 | Dict::FileName fn(f0); |
195 | 0 | RET_ON_ERR(open_file_readlock(in, fn.path())); |
196 | 0 | RET_ON_ERR(merge(in, fn.path())); |
197 | 0 | return no_err; |
198 | 0 | } |
199 | | |
200 | 0 | PosibErr<void> WritableBase::update(FStream & in, ParmString fn) { |
201 | 0 | typedef PosibErr<void> Ret; |
202 | 0 | { |
203 | 0 | Ret pe = merge(in, fn); |
204 | 0 | if (pe.has_err() && compatibility_file_name.empty()) return pe; |
205 | 0 | } { |
206 | 0 | Ret pe = update_file_date_info(in); |
207 | 0 | if (pe.has_err() && compatibility_file_name.empty()) return pe; |
208 | 0 | } |
209 | 0 | return no_err; |
210 | 0 | } |
211 | | |
212 | 0 | PosibErr<void> WritableBase::save2(FStream & out, ParmString fn) { |
213 | 0 | truncate_file(out, fn); |
214 | | |
215 | 0 | RET_ON_ERR(save(out,fn)); |
216 | | |
217 | 0 | out.flush(); |
218 | |
|
219 | 0 | return no_err; |
220 | 0 | } |
221 | | |
222 | 0 | PosibErr<void> WritableBase::save_as(ParmString fn) { |
223 | 0 | compatibility_file_name = ""; |
224 | 0 | set_file_name(fn); |
225 | 0 | FStream inout; |
226 | 0 | RET_ON_ERR(open_file_writelock(inout, file_name())); |
227 | 0 | RET_ON_ERR(save2(inout, file_name())); |
228 | 0 | RET_ON_ERR(update_file_date_info(inout)); |
229 | 0 | return no_err; |
230 | 0 | } |
231 | | |
232 | 0 | PosibErr<void> WritableBase::save(bool do_update) { |
233 | 0 | FStream inout; |
234 | 0 | RET_ON_ERR_SET(open_file_writelock(inout, file_name()), |
235 | 0 | bool, prev_existed); |
236 | | |
237 | 0 | if (do_update |
238 | 0 | && prev_existed |
239 | 0 | && get_modification_time(inout) > cur_file_date) |
240 | 0 | RET_ON_ERR(update(inout, file_name())); |
241 | | |
242 | 0 | RET_ON_ERR(save2(inout, file_name())); |
243 | 0 | RET_ON_ERR(update_file_date_info(inout)); |
244 | | |
245 | 0 | if (compatibility_file_name.size() != 0) { |
246 | 0 | remove_file(compatibility_file_name.c_str()); |
247 | 0 | compatibility_file_name = ""; |
248 | 0 | } |
249 | |
|
250 | 0 | return no_err; |
251 | 0 | } |
252 | | |
253 | 0 | PosibErr<void> WritableBase::clear() { |
254 | 0 | word_lookup->clear(); |
255 | 0 | soundslike_lookup_.clear(); |
256 | 0 | buffer.reset(); |
257 | 0 | return no_err; |
258 | 0 | } |
259 | | |
260 | | PosibErr<void> WritableBase::set_file_encoding(ParmString enc, Config & c) |
261 | 2.45k | { |
262 | 2.45k | if (enc == file_encoding) return no_err; |
263 | 2.45k | if (enc == "") enc = lang()->charmap(); |
264 | 2.45k | RET_ON_ERR(iconv.setup(c, enc, lang()->charmap(), NormFrom)); |
265 | 2.45k | RET_ON_ERR(oconv.setup(c, lang()->charmap(), enc, NormTo)); |
266 | 2.45k | if (iconv || oconv) |
267 | 0 | file_encoding = enc; |
268 | 2.45k | else |
269 | 2.45k | file_encoding = ""; |
270 | 2.45k | return no_err; |
271 | 2.45k | } |
272 | | |
273 | | |
274 | | ///////////////////////////////////////////////////////////////////// |
275 | | // |
276 | | // Common Stuff |
277 | | // |
278 | | |
279 | | // a word is stored in memory as follows |
280 | | // <word info><size><word...><null> |
281 | | // the hash table points to the word and not the start of the block |
282 | | |
283 | | static inline void set_word(WordEntry & res, Str w) |
284 | 0 | { |
285 | 0 | res.word = w; |
286 | 0 | res.word_size = (byte)w[-1]; |
287 | 0 | res.word_info = (byte)w[-2]; |
288 | 0 | res.aff = ""; |
289 | 0 | } |
290 | | |
291 | | // a soundslike is stored in memory as follows |
292 | | // <word info><size><sl...><null> |
293 | | // the hash table points to the sl and not the start of the block |
294 | | |
295 | | static inline void set_sl(WordEntry & res, Str w) |
296 | 0 | { |
297 | 0 | res.word = w; |
298 | 0 | res.word_size = (byte)w[-1]; |
299 | 0 | } |
300 | | |
301 | | static void soundslike_next(WordEntry * w) |
302 | 0 | { |
303 | 0 | const Str * & i = (const Str * &)(w->intr[0]); |
304 | 0 | const Str * end = (const Str * )(w->intr[1]); |
305 | 0 | set_word(*w, *i); |
306 | 0 | ++i; |
307 | 0 | if (i == end) w->adv_ = 0; |
308 | 0 | } |
309 | | |
310 | | static void sl_init(const StrVector * tmp, WordEntry & o) |
311 | 0 | { |
312 | 0 | const Str * i = tmp->pbegin(); |
313 | 0 | const Str * end = tmp->pend(); |
314 | 0 | set_word(o, *i); |
315 | 0 | ++i; |
316 | 0 | if (i != end) { |
317 | 0 | o.intr[0] = (void *)i; |
318 | 0 | o.intr[1] = (void *)end; |
319 | 0 | o.adv_ = soundslike_next; |
320 | 0 | } else { |
321 | 0 | o.intr[0] = 0; |
322 | 0 | } |
323 | 0 | } |
324 | | |
325 | | struct SoundslikeElements : public SoundslikeEnumeration { |
326 | | |
327 | | typedef SoundslikeLookup::const_iterator Itr; |
328 | | |
329 | | Itr i; |
330 | | Itr end; |
331 | | |
332 | | WordEntry d; |
333 | | |
334 | 69.3k | SoundslikeElements(Itr i0, Itr end0) : i(i0), end(end0) { |
335 | 69.3k | d.what = WordEntry::Soundslike; |
336 | 69.3k | } |
337 | | |
338 | 69.3k | WordEntry * next(int) { |
339 | 69.3k | if (i == end) return 0; |
340 | 0 | set_sl(d, i->first); |
341 | 0 | d.intr[0] = (void *)(&i->second); |
342 | 0 | ++i; |
343 | 0 | return &d; |
344 | 69.3k | } |
345 | | }; |
346 | | |
347 | | struct CleanElements : public SoundslikeEnumeration { |
348 | | |
349 | | typedef WordLookup::const_iterator Itr; |
350 | | |
351 | | Itr i; |
352 | | Itr end; |
353 | | |
354 | | WordEntry d; |
355 | | |
356 | 0 | CleanElements(Itr i0, Itr end0) : i(i0), end(end0) { |
357 | 0 | d.what = WordEntry::Word; |
358 | 0 | } |
359 | | |
360 | 0 | WordEntry * next(int) { |
361 | 0 | if (i == end) return 0; |
362 | 0 | set_word(d, *i); |
363 | 0 | ++i; |
364 | 0 | return &d; |
365 | 0 | } |
366 | | }; |
367 | | |
368 | | struct ElementsParms { |
369 | | typedef WordEntry * Value; |
370 | | typedef WordLookup::const_iterator Iterator; |
371 | | Iterator end_; |
372 | | WordEntry data; |
373 | 0 | ElementsParms(Iterator e) : end_(e) {} |
374 | 0 | bool endf(Iterator i) const {return i==end_;} |
375 | 0 | Value deref(Iterator i) {set_word(data, *i); return &data;} |
376 | 0 | static Value end_state() {return 0;} |
377 | | }; |
378 | | |
379 | | ///////////////////////////////////////////////////////////////////// |
380 | | // |
381 | | // WritableDict |
382 | | // |
383 | | |
384 | | class WritableDict : public WritableBase |
385 | | { |
386 | | public: //but don't use |
387 | | PosibErr<void> save(FStream &, ParmString); |
388 | | PosibErr<void> merge(FStream &, ParmString, Config * config); |
389 | | |
390 | | public: |
391 | | |
392 | | WritableDict(const Config & cfg) |
393 | 1.63k | : WritableBase(basic_dict, "WritableDict", ".pws", ".per", cfg) {} |
394 | | |
395 | | Size size() const; |
396 | | bool empty() const; |
397 | | |
398 | 0 | PosibErr<void> add(ParmString w) {return Dictionary::add(w);} |
399 | | PosibErr<void> add(ParmString w, ParmString s); |
400 | | |
401 | | bool lookup(ParmString word, const SensitiveCompare *, WordEntry &) const; |
402 | | |
403 | | bool clean_lookup(ParmString sondslike, WordEntry &) const; |
404 | | |
405 | | bool soundslike_lookup(const WordEntry & soundslike, WordEntry &) const; |
406 | | bool soundslike_lookup(ParmString soundslike, WordEntry &) const; |
407 | | |
408 | | WordEntryEnumeration * detailed_elements() const; |
409 | | |
410 | | SoundslikeEnumeration * soundslike_elements() const; |
411 | | }; |
412 | | |
413 | | WritableDict::Size WritableDict::size() const |
414 | 7.29k | { |
415 | 7.29k | return word_lookup->size(); |
416 | 7.29k | } |
417 | | |
418 | | bool WritableDict::empty() const |
419 | 0 | { |
420 | 0 | return word_lookup->empty(); |
421 | 0 | } |
422 | | |
423 | | bool WritableDict::lookup(ParmString word, const SensitiveCompare * c, |
424 | | WordEntry & o) const |
425 | 394k | { |
426 | 394k | o.clear(); |
427 | 394k | pair<WordLookup::iterator, WordLookup::iterator> p(word_lookup->equal_range(word)); |
428 | 394k | while (p.first != p.second) { |
429 | 0 | if ((*c)(word,*p.first)) { |
430 | 0 | o.what = WordEntry::Word; |
431 | 0 | set_word(o, *p.first); |
432 | 0 | return true; |
433 | 0 | } |
434 | 0 | ++p.first; |
435 | 0 | } |
436 | 394k | return false; |
437 | 394k | } |
438 | | |
439 | | bool WritableDict::clean_lookup(ParmString sl, WordEntry & o) const |
440 | 15.0M | { |
441 | 15.0M | o.clear(); |
442 | 15.0M | pair<WordLookup::iterator, WordLookup::iterator> p(word_lookup->equal_range(sl)); |
443 | 15.0M | if (p.first == p.second) return false; |
444 | 0 | o.what = WordEntry::Word; |
445 | 0 | set_word(o, *p.first); |
446 | 0 | return true; |
447 | | // FIXME: Deal with multiple entries |
448 | 15.0M | } |
449 | | |
450 | | bool WritableDict::soundslike_lookup(const WordEntry & word, WordEntry & o) const |
451 | 0 | { |
452 | 0 | if (use_soundslike) { |
453 | |
|
454 | 0 | const StrVector * tmp |
455 | 0 | = (const StrVector *)(word.intr[0]); |
456 | 0 | o.clear(); |
457 | |
|
458 | 0 | o.what = WordEntry::Word; |
459 | 0 | sl_init(tmp, o); |
460 | |
|
461 | 0 | } else { |
462 | | |
463 | 0 | o.what = WordEntry::Word; |
464 | 0 | o.word = word.word; |
465 | 0 | o.word_size = word.word_size; |
466 | 0 | o.word_info = word.word_info; |
467 | 0 | o.aff = ""; |
468 | | |
469 | 0 | } |
470 | 0 | return true; |
471 | 0 | } |
472 | | |
473 | | bool WritableDict::soundslike_lookup(ParmString word, WordEntry & o) const |
474 | 0 | { |
475 | 0 | if (use_soundslike) { |
476 | |
|
477 | 0 | o.clear(); |
478 | 0 | SoundslikeLookup::const_iterator i = soundslike_lookup_.find(word); |
479 | 0 | if (i == soundslike_lookup_.end()) { |
480 | 0 | return false; |
481 | 0 | } else { |
482 | 0 | o.what = WordEntry::Word; |
483 | 0 | sl_init(&i->second, o); |
484 | 0 | return true; |
485 | 0 | } |
486 | | |
487 | 0 | } else { |
488 | |
|
489 | 0 | return WritableDict::clean_lookup(word, o); |
490 | |
|
491 | 0 | } |
492 | 0 | } |
493 | | |
494 | 46.2k | SoundslikeEnumeration * WritableDict::soundslike_elements() const { |
495 | 46.2k | if (use_soundslike) |
496 | 46.2k | return new SoundslikeElements(soundslike_lookup_.begin(), |
497 | 46.2k | soundslike_lookup_.end()); |
498 | 0 | else |
499 | 0 | return new CleanElements(word_lookup->begin(), |
500 | 0 | word_lookup->end()); |
501 | 46.2k | } |
502 | | |
503 | 0 | WritableDict::Enum * WritableDict::detailed_elements() const { |
504 | 0 | return new MakeEnumeration<ElementsParms> |
505 | 0 | (word_lookup->begin(),ElementsParms(word_lookup->end())); |
506 | 0 | } |
507 | | |
508 | 0 | PosibErr<void> WritableDict::add(ParmString w, ParmString s) { |
509 | 0 | if (validate_words) |
510 | 0 | RET_ON_ERR(check_if_valid(*lang(),w)); |
511 | 0 | else |
512 | 0 | RET_ON_ERR(check_if_sane(*lang(),w)); |
513 | 0 | SensitiveCompare c(lang()); |
514 | 0 | WordEntry we; |
515 | 0 | if (WritableDict::lookup(w,&c,we)) return no_err; |
516 | 0 | if (w.size() > 250) |
517 | 0 | return make_err(invalid_word, |
518 | 0 | MsgConv(*lang())(w), |
519 | 0 | _("Word larger than 250 characters.")); |
520 | 0 | byte * w2; |
521 | 0 | w2 = (byte *)buffer.alloc(w.size() + 3); |
522 | 0 | *w2++ = lang()->get_word_info(w); |
523 | 0 | *w2++ = (byte)w.size(); |
524 | 0 | memcpy(w2, w.str(), w.size() + 1); |
525 | 0 | word_lookup->insert((char *)w2); |
526 | 0 | if (use_soundslike) { |
527 | 0 | byte * s2; |
528 | 0 | s2 = (byte *)buffer.alloc(s.size() + 2); |
529 | 0 | *s2++ = s.size(); |
530 | 0 | memcpy(s2, s.str(), s.size() + 1); |
531 | 0 | soundslike_lookup_[(char *)s2].push_back((char *)w2); |
532 | 0 | } |
533 | 0 | return no_err; |
534 | 0 | } |
535 | | |
536 | | PosibErr<void> WritableDict::merge(FStream & in, |
537 | | ParmString file_name, |
538 | | Config * config) |
539 | 0 | { |
540 | 0 | typedef PosibErr<void> Ret; |
541 | 0 | unsigned int ver; |
542 | |
|
543 | 0 | String buf; |
544 | 0 | DataPair dp; |
545 | |
|
546 | 0 | if (!getline(in, dp, buf)) |
547 | 0 | make_err(bad_file_format, file_name); |
548 | |
|
549 | 0 | split(dp); |
550 | 0 | if (dp.key == "personal_wl") |
551 | 0 | ver = 10; |
552 | 0 | else if (dp.key == "personal_ws-1.1") |
553 | 0 | ver = 11; |
554 | 0 | else |
555 | 0 | return make_err(bad_file_format, file_name); |
556 | | |
557 | 0 | split(dp); |
558 | 0 | { |
559 | 0 | Ret pe = set_check_lang(dp.key, *config); |
560 | 0 | if (pe.has_err()) |
561 | 0 | return pe.with_file(file_name); |
562 | 0 | } |
563 | | |
564 | 0 | split(dp); // count not used at the moment |
565 | |
|
566 | 0 | split(dp); |
567 | 0 | if (dp.key.size > 0) |
568 | 0 | set_file_encoding(dp.key, *config); |
569 | 0 | else |
570 | 0 | set_file_encoding("", *config); |
571 | | |
572 | 0 | ConvP conv(iconv); |
573 | 0 | while (getline_n_unescape(in, dp, buf)) { |
574 | 0 | if (ver == 10) |
575 | 0 | split(dp); |
576 | 0 | else |
577 | 0 | dp.key = dp.value; |
578 | 0 | Ret pe = add(conv(dp.key)); |
579 | 0 | if (pe.has_err()) { |
580 | 0 | clear(); |
581 | 0 | return pe.with_file(file_name); |
582 | 0 | } |
583 | 0 | } |
584 | 0 | return no_err; |
585 | 0 | } |
586 | | |
587 | | struct CStrLess { |
588 | 0 | bool operator() (const char * x, const char * y) const { |
589 | 0 | return strcmp(x, y) < 0; |
590 | 0 | } |
591 | | }; |
592 | | |
593 | | PosibErr<void> WritableDict::save(FStream & out, ParmString file_name) |
594 | 0 | { |
595 | 0 | out.printf("personal_ws-1.1 %s %i %s\n", |
596 | 0 | lang_name(), word_lookup->size(), file_encoding.c_str()); |
597 | |
|
598 | 0 | Vector<const char *> words; |
599 | 0 | words.reserve(word_lookup->size()); |
600 | 0 | for (WordLookup::const_iterator i = word_lookup->begin(), e = word_lookup->end(); |
601 | 0 | i != e; ++i) |
602 | 0 | words.push_back(*i); |
603 | 0 | std::sort(words.begin(), words.end(), CStrLess()); |
604 | | |
605 | 0 | ConvP conv(oconv); |
606 | 0 | for (Vector<const char *>::const_iterator i = words.begin(), e = words.end(); |
607 | 0 | i != e; ++i) { |
608 | 0 | write_n_escape(out, conv(*i)); |
609 | 0 | out << '\n'; |
610 | 0 | } |
611 | 0 | return no_err; |
612 | 0 | } |
613 | | |
614 | | ///////////////////////////////////////////////////////////////////// |
615 | | // |
616 | | // WritableReplList |
617 | | // |
618 | | |
619 | | static inline StrVector * get_vector(Str s) |
620 | 0 | { |
621 | 0 | return (StrVector *)(s - sizeof(StrVector) - 2); |
622 | 0 | } |
623 | | |
624 | | class WritableReplDict : public WritableBase |
625 | | { |
626 | | WritableReplDict(const WritableReplDict&); |
627 | | WritableReplDict& operator=(const WritableReplDict&); |
628 | | |
629 | | public: |
630 | | WritableReplDict(const Config & cfg) |
631 | 817 | : WritableBase(replacement_dict, "WritableReplDict", ".prepl",".rpl", cfg) |
632 | 817 | { |
633 | 817 | fast_lookup = true; |
634 | 817 | } |
635 | | ~WritableReplDict(); |
636 | | |
637 | | Size size() const; |
638 | | bool empty() const; |
639 | | |
640 | | bool lookup(ParmString, const SensitiveCompare *, WordEntry &) const; |
641 | | |
642 | | bool clean_lookup(ParmString sondslike, WordEntry &) const; |
643 | | |
644 | | bool soundslike_lookup(const WordEntry &, WordEntry &) const; |
645 | | bool soundslike_lookup(ParmString, WordEntry &) const; |
646 | | |
647 | | bool repl_lookup(const WordEntry &, WordEntry &) const; |
648 | | bool repl_lookup(ParmString, WordEntry &) const; |
649 | | |
650 | | WordEntryEnumeration * detailed_elements() const; |
651 | | SoundslikeEnumeration * soundslike_elements() const; |
652 | | |
653 | 0 | PosibErr<void> add_repl(ParmString mis, ParmString cor) { |
654 | 0 | return Dictionary::add_repl(mis,cor);} |
655 | | PosibErr<void> add_repl(ParmString mis, ParmString cor, ParmString s); |
656 | | |
657 | | private: |
658 | | PosibErr<void> save(FStream &, ParmString ); |
659 | | PosibErr<void> merge(FStream &, ParmString , Config * config); |
660 | | }; |
661 | | |
662 | | WritableReplDict::Size WritableReplDict::size() const |
663 | 3.24k | { |
664 | 3.24k | return word_lookup->size(); |
665 | 3.24k | } |
666 | | |
667 | | bool WritableReplDict::empty() const |
668 | 0 | { |
669 | 0 | return word_lookup->empty(); |
670 | 0 | } |
671 | | |
672 | | bool WritableReplDict::lookup(ParmString word, const SensitiveCompare * c, |
673 | | WordEntry & o) const |
674 | 0 | { |
675 | 0 | o.clear(); |
676 | 0 | pair<WordLookup::iterator, WordLookup::iterator> p(word_lookup->equal_range(word)); |
677 | 0 | while (p.first != p.second) { |
678 | 0 | if ((*c)(word,*p.first)) { |
679 | 0 | o.what = WordEntry::Misspelled; |
680 | 0 | set_word(o, *p.first); |
681 | 0 | o.intr[0] = (void *)*p.first; |
682 | 0 | return true; |
683 | 0 | } |
684 | 0 | ++p.first; |
685 | 0 | } |
686 | 0 | return false; |
687 | 0 | } |
688 | | |
689 | | bool WritableReplDict::clean_lookup(ParmString sl, WordEntry & o) const |
690 | 7.53M | { |
691 | 7.53M | o.clear(); |
692 | 7.53M | pair<WordLookup::iterator, WordLookup::iterator> p(word_lookup->equal_range(sl)); |
693 | 7.53M | if (p.first == p.second) return false; |
694 | 0 | o.what = WordEntry::Misspelled; |
695 | 0 | set_word(o, *p.first); |
696 | 0 | o.intr[0] = (void *)*p.first; |
697 | 0 | return true; |
698 | | // FIXME: Deal with multiple entries |
699 | 7.53M | } |
700 | | |
701 | | bool WritableReplDict::soundslike_lookup(const WordEntry & word, WordEntry & o) const |
702 | 0 | { |
703 | 0 | if (use_soundslike) { |
704 | 0 | const StrVector * tmp = (const StrVector *)(word.intr[0]); |
705 | 0 | o.clear(); |
706 | 0 | o.what = WordEntry::Misspelled; |
707 | 0 | sl_init(tmp, o); |
708 | 0 | } else { |
709 | 0 | o.what = WordEntry::Misspelled; |
710 | 0 | o.word = word.word; |
711 | 0 | o.word_size = word.word_size; |
712 | 0 | o.aff = ""; |
713 | 0 | } |
714 | 0 | return true; |
715 | 0 | } |
716 | | |
717 | | bool WritableReplDict::soundslike_lookup(ParmString soundslike, WordEntry & o) const |
718 | 0 | { |
719 | 0 | if (use_soundslike) { |
720 | 0 | o.clear(); |
721 | 0 | SoundslikeLookup::const_iterator i = soundslike_lookup_.find(soundslike); |
722 | 0 | if (i == soundslike_lookup_.end()) { |
723 | 0 | return false; |
724 | 0 | } else { |
725 | 0 | o.what = WordEntry::Misspelled; |
726 | 0 | sl_init(&(i->second), o); |
727 | 0 | return true; |
728 | 0 | } |
729 | 0 | } else { |
730 | 0 | return WritableReplDict::clean_lookup(soundslike, o); |
731 | 0 | } |
732 | 0 | } |
733 | | |
734 | 23.1k | SoundslikeEnumeration * WritableReplDict::soundslike_elements() const { |
735 | 23.1k | if (use_soundslike) |
736 | 23.1k | return new SoundslikeElements(soundslike_lookup_.begin(), |
737 | 23.1k | soundslike_lookup_.end()); |
738 | 0 | else |
739 | 0 | return new CleanElements(word_lookup->begin(), |
740 | 0 | word_lookup->end()); |
741 | 23.1k | } |
742 | | |
743 | 0 | WritableReplDict::Enum * WritableReplDict::detailed_elements() const { |
744 | 0 | return new MakeEnumeration<ElementsParms> |
745 | 0 | (word_lookup->begin(),ElementsParms(word_lookup->end())); |
746 | 0 | } |
747 | | |
748 | | static void repl_next(WordEntry * w) |
749 | 0 | { |
750 | 0 | const Str * & i = (const Str * &)(w->intr[0]); |
751 | 0 | const Str * end = (const Str * )(w->intr[1]); |
752 | 0 | set_word(*w, *i); |
753 | 0 | ++i; |
754 | 0 | if (i == end) w->adv_ = 0; |
755 | 0 | } |
756 | | |
757 | | static void repl_init(const StrVector * tmp, WordEntry & o) |
758 | 0 | { |
759 | 0 | o.what = WordEntry::Word; |
760 | 0 | const Str * i = tmp->pbegin(); |
761 | 0 | const Str * end = tmp->pend(); |
762 | 0 | set_word(o, *i); |
763 | 0 | ++i; |
764 | 0 | if (i != end) { |
765 | 0 | o.intr[0] = (void *)i; |
766 | 0 | o.intr[1] = (void *)end; |
767 | 0 | o.adv_ = repl_next; |
768 | 0 | } else { |
769 | 0 | o.intr[0] = 0; |
770 | 0 | } |
771 | 0 | } |
772 | | |
773 | | bool WritableReplDict::repl_lookup(const WordEntry & w, WordEntry & o) const |
774 | 0 | { |
775 | 0 | const StrVector * repls; |
776 | 0 | if (w.intr[0] && !w.intr[1]) { // the intr are not for the sl iter |
777 | 0 | repls = get_vector(w.word); |
778 | 0 | } else { |
779 | 0 | SensitiveCompare c(lang()); // FIXME: This is not exactly right |
780 | 0 | WordEntry tmp; |
781 | 0 | WritableReplDict::lookup(w.word, &c, tmp); |
782 | 0 | repls = get_vector(tmp.word); |
783 | 0 | if (!repls) return false; |
784 | 0 | } |
785 | 0 | o.clear(); |
786 | 0 | repl_init(repls, o); |
787 | 0 | return true; |
788 | 0 | } |
789 | | |
790 | | bool WritableReplDict::repl_lookup(ParmString word, WordEntry & o) const |
791 | 0 | { |
792 | 0 | WordEntry w; |
793 | 0 | w.word = word; |
794 | 0 | return WritableReplDict::repl_lookup(w, o); |
795 | 0 | } |
796 | | |
797 | | PosibErr<void> WritableReplDict::add_repl(ParmString mis, ParmString cor, ParmString sl) |
798 | 0 | { |
799 | 0 | if (mis.size() > 250 || cor.size() > 250) |
800 | 0 | return make_err(invalid_word, |
801 | 0 | MsgConv(*lang())(mis.size() > 250 ? mis : cor), |
802 | 0 | _("Word larger than 250 characters.")); |
803 | 0 | Str m; |
804 | 0 | SensitiveCompare cmp(lang()); // FIXME: I don't think this is completely correct |
805 | 0 | WordEntry we; |
806 | |
|
807 | 0 | pair<WordLookup::iterator, WordLookup::iterator> p0(word_lookup->equal_range(mis)); |
808 | 0 | WordLookup::iterator p = p0.first; |
809 | |
|
810 | 0 | for (; p != p0.second && !cmp(mis,*p); ++p); |
811 | |
|
812 | 0 | if (p == p0.second) { |
813 | 0 | byte * m0 = (byte *)buffer.alloc(sizeof(StrVector) + mis.size() + 3, sizeof(void *)); |
814 | 0 | new (m0) StrVector; |
815 | 0 | m0 += sizeof(StrVector); |
816 | 0 | *m0++ = lang()->get_word_info(mis); |
817 | 0 | *m0++ = mis.size(); |
818 | 0 | memcpy(m0, mis.str(), mis.size() + 1); |
819 | 0 | m = (char *)m0; |
820 | 0 | p = word_lookup->insert(m).first; |
821 | 0 | } else { |
822 | 0 | m = *p; |
823 | 0 | } |
824 | |
|
825 | 0 | StrVector * v = get_vector(m); |
826 | |
|
827 | 0 | for (StrVector::iterator i = v->begin(); i != v->end(); ++i) |
828 | 0 | if (cmp(cor, *i)) return no_err; |
829 | | |
830 | 0 | byte * c0 = (byte *)buffer.alloc(cor.size() + 3); |
831 | 0 | *c0++ = lang()->get_word_info(cor); |
832 | 0 | *c0++ = cor.size(); |
833 | 0 | memcpy(c0, cor.str(), cor.size() + 1); |
834 | 0 | v->push_back((char *)c0); |
835 | |
|
836 | 0 | if (use_soundslike) { |
837 | 0 | byte * s0 = (byte *)buffer.alloc(sl.size() + 2); |
838 | 0 | *s0++ = sl.size(); |
839 | 0 | memcpy(s0, sl.str(), sl.size() + 1); |
840 | 0 | soundslike_lookup_[(char *)s0].push_back(m); |
841 | 0 | } |
842 | |
|
843 | 0 | return no_err; |
844 | 0 | } |
845 | | |
846 | | PosibErr<void> WritableReplDict::save (FStream & out, ParmString file_name) |
847 | 0 | { |
848 | 0 | out.printf("personal_repl-1.1 %s 0 %s\n", lang_name(), file_encoding.c_str()); |
849 | | |
850 | 0 | Vector<const char *> words; |
851 | 0 | words.reserve(word_lookup->size()); |
852 | 0 | for (WordLookup::const_iterator i = word_lookup->begin(), e = word_lookup->end(); |
853 | 0 | i != e; ++i) |
854 | 0 | words.push_back(*i); |
855 | 0 | std::sort(words.begin(), words.end(), CStrLess()); |
856 | |
|
857 | 0 | ConvP conv1(oconv); |
858 | 0 | ConvP conv2(oconv); |
859 | |
|
860 | 0 | Vector<const char *> v; |
861 | 0 | for (Vector<const char *>::const_iterator i = words.begin(), e = words.end(); |
862 | 0 | i != e; ++i) |
863 | 0 | { |
864 | 0 | v = *get_vector(*i); // make a copy |
865 | 0 | std::sort(v.begin(), v.end(), CStrLess()); |
866 | 0 | for (StrVector::iterator j = v.begin(); j != v.end(); ++j) |
867 | 0 | { |
868 | 0 | write_n_escape(out, conv1(*i)); |
869 | 0 | out << ' '; |
870 | 0 | write_n_escape(out, conv2(*j)); |
871 | 0 | out << '\n'; |
872 | 0 | } |
873 | 0 | } |
874 | 0 | return no_err; |
875 | 0 | } |
876 | | |
877 | | PosibErr<void> WritableReplDict::merge(FStream & in, |
878 | | ParmString file_name, |
879 | | Config * config) |
880 | 0 | { |
881 | 0 | typedef PosibErr<void> Ret; |
882 | 0 | unsigned int version; |
883 | 0 | unsigned int num_words, num_repls; |
884 | |
|
885 | 0 | String buf; |
886 | 0 | DataPair dp; |
887 | |
|
888 | 0 | if (!getline(in, dp, buf)) |
889 | 0 | make_err(bad_file_format, file_name); |
890 | |
|
891 | 0 | split(dp); |
892 | 0 | if (dp.key == "personal_repl") |
893 | 0 | version = 10; |
894 | 0 | else if (dp.key == "personal_repl-1.1") |
895 | 0 | version = 11; |
896 | 0 | else |
897 | 0 | return make_err(bad_file_format, file_name); |
898 | | |
899 | 0 | split(dp); |
900 | 0 | { |
901 | 0 | Ret pe = set_check_lang(dp.key, *config); |
902 | 0 | if (pe.has_err()) |
903 | 0 | return pe.with_file(file_name); |
904 | 0 | } |
905 | | |
906 | 0 | unsigned int num_soundslikes = 0; |
907 | 0 | if (version == 10) { |
908 | 0 | split(dp); |
909 | 0 | num_soundslikes = atoi(dp.key); |
910 | 0 | } |
911 | |
|
912 | 0 | split(dp); // not used at the moment |
913 | |
|
914 | 0 | split(dp); |
915 | 0 | if (dp.key.size > 0) |
916 | 0 | set_file_encoding(dp.key, *config); |
917 | 0 | else |
918 | 0 | set_file_encoding("", *config); |
919 | |
|
920 | 0 | if (version == 11) { |
921 | |
|
922 | 0 | ConvP conv1(iconv); |
923 | 0 | ConvP conv2(iconv); |
924 | 0 | for (;;) { |
925 | 0 | bool res = getline_n_unescape(in, buf, '\n'); |
926 | 0 | if (!res) break; |
927 | 0 | char * mis = buf.mstr(); |
928 | 0 | char * repl = strchr(mis, ' '); |
929 | 0 | if (!repl) continue; // bad line, ignore |
930 | 0 | *repl = '\0'; // split string |
931 | 0 | ++repl; |
932 | 0 | if (!repl[0]) continue; // empty repl, ignore |
933 | 0 | Ret pe = WritableReplDict::add_repl(conv1(mis), conv2(repl)); |
934 | 0 | if (pe.has_err()) { |
935 | 0 | clear(); |
936 | 0 | return pe.with_file(file_name); |
937 | 0 | } |
938 | 0 | } |
939 | | |
940 | 0 | } else { |
941 | | |
942 | 0 | String mis, sound, repl; |
943 | 0 | unsigned int h,i,j; |
944 | 0 | for (h=0; h != num_soundslikes; ++h) { |
945 | 0 | in >> sound >> num_words; |
946 | 0 | for (i = 0; i != num_words; ++i) { |
947 | 0 | in >> mis >> num_repls; |
948 | 0 | in.ignore(); // ignore space |
949 | 0 | for (j = 0; j != num_repls; ++j) { |
950 | 0 | in.getline(repl, ','); |
951 | 0 | WritableReplDict::add_repl(mis, repl).ignore_err(); |
952 | 0 | } |
953 | 0 | } |
954 | 0 | } |
955 | |
|
956 | 0 | } |
957 | 0 | return no_err; |
958 | 0 | } |
959 | | |
960 | | WritableReplDict::~WritableReplDict() |
961 | 817 | { |
962 | 817 | WordLookup::iterator i = word_lookup->begin(); |
963 | 817 | WordLookup::iterator e = word_lookup->end(); |
964 | | |
965 | 817 | for (;i != e; ++i) |
966 | 0 | get_vector(*i)->~StrVector(); |
967 | 817 | } |
968 | | |
969 | | } |
970 | | |
971 | | namespace aspeller { |
972 | | |
973 | 1.63k | Dictionary * new_default_writable_dict(const Config & cfg) { |
974 | 1.63k | return new WritableDict(cfg); |
975 | 1.63k | } |
976 | | |
977 | 817 | Dictionary * new_default_replacement_dict(const Config & cfg) { |
978 | 817 | return new WritableReplDict(cfg); |
979 | 817 | } |
980 | | |
981 | | } |