|
1 // Copyright (c) 2002-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 // $Header$ |
|
15 // |
|
16 // |
|
17 |
|
18 #include "InFile.h" // This module |
|
19 |
|
20 //----------------------------------------------------------------------------- |
|
21 // Gets a file. Leaves in case of any failure. |
|
22 //----------------------------------------------------------------------------- |
|
23 |
|
24 HBufC8* InFile::GetL( const TDesC& aFile ) |
|
25 { |
|
26 |
|
27 // Connect to file server, Leave if fails. |
|
28 RFs fs; |
|
29 TInt r; |
|
30 if ( r = fs.Connect(), r != KErrNone ) |
|
31 User::Leave(r); |
|
32 |
|
33 // Open the file for reading, Leave if fails. |
|
34 RFile file; |
|
35 if ( r = file.Open( fs, aFile, EFileRead ), r != KErrNone ) |
|
36 { |
|
37 fs.Close(); |
|
38 User::Leave(r); |
|
39 } |
|
40 |
|
41 // Get file size, Leave if fails. |
|
42 TInt size; |
|
43 if ( r = file.Size( size ), r != KErrNone ) |
|
44 { |
|
45 file.Close(); |
|
46 fs.Close(); |
|
47 User::Leave(r); |
|
48 } |
|
49 |
|
50 // Allocate heap buffer long enough. Leave if fails. |
|
51 HBufC8* hbuf = HBufC8::NewMax(size); |
|
52 if ( hbuf == NULL ) |
|
53 { |
|
54 file.Close(); |
|
55 fs.Close(); |
|
56 User::Leave(KErrNoMemory); |
|
57 } |
|
58 |
|
59 // Read whole file if there's data. |
|
60 if ( size > 0 ) |
|
61 { |
|
62 TPtr8 temp(NULL,0); |
|
63 temp.Set(hbuf->Des()); |
|
64 r = file.Read(temp); |
|
65 } |
|
66 |
|
67 // Close file and disconnect from file server. |
|
68 file.Close(); |
|
69 fs.Close(); |
|
70 |
|
71 // Delete the buffer and leave if read failed. |
|
72 if ( r != KErrNone ) |
|
73 { |
|
74 delete hbuf; |
|
75 User::Leave(r); |
|
76 } |
|
77 |
|
78 // Successful, return pointer to heap buffer. |
|
79 return hbuf; |
|
80 } |
|
81 |
|
82 //----------------------------------------------------------------------------- |
|
83 |
|
84 HBufC8* InFile::GetL( const TDesC& aPath, const TDesC& aFile ) |
|
85 { |
|
86 TParse parse; |
|
87 TInt r = parse.Set( aFile, NULL, &aPath ); |
|
88 if ( r != KErrNone ) |
|
89 User::Leave(r); |
|
90 return GetL( parse.FullName() ); |
|
91 } |
|
92 |
|
93 //----------------------------------------------------------------------------- |
|
94 // End of File |
|
95 //----------------------------------------------------------------------------- |