diff -r 000000000000 -r e686773b3f54 phonebookengines/contactsmodel/tsrc/T_rndutils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/phonebookengines/contactsmodel/tsrc/T_rndutils.cpp Tue Feb 02 10:12:17 2010 +0200 @@ -0,0 +1,188 @@ +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// + +#include "T_rndutils.h" + +const TInt KMaxWordLength = 64 ; +const TInt KMaxNgramSize = 8; + +void CWordNgrams::ConstructL(const TDesC& aFilename, const TInt aN, TBool aUseNarrowChar) + { + ASSERT(aN0); + + RFs fsSession; + RFileReadStream fileStream; + User::LeaveIfError(fsSession.Connect()); + CleanupClosePushL(fsSession); + User::LeaveIfError(fileStream.Open(fsSession, aFilename, EFileRead)); + CleanupClosePushL(fileStream); + TUint number = 0; + TBuf8 ngram8; +#ifdef _UNICODE + TBuf ngram; +#else +#define ngram ngram8; +#endif + TInt err=KErrNone; + while(err==KErrNone) + { + TRAP(err, number = fileStream.ReadUint8L()); + if(err==KErrNone) + { + if(aUseNarrowChar) + { + fileStream.ReadL(ngram8,aN); + SetNgram(ngram8, number); + } + else + { + fileStream.ReadL(ngram,aN); + SetNgram(ngram, number); + } + } + } + CleanupStack::PopAndDestroy(2); + } + + +HBufC* CWordNgrams::WordLC() + { + HBufC* buffer = HBufC::NewLC(KMaxWordLength); + TPtr word = buffer->Des(); + while(AddLetter(word)) + {} + return buffer; + } + + +/** Return false if done*/ +TBool CWordNgrams::AddLetter(TDes& aWord) + { + if(aWord.Length()==aWord.MaxLength()) + { + return EFalse; + } + TChar c = NextLetter(aWord); + if(c==0) + { + return EFalse; + } + aWord.Append(c); + return ETrue; + } + +// +// CWordDigrams. +// + + +CWordDigrams::CWordDigrams(TInt64& aRandSeed) : iRandSeed(aRandSeed) + {} + + +CWordDigrams* CWordDigrams::NewLC(const TDesC& aFilename, TInt64& aRandSeed) + { + CWordDigrams* self = new(ELeave) CWordDigrams(aRandSeed); + CleanupStack::PushL(self); + self->ConstructL(aFilename); + return self; + } + +void CWordDigrams::SetNgram(const TDesC8& aNgram, TUint aNumber) + { + iDigrams[Index(aNgram[0])][Index(aNgram[1])] = (TUint8) aNumber; + } + +void CWordDigrams::SetNgram(const TDesC16& /*aNgram*/, TUint /*aNumber*/) + {} + +void CWordDigrams::ConstructL(const TDesC& aFilename) + { + CWordNgrams::ConstructL(aFilename); + for(TInt i=0;i