1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsIServiceManager.h"
7 #include "nsIPlatformCharset.h"
11 /**********************************************************
13 **********************************************************/
14 nsICharsetConverterManager
* OS2Uni::gCharsetManager
= nullptr;
19 const char* mConvName
;
20 nsIUnicodeEncoder
* mEncoder
;
21 nsIUnicodeDecoder
* mDecoder
;
24 #define eCONVERTER_COUNT 17
25 ConverterInfo gConverterInfo
[eCONVERTER_COUNT
] =
27 { 0, "", nullptr, nullptr },
28 { 1252, "windows-1252", nullptr, nullptr },
29 { 1208, "UTF-8", nullptr, nullptr },
30 { 1250, "windows-1250", nullptr, nullptr },
31 { 1251, "windows-1251", nullptr, nullptr },
32 { 813, "ISO-8859-7", nullptr, nullptr },
33 { 1254, "windows-1254", nullptr, nullptr },
34 { 864, "IBM864", nullptr, nullptr },
35 { 1257, "windows-1257", nullptr, nullptr },
36 { 874, "windows-874", nullptr, nullptr },
37 { 932, "Shift_JIS", nullptr, nullptr },
38 { 943, "Shift_JIS", nullptr, nullptr },
39 { 1381, "GB2312", nullptr, nullptr },
40 { 1386, "GB2312", nullptr, nullptr },
41 { 949, "EUC-KR", nullptr, nullptr },
42 { 950, "Big5", nullptr, nullptr },
43 { 1361, "x-johab", nullptr, nullptr }
47 OS2Uni::GetUconvObject(int aCodePage
, ConverterRequest aReq
)
49 if (gCharsetManager
== nullptr) {
50 CallGetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID
, &gCharsetManager
);
54 nsISupports
* uco
= nullptr;
55 for (int i
= 0; i
< eCONVERTER_COUNT
; i
++) {
56 if (aCodePage
== gConverterInfo
[i
].mCodePage
) {
57 if (gConverterInfo
[i
].mEncoder
== nullptr) {
59 nsAutoCString charset
;
61 nsCOMPtr
<nsIPlatformCharset
>
62 plat(do_GetService(NS_PLATFORMCHARSET_CONTRACTID
, &rv
));
63 if (NS_SUCCEEDED(rv
)) {
64 plat
->GetCharset(kPlatformCharsetSel_FileName
, charset
);
66 // default to IBM850 if this should fail
69 convname
= charset
.get();
71 convname
= gConverterInfo
[i
].mConvName
;
73 rv
= gCharsetManager
->GetUnicodeEncoderRaw(convname
,
74 &gConverterInfo
[i
].mEncoder
);
75 gConverterInfo
[i
].mEncoder
->
76 SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace
,
78 gCharsetManager
->GetUnicodeDecoderRaw(convname
,
79 &gConverterInfo
[i
].mDecoder
);
80 NS_ASSERTION(NS_SUCCEEDED(rv
), "Failed to get converter");
82 if (aReq
== eConv_Encoder
) {
83 uco
= gConverterInfo
[i
].mEncoder
;
85 uco
= gConverterInfo
[i
].mDecoder
;
94 void OS2Uni::FreeUconvObjects()
96 for (int i
= 0; i
< eCONVERTER_COUNT
; i
++) {
97 NS_IF_RELEASE(gConverterInfo
[i
].mEncoder
);
98 NS_IF_RELEASE(gConverterInfo
[i
].mDecoder
);
100 NS_IF_RELEASE(gCharsetManager
);
103 /**********************************************************
105 **********************************************************/
107 WideCharToMultiByte(int aCodePage
, const PRUnichar
* aSrc
,
108 int32_t aSrcLength
, nsAutoCharBuffer
& aResult
,
109 int32_t& aResultLength
)
112 nsISupports
* sup
= OS2Uni::GetUconvObject(aCodePage
, eConv_Encoder
);
113 nsCOMPtr
<nsIUnicodeEncoder
> uco
= do_QueryInterface(sup
);
115 if (NS_FAILED(uco
->GetMaxLength(aSrc
, aSrcLength
, &aResultLength
))) {
116 return NS_ERROR_UNEXPECTED
;
118 if (!aResult
.SetLength(aResultLength
+ 1))
119 return NS_ERROR_OUT_OF_MEMORY
;
120 char* str
= aResult
.Elements();
122 rv
= uco
->Convert(aSrc
, &aSrcLength
, str
, &aResultLength
);
123 aResult
[aResultLength
] = '\0';
127 /**********************************************************
129 **********************************************************/
131 MultiByteToWideChar(int aCodePage
, const char* aSrc
,
132 int32_t aSrcLength
, nsAutoChar16Buffer
& aResult
,
133 int32_t& aResultLength
)
136 nsISupports
* sup
= OS2Uni::GetUconvObject(aCodePage
, eConv_Decoder
);
137 nsCOMPtr
<nsIUnicodeDecoder
> uco
= do_QueryInterface(sup
);
139 if (NS_FAILED(uco
->GetMaxLength(aSrc
, aSrcLength
, &aResultLength
))) {
140 return NS_ERROR_UNEXPECTED
;
142 if (!aResult
.SetLength(aResultLength
+ 1))
143 return NS_ERROR_OUT_OF_MEMORY
;
144 PRUnichar
* str
= aResult
.Elements();
146 rv
= uco
->Convert(aSrc
, &aSrcLength
, str
, &aResultLength
);
147 aResult
[aResultLength
] = '\0';