1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/*
For general Scribus (>=1.3.2) copyright and licensing information
please refer to the COPYING file provided with the
program. Following this notice may exist a copyright and/or license
notice that predates the release of Scribus 1.3.2 for which a new
license (GPL+exception) is in place.
*/
#include "suggest.h"
#include "scpaths.h"
#include <QDebug>
#include <QFileInfo>
/*!
\brief Delimiter between different components of formatted strings for aspell dictionary entries.
*/
const char* Speller::Aspell::Suggest::kDICT_DELIM = "/";
const char* Speller::Aspell::Suggest::kEMPTY = "*";
const char* Speller::Aspell::Suggest::kDEF_LANG = "en";
const char* Speller::Aspell::Suggest::kDEF_JARGON = ""; // I.e., no jargon
const char* Speller::Aspell::Suggest::kDEF_ENCODING = "utf-8";
void Speller::Aspell::Suggest::checkError() throw( std::runtime_error )
{
if( aspell_speller_error_number( fspeller ) != 0 )
{
std::string err_msg =
std::string( "(Aspell::Speller::Suggest::checkError): "
"aspell speller error " ) +
aspell_speller_error_message( fspeller );
throw std::runtime_error( err_msg );
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::checkConfigError()
throw( std::runtime_error )
{
if( aspell_config_error_number( fconfig ) != 0 )
{
std::string err_msg =
std::string( "(Aspell::Speller::Suggest::checkConfig"
"Error): aspell speller error " ) +
aspell_config_error_message( fconfig );
throw std::runtime_error( err_msg );
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::init(const std::string& lang,
const std::string& jargon,
const std::string& encoding)
throw( std::invalid_argument, std::runtime_error )
{
// Save aspell configuration values
flang = lang;
fjargon = jargon;
fencoding = encoding;
fconfig = new_aspell_config();
try
{
setConfig();
}
catch( const std::invalid_argument& err )
{
throw err;
}
//qDebug()<<QString::fromStdString(std::string( aspell_config_retrieve( fconfig, "data-dir") ) );
//qDebug()<<QString::fromStdString(std::string( aspell_config_retrieve( fconfig, "dict-dir") ) );
//qDebug()<<QString::fromStdString(std::string( aspell_config_retrieve( fconfig, "local-data-dir") ) );
AspellCanHaveError* ret = new_aspell_speller( fconfig );
delete_aspell_config( fconfig );
if( aspell_error_number( ret ) != 0 )
{
//qDebug()<<aspell_error_number( ret )<<aspell_error_message(ret);
delete_aspell_can_have_error( ret );
throw std::runtime_error( "(Aspell::Speller::Suggest::init"
"): Error in creating speller." );
}
else
{
fspeller = to_aspell_speller( ret );
fconfig = aspell_speller_config( fspeller );
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::listDicts(std::vector<AspellDictInfo>& vals)
{
AspellDictInfoList* dlist = get_aspell_dict_info_list( fconfig );
AspellDictInfoEnumeration* dinfo_enum =
aspell_dict_info_list_elements( dlist );
const AspellDictInfo* entry;
while( (entry = aspell_dict_info_enumeration_next( dinfo_enum )) )
{
vals.push_back( *entry );
}
delete_aspell_dict_info_enumeration( dinfo_enum );
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::listDicts(std::vector<std::string>& vals)
{
setConfig();
std::vector<AspellDictInfo> entries;
listDicts( entries );
for( std::vector<AspellDictInfo>::const_iterator i = entries.begin();
i != entries.end();
++i )
{
std::string jargon( i->jargon );
std::ostringstream fmt_entry;
fmt_entry << i->name << kDICT_DELIM << i->code << kDICT_DELIM
<< (jargon == "" ? kEMPTY : jargon) << kDICT_DELIM
<< i->size;
vals.push_back( fmt_entry.str() );
}
}
//__________________________________________________________________________
void
Speller::Aspell::Suggest::printWordList(const AspellWordList* wlist,
char delim)
throw( std::invalid_argument )
{
if( ! wlist )
{
throw std::invalid_argument( "(Aspell.Speller.Suggest.print"
"WordList): word list pointer "
"is null." );
}
AspellStringEnumeration* enum_list =
aspell_word_list_elements( wlist );
const char* next;
while( (next = aspell_string_enumeration_next( enum_list )) )
{
std::cout << next << delim;
}
delete_aspell_string_enumeration( enum_list );
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::setConfig() throw( std::invalid_argument )
{
try
{
setConfigOpt( "lang", flang );
setConfigOpt( "jargon", fjargon );
setConfigOpt( "encoding", fencoding );
#ifdef Q_WS_MAC && ASPELLRELATIVEDICTDIR
QString location(ScPaths::instance().bundleDir()+"/Contents/"+ASPELLRELATIVEDICTDIR);
//qDebug()<<"aspell location:"<<location;
QFileInfo fi(location);
if (fi.exists())
{
//qDebug()<<"setting dict-dir to:"<<location;
setConfigOpt( "dict-dir", location.toStdString());
setConfigOpt( "local-data-dir", location.toStdString());
//qDebug()<<"getting local-data-dir:"<<QString::fromStdString(getConfigOpt("local-data-dir"));
//qDebug()<<"getting dict-dir:"<<QString::fromStdString(getConfigOpt("dict-dir"));
}
#endif
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
void
Speller::Aspell::Suggest::storeWordList(const AspellWordList* wlist,
std::vector<std::string>& replacement)
throw( std::invalid_argument )
{
if( ! wlist )
{
throw std::invalid_argument( "(Aspell.Speller.Suggest.store"
"WordList): word list pointer "
"is null." );
}
AspellStringEnumeration* enum_list =
aspell_word_list_elements( wlist );
const char* next;
while( (next = aspell_string_enumeration_next( enum_list )) )
{
replacement.push_back( next );
}
delete_aspell_string_enumeration( enum_list );
}
//__________________________________________________________________________
Speller::Aspell::Suggest::Suggest(const std::string& lang,
const std::string& jargon,
const std::string& encoding)
throw( std::invalid_argument, std::runtime_error )
{
// Default constructor
try
{
init( lang, jargon, encoding );
}
catch( const std::invalid_argument& err )
{
throw err;
}
catch( const std::runtime_error& err )
{
throw err;
}
}
//__________________________________________________________________________
Speller::Aspell::Suggest::Suggest(const AspellDictInfo* dinfo,
const std::string& encoding)
throw( std::invalid_argument, std::runtime_error )
{
try
{
init( dinfo->code, dinfo->jargon, encoding );
}
catch( const std::invalid_argument& err )
{
throw err;
}
catch( const std::runtime_error& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::addPersonalList(const std::string& word)
throw( std::runtime_error )
{
// A std::runtime_error exception is thrown if
// an error occcurs.
// FIXME: Return value should be something meaningful from
// aspell_speller_add_to_personal.
aspell_speller_add_to_personal( fspeller, word.c_str(), -1 );
try
{
checkError();
}
catch( const std::runtime_error& err )
{
throw err;
}
}
//__________________________________________________________________________
bool Speller::Aspell::Suggest::checkWord(const std::string& word)
{
bool status = true;
if( aspell_speller_check( fspeller, word.c_str(), -1 ) == 0 )
{
status = false;
}
return status;
}
//__________________________________________________________________________
bool Speller::Aspell::Suggest::checkWord(const std::string& word,
std::vector<std::string>& replacement,
bool always)
throw( std::invalid_argument )
{
// Checks 'word', and returns true if it is spelt correctly,
// else returns false. If 'always' is true, returns an
// array of suggestions in 'replacement', regardless of
// whether 'word' is spelt incorrectly or not. Else, if
// 'always' is false, the list of suggestions is stored only if
// 'word'is spelt incorrectly.
bool status = checkWord( word );
if( always )
{
const AspellWordList* wlist =
aspell_speller_suggest( fspeller, word.c_str(), -1 );
try
{
storeWordList( wlist, replacement );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
else
{
if( ! status )
{
const AspellWordList* wlist =
aspell_speller_suggest( fspeller, word.c_str(),
-1 );
try
{
storeWordList( wlist, replacement );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
}
return status;
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::clearSessionList() throw( std::runtime_error )
{
aspell_speller_clear_session( fspeller );
try
{
checkError();
}
catch( const std::runtime_error& err )
{
throw err;
}
}
//__________________________________________________________________________
std::string Speller::Aspell::Suggest::getConfigOpt(const std::string& opt)
{
return std::string( aspell_config_retrieve( fconfig, opt.c_str() ) );
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::getConfigOpt(const std::string& opt,
std::vector<std::string>& vals)
{
// Stores current setting of configuration option, 'opt', which
// has a value of list type, in 'vals'.
AspellStringList* list = new_aspell_string_list();
AspellMutableContainer* lst0 =
aspell_string_list_to_mutable_container( list );
aspell_config_retrieve_list( fconfig, opt.c_str(), lst0 );
AspellStringEnumeration* enum_list =
aspell_string_list_elements( list );
const char* next;
while( (next = aspell_string_enumeration_next( enum_list )) )
{
vals.push_back( next );
}
delete_aspell_string_enumeration( enum_list );
delete_aspell_string_list( list );
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::ignoreWord(const std::string& word)
throw( std::runtime_error )
{
// FIXME: Return value should be something meaningful from
// aspell_speller_add_to_session.
aspell_speller_add_to_session( fspeller, word.c_str(), -1 );
try
{
checkError();
}
catch( const std::runtime_error& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::printMainList() throw( std::invalid_argument )
{
const AspellWordList* wlist =
aspell_speller_main_word_list( fspeller );
try
{
printWordList( wlist );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::printPersonalList()
throw( std::invalid_argument )
{
const AspellWordList* wlist =
aspell_speller_personal_word_list( fspeller );
try
{
printWordList( wlist );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::printSessionList()
throw( std::invalid_argument )
{
const AspellWordList* wlist =
aspell_speller_session_word_list( fspeller );
try
{
printWordList( wlist );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
bool Speller::Aspell::Suggest::printSuggestions(const std::string& word,
bool always)
throw( std::invalid_argument )
{
// Checks 'word', and returns true if it is spelt correctly,
// else returns false. If 'always' is true, prints a list of
// suggestions to the console as UTF-8, regardless of whether
// 'word' is spelt correctly, or not. Else, if 'always' is
// false, only prints the list if 'word' is incorrect.
bool status = checkWord( word );
if( always )
{
const AspellWordList* wlist =
aspell_speller_suggest( fspeller, word.c_str(), -1 );
try
{
printWordList( wlist );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
else
{
if( ! status )
{
const AspellWordList* wlist =
aspell_speller_suggest( fspeller, word.c_str(),
-1 );
try
{
printWordList( wlist );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
}
return status;
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::resetConfig()
throw( std::invalid_argument, std::runtime_error )
{
delete_aspell_config( fconfig );
fconfig = new_aspell_config();
try
{
setConfig();
}
catch( const std::invalid_argument& err )
{
throw err;
}
AspellCanHaveError* ret = new_aspell_speller( fconfig );
if( aspell_error_number( ret ) != 0 )
{
delete_aspell_can_have_error( ret );
throw std::runtime_error( "(Aspell::Speller::Suggest::Reset"
"Config): Error in creating "
"speller." );
}
else
{
// Following statement causes a crash, hence commented out
//delete_aspell_speller( fspeller );
fspeller = to_aspell_speller( ret );
delete_aspell_config( fconfig );
fconfig = aspell_speller_config( fspeller );
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::resetConfig(const std::string& lang,
const std::string& jargon,
const std::string& encoding)
throw( std::invalid_argument, std::runtime_error )
{
// Save new aspell configuration values
flang = lang;
fjargon = jargon;
fencoding = encoding;
try
{
resetConfig(); // Actually reset configuration
}
catch( const std::invalid_argument& err )
{
throw err;
}
catch( const std::runtime_error& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::saveLists() throw( std::runtime_error )
{
// Saves all word lists.
aspell_speller_save_all_word_lists( fspeller );
try
{
checkError();
}
catch( const std::runtime_error& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::setConfigOpt(const std::string& opt,
const std::string& val)
throw( std::invalid_argument )
{
aspell_config_replace( fconfig, opt.c_str(), val.c_str() );
try
{
checkConfigError();
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
void
Speller::Aspell::Suggest::StoreMainList(std::vector<std::string>& replacement)
throw( std::invalid_argument )
{
const AspellWordList* wlist =
aspell_speller_main_word_list( fspeller );
try
{
storeWordList( wlist, replacement );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::StorePersonalList(std::vector<std::string>& replacement) throw( std::invalid_argument )
{
// Stores personal word list. A std::invalid_argument
// exception is thrown in case of error.
const AspellWordList* wlist =
aspell_speller_personal_word_list( fspeller );
try
{
storeWordList( wlist, replacement );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
void Speller::Aspell::Suggest::StoreSessionList(std::vector<std::string>& replacement) throw( std::invalid_argument )
{
// Stores session word list. A std::invalid_argument exception
// is thrown in case of error.
const AspellWordList* wlist =
aspell_speller_session_word_list( fspeller );
try
{
storeWordList( wlist, replacement );
}
catch( const std::invalid_argument& err )
{
throw err;
}
}
//__________________________________________________________________________
//@@@@@@@@@@@@@@@@@@@@@@@@@ END OF FILE @@@@@@@@@@@@@@@@@@@@@@@@@
|