intl/uconv/ucvcn/nsUnicodeToHZ.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/uconv/ucvcn/nsUnicodeToHZ.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/* -*- Mode: C; tab-width: 4; 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 + * A character set converter from Unicode to HZ.
    1.10 + * 
    1.11 + *
    1.12 + * @created         08/Sept/1999
    1.13 + * @author  Yueheng Xu, Yueheng.Xu@intel.com
    1.14 + * Revision History
    1.15 + * 04/Oct/1999. Yueheng Xu: Fixed line continuation problem when line 
    1.16 + *                          ended by '~';
    1.17 + *              Used table UnicodeToGBK[] to speed up the mapping.
    1.18 + */
    1.19 +#include "nsUnicodeToHZ.h"
    1.20 +#include "gbku.h"
    1.21 +//----------------------------------------------------------------------
    1.22 +// Class nsUnicodeToGBK [implementation]
    1.23 +#define HZ_STATE_GB		1
    1.24 +#define HZ_STATE_ASCII	2
    1.25 +#define HZ_STATE_TILD	3
    1.26 +#define HZLEAD1 '~'
    1.27 +#define HZLEAD2 '{'
    1.28 +#define HZLEAD3 '}'
    1.29 +#define UNICODE_TILD	0x007E
    1.30 +nsUnicodeToHZ::nsUnicodeToHZ() : nsEncoderSupport(6)
    1.31 +{
    1.32 +  mHZState = HZ_STATE_ASCII;	// per HZ spec, default to HZ mode
    1.33 +}
    1.34 +NS_IMETHODIMP nsUnicodeToHZ::ConvertNoBuff(
    1.35 +  const char16_t * aSrc, 
    1.36 +  int32_t * aSrcLength, 
    1.37 +  char * aDest, 
    1.38 +  int32_t * aDestLength)
    1.39 +{
    1.40 +  int32_t i=0;
    1.41 +  int32_t iSrcLength = *aSrcLength;
    1.42 +  int32_t iDestLength = 0;
    1.43 +
    1.44 +  for (i=0;i< iSrcLength;i++)
    1.45 +  {
    1.46 +    if(! IS_ASCII(*aSrc))
    1.47 +    {
    1.48 +      // hi byte has something, it is not ASCII, process as a GB
    1.49 +      if ( mHZState != HZ_STATE_GB )
    1.50 +      {
    1.51 +        // we are adding a '~{' ESC sequence to star a HZ string
    1.52 +        mHZState = HZ_STATE_GB;
    1.53 +        aDest[0] = '~';
    1.54 +        aDest[1] = '{';
    1.55 +        aDest += 2;	// increment 2 bytes
    1.56 +        iDestLength +=2;
    1.57 +      }
    1.58 +      if(mUtil.UnicodeToGBKChar(*aSrc, true, &aDest[0], &aDest[1])) {
    1.59 +        aDest += 2;	// increment 2 bytes
    1.60 +        iDestLength +=2;
    1.61 +      } else {
    1.62 +        // some thing that we cannot convert
    1.63 +        // xxx fix me ftang
    1.64 +        // error handling here
    1.65 +      }
    1.66 +    } else {
    1.67 +      // this is an ASCII
    1.68 +
    1.69 +      // if we are in HZ mode, end it by adding a '~}' ESC sequence
    1.70 +      if ( mHZState == HZ_STATE_GB )
    1.71 +      {
    1.72 +        mHZState = HZ_STATE_ASCII;
    1.73 +        aDest[0] = '~';
    1.74 +        aDest[1] = '}';
    1.75 +        aDest += 2; // increment 2 bytes
    1.76 +        iDestLength +=2;
    1.77 +      }
    1.78 +          
    1.79 +      // if this is a regular char '~' , convert it to two '~'
    1.80 +      if ( *aSrc == UNICODE_TILD )
    1.81 +      {
    1.82 +        aDest[0] = '~';
    1.83 +        aDest[1] = '~';
    1.84 +        aDest += 2; // increment 2 bytes
    1.85 +        iDestLength +=2;
    1.86 +      } else {
    1.87 +        // other regular ASCII chars convert by normal ways
    1.88 +	
    1.89 +        // Is this works for both little endian and big endian machines ?
    1.90 +        *aDest = (char) ( (char16_t)(*aSrc) );
    1.91 +        aDest++; // increment 1 byte
    1.92 +        iDestLength +=1;
    1.93 +      }
    1.94 +    }
    1.95 +    aSrc++;	 // increment 2 bytes
    1.96 +    if ( iDestLength >= (*aDestLength) )
    1.97 +    {
    1.98 +      break;
    1.99 +    }
   1.100 +  }
   1.101 +  *aDestLength = iDestLength;
   1.102 +  *aSrcLength = i;
   1.103 +  return NS_OK;
   1.104 +}
   1.105 +
   1.106 +NS_IMETHODIMP nsUnicodeToHZ::FinishNoBuff(char * aDest, int32_t * aDestLength)
   1.107 +{
   1.108 +  if ( mHZState == HZ_STATE_GB )
   1.109 +  {
   1.110 +    // if we are in HZ mode, end it by adding a '~}' ESC sequence
   1.111 +    mHZState = HZ_STATE_ASCII;
   1.112 +    aDest[0] = '~';
   1.113 +    aDest[1] = '}';
   1.114 +    *aDestLength = 2;
   1.115 +  } else {
   1.116 +    *aDestLength = 0;
   1.117 +  }
   1.118 +  return NS_OK;
   1.119 +}

mercurial