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