1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
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>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either of the GNU General Public License Version 2 or later (the "GPL"),
27
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
41
#include "nsCollationUnix.h"
42
#include "nsIServiceManager.h"
43
#include "nsIComponentManager.h"
44
#include "nsLocaleCID.h"
45
#include "nsILocaleService.h"
46
#include "nsIPlatformCharset.h"
47
#include "nsIPosixLocale.h"
49
#include "nsIPrefBranch.h"
50
#include "nsIPrefService.h"
51
#include "nsIPrefLocalizedString.h"
52
#include "nsUnicharUtils.h"
54
//#define DEBUG_UNIX_COLLATION
56
inline void nsCollationUnix::DoSetLocale()
58
char *locale = setlocale(LC_COLLATE, NULL);
59
mSavedLocale.Assign(locale ? locale : "");
60
if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) {
61
(void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mLocale,0,MAX_LOCALE_LEN)).get());
65
inline void nsCollationUnix::DoRestoreLocale()
67
if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) {
68
(void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mSavedLocale,0,MAX_LOCALE_LEN)).get());
72
nsCollationUnix::nsCollationUnix()
77
nsCollationUnix::~nsCollationUnix()
79
if (mCollation != NULL)
83
NS_IMPL_ISUPPORTS1(nsCollationUnix, nsICollation)
85
nsresult nsCollationUnix::Initialize(nsILocale* locale)
87
#define kPlatformLocaleLength 64
88
NS_ASSERTION(mCollation == NULL, "Should only be initialized once");
92
mCollation = new nsCollation;
93
if (mCollation == NULL) {
94
NS_ASSERTION(0, "mCollation creation failed");
95
return NS_ERROR_OUT_OF_MEMORY;
98
// default platform locale
101
nsAutoString localeStr;
102
NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_COLLATE##PLATFORM");
104
// get locale string, use app default if no locale specified
105
if (locale == nsnull) {
106
nsCOMPtr<nsILocaleService> localeService =
107
do_GetService(NS_LOCALESERVICE_CONTRACTID, &res);
108
if (NS_SUCCEEDED(res)) {
109
nsCOMPtr<nsILocale> appLocale;
110
res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
111
if (NS_SUCCEEDED(res)) {
112
res = appLocale->GetCategory(aCategory, localeStr);
113
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get app locale info");
118
res = locale->GetCategory(aCategory, localeStr);
119
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get locale info");
122
// Get platform locale and charset name from locale, if available
123
if (NS_SUCCEEDED(res)) {
124
// keep the same behavior as 4.x as well as avoiding Linux collation key problem
125
if (localeStr.LowerCaseEqualsLiteral("en_us")) { // note: locale is in platform format
126
localeStr.AssignLiteral("C");
129
nsCOMPtr <nsIPosixLocale> posixLocale = do_GetService(NS_POSIXLOCALE_CONTRACTID, &res);
130
if (NS_SUCCEEDED(res)) {
131
res = posixLocale->GetPlatformLocale(localeStr, mLocale);
134
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
135
if (NS_SUCCEEDED(res)) {
136
nsCAutoString mappedCharset;
137
res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
138
if (NS_SUCCEEDED(res)) {
139
mCollation->SetCharset(mappedCharset.get());
148
nsresult nsCollationUnix::CompareString(PRInt32 strength,
149
const nsAString& string1,
150
const nsAString& string2,
153
nsresult res = NS_OK;
155
nsAutoString stringNormalized1, stringNormalized2;
156
if (strength != kCollationCaseSensitive) {
157
res = mCollation->NormalizeString(string1, stringNormalized1);
158
if (NS_FAILED(res)) {
161
res = mCollation->NormalizeString(string2, stringNormalized2);
162
if (NS_FAILED(res)) {
166
stringNormalized1 = string1;
167
stringNormalized2 = string2;
170
// convert unicode to charset
173
res = mCollation->UnicodeToChar(stringNormalized1, &str1);
174
if (NS_SUCCEEDED(res) && str1 != NULL) {
175
res = mCollation->UnicodeToChar(stringNormalized2, &str2);
176
if (NS_SUCCEEDED(res) && str2 != NULL) {
178
*result = strcoll(str1, str2);
189
nsresult nsCollationUnix::AllocateRawSortKey(PRInt32 strength,
190
const nsAString& stringIn,
191
PRUint8** key, PRUint32* outLen)
193
nsresult res = NS_OK;
195
nsAutoString stringNormalized;
196
if (strength != kCollationCaseSensitive) {
197
res = mCollation->NormalizeString(stringIn, stringNormalized);
201
stringNormalized = stringIn;
203
// convert unicode to charset
206
res = mCollation->UnicodeToChar(stringNormalized, &str);
207
if (NS_SUCCEEDED(res) && str != NULL) {
209
// call strxfrm to generate a key
210
size_t len = strxfrm(nsnull, str, 0) + 1;
211
void *buffer = PR_Malloc(len);
213
res = NS_ERROR_OUT_OF_MEMORY;
214
} else if (strxfrm((char *)buffer, str, len) >= len) {
216
res = NS_ERROR_FAILURE;
218
*key = (PRUint8 *)buffer;
228
nsresult nsCollationUnix::CompareRawSortKey(const PRUint8* key1, PRUint32 len1,
229
const PRUint8* key2, PRUint32 len2,
232
*result = PL_strcmp((const char *)key1, (const char *)key2);