|
// $Id$
//
// Copyright � 2009 Phil Cairns
// This code is published under the Code Project Open Licence. See
// http://www.codeproject.com/info/cpol10.aspx for licence details.
//
#include "stdafx.h"
#include "SpellCheck.h"
SpellCheck::SpellCheck(void) : _ready(false), _psc(0)
{
InitializeCriticalSection(&csProt);
}
SpellCheck::~SpellCheck(void)
{
// Make sure that we're not in the middle of loading
// or using the spell checker at this point
EnterCriticalSection(&csProt);
_ready = false;
delete _psc;
_psc = 0;
LeaveCriticalSection(&csProt);
// Now close down the critical sections
DeleteCriticalSection(&csProt);
}
void SpellCheck::loadThread(void* pv)
{
// Call the load method
SpellCheck* pThis = (SpellCheck*)pv;
EnterCriticalSection(&pThis->csProt);
try
{
// If we're already loaded, we need to unload
if (pThis->_psc != 0)
{
delete pThis->_psc;
}
// This loads the main dictionary
pThis->_psc = new Hunspell(pThis->_affPath.c_str(), pThis->_dicPath.c_str());
// Now we load the extra words that have been added
FILE* pf;
if (0 == fopen_s(&pf, pThis->_extPath.c_str(), "rt"))
{
char word[1024];
while (!feof(pf) && !ferror(pf) && 0 != fgets(word, sizeof(word), pf))
{
size_t len = strlen(word);
if (len > 2)
{
if (word[len - 1] == '\n')
{
word[len - 1] = 0; // get rid of the '\n'
}
pThis->_psc->add(word);
}
}
fclose(pf);
}
// Indicate that we're ready to go
pThis->_ready = true;
}
catch (...)
{
// Whatever happens, we need to be able
// to leave the critical section
ATLTRACE("Unhandled exception while loading dictionaries\n");
}
LeaveCriticalSection(&pThis->csProt);
}
void SpellCheck::loadDicts(const char* affPath, const char* dicPath, const char* extPath)
{
EnterCriticalSection(&csProt);
_affPath = affPath;
_dicPath = dicPath;
_extPath = extPath;
LeaveCriticalSection(&csProt);
_beginthread(loadThread, 0, this);
}
void SpellCheck::loadDicts(const std::string& affPath, const std::string& dicPath, const std::string& extPath)
{
EnterCriticalSection(&csProt);
_affPath = affPath;
_dicPath = dicPath;
_extPath = extPath;
LeaveCriticalSection(&csProt);
_beginthread(loadThread, 0, this);
}
bool SpellCheck::wordIsOK(const char* szWord)
{
// Protect use of the spell checker
EnterCriticalSection(&csProt);
// If we're not ready, just say that it's OK
if (!_ready)
{
LeaveCriticalSection(&csProt);
return true;
}
bool ret = true; // Assume innocent
try
{
ret = (_psc->spell(szWord) != 0);
}
catch (...)
{
// Whatever happens, we need to be able
// to leave the critical section
}
LeaveCriticalSection(&csProt);
return ret;
}
bool SpellCheck::wordIsOK(const std::string& sWord)
{
return wordIsOK(sWord.c_str());
}
void SpellCheck::suggest(const char* szWord, STRINGLIST& opts)
{
opts.clear();
// Protect use of the spell checker
EnterCriticalSection(&csProt);
// If we're not ready, just bug out with no options
if (!_ready)
{
LeaveCriticalSection(&csProt);
return;
}
// Try to find the suggestions
try
{
char** wordList;
int wordCount = _psc->suggest(&wordList, szWord);
for (int i = 0; i < wordCount; i++)
{
opts.push_back(wordList[i]);
}
_psc->free_list(&wordList, wordCount);
}
catch (...)
{
// Whatever happens, we need to be able
// to leave the critical section
}
LeaveCriticalSection(&csProt);
}
void SpellCheck::suggest(const std::string& sWord, STRINGLIST& opts)
{
suggest(sWord.c_str(), opts);
}
void SpellCheck::addWord(const char* szWord)
{
// Protect use of the spell checker
EnterCriticalSection(&csProt);
// If we're not ready, just bug out with no options
if (!_ready)
{
LeaveCriticalSection(&csProt);
return;
}
// Try to add the word to the current dictionary
try
{
_psc->add(szWord);
// Add the word to the permanent custom dictionary.
FILE* pf;
if (0 == fopen_s(&pf, _extPath.c_str(), "at"))
{
fputs(szWord, pf);
fputc('\n', pf);
fclose(pf);
}
}
catch (...)
{
// Whatever happens, we need to be able
// to leave the critical section
}
LeaveCriticalSection(&csProt);
}
void SpellCheck::addWord(const std::string& sWord)
{
addWord(sWord.c_str());
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.