Debian Codegraph @

unikey/charset.h

raw
1 // -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 /*------------------------------------------------------------------------------
3 VnConv: Vietnamese Encoding Converter Library
4 UniKey Project: http://unikey.sourceforge.net
5 Copyleft (C) 1998-2002 Pham Kim Long
6 Contact: [email protected]
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 --------------------------------------------------------------------------------*/
22
23 #ifndef __CHARSET_CONVERT_H
24 #define __CHARSET_CONVERT_H
25
26 #if !defined(_WIN32)
27 #include <stdint.h>
28 #endif
29
30 #if defined(_WIN32)
31 #if defined(UNIKEYHOOK)
32 #define DllInterface __declspec( dllexport )
33 #else
34 #define DllInterface __declspec( dllimport )
35 #endif
36 #else
37 #define DllInterface //not used
38 #define DllExport
39 #define DllImport
40 #endif
41
42 #include "vnconv.h"
43 #include "byteio.h"
44 #include "pattern.h"
45
46 #define TOTAL_VNCHARS 213
47 #define TOTAL_ALPHA_VNCHARS 186
48
49 #if defined(_WIN32)
50 typedef unsigned __int32 StdVnChar;
51 typedef unsigned __int16 UnicodeChar;
52 typedef unsigned __int16 UKWORD;
53 typedef unsigned __int32 UKDWORD;
54 #else
55 //typedef unsigned int StdVnChar; //the size should be more specific
56 typedef uint32_t StdVnChar;
57 typedef uint16_t UnicodeChar;
58 typedef uint16_t UKWORD;
59 typedef uint32_t UKDWORD;
60 #endif
61
62 //typedef unsigned short UnicodeChar;
63 //typedef unsigned short UKWORD;
64
65 //typedef unsigned int UKDWORD; //the size should be more specific
66
67 #ifndef LOWORD
68 #define LOWORD(l) ((UKWORD)(l))
69 #endif
70
71 #ifndef HIWORD
72 #define HIWORD(l) ((UKWORD)(((UKDWORD)(l) >> 16) & 0xFFFF))
73 #endif
74
75 #ifndef MAKEWORD
76 #define MAKEWORD(a, b) ((UKWORD)(((UKBYTE)(a)) | ((UKWORD)((UKBYTE)(b))) << 8))
77 #endif
78
79 const StdVnChar VnStdCharOffset = 0x10000;
80 const StdVnChar INVALID_STD_CHAR = 0xFFFFFFFF;
81 //const unsigned char PadChar = '?'; //? is used for VIQR charset
82 const unsigned char PadChar = '#';
83 const unsigned char PadStartQuote = '\"';
84 const unsigned char PadEndQuote = '\"';
85 const unsigned char PadEllipsis = '.';
86
87 class DllInterface VnCharset {
88 public:
89 virtual void startInput() {};
90 virtual void startOutput() {};
91 // virtual UKBYTE *nextInput(UKBYTE *input, int inLen, StdVnChar & stdChar, int & bytesRead) = 0;
92 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) = 0;
93
94 //------------------------------------------------------------------------
95 // put a character to the output after converting it
96 // Arguments:
97 // output[in]: output buffer
98 // stdChar[in]: character in standard charset
99 // outLen[out]: length of converted sequence
100 // maxAvail[in]: max length available.
101 // Returns: next position in output
102 //------------------------------------------------------------------------
103 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) = 0;
104 virtual int elementSize();
105 virtual ~VnCharset() {}
106 };
107
108 //--------------------------------------------------
109 class SingleByteCharset: public VnCharset {
110 protected:
111 UKWORD m_stdMap[256];
112 unsigned char * m_vnChars;
113 public:
114 SingleByteCharset(unsigned char * vnChars);
115 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
116 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
117 };
118
119 //--------------------------------------------------
120 class VnInternalCharset: public VnCharset {
121 public:
122 VnInternalCharset() {};
123 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
124 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
125 virtual int elementSize();
126 };
127
128 //--------------------------------------------------
129 class UnicodeCharset: public VnCharset {
130 protected:
131 UKDWORD m_vnChars[TOTAL_VNCHARS];
132 UnicodeChar * m_toUnicode;
133 public:
134 UnicodeCharset(UnicodeChar *vnChars);
135 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
136 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
137 virtual int elementSize();
138 };
139
140 //--------------------------------------------------
141 class DoubleByteCharset: public VnCharset {
142 protected:
143 UKWORD m_stdMap[256];
144 UKDWORD m_vnChars[TOTAL_VNCHARS];
145 UKWORD * m_toDoubleChar;
146 public:
147 DoubleByteCharset(UKWORD *vnChars);
148 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
149 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
150 };
151
152 //--------------------------------------------------
153 class UnicodeUTF8Charset: public UnicodeCharset
154 {
155 public:
156 UnicodeUTF8Charset(UnicodeChar *vnChars) : UnicodeCharset(vnChars) {}
157
158 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
159 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
160 };
161
162 //--------------------------------------------------
163 class UnicodeRefCharset: public UnicodeCharset
164 {
165 public:
166 UnicodeRefCharset(UnicodeChar *vnChars) : UnicodeCharset(vnChars) {}
167
168 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
169 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
170 };
171
172 //--------------------------------------------------
173 class UnicodeHexCharset: public UnicodeRefCharset
174 {
175 public:
176 UnicodeHexCharset(UnicodeChar *vnChars) : UnicodeRefCharset(vnChars) {}
177 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
178 };
179
180 //--------------------------------------------------
181 class UnicodeCStringCharset: public UnicodeCharset
182 {
183 protected:
184 int m_prevIsHex;
185 public:
186 UnicodeCStringCharset(UnicodeChar *vnChars) : UnicodeCharset(vnChars) {}
187 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
188 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
189 virtual void startInput();
190 };
191
192 //--------------------------------------------------
193 class WinCP1258Charset: public VnCharset {
194 protected:
195 UKWORD m_stdMap[256];
196 UKDWORD m_vnChars[TOTAL_VNCHARS*2];
197 UKWORD *m_toDoubleChar;
198 int m_totalChars;
199
200 public:
201 WinCP1258Charset(UKWORD *compositeChars, UKWORD *precomposedChars);
202 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
203 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
204 };
205
206 //--------------------------------------------------
207 struct UniCompCharInfo {
208 UKDWORD compChar;
209 int stdIndex;
210 };
211
212 class UnicodeCompCharset: public VnCharset {
213 protected:
214 UniCompCharInfo m_info[TOTAL_VNCHARS*2];
215 UKDWORD *m_uniCompChars;
216 int m_totalChars;
217 public:
218 UnicodeCompCharset(UnicodeChar *uniChars, UKDWORD *uniCompChars);
219 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
220 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
221 virtual int elementSize();
222 };
223
224 //--------------------------------------------------
225 class VIQRCharset: public VnCharset {
226 protected:
227 UKDWORD *m_vnChars;
228 UKWORD m_stdMap[256];
229 int m_atWordBeginning;
230 int m_escapeBowl;
231 int m_escapeRoof;
232 int m_escapeHook;
233 int m_escapeTone;
234 int m_gotTone;
235 int m_escAll;
236 int m_noOutEsc;
237 public:
238 int m_suspicious;
239 VIQRCharset(UKDWORD *vnChars);
240 virtual void startInput();
241 virtual void startOutput();
242 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
243 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
244 };
245
246 //--------------------------------------------------
247 class UTF8VIQRCharset: public VnCharset {
248
249 protected:
250 VIQRCharset *m_pViqr;
251 UnicodeUTF8Charset *m_pUtf;
252
253 public:
254 UTF8VIQRCharset(UnicodeUTF8Charset *pUtf, VIQRCharset *pViqr);
255 virtual void startInput();
256 virtual void startOutput();
257 virtual int nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead);
258 virtual int putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen);
259 };
260
261
262 //--------------------------------------------------
263 class DllInterface CVnCharsetLib {
264 protected:
265 SingleByteCharset * m_sgCharsets[CONV_TOTAL_SINGLE_CHARSETS];
266 DoubleByteCharset * m_dbCharsets[CONV_TOTAL_DOUBLE_CHARSETS];
267 UnicodeCharset * m_pUniCharset;
268 UnicodeCompCharset * m_pUniCompCharset;
269 UnicodeUTF8Charset * m_pUniUTF8;
270 UnicodeRefCharset * m_pUniRef;
271 UnicodeHexCharset * m_pUniHex;
272 VIQRCharset * m_pVIQRCharObj;
273 UTF8VIQRCharset * m_pUVIQRCharObj;
274 WinCP1258Charset * m_pWinCP1258;
275 UnicodeCStringCharset *m_pUniCString;
276 VnInternalCharset *m_pVnIntCharset;
277
278 public:
279 PatternList m_VIQREscPatterns, m_VIQROutEscPatterns;
280 VnConvOptions m_options;
281 CVnCharsetLib();
282 ~CVnCharsetLib();
283 VnCharset * getVnCharset(int charsetIdx);
284 };
285
286 extern unsigned char SingleByteTables[][TOTAL_VNCHARS];
287 extern UKWORD DoubleByteTables[][TOTAL_VNCHARS];
288 extern UnicodeChar UnicodeTable[TOTAL_VNCHARS];
289 extern UKDWORD VIQRTable[TOTAL_VNCHARS];
290 extern UKDWORD UnicodeComposite[TOTAL_VNCHARS];
291 extern UKWORD WinCP1258[TOTAL_VNCHARS];
292 extern UKWORD WinCP1258Pre[TOTAL_VNCHARS];
293
294 extern DllInterface CVnCharsetLib VnCharsetLibObj;
295 extern VnConvOptions VnConvGlobalOptions;
296 extern int StdVnNoTone[TOTAL_VNCHARS];
297 extern int StdVnRootChar[TOTAL_VNCHARS];
298
299 DllInterface int genConvert(VnCharset & incs, VnCharset & outcs, ByteInStream & input, ByteOutStream & output);
300
301 StdVnChar StdVnToUpper(StdVnChar ch);
302 StdVnChar StdVnToLower(StdVnChar ch);
303 StdVnChar StdVnGetRoot(StdVnChar ch);
304
305 #endif