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: #include "nsUnicodeToGB2312V2.h" michael@0: #include "gbku.h" michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Class nsUnicodeToGB2312V2 [implementation] michael@0: nsUnicodeToGB2312V2::nsUnicodeToGB2312V2() : michael@0: nsEncoderSupport(2) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToGB2312V2::ConvertNoBuff(const char16_t * aSrc, michael@0: int32_t * aSrcLength, michael@0: char * aDest, michael@0: int32_t * aDestLength) michael@0: { michael@0: int32_t iSrcLength = 0; michael@0: int32_t iDestLength = 0; michael@0: nsresult res = NS_OK; michael@0: michael@0: while (iSrcLength < *aSrcLength) michael@0: { michael@0: //if unicode's hi byte has something, it is not ASCII, must be a GB michael@0: if(IS_ASCII(*aSrc)) michael@0: { michael@0: // this is an ASCII michael@0: *aDest = CAST_UNICHAR_TO_CHAR(*aSrc); michael@0: aDest++; // increment 1 byte michael@0: iDestLength +=1; michael@0: } else { michael@0: char byte1, byte2; michael@0: if(mUtil.UnicodeToGBKChar(*aSrc, false, &byte1, &byte2)) michael@0: { michael@0: if(iDestLength+2 > *aDestLength) michael@0: { michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: aDest[0]=byte1; michael@0: aDest[1]=byte2; michael@0: aDest += 2; // increment 2 bytes michael@0: iDestLength +=2; // each GB char count as two in char* string michael@0: } else { michael@0: // cannot convert michael@0: res= NS_ERROR_UENC_NOMAPPING; michael@0: iSrcLength++; // include length of the unmapped character michael@0: break; michael@0: } michael@0: } michael@0: iSrcLength++ ; // each unicode char just count as one in char16_t* string michael@0: aSrc++; michael@0: if ( iDestLength >= (*aDestLength) && (iSrcLength < *aSrcLength )) michael@0: { michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: } michael@0: *aDestLength = iDestLength; michael@0: *aSrcLength = iSrcLength; michael@0: return res; michael@0: }