Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C++

A Spell Checking Engine

Rate me:
Please Sign up or sign in to vote.
4.88/5 (16 votes)
5 Feb 2001 265.8K   7K   108  
A free spell checking engine for use in your C++ applications. Includes the current US English dictionary
/*

  Copyright:		2000
  Author:			Matthew T Gullett
  Email:			gullettm@yahoo.com
  Name:				CFPSSpellCheckEngineOptions
  Part of:			Spell Checking Engine 
  Requires:			

  DESCRIPTION
  ----------------------------------------------------------
  This class is designed to implement options support for
  the spell checking engine.


  INFO:
  ----------------------------------------------------------
  This class is provided -as is-.  No warranty as to the
  function or performance of this class is provided either 
  written or implied.  
  
  You may freely use this code and modify it as necessary,
  as long as this header is unmodified and credit is given
  to the author in the application(s) in which it is
  incorporated.

*/

#include "stdafx.h"
#include "FPSSpellChecker.h"
#include "FPSSpellCheckEngineOptions.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFPSSpellCheckEngineOptions::CFPSSpellCheckEngineOptions()
{
	m_bCheckWhileTyping = TRUE;
	m_bIgnoreUpperCase = TRUE;
	m_bIgnoreNumbers = TRUE;
	m_bIgnoreAddresses = TRUE;
	m_strLanguage = FPSDICHEADER_LANG_ENGLISH;
}

CFPSSpellCheckEngineOptions::~CFPSSpellCheckEngineOptions()
{

}

int CFPSSpellCheckEngineOptions::LoadOptions(LPCSTR lpszFileName)
{
	ASSERT(lpszFileName);
	ASSERT(AfxIsValidString(lpszFileName));

	TRACE("CFPSSpellCheckEngineOptions::LoadOptions(%s)\n",lpszFileName);

	int iReturn = FPSSPELLCHECK_ERROR_NONE;
	CFile fp;

	m_strOptionsFile = lpszFileName;

	try
	{
		if (fp.Open(lpszFileName, CFile::modeRead | CFile::shareDenyNone))
		{
			CArchive ar(&fp, CArchive::load);

			ar >> m_bCheckWhileTyping;
			ar >> m_bIgnoreUpperCase;
			ar >> m_bIgnoreNumbers;
			ar >> m_bIgnoreAddresses;
			ar >> m_strLanguage;
			ar >> m_strMainDic;
			ar >> m_strUserDic;
			ar >> m_strCommonDic;

			ar.Close();
			fp.Close();
		}
		else
		{
			iReturn = FPSSCEOptions_ERROR_FILE_OPEN_FAILED;
			TRACE("CFPSSpellCheckEngineOptions::LoadOptions(%s) file failed to open\n",lpszFileName);
		}
	}
	catch(CException* pError)
	{
		iReturn = FPSSPELLCHECK_ERROR_EXCEPTION;
		TRACE("CFPSSpellCheckEngineOptions::LoadOptions(%s) EXCEPTION\n",lpszFileName);

		pError->Delete();
	}
	catch(...)
	{
		iReturn = FPSSPELLCHECK_ERROR_EXCEPTION;
		TRACE("CFPSSpellCheckEngineOptions::LoadOptions(%s) EXCEPTION\n",lpszFileName);
	}

	CheckForDefaults();

	return iReturn;
}

BOOL CFPSSpellCheckEngineOptions::SaveOptions(LPCSTR lpszFileName)
{
	ASSERT(lpszFileName);
	ASSERT(AfxIsValidString(lpszFileName));

	TRACE("CFPSSpellCheckEngineOptions::SaveOptions(%s)\n",lpszFileName);

	int iReturn = FPSSPELLCHECK_ERROR_NONE;
	CFile fp;

	try
	{
		if (fp.Open(lpszFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone))
		{
			CArchive ar(&fp, CArchive::store);

			ar << m_bCheckWhileTyping;
			ar << m_bIgnoreUpperCase;
			ar << m_bIgnoreNumbers;
			ar << m_bIgnoreAddresses;
			ar << m_strLanguage;
			ar << m_strMainDic;
			ar << m_strUserDic;
			ar << m_strCommonDic;

			ar.Close();
			fp.Close();
		}
		else
		{
			iReturn = FPSSCEOptions_ERROR_FILE_OPEN_FAILED;
			TRACE("CFPSSpellCheckEngineOptions::SaveOptions(%s) file failed to open\n",lpszFileName);
		}
	}
	catch(CException* pError)
	{
		iReturn = FPSSPELLCHECK_ERROR_EXCEPTION;
		TRACE("CFPSSpellCheckEngineOptions::SaveOptions(%s) EXCEPTION\n",lpszFileName);

		pError->Delete();
	}
	catch(...)
	{
		iReturn = FPSSPELLCHECK_ERROR_EXCEPTION;
		TRACE("CFPSSpellCheckEngineOptions::SaveOptions(%s) EXCEPTION\n",lpszFileName);
	}

	return iReturn;
}

void CFPSSpellCheckEngineOptions::CheckForDefaults()
{
	char szAppPath[2000];
	char szDrive[200];
	char szDir[200];

	VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), szAppPath, 2000) != 0);
	_splitpath(szAppPath, szDrive, szDir, NULL, NULL);

	if (m_strLanguage == "")
		m_strLanguage = FPSDICHEADER_LANG_ENGLISH;
	if (m_strMainDic == "")
	{
		m_strMainDic = szDrive; 
		m_strMainDic += szDir;
		m_strMainDic += "USMain.dic";
	}
	if (m_strUserDic)
	{
		m_strUserDic = szDrive;
		m_strUserDic += szDir;
		m_strUserDic += "USUser.dic";
	}
	if (m_strCommonDic)
	{
		m_strCommonDic = szDrive;
		m_strCommonDic += szDir;
		m_strCommonDic += "USCommon.dic";
	}
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions