2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsCollationUnix.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsILocaleService.h"
|
|
|
|
#include "nsIPlatformCharset.h"
|
2011-06-22 00:34:27 -07:00
|
|
|
#include "nsPosixLocale.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
//#define DEBUG_UNIX_COLLATION
|
|
|
|
|
|
|
|
inline void nsCollationUnix::DoSetLocale()
|
|
|
|
{
|
2012-08-24 00:49:00 -07:00
|
|
|
char *locale = setlocale(LC_COLLATE, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
mSavedLocale.Assign(locale ? locale : "");
|
|
|
|
if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) {
|
|
|
|
(void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mLocale,0,MAX_LOCALE_LEN)).get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void nsCollationUnix::DoRestoreLocale()
|
|
|
|
{
|
|
|
|
if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) {
|
|
|
|
(void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mSavedLocale,0,MAX_LOCALE_LEN)).get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 00:49:00 -07:00
|
|
|
nsCollationUnix::nsCollationUnix() : mCollation(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCollationUnix::~nsCollationUnix()
|
|
|
|
{
|
2012-08-24 00:49:00 -07:00
|
|
|
if (mCollation)
|
2007-03-22 10:30:00 -07:00
|
|
|
delete mCollation;
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsCollationUnix, nsICollation)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult nsCollationUnix::Initialize(nsILocale* locale)
|
|
|
|
{
|
|
|
|
#define kPlatformLocaleLength 64
|
2012-08-24 00:49:00 -07:00
|
|
|
NS_ASSERTION(!mCollation, "Should only be initialized once");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
mCollation = new nsCollation;
|
|
|
|
|
|
|
|
// default platform locale
|
|
|
|
mLocale.Assign('C');
|
|
|
|
|
|
|
|
nsAutoString localeStr;
|
|
|
|
NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_COLLATE##PLATFORM");
|
|
|
|
|
|
|
|
// get locale string, use app default if no locale specified
|
2012-07-30 07:20:58 -07:00
|
|
|
if (locale == nullptr) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsILocaleService> localeService =
|
|
|
|
do_GetService(NS_LOCALESERVICE_CONTRACTID, &res);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
nsCOMPtr<nsILocale> appLocale;
|
|
|
|
res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
res = appLocale->GetCategory(aCategory, localeStr);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get app locale info");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = locale->GetCategory(aCategory, localeStr);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get locale info");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get platform locale and charset name from locale, if available
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
// keep the same behavior as 4.x as well as avoiding Linux collation key problem
|
|
|
|
if (localeStr.LowerCaseEqualsLiteral("en_us")) { // note: locale is in platform format
|
2014-05-26 11:55:08 -07:00
|
|
|
localeStr.Assign('C');
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-22 00:34:27 -07:00
|
|
|
nsPosixLocale::GetPlatformLocale(localeStr, mLocale);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString mappedCharset;
|
2007-03-22 10:30:00 -07:00
|
|
|
res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
mCollation->SetCharset(mappedCharset.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult nsCollationUnix::CompareString(int32_t strength,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString& string1,
|
|
|
|
const nsAString& string2,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t* result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
nsAutoString stringNormalized1, stringNormalized2;
|
|
|
|
if (strength != kCollationCaseSensitive) {
|
|
|
|
res = mCollation->NormalizeString(string1, stringNormalized1);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res = mCollation->NormalizeString(string2, stringNormalized2);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stringNormalized1 = string1;
|
|
|
|
stringNormalized2 = string2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert unicode to charset
|
|
|
|
char *str1, *str2;
|
|
|
|
|
|
|
|
res = mCollation->UnicodeToChar(stringNormalized1, &str1);
|
2012-08-24 00:49:00 -07:00
|
|
|
if (NS_SUCCEEDED(res) && str1) {
|
2007-03-22 10:30:00 -07:00
|
|
|
res = mCollation->UnicodeToChar(stringNormalized2, &str2);
|
2012-08-24 00:49:00 -07:00
|
|
|
if (NS_SUCCEEDED(res) && str2) {
|
2007-09-05 06:47:22 -07:00
|
|
|
DoSetLocale();
|
|
|
|
*result = strcoll(str1, str2);
|
|
|
|
DoRestoreLocale();
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_Free(str2);
|
|
|
|
}
|
|
|
|
PR_Free(str1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult nsCollationUnix::AllocateRawSortKey(int32_t strength,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString& stringIn,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t** key, uint32_t* outLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
nsAutoString stringNormalized;
|
|
|
|
if (strength != kCollationCaseSensitive) {
|
|
|
|
res = mCollation->NormalizeString(stringIn, stringNormalized);
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
stringNormalized = stringIn;
|
|
|
|
}
|
|
|
|
// convert unicode to charset
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
res = mCollation->UnicodeToChar(stringNormalized, &str);
|
2012-08-24 00:49:00 -07:00
|
|
|
if (NS_SUCCEEDED(res) && str) {
|
2007-09-05 06:47:22 -07:00
|
|
|
DoSetLocale();
|
|
|
|
// call strxfrm to generate a key
|
2012-07-30 07:20:58 -07:00
|
|
|
size_t len = strxfrm(nullptr, str, 0) + 1;
|
2007-09-05 06:47:22 -07:00
|
|
|
void *buffer = PR_Malloc(len);
|
|
|
|
if (!buffer) {
|
|
|
|
res = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else if (strxfrm((char *)buffer, str, len) >= len) {
|
|
|
|
PR_Free(buffer);
|
|
|
|
res = NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
*key = (uint8_t *)buffer;
|
2007-09-05 06:47:22 -07:00
|
|
|
*outLen = len;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-09-05 06:47:22 -07:00
|
|
|
DoRestoreLocale();
|
|
|
|
PR_Free(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult nsCollationUnix::CompareRawSortKey(const uint8_t* key1, uint32_t len1,
|
|
|
|
const uint8_t* key2, uint32_t len2,
|
|
|
|
int32_t* result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*result = PL_strcmp((const char *)key1, (const char *)key2);
|
|
|
|
return NS_OK;
|
|
|
|
}
|