|
1 // Copyright (c) 2006-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 the License "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 // |
|
15 |
|
16 #include <e32std.h> |
|
17 #include <e32def.h> |
|
18 #include <e32des8.h> |
|
19 #include "unicodeconv.h" |
|
20 |
|
21 //replacement character to be used when unicode cannot be converted |
|
22 const TUint8 KForeignReplacement = 0x5F; |
|
23 |
|
24 //This function converts from Unicoded characters, to foreign characters and adds them into a descriptor |
|
25 EXPORT_C void UnicodeConv::ConvertFromUnicodeL(TDes8& aForeign, const TDesC16& aUnicode) |
|
26 { |
|
27 UnicodeConv::ConvertFromUnicodeL(aForeign, aUnicode, ETrue); |
|
28 } |
|
29 |
|
30 //This function converts from Unicoded characters, to foreign characters and adds them into a descriptor |
|
31 EXPORT_C TInt UnicodeConv::ConvertFromUnicodeL(TDes8& aForeign, const TDesC16& aUnicode, TBool leaveWhenOverflow) |
|
32 { |
|
33 const TInt unicodeLength = aUnicode.Length(); |
|
34 |
|
35 //loop going through the character of the unicode descriptor |
|
36 for(TInt i=0; i<unicodeLength; i++) |
|
37 { |
|
38 const TUint16 unicodeChar = aUnicode[i]; |
|
39 |
|
40 // if the output buffer is already full, leave with KErrOverflow |
|
41 if ( aForeign.Length() >= aForeign.MaxLength() ) |
|
42 { |
|
43 if (leaveWhenOverflow) |
|
44 User::Leave(KErrOverflow); |
|
45 else |
|
46 return KErrOverflow; |
|
47 } |
|
48 |
|
49 //charcters from 0x0000 to 0x007F, can be mapped directly |
|
50 if(unicodeChar<0x0080) |
|
51 { |
|
52 aForeign.Append(static_cast<TUint8>(unicodeChar)); |
|
53 } |
|
54 else |
|
55 { |
|
56 TInt trailByte = KErrNotFound; |
|
57 TInt returnValue = TConvDataStruct::ConvertSingleUnicode(unicodeChar,trailByte); |
|
58 |
|
59 if(returnValue!=KErrNotFound) |
|
60 { |
|
61 if(trailByte!=KErrNotFound) |
|
62 { |
|
63 // as two bytes are being added check enough space for second |
|
64 if ( aForeign.Length() + 2 <= aForeign.MaxLength() ) |
|
65 { |
|
66 aForeign.Append(static_cast<TUint8>(returnValue)); |
|
67 aForeign.Append(static_cast<TUint8>(trailByte)); |
|
68 } |
|
69 else |
|
70 { |
|
71 if (leaveWhenOverflow) |
|
72 User::Leave(KErrOverflow); |
|
73 else |
|
74 return KErrOverflow; |
|
75 } |
|
76 } |
|
77 else |
|
78 aForeign.Append(static_cast<TUint8>(returnValue)); |
|
79 } |
|
80 else |
|
81 aForeign.Append(KForeignReplacement); |
|
82 } |
|
83 } |
|
84 |
|
85 return KErrNone; |
|
86 } |
|
87 |
|
88 //This function converts from foreign characters into unicode and adds them into a descriptor |
|
89 EXPORT_C void UnicodeConv::ConvertToUnicodeL(TDes16& aUnicode, const TDesC8& aForeign) |
|
90 { |
|
91 UnicodeConv::ConvertToUnicodeL(aUnicode, aForeign, ETrue); |
|
92 } |
|
93 |
|
94 //This function converts from foreign characters into unicode and adds them into a descriptor |
|
95 EXPORT_C TInt UnicodeConv::ConvertToUnicodeL(TDes16& aUnicode, const TDesC8& aForeign, TBool leaveWhenOverflow) |
|
96 { |
|
97 const TInt foreignLength = aForeign.Length(); |
|
98 |
|
99 //loop going through the characters of the foreign descriptor |
|
100 for(TInt i = 0; i<foreignLength; i++) |
|
101 { |
|
102 const TUint8 leadForeign = aForeign[i]; |
|
103 TUint8 tailForeign = 0x00; |
|
104 |
|
105 // Check there is enough space in the output buffer, and leave with KErrOverflow if not |
|
106 if ( aUnicode.Length() == aUnicode.MaxLength() ) |
|
107 { |
|
108 if (leaveWhenOverflow) |
|
109 User::Leave(KErrOverflow); |
|
110 else |
|
111 return KErrOverflow; |
|
112 } |
|
113 |
|
114 //charcters from 0x00 to 0x7F, can be mapped directly |
|
115 if(leadForeign < 0x80) |
|
116 aUnicode.Append(static_cast<TUint16>(leadForeign)); |
|
117 else |
|
118 { |
|
119 if((i+1)<foreignLength) |
|
120 tailForeign = aForeign[i+1]; |
|
121 |
|
122 const TLeadOrSingle* structPtr = TConvDataStruct::KFirstByteConversions + (leadForeign-0x80); |
|
123 |
|
124 if(structPtr->iUnicodeIfSingle) |
|
125 aUnicode.Append(structPtr->iUnicodeIfSingle); |
|
126 else |
|
127 { |
|
128 if(TConvDataStruct::KMinTrailByte<=tailForeign && tailForeign<=TConvDataStruct::KMaxTrailByte) |
|
129 aUnicode.Append(TConvDataStruct::KDoubleByteConversions[structPtr->iDoubleByteIndex+ |
|
130 (tailForeign - TConvDataStruct::KMinTrailByte)]); |
|
131 else |
|
132 aUnicode.Append(0xFFFD); |
|
133 i++; |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 return KErrNone; |
|
139 } |
|
140 |
|
141 EXPORT_C TBool UnicodeConv::IsLegalShortNameCharacter (TUint aCharacter) |
|
142 { |
|
143 //1. aCharacter >= 0x0080 |
|
144 if (aCharacter>=0x0080) |
|
145 { |
|
146 if (aCharacter>0xFFFF) |
|
147 return EFalse; |
|
148 |
|
149 TInt trailByte = KErrNotFound; |
|
150 TInt returnValue = TConvDataStruct::ConvertSingleUnicode(aCharacter,trailByte); |
|
151 |
|
152 if(returnValue!=KErrNotFound) |
|
153 return ETrue; |
|
154 else |
|
155 return EFalse; |
|
156 } |
|
157 |
|
158 // For most common cases: |
|
159 // Note: lower case characters are considered legal DOS char here. |
|
160 if ((aCharacter>='a' && aCharacter<='z') || |
|
161 (aCharacter>='A' && aCharacter<='Z') || |
|
162 (aCharacter>='0' && aCharacter<='9')) |
|
163 { |
|
164 return ETrue; |
|
165 } |
|
166 // Checking for illegal chars: |
|
167 // 2. aCharacter <= 0x20 |
|
168 // Note: leading 0x05 byte should be guarded by callers of this function |
|
169 // as the information of the position of the character is required. |
|
170 if (aCharacter < 0x20) |
|
171 return EFalse; |
|
172 // Space (' ') is not considered as a legal DOS char here. |
|
173 if (aCharacter == 0x20) |
|
174 return EFalse; |
|
175 |
|
176 // 3. 0x20 < aCharacter < 0x80 |
|
177 // According to FAT Spec, "following characters are not legal in any bytes of DIR_Name": |
|
178 switch (aCharacter) |
|
179 { |
|
180 case 0x22: // '"' |
|
181 case 0x2A: // '*' |
|
182 case 0x2B: // '+' |
|
183 case 0x2C: // ',' |
|
184 //case 0x2E: // '.' // Although '.' is not allowed in any bytes of DIR_Name, it |
|
185 // is a valid character in short file names. |
|
186 case 0x2F: // '/' |
|
187 case 0x3A: // ':' |
|
188 case 0x3B: // ';' |
|
189 case 0x3C: // '<' |
|
190 case 0x3D: // '=' |
|
191 case 0x3E: // '>' |
|
192 case 0x3F: // '?' |
|
193 case 0x5B: // '[' |
|
194 case 0x5C: // '\' |
|
195 case 0x5D: // ']' |
|
196 case 0x7C: // '|' |
|
197 return EFalse; |
|
198 default: |
|
199 return ETrue; |
|
200 } |
|
201 } |
|
202 |