diff --git a/intl/locale/src/nsCollation.cpp b/intl/locale/src/nsCollation.cpp index c808d00b3c0..c4646cb46da 100644 --- a/intl/locale/src/nsCollation.cpp +++ b/intl/locale/src/nsCollation.cpp @@ -16,10 +16,14 @@ * Reserved. */ +#define NS_IMPL_IDS +#include "nsIServiceManager.h" +#include "nsICharsetConverterManager.h" #include "nsRepository.h" #include "nsCollation.h" #include "nsCollationCID.h" #include "nsUnicharUtilCIID.h" +#include "prmem.h" //////////////////////////////////////////////////////////////////////////////// @@ -147,6 +151,36 @@ nsresult nsCollation::NormalizeString(nsAutoString& stringInOut) return NS_OK; } +nsresult nsCollation::UnicodeToChar(const nsString& src, char** dst, const nsString& aCharset) +{ + nsICharsetConverterManager * ccm = nsnull; + nsresult res; + + res = nsServiceManager::GetService(kCharsetConverterManagerCID, + kICharsetConverterManagerIID, + (nsISupports**)&ccm); + if(NS_SUCCEEDED(res) && (nsnull != ccm)) { + nsIUnicodeEncoder* encoder = nsnull; + res = ccm->GetUnicodeEncoder(&aCharset, &encoder); + if(NS_SUCCEEDED(res) && (nsnull != encoder)) { + const PRUnichar *unichars = src.GetUnicode(); + PRInt32 unicharLength = src.Length(); + PRInt32 dstLength; + res = encoder->Length(unichars, 0, unicharLength, &dstLength); + *dst = (char *) PR_Malloc(dstLength + 1); + if (*dst != nsnull) { + res = encoder->Convert(unichars, 0, &unicharLength, *dst, 0, &dstLength); + (*dst)[dstLength] = '\0'; + } + else { + res = NS_ERROR_OUT_OF_MEMORY; + } + NS_IF_RELEASE(encoder); + } + nsServiceManager::ReleaseService(kCharsetConverterManagerCID, ccm); + } + return res; +} diff --git a/intl/locale/src/nsCollation.h b/intl/locale/src/nsCollation.h index 0ac1d5e14f9..f8c23dbf0a9 100644 --- a/intl/locale/src/nsCollation.h +++ b/intl/locale/src/nsCollation.h @@ -58,6 +58,10 @@ public: // normalize string before collation key generation nsresult NormalizeString(nsAutoString& stringInOut); + // charset conversion util, C string buffer is allocate by PR_Malloc, caller should call PR_Free + nsresult UnicodeToChar(const nsString& src, char** dst, const nsString& aCharset); + + protected: nsICaseConversion* mCaseConversion; };