Added UnicodeToChar to nsCollation class.

This commit is contained in:
nhotta%netscape.com 1999-02-04 23:17:37 +00:00
Родитель 6fb5e23283
Коммит 57da78b87d
2 изменённых файлов: 38 добавлений и 0 удалений

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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;
};