Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 // Creates a new CReader object.
21 CReader::CReader(const TDesC8& aBuffer, TReaderType aType)
28 // Returns ETrue is aPattern is in the buffer, EFalse otherwise.
30 TBool CReader::Match(const TDesC8& aPattern)
32 return (iBuffer.Match(aPattern) != KErrNotFound);
37 // Reads aBuf.Length() bytes into a descriptor.
39 void CReader::ReadBytesL(TDes8& aBuf)
41 TInt bufLen = aBuf.Length();
43 if (!CheckEnoughData(bufLen))
48 for (TInt i = 0; i < bufLen; i++)
50 aBuf[i] = iBuffer[iBufPos++];
56 // Reads a byte from the buffer.
58 void CReader::ReadByteL(TUint8& aData)
60 if (!CheckEnoughData(sizeof(TUint8)))
65 aData = iBuffer[iBufPos++];
69 // Reads 16 bits of data at the current reader position.
70 // Some formats (mainly those developed by Microsoft) still use little-endian
71 // encoding of fields. The file format specifications will specify which
72 // byte-ordering scheme is used.
74 void CReader::Read16L(TUint16& aData, TBool aLittleEndian)
76 if (!CheckEnoughData(sizeof(TUint16)))
81 TUint8 a = iBuffer[iBufPos++];
82 TUint8 b = iBuffer[iBufPos++];
96 // Reads 32 bits of data at the current reader position.
97 // Some formats (mainly those developed by Microsoft) still use little-endian
98 // encoding of fields. The file format specifications will specify which
99 // byte-ordering scheme is used.
101 void CReader::Read32L(TUint32& aData, TBool aLittleEndian)
103 if (!CheckEnoughData(sizeof(TUint32)))
105 User::Leave(KErrEof);
108 TUint8 a = iBuffer[iBufPos++];
109 TUint8 b = iBuffer[iBufPos++];
110 TUint8 c = iBuffer[iBufPos++];
111 TUint8 d = iBuffer[iBufPos++];
115 aData = MAKE_INT32(d, c, b, a);
119 aData = MAKE_INT32(a, b, c, d);
125 // Reads 64 bits of data at the current reader position.
126 // Some formats (mainly those developed by Microsoft) still use little-endian
127 // encoding of fields. The file format specifications will specify which
128 // byte-ordering scheme is used.
130 void CReader::Read64L(TInt64& aData, TBool aLittleEndian)
135 Read32L(high, aLittleEndian);
136 Read32L(low, aLittleEndian);
140 aData = MAKE_TINT64(low, high);
144 aData = MAKE_TINT64(high, low);
152 TBool CReader::CheckEnoughData(TInt aNeeded)
154 return ((iBufPos + aNeeded) <= iBuffer.Length());
159 // Skips forwards or backwards aOffset number of bytes.
161 TInt CReader::Seek(TInt aOffset)
163 TInt newBufPos = iBufPos + aOffset;
165 if ((newBufPos < 0) || (newBufPos >= iBuffer.Length()))
167 // Trying to seek past the bounds of the buffer.
168 return KErrUnderflow;
176 // Skips forwards or backwards aOffset number of bytes.
178 TInt CReader::Seek(TInt64 aOffset)
180 TInt64 newBufPos = iBufPos + aOffset;
182 if ((newBufPos < 0) || (newBufPos >= iBuffer.Length()))
184 // Trying to seek past the bounds of the buffer.
185 return KErrUnderflow;