From a3840bade83e7be8ace7cf70ccf514da460a68b8 Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Tue, 4 Jan 2000 20:36:30 +0000 Subject: [PATCH] Changed nsILocaleService implementation not to use static variable/function, changed clients to use the service manager. For unix, also changed to check the cuurent locale before calling setlocale. bug 22281,20380, r=ftang,tao. --- intl/locale/src/nsLocaleService.cpp | 37 +++------------- intl/locale/src/nsScriptableDateFormat.cpp | 5 +-- intl/locale/src/unix/nsCollationUnix.cpp | 43 ++++++++++++------- intl/locale/src/unix/nsDateTimeFormatUnix.cpp | 6 +-- intl/locale/src/windows/nsCollationWin.cpp | 7 +-- .../src/windows/nsDateTimeFormatWin.cpp | 6 +-- 6 files changed, 40 insertions(+), 64 deletions(-) diff --git a/intl/locale/src/nsLocaleService.cpp b/intl/locale/src/nsLocaleService.cpp index 345b60e1d96..4341bd63fa3 100644 --- a/intl/locale/src/nsLocaleService.cpp +++ b/intl/locale/src/nsLocaleService.cpp @@ -122,22 +122,17 @@ public: // NS_DECL_NSILOCALESERVICE - // - // implementation methods - // - static nsresult GetLocaleService(nsILocaleService** localeService); - -protected: nsLocaleService(void); virtual ~nsLocaleService(void); +protected: + nsresult SetSystemLocale(void); nsresult SetApplicationLocale(void); nsILocale* mSystemLocale; nsILocale* mApplicationLocale; - static nsILocaleService* gLocaleService; }; @@ -168,7 +163,6 @@ protected: }; -nsILocaleService* nsLocaleService::gLocaleService = nsnull; // @@ -246,7 +240,7 @@ nsLocaleService::nsLocaleService(void) int i; nsString category; nsLocale* resultLocale = new nsLocale(); - if (resultLocale==NULL) { posixConverter->Release(); return; } + if (resultLocale==NULL) { posixConverter->Release(); return; } for(i=0;iRelease(); if (mApplicationLocale) mApplicationLocale->Release(); } @@ -464,25 +457,6 @@ nsLocaleService::GetLocaleFromAcceptLanguage(const char *acceptLanguage, nsILoca return result; } -nsresult -nsLocaleService::GetLocaleService(nsILocaleService** localeService) -{ - if (!gLocaleService) - { - nsLocaleService* locale_service = new nsLocaleService(); - if (!locale_service) - { - *localeService = NS_STATIC_CAST(nsILocaleService*,locale_service); - return NS_ERROR_OUT_OF_MEMORY; - } - - gLocaleService = NS_STATIC_CAST(nsILocaleService*,locale_service); - - } - - *localeService = gLocaleService; - return NS_OK; -} nsresult nsLocaleService::GetLocaleComponentForUserAgent(PRUnichar **_retval) @@ -506,7 +480,10 @@ nsLocaleService::GetLocaleComponentForUserAgent(PRUnichar **_retval) nsresult NS_NewLocaleService(nsILocaleService** result) { - return nsLocaleService::GetLocaleService(result); + if(!result) + return NS_ERROR_NULL_POINTER; + *result = new nsLocaleService(); + return (nsnull == *result) ? NS_ERROR_OUT_OF_MEMORY : NS_OK; } diff --git a/intl/locale/src/nsScriptableDateFormat.cpp b/intl/locale/src/nsScriptableDateFormat.cpp index 0fbd1cd24ca..11c0c61a6a5 100644 --- a/intl/locale/src/nsScriptableDateFormat.cpp +++ b/intl/locale/src/nsScriptableDateFormat.cpp @@ -85,7 +85,6 @@ NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime( PRInt32 second, PRUnichar **dateTimeString) { - nsILocaleService *localeService; nsILocale* aLocale; nsString localeName(locale); nsresult rv; @@ -94,12 +93,10 @@ NS_IMETHODIMP nsScriptableDateFormat::FormatDateTime( // get locale service - rv = nsComponentManager::CreateInstance(kLocaleServiceCID, NULL, - nsILocaleService::GetIID(), (void**)&localeService); + NS_WITH_SERVICE(nsILocaleService, localeService, kLocaleServiceCID, &rv); if (NS_SUCCEEDED(rv)) { rv = localeName.Length() ? localeService->NewLocale(localeName.GetUnicode(), &aLocale) : localeService->GetApplicationLocale(&aLocale); - localeService->Release(); if (NS_SUCCEEDED(rv) && aLocale) { nsIDateTimeFormat *aDateTimeFormat; diff --git a/intl/locale/src/unix/nsCollationUnix.cpp b/intl/locale/src/unix/nsCollationUnix.cpp index 6a196364713..04ddc6be673 100644 --- a/intl/locale/src/unix/nsCollationUnix.cpp +++ b/intl/locale/src/unix/nsCollationUnix.cpp @@ -23,6 +23,7 @@ #include #include "prmem.h" #include "nsCollationUnix.h" +#include "nsIServiceManager.h" #include "nsIComponentManager.h" #include "nsLocaleCID.h" #include "nsILocaleService.h" @@ -76,14 +77,10 @@ nsresult nsCollationUnix::Initialize(nsILocale* locale) // get locale string, use app default if no locale specified if (locale == nsnull) { - nsILocaleService *localeService; - - res = nsComponentManager::CreateInstance(kLocaleServiceCID, NULL, - nsILocaleService::GetIID(), (void**)&localeService); + NS_WITH_SERVICE(nsILocaleService, localeService, kLocaleServiceCID, &res); if (NS_SUCCEEDED(res)) { nsILocale *appLocale; res = localeService->GetApplicationLocale(&appLocale); - localeService->Release(); if (NS_SUCCEEDED(res)) { res = appLocale->GetCategory(aCategory.GetUnicode(), &aLocaleUnichar); appLocale->Release(); @@ -135,6 +132,10 @@ nsresult nsCollationUnix::Initialize(nsILocale* locale) if (NULL != (const char *)tmp) { printf("nsCollationUnix::Initialize mLocale = %s\n", (const char *)tmp); } + nsAutoCString tmp2(mCharset); + if (NULL != (const char *)tmp2) { + printf("nsCollationUnix::Initialize mCharset = %s\n", (const char *)tmp2); + } #endif return NS_OK; @@ -158,13 +159,10 @@ nsresult nsCollationUnix::GetSortKeyLen(const nsCollationStrength strength, res = mCollation->UnicodeToChar(stringNormalized, &str, mCharset); if (NS_SUCCEEDED(res) && str != NULL) { - char *cstr = mLocale.ToNewCString(); - char *old_locale = setlocale(LC_COLLATE, ""); - (void) setlocale(LC_COLLATE, cstr); + DoSetLocale(); // call strxfrm to calculate a key length int len = strxfrm(NULL, str, 0) + 1; - (void) setlocale(LC_COLLATE, old_locale); - delete [] cstr; + DoRestoreLocale(); *outLen = (len == -1) ? 0 : (PRUint32)len; PR_Free(str); } @@ -186,13 +184,10 @@ nsresult nsCollationUnix::CreateRawSortKey(const nsCollationStrength strength, res = mCollation->UnicodeToChar(stringNormalized, &str, mCharset); if (NS_SUCCEEDED(res) && str != NULL) { - char *cstr = mLocale.ToNewCString(); - char *old_locale = setlocale(LC_COLLATE, ""); - (void) setlocale(LC_COLLATE, cstr); + DoSetLocale(); // call strxfrm to generate a key int len = strxfrm((char *) key, str, strlen(str)); - (void) setlocale(LC_COLLATE, old_locale); - delete [] cstr; + DoRestoreLocale(); *outLen = (len == -1) ? 0 : (PRUint32)len; PR_Free(str); } @@ -200,3 +195,21 @@ nsresult nsCollationUnix::CreateRawSortKey(const nsCollationStrength strength, return res; } +inline void nsCollationUnix::DoSetLocale() +{ + char *locale = setlocale(LC_COLLATE, ""); + mSavedLocale.SetString(locale ? locale : ""); + if (!mSavedLocale.EqualsIgnoreCase(mLocale)) { + char newLocale[128]; + (void) setlocale(LC_COLLATE, mLocale.ToCString(newLocale, 128)); + } +} + +inline void nsCollationUnix::DoRestoreLocale() +{ + if (!mSavedLocale.EqualsIgnoreCase(mLocale)) { + char oldLocale[128]; + (void) setlocale(LC_COLLATE, mSavedLocale.ToCString(oldLocale, 128)); + } +} + diff --git a/intl/locale/src/unix/nsDateTimeFormatUnix.cpp b/intl/locale/src/unix/nsDateTimeFormatUnix.cpp index 3726c37d1cb..ff32238dbbc 100644 --- a/intl/locale/src/unix/nsDateTimeFormatUnix.cpp +++ b/intl/locale/src/unix/nsDateTimeFormatUnix.cpp @@ -70,14 +70,10 @@ nsresult nsDateTimeFormatUnix::Initialize(nsILocale* locale) // get locale name string, use app default if no locale specified if (NULL == locale) { - nsILocaleService *localeService; - - res = nsComponentManager::CreateInstance(kLocaleServiceCID, NULL, - nsILocaleService::GetIID(), (void**)&localeService); + NS_WITH_SERVICE(nsILocaleService, localeService, kLocaleServiceCID, &res); if (NS_SUCCEEDED(res)) { nsILocale *appLocale; res = localeService->GetApplicationLocale(&appLocale); - localeService->Release(); if (NS_SUCCEEDED(res)) { res = appLocale->GetCategory(aCategory.GetUnicode(), &aLocaleUnichar); if (NS_SUCCEEDED(res) && NULL != aLocaleUnichar) { diff --git a/intl/locale/src/windows/nsCollationWin.cpp b/intl/locale/src/windows/nsCollationWin.cpp index 317de165cfe..1762703c23d 100644 --- a/intl/locale/src/windows/nsCollationWin.cpp +++ b/intl/locale/src/windows/nsCollationWin.cpp @@ -22,6 +22,7 @@ #include "nsCollationWin.h" +#include "nsIServiceManager.h" #include "nsIComponentManager.h" #include "nsLocaleCID.h" #include "nsILocaleService.h" @@ -86,14 +87,10 @@ nsresult nsCollationWin::Initialize(nsILocale* locale) // get locale string, use app default if no locale specified if (locale == nsnull) { - nsILocaleService *localeService; - - res = nsComponentManager::CreateInstance(kLocaleServiceCID, NULL, - nsILocaleService::GetIID(), (void**)&localeService); + NS_WITH_SERVICE(nsILocaleService, localeService, kLocaleServiceCID, &res); if (NS_SUCCEEDED(res)) { nsILocale *appLocale; res = localeService->GetApplicationLocale(&appLocale); - localeService->Release(); if (NS_SUCCEEDED(res)) { res = appLocale->GetCategory(aCategory.GetUnicode(), &aLocaleUnichar); appLocale->Release(); diff --git a/intl/locale/src/windows/nsDateTimeFormatWin.cpp b/intl/locale/src/windows/nsDateTimeFormatWin.cpp index 363c94d7696..df26c5dbee0 100644 --- a/intl/locale/src/windows/nsDateTimeFormatWin.cpp +++ b/intl/locale/src/windows/nsDateTimeFormatWin.cpp @@ -91,14 +91,10 @@ nsresult nsDateTimeFormatWin::Initialize(nsILocale* locale) // get locale string, use app default if no locale specified if (NULL == locale) { - nsILocaleService *localeService; - - res = nsComponentManager::CreateInstance(kLocaleServiceCID, NULL, - nsILocaleService::GetIID(), (void**)&localeService); + NS_WITH_SERVICE(nsILocaleService, localeService, kLocaleServiceCID, &res); if (NS_SUCCEEDED(res)) { nsILocale *appLocale; res = localeService->GetApplicationLocale(&appLocale); - localeService->Release(); if (NS_SUCCEEDED(res)) { res = appLocale->GetCategory(aCategory.GetUnicode(), &aLocaleUnichar); if (NS_SUCCEEDED(res) && NULL != aLocaleUnichar) {