Bug 468575 - Scrape some gunk off the config/ grout, r=ted
[wine-gecko.git] / intl / uconv / ucvcn / nsUnicodeToHZ.cpp
blob2369292dde9aa183991e5d1c0733df0391f9a009
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 /**
38 * A character set converter from Unicode to HZ.
41 * @created 08/Sept/1999
42 * @author Yueheng Xu, Yueheng.Xu@intel.com
43 * Revision History
44 * 04/Oct/1999. Yueheng Xu: Fixed line continuation problem when line
45 * ended by '~';
46 * Used table UnicodeToGBK[] to speed up the mapping.
48 #include "nsUnicodeToHZ.h"
49 #include "nsUCvCnDll.h"
50 #include "gbku.h"
51 //----------------------------------------------------------------------
52 // Class nsUnicodeToGBK [implementation]
53 #define HZ_STATE_GB 1
54 #define HZ_STATE_ASCII 2
55 #define HZ_STATE_TILD 3
56 #define HZLEAD1 '~'
57 #define HZLEAD2 '{'
58 #define HZLEAD3 '}'
59 #define UNICODE_TILD 0x007E
60 nsUnicodeToHZ::nsUnicodeToHZ() : nsEncoderSupport(6)
62 mUtil.InitToGBKTable();
63 mHZState = HZ_STATE_ASCII; // per HZ spec, default to HZ mode
65 NS_IMETHODIMP nsUnicodeToHZ::ConvertNoBuff(
66 const PRUnichar * aSrc,
67 PRInt32 * aSrcLength,
68 char * aDest,
69 PRInt32 * aDestLength)
71 PRInt32 i=0;
72 PRInt32 iSrcLength = *aSrcLength;
73 PRInt32 iDestLength = 0;
75 for (i=0;i< iSrcLength;i++)
77 if(! IS_ASCII(*aSrc))
79 // hi byte has something, it is not ASCII, process as a GB
80 if ( mHZState != HZ_STATE_GB )
82 // we are adding a '~{' ESC sequence to star a HZ string
83 mHZState = HZ_STATE_GB;
84 aDest[0] = '~';
85 aDest[1] = '{';
86 aDest += 2; // increment 2 bytes
87 iDestLength +=2;
89 if(mUtil.UnicodeToGBKChar(*aSrc, PR_TRUE, &aDest[0], &aDest[1])) {
90 aDest += 2; // increment 2 bytes
91 iDestLength +=2;
92 } else {
93 // some thing that we cannot convert
94 // xxx fix me ftang
95 // error handling here
97 } else {
98 // this is an ASCII
100 // if we are in HZ mode, end it by adding a '~}' ESC sequence
101 if ( mHZState == HZ_STATE_GB )
103 mHZState = HZ_STATE_ASCII;
104 aDest[0] = '~';
105 aDest[1] = '}';
106 aDest += 2; // increment 2 bytes
107 iDestLength +=2;
110 // if this is a regular char '~' , convert it to two '~'
111 if ( *aSrc == UNICODE_TILD )
113 aDest[0] = '~';
114 aDest[1] = '~';
115 aDest += 2; // increment 2 bytes
116 iDestLength +=2;
117 } else {
118 // other regular ASCII chars convert by normal ways
120 // Is this works for both little endian and big endian machines ?
121 *aDest = (char) ( (PRUnichar)(*aSrc) );
122 aDest++; // increment 1 byte
123 iDestLength +=1;
126 aSrc++; // increment 2 bytes
127 if ( iDestLength >= (*aDestLength) )
129 break;
132 *aDestLength = iDestLength;
133 *aSrcLength = i;
134 return NS_OK;
137 NS_IMETHODIMP nsUnicodeToHZ::FinishNoBuff(char * aDest, PRInt32 * aDestLength)
139 if ( mHZState == HZ_STATE_GB )
141 // if we are in HZ mode, end it by adding a '~}' ESC sequence
142 mHZState = HZ_STATE_ASCII;
143 aDest[0] = '~';
144 aDest[1] = '}';
145 *aDestLength = 2;
146 } else {
147 *aDestLength = 0;
149 return NS_OK;
152 NS_IMETHODIMP nsUnicodeToHZ::FillInfo(PRUint32 *aInfo)
154 mUtil.FillGB2312Info(aInfo);
155 //GB2312 font lib also have single byte ASCII characters, set them here
156 for ( PRUint16 u = 0x0000; u <= 0x007F; u++)
157 SET_REPRESENTABLE(aInfo, u);
158 return NS_OK;