crypto/weakcryptospi/test/tcryptospi/src/filecompare.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalTechnology
       
    22 */
       
    23 #include "filecompare.h"
       
    24 
       
    25 #include "cryptospi/cryptospidef.h"
       
    26 #include <e32std.h>
       
    27 #include <e32def.h>
       
    28 #include <f32file.h>
       
    29 
       
    30 const TInt KCompareBlockSize = 0xff;
       
    31 
       
    32 //const TInt KCompareEqual = 0;
       
    33 const TInt KCompareGreaterThan = 1;
       
    34 const TInt KCompareLessThan = -1;
       
    35 
       
    36 // a simple utility class to compare 2 files, returns
       
    37 TInt TFileCompare::CompareL(TPtrC aFile1Path, TPtrC aFile2Path)
       
    38 	{
       
    39 	// assume is all well, compare always 
       
    40 	TInt retVal = 0;
       
    41 
       
    42 	RFs 	rFs;
       
    43 	RFile	file1;
       
    44 	RFile	file2;
       
    45 
       
    46 	User::LeaveIfError(rFs.Connect());
       
    47 	CleanupClosePushL(rFs);
       
    48 	User::LeaveIfError(file1.Open(rFs, aFile1Path, EFileRead));
       
    49 	CleanupClosePushL(file1);
       
    50 	User::LeaveIfError(file2.Open(rFs, aFile2Path, EFileRead));
       
    51 	CleanupClosePushL(file2);
       
    52 	
       
    53 	TInt file1Size;
       
    54 	file1.Size(file1Size); 
       
    55 	
       
    56 	TInt file2Size;
       
    57 	file2.Size(file2Size);
       
    58 
       
    59 	// bail out if the sizes of the 2 files are different
       
    60 	if(file1Size == file2Size)
       
    61 		{
       
    62 				
       
    63 		TBuf8<KCompareBlockSize> tempBuf1;
       
    64 		TBuf8<KCompareBlockSize> tempBuf2;
       
    65 	
       
    66 		TInt compareBlockSize;
       
    67 		TInt loopCount;
       
    68 		
       
    69 		if(file1Size > KCompareBlockSize)
       
    70 		{
       
    71 			compareBlockSize = KCompareBlockSize;
       
    72 			loopCount = file1Size / compareBlockSize;
       
    73 		}
       
    74 		else
       
    75 		{
       
    76 			loopCount = 1;
       
    77 			compareBlockSize = file1Size;
       
    78 		}
       
    79 			
       
    80 	
       
    81 		// read through the files comparing each block
       
    82 		for(TInt i = 0; i < loopCount; i++)
       
    83 			{
       
    84 			
       
    85 			file1.Read(tempBuf1, compareBlockSize);
       
    86 			file2.Read(tempBuf2, compareBlockSize);
       
    87 			
       
    88 			// remember, 0 is return value for a successful comparison
       
    89 			
       
    90 			retVal = tempBuf1.Compare(tempBuf2);
       
    91 			if( retVal != 0 )
       
    92 				{
       
    93 				//if files fail to match, break out of the loop
       
    94 				break;
       
    95 				}		
       
    96 			}
       
    97 		}
       
    98 	else
       
    99 		{
       
   100 		if(file1Size > file2Size)
       
   101 			retVal = KCompareGreaterThan;
       
   102 		else
       
   103 			retVal = KCompareLessThan;
       
   104 		}
       
   105 
       
   106 	CleanupStack::PopAndDestroy(&file2);
       
   107 	CleanupStack::PopAndDestroy(&file1);
       
   108 	
       
   109 	CleanupStack::PopAndDestroy(&rFs);
       
   110 	
       
   111 	return retVal;		
       
   112 	}