Click here to Skip to main content
15,887,844 members
Articles / Desktop Programming / MFC

SmartLexicon

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
30 Aug 2006GPL310 min read 85.9K   3.3K   51  
A multilingual dictionary engine with regular expressions support and Web browser integration.
/*  This file is a part of SmartLexicon, a multi-lingual dictionary engine.

    Copyright (C) 2005, Kostas Giannakakis

    SmartLexicon is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    SmartLexicon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SmartLexicon; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "StdAfx.h"
#include "LexDataBaseStore.h"

CLexDataBaseStore::CLexDataBaseStore(void) : isOpen(FALSE)
{
	appKeyName = _T("Software\\KG\\Lexicon");
}

CLexDataBaseStore::~CLexDataBaseStore(void)
{	
	if (isOpen)
	{
		if (reg.SetKey(appKeyName, FALSE))
		{
			reg.WriteDword(_T("Count"), count);
		}
	}
	dictNameArray.RemoveAll();
}


void CLexDataBaseStore::Open(CString aAppKeyName)
{
	appKeyName = aAppKeyName;
	
	reg.SetRootKey(HKEY_CURRENT_USER);
	if (!reg.KeyExists(appKeyName))
	{
		count = 0;
		if (reg.CreateKey(appKeyName))
		{
			reg.WriteDword(_T("Count"), 0);
			isOpen = TRUE;
		}
	}
	else
	{
		if (reg.SetKey(appKeyName, FALSE))
		{
			CString keyName;
			
			count = reg.ReadDword(_T("Count"), 0);
			isOpen = TRUE;

			for(int i=0; i<count; i++)
			{
				keyName.Format(_T("%s\\Dict%02x"), appKeyName, i + 1);
				if (reg.SetKey(keyName, FALSE))
				{
					dictNameArray.Add(reg.ReadString(_T("Name"),_T("")));
				}
				else
				{
					break;
				}
			}
			count = i;
		}
	}
}

//BOOL CLexDataBaseStore::IsDuplicateName(CString name)
//{
//	if (!isOpen)
//		return(TRUE);
//
//	for(INT_PTR i=0; i<dictNameArray.GetCount(); i++)
//	{
//		if (name == dictNameArray.GetAt(i))
//			return(TRUE);
//	}
//	return(FALSE);
//}

BOOL CLexDataBaseStore::RegisterLexicon(const CString& name,
										const CString& pathSource,
										const CString& pathIndex,
										const CString& lang1,
										const CString& lang2,
										int type,
										BOOL autoload)
{
	if(!isOpen /*|| IsDuplicateName(name)*/)
		return(FALSE);
	
	BOOL OK = WriteDictionaryProperties(count,
				 					    name,
									    pathSource,
										pathIndex,
									    lang1,
									    lang2,
										type,
									    autoload);
	if (OK)
	{
		dictNameArray.Add(name);
		count++;
	}

	return(OK);
}

BOOL CLexDataBaseStore::WriteDictionaryProperties(int index,
				 								  const CString& name,
												  const CString& pathSource,
												  const CString& pathIndex,
												  const CString& lang1,
												  const CString& lang2,
												  int type,
												  BOOL autoload)
{
	if(!isOpen || index > count)
		return(FALSE);

	CString keyName;
	keyName.Format(_T("%s\\Dict%02x"), appKeyName, index + 1);

	if (!reg.SetKey(keyName, TRUE))
		return(FALSE);

	BOOL OK;
	OK =  reg.WriteString(_T("Name"), name);
	OK = OK && reg.WriteString(_T("Path"), pathSource);
	OK = OK && reg.WriteString(_T("Path Index"), pathIndex);
	OK = OK && reg.WriteString(_T("Language 1"), lang1);
	OK = OK && reg.WriteString(_T("Language 2"), lang2);
	OK = OK && reg.WriteDword(_T("Type"), type);
	OK = OK && reg.WriteBool(_T("Autoload"), autoload);

	return(OK);
}

BOOL CLexDataBaseStore::ReadDictionaryProperties(int index,
									  CString& name,
									  CString& pathSource,
									  CString& pathIndex,
									  CString& lang1,
									  CString& lang2,
									  int &type,
									  BOOL& autoload)
{
	if(!isOpen || index >= count)
		return(FALSE);

	CString keyName;
	keyName.Format(_T("%s\\Dict%02x"), appKeyName, index + 1);

	if (!reg.SetKey(keyName, FALSE))
		return(FALSE);
	
	CString retValue;

	retValue = reg.ReadString(_T("Name"), _T(""));
	if (retValue != _T(""))
		name = retValue;
	else
		return(FALSE);

	retValue = reg.ReadString(_T("Path"), _T(""));
	if (retValue != _T(""))
		pathSource = retValue;
	else
		return(FALSE);

	retValue = reg.ReadString(_T("Path Index"), _T(""));
	if (retValue != _T(""))
		pathIndex = retValue;
	else
		return(FALSE);

	retValue = reg.ReadString(_T("Language 1"), _T(""));
	if (retValue != _T(""))
		lang1 = retValue;
	else
		return(FALSE);

	retValue = reg.ReadString(_T("Language 2"), _T(""));
	if (retValue != _T(""))
		lang2 = retValue;
	else
		return(FALSE);

	type = reg.ReadDword(_T("Type"), 0);
	autoload = reg.ReadBool(_T("Autoload"), FALSE);

	return(TRUE);
}

BOOL CLexDataBaseStore::DeleteDictionary(int index)
{
	if(!isOpen || index >= count || count == 0)
		return(FALSE);

	CString name, pathSource, pathIndex, lang1, lang2;
	int type;
	BOOL autoload;

	dictNameArray.RemoveAt(index);
	for(int i=index; i<count-1; i++)
	{
		if (ReadDictionaryProperties(i+1, name, pathSource, pathIndex,
			                         lang1, lang2, type, autoload))
		{
			WriteDictionaryProperties(i, name, pathSource, pathIndex,
				                      lang1, lang2, type, autoload);
		}
		else
		{
			return(FALSE);
		}
	}

	CString keyName;
	keyName.Format(_T("%s\\Dict%02x"), appKeyName, count);
	if (reg.DeleteKey(keyName))
	{
		count--;
		return(TRUE);
	}
	return(FALSE);
}


// Testing

#ifdef _DEBUG

CLexDataBaseStoreTest::CLexDataBaseStoreTest(void) {};
CLexDataBaseStoreTest::~CLexDataBaseStoreTest(void) {};

BOOL CLexDataBaseStoreTest::Test1()
{
	CRegistry reg;
	reg.SetRootKey(HKEY_CURRENT_USER);
	reg.DeleteKey(_T("Software\\KG\\LexiconTest"));

	CLexDataBaseStore *lexDB = new CLexDataBaseStore;
	lexDB->Open(_T("Software\\KG\\LexiconTest"));
	delete lexDB;

	BOOL OK = reg.KeyExists(_T("Software\\KG\\LexiconTest"));
	ASSERT(OK);
	return(OK);
}

BOOL CLexDataBaseStoreTest::Test2()
{
	CRegistry reg;
	reg.SetRootKey(HKEY_CURRENT_USER);
	reg.DeleteKey(_T("Software\\KG\\LexiconTest"));

	CLexDataBaseStore *lexDB = new CLexDataBaseStore;
	lexDB->Open(_T("Software\\KG\\LexiconTest"));
	BOOL OK;

	OK = lexDB->RegisterLexicon(L"Dictionary 1", L"void", L"index", 
								L"German", L"English", 0, TRUE);
	OK = OK && lexDB->RegisterLexicon(L"Dictionary 2", L"void", L"index2", 
										L"English", L"German", 1, FALSE);
	OK = OK && !lexDB->RegisterLexicon(L"Dictionary 1", L"void2", L"index3", 
										L"Greek", L"English", 0, TRUE);
	ASSERT(OK);
	delete lexDB;

	lexDB = new CLexDataBaseStore;
	lexDB->Open(_T("Software\\KG\\LexiconTest"));

	CString name, path, pathIndex, lang1, lang2;
	int type;
	BOOL autoload;

	OK = (lexDB->GetCount() == 2);
	ASSERT(OK);
	OK = lexDB->ReadDictionaryProperties(0, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 1");
	ASSERT(path == L"void");
	ASSERT(pathIndex == L"index");
	ASSERT(lang1 == L"German");
	ASSERT(lang2 == L"English");
	ASSERT(type == 0);
	ASSERT(autoload == TRUE);

	OK = lexDB->ReadDictionaryProperties(1, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 2");
	ASSERT(path == L"void");
	ASSERT(pathIndex == L"index2");
	ASSERT(lang1 == L"English");
	ASSERT(lang2 == L"German");
	ASSERT(type == 1);
	ASSERT(autoload == FALSE);

	OK = !lexDB->ReadDictionaryProperties(2, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	delete lexDB;

	return(OK);
}

BOOL CLexDataBaseStoreTest::Test3()
{
	CRegistry reg;
	reg.SetRootKey(HKEY_CURRENT_USER);
	reg.DeleteKey(_T("Software\\KG\\LexiconTest"));

	CLexDataBaseStore *lexDB = new CLexDataBaseStore;
	lexDB->Open(_T("Software\\KG\\LexiconTest"));
	BOOL OK;

	CString name, path, pathIndex, lang1, lang2;
	int type;
	BOOL autoload;

	// Register 4 dictionaries
	OK =   lexDB->RegisterLexicon(L"Dictionary 1", L"void", L"index", L"German", L"English", 0, TRUE);
	OK = OK && lexDB->RegisterLexicon(L"Dictionary 2", L"void", L"index2", L"English", L"German", 1, FALSE);
	OK = OK && lexDB->RegisterLexicon(L"Dictionary 3", L"void2", L"index3", L"Greek", L"English", 2, TRUE);
	OK = OK && lexDB->RegisterLexicon(L"Dictionary 4", L"void2", L"index4", L"Greek", L"English", 3, TRUE);

	// Delete dictionary 4
	OK = OK && lexDB->DeleteDictionary(3);
	ASSERT(OK);
	ASSERT(lexDB->GetCount() == 3);

	OK = lexDB->ReadDictionaryProperties(0, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 1");
	ASSERT(path == L"void");
	ASSERT(pathIndex == L"index");
	ASSERT(lang1 == L"German");
	ASSERT(lang2 == L"English");
	ASSERT(type == 0);
	ASSERT(autoload == TRUE);

	OK = lexDB->ReadDictionaryProperties(1, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 2");
	ASSERT(path == L"void");
	ASSERT(pathIndex == L"index2");
	ASSERT(lang1 == L"English");
	ASSERT(lang2 == L"German");
	ASSERT(type == 1);
	ASSERT(autoload == FALSE);

	OK = lexDB->ReadDictionaryProperties(2, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 3");
	ASSERT(path == L"void2");
	ASSERT(pathIndex == L"index3");
	ASSERT(lang1 == L"Greek");
	ASSERT(lang2 == L"English");
	ASSERT(type == 2);
	ASSERT(autoload == TRUE);

	OK = !lexDB->ReadDictionaryProperties(3, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);

	// Register dictionary 4 again
	OK = lexDB->RegisterLexicon(L"Dictionary 4", L"void2", L"index4", L"Greek", L"English", 3, TRUE);
	ASSERT(OK);
	ASSERT(lexDB->GetCount() == 4);

	// Delete dictionary 3
	OK = OK && lexDB->DeleteDictionary(2);
	ASSERT(OK);
	ASSERT(lexDB->GetCount() == 3);

	OK = lexDB->ReadDictionaryProperties(0, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 1");
	ASSERT(path == L"void");
	ASSERT(pathIndex == L"index");
	ASSERT(lang1 == L"German");
	ASSERT(lang2 == L"English");
	ASSERT(type == 0);
	ASSERT(autoload == TRUE);

	OK = lexDB->ReadDictionaryProperties(1, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 2");
	ASSERT(path == L"void");
	ASSERT(pathIndex == L"index2");
	ASSERT(lang1 == L"English");
	ASSERT(lang2 == L"German");
	ASSERT(type == 1);
	ASSERT(autoload == FALSE);

	OK = lexDB->ReadDictionaryProperties(2, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 4");
	ASSERT(path == L"void2");
	ASSERT(pathIndex == L"index4");
	ASSERT(lang1 == L"Greek");
	ASSERT(lang2 == L"English");
	ASSERT(type == 3);
	ASSERT(autoload == TRUE);

	// Delete dictionary 1
	OK = lexDB->DeleteDictionary(0);
	ASSERT(OK);
	ASSERT(lexDB->GetCount() == 2);

	// Destroy object
	delete lexDB;

	lexDB = new CLexDataBaseStore;
	lexDB->Open(_T("Software\\KG\\LexiconTest"));

	OK = (lexDB->GetCount() == 2);
	ASSERT(OK);

	OK = lexDB->ReadDictionaryProperties(0, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 2");
	ASSERT(path == L"void");
	ASSERT(pathIndex == L"index2");
	ASSERT(lang1 == L"English");
	ASSERT(lang2 == L"German");
	ASSERT(type == 1);
	ASSERT(autoload == FALSE);

	OK = lexDB->ReadDictionaryProperties(1, name, path, pathIndex,
										lang1, lang2, type, autoload);
	ASSERT(OK);
	ASSERT(name == L"Dictionary 4");
	ASSERT(path == L"void2");
	ASSERT(pathIndex == L"index4");
	ASSERT(lang1 == L"Greek");
	ASSERT(lang2 == L"English");
	ASSERT(type == 3);
	ASSERT(autoload == TRUE);

	delete lexDB;
	return(OK);
}

#endif //_DEBUG

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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Self employed
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions