Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
23 #include "filereader.h"
25 #include "cryptospi/cryptospidef.h"
30 // a simple utility class to read from a text file given the path in its
33 CFileReader* CFileReader::NewL(TPtrC aFilePath)
35 CFileReader* self=CFileReader::NewLC(aFilePath);
36 CleanupStack::Pop(self);
40 CFileReader* CFileReader::NewLC(TPtrC aFilePath)
42 CFileReader* self=new(ELeave) CFileReader();
43 CleanupStack::PushL(self);
44 self->ConstructL(aFilePath);
49 CFileReader* CFileReader::NewL(TPtrC aFilePath, TInt aBlockSize)
51 CFileReader* self=CFileReader::NewLC(aFilePath, aBlockSize);
52 CleanupStack::Pop(self);
56 CFileReader* CFileReader::NewLC(TPtrC aFilePath, TInt aBlockSize)
58 CFileReader* self=new(ELeave) CFileReader();
59 CleanupStack::PushL(self);
60 self->ConstructL(aFilePath, aBlockSize);
65 CFileReader::~CFileReader()
67 // nulled in constructor so safe to call
76 CFileReader::CFileReader() : iFileContents(0), iFilePath(0)
80 TBool CFileReader::ReadBlockL()
82 TInt moreData = EFalse;
87 iFileContents = HBufC8::NewL(iBlockSize);
94 iFileContents = HBufC8::NewL(length);
98 TPtr8 ptr = iFileContents->Des();
100 // use appropriate read method depending on whether
101 // we are block reading or reading the whole file;
104 iFile.Read(iFileOffset, ptr, iBlockSize);
109 if(iFileOffset < size)
112 iFileOffset+=iBlockSize;
124 void CFileReader::ConstructL(TPtrC aFilePath)
126 iFilePath = HBufC::NewL(aFilePath.Length());
128 iFilePath->Des().Copy(aFilePath);
134 User::LeaveIfError(iRfs.Connect());
135 User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
140 void CFileReader::ConstructL(TPtrC aFilePath, TInt aBlockSize)
142 iFilePath = HBufC::NewL(aFilePath.Length());
144 iFilePath->Des().Copy(aFilePath);
148 iBlockSize = aBlockSize;
150 User::LeaveIfError(iRfs.Connect());
151 User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
154 CFileReader::operator const TPtrC8()
156 return iFileContents->Des();
159 TInt CFileReader::NumBlocks()
165 iFile.Size(fileSize);
167 numBlocks = fileSize / iBlockSize;
169 mod = fileSize % iBlockSize;
179 RInteger CFileReader::ParseIntegerL()
181 HBufC8* buf = ParseBinaryL(iFileContents->Des());
182 CleanupStack::PushL(buf);
183 RInteger result = RInteger::NewL(*buf);
184 CleanupStack::PopAndDestroy(buf);
189 HBufC8* CFileReader::ParseBinaryL(const TDesC8& aDes)
191 __ASSERT_ALWAYS(aDes.Length() % 2 == 0, User::Panic(_L("ParseBinaryL"), KErrArgument));
192 TInt length = aDes.Length() / 2;
193 HBufC8* buf = HBufC8::NewL(length);
194 TPtr8 ptr = buf->Des();
195 ptr.SetLength(length);
197 for (TInt i = 0 ; i < aDes.Length() ; i += 2)
200 tmp=(TUint8)(aDes[i]-(aDes[i]>'9'?('A'-10):'0'));
202 tmp|=(TUint8)(aDes[i+1]-(aDes[i+1]>'9'?('A'-10):'0'));