Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

suggest.cpp

Go to the documentation of this file.
00001 /*
00002   For general Scribus (>=1.3.2) copyright and licensing information
00003   please refer to the COPYING file provided with the
00004   program. Following this notice may exist a copyright and/or license
00005   notice that predates the release of Scribus 1.3.2 for which a new
00006   license (GPL+exception) is in place.
00007 */
00008 #include "suggest.h"
00012 const char* Speller::Aspell::Suggest::kDICT_DELIM = "/";
00013 const char* Speller::Aspell::Suggest::kEMPTY = "*";
00014 const char* Speller::Aspell::Suggest::kDEF_LANG = "en";
00015 const char* Speller::Aspell::Suggest::kDEF_JARGON = "";  // I.e., no jargon
00016 const char* Speller::Aspell::Suggest::kDEF_ENCODING = "utf-8";
00017 
00018 void Speller::Aspell::Suggest::checkError() throw( std::runtime_error )
00019 {
00020     if( aspell_speller_error_number( fspeller ) != 0 )
00021     {
00022         std::string err_msg =
00023             std::string( "(Aspell::Speller::Suggest::checkError): "
00024                      "aspell speller error " ) +
00025             aspell_speller_error_message( fspeller );
00026         throw std::runtime_error( err_msg );
00027     }
00028 }
00029 //__________________________________________________________________________
00030 void Speller::Aspell::Suggest::checkConfigError()
00031     throw( std::runtime_error )
00032 {
00033     if( aspell_config_error_number( fconfig ) != 0 )
00034     {
00035         std::string err_msg =
00036             std::string( "(Aspell::Speller::Suggest::checkConfig"
00037                      "Error): aspell speller error " ) +
00038             aspell_config_error_message( fconfig );
00039         throw std::runtime_error( err_msg );
00040     }
00041 }
00042 //__________________________________________________________________________
00043 void Speller::Aspell::Suggest::init(const std::string& lang,
00044                     const std::string& jargon,
00045                     const std::string& encoding)
00046     throw( std::invalid_argument, std::runtime_error )
00047 {
00048     // Save aspell configuration values
00049     flang = lang;
00050     fjargon = jargon;
00051     fencoding = encoding;
00052 
00053     fconfig = new_aspell_config();
00054     try
00055     {
00056         setConfig();
00057     }
00058     catch( const std::invalid_argument& err )
00059     {
00060         throw err;
00061     }
00062 
00063     AspellCanHaveError* ret = new_aspell_speller( fconfig );
00064     delete_aspell_config( fconfig );
00065     if( aspell_error_number( ret ) != 0 )
00066     {
00067         delete_aspell_can_have_error( ret );
00068         throw std::runtime_error( "(Aspell::Speller::Suggest::init"
00069                       "): Error in creating speller." );
00070     }
00071     else
00072     {
00073         fspeller = to_aspell_speller( ret );
00074         fconfig = aspell_speller_config( fspeller );
00075     }
00076 }
00077 //__________________________________________________________________________
00078 void Speller::Aspell::Suggest::listDicts(std::vector<AspellDictInfo>& vals)
00079 {
00080     AspellDictInfoList* dlist = get_aspell_dict_info_list( fconfig );
00081     AspellDictInfoEnumeration* dinfo_enum =
00082         aspell_dict_info_list_elements( dlist );
00083     const AspellDictInfo* entry;
00084     while( (entry = aspell_dict_info_enumeration_next( dinfo_enum )) )
00085     {
00086         vals.push_back( *entry );
00087     }
00088     delete_aspell_dict_info_enumeration( dinfo_enum );
00089 }
00090 //__________________________________________________________________________
00091 void Speller::Aspell::Suggest::listDicts(std::vector<std::string>& vals)
00092 {
00093     std::vector<AspellDictInfo> entries;
00094     listDicts( entries );
00095     for( std::vector<AspellDictInfo>::const_iterator i = entries.begin();
00096              i != entries.end();
00097              ++i )
00098         {
00099         std::string jargon( i->jargon );
00100         std::ostringstream fmt_entry;
00101         fmt_entry << i->name << kDICT_DELIM << i->code << kDICT_DELIM
00102               << (jargon == "" ? kEMPTY : jargon) << kDICT_DELIM
00103               << i->size;
00104                 vals.push_back( fmt_entry.str() );
00105         }
00106 }
00107 //__________________________________________________________________________
00108 void
00109 Speller::Aspell::Suggest::printWordList(const AspellWordList* wlist,
00110                     char delim)
00111     throw( std::invalid_argument )
00112 {
00113     if( ! wlist )
00114     {
00115         throw std::invalid_argument( "(Aspell.Speller.Suggest.print"
00116                          "WordList): word list pointer "
00117                          "is null." );
00118     }
00119 
00120     AspellStringEnumeration* enum_list =
00121         aspell_word_list_elements( wlist );
00122     const char* next;
00123     while( (next = aspell_string_enumeration_next( enum_list )) )
00124     {
00125         std::cout << next << delim;
00126     }
00127     delete_aspell_string_enumeration( enum_list );
00128 }
00129 //__________________________________________________________________________
00130 void Speller::Aspell::Suggest::setConfig() throw( std::invalid_argument )
00131 {
00132     try
00133     {
00134         setConfigOpt( "lang", flang );
00135         setConfigOpt( "jargon", fjargon );
00136         setConfigOpt( "encoding", fencoding );
00137     }
00138     catch( const std::invalid_argument& err )
00139     {
00140         throw err;
00141     }
00142 }
00143 //__________________________________________________________________________
00144 void
00145 Speller::Aspell::Suggest::storeWordList(const AspellWordList* wlist,
00146                     std::vector<std::string>& replacement)
00147     throw( std::invalid_argument )
00148 {
00149     if( ! wlist )
00150     {
00151         throw std::invalid_argument( "(Aspell.Speller.Suggest.store"
00152                          "WordList): word list pointer "
00153                          "is null." );
00154     }
00155 
00156     AspellStringEnumeration* enum_list =
00157         aspell_word_list_elements( wlist );
00158     const char* next;
00159     while( (next = aspell_string_enumeration_next( enum_list )) )
00160     {
00161         replacement.push_back( next );
00162     }
00163     delete_aspell_string_enumeration( enum_list );
00164 }
00165 //__________________________________________________________________________
00166 Speller::Aspell::Suggest::Suggest(const std::string& lang,
00167                   const std::string& jargon,
00168                   const std::string& encoding)
00169     throw( std::invalid_argument, std::runtime_error )
00170 {
00171     // Default constructor
00172     try
00173     {
00174         init( lang, jargon, encoding );
00175     }
00176     catch( const std::invalid_argument& err )
00177     {
00178         throw err;
00179     }
00180     catch( const std::runtime_error& err )
00181     {
00182         throw err;
00183     }
00184 }
00185 //__________________________________________________________________________
00186 Speller::Aspell::Suggest::Suggest(const AspellDictInfo* dinfo,
00187                   const std::string& encoding)
00188     throw( std::invalid_argument, std::runtime_error )
00189 {
00190     try
00191     {
00192         init( dinfo->code, dinfo->jargon, encoding );
00193     }
00194     catch( const std::invalid_argument& err )
00195     {
00196         throw err;
00197     }
00198     catch( const std::runtime_error& err )
00199     {
00200         throw err;
00201     }
00202 }
00203 //__________________________________________________________________________
00204 void Speller::Aspell::Suggest::addPersonalList(const std::string& word)
00205     throw( std::runtime_error )
00206 {
00207     //  A std::runtime_error exception is thrown if
00208     // an error occcurs.
00209 
00210     // FIXME: Return value should be something meaningful from
00211     // aspell_speller_add_to_personal.
00212     aspell_speller_add_to_personal( fspeller, word.c_str(), -1 );
00213     try
00214     {
00215         checkError();
00216     }
00217     catch( const std::runtime_error& err )
00218     {
00219         throw err;
00220     }
00221 }
00222 //__________________________________________________________________________
00223 bool Speller::Aspell::Suggest::checkWord(const std::string& word)
00224 {
00225     bool status = true;
00226 
00227     if( aspell_speller_check( fspeller, word.c_str(), -1 ) == 0 )
00228     {
00229         status = false;
00230     }
00231     return status;
00232 }
00233 //__________________________________________________________________________
00234 bool Speller::Aspell::Suggest::checkWord(const std::string& word,
00235                      std::vector<std::string>& replacement,
00236                      bool always)
00237     throw( std::invalid_argument )
00238 {
00239     // Checks 'word', and returns true if it is spelt correctly,
00240     // else returns false. If 'always' is true, returns an
00241     // array of suggestions in 'replacement', regardless of
00242     // whether 'word' is spelt incorrectly or not. Else, if
00243     // 'always' is false, the list of suggestions is stored only if
00244     // 'word'is spelt incorrectly.
00245     bool status = checkWord( word );
00246 
00247     if( always )
00248     {
00249         const AspellWordList* wlist =
00250             aspell_speller_suggest( fspeller, word.c_str(), -1 );
00251         try
00252         {
00253             storeWordList( wlist, replacement );
00254         }
00255         catch( const std::invalid_argument& err )
00256         {
00257             throw err;
00258         }
00259     }
00260     else
00261     {
00262         if( ! status )
00263         {
00264             const AspellWordList* wlist =
00265                 aspell_speller_suggest( fspeller, word.c_str(),
00266                             -1 );
00267             try
00268             {
00269                 storeWordList( wlist, replacement );
00270             }
00271             catch( const std::invalid_argument& err )
00272             {
00273                 throw err;
00274             }
00275         }
00276     }
00277     return status;
00278 }
00279 //__________________________________________________________________________
00280 void Speller::Aspell::Suggest::clearSessionList() throw( std::runtime_error )
00281 {
00282     aspell_speller_clear_session( fspeller );
00283     try
00284     {
00285         checkError();
00286     }
00287     catch( const std::runtime_error& err )
00288     {
00289         throw err;
00290     }  
00291 }
00292 //__________________________________________________________________________
00293 std::string Speller::Aspell::Suggest::getConfigOpt(const std::string& opt)
00294 {
00295     return std::string( aspell_config_retrieve( fconfig, opt.c_str() ) );
00296 }
00297 //__________________________________________________________________________
00298 void Speller::Aspell::Suggest::getConfigOpt(const std::string& opt,
00299                         std::vector<std::string>& vals)
00300 {
00301     // Stores current setting of configuration option, 'opt', which
00302     // has a value of list type, in 'vals'.
00303     AspellStringList* list = new_aspell_string_list();
00304     AspellMutableContainer* lst0 =
00305         aspell_string_list_to_mutable_container( list );
00306     aspell_config_retrieve_list( fconfig, opt.c_str(), lst0 );
00307     AspellStringEnumeration* enum_list =
00308         aspell_string_list_elements( list );
00309     const char* next;
00310     while( (next = aspell_string_enumeration_next( enum_list )) )
00311     {
00312         vals.push_back( next );
00313     }
00314     delete_aspell_string_enumeration( enum_list );
00315     delete_aspell_string_list( list );
00316 }
00317 //__________________________________________________________________________
00318 void Speller::Aspell::Suggest::ignoreWord(const std::string& word)
00319     throw( std::runtime_error )
00320 {
00321     // FIXME: Return value should be something meaningful from
00322     // aspell_speller_add_to_session.
00323     aspell_speller_add_to_session( fspeller, word.c_str(), -1 );
00324     try
00325     {
00326         checkError();
00327     }
00328     catch( const std::runtime_error& err )
00329     {
00330         throw err;
00331     }
00332 }
00333 //__________________________________________________________________________
00334 void Speller::Aspell::Suggest::printMainList() throw( std::invalid_argument )
00335 {
00336     const AspellWordList* wlist =
00337         aspell_speller_main_word_list( fspeller );
00338     try
00339     {
00340         printWordList( wlist );
00341     }
00342     catch( const std::invalid_argument& err )
00343     {
00344         throw err;
00345     }
00346 }
00347 //__________________________________________________________________________
00348 void Speller::Aspell::Suggest::printPersonalList()
00349     throw( std::invalid_argument )
00350 {
00351     const AspellWordList* wlist =
00352         aspell_speller_personal_word_list( fspeller );
00353     try
00354     {
00355         printWordList( wlist );
00356     }
00357     catch( const std::invalid_argument& err )
00358     {
00359         throw err;
00360     }
00361 }
00362 //__________________________________________________________________________
00363 void Speller::Aspell::Suggest::printSessionList()
00364     throw( std::invalid_argument )
00365 {
00366     const AspellWordList* wlist =
00367         aspell_speller_session_word_list( fspeller );
00368     try
00369     {
00370         printWordList( wlist );
00371     }
00372     catch( const std::invalid_argument& err )
00373     {
00374         throw err;
00375     }
00376 }
00377 //__________________________________________________________________________
00378 bool Speller::Aspell::Suggest::printSuggestions(const std::string& word,
00379                         bool always)
00380     throw( std::invalid_argument )
00381 {
00382     // Checks 'word', and returns true if it is spelt correctly,
00383     // else returns false. If 'always' is true, prints a list of
00384     // suggestions to the console as UTF-8, regardless of whether
00385     // 'word' is spelt correctly, or not. Else, if 'always' is
00386     // false, only prints the list if 'word' is incorrect.
00387     bool status = checkWord( word );
00388 
00389     if( always )
00390     {
00391         const AspellWordList* wlist =
00392             aspell_speller_suggest( fspeller, word.c_str(), -1 );
00393         try
00394         {
00395             printWordList( wlist );
00396         }
00397         catch( const std::invalid_argument& err )
00398         {
00399             throw err;
00400         }
00401     }
00402     else
00403     {
00404         if( ! status )
00405         {
00406             const AspellWordList* wlist =
00407                 aspell_speller_suggest( fspeller, word.c_str(),
00408                             -1 );
00409             try
00410             {
00411                 printWordList( wlist );
00412             }
00413             catch( const std::invalid_argument& err )
00414             {
00415                 throw err;
00416             }
00417         }
00418     }
00419     return status;  
00420 }
00421 //__________________________________________________________________________
00422 void Speller::Aspell::Suggest::resetConfig()
00423     throw( std::invalid_argument, std::runtime_error )
00424 {
00425     delete_aspell_config( fconfig );
00426     fconfig = new_aspell_config();
00427     try
00428     {
00429         setConfig();
00430     }
00431     catch( const std::invalid_argument& err )
00432     {
00433         throw err;
00434     }
00435 
00436     AspellCanHaveError* ret = new_aspell_speller( fconfig );
00437     if( aspell_error_number( ret ) != 0 )
00438     {
00439         delete_aspell_can_have_error( ret );
00440         throw std::runtime_error( "(Aspell::Speller::Suggest::Reset"
00441                       "Config): Error in creating "
00442                       "speller." );
00443     }
00444     else
00445     {
00446         // Following statement causes a crash, hence commented out
00447         //delete_aspell_speller( fspeller );
00448         fspeller = to_aspell_speller( ret );
00449         delete_aspell_config( fconfig );
00450         fconfig = aspell_speller_config( fspeller );
00451     }
00452 }
00453 //__________________________________________________________________________
00454 void Speller::Aspell::Suggest::resetConfig(const std::string& lang,
00455                        const std::string& jargon,
00456                        const std::string& encoding)
00457     throw( std::invalid_argument, std::runtime_error )
00458 {
00459     // Save new aspell configuration values
00460     flang = lang;
00461     fjargon = jargon;
00462     fencoding = encoding;
00463 
00464     try
00465     {
00466         resetConfig();  // Actually reset configuration
00467     }
00468     catch( const std::invalid_argument& err )
00469     {
00470         throw err;
00471     }
00472     catch( const std::runtime_error& err )
00473     {
00474         throw err;
00475     }
00476 }
00477 //__________________________________________________________________________
00478 void Speller::Aspell::Suggest::saveLists() throw( std::runtime_error )
00479 {
00480     // Saves all word lists.
00481     aspell_speller_save_all_word_lists( fspeller );
00482     try
00483     {
00484         checkError();
00485     }
00486     catch( const std::runtime_error& err )
00487     {
00488         throw err;
00489     }
00490 }
00491 //__________________________________________________________________________
00492 void Speller::Aspell::Suggest::setConfigOpt(const std::string& opt,
00493                         const std::string& val)
00494     throw( std::invalid_argument )
00495 {
00496     aspell_config_replace( fconfig, opt.c_str(), val.c_str() );
00497     try
00498     {
00499         checkConfigError();
00500     }
00501     catch( const std::invalid_argument& err )
00502     {
00503         throw err;
00504     }
00505 }
00506 //__________________________________________________________________________
00507 void
00508 Speller::Aspell::Suggest::StoreMainList(std::vector<std::string>& replacement)
00509     throw( std::invalid_argument )
00510 {
00511     const AspellWordList* wlist =
00512         aspell_speller_main_word_list( fspeller );
00513     try
00514     {
00515         storeWordList( wlist, replacement );
00516     }
00517     catch( const std::invalid_argument& err )
00518     {
00519         throw err;
00520     }
00521 }
00522 //__________________________________________________________________________
00523 void Speller::Aspell::Suggest::StorePersonalList(std::vector<std::string>& replacement) throw( std::invalid_argument )
00524 {
00525     // Stores personal word list. A std::invalid_argument
00526     // exception is thrown in case of error.
00527     const AspellWordList* wlist =
00528         aspell_speller_personal_word_list( fspeller );
00529     try
00530     {
00531         storeWordList( wlist, replacement );
00532     }
00533     catch( const std::invalid_argument& err )
00534     {
00535         throw err;
00536     }
00537 }
00538 //__________________________________________________________________________
00539 void Speller::Aspell::Suggest::StoreSessionList(std::vector<std::string>& replacement) throw( std::invalid_argument )
00540 {
00541     // Stores session word list. A std::invalid_argument exception
00542     // is thrown in case of error.
00543     const AspellWordList* wlist =
00544         aspell_speller_session_word_list( fspeller );
00545     try
00546     {
00547         storeWordList( wlist, replacement );
00548     }
00549     catch( const std::invalid_argument& err )
00550     {
00551         throw err;
00552     }
00553 }
00554 //__________________________________________________________________________
00555 //@@@@@@@@@@@@@@@@@@@@@@@@@ END OF FILE @@@@@@@@@@@@@@@@@@@@@@@@@
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.