os/mm/mmlibs/mmfw/Recogniser/src/reader.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "readers.h"
sl@0
    17
sl@0
    18
//
sl@0
    19
// Creates a new CReader object.
sl@0
    20
//
sl@0
    21
CReader::CReader(const TDesC8& aBuffer, TReaderType aType)
sl@0
    22
 :	iBuffer(aBuffer),
sl@0
    23
 	iType(aType)
sl@0
    24
	{
sl@0
    25
	}
sl@0
    26
sl@0
    27
//
sl@0
    28
// Returns ETrue is aPattern is in the buffer, EFalse otherwise.
sl@0
    29
//
sl@0
    30
TBool CReader::Match(const TDesC8& aPattern)
sl@0
    31
	{
sl@0
    32
	return (iBuffer.Match(aPattern) != KErrNotFound);
sl@0
    33
	}
sl@0
    34
sl@0
    35
sl@0
    36
//
sl@0
    37
// Reads aBuf.Length() bytes into a descriptor.
sl@0
    38
//
sl@0
    39
void CReader::ReadBytesL(TDes8& aBuf)
sl@0
    40
	{
sl@0
    41
	TInt bufLen = aBuf.Length();
sl@0
    42
	
sl@0
    43
	if (!CheckEnoughData(bufLen))
sl@0
    44
		{
sl@0
    45
		User::Leave(KErrEof);
sl@0
    46
		}
sl@0
    47
	
sl@0
    48
	for (TInt i = 0; i < bufLen; i++)
sl@0
    49
		{
sl@0
    50
		aBuf[i] = iBuffer[iBufPos++];
sl@0
    51
		}
sl@0
    52
	}
sl@0
    53
sl@0
    54
sl@0
    55
//
sl@0
    56
// Reads a byte from the buffer.
sl@0
    57
//
sl@0
    58
void CReader::ReadByteL(TUint8& aData)
sl@0
    59
	{
sl@0
    60
	if (!CheckEnoughData(sizeof(TUint8)))
sl@0
    61
		{
sl@0
    62
		User::Leave(KErrEof);
sl@0
    63
		}
sl@0
    64
			
sl@0
    65
	aData = iBuffer[iBufPos++];
sl@0
    66
	}
sl@0
    67
sl@0
    68
//
sl@0
    69
// Reads 16 bits of data at the current reader position.
sl@0
    70
// Some formats (mainly those developed by Microsoft) still use little-endian
sl@0
    71
// encoding of fields. The file format specifications will specify which
sl@0
    72
// byte-ordering scheme is used.
sl@0
    73
//
sl@0
    74
void CReader::Read16L(TUint16& aData, TBool aLittleEndian)
sl@0
    75
	{
sl@0
    76
	if (!CheckEnoughData(sizeof(TUint16)))
sl@0
    77
		{
sl@0
    78
		User::Leave(KErrEof);
sl@0
    79
		}
sl@0
    80
	
sl@0
    81
	TUint8 a = iBuffer[iBufPos++];
sl@0
    82
	TUint8 b = iBuffer[iBufPos++];
sl@0
    83
	
sl@0
    84
	if (aLittleEndian)
sl@0
    85
		{
sl@0
    86
		aData = (b << 8) | a;
sl@0
    87
		}
sl@0
    88
	else
sl@0
    89
		{
sl@0
    90
		aData = (a << 8) | b;
sl@0
    91
		}
sl@0
    92
	}
sl@0
    93
sl@0
    94
sl@0
    95
//
sl@0
    96
// Reads 32 bits of data at the current reader position.
sl@0
    97
// Some formats (mainly those developed by Microsoft) still use little-endian
sl@0
    98
// encoding of fields. The file format specifications will specify which
sl@0
    99
// byte-ordering scheme is used.
sl@0
   100
//
sl@0
   101
void CReader::Read32L(TUint32& aData, TBool aLittleEndian)
sl@0
   102
	{
sl@0
   103
	if (!CheckEnoughData(sizeof(TUint32)))
sl@0
   104
		{
sl@0
   105
		User::Leave(KErrEof);
sl@0
   106
		}
sl@0
   107
	
sl@0
   108
	TUint8 a = iBuffer[iBufPos++];
sl@0
   109
	TUint8 b = iBuffer[iBufPos++];
sl@0
   110
	TUint8 c = iBuffer[iBufPos++];
sl@0
   111
	TUint8 d = iBuffer[iBufPos++];
sl@0
   112
	
sl@0
   113
	if (aLittleEndian)
sl@0
   114
		{
sl@0
   115
		aData = MAKE_INT32(d, c, b, a);
sl@0
   116
		}
sl@0
   117
	else
sl@0
   118
		{
sl@0
   119
		aData = MAKE_INT32(a, b, c, d);
sl@0
   120
		}
sl@0
   121
	}
sl@0
   122
sl@0
   123
sl@0
   124
//
sl@0
   125
// Reads 64 bits of data at the current reader position.
sl@0
   126
// Some formats (mainly those developed by Microsoft) still use little-endian
sl@0
   127
// encoding of fields. The file format specifications will specify which
sl@0
   128
// byte-ordering scheme is used.
sl@0
   129
//
sl@0
   130
void CReader::Read64L(TInt64& aData, TBool aLittleEndian)
sl@0
   131
	{
sl@0
   132
	TUint32 high;
sl@0
   133
	TUint32 low;
sl@0
   134
	
sl@0
   135
	Read32L(high, aLittleEndian);
sl@0
   136
	Read32L(low, aLittleEndian);
sl@0
   137
	
sl@0
   138
	if (aLittleEndian)
sl@0
   139
		{
sl@0
   140
		aData = MAKE_TINT64(low, high);
sl@0
   141
		}
sl@0
   142
	else
sl@0
   143
		{
sl@0
   144
		aData = MAKE_TINT64(high, low);
sl@0
   145
		}
sl@0
   146
	}
sl@0
   147
sl@0
   148
sl@0
   149
//
sl@0
   150
//
sl@0
   151
//
sl@0
   152
TBool CReader::CheckEnoughData(TInt aNeeded)
sl@0
   153
	{
sl@0
   154
	return ((iBufPos + aNeeded) <= iBuffer.Length());
sl@0
   155
	}
sl@0
   156
sl@0
   157
sl@0
   158
//
sl@0
   159
// Skips forwards or backwards aOffset number of bytes.
sl@0
   160
//
sl@0
   161
TInt CReader::Seek(TInt aOffset)
sl@0
   162
	{
sl@0
   163
	TInt newBufPos = iBufPos + aOffset;
sl@0
   164
	
sl@0
   165
	if ((newBufPos < 0) || (newBufPos >= iBuffer.Length()))
sl@0
   166
		{
sl@0
   167
		// Trying to seek past the bounds of the buffer.
sl@0
   168
		return KErrUnderflow;
sl@0
   169
		}
sl@0
   170
		
sl@0
   171
	iBufPos += aOffset;
sl@0
   172
	return KErrNone;
sl@0
   173
	}
sl@0
   174
	
sl@0
   175
//
sl@0
   176
// Skips forwards or backwards aOffset number of bytes.
sl@0
   177
//
sl@0
   178
TInt CReader::Seek(TInt64 aOffset)
sl@0
   179
    {
sl@0
   180
    TInt64 newBufPos = iBufPos + aOffset;
sl@0
   181
    
sl@0
   182
    if ((newBufPos < 0) || (newBufPos >= iBuffer.Length()))
sl@0
   183
        {
sl@0
   184
        // Trying to seek past the bounds of the buffer.
sl@0
   185
        return KErrUnderflow;
sl@0
   186
        }
sl@0
   187
        
sl@0
   188
    iBufPos += aOffset;
sl@0
   189
    return KErrNone;
sl@0
   190
    }
sl@0
   191