os/kernelhwsrv/kerneltest/f32test/locl/CodepageUtils/src/t_cputils_unicodeconv.cpp
First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 #include "t_cputils_unicodeconv.h"
21 //replacement character to be used when unicode cannot be converted
22 const TUint8 KForeignReplacement = 0x5F;
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)
27 UnicodeConv::ConvertFromUnicodeL(aForeign, aUnicode, ETrue);
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)
33 const TInt unicodeLength = aUnicode.Length();
35 //loop going through the character of the unicode descriptor
36 for(TInt i=0; i<unicodeLength; i++)
38 const TUint16 unicodeChar = aUnicode[i];
40 // if the output buffer is already full, leave with KErrOverflow
41 if ( aForeign.Length() >= aForeign.MaxLength() )
43 if (leaveWhenOverflow)
44 User::Leave(KErrOverflow);
49 //charcters from 0x0000 to 0x007F, can be mapped directly
50 if(unicodeChar<0x0080)
52 aForeign.Append(static_cast<TUint8>(unicodeChar));
56 TInt trailByte = KErrNotFound;
57 TInt returnValue = TConvDataStruct::ConvertSingleUnicode(unicodeChar,trailByte);
59 if(returnValue!=KErrNotFound)
61 if(trailByte!=KErrNotFound)
63 // as two bytes are being added check enough space for second
64 if ( aForeign.Length() + 2 <= aForeign.MaxLength() )
66 aForeign.Append(static_cast<TUint8>(returnValue));
67 aForeign.Append(static_cast<TUint8>(trailByte));
71 if (leaveWhenOverflow)
72 User::Leave(KErrOverflow);
78 aForeign.Append(static_cast<TUint8>(returnValue));
81 aForeign.Append(KForeignReplacement);
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)
91 UnicodeConv::ConvertToUnicodeL(aUnicode, aForeign, ETrue);
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)
97 const TInt foreignLength = aForeign.Length();
99 //loop going through the characters of the foreign descriptor
100 for(TInt i = 0; i<foreignLength; i++)
102 const TUint8 leadForeign = aForeign[i];
103 TUint8 tailForeign = 0x00;
105 // Check there is enough space in the output buffer, and leave with KErrOverflow if not
106 if ( aUnicode.Length() == aUnicode.MaxLength() )
108 if (leaveWhenOverflow)
109 User::Leave(KErrOverflow);
114 //charcters from 0x00 to 0x7F, can be mapped directly
115 if(leadForeign < 0x80)
116 aUnicode.Append(static_cast<TUint16>(leadForeign));
119 if((i+1)<foreignLength)
120 tailForeign = aForeign[i+1];
122 const TLeadOrSingle* structPtr = TConvDataStruct::KFirstByteConversions + (leadForeign-0x80);
124 if(structPtr->iUnicodeIfSingle)
125 aUnicode.Append(structPtr->iUnicodeIfSingle);
128 if(TConvDataStruct::KMinTrailByte<=tailForeign && tailForeign<=TConvDataStruct::KMaxTrailByte)
129 aUnicode.Append(TConvDataStruct::KDoubleByteConversions[structPtr->iDoubleByteIndex+
130 (tailForeign - TConvDataStruct::KMinTrailByte)]);
132 aUnicode.Append(0xFFFD);
141 EXPORT_C TBool UnicodeConv::IsLegalShortNameCharacter (TUint aCharacter)
143 //1. aCharacter >= 0x0080
144 if (aCharacter>=0x0080)
146 if (aCharacter>0xFFFF)
149 TInt trailByte = KErrNotFound;
150 TInt returnValue = TConvDataStruct::ConvertSingleUnicode(aCharacter,trailByte);
152 if(returnValue!=KErrNotFound)
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'))
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)
172 // Space (' ') is not considered as a legal DOS char here.
173 if (aCharacter == 0x20)
176 // 3. 0x20 < aCharacter < 0x80
177 // According to FAT Spec, "following characters are not legal in any bytes of DIR_Name":
184 //case 0x2E: // '.' // Although '.' is not allowed in any bytes of DIR_Name, it
185 // is a valid character in short file names.