Update contrib.
1 // Copyright (c) 2005-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 "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.
16 // Implementation for wcpcpy API
29 #ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
31 #include "localeinfo.h"
34 #ifdef SYMBIAN_DISTINCT_LOCALE_MODEL
38 #if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
39 #include "libc_wsd_defs.h"
44 #define MAX_COL_LEVEL 1
52 extern "C" char* LC_COLLATE_LocaleName[30];
55 char *GET_WSD_VAR_NAME(LC_COLLATE_LocaleName, g)();
56 #define LC_COLLATE_LocaleName (GET_WSD_VAR_NAME(LC_COLLATE_LocaleName, g)())
59 #endif //__SYMBIAN32__
65 static int DoConvertionToUnicode(const char* s1, TUint16* aUnicodeText);
68 int strcoll(const char *s1, const char *s2)
70 #ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
74 if((strcmp("C",(const char*) LC_COLLATE_LocaleName)==0) ||(strcmp("POSIX", (const char*) LC_COLLATE_LocaleName)==0))
75 return strcmp (s1,s2);
78 TUint16* text1 = new TText16[strlen(s1) + 1];
79 TUint16* text2 = new TText16[strlen(s2 ) + 1];
81 if((DoConvertionToUnicode(s1, text1) == -1) || (DoConvertionToUnicode(s2, text2) == -1))
88 const TPtrC leftString((TText*) text1);
89 const TPtrC rightString((TText*) text2);
91 TInt left = leftString.Length();
92 TInt right = rightString.Length();
93 #ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
94 ret=(char*)LC_COLLATE_LocaleName;
98 #ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
99 retVal = Mem :: CompareC(text1,left,text2,right, MAX_COL_LEVEL,NULL);
101 TInt retVal = Mem :: CompareC(text1,left,text2,right, MAX_COL_LEVEL,NULL);
103 #ifdef SYMBIAN_OE_ENHANCED_LOCALE_SUPPORT
107 CLocale* loc = CLocale::GetInstance();
108 TExtendedLocale elocale;
111 #ifdef SYMBIAN_DISTINCT_LOCALE_MODEL
112 text = loc->GetCollateLocaleName();
113 TPtrC localenew(text);
114 retVal = elocale.LoadLocaleAspect(localenew);
115 if ( retVal == KErrNotFound ) // load old collate dll
117 text = loc->GetLocaleName();
119 retVal = elocale.LoadLocaleAspect(ELocaleCollateSetting,locale);
122 text = loc->GetLocaleName();
124 retVal = elocale.LoadLocaleAspect(ELocaleCollateSetting,locale);
126 if( retVal == KErrNone )
128 TCollationMethod chmethod=elocale.GetPreferredCollationMethod(0);
129 retVal = Mem :: CompareC(text1,left,text2,right, MAX_COL_LEVEL,&chmethod);
140 static int DoConvertionToUnicode( const char* s1, TUint16* aUnicodeText)
145 r = fileSession.Connect();
150 CleanupClosePushL(fileSession);
152 CCnvCharacterSetConverter* conv = CCnvCharacterSetConverter::NewL() ;
153 CleanupStack::PushL(conv);
155 CArrayFix<CCnvCharacterSetConverter::SCharacterSet> *charSet = CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableL(fileSession);
156 CleanupStack::PushL( charSet );
159 TInt count = charSet->Count();
160 TUint charSetUID = 0 ;
163 CCnvCharacterSetConverter::SCharacterSet characterSet = charSet->At(i);
165 charSetUID = characterSet.Identifier();
166 // const TBufC<25> charSetName = characterSet.NameIsFileName()?TParsePtrC(characterSet.Name()).Name():characterSet.Name();
167 const TBufC<50> charSetName = characterSet.NameIsFileName()?TParsePtrC(characterSet.Name()).Name():characterSet.Name();
168 const int CHAR_SET_NAME_SIZE = 100; //worst case size
169 TBuf8<CHAR_SET_NAME_SIZE> aCharSetName;
170 // if(wcstombs((char *)aCharSetName.Ptr(),(wchar_t*)(const_cast<TBufC<25>*>(&charSetName))->Des().PtrZ(),CHAR_SET_NAME_SIZE) == (size_t)-1)
171 if(wcstombs((char *)aCharSetName.Ptr(),(wchar_t*)(const_cast<TBufC<50>*>(&charSetName))->Des().PtrZ(),CHAR_SET_NAME_SIZE) == (size_t)-1)
173 CleanupStack::PopAndDestroy(3);
176 if(!aCharSetName.Compare(TPtrC8((const TText8*) LC_COLLATE_LocaleName)))
180 HBufC8* stdInterName = conv->ConvertCharacterSetIdentifierToStandardNameL(charSetUID, fileSession);
181 if(NULL != stdInterName)
183 if(!stdInterName->Compare(TPtrC8((const TText8*) LC_COLLATE_LocaleName)))
197 CleanupStack::PopAndDestroy(3);
201 TPtrC8 remainderOfForeignText((const TText8*) s1);
203 while(remainderOfForeignText.Length() > 0)
205 TBuf16<300> unicodeText;
206 TInt retVal = KErrNone;
208 CCnvCharacterSetConverter::TAvailability avail = conv->PrepareToConvertToOrFromL(charSetUID, fileSession);
209 if(CCnvCharacterSetConverter::ENotAvailable == avail)
211 CleanupStack::PopAndDestroy(3);
215 TInt state = CCnvCharacterSetConverter::KStateDefault;
216 TInt aNumberOfUnconvertibleCharacters = 0;
217 TInt aIndexOfFirstByteOfFirstUnconvertibleCharacter = 0;
219 retVal = conv->ConvertToUnicode(unicodeText, remainderOfForeignText, state, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter);
220 length += unicodeText.Length();
222 wcsncpy((wchar_t*)aUnicodeText, (wchar_t*) unicodeText.Ptr(), unicodeText.Length() );
224 if(retVal < 0 && (retVal != CCnvCharacterSetConverter::EErrorIllFormedInput))
226 CleanupStack::PopAndDestroy(3);
230 if(aNumberOfUnconvertibleCharacters)
233 CleanupStack::PopAndDestroy(3);
237 if(retVal == CCnvCharacterSetConverter::EErrorIllFormedInput)
240 CleanupStack::PopAndDestroy(3);
244 remainderOfForeignText.Set(remainderOfForeignText.Right(retVal));
247 aUnicodeText[length] = '\0';
248 CleanupStack::PopAndDestroy(3);