intl/uconv/ucvcn/nsUnicodeToGB2312V2.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/uconv/ucvcn/nsUnicodeToGB2312V2.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,65 @@
     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 +#include "nsUnicodeToGB2312V2.h"
    1.10 +#include "gbku.h"
    1.11 +
    1.12 +//----------------------------------------------------------------------
    1.13 +// Class nsUnicodeToGB2312V2 [implementation]
    1.14 +nsUnicodeToGB2312V2::nsUnicodeToGB2312V2() :
    1.15 +  nsEncoderSupport(2)
    1.16 +{
    1.17 +}
    1.18 +
    1.19 +NS_IMETHODIMP nsUnicodeToGB2312V2::ConvertNoBuff(const char16_t * aSrc, 
    1.20 +                                                 int32_t * aSrcLength, 
    1.21 +                                                 char * aDest, 
    1.22 +                                                 int32_t * aDestLength)
    1.23 +{
    1.24 +  int32_t iSrcLength = 0;
    1.25 +  int32_t iDestLength = 0;
    1.26 +  nsresult res = NS_OK;
    1.27 +  
    1.28 +  while (iSrcLength < *aSrcLength)
    1.29 +  {
    1.30 +    //if unicode's hi byte has something, it is not ASCII, must be a GB
    1.31 +    if(IS_ASCII(*aSrc))
    1.32 +    {
    1.33 +      // this is an ASCII
    1.34 +      *aDest = CAST_UNICHAR_TO_CHAR(*aSrc);
    1.35 +      aDest++; // increment 1 byte
    1.36 +      iDestLength +=1;
    1.37 +    } else {
    1.38 +      char byte1, byte2;
    1.39 +      if(mUtil.UnicodeToGBKChar(*aSrc, false, &byte1, &byte2))
    1.40 +      {
    1.41 +        if(iDestLength+2 > *aDestLength) 
    1.42 +        {
    1.43 +          res = NS_OK_UENC_MOREOUTPUT;
    1.44 +          break;
    1.45 +        }
    1.46 +        aDest[0]=byte1;
    1.47 +        aDest[1]=byte2;
    1.48 +        aDest += 2;  // increment 2 bytes
    1.49 +        iDestLength +=2; // each GB char count as two in char* string
    1.50 +      } else {
    1.51 +        // cannot convert
    1.52 +        res= NS_ERROR_UENC_NOMAPPING;
    1.53 +        iSrcLength++;   // include length of the unmapped character
    1.54 +        break;
    1.55 +      }
    1.56 +    }
    1.57 +    iSrcLength++ ;   // each unicode char just count as one in char16_t* string
    1.58 +    aSrc++;  
    1.59 +    if ( iDestLength >= (*aDestLength) && (iSrcLength < *aSrcLength ))
    1.60 +    {
    1.61 +      res = NS_OK_UENC_MOREOUTPUT;
    1.62 +      break;
    1.63 +    }
    1.64 +  }
    1.65 +  *aDestLength = iDestLength;
    1.66 +  *aSrcLength = iSrcLength;
    1.67 +  return res;
    1.68 +}

mercurial