os/mm/mmlibs/mmfw/Recogniser/src/reader.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmlibs/mmfw/Recogniser/src/reader.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,191 @@
     1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "readers.h"
    1.20 +
    1.21 +//
    1.22 +// Creates a new CReader object.
    1.23 +//
    1.24 +CReader::CReader(const TDesC8& aBuffer, TReaderType aType)
    1.25 + :	iBuffer(aBuffer),
    1.26 + 	iType(aType)
    1.27 +	{
    1.28 +	}
    1.29 +
    1.30 +//
    1.31 +// Returns ETrue is aPattern is in the buffer, EFalse otherwise.
    1.32 +//
    1.33 +TBool CReader::Match(const TDesC8& aPattern)
    1.34 +	{
    1.35 +	return (iBuffer.Match(aPattern) != KErrNotFound);
    1.36 +	}
    1.37 +
    1.38 +
    1.39 +//
    1.40 +// Reads aBuf.Length() bytes into a descriptor.
    1.41 +//
    1.42 +void CReader::ReadBytesL(TDes8& aBuf)
    1.43 +	{
    1.44 +	TInt bufLen = aBuf.Length();
    1.45 +	
    1.46 +	if (!CheckEnoughData(bufLen))
    1.47 +		{
    1.48 +		User::Leave(KErrEof);
    1.49 +		}
    1.50 +	
    1.51 +	for (TInt i = 0; i < bufLen; i++)
    1.52 +		{
    1.53 +		aBuf[i] = iBuffer[iBufPos++];
    1.54 +		}
    1.55 +	}
    1.56 +
    1.57 +
    1.58 +//
    1.59 +// Reads a byte from the buffer.
    1.60 +//
    1.61 +void CReader::ReadByteL(TUint8& aData)
    1.62 +	{
    1.63 +	if (!CheckEnoughData(sizeof(TUint8)))
    1.64 +		{
    1.65 +		User::Leave(KErrEof);
    1.66 +		}
    1.67 +			
    1.68 +	aData = iBuffer[iBufPos++];
    1.69 +	}
    1.70 +
    1.71 +//
    1.72 +// Reads 16 bits of data at the current reader position.
    1.73 +// Some formats (mainly those developed by Microsoft) still use little-endian
    1.74 +// encoding of fields. The file format specifications will specify which
    1.75 +// byte-ordering scheme is used.
    1.76 +//
    1.77 +void CReader::Read16L(TUint16& aData, TBool aLittleEndian)
    1.78 +	{
    1.79 +	if (!CheckEnoughData(sizeof(TUint16)))
    1.80 +		{
    1.81 +		User::Leave(KErrEof);
    1.82 +		}
    1.83 +	
    1.84 +	TUint8 a = iBuffer[iBufPos++];
    1.85 +	TUint8 b = iBuffer[iBufPos++];
    1.86 +	
    1.87 +	if (aLittleEndian)
    1.88 +		{
    1.89 +		aData = (b << 8) | a;
    1.90 +		}
    1.91 +	else
    1.92 +		{
    1.93 +		aData = (a << 8) | b;
    1.94 +		}
    1.95 +	}
    1.96 +
    1.97 +
    1.98 +//
    1.99 +// Reads 32 bits of data at the current reader position.
   1.100 +// Some formats (mainly those developed by Microsoft) still use little-endian
   1.101 +// encoding of fields. The file format specifications will specify which
   1.102 +// byte-ordering scheme is used.
   1.103 +//
   1.104 +void CReader::Read32L(TUint32& aData, TBool aLittleEndian)
   1.105 +	{
   1.106 +	if (!CheckEnoughData(sizeof(TUint32)))
   1.107 +		{
   1.108 +		User::Leave(KErrEof);
   1.109 +		}
   1.110 +	
   1.111 +	TUint8 a = iBuffer[iBufPos++];
   1.112 +	TUint8 b = iBuffer[iBufPos++];
   1.113 +	TUint8 c = iBuffer[iBufPos++];
   1.114 +	TUint8 d = iBuffer[iBufPos++];
   1.115 +	
   1.116 +	if (aLittleEndian)
   1.117 +		{
   1.118 +		aData = MAKE_INT32(d, c, b, a);
   1.119 +		}
   1.120 +	else
   1.121 +		{
   1.122 +		aData = MAKE_INT32(a, b, c, d);
   1.123 +		}
   1.124 +	}
   1.125 +
   1.126 +
   1.127 +//
   1.128 +// Reads 64 bits of data at the current reader position.
   1.129 +// Some formats (mainly those developed by Microsoft) still use little-endian
   1.130 +// encoding of fields. The file format specifications will specify which
   1.131 +// byte-ordering scheme is used.
   1.132 +//
   1.133 +void CReader::Read64L(TInt64& aData, TBool aLittleEndian)
   1.134 +	{
   1.135 +	TUint32 high;
   1.136 +	TUint32 low;
   1.137 +	
   1.138 +	Read32L(high, aLittleEndian);
   1.139 +	Read32L(low, aLittleEndian);
   1.140 +	
   1.141 +	if (aLittleEndian)
   1.142 +		{
   1.143 +		aData = MAKE_TINT64(low, high);
   1.144 +		}
   1.145 +	else
   1.146 +		{
   1.147 +		aData = MAKE_TINT64(high, low);
   1.148 +		}
   1.149 +	}
   1.150 +
   1.151 +
   1.152 +//
   1.153 +//
   1.154 +//
   1.155 +TBool CReader::CheckEnoughData(TInt aNeeded)
   1.156 +	{
   1.157 +	return ((iBufPos + aNeeded) <= iBuffer.Length());
   1.158 +	}
   1.159 +
   1.160 +
   1.161 +//
   1.162 +// Skips forwards or backwards aOffset number of bytes.
   1.163 +//
   1.164 +TInt CReader::Seek(TInt aOffset)
   1.165 +	{
   1.166 +	TInt newBufPos = iBufPos + aOffset;
   1.167 +	
   1.168 +	if ((newBufPos < 0) || (newBufPos >= iBuffer.Length()))
   1.169 +		{
   1.170 +		// Trying to seek past the bounds of the buffer.
   1.171 +		return KErrUnderflow;
   1.172 +		}
   1.173 +		
   1.174 +	iBufPos += aOffset;
   1.175 +	return KErrNone;
   1.176 +	}
   1.177 +	
   1.178 +//
   1.179 +// Skips forwards or backwards aOffset number of bytes.
   1.180 +//
   1.181 +TInt CReader::Seek(TInt64 aOffset)
   1.182 +    {
   1.183 +    TInt64 newBufPos = iBufPos + aOffset;
   1.184 +    
   1.185 +    if ((newBufPos < 0) || (newBufPos >= iBuffer.Length()))
   1.186 +        {
   1.187 +        // Trying to seek past the bounds of the buffer.
   1.188 +        return KErrUnderflow;
   1.189 +        }
   1.190 +        
   1.191 +    iBufPos += aOffset;
   1.192 +    return KErrNone;
   1.193 +    }
   1.194 +