Backed out changeset 3fe07c50c854 (bug 946316) for bustage. a=backout
[gecko.git] / widget / os2 / nsOS2Uni.cpp
blob8b1884797106a8e4e35cb56a4538ae6734d59f3a
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/. */
5 #include "nsOS2Uni.h"
6 #include "nsIServiceManager.h"
7 #include "nsIPlatformCharset.h"
8 #include <stdlib.h>
11 /**********************************************************
12 OS2Uni
13 **********************************************************/
14 nsICharsetConverterManager* OS2Uni::gCharsetManager = nullptr;
16 struct ConverterInfo
18 uint16_t mCodePage;
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 }
46 nsISupports*
47 OS2Uni::GetUconvObject(int aCodePage, ConverterRequest aReq)
49 if (gCharsetManager == nullptr) {
50 CallGetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &gCharsetManager);
53 nsresult rv;
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) {
58 const char* convname;
59 nsAutoCString charset;
60 if (aCodePage == 0) {
61 nsCOMPtr<nsIPlatformCharset>
62 plat(do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv));
63 if (NS_SUCCEEDED(rv)) {
64 plat->GetCharset(kPlatformCharsetSel_FileName, charset);
65 } else {
66 // default to IBM850 if this should fail
67 charset = "IBM850";
69 convname = charset.get();
70 } else {
71 convname = gConverterInfo[i].mConvName;
73 rv = gCharsetManager->GetUnicodeEncoderRaw(convname,
74 &gConverterInfo[i].mEncoder);
75 gConverterInfo[i].mEncoder->
76 SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace,
77 nullptr, '?');
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;
84 } else {
85 uco = gConverterInfo[i].mDecoder;
87 break;
91 return uco;
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 /**********************************************************
104 WideCharToMultiByte
105 **********************************************************/
106 nsresult
107 WideCharToMultiByte(int aCodePage, const PRUnichar* aSrc,
108 int32_t aSrcLength, nsAutoCharBuffer& aResult,
109 int32_t& aResultLength)
111 nsresult rv;
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';
124 return rv;
127 /**********************************************************
128 MultiByteToWideChar
129 **********************************************************/
130 nsresult
131 MultiByteToWideChar(int aCodePage, const char* aSrc,
132 int32_t aSrcLength, nsAutoChar16Buffer& aResult,
133 int32_t& aResultLength)
135 nsresult rv;
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';
148 return rv;