|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsUnicodeToGB2312V2.h" |
|
7 #include "gbku.h" |
|
8 |
|
9 //---------------------------------------------------------------------- |
|
10 // Class nsUnicodeToGB2312V2 [implementation] |
|
11 nsUnicodeToGB2312V2::nsUnicodeToGB2312V2() : |
|
12 nsEncoderSupport(2) |
|
13 { |
|
14 } |
|
15 |
|
16 NS_IMETHODIMP nsUnicodeToGB2312V2::ConvertNoBuff(const char16_t * aSrc, |
|
17 int32_t * aSrcLength, |
|
18 char * aDest, |
|
19 int32_t * aDestLength) |
|
20 { |
|
21 int32_t iSrcLength = 0; |
|
22 int32_t iDestLength = 0; |
|
23 nsresult res = NS_OK; |
|
24 |
|
25 while (iSrcLength < *aSrcLength) |
|
26 { |
|
27 //if unicode's hi byte has something, it is not ASCII, must be a GB |
|
28 if(IS_ASCII(*aSrc)) |
|
29 { |
|
30 // this is an ASCII |
|
31 *aDest = CAST_UNICHAR_TO_CHAR(*aSrc); |
|
32 aDest++; // increment 1 byte |
|
33 iDestLength +=1; |
|
34 } else { |
|
35 char byte1, byte2; |
|
36 if(mUtil.UnicodeToGBKChar(*aSrc, false, &byte1, &byte2)) |
|
37 { |
|
38 if(iDestLength+2 > *aDestLength) |
|
39 { |
|
40 res = NS_OK_UENC_MOREOUTPUT; |
|
41 break; |
|
42 } |
|
43 aDest[0]=byte1; |
|
44 aDest[1]=byte2; |
|
45 aDest += 2; // increment 2 bytes |
|
46 iDestLength +=2; // each GB char count as two in char* string |
|
47 } else { |
|
48 // cannot convert |
|
49 res= NS_ERROR_UENC_NOMAPPING; |
|
50 iSrcLength++; // include length of the unmapped character |
|
51 break; |
|
52 } |
|
53 } |
|
54 iSrcLength++ ; // each unicode char just count as one in char16_t* string |
|
55 aSrc++; |
|
56 if ( iDestLength >= (*aDestLength) && (iSrcLength < *aSrcLength )) |
|
57 { |
|
58 res = NS_OK_UENC_MOREOUTPUT; |
|
59 break; |
|
60 } |
|
61 } |
|
62 *aDestLength = iDestLength; |
|
63 *aSrcLength = iSrcLength; |
|
64 return res; |
|
65 } |