sl@0: /*
sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: /**
sl@0:  @file
sl@0:  @internalTechnology
sl@0: */
sl@0: #include "filereader.h"
sl@0: 
sl@0: #include "cryptospi/cryptospidef.h"
sl@0: #include <e32std.h>
sl@0: #include <e32def.h>
sl@0: #include <f32file.h>
sl@0: 
sl@0: // a simple utility class to read from a text file given the path in its 
sl@0: // NewLC / NewL 
sl@0: 
sl@0: CFileReader* CFileReader::NewL(TPtrC aFilePath)
sl@0: 	{
sl@0: 	CFileReader* self=CFileReader::NewLC(aFilePath);
sl@0: 	CleanupStack::Pop(self);
sl@0: 	return self;				
sl@0: 	}
sl@0: 
sl@0: CFileReader* CFileReader::NewLC(TPtrC aFilePath)
sl@0: 	{
sl@0: 	CFileReader* self=new(ELeave) CFileReader();
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aFilePath);
sl@0: 	return self;				
sl@0: 	}
sl@0: 	
sl@0: 	
sl@0: CFileReader* CFileReader::NewL(TPtrC aFilePath, TInt aBlockSize)
sl@0: 	{
sl@0: 	CFileReader* self=CFileReader::NewLC(aFilePath, aBlockSize);
sl@0: 	CleanupStack::Pop(self);
sl@0: 	return self;				
sl@0: 	}
sl@0: 
sl@0: CFileReader* CFileReader::NewLC(TPtrC aFilePath, TInt aBlockSize)
sl@0: 	{
sl@0: 	CFileReader* self=new(ELeave) CFileReader();
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aFilePath, aBlockSize);
sl@0: 	return self;				
sl@0: 	}	
sl@0: 	
sl@0: 	
sl@0: CFileReader::~CFileReader()
sl@0: 	{
sl@0: 	// nulled in constructor so safe to call
sl@0: 	delete iFileContents;	
sl@0: 	
sl@0: 	delete iFilePath;
sl@0: 	
sl@0: 	iFile.Close();
sl@0: 	iRfs.Close();
sl@0: 	}
sl@0: 	
sl@0: CFileReader::CFileReader() : iFileContents(0), iFilePath(0)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: TBool CFileReader::ReadBlockL()
sl@0: 	{
sl@0: 	TInt moreData = EFalse;
sl@0: 
sl@0: 	if(iBlockSize)
sl@0: 		{
sl@0: 		delete iFileContents;
sl@0: 		iFileContents = HBufC8::NewL(iBlockSize);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		TInt length;
sl@0: 		iFile.Size(length);
sl@0: 		delete iFileContents;
sl@0: 		iFileContents = HBufC8::NewL(length);
sl@0: 		}
sl@0: 		
sl@0: 					
sl@0: 	TPtr8 ptr = iFileContents->Des();
sl@0: 
sl@0: 	// use appropriate read method depending on whether
sl@0: 	// we are block reading or reading the whole file;
sl@0: 	if(iBlockSize)
sl@0: 		{
sl@0: 		iFile.Read(iFileOffset, ptr, iBlockSize);
sl@0: 
sl@0: 		TInt size;
sl@0: 		iFile.Size(size);
sl@0: 
sl@0: 		if(iFileOffset < size)
sl@0: 			moreData = ETrue;
sl@0: 
sl@0: 		iFileOffset+=iBlockSize;
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		iFile.Read(ptr);
sl@0: 		}
sl@0: 	
sl@0: 	
sl@0: 	return moreData;
sl@0: 	
sl@0: 	}
sl@0: 	
sl@0: void CFileReader::ConstructL(TPtrC aFilePath)	
sl@0: 	{
sl@0: 	iFilePath = HBufC::NewL(aFilePath.Length());
sl@0: 	
sl@0: 	iFilePath->Des().Copy(aFilePath);
sl@0: 	
sl@0: 	iFileOffset = 0;
sl@0: 	
sl@0: 	iBlockSize = 0;
sl@0: 
sl@0: 	User::LeaveIfError(iRfs.Connect());
sl@0: 	User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
sl@0: 	
sl@0: 	ReadBlockL();
sl@0: 	}
sl@0: 
sl@0: void CFileReader::ConstructL(TPtrC aFilePath, TInt aBlockSize)	
sl@0: 	{
sl@0: 	iFilePath = HBufC::NewL(aFilePath.Length());
sl@0: 	
sl@0: 	iFilePath->Des().Copy(aFilePath);
sl@0: 	
sl@0: 	iFileOffset = 0;
sl@0: 	
sl@0: 	iBlockSize = aBlockSize;
sl@0: 	
sl@0: 	User::LeaveIfError(iRfs.Connect());
sl@0: 	User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
sl@0: 	}
sl@0: 
sl@0: CFileReader::operator const TPtrC8()
sl@0: 	{
sl@0: 	return iFileContents->Des();
sl@0: 	}	
sl@0: 	
sl@0: TInt CFileReader::NumBlocks()
sl@0: 	{
sl@0: 	TInt numBlocks;
sl@0: 	TInt fileSize;
sl@0: 	TInt mod;
sl@0: 
sl@0: 	iFile.Size(fileSize);
sl@0: 
sl@0: 	numBlocks = fileSize / iBlockSize;
sl@0: 	
sl@0: 	mod = fileSize % iBlockSize;
sl@0: 	
sl@0: 	if(mod) 
sl@0: 		{
sl@0: 		numBlocks++;	
sl@0: 		}
sl@0: 	
sl@0: 	return numBlocks;
sl@0: 	}
sl@0: 	
sl@0: RInteger CFileReader::ParseIntegerL()
sl@0:     {
sl@0: 	HBufC8* buf = ParseBinaryL(iFileContents->Des());
sl@0: 	CleanupStack::PushL(buf);
sl@0:     RInteger result = RInteger::NewL(*buf);
sl@0:     CleanupStack::PopAndDestroy(buf);
sl@0: 
sl@0:     return result;
sl@0:     }
sl@0: 
sl@0: HBufC8* CFileReader::ParseBinaryL(const TDesC8& aDes)
sl@0: 	{
sl@0:     __ASSERT_ALWAYS(aDes.Length() % 2 == 0, User::Panic(_L("ParseBinaryL"), KErrArgument));
sl@0: 	TInt length = aDes.Length() / 2;
sl@0: 	HBufC8* buf = HBufC8::NewL(length);
sl@0:     TPtr8 ptr = buf->Des();
sl@0: 	ptr.SetLength(length);
sl@0: 
sl@0:     for (TInt i = 0 ; i < aDes.Length() ; i += 2)
sl@0:         {
sl@0:         TUint8 tmp;
sl@0:         tmp=(TUint8)(aDes[i]-(aDes[i]>'9'?('A'-10):'0'));
sl@0:         tmp*=16;
sl@0:         tmp|=(TUint8)(aDes[i+1]-(aDes[i+1]>'9'?('A'-10):'0'));
sl@0: 		ptr[i / 2] = tmp;
sl@0:         }
sl@0: 
sl@0: 	return buf;
sl@0: 	}
sl@0: