Back to index

lightning-sunbird  0.9+nobinonly
nsCollationOS2.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Mozilla OS/2 libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * John Fairhurst, <john_fairhurst@iname.com>.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK *****
00036  * This Original Code has been modified by IBM Corporation.
00037  * Modifications made by IBM described herein are
00038  * Copyright (c) International Business Machines
00039  * Corporation, 2000
00040  *
00041  * Modifications to Mozilla code or documentation
00042  * identified per MPL Section 3.3
00043  *
00044  * Date             Modified by     Description of modification
00045  * 07/05/2000       IBM Corp.      Reworked file.
00046  */
00047 
00048 #include "unidef.h"
00049 #include "prmem.h"
00050 #include "nsCollationOS2.h"
00051 #include "nsIServiceManager.h"
00052 #include "nsIComponentManager.h"
00053 #include "nsLocaleCID.h"
00054 #include "nsILocaleService.h"
00055 #include "nsIPlatformCharset.h"
00056 #include "nsIOS2Locale.h"
00057 #include "nsCOMPtr.h"
00058 #include "nsString.h"
00059 #include "nsCRT.h"
00060 
00061 NS_IMPL_ISUPPORTS1(nsCollationOS2, nsICollation)
00062 
00063 nsCollationOS2::nsCollationOS2()
00064 {
00065   mCollation = NULL;
00066 }
00067 
00068 nsCollationOS2::~nsCollationOS2()
00069 {
00070    if (mCollation != NULL)
00071      delete mCollation;
00072 }
00073 
00074 /* Workaround for GCC problem */
00075 #ifndef LOCI_iCodepage
00076 #define LOCI_iCodepage ((LocaleItem)111)
00077 #endif
00078 
00079 nsresult nsCollationOS2::Initialize(nsILocale *locale)
00080 {
00081   NS_ASSERTION(mCollation == NULL, "Should only be initialized once");
00082 
00083   nsresult res;
00084 
00085   mCollation = new nsCollation;
00086   if (mCollation == NULL) {
00087     NS_ASSERTION(0, "mCollation creation failed");
00088     return NS_ERROR_OUT_OF_MEMORY;
00089   }
00090 
00091   return NS_OK;
00092 }
00093 
00094 
00095 nsresult nsCollationOS2::CompareString(PRInt32 strength, 
00096                                        const nsAString& string1, const nsAString& string2, PRInt32* result)
00097 {
00098   nsAutoString stringNormalized1, stringNormalized2;
00099   if (strength != kCollationCaseSensitive) {
00100     nsresult res;
00101     res = mCollation->NormalizeString(string1, stringNormalized1);
00102     if (NS_FAILED(res)) {
00103       return res;
00104     }
00105     res = mCollation->NormalizeString(string2, stringNormalized2);
00106     if (NS_FAILED(res)) {
00107       return res;
00108     }
00109   } else {
00110     stringNormalized1 = string1;
00111     stringNormalized2 = string2;
00112   }
00113 
00114   LocaleObject locObj = NULL;
00115   int ret = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &locObj);
00116   if (ret != ULS_SUCCESS)
00117     UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"C", &locObj);
00118 
00119   *result = UniStrcoll(locObj, (UniChar *)stringNormalized1.get(), (UniChar *)stringNormalized2.get());
00120 
00121   UniFreeLocaleObject(locObj);
00122 
00123   return NS_OK;
00124 }
00125  
00126 
00127 nsresult nsCollationOS2::AllocateRawSortKey(PRInt32 strength, 
00128                                             const nsAString& stringIn, PRUint8** key, PRUint32* outLen)
00129 {
00130   nsresult res = NS_OK;
00131 
00132   nsAutoString stringNormalized;
00133   if (strength != kCollationCaseSensitive) {
00134     res = mCollation->NormalizeString(stringIn, stringNormalized);
00135     if (NS_FAILED(res))
00136       return res;
00137   } else {
00138     stringNormalized = stringIn;
00139   }
00140 
00141   LocaleObject locObj = NULL;
00142   int ret = UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &locObj);
00143   if (ret != ULS_SUCCESS)
00144     UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"C", &locObj);
00145 
00146   res = NS_ERROR_FAILURE;               // From here on out assume failure...
00147   int length = UniStrxfrm(locObj, NULL, NS_REINTERPRET_CAST(const UniChar *,stringNormalized.get()),0);
00148   if (length >= 0) {
00149     length += 5;                        // Allow for the "extra" chars UniStrxfrm()
00150                                         //  will out put (overrunning the buffer if
00151                                         // you let it...)
00152     // Magic, persistant buffer.  If it's not twice the size we need,
00153     // we grow/reallocate it 4X so it doesn't grow often.
00154     static UniChar* pLocalBuffer = NULL;
00155     static int iBufferLength = 100;
00156     if (iBufferLength < length*2) {
00157       if ( pLocalBuffer ) {
00158         free(pLocalBuffer);
00159         pLocalBuffer = nsnull;
00160       }
00161       iBufferLength = length*4;
00162     }
00163     if (!pLocalBuffer)
00164       pLocalBuffer = (UniChar*) malloc(sizeof(UniChar) * iBufferLength);
00165     if (pLocalBuffer) {
00166       // Do the Xfrm
00167       int uLen = UniStrxfrm(locObj, pLocalBuffer, NS_REINTERPRET_CAST(const UniChar *,stringNormalized.get()), iBufferLength);
00168       // See how big the result really is
00169       uLen = UniStrlen(pLocalBuffer);
00170       // make sure it will fit in the output buffer...
00171       if (uLen < iBufferLength) {
00172           // Success!
00173           // Give 'em the real size in bytes...
00174           *key = (PRUint8 *)nsCRT::strdup((PRUnichar*) pLocalBuffer);
00175           *outLen = uLen * 2 + 2;
00176           res = NS_OK;
00177       }
00178     }
00179   }
00180   UniFreeLocaleObject(locObj);
00181 
00182   return res;
00183 }
00184 
00185 nsresult nsCollationOS2::CompareRawSortKey(const PRUint8* key1, PRUint32 len1, 
00186                                            const PRUint8* key2, PRUint32 len2, 
00187                                            PRInt32* result)
00188 {
00189   *result = PL_strcmp((const char *)key1, (const char *)key2);
00190   return NS_OK;
00191 }