michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsIUnicodeEncoder_h___ michael@0: #define nsIUnicodeEncoder_h___ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsError.h" michael@0: #include "nsISupports.h" michael@0: michael@0: // Interface ID for our Unicode Encoder interface michael@0: // {2B2CA3D0-A4C9-11d2-8AA1-00600811A836} michael@0: #define NS_IUNICODEENCODER_IID \ michael@0: { 0x2b2ca3d0, 0xa4c9, 0x11d2, \ michael@0: { 0x8a, 0xa1, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }} michael@0: michael@0: // Interface ID for our Unicode Character Encoder interface michael@0: // {299BCCD0-C6DF-11d2-8AA8-00600811A836} michael@0: #define NS_IUNICHARENCODER_IID \ michael@0: { 0x299bccd0, 0xc6df, 0x11d2, \ michael@0: {0x8a, 0xa8, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }} michael@0: michael@0: michael@0: #define NS_UNICODEENCODER_CONTRACTID_BASE "@mozilla.org/intl/unicode/encoder;1?charset=" michael@0: michael@0: /** michael@0: * Interface which converts a single character from Unicode into a given michael@0: * charset. michael@0: * michael@0: * @created 17/Feb/1999 michael@0: * @author Catalin Rotaru [CATA] michael@0: */ michael@0: class nsIUnicharEncoder : public nsISupports michael@0: { michael@0: public: michael@0: NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICHARENCODER_IID) michael@0: michael@0: /** michael@0: * Converts a character from Unicode to a Charset. michael@0: */ michael@0: NS_IMETHOD Convert(char16_t aChar, char * aDest, int32_t * aDestLength) = 0; michael@0: }; michael@0: michael@0: NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicharEncoder, NS_IUNICHARENCODER_IID) michael@0: michael@0: // michael@0: // Malloc an Encoder (unicode -> charset) buffer if the michael@0: // result won't fit in the static buffer michael@0: // michael@0: // p = the buffer pointer (char*) michael@0: // e = encoder (nsIUnicodeEncoder*) michael@0: // s = string (char16_t*) michael@0: // l = string length (int32_t) michael@0: // sb = static buffer (char[]) michael@0: // sbl = static buffer length (uint32_t) michael@0: // al = actual buffer length (int32_t) michael@0: // michael@0: #define ENCODER_BUFFER_ALLOC_IF_NEEDED(p,e,s,l,sb,sbl,al) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (e \ michael@0: && NS_SUCCEEDED((e)->GetMaxLength((s), (l), &(al)))\ michael@0: && ((al) > (int32_t)(sbl)) \ michael@0: && (nullptr!=((p)=(char*)nsMemory::Alloc((al)+1))) \ michael@0: ) { \ michael@0: } \ michael@0: else { \ michael@0: (p) = (char*)(sb); \ michael@0: (al) = (sbl); \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: // michael@0: // Free the Encoder buffer if it was allocated michael@0: // michael@0: #define ENCODER_BUFFER_FREE_IF_NEEDED(p,sb) \ michael@0: PR_BEGIN_MACRO \ michael@0: if ((p) != (char*)(sb)) \ michael@0: nsMemory::Free(p); \ michael@0: PR_END_MACRO michael@0: michael@0: /** michael@0: * Interface for a Converter from Unicode into a Charset. michael@0: * michael@0: * @created 23/Nov/1998 michael@0: * @author Catalin Rotaru [CATA] michael@0: */ michael@0: class nsIUnicodeEncoder : public nsISupports michael@0: { michael@0: public: michael@0: NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICODEENCODER_IID) michael@0: michael@0: enum { michael@0: kOnError_Signal, // on an error, stop and signal michael@0: kOnError_CallBack, // on an error, call the error handler michael@0: kOnError_Replace // on an error, replace with a different character michael@0: }; michael@0: michael@0: /** michael@0: * Converts the data from Unicode to a Charset. michael@0: * michael@0: * About the byte ordering: michael@0: * - The input stream is Unicode, having the byte order which is internal michael@0: * for the machine on which the converter is running on. michael@0: * - For output, if the converter cares (that depends of the charset, for michael@0: * example a singlebyte will ignore the byte ordering) it should assume michael@0: * network order. If necessary and requested, we can add a method michael@0: * SetOutputByteOrder() so that the reverse order can be used, too. That michael@0: * method would have as default the assumed network order. michael@0: * michael@0: * For the last converted char, even if there is not enough output michael@0: * space, a partial output must be done until all available space will be michael@0: * used. The rest of the output should be buffered until more space becomes michael@0: * available. But this is not also true about the error handling method!!! michael@0: * So be very, very careful... michael@0: * michael@0: * @param aSrc [IN] the source data buffer michael@0: * @param aSrcLength [IN/OUT] the length of source data buffer; after michael@0: * conversion will contain the number of Unicode michael@0: * characters read michael@0: * @param aDest [OUT] the destination data buffer michael@0: * @param aDestLength [IN/OUT] the length of the destination data buffer; michael@0: * after conversion will contain the number of bytes michael@0: * written michael@0: * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion michael@0: * was done; more output space is needed to continue michael@0: * NS_OK_UENC_MOREINPUT if only a partial conversion michael@0: * was done; more input is needed to continue. This can michael@0: * occur when the last UTF-16 code point in the input is michael@0: * the first of a surrogate pair. michael@0: * NS_ERROR_UENC_NOMAPPING if character without mapping michael@0: * was encountered and the behavior was set to "signal". michael@0: */ michael@0: NS_IMETHOD Convert(const char16_t * aSrc, int32_t * aSrcLength, michael@0: char * aDest, int32_t * aDestLength) = 0; michael@0: michael@0: /** michael@0: * Finishes the conversion. The converter has the possibility to write some michael@0: * extra data and flush its final state. michael@0: * michael@0: * @param aDest [OUT] the destination data buffer michael@0: * @param aDestLength [IN/OUT] the length of destination data buffer; after michael@0: * conversion it will contain the number of bytes written michael@0: * @return NS_OK_UENC_MOREOUTPUT if only a partial conversion michael@0: * was done; more output space is needed to continue michael@0: */ michael@0: NS_IMETHOD Finish(char * aDest, int32_t * aDestLength) = 0; michael@0: michael@0: /** michael@0: * Returns a quick estimation of the size of the buffer needed to hold the michael@0: * converted data. Remember: this estimation is >= with the actual size of michael@0: * the buffer needed. It will be computed for the "worst case" michael@0: * michael@0: * @param aSrc [IN] the source data buffer michael@0: * @param aSrcLength [IN] the length of source data buffer michael@0: * @param aDestLength [OUT] the needed size of the destination buffer michael@0: * @return NS_OK_UENC_EXACTLENGTH if an exact length was computed michael@0: * NS_OK if all we have is an approximation michael@0: */ michael@0: NS_IMETHOD GetMaxLength(const char16_t * aSrc, int32_t aSrcLength, michael@0: int32_t * aDestLength) = 0; michael@0: michael@0: /** michael@0: * Resets the charset converter so it may be recycled for a completely michael@0: * different and urelated buffer of data. michael@0: */ michael@0: NS_IMETHOD Reset() = 0; michael@0: michael@0: /** michael@0: * Specify what to do when a character cannot be mapped into the dest charset michael@0: * michael@0: * @param aOrder [IN] the behavior; taken from the enum michael@0: */ michael@0: NS_IMETHOD SetOutputErrorBehavior(int32_t aBehavior, michael@0: nsIUnicharEncoder * aEncoder, char16_t aChar) = 0; michael@0: }; michael@0: michael@0: NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicodeEncoder, NS_IUNICODEENCODER_IID) michael@0: michael@0: #endif /* nsIUnicodeEncoder_h___ */