os/mm/mmlibs/mmfw/Recogniser/src/filereader.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/filereader.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,216 @@
     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 +//
    1.23 +//
    1.24 +CFileReader::CFileReader(RFile* aFile)
    1.25 + :	CBufferReader(iFileBuffer, CReader::EFile),
    1.26 + 	iFile(aFile)
    1.27 +	{
    1.28 +	}
    1.29 +
    1.30 +
    1.31 +//
    1.32 +//
    1.33 +//
    1.34 +CFileReader::~CFileReader()
    1.35 +	{
    1.36 +	}
    1.37 +
    1.38 +
    1.39 +//
    1.40 +//
    1.41 +//
    1.42 +CFileReader* CFileReader::NewLC(RFile* aFile)
    1.43 +	{
    1.44 +	CFileReader* self = new(ELeave) CFileReader(aFile);
    1.45 +	CleanupStack::PushL(self);
    1.46 +	self->ConstructL();
    1.47 +	return self;
    1.48 +	}
    1.49 +
    1.50 +
    1.51 +//
    1.52 +//
    1.53 +//
    1.54 +void CFileReader::ConstructL()
    1.55 +	{
    1.56 +	TInt pos = 0;
    1.57 +	User::LeaveIfError(iFile->Seek(ESeekStart, pos));
    1.58 +	User::LeaveIfError(iFile->Read(iFileBuffer));
    1.59 +	}
    1.60 +
    1.61 +
    1.62 +//
    1.63 +// Checks if there is aAmount of data left in the buffer.
    1.64 +// It is important to call the base-class implementation first
    1.65 +// to ensure correct operation.
    1.66 +//
    1.67 +TBool CFileReader::CheckEnoughData(TInt aAmount)
    1.68 +	{
    1.69 +	if (CBufferReader::CheckEnoughData(aAmount))
    1.70 +		{
    1.71 +		return ETrue;
    1.72 +		}
    1.73 +		
    1.74 +	// Try to read more data.
    1.75 +	TInt bufPos = CBufferReader::Position();
    1.76 +	TInt err = PhysicallySeekAndRead(bufPos - iFileBuffer.Length());
    1.77 +	if (err == KErrNone)
    1.78 +		{
    1.79 +		// The read may have succeeded but that 
    1.80 +		// still doesn't mean we have enough data.
    1.81 +		return (aAmount <= iFileBuffer.Length());
    1.82 +		}
    1.83 +	
    1.84 +	return EFalse;
    1.85 +	}
    1.86 +
    1.87 +
    1.88 +//
    1.89 +//
    1.90 +//
    1.91 +void CFileReader::Reset()
    1.92 +	{
    1.93 +	CBufferReader::Reset(); // This will reset iBufPos.
    1.94 +	
    1.95 +	if (iFilePos != 0)
    1.96 +		{
    1.97 +		// We need to seek to the start and fill the buffer.
    1.98 +		iFilePos = 0;
    1.99 +		TInt filepos = 0;
   1.100 +		TInt err = iFile->Seek(ESeekStart, filepos);
   1.101 +		if (err == KErrNone)
   1.102 +			{
   1.103 +			err = iFile->Read(iFileBuffer);
   1.104 +			}
   1.105 +			
   1.106 +		if (err != KErrNone)
   1.107 +			{
   1.108 +			iFileBuffer.Zero();
   1.109 +			}
   1.110 +		}
   1.111 +	else
   1.112 +		{
   1.113 +		// There's no need to seek and read.
   1.114 +		iFilePos = 0;
   1.115 +		}
   1.116 +	}
   1.117 +
   1.118 +
   1.119 +//
   1.120 +//
   1.121 +//
   1.122 +void CFileReader::SeekL(TInt aOffset)
   1.123 +	{
   1.124 +	TInt err = CReader::Seek(aOffset);
   1.125 +	if (err == KErrUnderflow)
   1.126 +		{
   1.127 +		TInt bufPos = CBufferReader::Position();
   1.128 +		aOffset += bufPos - iFileBuffer.Length();
   1.129 +		User::LeaveIfError(PhysicallySeekAndRead(aOffset));
   1.130 +		}
   1.131 +	}
   1.132 +
   1.133 +
   1.134 +//
   1.135 +// It could be possible for a 64-bit field in formats such as MPEG4
   1.136 +// to have values that would fit in a 32-bit variable. In this case
   1.137 +// we can use it for seeking. This function checks if a 64-bit value
   1.138 +// is compatible with RFile's 32-bit operations.
   1.139 +//
   1.140 +void CFileReader::SeekL(TInt64 aOffset)
   1.141 +	{
   1.142 +	if (aOffset < KMinTInt64)
   1.143 +		{
   1.144 +		User::Leave(KErrNotSupported);
   1.145 +		}
   1.146 +		
   1.147 +	if (aOffset > KMaxTInt64)
   1.148 +		{
   1.149 +		User::Leave(KErrNotSupported);
   1.150 +		}
   1.151 +	
   1.152 +	if (aOffset < (TInt64)KMaxTInt)
   1.153 +	    {
   1.154 +        TInt low = (TInt)I64LOW(aOffset);
   1.155 +        SeekL(low);
   1.156 +	    }
   1.157 +	else
   1.158 +	    {
   1.159 +        TInt err = CReader::Seek(aOffset);
   1.160 +        if (err == KErrUnderflow)
   1.161 +            {
   1.162 +            TInt64 bufPos = CBufferReader::Position();
   1.163 +            aOffset += bufPos - iFileBuffer.Length();
   1.164 +            User::LeaveIfError(PhysicallySeekAndRead(aOffset));
   1.165 +            }
   1.166 +	    }
   1.167 +	}
   1.168 +	
   1.169 +//
   1.170 +// This function seeks forward/backward aOffset bytes
   1.171 +// and fills the buffer from that point.
   1.172 +//
   1.173 +TInt CFileReader::PhysicallySeekAndRead(TInt aOffset)
   1.174 +	{
   1.175 +	TInt err;
   1.176 +	
   1.177 +	// New buffer contents so read from the start of it.
   1.178 +	CBufferReader::Reset();
   1.179 +	
   1.180 +	iFilePos = aOffset;
   1.181 +	err = iFile->Seek(ESeekCurrent, aOffset);
   1.182 +	
   1.183 +	if (err != KErrNone)
   1.184 +		{
   1.185 +		return err;
   1.186 +		}
   1.187 +
   1.188 +	err = iFile->Read(iFileBuffer);
   1.189 +	if (err != KErrNone)
   1.190 +		{
   1.191 +		return err;
   1.192 +		}
   1.193 +	return (iFileBuffer.Length() == 0 ? KErrEof : KErrNone);
   1.194 +	}
   1.195 +TInt CFileReader::PhysicallySeekAndRead(TInt64 aOffset)
   1.196 +    {
   1.197 +    TInt err;
   1.198 +    // New buffer contents so read from the start of it.
   1.199 +    CBufferReader::Reset();
   1.200 +    
   1.201 +    iFilePos = aOffset;
   1.202 +    RFile64* tempfile;
   1.203 +    tempfile = static_cast<RFile64*> (iFile);
   1.204 + 
   1.205 +    err = tempfile->Seek(ESeekCurrent, iFilePos);
   1.206 +	if (err != KErrNone)
   1.207 +		{
   1.208 +		return err;
   1.209 +		}
   1.210 +
   1.211 +	err = iFile->Read(iFileBuffer);
   1.212 +	if (err != KErrNone)
   1.213 +		{
   1.214 +		return err;
   1.215 +		}
   1.216 +		
   1.217 +	return (iFileBuffer.Length() == 0 ? KErrEof : KErrNone);
   1.218 +	}
   1.219 +