sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
/**
|
sl@0
|
20 |
@file
|
sl@0
|
21 |
@internalTechnology
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
#include "filereader.h"
|
sl@0
|
24 |
|
sl@0
|
25 |
#include "cryptospi/cryptospidef.h"
|
sl@0
|
26 |
#include <e32std.h>
|
sl@0
|
27 |
#include <e32def.h>
|
sl@0
|
28 |
#include <f32file.h>
|
sl@0
|
29 |
|
sl@0
|
30 |
// a simple utility class to read from a text file given the path in its
|
sl@0
|
31 |
// NewLC / NewL
|
sl@0
|
32 |
|
sl@0
|
33 |
CFileReader* CFileReader::NewL(TPtrC aFilePath)
|
sl@0
|
34 |
{
|
sl@0
|
35 |
CFileReader* self=CFileReader::NewLC(aFilePath);
|
sl@0
|
36 |
CleanupStack::Pop(self);
|
sl@0
|
37 |
return self;
|
sl@0
|
38 |
}
|
sl@0
|
39 |
|
sl@0
|
40 |
CFileReader* CFileReader::NewLC(TPtrC aFilePath)
|
sl@0
|
41 |
{
|
sl@0
|
42 |
CFileReader* self=new(ELeave) CFileReader();
|
sl@0
|
43 |
CleanupStack::PushL(self);
|
sl@0
|
44 |
self->ConstructL(aFilePath);
|
sl@0
|
45 |
return self;
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
|
sl@0
|
49 |
CFileReader* CFileReader::NewL(TPtrC aFilePath, TInt aBlockSize)
|
sl@0
|
50 |
{
|
sl@0
|
51 |
CFileReader* self=CFileReader::NewLC(aFilePath, aBlockSize);
|
sl@0
|
52 |
CleanupStack::Pop(self);
|
sl@0
|
53 |
return self;
|
sl@0
|
54 |
}
|
sl@0
|
55 |
|
sl@0
|
56 |
CFileReader* CFileReader::NewLC(TPtrC aFilePath, TInt aBlockSize)
|
sl@0
|
57 |
{
|
sl@0
|
58 |
CFileReader* self=new(ELeave) CFileReader();
|
sl@0
|
59 |
CleanupStack::PushL(self);
|
sl@0
|
60 |
self->ConstructL(aFilePath, aBlockSize);
|
sl@0
|
61 |
return self;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
|
sl@0
|
65 |
CFileReader::~CFileReader()
|
sl@0
|
66 |
{
|
sl@0
|
67 |
// nulled in constructor so safe to call
|
sl@0
|
68 |
delete iFileContents;
|
sl@0
|
69 |
|
sl@0
|
70 |
delete iFilePath;
|
sl@0
|
71 |
|
sl@0
|
72 |
iFile.Close();
|
sl@0
|
73 |
iRfs.Close();
|
sl@0
|
74 |
}
|
sl@0
|
75 |
|
sl@0
|
76 |
CFileReader::CFileReader() : iFileContents(0), iFilePath(0)
|
sl@0
|
77 |
{
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
TBool CFileReader::ReadBlockL()
|
sl@0
|
81 |
{
|
sl@0
|
82 |
TInt moreData = EFalse;
|
sl@0
|
83 |
|
sl@0
|
84 |
if(iBlockSize)
|
sl@0
|
85 |
{
|
sl@0
|
86 |
delete iFileContents;
|
sl@0
|
87 |
iFileContents = HBufC8::NewL(iBlockSize);
|
sl@0
|
88 |
}
|
sl@0
|
89 |
else
|
sl@0
|
90 |
{
|
sl@0
|
91 |
TInt length;
|
sl@0
|
92 |
iFile.Size(length);
|
sl@0
|
93 |
delete iFileContents;
|
sl@0
|
94 |
iFileContents = HBufC8::NewL(length);
|
sl@0
|
95 |
}
|
sl@0
|
96 |
|
sl@0
|
97 |
|
sl@0
|
98 |
TPtr8 ptr = iFileContents->Des();
|
sl@0
|
99 |
|
sl@0
|
100 |
// use appropriate read method depending on whether
|
sl@0
|
101 |
// we are block reading or reading the whole file;
|
sl@0
|
102 |
if(iBlockSize)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
iFile.Read(iFileOffset, ptr, iBlockSize);
|
sl@0
|
105 |
|
sl@0
|
106 |
TInt size;
|
sl@0
|
107 |
iFile.Size(size);
|
sl@0
|
108 |
|
sl@0
|
109 |
if(iFileOffset < size)
|
sl@0
|
110 |
moreData = ETrue;
|
sl@0
|
111 |
|
sl@0
|
112 |
iFileOffset+=iBlockSize;
|
sl@0
|
113 |
}
|
sl@0
|
114 |
else
|
sl@0
|
115 |
{
|
sl@0
|
116 |
iFile.Read(ptr);
|
sl@0
|
117 |
}
|
sl@0
|
118 |
|
sl@0
|
119 |
|
sl@0
|
120 |
return moreData;
|
sl@0
|
121 |
|
sl@0
|
122 |
}
|
sl@0
|
123 |
|
sl@0
|
124 |
void CFileReader::ConstructL(TPtrC aFilePath)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
iFilePath = HBufC::NewL(aFilePath.Length());
|
sl@0
|
127 |
|
sl@0
|
128 |
iFilePath->Des().Copy(aFilePath);
|
sl@0
|
129 |
|
sl@0
|
130 |
iFileOffset = 0;
|
sl@0
|
131 |
|
sl@0
|
132 |
iBlockSize = 0;
|
sl@0
|
133 |
|
sl@0
|
134 |
User::LeaveIfError(iRfs.Connect());
|
sl@0
|
135 |
User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
|
sl@0
|
136 |
|
sl@0
|
137 |
ReadBlockL();
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
void CFileReader::ConstructL(TPtrC aFilePath, TInt aBlockSize)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
iFilePath = HBufC::NewL(aFilePath.Length());
|
sl@0
|
143 |
|
sl@0
|
144 |
iFilePath->Des().Copy(aFilePath);
|
sl@0
|
145 |
|
sl@0
|
146 |
iFileOffset = 0;
|
sl@0
|
147 |
|
sl@0
|
148 |
iBlockSize = aBlockSize;
|
sl@0
|
149 |
|
sl@0
|
150 |
User::LeaveIfError(iRfs.Connect());
|
sl@0
|
151 |
User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
|
sl@0
|
152 |
}
|
sl@0
|
153 |
|
sl@0
|
154 |
CFileReader::operator const TPtrC8()
|
sl@0
|
155 |
{
|
sl@0
|
156 |
return iFileContents->Des();
|
sl@0
|
157 |
}
|
sl@0
|
158 |
|
sl@0
|
159 |
TInt CFileReader::NumBlocks()
|
sl@0
|
160 |
{
|
sl@0
|
161 |
TInt numBlocks;
|
sl@0
|
162 |
TInt fileSize;
|
sl@0
|
163 |
TInt mod;
|
sl@0
|
164 |
|
sl@0
|
165 |
iFile.Size(fileSize);
|
sl@0
|
166 |
|
sl@0
|
167 |
numBlocks = fileSize / iBlockSize;
|
sl@0
|
168 |
|
sl@0
|
169 |
mod = fileSize % iBlockSize;
|
sl@0
|
170 |
|
sl@0
|
171 |
if(mod)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
numBlocks++;
|
sl@0
|
174 |
}
|
sl@0
|
175 |
|
sl@0
|
176 |
return numBlocks;
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
RInteger CFileReader::ParseIntegerL()
|
sl@0
|
180 |
{
|
sl@0
|
181 |
HBufC8* buf = ParseBinaryL(iFileContents->Des());
|
sl@0
|
182 |
CleanupStack::PushL(buf);
|
sl@0
|
183 |
RInteger result = RInteger::NewL(*buf);
|
sl@0
|
184 |
CleanupStack::PopAndDestroy(buf);
|
sl@0
|
185 |
|
sl@0
|
186 |
return result;
|
sl@0
|
187 |
}
|
sl@0
|
188 |
|
sl@0
|
189 |
HBufC8* CFileReader::ParseBinaryL(const TDesC8& aDes)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
__ASSERT_ALWAYS(aDes.Length() % 2 == 0, User::Panic(_L("ParseBinaryL"), KErrArgument));
|
sl@0
|
192 |
TInt length = aDes.Length() / 2;
|
sl@0
|
193 |
HBufC8* buf = HBufC8::NewL(length);
|
sl@0
|
194 |
TPtr8 ptr = buf->Des();
|
sl@0
|
195 |
ptr.SetLength(length);
|
sl@0
|
196 |
|
sl@0
|
197 |
for (TInt i = 0 ; i < aDes.Length() ; i += 2)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
TUint8 tmp;
|
sl@0
|
200 |
tmp=(TUint8)(aDes[i]-(aDes[i]>'9'?('A'-10):'0'));
|
sl@0
|
201 |
tmp*=16;
|
sl@0
|
202 |
tmp|=(TUint8)(aDes[i+1]-(aDes[i+1]>'9'?('A'-10):'0'));
|
sl@0
|
203 |
ptr[i / 2] = tmp;
|
sl@0
|
204 |
}
|
sl@0
|
205 |
|
sl@0
|
206 |
return buf;
|
sl@0
|
207 |
}
|
sl@0
|
208 |
|