|
1 // file_reader.cpp |
|
2 // |
|
3 // Copyright (c) 2005 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include "file_reader.h" |
|
14 |
|
15 |
|
16 CFileReader* CFileReader::NewL(TInt aBlockSize, RFs& aFs, TBool aDirect) |
|
17 { |
|
18 CFileReader* self = new(ELeave) CFileReader(aFs, aDirect, aBlockSize); |
|
19 CleanupStack::PushL(self); |
|
20 self->ConstructL(); |
|
21 CleanupStack::Pop(self); |
|
22 return self; |
|
23 } |
|
24 |
|
25 CFileReader::~CFileReader() |
|
26 { |
|
27 Cancel(); |
|
28 iFile.Close(); |
|
29 delete iBuf; |
|
30 } |
|
31 |
|
32 void CFileReader::Read(const TDesC& aFileName, MFileReaderObserver& aObserver) |
|
33 { |
|
34 Read(aFileName, 0, aObserver); |
|
35 } |
|
36 |
|
37 void CFileReader::Read(const TDesC& aFileName, TInt aOffset, MFileReaderObserver& aObserver) |
|
38 { |
|
39 ASSERT(!IsActive()); |
|
40 iObserver = &aObserver; |
|
41 iFirstRead = ETrue; |
|
42 if (iDirect) |
|
43 { |
|
44 iDirectOffset = aOffset; |
|
45 iFileName = aFileName; |
|
46 ReadNextDirectChunk(); |
|
47 } |
|
48 else |
|
49 { |
|
50 iFile.Close(); |
|
51 TInt err = iFile.Open(iFs, aFileName, EFileRead); |
|
52 if (err) |
|
53 { |
|
54 iObserver->HandleFileReadError(err); |
|
55 } |
|
56 else |
|
57 { |
|
58 err = iFile.Seek(ESeekStart, aOffset); |
|
59 if (err) |
|
60 { |
|
61 iObserver->HandleFileReadError(err); |
|
62 } |
|
63 ReadNextChunk(); |
|
64 } |
|
65 } |
|
66 } |
|
67 |
|
68 CFileReader::CFileReader(RFs& aFs, TBool aDirect, TInt aBlockSize) |
|
69 : CActive(CActive::EPriorityStandard), iFs(aFs), iBlockSize(aBlockSize), iPtr(NULL, 0), iDirect(aDirect) |
|
70 { |
|
71 CActiveScheduler::Add(this); |
|
72 } |
|
73 |
|
74 void CFileReader::ConstructL() |
|
75 { |
|
76 iBuf = HBufC8::NewL(iBlockSize); |
|
77 iPtr.Set(iBuf->Des()); |
|
78 } |
|
79 |
|
80 void CFileReader::ReadNextChunk() |
|
81 { |
|
82 ASSERT(!IsActive()); |
|
83 iPtr.Zero(); |
|
84 iFile.Read(iPtr, iBlockSize, iStatus); |
|
85 SetActive(); |
|
86 } |
|
87 |
|
88 void CFileReader::ReadNextDirectChunk() |
|
89 { |
|
90 ASSERT(!IsActive()); |
|
91 iPtr.Zero(); |
|
92 SetActive(); |
|
93 TRequestStatus* status = &iStatus; |
|
94 User::RequestComplete(status, KErrNone); |
|
95 } |
|
96 |
|
97 void CFileReader::DoCancel() |
|
98 { |
|
99 // Can't cancel reads, to do nothing. |
|
100 } |
|
101 |
|
102 void CFileReader::RunL() |
|
103 { |
|
104 TInt err; |
|
105 if (iDirect) |
|
106 { |
|
107 err = iFs.ReadFileSection(iFileName, iDirectOffset, iPtr, iBlockSize); |
|
108 if (err == KErrNone) |
|
109 { |
|
110 iDirectOffset += iPtr.Length(); |
|
111 } |
|
112 } |
|
113 else |
|
114 { |
|
115 err = iStatus.Int(); |
|
116 } |
|
117 |
|
118 if (err) |
|
119 { |
|
120 iObserver->HandleFileReadError(err); |
|
121 return; |
|
122 } |
|
123 |
|
124 TBool keepGoing(EFalse); |
|
125 if (iFirstRead) |
|
126 { |
|
127 iFirstRead = EFalse; |
|
128 iObserver->HandleFileData(iPtr, MFileReaderObserver::EFirst, keepGoing); |
|
129 } |
|
130 else if (iPtr.Length() < iBlockSize) |
|
131 { |
|
132 iFile.Close(); |
|
133 TBool dummy; |
|
134 iObserver->HandleFileData(iPtr, MFileReaderObserver::ELast, dummy); |
|
135 } |
|
136 else |
|
137 { |
|
138 iObserver->HandleFileData(iPtr, MFileReaderObserver::EMiddle, keepGoing); |
|
139 } |
|
140 |
|
141 if (keepGoing) |
|
142 { |
|
143 iDirect ? ReadNextDirectChunk() : ReadNextChunk(); |
|
144 } |
|
145 } |
|
146 |