2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-09-06 11:41:00 -07:00
|
|
|
|
|
|
|
#include "nsOS2Uni.h"
|
2007-09-06 11:41:01 -07:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIPlatformCharset.h"
|
|
|
|
#include <stdlib.h>
|
2007-09-06 11:41:00 -07:00
|
|
|
|
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
/**********************************************************
|
|
|
|
OS2Uni
|
|
|
|
**********************************************************/
|
2012-07-30 07:20:58 -07:00
|
|
|
nsICharsetConverterManager* OS2Uni::gCharsetManager = nullptr;
|
2007-09-06 11:41:00 -07:00
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
struct ConverterInfo
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t mCodePage;
|
2009-11-03 13:29:31 -08:00
|
|
|
const char* mConvName;
|
2007-09-06 11:41:01 -07:00
|
|
|
nsIUnicodeEncoder* mEncoder;
|
|
|
|
nsIUnicodeDecoder* mDecoder;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define eCONVERTER_COUNT 17
|
|
|
|
ConverterInfo gConverterInfo[eCONVERTER_COUNT] =
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
{ 0, "", nullptr, nullptr },
|
|
|
|
{ 1252, "windows-1252", nullptr, nullptr },
|
|
|
|
{ 1208, "UTF-8", nullptr, nullptr },
|
|
|
|
{ 1250, "windows-1250", nullptr, nullptr },
|
|
|
|
{ 1251, "windows-1251", nullptr, nullptr },
|
|
|
|
{ 813, "ISO-8859-7", nullptr, nullptr },
|
|
|
|
{ 1254, "windows-1254", nullptr, nullptr },
|
|
|
|
{ 864, "IBM864", nullptr, nullptr },
|
|
|
|
{ 1257, "windows-1257", nullptr, nullptr },
|
|
|
|
{ 874, "windows-874", nullptr, nullptr },
|
|
|
|
{ 932, "Shift_JIS", nullptr, nullptr },
|
|
|
|
{ 943, "Shift_JIS", nullptr, nullptr },
|
|
|
|
{ 1381, "GB2312", nullptr, nullptr },
|
|
|
|
{ 1386, "GB2312", nullptr, nullptr },
|
|
|
|
{ 949, "x-windows-949", nullptr, nullptr },
|
|
|
|
{ 950, "Big5", nullptr, nullptr },
|
|
|
|
{ 1361, "x-johab", nullptr, nullptr }
|
2007-09-06 11:41:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
nsISupports*
|
|
|
|
OS2Uni::GetUconvObject(int aCodePage, ConverterRequest aReq)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
if (gCharsetManager == nullptr) {
|
2007-09-06 11:41:04 -07:00
|
|
|
CallGetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &gCharsetManager);
|
2007-09-06 11:41:01 -07:00
|
|
|
}
|
2007-09-06 11:41:00 -07:00
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
nsresult rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsISupports* uco = nullptr;
|
2007-09-06 11:41:01 -07:00
|
|
|
for (int i = 0; i < eCONVERTER_COUNT; i++) {
|
|
|
|
if (aCodePage == gConverterInfo[i].mCodePage) {
|
2012-07-30 07:20:58 -07:00
|
|
|
if (gConverterInfo[i].mEncoder == nullptr) {
|
2007-09-06 11:41:01 -07:00
|
|
|
const char* convname;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString charset;
|
2007-09-06 11:41:01 -07:00
|
|
|
if (aCodePage == 0) {
|
|
|
|
nsCOMPtr<nsIPlatformCharset>
|
|
|
|
plat(do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
plat->GetCharset(kPlatformCharsetSel_FileName, charset);
|
|
|
|
} else {
|
|
|
|
// default to IBM850 if this should fail
|
|
|
|
charset = "IBM850";
|
|
|
|
}
|
|
|
|
convname = charset.get();
|
|
|
|
} else {
|
|
|
|
convname = gConverterInfo[i].mConvName;
|
|
|
|
}
|
|
|
|
rv = gCharsetManager->GetUnicodeEncoderRaw(convname,
|
|
|
|
&gConverterInfo[i].mEncoder);
|
|
|
|
gConverterInfo[i].mEncoder->
|
|
|
|
SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, '?');
|
2007-09-06 11:41:01 -07:00
|
|
|
gCharsetManager->GetUnicodeDecoderRaw(convname,
|
|
|
|
&gConverterInfo[i].mDecoder);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get converter");
|
|
|
|
}
|
|
|
|
if (aReq == eConv_Encoder) {
|
|
|
|
uco = gConverterInfo[i].mEncoder;
|
|
|
|
} else {
|
|
|
|
uco = gConverterInfo[i].mDecoder;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
return uco;
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
void OS2Uni::FreeUconvObjects()
|
2007-09-06 11:41:00 -07:00
|
|
|
{
|
2007-09-06 11:41:01 -07:00
|
|
|
for (int i = 0; i < eCONVERTER_COUNT; i++) {
|
|
|
|
NS_IF_RELEASE(gConverterInfo[i].mEncoder);
|
|
|
|
NS_IF_RELEASE(gConverterInfo[i].mDecoder);
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|
2007-09-06 11:41:01 -07:00
|
|
|
NS_IF_RELEASE(gCharsetManager);
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
/**********************************************************
|
|
|
|
WideCharToMultiByte
|
|
|
|
**********************************************************/
|
2007-09-09 02:07:20 -07:00
|
|
|
nsresult
|
2007-09-06 11:41:01 -07:00
|
|
|
WideCharToMultiByte(int aCodePage, const PRUnichar* aSrc,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aSrcLength, nsAutoCharBuffer& aResult,
|
|
|
|
int32_t& aResultLength)
|
2007-09-06 11:41:00 -07:00
|
|
|
{
|
2007-09-06 11:41:01 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsISupports* sup = OS2Uni::GetUconvObject(aCodePage, eConv_Encoder);
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> uco = do_QueryInterface(sup);
|
|
|
|
|
|
|
|
if (NS_FAILED(uco->GetMaxLength(aSrc, aSrcLength, &aResultLength))) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|
2007-09-18 16:12:06 -07:00
|
|
|
if (!aResult.SetLength(aResultLength + 1))
|
2007-09-06 11:41:01 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-09-18 16:12:06 -07:00
|
|
|
char* str = aResult.Elements();
|
2007-09-06 11:41:00 -07:00
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
rv = uco->Convert(aSrc, &aSrcLength, str, &aResultLength);
|
2007-09-18 16:12:06 -07:00
|
|
|
aResult[aResultLength] = '\0';
|
2007-09-06 11:41:01 -07:00
|
|
|
return rv;
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-06 11:41:01 -07:00
|
|
|
/**********************************************************
|
|
|
|
MultiByteToWideChar
|
|
|
|
**********************************************************/
|
2007-09-09 02:07:20 -07:00
|
|
|
nsresult
|
2007-09-06 11:41:01 -07:00
|
|
|
MultiByteToWideChar(int aCodePage, const char* aSrc,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aSrcLength, nsAutoChar16Buffer& aResult,
|
|
|
|
int32_t& aResultLength)
|
2007-09-06 11:41:00 -07:00
|
|
|
{
|
2007-09-06 11:41:01 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsISupports* sup = OS2Uni::GetUconvObject(aCodePage, eConv_Decoder);
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> uco = do_QueryInterface(sup);
|
|
|
|
|
|
|
|
if (NS_FAILED(uco->GetMaxLength(aSrc, aSrcLength, &aResultLength))) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|
2007-09-18 16:12:06 -07:00
|
|
|
if (!aResult.SetLength(aResultLength + 1))
|
2007-09-06 11:41:01 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-09-18 16:12:06 -07:00
|
|
|
PRUnichar* str = aResult.Elements();
|
2007-09-06 11:41:01 -07:00
|
|
|
|
|
|
|
rv = uco->Convert(aSrc, &aSrcLength, str, &aResultLength);
|
2007-09-18 16:12:06 -07:00
|
|
|
aResult[aResultLength] = '\0';
|
2007-09-06 11:41:01 -07:00
|
|
|
return rv;
|
2007-09-06 11:41:00 -07:00
|
|
|
}
|