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