1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is WinCEUConvAdapter for Windows CE
16 * The Initial Developer of the Original Code is
17 * Doug Turner <dougt@meer.net>.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
39 #include "nsNativeUConvService.h"
40 #include "nsIUnicodeDecoder.h"
41 #include "nsIUnicodeEncoder.h"
42 #include "nsICharRepresentable.h"
43 #include "nsIPlatformCharset.h"
44 #include "nsIServiceManager.h"
46 #include "nsUCSupport.h"
47 #include "nsUTF8ToUnicode.h"
48 #include "nsUnicodeToUTF8.h"
51 void DisplayLastError(const char * msg
)
53 int flags
= MB_APPLMODAL
| MB_TOPMOST
| MB_SETFOREGROUND
;
54 int error
= GetLastError();
57 case ERROR_NO_UNICODE_TRANSLATION
:
58 MessageBox(0, "ERROR_NO_UNICODE_TRANSLATION", msg
, flags
);
60 case ERROR_INVALID_PARAMETER
:
61 MessageBox(0, "ERROR_INVALID_PARAMETER", msg
, flags
);
63 case ERROR_INVALID_FLAGS
:
64 MessageBox(0, "ERROR_INVALID_FLAGS", msg
, flags
);
66 case ERROR_INSUFFICIENT_BUFFER
:
67 MessageBox(0, "ERROR_INSUFFICIENT_BUFFER", msg
, flags
);
70 MessageBox(0, "other...", msg
, flags
);
76 class WinCEUConvAdapter
: public nsIUnicodeDecoder
,
77 public nsIUnicodeEncoder
,
78 public nsICharRepresentable
83 virtual ~WinCEUConvAdapter();
85 nsresult
Init(const char* from
, const char* to
);
91 NS_IMETHOD
Convert(const char * aSrc
,
94 PRInt32
* aDestLength
);
96 NS_IMETHOD
GetMaxLength(const char * aSrc
,
98 PRInt32
* aDestLength
);
103 NS_IMETHOD
Convert(const PRUnichar
* aSrc
,
104 PRInt32
* aSrcLength
,
106 PRInt32
* aDestLength
);
109 NS_IMETHOD
Finish(char * aDest
, PRInt32
* aDestLength
);
111 NS_IMETHOD
GetMaxLength(const PRUnichar
* aSrc
,
113 PRInt32
* aDestLength
);
115 // defined by the Decoder: NS_IMETHOD Reset();
117 NS_IMETHOD
SetOutputErrorBehavior(PRInt32 aBehavior
,
118 nsIUnicharEncoder
* aEncoder
,
121 NS_IMETHOD
FillInfo(PRUint32
* aInfo
);
126 NS_IMPL_ISUPPORTS3(WinCEUConvAdapter
,
129 nsICharRepresentable
)
131 WinCEUConvAdapter::WinCEUConvAdapter()
136 WinCEUConvAdapter::~WinCEUConvAdapter()
140 typedef struct CharsetCodePageMap
{
146 static CsCpMap theCsCPMap
[] = {
151 {"HZ-GB-2312 ", 52936},
159 {"ISO-2022-JP", 50220},
160 {"ISO-2022-KR", 50225},
161 {"ISO-8859-15", 28605},
162 {"ISO-8859-1", 28591},
163 {"ISO-8859-2", 28592},
164 {"ISO-8859-3", 28593},
165 {"ISO-8859-4", 28594},
166 {"ISO-8859-5", 28595},
167 {"ISO-8859-6", 28596},
168 {"ISO-8859-7", 28597},
169 {"ISO-8859-8", 28598},
170 {"ISO-8859-8-I", 1255},
171 {"ISO-8859-9", 28599},
178 {"ks_c_5601-1987", 949},
180 {"windows-1250", 1250},
181 {"windows-1251", 1251},
182 {"windows-1252", 1252},
183 {"windows-1253", 1253},
184 {"windows-1254", 1254},
185 {"windows-1255", 1255},
186 {"windows-1256", 1256},
187 {"windows-1257", 1257},
188 {"windows-1258", 1258},
189 {"windows-874", 874},
190 {"windows-936", 936},
191 {"x-mac-arabic", 10004},
193 {"x-mac-cyrillic", 10007},
194 {"x-mac-greek", 10006},
195 {"x-mac-hebrew", 10005},
196 {"x-mac-icelandi", 10079},
203 WinCEUConvAdapter::Init(const char* from
, const char* to
)
205 const char* cpstring
= nsnull
;
207 if (!strcmp(from
, "UCS-2"))
219 if (theCsCPMap
[i
].charset
[0] == nsnull
)
222 if (!strcmp(theCsCPMap
[i
].charset
, cpstring
))
224 mCodepage
= theCsCPMap
[i
].codepage
;
231 return NS_ERROR_FAILURE
;
237 WinCEUConvAdapter::Convert(const char * aSrc
,
238 PRInt32
* aSrcLength
,
240 PRInt32
* aDestLength
)
243 return NS_ERROR_FAILURE
;
245 int count
= MultiByteToWideChar(mCodepage
,
252 if (count
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
)
254 // fall back on the current system Windows "ANSI" code page
255 count
= MultiByteToWideChar(CP_ACP
,
265 DisplayLastError("MultiByteToWideChar");
268 *aDestLength
= count
;
274 WinCEUConvAdapter::GetMaxLength(const char * aSrc
,
276 PRInt32
* aDestLength
)
278 if (mCodepage
== -1 || aSrc
== nsnull
)
279 return NS_ERROR_FAILURE
;
281 int count
= MultiByteToWideChar(mCodepage
,
288 if (count
== 0 && GetLastError() == ERROR_INVALID_PARAMETER
)
290 // fall back on the current system Windows "ANSI" code page
292 count
= MultiByteToWideChar(CP_ACP
,
302 DisplayLastError("MultiByteToWideChar (0)");
305 *aDestLength
= count
;
310 WinCEUConvAdapter::Reset()
318 WinCEUConvAdapter::Convert(const PRUnichar
* aSrc
,
319 PRInt32
* aSrcLength
,
321 PRInt32
* aDestLength
)
324 return NS_ERROR_FAILURE
;
326 char * defaultChar
= "?";
327 int count
= WideCharToMultiByte(mCodepage
,
338 DisplayLastError("WideCharToMultiByte");
342 *aDestLength
= count
;
349 WinCEUConvAdapter::Finish(char * aDest
, PRInt32
* aDestLength
)
356 WinCEUConvAdapter::GetMaxLength(const PRUnichar
* aSrc
,
358 PRInt32
* aDestLength
)
361 return NS_ERROR_FAILURE
;
363 int count
= WideCharToMultiByte(mCodepage
,
373 DisplayLastError("WideCharToMultiByte (0)");
376 *aDestLength
= count
;
380 // defined by the Decoder: NS_IMETHOD Reset();
383 WinCEUConvAdapter::SetOutputErrorBehavior(PRInt32 aBehavior
,
384 nsIUnicharEncoder
* aEncoder
,
391 WinCEUConvAdapter::FillInfo(PRUint32
* aInfo
)
396 // NativeUConvService
398 NS_IMPL_ISUPPORTS1(NativeUConvService
,
399 nsINativeUConvService
);
402 NativeUConvService::GetNativeConverter(const char* from
,
404 nsISupports
** aResult
)
410 if (!strcmp(from
, "UCS-2") &&
411 !strcmp(to
, "UTF-8") )
413 nsUnicodeToUTF8
* inst
= new nsUnicodeToUTF8();
419 if (!strcmp(from
, "UTF-8") &&
420 !strcmp(to
, "UCS-2") )
422 nsUTF8ToUnicode
* inst
= new nsUTF8ToUnicode();
424 *aResult
= (nsIUnicodeDecoder
*) inst
;
428 WinCEUConvAdapter
* ucl
= new WinCEUConvAdapter();
430 return NS_ERROR_OUT_OF_MEMORY
;
432 nsresult rv
= ucl
->Init(from
, to
);
434 if (NS_SUCCEEDED(rv
)) {
435 NS_ADDREF(*aResult
= (nsISupports
*)(nsIUnicharEncoder
*)ucl
);