Bug 454376 add -lCrun -lCstd for Solaris OS_LIBS, r=bsmedberg
[wine-gecko.git] / intl / uconv / native / nsWinCEUConvService.cpp
blob16881c474e8f21de54cb928927ae33ce040cf51a
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
12 * License.
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.
21 * Contributor(s):
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 ***** */
37 #include <windows.h>
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"
50 #ifdef ALERT_DBG
51 void DisplayLastError(const char * msg)
53 int flags = MB_APPLMODAL | MB_TOPMOST | MB_SETFOREGROUND;
54 int error = GetLastError();
55 switch (error)
57 case ERROR_NO_UNICODE_TRANSLATION:
58 MessageBox(0, "ERROR_NO_UNICODE_TRANSLATION", msg, flags);
59 break;
60 case ERROR_INVALID_PARAMETER:
61 MessageBox(0, "ERROR_INVALID_PARAMETER", msg, flags);
62 break;
63 case ERROR_INVALID_FLAGS:
64 MessageBox(0, "ERROR_INVALID_FLAGS", msg, flags);
65 break;
66 case ERROR_INSUFFICIENT_BUFFER:
67 MessageBox(0, "ERROR_INSUFFICIENT_BUFFER", msg, flags);
68 break;
69 default:
70 MessageBox(0, "other...", msg, flags);
73 #endif
76 class WinCEUConvAdapter : public nsIUnicodeDecoder,
77 public nsIUnicodeEncoder,
78 public nsICharRepresentable
80 public:
82 WinCEUConvAdapter();
83 virtual ~WinCEUConvAdapter();
85 nsresult Init(const char* from, const char* to);
87 NS_DECL_ISUPPORTS;
89 // Decoder methods:
91 NS_IMETHOD Convert(const char * aSrc,
92 PRInt32 * aSrcLength,
93 PRUnichar * aDest,
94 PRInt32 * aDestLength);
96 NS_IMETHOD GetMaxLength(const char * aSrc,
97 PRInt32 aSrcLength,
98 PRInt32 * aDestLength);
99 NS_IMETHOD Reset();
101 // Encoder methods:
103 NS_IMETHOD Convert(const PRUnichar * aSrc,
104 PRInt32 * aSrcLength,
105 char * aDest,
106 PRInt32 * aDestLength);
109 NS_IMETHOD Finish(char * aDest, PRInt32 * aDestLength);
111 NS_IMETHOD GetMaxLength(const PRUnichar * aSrc,
112 PRInt32 aSrcLength,
113 PRInt32 * aDestLength);
115 // defined by the Decoder: NS_IMETHOD Reset();
117 NS_IMETHOD SetOutputErrorBehavior(PRInt32 aBehavior,
118 nsIUnicharEncoder * aEncoder,
119 PRUnichar aChar);
121 NS_IMETHOD FillInfo(PRUint32* aInfo);
123 PRUint32 mCodepage;
126 NS_IMPL_ISUPPORTS3(WinCEUConvAdapter,
127 nsIUnicodeDecoder,
128 nsIUnicodeEncoder,
129 nsICharRepresentable)
131 WinCEUConvAdapter::WinCEUConvAdapter()
133 mCodepage = -1;
136 WinCEUConvAdapter::~WinCEUConvAdapter()
140 typedef struct CharsetCodePageMap {
141 char charset[32];
142 PRUint16 codepage;
143 } CsCpMap;
146 static CsCpMap theCsCPMap[] = {
147 {"Big5", 950},
148 {"EUC-JP", 51932},
149 {"EUC-KR", 949},
150 {"GB2312", 936},
151 {"HZ-GB-2312 ", 52936},
152 {"IBM850", 850},
153 {"IBM852", 852},
154 {"IBM855", 855},
155 {"IBM857", 857},
156 {"IBM862", 862},
157 {"IBM866", 866},
158 {"IBM869", 869},
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},
172 {"Shift_JIS", 932},
173 {"TIS-620", 874},
174 {"UTF-16", 1200},
175 {"UTF-7", 65000},
176 {"UTF-8", 65001},
177 {"gb_2312-80", 936},
178 {"ks_c_5601-1987", 949},
179 {"us-ascii", 20127},
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},
192 {"x-mac-ce", 10029},
193 {"x-mac-cyrillic", 10007},
194 {"x-mac-greek", 10006},
195 {"x-mac-hebrew", 10005},
196 {"x-mac-icelandi", 10079},
197 {"x-x-big5", 950},
198 {nsnull, 0}
202 nsresult
203 WinCEUConvAdapter::Init(const char* from, const char* to)
205 const char* cpstring = nsnull;
207 if (!strcmp(from, "UCS-2"))
209 cpstring = to;
211 else
213 cpstring = from;
216 int i = 0;
217 while (1)
219 if (theCsCPMap[i].charset[0] == nsnull)
220 break;
222 if (!strcmp(theCsCPMap[i].charset, cpstring))
224 mCodepage = theCsCPMap[i].codepage;
225 break;
227 i++;
230 if (mCodepage == -1)
231 return NS_ERROR_FAILURE;
233 return NS_OK;
236 NS_IMETHODIMP
237 WinCEUConvAdapter::Convert(const char * aSrc,
238 PRInt32 * aSrcLength,
239 PRUnichar * aDest,
240 PRInt32 * aDestLength)
242 if (mCodepage == -1)
243 return NS_ERROR_FAILURE;
245 int count = MultiByteToWideChar(mCodepage,
247 aSrc,
248 *aSrcLength,
249 aDest,
250 *aDestLength);
252 if (count == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
254 // fall back on the current system Windows "ANSI" code page
255 count = MultiByteToWideChar(CP_ACP,
257 aSrc,
258 *aSrcLength,
259 aDest,
260 *aDestLength);
263 #ifdef ALERT_DBG
264 if (count == 0)
265 DisplayLastError("MultiByteToWideChar");
266 #endif
268 *aDestLength = count;
269 *aSrcLength = count;
270 return NS_OK;
273 NS_IMETHODIMP
274 WinCEUConvAdapter::GetMaxLength(const char * aSrc,
275 PRInt32 aSrcLength,
276 PRInt32 * aDestLength)
278 if (mCodepage == -1 || aSrc == nsnull )
279 return NS_ERROR_FAILURE;
281 int count = MultiByteToWideChar(mCodepage,
282 MB_PRECOMPOSED,
283 aSrc,
284 aSrcLength,
285 NULL,
286 NULL);
288 if (count == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
290 // fall back on the current system Windows "ANSI" code page
292 count = MultiByteToWideChar(CP_ACP,
293 MB_PRECOMPOSED,
294 aSrc,
295 aSrcLength,
296 NULL,
297 NULL);
300 #ifdef ALERT_DBG
301 if (count == 0)
302 DisplayLastError("MultiByteToWideChar (0)");
303 #endif
305 *aDestLength = count;
306 return NS_OK;
309 NS_IMETHODIMP
310 WinCEUConvAdapter::Reset()
312 return NS_OK;
315 // Encoder methods:
317 NS_IMETHODIMP
318 WinCEUConvAdapter::Convert(const PRUnichar * aSrc,
319 PRInt32 * aSrcLength,
320 char * aDest,
321 PRInt32 * aDestLength)
323 if (mCodepage == -1)
324 return NS_ERROR_FAILURE;
326 char * defaultChar = "?";
327 int count = WideCharToMultiByte(mCodepage,
329 aSrc,
330 *aSrcLength,
331 aDest,
332 *aDestLength,
333 defaultChar,
334 NULL);
336 #ifdef ALERT_DBG
337 if (count == 0)
338 DisplayLastError("WideCharToMultiByte");
339 #endif
341 *aSrcLength = count;
342 *aDestLength = count;
344 return NS_OK;
348 NS_IMETHODIMP
349 WinCEUConvAdapter::Finish(char * aDest, PRInt32 * aDestLength)
351 *aDestLength = 0;
352 return NS_OK;
355 NS_IMETHODIMP
356 WinCEUConvAdapter::GetMaxLength(const PRUnichar * aSrc,
357 PRInt32 aSrcLength,
358 PRInt32 * aDestLength)
360 if (mCodepage == -1)
361 return NS_ERROR_FAILURE;
363 int count = WideCharToMultiByte(mCodepage,
365 aSrc,
366 aSrcLength,
367 NULL,
368 NULL,
369 NULL,
370 NULL);
371 #ifdef ALERT_DBG
372 if (count == 0)
373 DisplayLastError("WideCharToMultiByte (0)");
374 #endif
376 *aDestLength = count;
377 return NS_OK;
380 // defined by the Decoder: NS_IMETHOD Reset();
382 NS_IMETHODIMP
383 WinCEUConvAdapter::SetOutputErrorBehavior(PRInt32 aBehavior,
384 nsIUnicharEncoder * aEncoder,
385 PRUnichar aChar)
387 return NS_OK;
390 NS_IMETHODIMP
391 WinCEUConvAdapter::FillInfo(PRUint32* aInfo)
393 return NS_OK;
396 // NativeUConvService
398 NS_IMPL_ISUPPORTS1(NativeUConvService,
399 nsINativeUConvService);
401 NS_IMETHODIMP
402 NativeUConvService::GetNativeConverter(const char* from,
403 const char* to,
404 nsISupports** aResult)
406 *aResult = nsnull;
410 if (!strcmp(from, "UCS-2") &&
411 !strcmp(to, "UTF-8") )
413 nsUnicodeToUTF8 * inst = new nsUnicodeToUTF8();
414 inst->AddRef();
415 *aResult = inst;
416 return NS_OK;
419 if (!strcmp(from, "UTF-8") &&
420 !strcmp(to, "UCS-2") )
422 nsUTF8ToUnicode * inst = new nsUTF8ToUnicode();
423 inst->AddRef();
424 *aResult = (nsIUnicodeDecoder*) inst;
425 return NS_OK;
428 WinCEUConvAdapter* ucl = new WinCEUConvAdapter();
429 if (!ucl)
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);
438 return rv;