os/ossrv/genericservices/mimerecognitionfw/rec/RECTXT.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/mimerecognitionfw/rec/RECTXT.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,120 @@
     1.4 +// Copyright (c) 1997-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 <apmrec.h>
    1.20 +#include <apmstd.h>
    1.21 +#include "RECTXT.H"
    1.22 +#include <ecom/implementationproxy.h>
    1.23 +
    1.24 +const TUid KUidMimeTxtRecognizer={0x100012FB};
    1.25 +const TInt KMinBufferLength=42;  // minimum amount of file needed to determine a text file IF it's not called .TXT
    1.26 +const TInt KMaxBufferLength=1024; // maximum amount of buffer space we will ever use
    1.27 +_LIT8(KDataTypeTextPlain,"text/plain");
    1.28 +_LIT(KTextFileExt,".txt");
    1.29 +
    1.30 +CApaTextRecognizer::CApaTextRecognizer()
    1.31 +	:CApaDataRecognizerType(KUidMimeTxtRecognizer,CApaDataRecognizerType::ELow)
    1.32 +	// Text files are low recognition - they don't have a clear signature
    1.33 +	{
    1.34 +	iCountDataTypes=1;
    1.35 +	}
    1.36 +
    1.37 +TUint CApaTextRecognizer::PreferredBufSize()
    1.38 +	{
    1.39 +	return KMaxBufferLength;
    1.40 +	}
    1.41 +
    1.42 +#if defined(_DEBUG)
    1.43 +TDataType CApaTextRecognizer::SupportedDataTypeL(TInt aIndex) const
    1.44 +#else
    1.45 +TDataType CApaTextRecognizer::SupportedDataTypeL(TInt /*aIndex*/) const
    1.46 +#endif
    1.47 +	{
    1.48 +	__ASSERT_DEBUG(aIndex==0,User::Invariant());
    1.49 +	return TDataType(KDataTypeTextPlain);
    1.50 +	}
    1.51 +
    1.52 +void CApaTextRecognizer::DoRecognizeL(const TDesC& aName, const TDesC8& aBuffer)
    1.53 +	{
    1.54 +	TBool nameRecognized=EFalse;
    1.55 +    
    1.56 +	// check if the file has valid UIDs 
    1.57 +	if (aBuffer.Length() >= 16)
    1.58 +		{
    1.59 +		// if the first 3 bytes are valid UIDs,then this file is not a plain/text. 
    1.60 +		// Set iConfidence appropriately and exit.
    1.61 +		const TCheckedUid checkUid(aBuffer.Left(16));    
    1.62 +		if (checkUid.UidType().IsValid())
    1.63 +			{
    1.64 +			iConfidence=ENotRecognized;
    1.65 +			return;
    1.66 +			}
    1.67 +		}
    1.68 +
    1.69 +	if (aName.Length()>4)
    1.70 +		{
    1.71 +		nameRecognized=(aName.Right(4).CompareF(KTextFileExt)==0);
    1.72 +		}
    1.73 +	const TInt length=Min(aBuffer.Length(), KMaxBufferLength);
    1.74 +	if (length<KMinBufferLength && !nameRecognized)
    1.75 +		return;
    1.76 +	TInt ii;
    1.77 +	for (ii=0; ii<length; ii++)
    1.78 +		{
    1.79 +		const TUint chr=aBuffer[ii];
    1.80 +		// these are guesses of what WON'T be in a text file
    1.81 +		if (chr<9 || chr==11 || chr==12 || (chr>13 && chr<32))
    1.82 +			{
    1.83 +			break;
    1.84 +			}
    1.85 +		if (chr>148)
    1.86 +			{
    1.87 +			break;
    1.88 +			}
    1.89 +		}
    1.90 +	const TBool validChars=(ii==length);
    1.91 +	
    1.92 +	if (nameRecognized)
    1.93 +		{
    1.94 +		iConfidence=validChars? EProbable : EUnlikely;
    1.95 +		}
    1.96 +	else
    1.97 +		{
    1.98 +		if (!validChars)
    1.99 +			{
   1.100 +			return;
   1.101 +			}
   1.102 +		iConfidence=EPossible;
   1.103 +		}
   1.104 +	iDataType=TDataType(KDataTypeTextPlain);
   1.105 +	}
   1.106 +
   1.107 +CApaDataRecognizerType* CApaTextRecognizer::CreateRecognizerL()
   1.108 +	{
   1.109 +	return new (ELeave) CApaTextRecognizer();
   1.110 +	}
   1.111 +
   1.112 +const TImplementationProxy ImplementationTable[] = 
   1.113 +    {
   1.114 +	IMPLEMENTATION_PROXY_ENTRY(0x101F7DA0,CApaTextRecognizer::CreateRecognizerL)
   1.115 +	};
   1.116 +
   1.117 +EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
   1.118 +    {
   1.119 +    aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
   1.120 +    return ImplementationTable;
   1.121 +    }
   1.122 +
   1.123 +