|
1 /* |
|
2 * Copyright (c) 2003 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 * Implementation of RUnicodeFile. |
|
16 * |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 // INCLUDE FILES |
|
22 |
|
23 #include "UnicodeFile.h" |
|
24 |
|
25 // CONSTANTS |
|
26 |
|
27 /// Little-endian Unicode file header. |
|
28 LOCAL_D const TUint16 KLittleEndianUnicodeHeader = 0xFEFF; |
|
29 |
|
30 // ================= MEMBER FUNCTIONS ======================= |
|
31 |
|
32 // --------------------------------------------------------- |
|
33 // RUnicodeFile::Open |
|
34 // --------------------------------------------------------- |
|
35 // |
|
36 TInt RUnicodeFile::Open( RFs& aFs, const TDesC& aName, TUint aFileMode ) |
|
37 { |
|
38 if( aFileMode & EFileWrite ) |
|
39 { |
|
40 // This supports only read access. |
|
41 User::Leave( KErrNotSupported ); |
|
42 } |
|
43 TInt err = iFile.Open( aFs, aName, aFileMode ); |
|
44 if ( !err ) |
|
45 { |
|
46 // Check Unicode endianness. |
|
47 TUint c = 0; |
|
48 TRAPD( err, c = GetCharL() ); |
|
49 if( !err ) |
|
50 { |
|
51 if ( c != KLittleEndianUnicodeHeader ) |
|
52 { |
|
53 err = KErrNotSupported; |
|
54 } |
|
55 } |
|
56 } |
|
57 if( err ) |
|
58 { |
|
59 iFile.Close(); |
|
60 } |
|
61 return err; |
|
62 } |
|
63 |
|
64 // --------------------------------------------------------- |
|
65 // RUnicodeFile::Replace |
|
66 // --------------------------------------------------------- |
|
67 // |
|
68 TInt RUnicodeFile::ReplaceL( RFs& aFs, const TDesC& aName, TUint aFileMode ) |
|
69 { |
|
70 if( aFileMode & EFileRead ) |
|
71 { |
|
72 // This supports only write access. |
|
73 User::Leave( KErrNotSupported ); |
|
74 } |
|
75 TInt err = iFile.Replace( aFs, aName, aFileMode ); |
|
76 if ( !err ) |
|
77 { |
|
78 // Write Unicode header. |
|
79 err = iFile.Write( TPtrC8( (TUint8*)&KLittleEndianUnicodeHeader, 2 ) ); |
|
80 } |
|
81 if( err ) |
|
82 { |
|
83 iFile.Close(); |
|
84 } |
|
85 return err; |
|
86 } |
|
87 |
|
88 // --------------------------------------------------------- |
|
89 // RUnicodeFile::Close |
|
90 // --------------------------------------------------------- |
|
91 // |
|
92 void RUnicodeFile::Close() |
|
93 { |
|
94 iFile.Close(); |
|
95 } |
|
96 |
|
97 // --------------------------------------------------------- |
|
98 // RUnicodeFile::GetCharL |
|
99 // --------------------------------------------------------- |
|
100 // |
|
101 TUint RUnicodeFile::GetCharL() |
|
102 { |
|
103 TUint c = 0; |
|
104 TPtr8 ptr( (TUint8*)(&c), 2 ); |
|
105 User::LeaveIfError( iFile.Read( ptr, 2 ) ); |
|
106 if( ptr.Length() == 0 ) |
|
107 { |
|
108 // Nothing is read, successfully: this must be EOF. |
|
109 c = 0; |
|
110 } |
|
111 else if( ptr.Length() == 1 ) |
|
112 { |
|
113 // Odd number of bytes in Unicode file? |
|
114 User::Leave( KErrCorrupt ); |
|
115 } |
|
116 return c; |
|
117 } |
|
118 |
|
119 // --------------------------------------------------------- |
|
120 // RUnicodeFile::Write |
|
121 // --------------------------------------------------------- |
|
122 // |
|
123 TInt RUnicodeFile::Write( const TDesC16& aDes ) |
|
124 { |
|
125 return iFile.Write ( TPtrC8( (const TUint8*)aDes.Ptr(), aDes.Size() ) ); |
|
126 } |