gecko/intl/locale/unix/nsCollationUnix.cpp
Birunthan Mohanathas e418b25428 Bug 1038537 - Part 3: Flatten intl/locale/src/ directory. r=smontagu
--HG--
rename : intl/locale/src/Makefile.in => intl/locale/Makefile.in
rename : intl/locale/src/PluralForm.jsm => intl/locale/PluralForm.jsm
rename : intl/locale/src/langGroups.properties => intl/locale/langGroups.properties
rename : intl/locale/src/language.properties => intl/locale/language.properties
rename : intl/locale/src/mac/moz.build => intl/locale/mac/moz.build
rename : intl/locale/src/mac/nsCollationMacUC.cpp => intl/locale/mac/nsCollationMacUC.cpp
rename : intl/locale/src/mac/nsCollationMacUC.h => intl/locale/mac/nsCollationMacUC.h
rename : intl/locale/src/mac/nsDateTimeFormatMac.cpp => intl/locale/mac/nsDateTimeFormatMac.cpp
rename : intl/locale/src/mac/nsDateTimeFormatMac.h => intl/locale/mac/nsDateTimeFormatMac.h
rename : intl/locale/src/mac/nsMacCharset.cpp => intl/locale/mac/nsMacCharset.cpp
rename : intl/locale/src/nsCollation.cpp => intl/locale/nsCollation.cpp
rename : intl/locale/src/nsCollation.h => intl/locale/nsCollation.h
rename : intl/locale/src/nsLanguageAtomService.cpp => intl/locale/nsLanguageAtomService.cpp
rename : intl/locale/src/nsLanguageAtomService.h => intl/locale/nsLanguageAtomService.h
rename : intl/locale/src/nsLocale.cpp => intl/locale/nsLocale.cpp
rename : intl/locale/src/nsLocale.h => intl/locale/nsLocale.h
rename : intl/locale/src/nsLocaleConstructors.h => intl/locale/nsLocaleConstructors.h
rename : intl/locale/src/nsLocaleService.cpp => intl/locale/nsLocaleService.cpp
rename : intl/locale/src/nsPlatformCharset.h => intl/locale/nsPlatformCharset.h
rename : intl/locale/src/nsScriptableDateFormat.cpp => intl/locale/nsScriptableDateFormat.cpp
rename : intl/locale/src/nsUConvPropertySearch.cpp => intl/locale/nsUConvPropertySearch.cpp
rename : intl/locale/src/nsUConvPropertySearch.h => intl/locale/nsUConvPropertySearch.h
rename : intl/locale/src/props2arrays.py => intl/locale/props2arrays.py
rename : intl/locale/src/unix/Makefile.in => intl/locale/unix/Makefile.in
rename : intl/locale/src/unix/moz.build => intl/locale/unix/moz.build
rename : intl/locale/src/unix/nsAndroidCharset.cpp => intl/locale/unix/nsAndroidCharset.cpp
rename : intl/locale/src/unix/nsCollationUnix.cpp => intl/locale/unix/nsCollationUnix.cpp
rename : intl/locale/src/unix/nsCollationUnix.h => intl/locale/unix/nsCollationUnix.h
rename : intl/locale/src/unix/nsDateTimeFormatUnix.cpp => intl/locale/unix/nsDateTimeFormatUnix.cpp
rename : intl/locale/src/unix/nsDateTimeFormatUnix.h => intl/locale/unix/nsDateTimeFormatUnix.h
rename : intl/locale/src/unix/nsPosixLocale.cpp => intl/locale/unix/nsPosixLocale.cpp
rename : intl/locale/src/unix/nsUNIXCharset.cpp => intl/locale/unix/nsUNIXCharset.cpp
rename : intl/locale/src/unix/unixcharset.properties => intl/locale/unix/unixcharset.properties
rename : intl/locale/src/windows/Makefile.in => intl/locale/windows/Makefile.in
rename : intl/locale/src/windows/moz.build => intl/locale/windows/moz.build
rename : intl/locale/src/windows/nsCollationWin.cpp => intl/locale/windows/nsCollationWin.cpp
rename : intl/locale/src/windows/nsCollationWin.h => intl/locale/windows/nsCollationWin.h
rename : intl/locale/src/windows/nsDateTimeFormatWin.cpp => intl/locale/windows/nsDateTimeFormatWin.cpp
rename : intl/locale/src/windows/nsDateTimeFormatWin.h => intl/locale/windows/nsDateTimeFormatWin.h
rename : intl/locale/src/windows/nsWin32Locale.cpp => intl/locale/windows/nsWin32Locale.cpp
rename : intl/locale/src/windows/nsWinCharset.cpp => intl/locale/windows/nsWinCharset.cpp
rename : intl/locale/src/windows/wincharset.properties => intl/locale/windows/wincharset.properties
2014-07-24 10:56:38 -07:00

190 lines
5.4 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include <locale.h>
#include "prmem.h"
#include "nsCollationUnix.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsILocaleService.h"
#include "nsIPlatformCharset.h"
#include "nsPosixLocale.h"
#include "nsCOMPtr.h"
#include "nsUnicharUtils.h"
#include "nsCRT.h"
//#define DEBUG_UNIX_COLLATION
inline void nsCollationUnix::DoSetLocale()
{
char *locale = setlocale(LC_COLLATE, nullptr);
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());
}
}
nsCollationUnix::nsCollationUnix() : mCollation(nullptr)
{
}
nsCollationUnix::~nsCollationUnix()
{
if (mCollation)
delete mCollation;
}
NS_IMPL_ISUPPORTS(nsCollationUnix, nsICollation)
nsresult nsCollationUnix::Initialize(nsILocale* locale)
{
#define kPlatformLocaleLength 64
NS_ASSERTION(!mCollation, "Should only be initialized once");
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
if (locale == nullptr) {
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
localeStr.Assign('C');
}
nsPosixLocale::GetPlatformLocale(localeStr, mLocale);
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
if (NS_SUCCEEDED(res)) {
nsAutoCString mappedCharset;
res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
if (NS_SUCCEEDED(res)) {
mCollation->SetCharset(mappedCharset.get());
}
}
}
return NS_OK;
}
nsresult nsCollationUnix::CompareString(int32_t strength,
const nsAString& string1,
const nsAString& string2,
int32_t* result)
{
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);
if (NS_SUCCEEDED(res) && str1) {
res = mCollation->UnicodeToChar(stringNormalized2, &str2);
if (NS_SUCCEEDED(res) && str2) {
DoSetLocale();
*result = strcoll(str1, str2);
DoRestoreLocale();
PR_Free(str2);
}
PR_Free(str1);
}
return res;
}
nsresult nsCollationUnix::AllocateRawSortKey(int32_t strength,
const nsAString& stringIn,
uint8_t** key, uint32_t* outLen)
{
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);
if (NS_SUCCEEDED(res) && str) {
DoSetLocale();
// call strxfrm to generate a key
size_t len = strxfrm(nullptr, str, 0) + 1;
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;
} else {
*key = (uint8_t *)buffer;
*outLen = len;
}
DoRestoreLocale();
PR_Free(str);
}
return res;
}
nsresult nsCollationUnix::CompareRawSortKey(const uint8_t* key1, uint32_t len1,
const uint8_t* key2, uint32_t len2,
int32_t* result)
{
*result = PL_strcmp((const char *)key1, (const char *)key2);
return NS_OK;
}