intl/uconv/public/nsIUnicodeEncoder.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/uconv/public/nsIUnicodeEncoder.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsIUnicodeEncoder_h___
    1.10 +#define nsIUnicodeEncoder_h___
    1.11 +
    1.12 +#include "nscore.h"
    1.13 +#include "nsError.h"
    1.14 +#include "nsISupports.h"
    1.15 +
    1.16 +// Interface ID for our Unicode Encoder interface
    1.17 +// {2B2CA3D0-A4C9-11d2-8AA1-00600811A836}
    1.18 +#define NS_IUNICODEENCODER_IID \
    1.19 +	{ 0x2b2ca3d0, 0xa4c9, 0x11d2, \
    1.20 +		{ 0x8a, 0xa1, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }}  
    1.21 +
    1.22 +// Interface ID for our Unicode Character Encoder interface
    1.23 +// {299BCCD0-C6DF-11d2-8AA8-00600811A836}
    1.24 +#define NS_IUNICHARENCODER_IID	\
    1.25 +	{ 0x299bccd0, 0xc6df, 0x11d2, \
    1.26 +		{0x8a, 0xa8, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 }}
    1.27 +
    1.28 +
    1.29 +#define NS_UNICODEENCODER_CONTRACTID_BASE "@mozilla.org/intl/unicode/encoder;1?charset="
    1.30 +
    1.31 +/**
    1.32 + * Interface which converts a single character from Unicode into a given 
    1.33 + * charset.
    1.34 + *
    1.35 + * @created         17/Feb/1999
    1.36 + * @author  Catalin Rotaru [CATA]
    1.37 + */
    1.38 +class nsIUnicharEncoder : public nsISupports
    1.39 +{
    1.40 +public:
    1.41 +  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICHARENCODER_IID)
    1.42 +
    1.43 +  /**
    1.44 +   * Converts a character from Unicode to a Charset.
    1.45 +   */
    1.46 +  NS_IMETHOD Convert(char16_t aChar, char * aDest, int32_t * aDestLength) = 0;
    1.47 +};
    1.48 +
    1.49 +NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicharEncoder, NS_IUNICHARENCODER_IID)
    1.50 +
    1.51 +//
    1.52 +// Malloc an Encoder (unicode -> charset) buffer if the
    1.53 +// result won't fit in the static buffer
    1.54 +//
    1.55 +//    p = the buffer pointer   (char*)
    1.56 +//    e = encoder              (nsIUnicodeEncoder*)
    1.57 +//    s = string               (char16_t*)
    1.58 +//    l = string length        (int32_t)
    1.59 +//   sb = static buffer        (char[])
    1.60 +//  sbl = static buffer length (uint32_t)
    1.61 +//   al = actual buffer length (int32_t)
    1.62 +//
    1.63 +#define ENCODER_BUFFER_ALLOC_IF_NEEDED(p,e,s,l,sb,sbl,al) \
    1.64 +  PR_BEGIN_MACRO                                          \
    1.65 +    if (e                                                 \
    1.66 +        && NS_SUCCEEDED((e)->GetMaxLength((s), (l), &(al)))\
    1.67 +        && ((al) > (int32_t)(sbl))                        \
    1.68 +        && (nullptr!=((p)=(char*)nsMemory::Alloc((al)+1))) \
    1.69 +        ) {                                               \
    1.70 +    }                                                     \
    1.71 +    else {                                                \
    1.72 +      (p) = (char*)(sb);                                  \
    1.73 +      (al) = (sbl);                                       \
    1.74 +    }                                                     \
    1.75 +  PR_END_MACRO 
    1.76 +
    1.77 +//
    1.78 +// Free the Encoder buffer if it was allocated
    1.79 +//
    1.80 +#define ENCODER_BUFFER_FREE_IF_NEEDED(p,sb) \
    1.81 +  PR_BEGIN_MACRO                            \
    1.82 +    if ((p) != (char*)(sb))                 \
    1.83 +      nsMemory::Free(p);                    \
    1.84 +  PR_END_MACRO 
    1.85 +
    1.86 +/**
    1.87 + * Interface for a Converter from Unicode into a Charset.
    1.88 + *
    1.89 + * @created         23/Nov/1998
    1.90 + * @author  Catalin Rotaru [CATA]
    1.91 + */
    1.92 +class nsIUnicodeEncoder : public nsISupports
    1.93 +{
    1.94 +public:
    1.95 +  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IUNICODEENCODER_IID)
    1.96 +
    1.97 +  enum {
    1.98 +    kOnError_Signal,        // on an error, stop and signal
    1.99 +    kOnError_CallBack,      // on an error, call the error handler
   1.100 +    kOnError_Replace       // on an error, replace with a different character
   1.101 +  };
   1.102 +
   1.103 +  /**
   1.104 +   * Converts the data from Unicode to a Charset.
   1.105 +   *
   1.106 +   * About the byte ordering:
   1.107 +   * - The input stream is Unicode, having the byte order which is internal
   1.108 +   * for the machine on which the converter is running on.
   1.109 +   * - For output, if the converter cares (that depends of the charset, for 
   1.110 +   * example a singlebyte will ignore the byte ordering) it should assume 
   1.111 +   * network order. If necessary and requested, we can add a method 
   1.112 +   * SetOutputByteOrder() so that the reverse order can be used, too. That 
   1.113 +   * method would have as default the assumed network order.
   1.114 +   *
   1.115 +   * For the last converted char, even if there is not enough output 
   1.116 +   * space, a partial output must be done until all available space will be 
   1.117 +   * used. The rest of the output should be buffered until more space becomes
   1.118 +   * available. But this is not also true about the error handling method!!!
   1.119 +   * So be very, very careful...
   1.120 +   *
   1.121 +   * @param aSrc        [IN] the source data buffer
   1.122 +   * @param aSrcLength  [IN/OUT] the length of source data buffer; after
   1.123 +   *                    conversion will contain the number of Unicode 
   1.124 +   *                    characters read
   1.125 +   * @param aDest       [OUT] the destination data buffer
   1.126 +   * @param aDestLength [IN/OUT] the length of the destination data buffer;
   1.127 +   *                    after conversion will contain the number of bytes
   1.128 +   *                    written
   1.129 +   * @return            NS_OK_UENC_MOREOUTPUT if only  a partial conversion
   1.130 +   *                    was done; more output space is needed to continue
   1.131 +   *                    NS_OK_UENC_MOREINPUT if only a partial conversion
   1.132 +   *                    was done; more input is needed to continue. This can
   1.133 +   *                    occur when the last UTF-16 code point in the input is
   1.134 +   *                    the first of a surrogate pair.
   1.135 +   *                    NS_ERROR_UENC_NOMAPPING if character without mapping
   1.136 +   *                    was encountered and the behavior was set to "signal".
   1.137 +   */
   1.138 +  NS_IMETHOD Convert(const char16_t * aSrc, int32_t * aSrcLength, 
   1.139 +      char * aDest, int32_t * aDestLength) = 0;
   1.140 +
   1.141 +  /**
   1.142 +   * Finishes the conversion. The converter has the possibility to write some 
   1.143 +   * extra data and flush its final state.
   1.144 +   *
   1.145 +   * @param aDest       [OUT] the destination data buffer
   1.146 +   * @param aDestLength [IN/OUT] the length of destination data buffer; after
   1.147 +   *                    conversion it will contain the number of bytes written
   1.148 +   * @return            NS_OK_UENC_MOREOUTPUT if only  a partial conversion
   1.149 +   *                    was done; more output space is needed to continue
   1.150 +   */
   1.151 +  NS_IMETHOD Finish(char * aDest, int32_t * aDestLength) = 0;
   1.152 +
   1.153 +  /**
   1.154 +   * Returns a quick estimation of the size of the buffer needed to hold the
   1.155 +   * converted data. Remember: this estimation is >= with the actual size of 
   1.156 +   * the buffer needed. It will be computed for the "worst case"
   1.157 +   *
   1.158 +   * @param aSrc        [IN] the source data buffer
   1.159 +   * @param aSrcLength  [IN] the length of source data buffer
   1.160 +   * @param aDestLength [OUT] the needed size of the destination buffer
   1.161 +   * @return            NS_OK_UENC_EXACTLENGTH if an exact length was computed
   1.162 +   *                    NS_OK if all we have is an approximation
   1.163 +   */
   1.164 +  NS_IMETHOD GetMaxLength(const char16_t * aSrc, int32_t aSrcLength, 
   1.165 +      int32_t * aDestLength) = 0;
   1.166 +
   1.167 +  /**
   1.168 +   * Resets the charset converter so it may be recycled for a completely 
   1.169 +   * different and urelated buffer of data.
   1.170 +   */
   1.171 +  NS_IMETHOD Reset() = 0;
   1.172 +
   1.173 +  /**
   1.174 +   * Specify what to do when a character cannot be mapped into the dest charset
   1.175 +   *
   1.176 +   * @param aOrder      [IN] the behavior; taken from the enum
   1.177 +   */
   1.178 +  NS_IMETHOD SetOutputErrorBehavior(int32_t aBehavior, 
   1.179 +      nsIUnicharEncoder * aEncoder, char16_t aChar) = 0;
   1.180 +};
   1.181 +
   1.182 +NS_DEFINE_STATIC_IID_ACCESSOR(nsIUnicodeEncoder, NS_IUNICODEENCODER_IID)
   1.183 +
   1.184 +#endif /* nsIUnicodeEncoder_h___ */

mercurial