Back to index

lightning-sunbird  0.9+nobinonly
nsCollationUnix.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include  <locale.h>
00040 #include "prmem.h"
00041 #include "nsCollationUnix.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsIComponentManager.h"
00044 #include "nsLocaleCID.h"
00045 #include "nsILocaleService.h"
00046 #include "nsIPlatformCharset.h"
00047 #include "nsIPosixLocale.h"
00048 #include "nsCOMPtr.h"
00049 #include "nsIPrefBranch.h"
00050 #include "nsIPrefService.h"
00051 #include "nsIPrefLocalizedString.h"
00052 #include "nsUnicharUtils.h"
00053 #include "nsCRT.h"
00054 //#define DEBUG_UNIX_COLLATION
00055 
00056 inline void nsCollationUnix::DoSetLocale()
00057 {
00058   char *locale = setlocale(LC_COLLATE, NULL);
00059   mSavedLocale.Assign(locale ? locale : "");
00060   if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) {
00061     (void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mLocale,0,MAX_LOCALE_LEN)).get());
00062   }
00063 }
00064 
00065 inline void nsCollationUnix::DoRestoreLocale()
00066 {
00067   if (!mSavedLocale.EqualsIgnoreCase(mLocale.get())) { 
00068     (void) setlocale(LC_COLLATE, PromiseFlatCString(Substring(mSavedLocale,0,MAX_LOCALE_LEN)).get());
00069   }
00070 }
00071 
00072 nsCollationUnix::nsCollationUnix() 
00073 {
00074   mCollation = NULL;
00075   mUseCodePointOrder = PR_FALSE;
00076 }
00077 
00078 nsCollationUnix::~nsCollationUnix() 
00079 {
00080   if (mCollation != NULL)
00081     delete mCollation;
00082 }
00083 
00084 NS_IMPL_ISUPPORTS1(nsCollationUnix, nsICollation)
00085 
00086 nsresult nsCollationUnix::Initialize(nsILocale* locale) 
00087 {
00088 #define kPlatformLocaleLength 64
00089   NS_ASSERTION(mCollation == NULL, "Should only be initialized once");
00090 
00091   nsresult res;
00092 
00093   nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
00094   if (prefBranch) {
00095     nsCOMPtr<nsIPrefLocalizedString> prefLocalString;
00096     res = prefBranch->GetComplexValue("intl.collationOption",
00097                                       NS_GET_IID(nsIPrefLocalizedString),
00098                                       getter_AddRefs(prefLocalString));
00099     if (NS_SUCCEEDED(res) && prefLocalString) {
00100       nsXPIDLString prefValue;
00101       prefLocalString->GetData(getter_Copies(prefValue));
00102       mUseCodePointOrder =
00103         prefValue.LowerCaseEqualsLiteral("usecodepointorder");
00104     }
00105   }
00106 
00107   mCollation = new nsCollation;
00108   if (mCollation == NULL) {
00109     NS_ASSERTION(0, "mCollation creation failed");
00110     return NS_ERROR_OUT_OF_MEMORY;
00111   }
00112 
00113   // default platform locale
00114   mLocale.Assign('C');
00115 
00116   nsAutoString localeStr;
00117   NS_NAMED_LITERAL_STRING(aCategory, "NSILOCALE_COLLATE##PLATFORM");
00118 
00119   // get locale string, use app default if no locale specified
00120   if (locale == nsnull) {
00121     nsCOMPtr<nsILocaleService> localeService = 
00122              do_GetService(NS_LOCALESERVICE_CONTRACTID, &res);
00123     if (NS_SUCCEEDED(res)) {
00124       nsCOMPtr<nsILocale> appLocale;
00125       res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
00126       if (NS_SUCCEEDED(res)) {
00127         res = appLocale->GetCategory(aCategory, localeStr);
00128         NS_ASSERTION(NS_SUCCEEDED(res), "failed to get app locale info");
00129       }
00130     }
00131   }
00132   else {
00133     res = locale->GetCategory(aCategory, localeStr);
00134     NS_ASSERTION(NS_SUCCEEDED(res), "failed to get locale info");
00135   }
00136 
00137   // Get platform locale and charset name from locale, if available
00138   if (NS_SUCCEEDED(res)) {
00139     // keep the same behavior as 4.x as well as avoiding Linux collation key problem
00140     if (localeStr.LowerCaseEqualsLiteral("en_us")) { // note: locale is in platform format
00141       localeStr.AssignLiteral("C");
00142     }
00143 
00144     nsCOMPtr <nsIPosixLocale> posixLocale = do_GetService(NS_POSIXLOCALE_CONTRACTID, &res);
00145     if (NS_SUCCEEDED(res)) {
00146       res = posixLocale->GetPlatformLocale(localeStr, mLocale);
00147     }
00148 
00149     nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
00150     if (NS_SUCCEEDED(res)) {
00151       nsCAutoString mappedCharset;
00152       res = platformCharset->GetDefaultCharsetForLocale(localeStr, mappedCharset);
00153       if (NS_SUCCEEDED(res)) {
00154         mCollation->SetCharset(mappedCharset.get());
00155       }
00156     }
00157   }
00158 
00159   return NS_OK;
00160 }
00161 
00162 
00163 nsresult nsCollationUnix::CompareString(PRInt32 strength,
00164                                         const nsAString& string1,
00165                                         const nsAString& string2,
00166                                         PRInt32* result) 
00167 {
00168   nsresult res = NS_OK;
00169 
00170   nsAutoString stringNormalized1, stringNormalized2;
00171   if (strength != kCollationCaseSensitive) {
00172     res = mCollation->NormalizeString(string1, stringNormalized1);
00173     if (NS_FAILED(res)) {
00174       return res;
00175     }
00176     res = mCollation->NormalizeString(string2, stringNormalized2);
00177     if (NS_FAILED(res)) {
00178       return res;
00179     }
00180   } else {
00181     stringNormalized1 = string1;
00182     stringNormalized2 = string2;
00183   }
00184 
00185   // convert unicode to charset
00186   char *str1, *str2;
00187 
00188   res = mCollation->UnicodeToChar(stringNormalized1, &str1);
00189   if (NS_SUCCEEDED(res) && str1 != NULL) {
00190     res = mCollation->UnicodeToChar(stringNormalized2, &str2);
00191     if (NS_SUCCEEDED(res) && str2 != NULL) {
00192       if (mUseCodePointOrder) {
00193         *result = strcmp(str1, str2);
00194       }
00195       else {
00196         DoSetLocale();
00197         *result = strcoll(str1, str2);
00198         DoRestoreLocale();
00199       }
00200       PR_Free(str2);
00201     }
00202     PR_Free(str1);
00203   }
00204 
00205   return res;
00206 }
00207 
00208 
00209 nsresult nsCollationUnix::AllocateRawSortKey(PRInt32 strength, 
00210                                              const nsAString& stringIn,
00211                                              PRUint8** key, PRUint32* outLen)
00212 {
00213   nsresult res = NS_OK;
00214 
00215   nsAutoString stringNormalized;
00216   if (strength != kCollationCaseSensitive) {
00217     res = mCollation->NormalizeString(stringIn, stringNormalized);
00218     if (NS_FAILED(res))
00219       return res;
00220   } else {
00221     stringNormalized = stringIn;
00222   }
00223   // convert unicode to charset
00224   char *str;
00225 
00226   res = mCollation->UnicodeToChar(stringNormalized, &str);
00227   if (NS_SUCCEEDED(res) && str != NULL) {
00228     if (mUseCodePointOrder) {
00229       *key = (PRUint8 *)str;
00230       *outLen = strlen(str) + 1;
00231     } else {
00232       DoSetLocale();
00233       // call strxfrm to generate a key 
00234       size_t len = strxfrm(nsnull, str, 0) + 1;
00235       void *buffer = PR_Malloc(len);
00236       if (!buffer) {
00237         res = NS_ERROR_OUT_OF_MEMORY;
00238       } else if (strxfrm((char *)buffer, str, len) >= len) {
00239         PR_Free(buffer);
00240         res = NS_ERROR_FAILURE;
00241       } else {
00242         *key = (PRUint8 *)buffer;
00243         *outLen = len;
00244       }
00245       DoRestoreLocale();
00246       PR_Free(str);
00247     }
00248   }
00249 
00250   return res;
00251 }
00252 
00253 nsresult nsCollationUnix::CompareRawSortKey(const PRUint8* key1, PRUint32 len1, 
00254                                             const PRUint8* key2, PRUint32 len2, 
00255                                             PRInt32* result)
00256 {
00257   *result = PL_strcmp((const char *)key1, (const char *)key2);
00258   return NS_OK;
00259 }