Back to index

lightning-sunbird  0.9+nobinonly
nsUnicodeEncodeHelper.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "pratom.h"
00039 #include "unicpriv.h"
00040 #include "nsIUnicodeEncoder.h"
00041 #include "nsIUnicodeEncodeHelper.h"
00042 #include "nsUConvDll.h"
00043 #include "nsIMappingCache.h"
00044 #include "nsMappingCache.h"
00045 #include "nsUnicodeEncodeHelper.h"
00046 
00047 //----------------------------------------------------------------------
00048 // Class nsUnicodeEncodeHelper [implementation]
00049 
00050 NS_IMPL_ISUPPORTS1(nsUnicodeEncodeHelper, nsIUnicodeEncodeHelper)
00051 
00052 nsUnicodeEncodeHelper::nsUnicodeEncodeHelper() 
00053 {
00054 }
00055 
00056 nsUnicodeEncodeHelper::~nsUnicodeEncodeHelper() 
00057 {
00058 }
00059 
00060 //----------------------------------------------------------------------
00061 // Interface nsIUnicodeEncodeHelper [implementation]
00062 
00063 NS_IMETHODIMP nsUnicodeEncodeHelper::ConvertByTable(
00064                                      const PRUnichar * aSrc, 
00065                                      PRInt32 * aSrcLength, 
00066                                      char * aDest, 
00067                                      PRInt32 * aDestLength, 
00068                                      uShiftTable * aShiftTable, 
00069                                      uMappingTable  * aMappingTable)
00070 {
00071   const PRUnichar * src = aSrc;
00072   const PRUnichar * srcEnd = aSrc + *aSrcLength;
00073   char * dest = aDest;
00074   PRInt32 destLen = *aDestLength;
00075 
00076   PRUnichar med;
00077   PRInt32 bcw; // byte count for write;
00078   nsresult res = NS_OK;
00079 
00080   while (src < srcEnd) {
00081     if (!uMapCode((uTable*) aMappingTable, NS_STATIC_CAST(PRUnichar, *(src++)), NS_REINTERPRET_CAST(PRUint16*, &med))) {
00082       res = NS_ERROR_UENC_NOMAPPING;
00083       break;
00084     }
00085 
00086     if (!uGenerate(aShiftTable, 0, med, (PRUint8 *)dest, destLen, 
00087       (PRUint32 *)&bcw)) { 
00088       src--;
00089       res = NS_OK_UENC_MOREOUTPUT;
00090       break;
00091     }
00092 
00093     dest += bcw;
00094     destLen -= bcw;
00095   }
00096 
00097   *aSrcLength = src - aSrc;
00098   *aDestLength  = dest - aDest;
00099   return res;
00100 }
00101 
00102 NS_IMETHODIMP nsUnicodeEncodeHelper::ConvertByMultiTable(
00103                                      const PRUnichar * aSrc, 
00104                                      PRInt32 * aSrcLength, 
00105                                      char * aDest, 
00106                                      PRInt32 * aDestLength, 
00107                                      PRInt32 aTableCount, 
00108                                      uShiftTable ** aShiftTable, 
00109                                      uMappingTable  ** aMappingTable)
00110 {
00111   const PRUnichar * src = aSrc;
00112   const PRUnichar * srcEnd = aSrc + *aSrcLength;
00113   char * dest = aDest;
00114   PRInt32 destLen = *aDestLength;
00115 
00116   PRUnichar med;
00117   PRInt32 bcw; // byte count for write;
00118   nsresult res = NS_OK;
00119   PRInt32 i;
00120 
00121   while (src < srcEnd) {
00122     for (i=0; i<aTableCount; i++) 
00123       if (uMapCode((uTable*) aMappingTable[i], NS_STATIC_CAST(PRUint16, *src), NS_REINTERPRET_CAST(PRUint16*, &med))) break;
00124 
00125     src++;
00126     if (i == aTableCount) {
00127       res = NS_ERROR_UENC_NOMAPPING;
00128       break;
00129     }
00130 
00131     if (!uGenerate(aShiftTable[i], 0, med, (PRUint8 *)dest, destLen, 
00132       (PRUint32 *)&bcw)) { 
00133       src--;
00134       res = NS_OK_UENC_MOREOUTPUT;
00135       break;
00136     }
00137 
00138     dest += bcw;
00139     destLen -= bcw;
00140   }
00141 
00142   *aSrcLength = src - aSrc;
00143   *aDestLength  = dest - aDest;
00144   return res;
00145 }
00146 
00147 
00148 NS_IMETHODIMP nsUnicodeEncodeHelper::CreateCache(nsMappingCacheType aType, nsIMappingCache* aResult)
00149 {
00150    return nsMappingCache::CreateCache(aType, aResult);
00151 }
00152 
00153 NS_IMETHODIMP nsUnicodeEncodeHelper::DestroyCache(nsIMappingCache aCache)
00154 {
00155    return nsMappingCache::DestroyCache(aCache);
00156 }
00157 
00158 NS_IMETHODIMP nsUnicodeEncodeHelper::FillInfo(PRUint32 *aInfo, uMappingTable  * aMappingTable)
00159 {
00160    uFillInfo((uTable*) aMappingTable, aInfo);
00161    return NS_OK;
00162 }
00163 NS_IMETHODIMP nsUnicodeEncodeHelper::FillInfo(PRUint32 *aInfo, PRInt32 aTableCount, uMappingTable  ** aMappingTable)
00164 {
00165    for (PRInt32 i=0; i<aTableCount; i++) 
00166       uFillInfo((uTable*) aMappingTable[i], aInfo);
00167    return NS_OK;
00168 }