📄 dictionary.cpp
字号:
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
int CDictionary::PatternMatchWord(LPCSTR szWord, CStringArray & straSuggestions)
{
straSuggestions.RemoveAll();
if (m_pRootNode == NULL) return 0;
m_pRootNode->GetPatternMatchingWords(szWord, straSuggestions);
return straSuggestions.GetSize();
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : LoadDictionary //
// Purpose : Load a previously saved dictionary file //
// Params : strFilename - Filename //
// Returns : true if load was successful, else false //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
bool CDictionary::LoadDictionary(const CString & strFilename)
{
bool bLoaded = false;
if (m_pRootNode)
{
// Remove the existing dictionary tree
delete m_pRootNode;
m_pRootNode = NULL;
}
try
{
// Open the file
CFile fileDictionary(strFilename, CFile::modeRead | CFile::shareDenyWrite);
CArchive archive(&fileDictionary, CArchive::load);
// And load the tree
m_pRootNode = new CNode;
m_pRootNode->Serialise(archive);
bLoaded = true;
}
catch (CFileException * e)
{
e->Delete();
}
catch (CArchiveException * e)
{
e->Delete();
}
return bLoaded;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : SaveDictionary //
// Purpose : Save the dictionary to file //
// Params : strFilename - Filename //
// Returns : true if saved successfully, else false //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
bool CDictionary::SaveDictionary(const CString & strFilename)
{
bool bSaved = false;
if (m_pRootNode)
{
try
{
// Open the file
CFile fileDictionary(strFilename, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
CArchive archive(&fileDictionary, CArchive::store);
// And save the tree
m_pRootNode->Serialise(archive);
bSaved = true;
}
catch (CFileException * e)
{
e->Delete();
}
catch (CArchiveException * e)
{
e->Delete();
}
}
return bSaved;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : CreateFromList //
// Purpose : Create a dictionary from a text file (one word per line) //
// Params : strFilename - Filename //
// bAppend - true to append to current dictionary, false to //
// start afresh //
// Returns : true if loaded successfully, else false //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
bool CDictionary::CreateFromList(const CString & strFilename, bool bAppend)
{
bool bCreated = false;
if (!bAppend)
{
// We are not appending the list, so delete any content we currently have
delete m_pRootNode;
}
try
{
// Open the file
CStdioFile fileList(strFilename, CFile::modeRead | CFile::shareDenyWrite);
CString strWord;
// And treat each line as a word
while(fileList.ReadString(strWord))
{
InsertWord(strWord);
}
bCreated = true;
}
catch (CFileException * e)
{
e->Delete();
}
return bCreated;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : SortSuggestions //
// Purpose : Sorts the words in the given array //
// Params : straSuggestions - Reference to CStringArray to sort //
// Returns : None //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
void CDictionary::SortSuggestions(CStringArray & straSuggestions)
{
bool bChanged=true;
while(bChanged)
{
bChanged=false;
for(int nItem = 1; nItem < straSuggestions.GetSize(); nItem++)
{
if(straSuggestions.GetAt(nItem - 1) > straSuggestions.GetAt(nItem))
{
CString strTemp = straSuggestions.GetAt(nItem - 1);
straSuggestions.SetAt(nItem - 1, straSuggestions.GetAt(nItem));
straSuggestions.SetAt(nItem, strTemp);
bChanged=true;
}
}
}
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : RemoveDuplicates //
// Purpose : Removed duplicates from the given array //
// Params : straSuggestions - Reference to CStringArray to process //
// Returns : None //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
void CDictionary::RemoveDuplicates(CStringArray & straSuggestions)
{
for (int nCount = straSuggestions.GetUpperBound(); nCount > 0; nCount--)
{
if (straSuggestions.GetAt(nCount) == straSuggestions.GetAt(nCount - 1))
straSuggestions.RemoveAt(nCount);
}
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : GetWordCount //
// Purpose : Count how many words are contained in the dictionary //
// Params : None //
// Returns : Word in dictionary //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
int CDictionary::GetWordCount()
{
int nWordCount = 0;
if (m_pRootNode)
m_pRootNode->GetWordCount(nWordCount);
return nWordCount;
}
//////////////////////////////////////////////////////////////////////////////
// //
// Function : RemoveWord //
// Purpose : Marks a word as deleted from the dictionary //
// Params : szWord - Word to remove //
// Returns : true if the word is removed, false otherwise //
// Throws : None //
// //
//////////////////////////////////////////////////////////////////////////////
bool CDictionary::RemoveWord(LPCSTR szWord)
{
bool bRemoved = false;
if (m_pRootNode)
{
// First off, go through the tree to find the word, getting the terminating node
// Note that we only remove the word that is an exact case match for the given word
WordMatch match;
CNode * pFinalNode = NULL;
m_pRootNode->IsWordListed(szWord, true, match, true, &pFinalNode);
if (match == eMatchPerfect && pFinalNode)
{
// We found the final node - change it's character to something other than '\0'
pFinalNode->m_Character = REMOVEDWORD_TERMINATOR;
bRemoved = true;
}
}
return bRemoved;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -