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.
This commit is contained in:
nhotta%netscape.com 2000-01-04 20:36:30 +00:00
Родитель 7378578fe5
Коммит a3840bade8
6 изменённых файлов: 40 добавлений и 64 удалений

Просмотреть файл

@ -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;i<LocaleListLength;i++) {
char* lc_temp = setlocale(posix_locale_category[i],"");
category = LocaleList[i];
@ -288,7 +282,6 @@ nsLocaleService::nsLocaleService(void)
nsLocaleService::~nsLocaleService(void)
{
gLocaleService = nsnull;
if (mSystemLocale) mSystemLocale->Release();
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;
}

Просмотреть файл

@ -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;

Просмотреть файл

@ -23,6 +23,7 @@
#include <locale.h>
#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));
}
}

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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();

Просмотреть файл

@ -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) {