|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "readers.h" |
|
17 |
|
18 // |
|
19 // |
|
20 // |
|
21 CFileReader::CFileReader(RFile* aFile) |
|
22 : CBufferReader(iFileBuffer, CReader::EFile), |
|
23 iFile(aFile) |
|
24 { |
|
25 } |
|
26 |
|
27 |
|
28 // |
|
29 // |
|
30 // |
|
31 CFileReader::~CFileReader() |
|
32 { |
|
33 } |
|
34 |
|
35 |
|
36 // |
|
37 // |
|
38 // |
|
39 CFileReader* CFileReader::NewLC(RFile* aFile) |
|
40 { |
|
41 CFileReader* self = new(ELeave) CFileReader(aFile); |
|
42 CleanupStack::PushL(self); |
|
43 self->ConstructL(); |
|
44 return self; |
|
45 } |
|
46 |
|
47 |
|
48 // |
|
49 // |
|
50 // |
|
51 void CFileReader::ConstructL() |
|
52 { |
|
53 TInt pos = 0; |
|
54 User::LeaveIfError(iFile->Seek(ESeekStart, pos)); |
|
55 User::LeaveIfError(iFile->Read(iFileBuffer)); |
|
56 } |
|
57 |
|
58 |
|
59 // |
|
60 // Checks if there is aAmount of data left in the buffer. |
|
61 // It is important to call the base-class implementation first |
|
62 // to ensure correct operation. |
|
63 // |
|
64 TBool CFileReader::CheckEnoughData(TInt aAmount) |
|
65 { |
|
66 if (CBufferReader::CheckEnoughData(aAmount)) |
|
67 { |
|
68 return ETrue; |
|
69 } |
|
70 |
|
71 // Try to read more data. |
|
72 TInt bufPos = CBufferReader::Position(); |
|
73 TInt err = PhysicallySeekAndRead(bufPos - iFileBuffer.Length()); |
|
74 if (err == KErrNone) |
|
75 { |
|
76 // The read may have succeeded but that |
|
77 // still doesn't mean we have enough data. |
|
78 return (aAmount <= iFileBuffer.Length()); |
|
79 } |
|
80 |
|
81 return EFalse; |
|
82 } |
|
83 |
|
84 |
|
85 // |
|
86 // |
|
87 // |
|
88 void CFileReader::Reset() |
|
89 { |
|
90 CBufferReader::Reset(); // This will reset iBufPos. |
|
91 |
|
92 if (iFilePos != 0) |
|
93 { |
|
94 // We need to seek to the start and fill the buffer. |
|
95 iFilePos = 0; |
|
96 TInt err = iFile->Seek(ESeekStart, iFilePos); |
|
97 if (err == KErrNone) |
|
98 { |
|
99 err = iFile->Read(iFileBuffer); |
|
100 } |
|
101 |
|
102 if (err != KErrNone) |
|
103 { |
|
104 iFileBuffer.Zero(); |
|
105 } |
|
106 } |
|
107 else |
|
108 { |
|
109 // There's no need to seek and read. |
|
110 iFilePos = 0; |
|
111 } |
|
112 } |
|
113 |
|
114 |
|
115 // |
|
116 // |
|
117 // |
|
118 void CFileReader::SeekL(TInt aOffset) |
|
119 { |
|
120 TInt err = CReader::Seek(aOffset); |
|
121 if (err == KErrUnderflow) |
|
122 { |
|
123 TInt bufPos = CBufferReader::Position(); |
|
124 aOffset += bufPos - iFileBuffer.Length(); |
|
125 User::LeaveIfError(PhysicallySeekAndRead(aOffset)); |
|
126 } |
|
127 } |
|
128 |
|
129 |
|
130 // |
|
131 // It could be possible for a 64-bit field in formats such as MPEG4 |
|
132 // to have values that would fit in a 32-bit variable. In this case |
|
133 // we can use it for seeking. This function checks if a 64-bit value |
|
134 // is compatible with RFile's 32-bit operations. |
|
135 // |
|
136 void CFileReader::SeekL(TInt64 aOffset) |
|
137 { |
|
138 if (aOffset < (TInt64)KMinTInt) |
|
139 { |
|
140 User::Leave(KErrNotSupported); |
|
141 } |
|
142 |
|
143 if (aOffset > (TInt64)KMaxTInt) |
|
144 { |
|
145 User::Leave(KErrNotSupported); |
|
146 } |
|
147 |
|
148 TInt low = (TInt)I64LOW(aOffset); |
|
149 SeekL(low); |
|
150 } |
|
151 |
|
152 // |
|
153 // This function seeks forward/backward aOffset bytes |
|
154 // and fills the buffer from that point. |
|
155 // |
|
156 TInt CFileReader::PhysicallySeekAndRead(TInt aOffset) |
|
157 { |
|
158 TInt err; |
|
159 |
|
160 // New buffer contents so read from the start of it. |
|
161 CBufferReader::Reset(); |
|
162 |
|
163 iFilePos = aOffset; |
|
164 err = iFile->Seek(ESeekCurrent, iFilePos); |
|
165 if (err != KErrNone) |
|
166 { |
|
167 return err; |
|
168 } |
|
169 |
|
170 err = iFile->Read(iFileBuffer); |
|
171 if (err != KErrNone) |
|
172 { |
|
173 return err; |
|
174 } |
|
175 |
|
176 return (iFileBuffer.Length() == 0 ? KErrEof : KErrNone); |
|
177 } |
|
178 |