| 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 |
#include <stddef.h> |
| 24 |
#include <search.h> |
| 25 |
#include <memory.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <stdlib.h> |
| 28 |
|
| 29 |
#include "charset.h" |
| 30 |
#include "data.h" |
| 31 |
|
| 32 |
int LoVowel['z'-'a'+1]; |
| 33 |
int HiVowel['Z'-'A'+1]; |
| 34 |
|
| 35 |
#define IS_VOWEL(x) ((x >= 'a' && x <= 'z' && LoVowel[x-'a']) || (x >= 'A' && x <= 'Z' && HiVowel[x-'A'])) |
| 36 |
|
| 37 |
SingleByteCharset *SgCharsets[CONV_TOTAL_SINGLE_CHARSETS]; |
| 38 |
DoubleByteCharset *DbCharsets[CONV_TOTAL_DOUBLE_CHARSETS]; |
| 39 |
|
| 40 |
DllExport CVnCharsetLib VnCharsetLibObj; |
| 41 |
|
| 42 |
////////////////////////////////////////////////////// |
| 43 |
// Generic VnCharset class |
| 44 |
////////////////////////////////////////////////////// |
| 45 |
int VnCharset::elementSize() |
| 46 |
{ |
| 47 |
return 1; |
| 48 |
} |
| 49 |
|
| 50 |
//------------------------------------------- |
| 51 |
int VnInternalCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 52 |
{ |
| 53 |
if (!is.getNextDW(stdChar)) { |
| 54 |
bytesRead = 0; |
| 55 |
return 0; |
| 56 |
} |
| 57 |
bytesRead = sizeof(UKDWORD); |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
//------------------------------------------- |
| 62 |
int VnInternalCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 63 |
{ |
| 64 |
outLen = sizeof(StdVnChar); |
| 65 |
os.putW((UKWORD)stdChar); |
| 66 |
return os.putW((UKWORD)(stdChar>>(sizeof(UKWORD)*8))); |
| 67 |
} |
| 68 |
|
| 69 |
//------------------------------------------- |
| 70 |
int VnInternalCharset::elementSize() |
| 71 |
{ |
| 72 |
return 4; |
| 73 |
} |
| 74 |
|
| 75 |
//------------------------------------------- |
| 76 |
SingleByteCharset::SingleByteCharset(unsigned char * vnChars) |
| 77 |
{ |
| 78 |
int i; |
| 79 |
m_vnChars = vnChars; |
| 80 |
memset(m_stdMap, 0, 256*sizeof(UKWORD)); |
| 81 |
for (i=0; i<TOTAL_VNCHARS; i++) { |
| 82 |
if (vnChars[i] != 0 && (i==TOTAL_VNCHARS-1 || vnChars[i] != vnChars[i+1])) |
| 83 |
m_stdMap[vnChars[i]] = i + 1; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
//------------------------------------------- |
| 88 |
int SingleByteCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 89 |
{ |
| 90 |
unsigned char ch; |
| 91 |
if (!is.getNext(ch)) { |
| 92 |
bytesRead = 0; |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
stdChar = (m_stdMap[ch])? (VnStdCharOffset + m_stdMap[ch] - 1) : ch; |
| 97 |
bytesRead = 1; |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
//------------------------------------------- |
| 103 |
int SingleByteCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 104 |
{ |
| 105 |
int ret; |
| 106 |
unsigned char ch; |
| 107 |
if (stdChar >= VnStdCharOffset) { |
| 108 |
outLen = 1; |
| 109 |
ch = m_vnChars[stdChar - VnStdCharOffset]; |
| 110 |
if (ch == 0) |
| 111 |
ch = (stdChar == StdStartQuote)? PadStartQuote : |
| 112 |
((stdChar == StdEndQuote)? PadEndQuote : |
| 113 |
((stdChar == StdEllipsis)? PadEllipsis: PadChar) ); |
| 114 |
ret = os.putB(ch); |
| 115 |
} |
| 116 |
else { |
| 117 |
if (stdChar > 255 || m_stdMap[stdChar]) { |
| 118 |
//this character is missing in the charset |
| 119 |
// output padding character |
| 120 |
outLen = 1; |
| 121 |
ret = os.putB(PadChar); |
| 122 |
} |
| 123 |
else { |
| 124 |
outLen = 1; |
| 125 |
ret = os.putB((UKBYTE)stdChar); |
| 126 |
} |
| 127 |
} |
| 128 |
return ret; |
| 129 |
} |
| 130 |
|
| 131 |
//------------------------------------------- |
| 132 |
int wideCharCompare(const void *ele1, const void *ele2) |
| 133 |
{ |
| 134 |
UKWORD ch1 = LOWORD(*((UKDWORD *)ele1)); |
| 135 |
UKWORD ch2 = LOWORD(*((UKDWORD *)ele2)); |
| 136 |
return (ch1 == ch2)? 0 : ((ch1 > ch2)? 1 : -1); |
| 137 |
} |
| 138 |
|
| 139 |
//------------------------------------------- |
| 140 |
UnicodeCharset::UnicodeCharset(UnicodeChar *vnChars) |
| 141 |
{ |
| 142 |
UKDWORD i; |
| 143 |
m_toUnicode = vnChars; |
| 144 |
for (i=0; i<TOTAL_VNCHARS; i++) |
| 145 |
m_vnChars[i] = (i << 16) + vnChars[i]; // high word is used for index |
| 146 |
qsort(m_vnChars, TOTAL_VNCHARS, sizeof(UKDWORD), wideCharCompare); |
| 147 |
} |
| 148 |
|
| 149 |
//------------------------------------------- |
| 150 |
int UnicodeCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 151 |
{ |
| 152 |
UnicodeChar uniCh; |
| 153 |
if (!is.getNextW(uniCh)) { |
| 154 |
bytesRead = 0; |
| 155 |
return 0; |
| 156 |
} |
| 157 |
bytesRead = sizeof(UnicodeChar); |
| 158 |
UKDWORD key = uniCh; |
| 159 |
UKDWORD *pChar = (UKDWORD *)bsearch(&key, m_vnChars, TOTAL_VNCHARS, sizeof(UKDWORD), wideCharCompare); |
| 160 |
if (pChar) |
| 161 |
stdChar = VnStdCharOffset + HIWORD(*pChar); |
| 162 |
else |
| 163 |
stdChar = uniCh; |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
//------------------------------------------- |
| 168 |
int UnicodeCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 169 |
{ |
| 170 |
outLen = sizeof(UnicodeChar); |
| 171 |
return os.putW((stdChar >= VnStdCharOffset)? |
| 172 |
m_toUnicode[stdChar-VnStdCharOffset] : (UnicodeChar)stdChar); |
| 173 |
} |
| 174 |
|
| 175 |
//------------------------------------------- |
| 176 |
int UnicodeCharset::elementSize() |
| 177 |
{ |
| 178 |
return 2; |
| 179 |
} |
| 180 |
|
| 181 |
//////////////////////////////////////// |
| 182 |
// Unicode decomposed |
| 183 |
//////////////////////////////////////// |
| 184 |
//------------------------------------------- |
| 185 |
int uniCompInfoCompare(const void *ele1, const void *ele2) |
| 186 |
{ |
| 187 |
UKDWORD ch1 = ((UniCompCharInfo *)ele1)->compChar; |
| 188 |
UKDWORD ch2 = ((UniCompCharInfo *)ele2)->compChar; |
| 189 |
return (ch1 == ch2)? 0 : ((ch1 > ch2)? 1 : -1); |
| 190 |
} |
| 191 |
|
| 192 |
UnicodeCompCharset::UnicodeCompCharset(UnicodeChar *uniChars, UKDWORD *uniCompChars) |
| 193 |
{ |
| 194 |
int i,k; |
| 195 |
m_uniCompChars = uniCompChars; |
| 196 |
m_totalChars = 0; |
| 197 |
for (i=0; i<TOTAL_VNCHARS; i++) { |
| 198 |
m_info[i].compChar = uniCompChars[i]; |
| 199 |
m_info[i].stdIndex = i; |
| 200 |
m_totalChars++; |
| 201 |
} |
| 202 |
|
| 203 |
for (k=0, i=TOTAL_VNCHARS; k<TOTAL_VNCHARS; k++) |
| 204 |
if (uniChars[k] != uniCompChars[k]) { |
| 205 |
m_info[i].compChar = uniChars[k]; |
| 206 |
m_info[i].stdIndex = k; |
| 207 |
m_totalChars++; |
| 208 |
i++; |
| 209 |
} |
| 210 |
|
| 211 |
qsort(m_info, m_totalChars, sizeof(UniCompCharInfo), uniCompInfoCompare); |
| 212 |
} |
| 213 |
|
| 214 |
//--------------------------------------------- |
| 215 |
int UnicodeCompCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 216 |
{ |
| 217 |
// read first char |
| 218 |
|
| 219 |
UniCompCharInfo key; |
| 220 |
UKWORD w; |
| 221 |
if (!is.getNextW(w)) { |
| 222 |
bytesRead = 0; |
| 223 |
return 0; |
| 224 |
} |
| 225 |
key.compChar = w; |
| 226 |
bytesRead = 2; |
| 227 |
|
| 228 |
UniCompCharInfo *pInfo = (UniCompCharInfo *)bsearch(&key, m_info, m_totalChars, |
| 229 |
sizeof(UniCompCharInfo), uniCompInfoCompare); |
| 230 |
if (!pInfo) |
| 231 |
stdChar = key.compChar; |
| 232 |
else { |
| 233 |
stdChar = pInfo->stdIndex + VnStdCharOffset; |
| 234 |
if (is.peekNextW(w)) { |
| 235 |
UKDWORD hi = w; |
| 236 |
if (hi > 0) { |
| 237 |
key.compChar += hi << 16; |
| 238 |
pInfo = (UniCompCharInfo *)bsearch(&key, m_info, m_totalChars, |
| 239 |
sizeof(UniCompCharInfo), uniCompInfoCompare); |
| 240 |
if (pInfo) { |
| 241 |
stdChar = pInfo->stdIndex + VnStdCharOffset; |
| 242 |
bytesRead += 2; |
| 243 |
is.getNextW(w); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
return 1; |
| 249 |
} |
| 250 |
|
| 251 |
//--------------------------------------------- |
| 252 |
int UnicodeCompCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 253 |
{ |
| 254 |
int ret; |
| 255 |
if (stdChar >= VnStdCharOffset) { |
| 256 |
UKDWORD uniCompCh = m_uniCompChars[stdChar-VnStdCharOffset]; |
| 257 |
UKWORD lo = LOWORD(uniCompCh); |
| 258 |
UKWORD hi = HIWORD(uniCompCh); |
| 259 |
outLen = 2; |
| 260 |
ret = os.putW(lo); |
| 261 |
if (hi > 0) { |
| 262 |
outLen += 2; |
| 263 |
ret = os.putW(hi); |
| 264 |
} |
| 265 |
} |
| 266 |
else { |
| 267 |
outLen = 2; |
| 268 |
ret = os.putW((UKWORD)stdChar); |
| 269 |
} |
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
//------------------------------------------- |
| 274 |
int UnicodeCompCharset::elementSize() |
| 275 |
{ |
| 276 |
return 2; |
| 277 |
} |
| 278 |
|
| 279 |
//////////////////////////////// |
| 280 |
// Unicode UTF-8 // |
| 281 |
//////////////////////////////// |
| 282 |
int UnicodeUTF8Charset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 283 |
{ |
| 284 |
UKWORD w1, w2, w3; |
| 285 |
UKBYTE first, second, third; |
| 286 |
UnicodeChar uniCh; |
| 287 |
|
| 288 |
bytesRead = 0; |
| 289 |
if (!is.getNext(first)) |
| 290 |
return 0; |
| 291 |
bytesRead = 1; |
| 292 |
|
| 293 |
if (first < 0x80) |
| 294 |
uniCh = first; // 1-byte sequence |
| 295 |
else if ((first & 0xE0) == 0xC0) { |
| 296 |
//2-byte sequence |
| 297 |
if (!is.peekNext(second)) |
| 298 |
return 0; |
| 299 |
if ((second & 0xC0) != 0x80) { |
| 300 |
stdChar = INVALID_STD_CHAR; |
| 301 |
return 1; |
| 302 |
} |
| 303 |
is.getNext(second); |
| 304 |
bytesRead = 2; |
| 305 |
w1 = first; |
| 306 |
w2 = second; |
| 307 |
uniCh = ((w1 & 0x001F) << 6) | (w2 & 0x3F); |
| 308 |
} |
| 309 |
else if ((first & 0xF0) == 0xE0) { |
| 310 |
//3-byte sequence |
| 311 |
if (!is.peekNext(second)) |
| 312 |
return 0; |
| 313 |
if ((second & 0xC0) != 0x80) { |
| 314 |
stdChar = INVALID_STD_CHAR; |
| 315 |
return 1; |
| 316 |
} |
| 317 |
is.getNext(second); |
| 318 |
bytesRead = 2; |
| 319 |
if (!is.peekNext(third)) |
| 320 |
return 0; |
| 321 |
if ((third & 0xC0) != 0x80) { |
| 322 |
stdChar = INVALID_STD_CHAR; |
| 323 |
return 1; |
| 324 |
} |
| 325 |
is.getNext(third); |
| 326 |
bytesRead = 3; |
| 327 |
w1 = first; |
| 328 |
w2 = second; |
| 329 |
w3 = third; |
| 330 |
uniCh = ((w1 & 0x000F) << 12) | ((w2 & 0x003F) << 6) | (w3 & 0x003F); |
| 331 |
} |
| 332 |
else { |
| 333 |
stdChar = INVALID_STD_CHAR; |
| 334 |
return 1; |
| 335 |
} |
| 336 |
|
| 337 |
// translate to StdVnChar |
| 338 |
UKDWORD key = uniCh; |
| 339 |
UKDWORD *pChar = (UKDWORD *)bsearch(&key, m_vnChars, TOTAL_VNCHARS, sizeof(UKDWORD), wideCharCompare); |
| 340 |
if (pChar) |
| 341 |
stdChar = VnStdCharOffset + HIWORD(*pChar); |
| 342 |
else stdChar = uniCh; |
| 343 |
return 1; |
| 344 |
} |
| 345 |
|
| 346 |
//------------------------------------------- |
| 347 |
int UnicodeUTF8Charset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 348 |
{ |
| 349 |
UnicodeChar uChar = (stdChar < VnStdCharOffset)? |
| 350 |
(UnicodeChar)stdChar : m_toUnicode[stdChar-VnStdCharOffset]; |
| 351 |
int ret; |
| 352 |
if (uChar < 0x0080) { |
| 353 |
outLen = 1; |
| 354 |
ret = os.putB((UKBYTE)uChar); |
| 355 |
} else if (uChar < 0x0800) { |
| 356 |
outLen = 2; |
| 357 |
os.putB(0xC0 | (UKBYTE)(uChar >> 6)); |
| 358 |
ret = os.putB(0x80 | (UKBYTE)(uChar & 0x003F)); |
| 359 |
} else { |
| 360 |
outLen = 3; |
| 361 |
os.putB(0xE0 | (UKBYTE)(uChar >> 12)); |
| 362 |
os.putB(0x80 | (UKBYTE)((uChar >> 6) & 0x003F)); |
| 363 |
ret = os.putB(0x80 | (UKBYTE)(uChar & 0x003F)); |
| 364 |
} |
| 365 |
return ret; |
| 366 |
} |
| 367 |
|
| 368 |
//////////////////////////////////////// |
| 369 |
// Unicode character reference &#D; // |
| 370 |
//////////////////////////////////////// |
| 371 |
int hexDigitValue(unsigned char digit) |
| 372 |
{ |
| 373 |
if (digit >= 'a' && digit <= 'f') |
| 374 |
return digit-'a'+10; |
| 375 |
if (digit >= 'A' && digit <= 'F') |
| 376 |
return digit-'A'+10; |
| 377 |
if (digit >= '0' && digit <= '9') |
| 378 |
return digit-'0'; |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
//-------------------------------------- |
| 384 |
int UnicodeRefCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 385 |
{ |
| 386 |
unsigned char ch; |
| 387 |
UnicodeChar uniCh; |
| 388 |
bytesRead = 0; |
| 389 |
if (!is.getNext(ch)) |
| 390 |
return 0; |
| 391 |
bytesRead = 1; |
| 392 |
uniCh = ch; |
| 393 |
if (ch == '&') { |
| 394 |
if (is.peekNext(ch) && ch == '#') { |
| 395 |
is.getNext(ch); |
| 396 |
bytesRead++; |
| 397 |
if (!is.eos()) { |
| 398 |
is.peekNext(ch); |
| 399 |
if (ch != 'x' && ch != 'X') { |
| 400 |
UKWORD code = 0; |
| 401 |
int digits = 0; |
| 402 |
while (is.peekNext(ch) && isdigit(ch) && digits < 5) { |
| 403 |
is.getNext(ch); |
| 404 |
bytesRead++; |
| 405 |
code = code*10 + (ch - '0'); |
| 406 |
digits++; |
| 407 |
} |
| 408 |
if (is.peekNext(ch) && ch == ';') { |
| 409 |
is.getNext(ch); |
| 410 |
bytesRead++; |
| 411 |
uniCh = code; |
| 412 |
} |
| 413 |
} |
| 414 |
else { |
| 415 |
is.getNext(ch); |
| 416 |
bytesRead++; |
| 417 |
UKWORD code = 0; |
| 418 |
int digits = 0; |
| 419 |
while (is.peekNext(ch) && isxdigit(ch) && digits < 4) { |
| 420 |
is.getNext(ch); |
| 421 |
bytesRead++; |
| 422 |
code = (code << 4) + hexDigitValue(ch); |
| 423 |
digits++; |
| 424 |
} |
| 425 |
if (is.peekNext(ch) && ch == ';') { |
| 426 |
is.getNext(ch); |
| 427 |
bytesRead++; |
| 428 |
uniCh = code; |
| 429 |
} |
| 430 |
} // hex digits |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
// translate to StdVnChar |
| 436 |
UKDWORD key = uniCh; |
| 437 |
UKDWORD *pChar = (UKDWORD *)bsearch(&key, m_vnChars, TOTAL_VNCHARS, sizeof(UKDWORD), wideCharCompare); |
| 438 |
if (pChar) |
| 439 |
stdChar = VnStdCharOffset + HIWORD(*pChar); |
| 440 |
else stdChar = uniCh; |
| 441 |
return 1; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
//-------------------------------- |
| 446 |
int UnicodeRefCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 447 |
{ |
| 448 |
UnicodeChar uChar = (stdChar < VnStdCharOffset)? |
| 449 |
(UnicodeChar)stdChar : m_toUnicode[stdChar-VnStdCharOffset]; |
| 450 |
int ret; |
| 451 |
if (uChar < 128) { |
| 452 |
outLen = 1; |
| 453 |
ret = os.putB((UKBYTE)uChar); |
| 454 |
} |
| 455 |
else { |
| 456 |
outLen = 2; |
| 457 |
os.putB((UKBYTE)'&'); |
| 458 |
os.putB((UKBYTE)'#'); |
| 459 |
|
| 460 |
int i, digit, prev, base; |
| 461 |
prev = 0; |
| 462 |
base = 10000; |
| 463 |
for (i=0; i < 5; i++) { |
| 464 |
digit = uChar / base; |
| 465 |
if (digit || prev) { |
| 466 |
prev = 1; |
| 467 |
outLen++; |
| 468 |
os.putB('0' + (unsigned char)digit); |
| 469 |
} |
| 470 |
uChar %= base; |
| 471 |
base /= 10; |
| 472 |
} |
| 473 |
ret = os.putB((UKBYTE)';'); |
| 474 |
outLen++; |
| 475 |
} |
| 476 |
return ret; |
| 477 |
} |
| 478 |
|
| 479 |
#define HEX_DIGIT(x) ((x < 10)? ('0'+x) : ('A'+x-10)) |
| 480 |
|
| 481 |
//-------------------------------- |
| 482 |
int UnicodeHexCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 483 |
{ |
| 484 |
UnicodeChar uChar = (stdChar < VnStdCharOffset)? |
| 485 |
(UnicodeChar)stdChar : m_toUnicode[stdChar-VnStdCharOffset]; |
| 486 |
int ret; |
| 487 |
if (uChar < 256) { |
| 488 |
outLen = 1; |
| 489 |
ret = os.putB((UKBYTE)uChar); |
| 490 |
} |
| 491 |
else { |
| 492 |
outLen = 3; |
| 493 |
os.putB('&'); |
| 494 |
os.putB('#'); |
| 495 |
os.putB('x'); |
| 496 |
|
| 497 |
int i, digit; |
| 498 |
int prev = 0; |
| 499 |
int shifts = 12; |
| 500 |
|
| 501 |
for (i=0; i < 4; i++) { |
| 502 |
digit = ((uChar >> shifts) & 0x000F); |
| 503 |
if (digit > 0 || prev) { |
| 504 |
prev = 1; |
| 505 |
outLen++; |
| 506 |
os.putB((UKBYTE)HEX_DIGIT(digit)); |
| 507 |
} |
| 508 |
shifts -= 4; |
| 509 |
} |
| 510 |
ret = os.putB(';'); |
| 511 |
outLen++; |
| 512 |
} |
| 513 |
return ret; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
///////////////////////////////// |
| 518 |
// Class UnicodeCStringCharset / |
| 519 |
///////////////////////////////// |
| 520 |
void UnicodeCStringCharset::startInput() |
| 521 |
{ |
| 522 |
m_prevIsHex = 0; |
| 523 |
} |
| 524 |
|
| 525 |
//---------------------------------------- |
| 526 |
int UnicodeCStringCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 527 |
{ |
| 528 |
unsigned char ch; |
| 529 |
UnicodeChar uniCh; |
| 530 |
bytesRead = 0; |
| 531 |
if (!is.getNext(ch)) |
| 532 |
return 0; |
| 533 |
bytesRead = 1; |
| 534 |
uniCh = ch; |
| 535 |
if (ch == '\\') { |
| 536 |
if (is.peekNext(ch) && (ch=='x' || ch=='X')) { |
| 537 |
is.getNext(ch); |
| 538 |
bytesRead++; |
| 539 |
UKWORD code = 0; |
| 540 |
int digits = 0; |
| 541 |
while (is.peekNext(ch) && isxdigit(ch) && digits < 4) { |
| 542 |
is.getNext(ch); |
| 543 |
bytesRead++; |
| 544 |
code = (code << 4) + hexDigitValue(ch); |
| 545 |
digits++; |
| 546 |
} |
| 547 |
uniCh = code; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
// translate to StdVnChar |
| 552 |
UKDWORD key = uniCh; |
| 553 |
UKDWORD *pChar = (UKDWORD *)bsearch(&key, m_vnChars, TOTAL_VNCHARS, sizeof(UKDWORD), wideCharCompare); |
| 554 |
if (pChar) |
| 555 |
stdChar = VnStdCharOffset + HIWORD(*pChar); |
| 556 |
else stdChar = uniCh; |
| 557 |
return 1; |
| 558 |
} |
| 559 |
|
| 560 |
//------------------------------------ |
| 561 |
int UnicodeCStringCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 562 |
{ |
| 563 |
UnicodeChar uChar = (stdChar < VnStdCharOffset)? |
| 564 |
(UnicodeChar)stdChar : m_toUnicode[stdChar-VnStdCharOffset]; |
| 565 |
int ret; |
| 566 |
if (uChar < 128 && !isxdigit(uChar) && uChar != 'x' && uChar != 'X') { |
| 567 |
outLen = 1; |
| 568 |
ret = os.putB((UKBYTE)uChar); |
| 569 |
} |
| 570 |
else { |
| 571 |
outLen = 2; |
| 572 |
os.putB('\\'); |
| 573 |
os.putB('x'); |
| 574 |
|
| 575 |
int i, digit; |
| 576 |
int prev = 0; |
| 577 |
int shifts = 12; |
| 578 |
|
| 579 |
for (i=0; i < 4; i++) { |
| 580 |
digit = ((uChar >> shifts) & 0x000F); |
| 581 |
if (digit > 0 || prev) { |
| 582 |
prev = 1; |
| 583 |
outLen++; |
| 584 |
os.putB((UKBYTE)HEX_DIGIT(digit)); |
| 585 |
} |
| 586 |
shifts -= 4; |
| 587 |
} |
| 588 |
ret = os.isOK(); |
| 589 |
m_prevIsHex = 1; |
| 590 |
} |
| 591 |
return ret; |
| 592 |
} |
| 593 |
|
| 594 |
///////////////////////////////// |
| 595 |
// Double-byte charsets // |
| 596 |
///////////////////////////////// |
| 597 |
DoubleByteCharset::DoubleByteCharset(UKWORD *vnChars) |
| 598 |
{ |
| 599 |
m_toDoubleChar = vnChars; |
| 600 |
memset(m_stdMap, 0, 256*sizeof(UKWORD)); |
| 601 |
for (int i=0; i<TOTAL_VNCHARS; i++) { |
| 602 |
if (vnChars[i] >> 8) // a 2-byte character |
| 603 |
m_stdMap[vnChars[i] >> 8] = 0xFFFF; //INVALID_STD_CHAR; |
| 604 |
else if (m_stdMap[vnChars[i]] == 0) |
| 605 |
m_stdMap[vnChars[i]] = i+1; |
| 606 |
m_vnChars[i] = (i << 16) + vnChars[i]; // high word is used for StdChar index |
| 607 |
} |
| 608 |
qsort(m_vnChars, TOTAL_VNCHARS, sizeof(UKDWORD), wideCharCompare); |
| 609 |
} |
| 610 |
|
| 611 |
//--------------------------------------------- |
| 612 |
int DoubleByteCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 613 |
{ |
| 614 |
unsigned char ch; |
| 615 |
|
| 616 |
// read first byte |
| 617 |
bytesRead = 0; |
| 618 |
if (!is.getNext(ch)) |
| 619 |
return 0; |
| 620 |
bytesRead = 1; |
| 621 |
stdChar = m_stdMap[ch]; |
| 622 |
if (stdChar == 0) |
| 623 |
stdChar = ch; |
| 624 |
else if (stdChar == 0xFFFF) |
| 625 |
stdChar = INVALID_STD_CHAR; |
| 626 |
else { |
| 627 |
stdChar += VnStdCharOffset - 1; |
| 628 |
UKBYTE hi; |
| 629 |
if (is.peekNext(hi) && hi > 0) { |
| 630 |
//test if a double-byte character is encountered |
| 631 |
UKDWORD key = MAKEWORD(ch,hi); |
| 632 |
UKDWORD *pChar = (UKDWORD *)bsearch(&key, m_vnChars, TOTAL_VNCHARS, sizeof(UKDWORD), wideCharCompare); |
| 633 |
if (pChar) { |
| 634 |
stdChar = VnStdCharOffset + HIWORD(*pChar); |
| 635 |
bytesRead = 2; |
| 636 |
is.getNext(hi); |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
return 1; |
| 641 |
} |
| 642 |
|
| 643 |
//--------------------------------------------- |
| 644 |
int DoubleByteCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 645 |
{ |
| 646 |
int ret; |
| 647 |
if (stdChar >= VnStdCharOffset) { |
| 648 |
UKWORD wCh = m_toDoubleChar[stdChar-VnStdCharOffset]; |
| 649 |
|
| 650 |
if (wCh & 0xFF00) { |
| 651 |
outLen = 2; |
| 652 |
os.putB((UKBYTE)(wCh & 0x00FF)); |
| 653 |
ret = os.putB((UKBYTE)(wCh >> 8)); |
| 654 |
} |
| 655 |
else { |
| 656 |
unsigned char b = (unsigned char)wCh; |
| 657 |
if (m_stdMap[b] == 0xFFFF) |
| 658 |
b = PadChar; |
| 659 |
outLen = 1; |
| 660 |
ret = os.putB(b); |
| 661 |
} |
| 662 |
/* |
| 663 |
outLen = 1; |
| 664 |
ret = os.putB((UKBYTE)(wCh & 0x00FF)); |
| 665 |
if (wCh & 0xFF00) { |
| 666 |
outLen = 2; |
| 667 |
ret = os.putB((UKBYTE)(wCh >> 8)); |
| 668 |
} |
| 669 |
*/ |
| 670 |
} |
| 671 |
else { |
| 672 |
if (stdChar > 255 || m_stdMap[stdChar]) { |
| 673 |
outLen = 1; |
| 674 |
ret = os.putB((UKBYTE)PadChar); |
| 675 |
} |
| 676 |
else { |
| 677 |
outLen = 1; |
| 678 |
ret = os.putB((UKBYTE)stdChar); |
| 679 |
} |
| 680 |
} |
| 681 |
return ret; |
| 682 |
} |
| 683 |
|
| 684 |
///////////////////////////////////////////// |
| 685 |
// Class: VIQRCharset // |
| 686 |
///////////////////////////////////////////// |
| 687 |
|
| 688 |
unsigned char VIQRTones[] = {'\'','`','?','~','.'}; |
| 689 |
|
| 690 |
const char *VIQREscapes[] = { |
| 691 |
"://", |
| 692 |
"/", |
| 693 |
"@", |
| 694 |
"mailto:", |
| 695 |
"email:", |
| 696 |
"news:", |
| 697 |
"www", |
| 698 |
"ftp" |
| 699 |
}; |
| 700 |
|
| 701 |
const int VIQREscCount = sizeof(VIQREscapes) / sizeof(char*); |
| 702 |
|
| 703 |
VIQRCharset::VIQRCharset(UKDWORD *vnChars) |
| 704 |
{ |
| 705 |
memset(m_stdMap, 0, 256*sizeof(UKWORD)); |
| 706 |
int i; |
| 707 |
UKDWORD dw; |
| 708 |
m_vnChars = vnChars; |
| 709 |
for (i=0; i<TOTAL_VNCHARS; i++) { |
| 710 |
dw = m_vnChars[i]; |
| 711 |
if (!(dw & 0xffffff00)) { //single byte |
| 712 |
//ch = (unsigned char)(dw & 0xff); |
| 713 |
m_stdMap[dw] = i+256; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
// set offset from base characters according to tone marks |
| 718 |
m_stdMap[(unsigned char)'\''] = 2; |
| 719 |
m_stdMap[(unsigned char)'`'] = 4; |
| 720 |
m_stdMap[(unsigned char)'?'] = 6; |
| 721 |
m_stdMap[(unsigned char)'~'] = 8; |
| 722 |
m_stdMap[(unsigned char)'.'] = 10; |
| 723 |
m_stdMap[(unsigned char)'^'] = 12; |
| 724 |
|
| 725 |
m_stdMap[(unsigned char)'('] = 24; |
| 726 |
m_stdMap[(unsigned char)'+'] = 26; |
| 727 |
m_stdMap[(unsigned char)'*'] = 26; |
| 728 |
} |
| 729 |
|
| 730 |
//--------------------------------------------------- |
| 731 |
void VIQRCharset::startInput() |
| 732 |
{ |
| 733 |
m_suspicious = 0; |
| 734 |
m_atWordBeginning = 1; |
| 735 |
m_gotTone = 0; |
| 736 |
m_escAll = 0; |
| 737 |
if (VnCharsetLibObj.m_options.viqrEsc) |
| 738 |
VnCharsetLibObj.m_VIQREscPatterns.reset(); |
| 739 |
} |
| 740 |
|
| 741 |
//--------------------------------------------------- |
| 742 |
int VIQRCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 743 |
{ |
| 744 |
unsigned char ch1; |
| 745 |
bytesRead = 0; |
| 746 |
|
| 747 |
if (!is.getNext(ch1)) |
| 748 |
return 0; |
| 749 |
bytesRead = 1; |
| 750 |
stdChar = m_stdMap[ch1]; |
| 751 |
|
| 752 |
if (VnCharsetLibObj.m_options.viqrEsc) { |
| 753 |
if (VnCharsetLibObj.m_VIQREscPatterns.foundAtNextChar(ch1)!=-1) { |
| 754 |
m_escAll = 1; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
if (m_escAll && (ch1==' ' || ch1=='\t' || ch1=='\r' || ch1=='\n')) |
| 759 |
m_escAll = 0; |
| 760 |
|
| 761 |
if (ch1 == '\\') { |
| 762 |
// ecape character , try to read next |
| 763 |
if (!is.getNext(ch1)) { |
| 764 |
bytesRead++; |
| 765 |
stdChar = m_stdMap[ch1]; |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
if (stdChar < 256) { |
| 770 |
stdChar = ch1; |
| 771 |
} |
| 772 |
else if (!m_escAll && !is.eos()) { |
| 773 |
// try to read the next byte |
| 774 |
unsigned char ch2; |
| 775 |
is.peekNext(ch2); |
| 776 |
unsigned char upper = toupper(ch1); |
| 777 |
if ((!VnCharsetLibObj.m_options.smartViqr || m_atWordBeginning) && |
| 778 |
upper == 'D' && (ch2 == 'd' || ch2 == 'D')) |
| 779 |
{ |
| 780 |
is.getNext(ch2); |
| 781 |
bytesRead++; |
| 782 |
stdChar += 2; // dd is 2 positions after d. |
| 783 |
} |
| 784 |
else { |
| 785 |
StdVnChar index = m_stdMap[ch2]; |
| 786 |
|
| 787 |
int cond; |
| 788 |
if (m_suspicious) { |
| 789 |
cond = IS_VOWEL(ch1) && |
| 790 |
( index == 2 || index == 4 || index == 8 || //not accepting ? . in suspicious mode |
| 791 |
(index == 12 && (upper == 'A' || upper == 'E' || upper == 'O')) || |
| 792 |
(m_stdMap[ch2] == 24 && upper== 'A') || |
| 793 |
(m_stdMap[ch2] == 26 && (upper == 'O' || upper == 'U')) ); |
| 794 |
if (cond) |
| 795 |
m_suspicious = 0; |
| 796 |
} |
| 797 |
else |
| 798 |
cond = IS_VOWEL(ch1) && |
| 799 |
((index <= 10 && index > 0 && (!m_gotTone || (index!=6 && index!=10)) ) || |
| 800 |
(index == 12 && (upper == 'A' || upper == 'E' || upper == 'O')) || |
| 801 |
(m_stdMap[ch2] == 24 && upper== 'A') || |
| 802 |
(m_stdMap[ch2] == 26 && (upper == 'O' || upper == 'U')) ); |
| 803 |
|
| 804 |
if (cond) { |
| 805 |
if (index > 0) |
| 806 |
m_gotTone = 1; //we have a tone/breve/hook in the current word |
| 807 |
|
| 808 |
// ok, take this byte |
| 809 |
is.getNext(ch2); |
| 810 |
bytesRead++; |
| 811 |
int offset = m_stdMap[ch2]; |
| 812 |
if (offset == 26) offset = 24; |
| 813 |
if (offset == 24 && (ch1 == 'u' || ch1 == 'U')) |
| 814 |
offset = 12; |
| 815 |
stdChar += offset; |
| 816 |
// check next byte |
| 817 |
if (is.peekNext(ch2)) { |
| 818 |
if (index > 10 && m_stdMap[ch2] > 0 && m_stdMap[ch2] <= 10) { |
| 819 |
// ok, take one more byte |
| 820 |
is.getNext(ch2); |
| 821 |
bytesRead++; |
| 822 |
stdChar += m_stdMap[ch2]; |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
m_atWordBeginning = (stdChar < 256); |
| 829 |
if (stdChar < 256) { |
| 830 |
m_gotTone = 0; //reset this flag because we are at the beginning of a new word |
| 831 |
} |
| 832 |
|
| 833 |
// adjust stdChar |
| 834 |
if (stdChar >= 256) |
| 835 |
stdChar += VnStdCharOffset - 256; |
| 836 |
return 1; |
| 837 |
} |
| 838 |
|
| 839 |
//--------------------------------------------------- |
| 840 |
void VIQRCharset::startOutput() |
| 841 |
{ |
| 842 |
m_escapeBowl = 0; |
| 843 |
m_escapeRoof = 0; |
| 844 |
m_escapeHook = 0; |
| 845 |
m_escapeTone = 0; |
| 846 |
m_noOutEsc = 0; |
| 847 |
VnCharsetLibObj.m_VIQROutEscPatterns.reset(); |
| 848 |
} |
| 849 |
|
| 850 |
//--------------------------------------------------- |
| 851 |
int VIQRCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 852 |
{ |
| 853 |
int ret; |
| 854 |
UKBYTE b; |
| 855 |
if (stdChar >= VnStdCharOffset) { |
| 856 |
outLen = 1; |
| 857 |
UKDWORD dw = m_vnChars[stdChar-VnStdCharOffset]; |
| 858 |
|
| 859 |
unsigned char first = (unsigned char)dw; |
| 860 |
unsigned char firstUpper = toupper(first); |
| 861 |
|
| 862 |
b = (UKBYTE)dw; |
| 863 |
ret = os.putB(b); |
| 864 |
if (VnCharsetLibObj.m_VIQROutEscPatterns.foundAtNextChar(b) != -1) |
| 865 |
m_noOutEsc = 1; |
| 866 |
|
| 867 |
if (m_noOutEsc && (b==' ' || b=='\t' || b=='\r' || b=='\n')) |
| 868 |
m_noOutEsc = 0; |
| 869 |
|
| 870 |
if (dw & 0x0000FF00) { |
| 871 |
// second byte is present |
| 872 |
unsigned char second = (UKBYTE)(dw >> 8); |
| 873 |
outLen++; |
| 874 |
ret = os.putB(second); |
| 875 |
|
| 876 |
if (dw & 0x00FF0000) { |
| 877 |
//third byte is present |
| 878 |
outLen++; |
| 879 |
ret = os.putB((UKBYTE)(dw >> 16)); |
| 880 |
m_escapeTone = 0; |
| 881 |
} |
| 882 |
else { |
| 883 |
UKWORD index = m_stdMap[second]; |
| 884 |
m_escapeTone = (index == 12 || index == 24 || index == 26); |
| 885 |
} |
| 886 |
|
| 887 |
VnCharsetLibObj.m_VIQROutEscPatterns.reset(); |
| 888 |
|
| 889 |
m_escapeBowl = 0; |
| 890 |
m_escapeHook = 0; |
| 891 |
m_escapeRoof = 0; |
| 892 |
} |
| 893 |
else { |
| 894 |
m_escapeTone = IS_VOWEL(first); |
| 895 |
m_escapeBowl = (firstUpper == 'A'); |
| 896 |
m_escapeHook = (firstUpper == 'U' || firstUpper == 'O'); |
| 897 |
m_escapeRoof = (firstUpper == 'A' || firstUpper == 'E' || firstUpper == 'O'); |
| 898 |
} |
| 899 |
} |
| 900 |
else { |
| 901 |
if (stdChar > 255) { |
| 902 |
outLen = 1; |
| 903 |
ret = os.putB((UKBYTE)PadChar); |
| 904 |
if (VnCharsetLibObj.m_VIQROutEscPatterns.foundAtNextChar((UKBYTE)PadChar) != -1) |
| 905 |
m_noOutEsc = 1; |
| 906 |
} |
| 907 |
else { |
| 908 |
outLen = 1; |
| 909 |
UKWORD index = m_stdMap[stdChar]; |
| 910 |
if (!VnCharsetLibObj.m_options.viqrMixed && !m_noOutEsc && |
| 911 |
(stdChar=='\\' || |
| 912 |
(index > 0 && index <= 10 && m_escapeTone) || |
| 913 |
(index == 12 && m_escapeRoof) || |
| 914 |
(index == 24 && m_escapeBowl) || |
| 915 |
(index == 26 && m_escapeHook))) { |
| 916 |
//(m_stdMap[stdChar] > 0 && m_stdMap[stdChar] <= 26)) { |
| 917 |
// tone mark, needs an escape character |
| 918 |
outLen++; |
| 919 |
ret = os.putB('\\'); |
| 920 |
if (VnCharsetLibObj.m_VIQROutEscPatterns.foundAtNextChar('\\') != -1) |
| 921 |
m_noOutEsc = 1; |
| 922 |
} |
| 923 |
b = (UKBYTE)stdChar; |
| 924 |
ret = os.putB(b); |
| 925 |
if (VnCharsetLibObj.m_VIQROutEscPatterns.foundAtNextChar(b) != -1) |
| 926 |
m_noOutEsc = 1; |
| 927 |
if (m_noOutEsc && (b==' ' || b=='\t' || b=='\r' || b=='\n')) |
| 928 |
m_noOutEsc = 0; |
| 929 |
} |
| 930 |
// reset escape marks |
| 931 |
m_escapeBowl = 0; |
| 932 |
m_escapeRoof = 0; |
| 933 |
m_escapeHook = 0; |
| 934 |
m_escapeTone = 0; |
| 935 |
} |
| 936 |
return ret; |
| 937 |
} |
| 938 |
|
| 939 |
///////////////////////////////////////////// |
| 940 |
// Class: UTF8VIQRCharset // |
| 941 |
///////////////////////////////////////////// |
| 942 |
|
| 943 |
//----------------------------------------- |
| 944 |
UTF8VIQRCharset::UTF8VIQRCharset(UnicodeUTF8Charset *pUtf, VIQRCharset *pViqr) |
| 945 |
{ |
| 946 |
m_pUtf = pUtf; |
| 947 |
m_pViqr = pViqr; |
| 948 |
} |
| 949 |
|
| 950 |
//----------------------------------------- |
| 951 |
void UTF8VIQRCharset::startInput() |
| 952 |
{ |
| 953 |
m_pUtf->startInput(); |
| 954 |
m_pViqr->startInput(); |
| 955 |
} |
| 956 |
|
| 957 |
//----------------------------------------- |
| 958 |
void UTF8VIQRCharset::startOutput() |
| 959 |
{ |
| 960 |
m_pUtf->startOutput(); |
| 961 |
m_pViqr->startOutput(); |
| 962 |
} |
| 963 |
|
| 964 |
//----------------------------------------- |
| 965 |
int UTF8VIQRCharset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 966 |
{ |
| 967 |
UKBYTE ch; |
| 968 |
|
| 969 |
if (!is.peekNext(ch)) |
| 970 |
return 0; |
| 971 |
|
| 972 |
if (ch > 0xBF && ch < 0xFE) { |
| 973 |
m_pViqr->startInput(); // just to reset the VIQR object state |
| 974 |
m_pViqr->m_suspicious = 1; |
| 975 |
return m_pUtf->nextInput(is, stdChar, bytesRead); |
| 976 |
} |
| 977 |
|
| 978 |
return m_pViqr->nextInput(is, stdChar, bytesRead); |
| 979 |
} |
| 980 |
|
| 981 |
//----------------------------------------- |
| 982 |
int UTF8VIQRCharset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 983 |
{ |
| 984 |
return m_pViqr->putChar(os, stdChar, outLen); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
//----------------------------------------- |
| 989 |
CVnCharsetLib::CVnCharsetLib() |
| 990 |
{ |
| 991 |
unsigned char ch; |
| 992 |
for (ch = 'a'; ch < 'z'; ch++) |
| 993 |
LoVowel[ch-'a'] = 0; |
| 994 |
LoVowel['a'-'a'] = 1; |
| 995 |
LoVowel['e'-'a'] = 1; |
| 996 |
LoVowel['i'-'a'] = 1; |
| 997 |
LoVowel['o'-'a'] = 1; |
| 998 |
LoVowel['u'-'a'] = 1; |
| 999 |
LoVowel['y'-'a'] = 1; |
| 1000 |
|
| 1001 |
for (ch = 'A'; ch < 'Z'; ch++) |
| 1002 |
HiVowel[ch-'A'] = 0; |
| 1003 |
HiVowel['A'-'A'] = 1; |
| 1004 |
HiVowel['E'-'A'] = 1; |
| 1005 |
HiVowel['I'-'A'] = 1; |
| 1006 |
HiVowel['O'-'A'] = 1; |
| 1007 |
HiVowel['U'-'A'] = 1; |
| 1008 |
HiVowel['Y'-'A'] = 1; |
| 1009 |
|
| 1010 |
m_pUniCharset = NULL; |
| 1011 |
m_pUniCompCharset = NULL; |
| 1012 |
m_pUniUTF8 = NULL; |
| 1013 |
m_pUniRef = NULL; |
| 1014 |
m_pUniHex = NULL; |
| 1015 |
m_pVIQRCharObj = NULL; |
| 1016 |
m_pUVIQRCharObj = NULL; |
| 1017 |
m_pWinCP1258 = NULL; |
| 1018 |
m_pVnIntCharset = NULL; |
| 1019 |
|
| 1020 |
int i; |
| 1021 |
for (i = 0; i < CONV_TOTAL_SINGLE_CHARSETS; i++) |
| 1022 |
m_sgCharsets[i] = NULL; |
| 1023 |
|
| 1024 |
for (i = 0; i < CONV_TOTAL_DOUBLE_CHARSETS; i++) |
| 1025 |
m_dbCharsets[i] = NULL; |
| 1026 |
|
| 1027 |
VnConvResetOptions(&m_options); |
| 1028 |
m_VIQREscPatterns.init((char**)VIQREscapes, VIQREscCount); |
| 1029 |
m_VIQROutEscPatterns.init((char**)VIQREscapes, VIQREscCount); |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
//----------------------------------------- |
| 1034 |
CVnCharsetLib::~CVnCharsetLib() |
| 1035 |
{ |
| 1036 |
if (m_pUniCharset) |
| 1037 |
delete m_pUniCharset; |
| 1038 |
if (m_pUniUTF8) |
| 1039 |
delete m_pUniUTF8; |
| 1040 |
if (m_pUniRef) |
| 1041 |
delete m_pUniRef; |
| 1042 |
if (m_pUniHex) |
| 1043 |
delete m_pUniHex; |
| 1044 |
if (m_pVIQRCharObj) |
| 1045 |
delete m_pVIQRCharObj; |
| 1046 |
if (m_pUVIQRCharObj) |
| 1047 |
delete m_pUVIQRCharObj; |
| 1048 |
if (m_pWinCP1258) |
| 1049 |
delete m_pWinCP1258; |
| 1050 |
if (m_pUniCString) |
| 1051 |
delete m_pUniCString; |
| 1052 |
if (m_pVnIntCharset) |
| 1053 |
delete m_pVnIntCharset; |
| 1054 |
|
| 1055 |
int i; |
| 1056 |
for (i = 0; i < CONV_TOTAL_SINGLE_CHARSETS; i++) |
| 1057 |
if (m_sgCharsets[i]) delete m_sgCharsets[i]; |
| 1058 |
|
| 1059 |
for (i = 0; i < CONV_TOTAL_DOUBLE_CHARSETS; i++) |
| 1060 |
if (m_dbCharsets[i]) delete m_dbCharsets[i]; |
| 1061 |
|
| 1062 |
} |
| 1063 |
|
| 1064 |
//----------------------------------------- |
| 1065 |
VnCharset * CVnCharsetLib::getVnCharset(int charsetIdx) |
| 1066 |
{ |
| 1067 |
switch (charsetIdx) { |
| 1068 |
|
| 1069 |
case CONV_CHARSET_UNICODE: |
| 1070 |
if (m_pUniCharset == NULL) |
| 1071 |
m_pUniCharset = new UnicodeCharset(UnicodeTable); |
| 1072 |
return m_pUniCharset; |
| 1073 |
case CONV_CHARSET_UNIDECOMPOSED: |
| 1074 |
if (m_pUniCompCharset == NULL) |
| 1075 |
m_pUniCompCharset = new UnicodeCompCharset(UnicodeTable, UnicodeComposite); |
| 1076 |
return m_pUniCompCharset; |
| 1077 |
case CONV_CHARSET_UNIUTF8: |
| 1078 |
case CONV_CHARSET_XUTF8: |
| 1079 |
if (m_pUniUTF8 == NULL) |
| 1080 |
m_pUniUTF8 = new UnicodeUTF8Charset(UnicodeTable); |
| 1081 |
return m_pUniUTF8; |
| 1082 |
|
| 1083 |
case CONV_CHARSET_UNIREF: |
| 1084 |
if (m_pUniRef == NULL) |
| 1085 |
m_pUniRef = new UnicodeRefCharset(UnicodeTable); |
| 1086 |
return m_pUniRef; |
| 1087 |
|
| 1088 |
case CONV_CHARSET_UNIREF_HEX: |
| 1089 |
if (m_pUniHex == NULL) |
| 1090 |
m_pUniHex = new UnicodeHexCharset(UnicodeTable); |
| 1091 |
return m_pUniHex; |
| 1092 |
|
| 1093 |
case CONV_CHARSET_UNI_CSTRING: |
| 1094 |
if (m_pUniCString == NULL) |
| 1095 |
m_pUniCString = new UnicodeCStringCharset(UnicodeTable); |
| 1096 |
return m_pUniCString; |
| 1097 |
|
| 1098 |
case CONV_CHARSET_WINCP1258: |
| 1099 |
if (m_pWinCP1258 == NULL) |
| 1100 |
m_pWinCP1258 = new WinCP1258Charset(WinCP1258, WinCP1258Pre); |
| 1101 |
return m_pWinCP1258; |
| 1102 |
|
| 1103 |
case CONV_CHARSET_VIQR: |
| 1104 |
if (m_pVIQRCharObj == NULL) |
| 1105 |
m_pVIQRCharObj = new VIQRCharset(VIQRTable); |
| 1106 |
return m_pVIQRCharObj; |
| 1107 |
|
| 1108 |
case CONV_CHARSET_VNSTANDARD: |
| 1109 |
if (m_pVnIntCharset == NULL) |
| 1110 |
m_pVnIntCharset = new VnInternalCharset(); |
| 1111 |
return m_pVnIntCharset; |
| 1112 |
|
| 1113 |
case CONV_CHARSET_UTF8VIQR: |
| 1114 |
if (m_pUVIQRCharObj == NULL) { |
| 1115 |
if (m_pVIQRCharObj == NULL) |
| 1116 |
m_pVIQRCharObj = new VIQRCharset(VIQRTable); |
| 1117 |
|
| 1118 |
if (m_pUniUTF8 == NULL) |
| 1119 |
m_pUniUTF8 = new UnicodeUTF8Charset(UnicodeTable); |
| 1120 |
m_pUVIQRCharObj = new UTF8VIQRCharset(m_pUniUTF8, m_pVIQRCharObj); |
| 1121 |
} |
| 1122 |
return m_pUVIQRCharObj; |
| 1123 |
|
| 1124 |
default: |
| 1125 |
if (IS_SINGLE_BYTE_CHARSET(charsetIdx)) { |
| 1126 |
int i = charsetIdx - CONV_CHARSET_TCVN3; |
| 1127 |
if (m_sgCharsets[i] == NULL) |
| 1128 |
m_sgCharsets[i] = new SingleByteCharset(SingleByteTables[i]); |
| 1129 |
return m_sgCharsets[i]; |
| 1130 |
} |
| 1131 |
else if (IS_DOUBLE_BYTE_CHARSET(charsetIdx)) { |
| 1132 |
int i = charsetIdx - CONV_CHARSET_VNIWIN; |
| 1133 |
if (m_dbCharsets[i] == NULL) |
| 1134 |
m_dbCharsets[i] = new DoubleByteCharset(DoubleByteTables[i]); |
| 1135 |
return m_dbCharsets[i]; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
return NULL; |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
//------------------------------------------------- |
| 1143 |
DllExport void VnConvSetOptions(VnConvOptions *pOptions) |
| 1144 |
{ |
| 1145 |
VnCharsetLibObj.m_options = *pOptions; |
| 1146 |
} |
| 1147 |
|
| 1148 |
//------------------------------------------------- |
| 1149 |
DllExport void VnConvGetOptions(VnConvOptions *pOptions) |
| 1150 |
{ |
| 1151 |
*pOptions = VnCharsetLibObj.m_options; |
| 1152 |
} |
| 1153 |
|
| 1154 |
//------------------------------------------------- |
| 1155 |
DllExport void VnConvResetOptions(VnConvOptions *pOptions) |
| 1156 |
{ |
| 1157 |
pOptions->viqrEsc = 1; |
| 1158 |
pOptions->viqrMixed = 0; |
| 1159 |
pOptions->toUpper = 0; |
| 1160 |
pOptions->toLower = 0; |
| 1161 |
pOptions->removeTone = 0; |
| 1162 |
pOptions->smartViqr = 1; |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
///////////////////////////////////////////// |
| 1167 |
// Class WinCP1258Charset |
| 1168 |
///////////////////////////////////////////// |
| 1169 |
WinCP1258Charset::WinCP1258Charset(UKWORD *compositeChars, UKWORD *precomposedChars) |
| 1170 |
{ |
| 1171 |
int i,k; |
| 1172 |
m_toDoubleChar = compositeChars; |
| 1173 |
memset(m_stdMap, 0, 256*sizeof(UKWORD)); |
| 1174 |
|
| 1175 |
// encode composite chars |
| 1176 |
for (i=0; i<TOTAL_VNCHARS; i++) { |
| 1177 |
if (compositeChars[i] >> 8) // a 2-byte character |
| 1178 |
m_stdMap[compositeChars[i] >> 8] = 0xFFFF; //INVALID_STD_CHAR; |
| 1179 |
else if (m_stdMap[compositeChars[i]] == 0) |
| 1180 |
m_stdMap[compositeChars[i]] = i+1; |
| 1181 |
|
| 1182 |
m_vnChars[i] = (i << 16) + compositeChars[i]; // high word is used for StdChar index |
| 1183 |
} |
| 1184 |
|
| 1185 |
m_totalChars = TOTAL_VNCHARS; |
| 1186 |
|
| 1187 |
//add precomposed chars to the table |
| 1188 |
for (k=0, i=TOTAL_VNCHARS; k<TOTAL_VNCHARS; k++) |
| 1189 |
if (precomposedChars[k] != compositeChars[k]) { |
| 1190 |
if (precomposedChars[k] >> 8) // a 2-byte character |
| 1191 |
m_stdMap[precomposedChars[k] >> 8] = 0xFFFF; //INVALID_STD_CHAR; |
| 1192 |
else if (m_stdMap[precomposedChars[k]] == 0) |
| 1193 |
m_stdMap[precomposedChars[k]] = k+1; |
| 1194 |
|
| 1195 |
m_vnChars[i] = (k << 16) + precomposedChars[k]; |
| 1196 |
m_totalChars++; |
| 1197 |
i++; |
| 1198 |
} |
| 1199 |
|
| 1200 |
qsort(m_vnChars, m_totalChars, sizeof(UKDWORD), wideCharCompare); |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
//--------------------------------------------------------------------- |
| 1205 |
// This fuction is basically the same as that of DoubleByteCharset |
| 1206 |
// with m_totalChars is used instead of constant TOTAL_VNCHARS |
| 1207 |
//--------------------------------------------------------------------- |
| 1208 |
int WinCP1258Charset::nextInput(ByteInStream & is, StdVnChar & stdChar, int & bytesRead) |
| 1209 |
{ |
| 1210 |
unsigned char ch; |
| 1211 |
|
| 1212 |
// read first byte |
| 1213 |
bytesRead = 0; |
| 1214 |
if (!is.getNext(ch)) |
| 1215 |
return 0; |
| 1216 |
bytesRead = 1; |
| 1217 |
stdChar = m_stdMap[ch]; |
| 1218 |
if (stdChar == 0) |
| 1219 |
stdChar = ch; |
| 1220 |
else if (stdChar == 0xFFFF) |
| 1221 |
stdChar = INVALID_STD_CHAR; |
| 1222 |
else { |
| 1223 |
stdChar += VnStdCharOffset - 1; |
| 1224 |
UKBYTE hi; |
| 1225 |
if (is.peekNext(hi) && hi > 0) { |
| 1226 |
//test if a double-byte character is encountered |
| 1227 |
UKDWORD key = MAKEWORD(ch,hi); |
| 1228 |
UKDWORD *pChar = (UKDWORD *)bsearch(&key, m_vnChars, m_totalChars, sizeof(UKDWORD), wideCharCompare); |
| 1229 |
if (pChar) { |
| 1230 |
stdChar = VnStdCharOffset + HIWORD(*pChar); |
| 1231 |
bytesRead = 2; |
| 1232 |
is.getNext(hi); |
| 1233 |
} |
| 1234 |
} |
| 1235 |
} |
| 1236 |
return 1; |
| 1237 |
} |
| 1238 |
|
| 1239 |
//--------------------------------------------------------------------- |
| 1240 |
// This fuction is exactly the same as that of DoubleByteCharset |
| 1241 |
//--------------------------------------------------------------------- |
| 1242 |
int WinCP1258Charset::putChar(ByteOutStream & os, StdVnChar stdChar, int & outLen) |
| 1243 |
{ |
| 1244 |
int ret; |
| 1245 |
if (stdChar >= VnStdCharOffset) { |
| 1246 |
UKWORD wCh = m_toDoubleChar[stdChar-VnStdCharOffset]; |
| 1247 |
|
| 1248 |
if (wCh & 0xFF00) { |
| 1249 |
outLen = 2; |
| 1250 |
os.putB((UKBYTE)(wCh & 0x00FF)); |
| 1251 |
ret = os.putB((UKBYTE)(wCh >> 8)); |
| 1252 |
} |
| 1253 |
else { |
| 1254 |
unsigned char b = (unsigned char)wCh; |
| 1255 |
if (m_stdMap[b] == 0xFFFF) |
| 1256 |
b = PadChar; |
| 1257 |
outLen = 1; |
| 1258 |
ret = os.putB(b); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
else { |
| 1262 |
if (stdChar > 255 || m_stdMap[stdChar]) { |
| 1263 |
outLen = 1; |
| 1264 |
ret = os.putB((UKBYTE)PadChar); |
| 1265 |
} |
| 1266 |
else { |
| 1267 |
outLen = 1; |
| 1268 |
ret = os.putB((UKBYTE)stdChar); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
return ret; |
| 1272 |
} |
| 1273 |
|
| 1274 |
#define IS_ODD(x) (x & 1) |
| 1275 |
#define IS_EVEN(x) (!(x & 1)) |
| 1276 |
|
| 1277 |
StdVnChar StdVnToUpper(StdVnChar ch) |
| 1278 |
{ |
| 1279 |
if (ch >= VnStdCharOffset && |
| 1280 |
ch<(VnStdCharOffset + TOTAL_ALPHA_VNCHARS) && |
| 1281 |
IS_ODD(ch)) |
| 1282 |
ch -= 1; |
| 1283 |
return ch; |
| 1284 |
} |
| 1285 |
|
| 1286 |
//---------------------------------------- |
| 1287 |
StdVnChar StdVnToLower(StdVnChar ch) |
| 1288 |
{ |
| 1289 |
if (ch >= VnStdCharOffset && |
| 1290 |
ch<(VnStdCharOffset + TOTAL_ALPHA_VNCHARS) && |
| 1291 |
IS_EVEN(ch)) |
| 1292 |
ch += 1; |
| 1293 |
return ch; |
| 1294 |
} |
| 1295 |
|
| 1296 |
//---------------------------------------- |
| 1297 |
StdVnChar StdVnGetRoot(StdVnChar ch) |
| 1298 |
{ |
| 1299 |
if (ch >= VnStdCharOffset && ch<VnStdCharOffset+TOTAL_VNCHARS) |
| 1300 |
ch = VnStdCharOffset + StdVnRootChar[ch-VnStdCharOffset]; |
| 1301 |
return ch; |
| 1302 |
} |