genericservices/mimerecognitionfw/rec/RECTXT.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 <apmrec.h>
       
    17 #include <apmstd.h>
       
    18 #include "RECTXT.H"
       
    19 #include <ecom/implementationproxy.h>
       
    20 
       
    21 const TUid KUidMimeTxtRecognizer={0x100012FB};
       
    22 const TInt KMinBufferLength=42;  // minimum amount of file needed to determine a text file IF it's not called .TXT
       
    23 const TInt KMaxBufferLength=1024; // maximum amount of buffer space we will ever use
       
    24 _LIT8(KDataTypeTextPlain,"text/plain");
       
    25 _LIT(KTextFileExt,".txt");
       
    26 
       
    27 CApaTextRecognizer::CApaTextRecognizer()
       
    28 	:CApaDataRecognizerType(KUidMimeTxtRecognizer,CApaDataRecognizerType::ELow)
       
    29 	// Text files are low recognition - they don't have a clear signature
       
    30 	{
       
    31 	iCountDataTypes=1;
       
    32 	}
       
    33 
       
    34 TUint CApaTextRecognizer::PreferredBufSize()
       
    35 	{
       
    36 	return KMaxBufferLength;
       
    37 	}
       
    38 
       
    39 #if defined(_DEBUG)
       
    40 TDataType CApaTextRecognizer::SupportedDataTypeL(TInt aIndex) const
       
    41 #else
       
    42 TDataType CApaTextRecognizer::SupportedDataTypeL(TInt /*aIndex*/) const
       
    43 #endif
       
    44 	{
       
    45 	__ASSERT_DEBUG(aIndex==0,User::Invariant());
       
    46 	return TDataType(KDataTypeTextPlain);
       
    47 	}
       
    48 
       
    49 void CApaTextRecognizer::DoRecognizeL(const TDesC& aName, const TDesC8& aBuffer)
       
    50 	{
       
    51 	TBool nameRecognized=EFalse;
       
    52     
       
    53 	// check if the file has valid UIDs 
       
    54 	if (aBuffer.Length() >= 16)
       
    55 		{
       
    56 		// if the first 3 bytes are valid UIDs,then this file is not a plain/text. 
       
    57 		// Set iConfidence appropriately and exit.
       
    58 		const TCheckedUid checkUid(aBuffer.Left(16));    
       
    59 		if (checkUid.UidType().IsValid())
       
    60 			{
       
    61 			iConfidence=ENotRecognized;
       
    62 			return;
       
    63 			}
       
    64 		}
       
    65 
       
    66 	if (aName.Length()>4)
       
    67 		{
       
    68 		nameRecognized=(aName.Right(4).CompareF(KTextFileExt)==0);
       
    69 		}
       
    70 	const TInt length=Min(aBuffer.Length(), KMaxBufferLength);
       
    71 	if (length<KMinBufferLength && !nameRecognized)
       
    72 		return;
       
    73 	TInt ii;
       
    74 	for (ii=0; ii<length; ii++)
       
    75 		{
       
    76 		const TUint chr=aBuffer[ii];
       
    77 		// these are guesses of what WON'T be in a text file
       
    78 		if (chr<9 || chr==11 || chr==12 || (chr>13 && chr<32))
       
    79 			{
       
    80 			break;
       
    81 			}
       
    82 		if (chr>148)
       
    83 			{
       
    84 			break;
       
    85 			}
       
    86 		}
       
    87 	const TBool validChars=(ii==length);
       
    88 	
       
    89 	if (nameRecognized)
       
    90 		{
       
    91 		iConfidence=validChars? EProbable : EUnlikely;
       
    92 		}
       
    93 	else
       
    94 		{
       
    95 		if (!validChars)
       
    96 			{
       
    97 			return;
       
    98 			}
       
    99 		iConfidence=EPossible;
       
   100 		}
       
   101 	iDataType=TDataType(KDataTypeTextPlain);
       
   102 	}
       
   103 
       
   104 CApaDataRecognizerType* CApaTextRecognizer::CreateRecognizerL()
       
   105 	{
       
   106 	return new (ELeave) CApaTextRecognizer();
       
   107 	}
       
   108 
       
   109 const TImplementationProxy ImplementationTable[] = 
       
   110     {
       
   111 	IMPLEMENTATION_PROXY_ENTRY(0x101F7DA0,CApaTextRecognizer::CreateRecognizerL)
       
   112 	};
       
   113 
       
   114 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   115     {
       
   116     aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   117     return ImplementationTable;
       
   118     }
       
   119 
       
   120