Back to index

lightning-sunbird  0.9+nobinonly
nsWinCEUConvService.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is WinCEUConvAdapter for Windows CE
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Doug Turner <dougt@meer.net>.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include <windows.h>
00038 
00039 #include "nsNativeUConvService.h"
00040 #include "nsIUnicodeDecoder.h"
00041 #include "nsIUnicodeEncoder.h"
00042 #include "nsICharRepresentable.h"
00043 #include "nsIPlatformCharset.h"
00044 #include "nsIServiceManager.h"
00045 
00046 #include "nsUCSupport.h"
00047 #include "nsUTF8ToUnicode.h"
00048 #include "nsUnicodeToUTF8.h"
00049 
00050 #ifdef ALERT_DBG
00051 void DisplayLastError(const char * msg)
00052 {
00053   int flags = MB_APPLMODAL | MB_TOPMOST | MB_SETFOREGROUND;
00054     int error = GetLastError();
00055     switch (error)
00056     {
00057     case ERROR_NO_UNICODE_TRANSLATION:
00058       MessageBox(0, "ERROR_NO_UNICODE_TRANSLATION", msg, flags);
00059       break;
00060     case ERROR_INVALID_PARAMETER:
00061       MessageBox(0, "ERROR_INVALID_PARAMETER", msg, flags);
00062       break;
00063     case ERROR_INVALID_FLAGS:
00064       MessageBox(0, "ERROR_INVALID_FLAGS", msg, flags);
00065       break;
00066     case ERROR_INSUFFICIENT_BUFFER:
00067       MessageBox(0, "ERROR_INSUFFICIENT_BUFFER", msg, flags);
00068       break;
00069     default:
00070       MessageBox(0, "other...", msg, flags);
00071     }
00072 }
00073 #endif
00074 
00075 
00076 class WinCEUConvAdapter : public nsIUnicodeDecoder,
00077                           public nsIUnicodeEncoder,
00078                           public nsICharRepresentable
00079 {
00080 public:
00081   
00082   WinCEUConvAdapter();
00083   virtual ~WinCEUConvAdapter();
00084   
00085   nsresult Init(const char* from, const char* to);
00086   
00087   NS_DECL_ISUPPORTS;
00088   
00089   // Decoder methods:
00090   
00091   NS_IMETHOD Convert(const char * aSrc, 
00092                      PRInt32 * aSrcLength, 
00093                      PRUnichar * aDest, 
00094                      PRInt32 * aDestLength);
00095   
00096   NS_IMETHOD GetMaxLength(const char * aSrc, 
00097                           PRInt32 aSrcLength, 
00098                           PRInt32 * aDestLength);
00099   NS_IMETHOD Reset();
00100   
00101   // Encoder methods:
00102   
00103   NS_IMETHOD Convert(const PRUnichar * aSrc, 
00104                      PRInt32 * aSrcLength, 
00105                      char * aDest, 
00106                      PRInt32 * aDestLength);
00107   
00108   
00109   NS_IMETHOD Finish(char * aDest, PRInt32 * aDestLength);
00110   
00111   NS_IMETHOD GetMaxLength(const PRUnichar * aSrc, 
00112                           PRInt32 aSrcLength, 
00113                           PRInt32 * aDestLength);
00114   
00115   // defined by the Decoder:  NS_IMETHOD Reset();
00116   
00117   NS_IMETHOD SetOutputErrorBehavior(PRInt32 aBehavior, 
00118                                     nsIUnicharEncoder * aEncoder, 
00119                                     PRUnichar aChar);
00120   
00121   NS_IMETHOD FillInfo(PRUint32* aInfo);
00122   
00123   PRUint32 mCodepage;
00124 };
00125 
00126 NS_IMPL_ISUPPORTS3(WinCEUConvAdapter,
00127                    nsIUnicodeDecoder,
00128                    nsIUnicodeEncoder,
00129                    nsICharRepresentable)
00130 
00131 WinCEUConvAdapter::WinCEUConvAdapter()
00132 {
00133   mCodepage = -1;
00134 }
00135 
00136 WinCEUConvAdapter::~WinCEUConvAdapter()
00137 {
00138 }
00139 
00140 typedef struct CharsetCodePageMap {
00141   char      charset[32];
00142   PRUint16  codepage;
00143 } CsCpMap;
00144 
00145 
00146 static CsCpMap theCsCPMap[] = {
00147   {"Big5",    950},
00148   {"EUC-JP", 51932},     
00149   {"EUC-KR", 949},  
00150   {"GB2312", 936},  
00151   {"HZ-GB-2312    ", 52936},     
00152   {"IBM850", 850},   
00153   {"IBM852", 852},   
00154   {"IBM855", 855},   
00155   {"IBM857", 857},   
00156   {"IBM862", 862},   
00157   {"IBM866", 866},   
00158   {"IBM869", 869},   
00159   {"ISO-2022-JP", 50220},     
00160   {"ISO-2022-KR", 50225},     
00161   {"ISO-8859-15", 28605},     
00162   {"ISO-8859-1", 28591},     
00163   {"ISO-8859-2", 28592},     
00164   {"ISO-8859-3", 28593},     
00165   {"ISO-8859-4", 28594},     
00166   {"ISO-8859-5", 28595},     
00167   {"ISO-8859-6", 28596},     
00168   {"ISO-8859-7", 28597},     
00169   {"ISO-8859-8", 28598},     
00170   {"ISO-8859-8-I", 1255},      
00171   {"ISO-8859-9", 28599},     
00172   {"Shift_JIS", 932},       
00173   {"TIS-620", 874},       
00174   {"UTF-16", 1200},      
00175   {"UTF-7", 65000},     
00176   {"UTF-8", 65001},     
00177   {"gb_2312-80", 936},       
00178   {"ks_c_5601-1987", 949},       
00179   {"us-ascii", 20127},     
00180   {"windows-1250", 1250},      
00181   {"windows-1251", 1251},      
00182   {"windows-1252", 1252},      
00183   {"windows-1253", 1253},      
00184   {"windows-1254", 1254},      
00185   {"windows-1255", 1255},      
00186   {"windows-1256", 1256},      
00187   {"windows-1257", 1257},      
00188   {"windows-1258", 1258},      
00189   {"windows-874", 874},       
00190   {"windows-936", 936},       
00191   {"x-mac-arabic", 10004},     
00192   {"x-mac-ce", 10029},     
00193   {"x-mac-cyrillic", 10007},     
00194   {"x-mac-greek", 10006},     
00195   {"x-mac-hebrew", 10005},     
00196   {"x-mac-icelandi", 10079},     
00197   {"x-x-big5", 950},       
00198   {nsnull, 0}
00199 };
00200 
00201 
00202 nsresult
00203 WinCEUConvAdapter::Init(const char* from, const char* to)
00204 {
00205   const char* cpstring = nsnull;
00206   
00207   if (!strcmp(from, "UCS-2"))
00208   {
00209     cpstring = to;
00210   }
00211   else
00212   {
00213     cpstring = from;
00214   }
00215   
00216   int i = 0;
00217   while (1)
00218   {
00219     if (theCsCPMap[i].charset[0] == nsnull)
00220       break;
00221     
00222     if (!strcmp(theCsCPMap[i].charset, cpstring))
00223     {
00224       mCodepage = theCsCPMap[i].codepage;
00225       break;
00226     }
00227     i++;
00228   }
00229   
00230   if (mCodepage == -1)
00231     return NS_ERROR_FAILURE;
00232   
00233   return NS_OK;
00234 }
00235 
00236 NS_IMETHODIMP
00237 WinCEUConvAdapter::Convert(const char * aSrc, 
00238                            PRInt32 * aSrcLength, 
00239                            PRUnichar * aDest, 
00240                            PRInt32 * aDestLength)
00241 {
00242   if (mCodepage == -1)
00243     return NS_ERROR_FAILURE;
00244   
00245   int count = MultiByteToWideChar(mCodepage,
00246                                   0,
00247                                   aSrc,
00248                                   *aSrcLength,
00249                                   aDest,
00250                                   *aDestLength);
00251   
00252   if (count == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
00253   {
00254     // fall back on the current system Windows "ANSI" code page
00255     count = MultiByteToWideChar(CP_ACP,
00256                                 0,
00257                                 aSrc,
00258                                 *aSrcLength,
00259                                 aDest,
00260                                 *aDestLength);
00261   }
00262   
00263 #ifdef ALERT_DBG
00264   if (count == 0)
00265     DisplayLastError("MultiByteToWideChar");
00266 #endif
00267   
00268   *aDestLength = count;
00269   *aSrcLength  = count;
00270   return NS_OK;
00271 }
00272 
00273 NS_IMETHODIMP
00274 WinCEUConvAdapter::GetMaxLength(const char * aSrc, 
00275                                 PRInt32 aSrcLength, 
00276                                 PRInt32 * aDestLength)
00277 {
00278   if (mCodepage == -1 || aSrc == nsnull )
00279     return NS_ERROR_FAILURE;
00280   
00281   int count = MultiByteToWideChar(mCodepage,
00282                                   MB_PRECOMPOSED,
00283                                   aSrc,
00284                                   aSrcLength,
00285                                   NULL,
00286                                   NULL);
00287   
00288   if (count == 0 && GetLastError() == ERROR_INVALID_PARAMETER)
00289   {
00290     // fall back on the current system Windows "ANSI" code page
00291     
00292     count = MultiByteToWideChar(CP_ACP,
00293                                 MB_PRECOMPOSED,
00294                                 aSrc,
00295                                 aSrcLength,
00296                                 NULL,
00297                                 NULL);
00298   }
00299   
00300 #ifdef ALERT_DBG  
00301   if (count == 0)
00302     DisplayLastError("MultiByteToWideChar (0)");
00303 #endif
00304   
00305   *aDestLength = count;
00306   return NS_OK;
00307 }
00308 
00309 NS_IMETHODIMP
00310 WinCEUConvAdapter::Reset()
00311 {
00312   return NS_OK;
00313 }
00314 
00315 // Encoder methods:
00316 
00317 NS_IMETHODIMP
00318 WinCEUConvAdapter::Convert(const PRUnichar * aSrc, 
00319                            PRInt32 * aSrcLength, 
00320                            char * aDest, 
00321                            PRInt32 * aDestLength)
00322 {
00323   if (mCodepage == -1)
00324     return NS_ERROR_FAILURE;
00325   
00326   char * defaultChar = "?";
00327   int count = WideCharToMultiByte(mCodepage,
00328                                   0,
00329                                   aSrc,
00330                                   *aSrcLength,
00331                                   aDest,
00332                                   *aDestLength,
00333                                   defaultChar,
00334                                   NULL);
00335   
00336 #ifdef ALERT_DBG
00337   if (count == 0)
00338     DisplayLastError("WideCharToMultiByte");
00339 #endif
00340   
00341   *aSrcLength = count;
00342   *aDestLength = count;
00343   
00344   return NS_OK;
00345 }
00346 
00347 
00348 NS_IMETHODIMP
00349 WinCEUConvAdapter::Finish(char * aDest, PRInt32 * aDestLength)
00350 {
00351   *aDestLength = 0;
00352   return NS_OK;
00353 }
00354 
00355 NS_IMETHODIMP
00356 WinCEUConvAdapter::GetMaxLength(const PRUnichar * aSrc, 
00357                                 PRInt32 aSrcLength, 
00358                                 PRInt32 * aDestLength)
00359 {
00360   if (mCodepage == -1)
00361     return NS_ERROR_FAILURE;
00362   
00363   int count = WideCharToMultiByte(mCodepage,
00364                                   0,
00365                                   aSrc,
00366                                   aSrcLength,
00367                                   NULL,
00368                                   NULL,
00369                                   NULL,
00370                                   NULL);
00371 #ifdef ALERT_DBG
00372   if (count == 0)
00373     DisplayLastError("WideCharToMultiByte (0)");
00374 #endif
00375   
00376   *aDestLength = count;
00377   return NS_OK;
00378 }
00379 
00380 // defined by the Decoder:  NS_IMETHOD Reset();
00381 
00382 NS_IMETHODIMP
00383 WinCEUConvAdapter::SetOutputErrorBehavior(PRInt32 aBehavior, 
00384                                           nsIUnicharEncoder * aEncoder, 
00385                                           PRUnichar aChar)
00386 {
00387   return NS_OK;
00388 }
00389 
00390 NS_IMETHODIMP
00391 WinCEUConvAdapter::FillInfo(PRUint32* aInfo)
00392 {
00393   return NS_OK;
00394 }
00395 
00396 // NativeUConvService
00397 
00398 NS_IMPL_ISUPPORTS1(NativeUConvService, 
00399                    nsINativeUConvService);
00400 
00401 NS_IMETHODIMP 
00402 NativeUConvService::GetNativeConverter(const char* from,
00403                                        const char* to,
00404                                        nsISupports** aResult) 
00405 {
00406   *aResult = nsnull;
00407   
00408   
00409   
00410   if (!strcmp(from, "UCS-2") && 
00411       !strcmp(to,   "UTF-8") )
00412   {
00413     nsUnicodeToUTF8 * inst = new nsUnicodeToUTF8();
00414     inst->AddRef();
00415     *aResult = inst;
00416     return NS_OK;
00417   }
00418   
00419   if (!strcmp(from, "UTF-8") &&
00420       !strcmp(to,   "UCS-2") )
00421   {
00422     nsUTF8ToUnicode * inst = new nsUTF8ToUnicode();
00423     inst->AddRef();
00424     *aResult = (nsIUnicodeDecoder*) inst;
00425     return NS_OK;
00426   }
00427   
00428   WinCEUConvAdapter* ucl = new WinCEUConvAdapter();
00429   if (!ucl)
00430     return NS_ERROR_OUT_OF_MEMORY;
00431   
00432   nsresult rv = ucl->Init(from, to);
00433   
00434   if (NS_SUCCEEDED(rv)) {
00435     NS_ADDREF(*aResult = (nsISupports*)(nsIUnicharEncoder*)ucl);
00436   }
00437   
00438   return rv;
00439 }