1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
23
* Pierre Phaneuf <pp@ludusdesign.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
42
#include "nsCollationUnix.h"
43
#include "nsIServiceManager.h"
44
#include "nsIComponentManager.h"
45
#include "nsLocaleCID.h"
46
#include "nsILocaleService.h"
47
#include "nsIPlatformCharset.h"
48
#include "nsIPosixLocale.h"
50
#include "nsIPrefBranch.h"
51
#include "nsIPrefService.h"
52
#include "nsIPrefLocalizedString.h"
53
#include "nsUnicharUtils.h"
55
//#define DEBUG_UNIX_COLLATION
57
inline void nsCollationUnix::DoSetLocale()
59
char *locale = setlocale(LC_COLLATE, NULL);
60
mSavedLocale.Assign(locale ? locale : "");
61
if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) {
62
(void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mLocale,0,MAX_LOCALE_LEN)).get());
66
inline void nsCollationUnix::DoRestoreLocale()
68
if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) {
69
(void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mSavedLocale,0,MAX_LOCALE_LEN)).get());
73
nsCollationUnix::nsCollationUnix()
76
mUseCodePointOrder = PR_FALSE;
79
nsCollationUnix::~nsCollationUnix()
81
if (mCollation != NULL)
85
NS_IMPL_ISUPPORTS1(nsCollationUnix, nsICollation)
87
nsresult nsCollationUnix::Initialize(nsILocale* locale)
89
#define kPlatformLocaleLength 64
90
NS_ASSERTION(mCollation == NULL, "Should only be initialized once");
94
nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
96
nsCOMPtr<nsIPrefLocalizedString> prefLocalString;
97
res = prefBranch->GetComplexValue("intl.collationOption",
98
NS_GET_IID(nsIPrefLocalizedString),
99
getter_AddRefs(prefLocalString));
100
if (NS_SUCCEEDED(res) && prefLocalString) {
101
nsXPIDLString prefValue;
102
prefLocalString->GetData(getter_Copies(prefValue));
104
prefValue.Equals(NS_LITERAL_STRING("useCodePointOrder"),
105
nsCaseInsensitiveStringComparator());
109
mCollation = new nsCollation;
110
if (mCollation == NULL) {
111
NS_ASSERTION(0, "mCollation creation failed");
112
return NS_ERROR_OUT_OF_MEMORY;
115
// default platform locale
118
nsAutoString localeStr;
119
NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_COLLATE##PLATFORM");
121
// get locale string, use app default if no locale specified
122
if (locale == nsnull) {
123
nsCOMPtr<nsILocaleService> localeService =
124
do_GetService(NS_LOCALESERVICE_CONTRACTID, &res);
125
if (NS_SUCCEEDED(res)) {
126
nsCOMPtr<nsILocale> appLocale;
127
res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
128
if (NS_SUCCEEDED(res)) {
129
res = appLocale->GetCategory(aCategory, localeStr);
130
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get app locale info");
135
res = locale->GetCategory(aCategory, localeStr);
136
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get locale info");
139
// Get platform locale and charset name from locale, if available
140
if (NS_SUCCEEDED(res)) {
141
// keep the same behavior as 4.x as well as avoiding Linux collation key problem
142
if (localeStr.EqualsIgnoreCase("en_US")) { // note: locale is in platform format
143
localeStr.Assign(NS_LITERAL_STRING("C"));
146
nsCOMPtr <nsIPosixLocale> posixLocale = do_GetService(NS_POSIXLOCALE_CONTRACTID, &res);
147
if (NS_SUCCEEDED(res)) {
148
res = posixLocale->GetPlatformLocale(localeStr, mLocale);
151
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
152
if (NS_SUCCEEDED(res)) {
153
nsCAutoString mappedCharset;
154
res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
155
if (NS_SUCCEEDED(res)) {
156
mCollation->SetCharset(mappedCharset.get());
165
nsresult nsCollationUnix::CompareString(PRInt32 strength,
166
const nsAString& string1,
167
const nsAString& string2,
170
nsresult res = NS_OK;
172
nsAutoString stringNormalized1, stringNormalized2;
173
if (strength != kCollationCaseSensitive) {
174
res = mCollation->NormalizeString(string1, stringNormalized1);
175
if (NS_FAILED(res)) {
178
res = mCollation->NormalizeString(string2, stringNormalized2);
179
if (NS_FAILED(res)) {
183
stringNormalized1 = string1;
184
stringNormalized2 = string2;
187
// convert unicode to charset
190
res = mCollation->UnicodeToChar(stringNormalized1, &str1);
191
if (NS_SUCCEEDED(res) && str1 != NULL) {
192
res = mCollation->UnicodeToChar(stringNormalized2, &str2);
193
if (NS_SUCCEEDED(res) && str2 != NULL) {
194
if (mUseCodePointOrder) {
195
*result = strcmp(str1, str2);
199
*result = strcoll(str1, str2);
211
nsresult nsCollationUnix::AllocateRawSortKey(PRInt32 strength,
212
const nsAString& stringIn,
213
PRUint8** key, PRUint32* outLen)
215
nsresult res = NS_OK;
217
nsAutoString stringNormalized;
218
if (strength != kCollationCaseSensitive) {
219
res = mCollation->NormalizeString(stringIn, stringNormalized);
223
stringNormalized = stringIn;
225
// convert unicode to charset
228
res = mCollation->UnicodeToChar(stringNormalized, &str);
229
if (NS_SUCCEEDED(res) && str != NULL) {
230
if (mUseCodePointOrder) {
231
*key = (PRUint8 *)str;
232
*outLen = strlen(str) + 1;
235
// call strxfrm to generate a key
236
size_t len = strxfrm(nsnull, str, 0) + 1;
237
void *buffer = PR_Malloc(len);
239
res = NS_ERROR_OUT_OF_MEMORY;
240
} else if (strxfrm((char *)buffer, str, len) >= len) {
242
res = NS_ERROR_FAILURE;
244
*key = (PRUint8 *)buffer;
255
nsresult nsCollationUnix::CompareRawSortKey(const PRUint8* key1, PRUint32 len1,
256
const PRUint8* key2, PRUint32 len2,
259
*result = PL_strcmp((const char *)key1, (const char *)key2);