Back to index

lightning-sunbird  0.9+nobinonly
nsCollation.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 "nsIPlatformCharset.h"
00040 #include "nsIServiceManager.h"
00041 #include "nsIComponentManager.h"
00042 #include "nsCollation.h"
00043 #include "nsCollationCID.h"
00044 #include "nsUnicharUtilCIID.h"
00045 #include "prmem.h"
00046 #include "nsReadableUtils.h"
00047 
00049 
00050 NS_DEFINE_CID(kCollationCID, NS_COLLATION_CID);
00051 
00052 NS_IMPL_ISUPPORTS1(nsCollationFactory, nsICollationFactory)
00053 
00054 nsresult nsCollationFactory::CreateCollation(nsILocale* locale, nsICollation** instancePtr)
00055 {
00056   // Create a collation interface instance.
00057   //
00058   nsICollation *inst;
00059   nsresult res;
00060   
00061   res = CallCreateInstance(kCollationCID, &inst);
00062   if (NS_FAILED(res)) {
00063     return res;
00064   }
00065 
00066   inst->Initialize(locale);
00067   *instancePtr = inst;
00068 
00069   return res;
00070 }
00071 
00073 
00074 nsCollation::nsCollation()
00075 {
00076   MOZ_COUNT_CTOR(nsCollation);
00077   nsresult res;
00078   mCaseConversion = do_GetService(NS_UNICHARUTIL_CONTRACTID, &res);
00079   NS_ASSERTION(NS_SUCCEEDED(res), "CreateInstance failed for kCaseConversionIID");
00080 }
00081 
00082 nsCollation::~nsCollation()
00083 {
00084   MOZ_COUNT_DTOR(nsCollation);
00085 }
00086 
00087 nsresult nsCollation::NormalizeString(const nsAString& stringIn, nsAString& stringOut)
00088 {
00089   if (!mCaseConversion) {
00090     stringOut = stringIn;
00091   }
00092   else {
00093     PRInt32 aLength = stringIn.Length();
00094 
00095     if (aLength <= 64) {
00096       PRUnichar conversionBuffer[64];
00097       mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
00098       stringOut.Assign(conversionBuffer, aLength);
00099     }
00100     else {
00101       PRUnichar* conversionBuffer;
00102       conversionBuffer = new PRUnichar[aLength];
00103       if (!conversionBuffer) {
00104         return NS_ERROR_OUT_OF_MEMORY;
00105       }
00106       mCaseConversion->ToLower(PromiseFlatString(stringIn).get(), conversionBuffer, aLength);
00107       stringOut.Assign(conversionBuffer, aLength);
00108       delete [] conversionBuffer;
00109     }
00110   }
00111   return NS_OK;
00112 }
00113 
00114 nsresult nsCollation::SetCharset(const char* aCharset)
00115 {
00116   NS_ENSURE_ARG_POINTER(aCharset);
00117 
00118   nsresult rv;
00119   nsCOMPtr <nsICharsetConverterManager> charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
00120   if (NS_SUCCEEDED(rv)) {
00121     rv = charsetConverterManager->GetUnicodeEncoder(aCharset,
00122                                                     getter_AddRefs(mEncoder));
00123   }
00124   return rv;
00125 }
00126 
00127 nsresult nsCollation::UnicodeToChar(const nsAString& aSrc, char** dst)
00128 {
00129   NS_ENSURE_ARG_POINTER(dst);
00130 
00131   nsresult res = NS_OK;
00132   if (!mEncoder)
00133     res = SetCharset("ISO-8859-1");
00134 
00135   if (NS_SUCCEEDED(res)) {
00136     const nsPromiseFlatString& src = PromiseFlatString(aSrc);
00137     const PRUnichar *unichars = src.get();
00138     PRInt32 unicharLength = src.Length();
00139     PRInt32 dstLength;
00140     res = mEncoder->GetMaxLength(unichars, unicharLength, &dstLength);
00141     if (NS_SUCCEEDED(res)) {
00142       PRInt32 bufLength = dstLength + 1 + 32; // extra 32 bytes for Finish() call
00143       *dst = (char *) PR_Malloc(bufLength);
00144       if (*dst) {
00145         **dst = '\0';
00146         res = mEncoder->Convert(unichars, &unicharLength, *dst, &dstLength);
00147 
00148         if (NS_SUCCEEDED(res) || (NS_ERROR_UENC_NOMAPPING == res)) {
00149           // Finishes the conversion. The converter has the possibility to write some 
00150           // extra data and flush its final state.
00151           PRInt32 finishLength = bufLength - dstLength; // remaining unused buffer length
00152           if (finishLength > 0) {
00153             res = mEncoder->Finish((*dst + dstLength), &finishLength);
00154             if (NS_SUCCEEDED(res)) {
00155               (*dst)[dstLength + finishLength] = '\0';
00156             }
00157           }
00158         }
00159         if (NS_FAILED(res)) {
00160           PR_Free(*dst);
00161           *dst = nsnull;
00162         }
00163       }
00164       else {
00165         res = NS_ERROR_OUT_OF_MEMORY;
00166       }
00167     }
00168   }
00169 
00170   return res;
00171 }
00172 
00173 
00174