|
1 /* |
|
2 * Copyright (c) 1998-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 #include <s32file.h> |
|
20 #include "t_utils.h" |
|
21 |
|
22 |
|
23 const TInt KBufferSize = 1024; |
|
24 |
|
25 EXPORT_C TBool Utils::CompareFileL(const TDesC& aFileName1, const TDesC& aFileName2, TInt &aError) |
|
26 { |
|
27 RFile file1, file2; |
|
28 RFs fs; |
|
29 TInt err1, err2, size1, size2; |
|
30 TBool success = EFalse; |
|
31 |
|
32 // checks files are different |
|
33 if(aFileName1 == aFileName2) |
|
34 { |
|
35 aError = KErrNotFound; |
|
36 return(EFalse); |
|
37 }; |
|
38 |
|
39 User::LeaveIfError(fs.Connect()); |
|
40 CleanupClosePushL(fs); |
|
41 |
|
42 // opens first file |
|
43 err1 = file1.Open(fs, aFileName1, EFileRead); |
|
44 if (err1 != KErrNone) |
|
45 { |
|
46 CleanupStack::PopAndDestroy(); // fs |
|
47 aError = KErrNotFound; |
|
48 return(EFalse); |
|
49 } |
|
50 CleanupClosePushL(file1); |
|
51 |
|
52 //opens second file |
|
53 err2 = file2.Open(fs, aFileName2, EFileRead); |
|
54 if (err2 != KErrNone) |
|
55 { |
|
56 CleanupStack::PopAndDestroy(2); // fs, file1 |
|
57 aError = KErrNotFound; |
|
58 return(EFalse); |
|
59 } |
|
60 CleanupClosePushL(file2); |
|
61 |
|
62 TBool finished = EFalse; |
|
63 HBufC8 *fileBuf1 = HBufC8::NewLC(KBufferSize); |
|
64 HBufC8 *fileBuf2 = HBufC8::NewLC(KBufferSize); |
|
65 TPtr8 filePtr1(fileBuf1->Des()); |
|
66 TPtr8 filePtr2(fileBuf2->Des()); |
|
67 |
|
68 file1.Size(size1); |
|
69 file2.Size(size2); |
|
70 |
|
71 // compares size, no need to do any more checking if they are different |
|
72 if(size1 == size2) |
|
73 { |
|
74 success = ETrue; |
|
75 do |
|
76 { |
|
77 // reads in buffer from each file |
|
78 // cannot rely on max length of descriptor so pass in size of |
|
79 // buffer to read |
|
80 err1 = file1.Read(filePtr1,KBufferSize); |
|
81 err2 = file2.Read(filePtr2,KBufferSize); |
|
82 // checks if an error has occured |
|
83 if(err1 != KErrNone && err2 != KErrNone) |
|
84 { |
|
85 finished = ETrue; |
|
86 success = EFalse; |
|
87 }; |
|
88 // executes a binary compare |
|
89 if(fileBuf1->Compare(*fileBuf2)!=0) |
|
90 { |
|
91 // binary compare failed, file are different |
|
92 finished = ETrue; |
|
93 success = EFalse; |
|
94 }; |
|
95 // checks for EOF |
|
96 if(fileBuf1->Length() != KBufferSize) |
|
97 finished = ETrue; |
|
98 } |
|
99 while(!finished); |
|
100 }; |
|
101 |
|
102 |
|
103 CleanupStack::PopAndDestroy(5); // fs, file1, file2, fileBuf1, fileBuf2 |
|
104 return(success); |
|
105 }; |