locales/loce32/src/unicodeconv.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2005 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 "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 
       
    20 #include <e32svr.h>
       
    21 #include <e32std.h>
       
    22 #include <e32def.h>
       
    23 #include <e32des8.h> 
       
    24 
       
    25 #include "unicodeconv.h"
       
    26 
       
    27 //replacement character to be used when unicode cannot be converted
       
    28 const TUint8 KForeignReplacement = 0x5F;
       
    29 
       
    30 //This function converts from Unicoded characters, to foreign characters and adds them into a descriptor
       
    31 void UnicodeConv::ConvertFromUnicodeL(TDes8& aForeign, const TDesC16& aUnicode)
       
    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 (aForeign.Length() >= aForeign.MaxLength())
       
    41 			{
       
    42 			User::Leave(KErrOverflow);
       
    43 			}
       
    44 		
       
    45 		//charcters from 0x0000 to 0x007F, can be mapped directly
       
    46 		if(unicodeChar<0x0080)
       
    47 			{
       
    48 			aForeign.Append(static_cast<TUint8>(unicodeChar));
       
    49 			}
       
    50 		else
       
    51 			{
       
    52 			TInt trailByte = KErrNotFound;
       
    53 			TInt returnValue = TConvDataStruct::ConvertSingleUnicode(unicodeChar,trailByte);
       
    54 			
       
    55 			if(returnValue!=KErrNotFound)
       
    56 				{
       
    57 				if(trailByte!=KErrNotFound)		
       
    58 					{
       
    59 					if (aForeign.Length() + 2 <= aForeign.MaxLength())
       
    60 						{											
       
    61 						aForeign.Append(static_cast<TUint8>(returnValue));
       
    62 						aForeign.Append(static_cast<TUint8>(trailByte));						
       
    63 						}
       
    64 					else
       
    65 						{
       
    66 						User::Leave(KErrOverflow);
       
    67 						}	
       
    68 					}
       
    69 				else
       
    70 					aForeign.Append(static_cast<TUint8>(returnValue));
       
    71 				}		
       
    72 			else
       
    73 				aForeign.Append(KForeignReplacement);
       
    74 			} 
       
    75 		}		
       
    76 	}
       
    77 				
       
    78 				
       
    79 
       
    80 //This function converts from foreign characters into unicode and adds them into a descriptor
       
    81 void UnicodeConv::ConvertToUnicodeL(TDes16& aUnicode, const TDesC8& aForeign)
       
    82 	{
       
    83 	const TInt foreignLength = aForeign.Length();
       
    84 
       
    85 	//loop going through the characters of the foreign descriptor
       
    86 	for(TInt i = 0; i<foreignLength; i++)
       
    87 		{
       
    88 		const TUint8 leadForeign = aForeign[i];
       
    89 		
       
    90 		if (aUnicode.Length() >= aUnicode.MaxLength())
       
    91 			{
       
    92 			User::Leave(KErrOverflow);
       
    93 			}
       
    94 		
       
    95 		TUint8 tailForeign = 0x00;
       
    96 		//charcters from 0x00 to 0x7F, can be mapped directly
       
    97 		if(leadForeign < 0x80)
       
    98 			aUnicode.Append(static_cast<TUint16>(leadForeign));
       
    99 		else
       
   100 			{
       
   101 			if((i+1)<foreignLength)
       
   102 				tailForeign = aForeign[i+1];
       
   103 
       
   104 			const TLeadOrSingle* structPtr = TConvDataStruct::KFirstByteConversions + (leadForeign-0x80);
       
   105 			
       
   106 			if(structPtr->iUnicodeIfSingle)
       
   107 				aUnicode.Append(structPtr->iUnicodeIfSingle);
       
   108 			else
       
   109 				{
       
   110 				if(TConvDataStruct::KMinTrailByte<=tailForeign && tailForeign<=TConvDataStruct::KMaxTrailByte)
       
   111 					aUnicode.Append(TConvDataStruct::KDoubleByteConversions[structPtr->iDoubleByteIndex+
       
   112 						(tailForeign - TConvDataStruct::KMinTrailByte)]);
       
   113 				else
       
   114 					aUnicode.Append(0xFFFD);
       
   115 				i++;
       
   116 				}
       
   117 			}
       
   118 		}  
       
   119 
       
   120 	}
       
   121 
       
   122 TBool UnicodeConv::IsLegalShortNameCharacter (TUint aCharacter)
       
   123 	{
       
   124 	if (aCharacter>=0x0080)
       
   125 		{
       
   126 		if (aCharacter>0xFFFF)
       
   127 			return EFalse;
       
   128 		
       
   129 		TInt trailByte = KErrNotFound;
       
   130 		TInt returnValue = TConvDataStruct::ConvertSingleUnicode(aCharacter,trailByte);
       
   131 		
       
   132 		if(returnValue!=KErrNotFound)
       
   133 			return ETrue;
       
   134 		}
       
   135 	if(aCharacter>= 'a' && aCharacter<= 'z')
       
   136 		return(ETrue);
       
   137 	if(aCharacter>= 'A' && aCharacter<= 'Z')
       
   138 		return(ETrue);
       
   139 	if(aCharacter>= '0' && aCharacter<= '9')
       
   140 		return(ETrue);
       
   141 
       
   142 	switch (aCharacter)
       
   143 		{
       
   144 		case '\'':
       
   145 		case '`':
       
   146 		case '-':
       
   147 		case '_':
       
   148 		case '^':
       
   149 		case '$':
       
   150 		case '~':
       
   151 		case '!':
       
   152 		case '#':
       
   153 		case '%':
       
   154 		case '&':
       
   155 		case '{':
       
   156 		case '}':
       
   157 		case '@':
       
   158 		case '(':
       
   159 		case ')':
       
   160 			return ETrue;
       
   161 		default:
       
   162 			return EFalse;
       
   163 		}
       
   164 	}		
       
   165