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