os/ossrv/genericopenlibs/openenvcore/libc/src/strcoll.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/strcoll.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,254 @@
     1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Name         : strcoll.cpp
    1.18 +// Part of      : MRT
    1.19 +// Implementation for wcpcpy API
    1.20 +// Version      : 1.0
    1.21 +//
    1.22 +
    1.23 +#include <stdlib.h>
    1.24 +#include <string.h>
    1.25 +#include <e32std.h>
    1.26 +#include <charconv.h>
    1.27 +#include <e32des16.h>
    1.28 +#include <f32file.h> 
    1.29 +#include <wchar.h>
    1.30 +#include <langinfo.h>
    1.31 +#include <e32debug.h>
    1.32 +#ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
    1.33 +#include <collate.h>
    1.34 +#include "localeinfo.h"
    1.35 +#endif
    1.36 +
    1.37 +#ifdef SYMBIAN_DISTINCT_LOCALE_MODEL
    1.38 +#include <utf.h> 
    1.39 +#endif
    1.40 +
    1.41 +#if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
    1.42 +#include "libc_wsd_defs.h"
    1.43 +#endif
    1.44 +
    1.45 +
    1.46 +#ifdef __SYMBIAN32__
    1.47 +#define MAX_COL_LEVEL 1
    1.48 +#endif
    1.49 +
    1.50 +#ifdef __cplusplus
    1.51 +extern "C" {
    1.52 +#endif
    1.53 +
    1.54 +#ifdef __SYMBIAN32__
    1.55 +	extern "C" char* LC_COLLATE_LocaleName[30];
    1.56 +#ifdef EMULATOR
    1.57 +
    1.58 +char *GET_WSD_VAR_NAME(LC_COLLATE_LocaleName, g)();
    1.59 +#define LC_COLLATE_LocaleName (GET_WSD_VAR_NAME(LC_COLLATE_LocaleName, g)())
    1.60 +#endif //EMULATOR
    1.61 +
    1.62 +#endif //__SYMBIAN32__
    1.63 +
    1.64 +#ifdef __cplusplus
    1.65 +}
    1.66 +#endif
    1.67 +
    1.68 +static int DoConvertionToUnicode(const char* s1, TUint16* aUnicodeText);
    1.69 +
    1.70 +EXPORT_C
    1.71 +int strcoll(const char *s1, const char *s2)
    1.72 +{
    1.73 +#ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
    1.74 +    TInt retVal;
    1.75 +	char *ret=NULL;
    1.76 +#endif
    1.77 +	if((strcmp("C",(const char*) LC_COLLATE_LocaleName)==0) ||(strcmp("POSIX", (const char*) LC_COLLATE_LocaleName)==0)) 
    1.78 +		return strcmp (s1,s2);
    1.79 +	else	  	
    1.80 +	{
    1.81 +		TUint16* text1 = new TText16[strlen(s1) + 1];
    1.82 +		TUint16* text2 = new TText16[strlen(s2 ) + 1];
    1.83 +		
    1.84 +		if((DoConvertionToUnicode(s1, text1) == -1) || (DoConvertionToUnicode(s2, text2) == -1))
    1.85 +			{
    1.86 +		  	delete [] text1;
    1.87 +		 	delete [] text2;
    1.88 +			return -1;
    1.89 +			}
    1.90 +
    1.91 +		const TPtrC leftString((TText*) text1);
    1.92 +		const TPtrC rightString((TText*) text2);
    1.93 +		
    1.94 +		TInt left = leftString.Length();
    1.95 +		TInt right = rightString.Length();
    1.96 +#ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
    1.97 +		ret=(char*)LC_COLLATE_LocaleName;
    1.98 +		if(*ret==NULL)
    1.99 +		{
   1.100 +#endif
   1.101 +#ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
   1.102 +		retVal = Mem :: CompareC(text1,left,text2,right, MAX_COL_LEVEL,NULL);	
   1.103 +#else
   1.104 +	   	TInt retVal = Mem :: CompareC(text1,left,text2,right, MAX_COL_LEVEL,NULL);
   1.105 +#endif
   1.106 +#ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
   1.107 +        }
   1.108 +        else
   1.109 +        {
   1.110 +        CLocale* loc = CLocale::GetInstance();
   1.111 +        TExtendedLocale elocale;
   1.112 +        const TText* text;
   1.113 +        
   1.114 +        #ifdef SYMBIAN_DISTINCT_LOCALE_MODEL
   1.115 +        text = loc->GetCollateLocaleName();
   1.116 +        TPtrC localenew(text);
   1.117 +        retVal = elocale.LoadLocaleAspect(localenew); 
   1.118 +        if ( retVal == KErrNotFound ) // load old collate dll
   1.119 +            {
   1.120 +            text = loc->GetLocaleName();
   1.121 +            TPtrC locale(text);
   1.122 +            retVal = elocale.LoadLocaleAspect(ELocaleCollateSetting,locale);  
   1.123 +            }
   1.124 +        #else
   1.125 +        text = loc->GetLocaleName();
   1.126 +        TPtrC locale(text);
   1.127 +        retVal = elocale.LoadLocaleAspect(ELocaleCollateSetting,locale);  
   1.128 +        #endif
   1.129 +        if( retVal == KErrNone )
   1.130 +            {    
   1.131 +            TCollationMethod chmethod=elocale.GetPreferredCollationMethod(0);
   1.132 +            retVal = Mem :: CompareC(text1,left,text2,right, MAX_COL_LEVEL,&chmethod);
   1.133 +            }
   1.134 +        }
   1.135 +#endif
   1.136 +	  	delete [] text1;
   1.137 +	 	delete [] text2;
   1.138 +		return retVal;
   1.139 +	}	
   1.140 +		 
   1.141 +}
   1.142 +
   1.143 +static int DoConvertionToUnicode( const char* s1, TUint16* aUnicodeText)
   1.144 +{
   1.145 +
   1.146 +    TInt r = KErrNone;
   1.147 +    RFs fileSession;
   1.148 +    r = fileSession.Connect();
   1.149 +    if (r != KErrNone)
   1.150 +        {
   1.151 +            return r;
   1.152 +        }
   1.153 +    CleanupClosePushL(fileSession);
   1.154 +        
   1.155 +    CCnvCharacterSetConverter* conv = CCnvCharacterSetConverter::NewL() ;
   1.156 +    CleanupStack::PushL(conv);
   1.157 +        
   1.158 +    CArrayFix<CCnvCharacterSetConverter::SCharacterSet> *charSet = CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableL(fileSession);
   1.159 +    CleanupStack::PushL( charSet );
   1.160 +    
   1.161 +    TInt i = 0;
   1.162 +    TInt count = charSet->Count();
   1.163 +    TUint charSetUID = 0 ;
   1.164 +    while(i < count)
   1.165 +    {
   1.166 +        CCnvCharacterSetConverter::SCharacterSet characterSet = charSet->At(i); 
   1.167 +        i++;
   1.168 +        charSetUID = characterSet.Identifier();
   1.169 +       // const TBufC<25> charSetName = characterSet.NameIsFileName()?TParsePtrC(characterSet.Name()).Name():characterSet.Name();                             
   1.170 +        const TBufC<50> charSetName = characterSet.NameIsFileName()?TParsePtrC(characterSet.Name()).Name():characterSet.Name();
   1.171 +        const int CHAR_SET_NAME_SIZE = 100; //worst case size
   1.172 +        TBuf8<CHAR_SET_NAME_SIZE> aCharSetName;
   1.173 +       // if(wcstombs((char *)aCharSetName.Ptr(),(wchar_t*)(const_cast<TBufC<25>*>(&charSetName))->Des().PtrZ(),CHAR_SET_NAME_SIZE) == (size_t)-1)
   1.174 +        if(wcstombs((char *)aCharSetName.Ptr(),(wchar_t*)(const_cast<TBufC<50>*>(&charSetName))->Des().PtrZ(),CHAR_SET_NAME_SIZE) == (size_t)-1)
   1.175 +            {
   1.176 +                CleanupStack::PopAndDestroy(3); 
   1.177 +                return -1;
   1.178 +            }
   1.179 +        if(!aCharSetName.Compare(TPtrC8((const TText8*) LC_COLLATE_LocaleName)))
   1.180 +        {
   1.181 +            break;;
   1.182 +        }
   1.183 +        HBufC8*  stdInterName = conv->ConvertCharacterSetIdentifierToStandardNameL(charSetUID, fileSession);
   1.184 +        if(NULL != stdInterName)
   1.185 +        {
   1.186 +            if(!stdInterName->Compare(TPtrC8((const TText8*) LC_COLLATE_LocaleName)))
   1.187 +            {
   1.188 +                delete stdInterName;
   1.189 +                stdInterName = NULL;
   1.190 +                break;
   1.191 +            }
   1.192 +            
   1.193 +            delete stdInterName;
   1.194 +            stdInterName = NULL;
   1.195 +        }
   1.196 +
   1.197 +	}
   1.198 +	if(!charSetUID)	
   1.199 +		{
   1.200 +				CleanupStack::PopAndDestroy(3); 
   1.201 +				return -1;
   1.202 +		}
   1.203 +
   1.204 +	TPtrC8 remainderOfForeignText((const TText8*) s1);
   1.205 +	TInt length = 0;
   1.206 +	while(remainderOfForeignText.Length() > 0)
   1.207 +	{
   1.208 +		TBuf16<300> unicodeText;
   1.209 +		TInt retVal = KErrNone;
   1.210 +					
   1.211 +		CCnvCharacterSetConverter::TAvailability  avail = conv->PrepareToConvertToOrFromL(charSetUID, fileSession);
   1.212 +		if(CCnvCharacterSetConverter::ENotAvailable == avail)
   1.213 +			{
   1.214 +				CleanupStack::PopAndDestroy(3); 
   1.215 +				return -1;
   1.216 +			}
   1.217 +					
   1.218 +			TInt state = CCnvCharacterSetConverter::KStateDefault;
   1.219 +			TInt aNumberOfUnconvertibleCharacters = 0;
   1.220 +			TInt aIndexOfFirstByteOfFirstUnconvertibleCharacter = 0;
   1.221 +
   1.222 +			retVal = conv->ConvertToUnicode(unicodeText, remainderOfForeignText, state, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter);
   1.223 +			length += unicodeText.Length();
   1.224 +			
   1.225 +			wcsncpy((wchar_t*)aUnicodeText, (wchar_t*) unicodeText.Ptr(), unicodeText.Length() );
   1.226 +			
   1.227 +			if(retVal < 0 && (retVal != CCnvCharacterSetConverter::EErrorIllFormedInput))
   1.228 +			{
   1.229 +				CleanupStack::PopAndDestroy(3); 
   1.230 +				return -1;
   1.231 +			}
   1.232 +					
   1.233 +			if(aNumberOfUnconvertibleCharacters)
   1.234 +			{
   1.235 +				
   1.236 +				CleanupStack::PopAndDestroy(3); 
   1.237 +				return -1;
   1.238 +			}
   1.239 +			
   1.240 +			if(retVal == CCnvCharacterSetConverter::EErrorIllFormedInput)
   1.241 +			{
   1.242 +				
   1.243 +				CleanupStack::PopAndDestroy(3); 
   1.244 +				return -1;
   1.245 +			}
   1.246 +			
   1.247 +			remainderOfForeignText.Set(remainderOfForeignText.Right(retVal));
   1.248 +
   1.249 +	}
   1.250 +	aUnicodeText[length] = '\0';
   1.251 +	CleanupStack::PopAndDestroy(3); 
   1.252 +	return 0;
   1.253 +
   1.254 +}		
   1.255 +
   1.256 +
   1.257 +