Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
nsUnicodeToISO2022JP Class Reference

A character set converter from Unicode to ISO2022JP. More...

#include <nsUnicodeToISO2022JP.h>

Inheritance diagram for nsUnicodeToISO2022JP:
Inheritance graph
[legend]
Collaboration diagram for nsUnicodeToISO2022JP:
Collaboration graph
[legend]

List of all members.

Public Types

enum  { kOnError_Signal, kOnError_CallBack, kOnError_Replace }

Public Member Functions

 nsUnicodeToISO2022JP ()
 Class constructor.
virtual ~nsUnicodeToISO2022JP ()
 Class destructor.
NS_IMETHOD Convert (const PRUnichar *aSrc, PRInt32 *aSrcLength, char *aDest, PRInt32 *aDestLength)
 Converts the data from Unicode to a Charset.
NS_IMETHOD Finish (char *aDest, PRInt32 *aDestLength)
 Finishes the conversion.
NS_IMETHOD SetOutputErrorBehavior (PRInt32 aBehavior, nsIUnicharEncoder *aEncoder, PRUnichar aChar)
 Specify what to do when a character cannot be mapped into the dest charset.
NS_IMETHOD GetMaxLength (const PRUnichar *aSrc, PRInt32 aSrcLength, PRInt32 *aDestLength)
 Returns a quick estimation of the size of the buffer needed to hold the converted data.

Protected Member Functions

nsresult ChangeCharset (PRInt32 aCharset, char *aDest, PRInt32 *aDestLength)
NS_IMETHOD ConvertNoBuffNoErr (const PRUnichar *aSrc, PRInt32 *aSrcLength, char *aDest, PRInt32 *aDestLength)
 Convert method but without the buffer management stuff and without error handling stuff.
NS_IMETHOD FinishNoBuff (char *aDest, PRInt32 *aDestLength)
 Finish method but without the buffer management stuff.
NS_IMETHOD Reset ()
 Resets the charset converter so it may be recycled for a completely different and urelated buffer of data.
NS_IMETHOD FillInfo (PRUint32 *aInfo)
NS_IMETHOD ConvertNoBuff (const PRUnichar *aSrc, PRInt32 *aSrcLength, char *aDest, PRInt32 *aDestLength)
 Convert method but without the buffer management stuff and with error handling stuff.
nsresult FlushBuffer (char **aDest, const char *aDestEnd)
 Copy as much as possible from the internal buffer to the destination.

Protected Attributes

PRInt32 mCharset
nsIUnicodeEncodeHelpermHelper
char * mBuffer
 Internal buffer for partial conversions.
PRInt32 mBufferCapacity
char * mBufferStart
char * mBufferEnd
PRInt32 mErrBehavior
 Error handling stuff.
nsCOMPtr< nsIUnicharEncodermErrEncoder
PRUnichar mErrChar
PRUint32 mMaxLengthFactor

Detailed Description

A character set converter from Unicode to ISO2022JP.

17/Feb/1999

Author:
Catalin Rotaru [CATA]

Definition at line 52 of file nsUnicodeToISO2022JP.h.


Member Enumeration Documentation

anonymous enum [inherited]
Enumerator:
kOnError_Signal 
kOnError_CallBack 
kOnError_Replace 

Definition at line 136 of file nsIUnicodeEncoder.h.

       {
    kOnError_Signal,        // on an error, stop and signal
    kOnError_CallBack,      // on an error, call the error handler
    kOnError_Replace       // on an error, replace with a different character
  };

Constructor & Destructor Documentation

Class constructor.

Definition at line 92 of file nsUnicodeToISO2022JP.cpp.

Here is the call graph for this function:

Class destructor.

Definition at line 99 of file nsUnicodeToISO2022JP.cpp.


Member Function Documentation

nsresult nsUnicodeToISO2022JP::ChangeCharset ( PRInt32  aCharset,
char *  aDest,
PRInt32 aDestLength 
) [protected]

Definition at line 104 of file nsUnicodeToISO2022JP.cpp.

{
  // both 2 and 3 generate the same escape sequence. 2 is for
  // the standard JISx0208 table, and 3 is for theCP932 extensions
  // therefore, we treat them as the same one.
  if(((2 == aCharset) && ( 3 == mCharset)) ||
     ((3 == aCharset) && ( 2 == mCharset)) )
  {
    mCharset = aCharset;
  }

  if(aCharset == mCharset) 
  {
    *aDestLength = 0;
    return NS_OK;
  } 
  
  if (*aDestLength < 3) {
    *aDestLength = 0;
    return NS_OK_UENC_MOREOUTPUT;
  }

  switch (aCharset) {
    case 0: // ASCII ISOREG 6
      aDest[0] = 0x1b;
      aDest[1] = '(';
      aDest[2] = 'B';
      break;
    case 1: // JIS X 0201-1976 ("Roman" set) ISOREG 14
      aDest[0] = 0x1b;
      aDest[1] = '(';
      aDest[2] = 'J';
      break;
    case 2: // JIS X 0208-1983 ISOREG 87
    case 3: // JIS X 0208-1983 
            // we currently use this for CP932 ext
      aDest[0] = 0x1b;
      aDest[1] = '$';
      aDest[2] = 'B';
      break;
    case 4: // JIS X 0201-1978 ISOREG 87- 
            // we currently do not have a diff mapping for it.
      aDest[0] = 0x1b;
      aDest[1] = '$';
      aDest[2] = '@';
      break;
  }

  mCharset = aCharset;
  *aDestLength = 3;
  return NS_OK;
}

Here is the caller graph for this function:

NS_IMETHODIMP nsEncoderSupport::Convert ( const PRUnichar aSrc,
PRInt32 aSrcLength,
char *  aDest,
PRInt32 aDestLength 
) [virtual, inherited]

Converts the data from Unicode to a Charset.

About the byte ordering:

  • The input stream is Unicode, having the byte order which is internal for the machine on which the converter is running on.
  • For output, if the converter cares (that depends of the charset, for example a singlebyte will ignore the byte ordering) it should assume network order. If necessary and requested, we can add a method SetOutputByteOrder() so that the reverse order can be used, too. That method would have as default the assumed network order.

Unless there is not enough output space, this method must consume all the available input data! We don't have partial input for the Unicode charset. And for the last converted char, even if there is not enought output space, a partial ouput must be done until all available space will be used. The rest of the output should be buffered until more space becomes available. But this is not also true about the error handling method!!! So be very, very careful...

Parameters:
aSrc[IN] the source data buffer
aSrcLength[IN/OUT] the length of source data buffer; after conversion will contain the number of Unicode characters read
aDest[OUT] the destination data buffer
aDestLength[IN/OUT] the length of the destination data buffer; after conversion will contain the number of bytes written
Returns:
NS_OK_UENC_MOREOUTPUT if only a partial conversion was done; more output space is needed to continue NS_ERROR_UENC_NOMAPPING if character without mapping was encountered and the behavior was set to "signal".

Implements nsIUnicodeEncoder.

Definition at line 475 of file nsUCSupport.cpp.

{
  // we do all operations using pointers internally
  const PRUnichar * src = aSrc;
  const PRUnichar * srcEnd = aSrc + *aSrcLength;
  char * dest = aDest;
  char * destEnd = aDest + *aDestLength;

  PRInt32 bcr, bcw; // byte counts for read & write;
  nsresult res;

  res = FlushBuffer(&dest, destEnd);
  if (res == NS_OK_UENC_MOREOUTPUT) goto final;

  bcr = srcEnd - src;
  bcw = destEnd - dest;
  res = ConvertNoBuff(src, &bcr, dest, &bcw);
  src += bcr;
  dest += bcw;
  if ((res == NS_OK_UENC_MOREOUTPUT) && (dest < destEnd)) {
    // convert exactly one character into the internal buffer
    // at this point, there should be at least a char in the input
    for (;;) {
      bcr = 1;
      bcw = mBufferCapacity;
      res = ConvertNoBuff(src, &bcr, mBuffer, &bcw);

      if (res == NS_OK_UENC_MOREOUTPUT) {
        delete [] mBuffer;
        mBufferCapacity *= 2;
        mBuffer = new char [mBufferCapacity];
      } else {
        src += bcr;
        mBufferStart = mBufferEnd = mBuffer;
        mBufferEnd += bcw;
        break;
      }
    }

    res = FlushBuffer(&dest, destEnd);
  }

final:
  *aSrcLength   -= srcEnd - src;
  *aDestLength  -= destEnd - dest;
  return res;
}

Here is the call graph for this function:

NS_IMETHODIMP nsEncoderSupport::ConvertNoBuff ( const PRUnichar aSrc,
PRInt32 aSrcLength,
char *  aDest,
PRInt32 aDestLength 
) [protected, inherited]

Convert method but without the buffer management stuff and with error handling stuff.

Reimplemented in nsUnicodeToGBK, nsUnicodeToHZ, nsUnicodeToGB2312GL, and nsUnicodeToGB2312V2.

Definition at line 394 of file nsUCSupport.cpp.

{
  // we do all operations using pointers internally
  const PRUnichar * src = aSrc;
  const PRUnichar * srcEnd = aSrc + *aSrcLength;
  char * dest = aDest;
  char * destEnd = aDest + *aDestLength;

  PRInt32 bcr, bcw; // byte counts for read & write;
  nsresult res;

  for (;;) {
    bcr = srcEnd - src;
    bcw = destEnd - dest;
    res = ConvertNoBuffNoErr(src, &bcr, dest, &bcw);
    src += bcr;
    dest += bcw;

    if (res == NS_ERROR_UENC_NOMAPPING) {
      if (mErrBehavior == kOnError_Replace) {
        const PRUnichar buff[] = {mErrChar};
        bcr = 1;
        bcw = destEnd - dest;
        src--; // back the input: maybe the guy won't consume consume anything.
        res = ConvertNoBuffNoErr(buff, &bcr, dest, &bcw);
        src += bcr;
        dest += bcw;
        if (res != NS_OK) break;
      } else if (mErrBehavior == kOnError_CallBack) {
        bcw = destEnd - dest;
        src--;
        res = mErrEncoder->Convert(*src, dest, &bcw);
        dest += bcw;
        // if enought output space then the last char was used
        if (res != NS_OK_UENC_MOREOUTPUT) src++;
        if (res != NS_OK) break;
      } else break;
    }
    else break;
  }

  *aSrcLength   -= srcEnd - src;
  *aDestLength  -= destEnd - dest;
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsUnicodeToISO2022JP::ConvertNoBuffNoErr ( const PRUnichar aSrc,
PRInt32 aSrcLength,
char *  aDest,
PRInt32 aDestLength 
) [protected, virtual]

Convert method but without the buffer management stuff and without error handling stuff.

Implements nsEncoderSupport.

Definition at line 175 of file nsUnicodeToISO2022JP.cpp.

{
  nsresult res = NS_OK;

  if (mHelper == nsnull) {
    res = CallCreateInstance(kUnicodeEncodeHelperCID, &mHelper);
    if (NS_FAILED(res)) return NS_ERROR_UENC_NOHELPER;
  }

  const PRUnichar * src = aSrc;
  const PRUnichar * srcEnd = aSrc + *aSrcLength;
  char * dest = aDest;
  char * destEnd = aDest + *aDestLength;
  PRInt32 bcr, bcw;
  PRInt32 i;

  while (src < srcEnd) {
    for (i=0; i< SIZE_OF_TABLES ; i++) {
      bcr = 1;
      bcw = destEnd - dest;
      res = mHelper->ConvertByTable(src, &bcr, dest, &bcw, 
          (uShiftTable *) g_ufShiftTables[i], 
          (uMappingTable *) g_ufMappingTables[i]);
      if (res != NS_ERROR_UENC_NOMAPPING) break;
    }

    if ( i>=  SIZE_OF_TABLES) {
      res = NS_ERROR_UENC_NOMAPPING;
      src++;
    }
    if (res != NS_OK) break;

    bcw = destEnd - dest;
    res = ChangeCharset(i, dest, &bcw);
    dest += bcw;
    if (res != NS_OK) break;

    bcr = srcEnd - src;
    bcw = destEnd - dest;
    res = mHelper->ConvertByTable(src, &bcr, dest, &bcw, 
        (uShiftTable *) g_ufShiftTables[i], 
        (uMappingTable *) g_ufMappingTables[i]);
    src += bcr;
    dest += bcw;

    if ((res != NS_OK) && (res != NS_ERROR_UENC_NOMAPPING)) break;
    if (res == NS_ERROR_UENC_NOMAPPING) src--;
  }

  *aSrcLength = src - aSrc;
  *aDestLength  = dest - aDest;
  return res;
}

Here is the call graph for this function:

NS_IMETHODIMP nsUnicodeToISO2022JP::FillInfo ( PRUint32 aInfo) [protected, virtual]

Implements nsEncoderSupport.

Definition at line 162 of file nsUnicodeToISO2022JP.cpp.

{
  nsresult res;

  if (mHelper == nsnull) {
    res = CallCreateInstance(kUnicodeEncodeHelperCID, &mHelper);
    if (NS_FAILED(res)) return NS_ERROR_UENC_NOHELPER;
  }
  return mHelper->FillInfo(aInfo, 
                  SIZE_OF_TABLES, 
                  (uMappingTable **) g_ufMappingTables);

}

Here is the call graph for this function:

NS_IMETHODIMP nsEncoderSupport::Finish ( char *  aDest,
PRInt32 aDestLength 
) [virtual, inherited]

Finishes the conversion.

The converter has the possibility to write some extra data and flush its final state.

Parameters:
aDest[OUT] the destination data buffer
aDestLength[IN/OUT] the length of destination data buffer; after conversion it will contain the number of bytes written
Returns:
NS_OK_UENC_MOREOUTPUT if only a partial conversion was done; more output space is needed to continue

Implements nsIUnicodeEncoder.

Definition at line 526 of file nsUCSupport.cpp.

{
  // we do all operations using pointers internally
  char * dest = aDest;
  char * destEnd = aDest + *aDestLength;

  PRInt32 bcw; // byte count for write;
  nsresult res;

  res = FlushBuffer(&dest, destEnd);
  if (res == NS_OK_UENC_MOREOUTPUT) goto final;

  // do the finish into the internal buffer.
  for (;;) {
    bcw = mBufferCapacity;
    res = FinishNoBuff(mBuffer, &bcw);

    if (res == NS_OK_UENC_MOREOUTPUT) {
      delete [] mBuffer;
      mBufferCapacity *= 2;
      mBuffer = new char [mBufferCapacity];
    } else {
      mBufferStart = mBufferEnd = mBuffer;
      mBufferEnd += bcw;
      break;
    }
  }

  res = FlushBuffer(&dest, destEnd);

final:
  *aDestLength  -= destEnd - dest;
  return res;
}

Here is the call graph for this function:

NS_IMETHODIMP nsUnicodeToISO2022JP::FinishNoBuff ( char *  aDest,
PRInt32 aDestLength 
) [protected]

Finish method but without the buffer management stuff.

Reimplemented from nsEncoderSupport.

Definition at line 233 of file nsUnicodeToISO2022JP.cpp.

{
  ChangeCharset(0, aDest, aDestLength);
  return NS_OK;
}

Here is the call graph for this function:

nsresult nsEncoderSupport::FlushBuffer ( char **  aDest,
const char *  aDestEnd 
) [protected, inherited]

Copy as much as possible from the internal buffer to the destination.

Definition at line 450 of file nsUCSupport.cpp.

{
  PRInt32 bcr, bcw; // byte counts for read & write;
  nsresult res = NS_OK;
  char * dest = *aDest;

  if (mBufferStart < mBufferEnd) {
    bcr = mBufferEnd - mBufferStart;
    bcw = aDestEnd - dest;
    if (bcw < bcr) bcr = bcw;
    memcpy(dest, mBufferStart, bcr);
    dest += bcr;
    mBufferStart += bcr;

    if (mBufferStart < mBufferEnd) res = NS_OK_UENC_MOREOUTPUT;
  }

  *aDest = dest;
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsEncoderSupport::GetMaxLength ( const PRUnichar aSrc,
PRInt32  aSrcLength,
PRInt32 aDestLength 
) [virtual, inherited]

Returns a quick estimation of the size of the buffer needed to hold the converted data.

Remember: this estimation is >= with the actual size of the buffer needed. It will be computed for the "worst case"

Parameters:
aSrc[IN] the source data buffer
aSrcLength[IN] the length of source data buffer
aDestLength[OUT] the needed size of the destination buffer
Returns:
NS_OK_UENC_EXACTLENGTH if an exact length was computed NS_OK if all we have is an approximation

Implements nsIUnicodeEncoder.

Definition at line 582 of file nsUCSupport.cpp.

{
  *aDestLength = aSrcLength * mMaxLengthFactor;
  return NS_OK;
}
NS_IMETHODIMP nsUnicodeToISO2022JP::Reset ( ) [protected, virtual]

Resets the charset converter so it may be recycled for a completely different and urelated buffer of data.

Reimplemented from nsEncoderSupport.

Definition at line 240 of file nsUnicodeToISO2022JP.cpp.

{
  mCharset = 0;
  return nsEncoderSupport::Reset();
}

Here is the caller graph for this function:

NS_IMETHODIMP nsEncoderSupport::SetOutputErrorBehavior ( PRInt32  aBehavior,
nsIUnicharEncoder aEncoder,
PRUnichar  aChar 
) [virtual, inherited]

Specify what to do when a character cannot be mapped into the dest charset.

Parameters:
aOrder[IN] the behavior; taken from the enum

Implements nsIUnicodeEncoder.

Definition at line 567 of file nsUCSupport.cpp.

{
  if (aBehavior == kOnError_CallBack && aEncoder == nsnull) 
    return NS_ERROR_NULL_POINTER;

  mErrEncoder = aEncoder;
  mErrBehavior = aBehavior;
  mErrChar = aChar;
  return NS_OK;
}

Member Data Documentation

char* nsEncoderSupport::mBuffer [protected, inherited]

Internal buffer for partial conversions.

Definition at line 332 of file nsUCSupport.h.

Definition at line 333 of file nsUCSupport.h.

char* nsEncoderSupport::mBufferEnd [protected, inherited]

Definition at line 335 of file nsUCSupport.h.

char* nsEncoderSupport::mBufferStart [protected, inherited]

Definition at line 334 of file nsUCSupport.h.

Definition at line 68 of file nsUnicodeToISO2022JP.h.

PRInt32 nsEncoderSupport::mErrBehavior [protected, inherited]

Error handling stuff.

Definition at line 340 of file nsUCSupport.h.

PRUnichar nsEncoderSupport::mErrChar [protected, inherited]

Definition at line 342 of file nsUCSupport.h.

Definition at line 341 of file nsUCSupport.h.

Definition at line 69 of file nsUnicodeToISO2022JP.h.

Definition at line 343 of file nsUCSupport.h.


The documentation for this class was generated from the following files: