os/mm/mmlibs/mmfw/Recogniser/src/filereader.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
//
sl@0
    20
//
sl@0
    21
CFileReader::CFileReader(RFile* aFile)
sl@0
    22
 :	CBufferReader(iFileBuffer, CReader::EFile),
sl@0
    23
 	iFile(aFile)
sl@0
    24
	{
sl@0
    25
	}
sl@0
    26
sl@0
    27
sl@0
    28
//
sl@0
    29
//
sl@0
    30
//
sl@0
    31
CFileReader::~CFileReader()
sl@0
    32
	{
sl@0
    33
	}
sl@0
    34
sl@0
    35
sl@0
    36
//
sl@0
    37
//
sl@0
    38
//
sl@0
    39
CFileReader* CFileReader::NewLC(RFile* aFile)
sl@0
    40
	{
sl@0
    41
	CFileReader* self = new(ELeave) CFileReader(aFile);
sl@0
    42
	CleanupStack::PushL(self);
sl@0
    43
	self->ConstructL();
sl@0
    44
	return self;
sl@0
    45
	}
sl@0
    46
sl@0
    47
sl@0
    48
//
sl@0
    49
//
sl@0
    50
//
sl@0
    51
void CFileReader::ConstructL()
sl@0
    52
	{
sl@0
    53
	TInt pos = 0;
sl@0
    54
	User::LeaveIfError(iFile->Seek(ESeekStart, pos));
sl@0
    55
	User::LeaveIfError(iFile->Read(iFileBuffer));
sl@0
    56
	}
sl@0
    57
sl@0
    58
sl@0
    59
//
sl@0
    60
// Checks if there is aAmount of data left in the buffer.
sl@0
    61
// It is important to call the base-class implementation first
sl@0
    62
// to ensure correct operation.
sl@0
    63
//
sl@0
    64
TBool CFileReader::CheckEnoughData(TInt aAmount)
sl@0
    65
	{
sl@0
    66
	if (CBufferReader::CheckEnoughData(aAmount))
sl@0
    67
		{
sl@0
    68
		return ETrue;
sl@0
    69
		}
sl@0
    70
		
sl@0
    71
	// Try to read more data.
sl@0
    72
	TInt bufPos = CBufferReader::Position();
sl@0
    73
	TInt err = PhysicallySeekAndRead(bufPos - iFileBuffer.Length());
sl@0
    74
	if (err == KErrNone)
sl@0
    75
		{
sl@0
    76
		// The read may have succeeded but that 
sl@0
    77
		// still doesn't mean we have enough data.
sl@0
    78
		return (aAmount <= iFileBuffer.Length());
sl@0
    79
		}
sl@0
    80
	
sl@0
    81
	return EFalse;
sl@0
    82
	}
sl@0
    83
sl@0
    84
sl@0
    85
//
sl@0
    86
//
sl@0
    87
//
sl@0
    88
void CFileReader::Reset()
sl@0
    89
	{
sl@0
    90
	CBufferReader::Reset(); // This will reset iBufPos.
sl@0
    91
	
sl@0
    92
	if (iFilePos != 0)
sl@0
    93
		{
sl@0
    94
		// We need to seek to the start and fill the buffer.
sl@0
    95
		iFilePos = 0;
sl@0
    96
		TInt filepos = 0;
sl@0
    97
		TInt err = iFile->Seek(ESeekStart, filepos);
sl@0
    98
		if (err == KErrNone)
sl@0
    99
			{
sl@0
   100
			err = iFile->Read(iFileBuffer);
sl@0
   101
			}
sl@0
   102
			
sl@0
   103
		if (err != KErrNone)
sl@0
   104
			{
sl@0
   105
			iFileBuffer.Zero();
sl@0
   106
			}
sl@0
   107
		}
sl@0
   108
	else
sl@0
   109
		{
sl@0
   110
		// There's no need to seek and read.
sl@0
   111
		iFilePos = 0;
sl@0
   112
		}
sl@0
   113
	}
sl@0
   114
sl@0
   115
sl@0
   116
//
sl@0
   117
//
sl@0
   118
//
sl@0
   119
void CFileReader::SeekL(TInt aOffset)
sl@0
   120
	{
sl@0
   121
	TInt err = CReader::Seek(aOffset);
sl@0
   122
	if (err == KErrUnderflow)
sl@0
   123
		{
sl@0
   124
		TInt bufPos = CBufferReader::Position();
sl@0
   125
		aOffset += bufPos - iFileBuffer.Length();
sl@0
   126
		User::LeaveIfError(PhysicallySeekAndRead(aOffset));
sl@0
   127
		}
sl@0
   128
	}
sl@0
   129
sl@0
   130
sl@0
   131
//
sl@0
   132
// It could be possible for a 64-bit field in formats such as MPEG4
sl@0
   133
// to have values that would fit in a 32-bit variable. In this case
sl@0
   134
// we can use it for seeking. This function checks if a 64-bit value
sl@0
   135
// is compatible with RFile's 32-bit operations.
sl@0
   136
//
sl@0
   137
void CFileReader::SeekL(TInt64 aOffset)
sl@0
   138
	{
sl@0
   139
	if (aOffset < KMinTInt64)
sl@0
   140
		{
sl@0
   141
		User::Leave(KErrNotSupported);
sl@0
   142
		}
sl@0
   143
		
sl@0
   144
	if (aOffset > KMaxTInt64)
sl@0
   145
		{
sl@0
   146
		User::Leave(KErrNotSupported);
sl@0
   147
		}
sl@0
   148
	
sl@0
   149
	if (aOffset < (TInt64)KMaxTInt)
sl@0
   150
	    {
sl@0
   151
        TInt low = (TInt)I64LOW(aOffset);
sl@0
   152
        SeekL(low);
sl@0
   153
	    }
sl@0
   154
	else
sl@0
   155
	    {
sl@0
   156
        TInt err = CReader::Seek(aOffset);
sl@0
   157
        if (err == KErrUnderflow)
sl@0
   158
            {
sl@0
   159
            TInt64 bufPos = CBufferReader::Position();
sl@0
   160
            aOffset += bufPos - iFileBuffer.Length();
sl@0
   161
            User::LeaveIfError(PhysicallySeekAndRead(aOffset));
sl@0
   162
            }
sl@0
   163
	    }
sl@0
   164
	}
sl@0
   165
	
sl@0
   166
//
sl@0
   167
// This function seeks forward/backward aOffset bytes
sl@0
   168
// and fills the buffer from that point.
sl@0
   169
//
sl@0
   170
TInt CFileReader::PhysicallySeekAndRead(TInt aOffset)
sl@0
   171
	{
sl@0
   172
	TInt err;
sl@0
   173
	
sl@0
   174
	// New buffer contents so read from the start of it.
sl@0
   175
	CBufferReader::Reset();
sl@0
   176
	
sl@0
   177
	iFilePos = aOffset;
sl@0
   178
	err = iFile->Seek(ESeekCurrent, aOffset);
sl@0
   179
	
sl@0
   180
	if (err != KErrNone)
sl@0
   181
		{
sl@0
   182
		return err;
sl@0
   183
		}
sl@0
   184
sl@0
   185
	err = iFile->Read(iFileBuffer);
sl@0
   186
	if (err != KErrNone)
sl@0
   187
		{
sl@0
   188
		return err;
sl@0
   189
		}
sl@0
   190
	return (iFileBuffer.Length() == 0 ? KErrEof : KErrNone);
sl@0
   191
	}
sl@0
   192
TInt CFileReader::PhysicallySeekAndRead(TInt64 aOffset)
sl@0
   193
    {
sl@0
   194
    TInt err;
sl@0
   195
    // New buffer contents so read from the start of it.
sl@0
   196
    CBufferReader::Reset();
sl@0
   197
    
sl@0
   198
    iFilePos = aOffset;
sl@0
   199
    RFile64* tempfile;
sl@0
   200
    tempfile = static_cast<RFile64*> (iFile);
sl@0
   201
 
sl@0
   202
    err = tempfile->Seek(ESeekCurrent, iFilePos);
sl@0
   203
	if (err != KErrNone)
sl@0
   204
		{
sl@0
   205
		return err;
sl@0
   206
		}
sl@0
   207
sl@0
   208
	err = iFile->Read(iFileBuffer);
sl@0
   209
	if (err != KErrNone)
sl@0
   210
		{
sl@0
   211
		return err;
sl@0
   212
		}
sl@0
   213
		
sl@0
   214
	return (iFileBuffer.Length() == 0 ? KErrEof : KErrNone);
sl@0
   215
	}
sl@0
   216