mmlibs/mmfw/Recogniser/src/reader.cpp
changeset 0 40261b775718
child 54 b68f3e90dca1
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     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 // Creates a new CReader object.
       
    20 //
       
    21 CReader::CReader(const TDesC8& aBuffer, TReaderType aType)
       
    22  :	iBuffer(aBuffer),
       
    23  	iType(aType)
       
    24 	{
       
    25 	}
       
    26 
       
    27 //
       
    28 // Returns ETrue is aPattern is in the buffer, EFalse otherwise.
       
    29 //
       
    30 TBool CReader::Match(const TDesC8& aPattern)
       
    31 	{
       
    32 	return (iBuffer.Match(aPattern) != KErrNotFound);
       
    33 	}
       
    34 
       
    35 
       
    36 //
       
    37 // Reads aBuf.Length() bytes into a descriptor.
       
    38 //
       
    39 void CReader::ReadBytesL(TDes8& aBuf)
       
    40 	{
       
    41 	TInt bufLen = aBuf.Length();
       
    42 	
       
    43 	if (!CheckEnoughData(bufLen))
       
    44 		{
       
    45 		User::Leave(KErrEof);
       
    46 		}
       
    47 	
       
    48 	for (TInt i = 0; i < bufLen; i++)
       
    49 		{
       
    50 		aBuf[i] = iBuffer[iBufPos++];
       
    51 		}
       
    52 	}
       
    53 
       
    54 
       
    55 //
       
    56 // Reads a byte from the buffer.
       
    57 //
       
    58 void CReader::ReadByteL(TUint8& aData)
       
    59 	{
       
    60 	if (!CheckEnoughData(sizeof(TUint8)))
       
    61 		{
       
    62 		User::Leave(KErrEof);
       
    63 		}
       
    64 			
       
    65 	aData = iBuffer[iBufPos++];
       
    66 	}
       
    67 
       
    68 //
       
    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.
       
    73 //
       
    74 void CReader::Read16L(TUint16& aData, TBool aLittleEndian)
       
    75 	{
       
    76 	if (!CheckEnoughData(sizeof(TUint16)))
       
    77 		{
       
    78 		User::Leave(KErrEof);
       
    79 		}
       
    80 	
       
    81 	TUint8 a = iBuffer[iBufPos++];
       
    82 	TUint8 b = iBuffer[iBufPos++];
       
    83 	
       
    84 	if (aLittleEndian)
       
    85 		{
       
    86 		aData = (b << 8) | a;
       
    87 		}
       
    88 	else
       
    89 		{
       
    90 		aData = (a << 8) | b;
       
    91 		}
       
    92 	}
       
    93 
       
    94 
       
    95 //
       
    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.
       
   100 //
       
   101 void CReader::Read32L(TUint32& aData, TBool aLittleEndian)
       
   102 	{
       
   103 	if (!CheckEnoughData(sizeof(TUint32)))
       
   104 		{
       
   105 		User::Leave(KErrEof);
       
   106 		}
       
   107 	
       
   108 	TUint8 a = iBuffer[iBufPos++];
       
   109 	TUint8 b = iBuffer[iBufPos++];
       
   110 	TUint8 c = iBuffer[iBufPos++];
       
   111 	TUint8 d = iBuffer[iBufPos++];
       
   112 	
       
   113 	if (aLittleEndian)
       
   114 		{
       
   115 		aData = MAKE_INT32(d, c, b, a);
       
   116 		}
       
   117 	else
       
   118 		{
       
   119 		aData = MAKE_INT32(a, b, c, d);
       
   120 		}
       
   121 	}
       
   122 
       
   123 
       
   124 //
       
   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.
       
   129 //
       
   130 void CReader::Read64L(TInt64& aData, TBool aLittleEndian)
       
   131 	{
       
   132 	TUint32 high;
       
   133 	TUint32 low;
       
   134 	
       
   135 	Read32L(high, aLittleEndian);
       
   136 	Read32L(low, aLittleEndian);
       
   137 	
       
   138 	if (aLittleEndian)
       
   139 		{
       
   140 		aData = MAKE_TINT64(low, high);
       
   141 		}
       
   142 	else
       
   143 		{
       
   144 		aData = MAKE_TINT64(high, low);
       
   145 		}
       
   146 	}
       
   147 
       
   148 
       
   149 //
       
   150 //
       
   151 //
       
   152 TBool CReader::CheckEnoughData(TInt aNeeded)
       
   153 	{
       
   154 	return ((iBufPos + aNeeded) <= iBuffer.Length());
       
   155 	}
       
   156 
       
   157 
       
   158 //
       
   159 // Skips forwards or backwards aOffset number of bytes.
       
   160 //
       
   161 TInt CReader::Seek(TInt aOffset)
       
   162 	{
       
   163 	TInt newBufPos = iBufPos + aOffset;
       
   164 	
       
   165 	if ((newBufPos < 0) || (newBufPos >= iBuffer.Length()))
       
   166 		{
       
   167 		// Trying to seek past the bounds of the buffer.
       
   168 		return KErrUnderflow;
       
   169 		}
       
   170 		
       
   171 	iBufPos += aOffset;
       
   172 	return KErrNone;
       
   173 	}
       
   174 	
       
   175 
       
   176 
       
   177