Back to index

lightning-sunbird  0.9+nobinonly
nsIUnicodeEncoder.h
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 #ifndef nsIUnicodeEncoder_h___
00039 #define nsIUnicodeEncoder_h___
00040 
00041 #include "nscore.h"
00042 #include "nsError.h"
00043 #include "nsISupports.h"
00044 
00045 // Interface ID for our Unicode Encoder interface
00046 // {2B2CA3D0-A4C9-11d2-8AA1-00600811A836}
00047 #define NS_IUNICODEENCODER_IID \
00048        { 0x2b2ca3d0, 0xa4c9, 0x11d2, \
00049               { 0x8a, 0xa1, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }}  
00050 
00051 // Interface ID for our Unicode Character Encoder interface
00052 // {299BCCD0-C6DF-11d2-8AA8-00600811A836}
00053 #define NS_IUNICHARENCODER_IID     \
00054        { 0x299bccd0, 0xc6df, 0x11d2, \
00055               {0x8a, 0xa8, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }}
00056 
00057 #define NS_OK_UENC_EXACTLENGTH      \
00058   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_UCONV, 0x21)
00059 
00060 #define NS_OK_UENC_MOREOUTPUT       \
00061   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_UCONV, 0x22)
00062 
00063 #define NS_ERROR_UENC_NOMAPPING     \
00064   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_UCONV, 0x23)
00065 
00066 #define NS_OK_UENC_MOREINPUT       \
00067   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_UCONV, 0x24)
00068 
00069 
00070 #define NS_UNICODEENCODER_CONTRACTID_BASE "@mozilla.org/intl/unicode/encoder;1?charset="
00071 
00079 class nsIUnicharEncoder : public nsISupports
00080 {
00081 public:
00082   NS_DEFINE_STATIC_IID_ACCESSOR(NS_IUNICHARENCODER_IID)
00083 
00084   
00087   NS_IMETHOD Convert(PRUnichar aChar, char * aDest, PRInt32 * aDestLength) = 0;
00088 };
00089 
00090 //
00091 // Malloc an Encoder (unicode -> charset) buffer if the
00092 // result won't fit in the static buffer
00093 //
00094 //    p = the buffer pointer   (char*)
00095 //    e = encoder              (nsIUnicodeEncoder*)
00096 //    s = string               (PRUnichar*)
00097 //    l = string length        (PRInt32)
00098 //   sb = static buffer        (char[])
00099 //  sbl = static buffer length (PRUint32)
00100 //   al = actual buffer length (PRInt32)
00101 //
00102 #define ENCODER_BUFFER_ALLOC_IF_NEEDED(p,e,s,l,sb,sbl,al) \
00103   PR_BEGIN_MACRO                                          \
00104     if (e                                                 \
00105         && NS_SUCCEEDED((e)->GetMaxLength((s), (l), &(al)))\
00106         && ((al) > (PRInt32)(sbl))                        \
00107         && (nsnull!=((p)=(char*)nsMemory::Alloc((al)+1))) \
00108         ) {                                               \
00109     }                                                     \
00110     else {                                                \
00111       (p) = (char*)(sb);                                  \
00112       (al) = (sbl);                                       \
00113     }                                                     \
00114   PR_END_MACRO 
00115 
00116 //
00117 // Free the Encoder buffer if it was allocated
00118 //
00119 #define ENCODER_BUFFER_FREE_IF_NEEDED(p,sb) \
00120   PR_BEGIN_MACRO                            \
00121     if ((p) != (char*)(sb))                 \
00122       nsMemory::Free(p);                    \
00123   PR_END_MACRO 
00124 
00131 class nsIUnicodeEncoder : public nsISupports
00132 {
00133 public:
00134   NS_DEFINE_STATIC_IID_ACCESSOR(NS_IUNICODEENCODER_IID)
00135 
00136   enum {
00137     kOnError_Signal,        // on an error, stop and signal
00138     kOnError_CallBack,      // on an error, call the error handler
00139     kOnError_Replace       // on an error, replace with a different character
00140   };
00141 
00175   NS_IMETHOD Convert(const PRUnichar * aSrc, PRInt32 * aSrcLength, 
00176       char * aDest, PRInt32 * aDestLength) = 0;
00177 
00188   NS_IMETHOD Finish(char * aDest, PRInt32 * aDestLength) = 0;
00189 
00201   NS_IMETHOD GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength, 
00202       PRInt32 * aDestLength) = 0;
00203 
00208   NS_IMETHOD Reset() = 0;
00209 
00215   NS_IMETHOD SetOutputErrorBehavior(PRInt32 aBehavior, 
00216       nsIUnicharEncoder * aEncoder, PRUnichar aChar) = 0;
00217 };
00218 
00219 #endif /* nsIUnicodeEncoder_h___ */