Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1207 lines
25 KiB

  1. /* File: D:\WACKER\htrn_jis\htrn_jis.c (Created: 28-Aug-1994)
  2. *
  3. * Copyright 1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 8 $
  7. * $Date: 5/15/02 4:42p $
  8. */
  9. #include <windows.h>
  10. #include <stdlib.h>
  11. #pragma hdrstop
  12. #include <tdll\stdtyp.h>
  13. // Smart Heap has a problem with manually loaded DLL's
  14. // so we are not using it in here.
  15. //#include <tdll\mc.h>
  16. #include <tdll\features.h>
  17. #include <tdll\translat.h>
  18. #include <tdll\session.h>
  19. #include <tdll\globals.h>
  20. #include <tdll\hlptable.h>
  21. #include <tdll\sf.h>
  22. #include <tdll\sess_ids.h>
  23. #if defined(CHARACTER_TRANSLATION)
  24. #include <tdll\translat.hh>
  25. #endif
  26. #include "htrn_jis.h"
  27. #include "htrn_jis.hh"
  28. #define IDS_GNRL_HELPFILE 102
  29. #if defined(INCL_USE_HTML_HELP)
  30. #define IDS_HTML_HELPFILE 114
  31. #endif
  32. static TCHAR szHelpFileName[FNAME_LEN];
  33. BOOL WINAPI _CRT_INIT(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpReserved);
  34. INT_PTR CALLBACK EncodeSelectDlg(HWND hDlg, UINT wMsg, WPARAM wPar, LPARAM lPar);
  35. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  36. * FUNCTION:
  37. * transJisEntry
  38. *
  39. * DESCRIPTION:
  40. * Currently, just initializes the C-Runtime library but may be used
  41. * for other things later.
  42. *
  43. * ARGUMENTS:
  44. * hInstDll - Instance of this DLL
  45. * fdwReason - Why this entry point is called
  46. * lpReserved - reserved
  47. *
  48. * RETURNS:
  49. * BOOL
  50. *
  51. */
  52. static HINSTANCE hInstanceDll;
  53. BOOL WINAPI transJisEntry(HINSTANCE hInstDll,
  54. DWORD fdwReason,
  55. LPVOID lpReserved)
  56. {
  57. hInstanceDll = hInstDll;
  58. // You need to initialize the C runtime if you use any C-Runtime
  59. // functions.
  60. #if defined(NDEBUG)
  61. return TRUE;
  62. #else
  63. return _CRT_INIT(hInstDll, fdwReason, lpReserved);
  64. #endif
  65. }
  66. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  67. * FUNCTION:
  68. *
  69. * DESCRIPTION:
  70. *
  71. * ARGUMENTS:
  72. *
  73. * RETURNS:
  74. *
  75. */
  76. VOID *transCreateHandle(HSESSION hSession)
  77. {
  78. pstICT pstI;
  79. pstI = malloc(sizeof(stICT));
  80. if (pstI)
  81. {
  82. memset(pstI, 0, sizeof(stICT));
  83. pstI->hSession = hSession;
  84. transInitHandle(pstI);
  85. }
  86. return (VOID *)pstI;
  87. }
  88. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  89. * FUNCTION:
  90. *
  91. * DESCRIPTION:
  92. *
  93. * ARGUMENTS:
  94. *
  95. * RETURNS:
  96. *
  97. */
  98. int transInitHandle(VOID *pHdl)
  99. {
  100. pstICT pstI;
  101. pstI = (pstICT)pHdl;
  102. if (pstI)
  103. {
  104. pstI->nInputMode = PASS_THRU_MODE;
  105. pstI->nOutputMode = PASS_THRU_MODE;
  106. }
  107. return TRANS_OK;
  108. }
  109. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  110. * FUNCTION:
  111. *
  112. * DESCRIPTION:
  113. *
  114. * ARGUMENTS:
  115. *
  116. * RETURNS:
  117. *
  118. */
  119. int transLoadHandle(VOID *pHdl)
  120. {
  121. pstICT pstI;
  122. SF_HANDLE hSF;
  123. long lSize;
  124. pstI = (pstICT)pHdl;
  125. if (pstI)
  126. {
  127. hSF = sessQuerySysFileHdl(pstI->hSession);
  128. if (hSF)
  129. {
  130. lSize = sizeof(int);
  131. sfGetSessionItem(hSF,
  132. SFID_TRANS_FIRST,
  133. &lSize,
  134. &pstI->nInputMode);
  135. lSize = sizeof(int);
  136. sfGetSessionItem(hSF,
  137. SFID_TRANS_FIRST + 1,
  138. &lSize,
  139. &pstI->nOutputMode);
  140. }
  141. }
  142. return TRANS_OK;
  143. }
  144. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  145. * FUNCTION:
  146. *
  147. * DESCRIPTION:
  148. *
  149. * ARGUMENTS:
  150. *
  151. * RETURNS:
  152. *
  153. */
  154. int transSaveHandle(VOID *pHdl)
  155. {
  156. pstICT pstI;
  157. SF_HANDLE hSF;
  158. pstI = (pstICT)pHdl;
  159. if (pstI)
  160. {
  161. hSF = sessQuerySysFileHdl(pstI->hSession);
  162. if (hSF)
  163. {
  164. sfPutSessionItem(hSF,
  165. SFID_TRANS_FIRST,
  166. sizeof(int),
  167. &pstI->nInputMode);
  168. sfPutSessionItem(hSF,
  169. SFID_TRANS_FIRST + 1,
  170. sizeof(int),
  171. &pstI->nOutputMode);
  172. }
  173. }
  174. return TRANS_OK;
  175. }
  176. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  177. * FUNCTION:
  178. *
  179. * DESCRIPTION:
  180. *
  181. * ARGUMENTS:
  182. *
  183. * RETURNS:
  184. *
  185. */
  186. int transDestroyHandle(VOID *pHdl)
  187. {
  188. pstICT pstI;
  189. pstI = (pstICT)pHdl;
  190. if (pstI)
  191. {
  192. free(pstI);
  193. pstI = NULL;
  194. }
  195. return TRANS_OK;
  196. }
  197. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  198. * FUNCTION:
  199. *
  200. * DESCRIPTION:
  201. *
  202. * ARGUMENTS:
  203. *
  204. * RETURNS:
  205. *
  206. */
  207. int transDoDialog(HWND hDlg, VOID *pHdl)
  208. {
  209. pstICT pstI;
  210. pstI = (pstICT)pHdl;
  211. if (pstI)
  212. {
  213. DialogBoxParam(
  214. hInstanceDll,
  215. "IDD_TRANSLATE",
  216. hDlg,
  217. EncodeSelectDlg,
  218. (LPARAM)pstI->hSession
  219. );
  220. }
  221. return TRANS_OK;
  222. }
  223. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  224. * FUNCTION:
  225. *
  226. * DESCRIPTION:
  227. *
  228. * ARGUMENTS:
  229. *
  230. * RETURNS:
  231. *
  232. */
  233. static void transPumpOutString(TCHAR *cReady, int *nReady, TCHAR *cOut)
  234. {
  235. TCHAR *pszPtr;
  236. pszPtr = cReady + *nReady;
  237. while (*cOut != TEXT('\0'))
  238. {
  239. *pszPtr++ = *cOut++;
  240. *nReady += 1;
  241. }
  242. }
  243. static void transPumpOutChar(TCHAR *cReady, int *nReady, TCHAR cOut)
  244. {
  245. TCHAR *pszPtr;
  246. pszPtr = cReady + *nReady;
  247. *pszPtr = cOut;
  248. *nReady += 1;
  249. }
  250. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  251. * FUNCTION:
  252. *
  253. * DESCRIPTION:
  254. *
  255. * ARGUMENTS:
  256. *
  257. * RETURNS:
  258. *
  259. */
  260. static void transShiftToJisOut(struct stShiftToJis *pstS,
  261. TCHAR *cReady,
  262. int *nReady,
  263. TCHAR cOne,
  264. TCHAR cTwo)
  265. {
  266. unsigned int adjust;
  267. unsigned int rowOffset;
  268. unsigned int cellOffset;
  269. if (!pstS->nInTwoByteMode)
  270. {
  271. transPumpOutString(cReady,
  272. nReady,
  273. TEXT("\x1B$B"));
  274. pstS->nInTwoByteMode = TRUE;
  275. }
  276. adjust = cTwo < 159;
  277. rowOffset = (cOne < 160) ? 112 : 176;
  278. cellOffset = adjust ? ((cTwo > 127) ? 32 : 31) : 126;
  279. cOne = (TCHAR)(((cOne - rowOffset) << 1) - adjust);
  280. cTwo -= (TCHAR)cellOffset;
  281. transPumpOutChar(cReady, nReady, cOne);
  282. transPumpOutChar(cReady, nReady, cTwo);
  283. }
  284. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  285. * FUNCTION:
  286. *
  287. * DESCRIPTION:
  288. *
  289. * ARGUMENTS:
  290. *
  291. * RETURNS:
  292. *
  293. */
  294. static void transShiftToEucOut(struct stShiftToJis *pstS,
  295. TCHAR *cReady,
  296. int *nReady,
  297. TCHAR cOne,
  298. TCHAR cTwo)
  299. {
  300. unsigned int adjust;
  301. unsigned int rowOffset;
  302. unsigned int cellOffset;
  303. adjust = cTwo < 159;
  304. rowOffset = (cOne < 160) ? 112 : 176;
  305. cellOffset = adjust ? ((cTwo > 127) ? 32 : 31) : 126;
  306. cOne = (TCHAR)(((cOne - rowOffset) << 1) - adjust);
  307. cOne |= 0x80;
  308. cTwo -= (TCHAR)cellOffset;
  309. cTwo |= 0x80;
  310. transPumpOutChar(cReady, nReady, cOne);
  311. transPumpOutChar(cReady, nReady, cTwo);
  312. }
  313. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  314. * FUNCTION:
  315. *
  316. * DESCRIPTION:
  317. *
  318. * ARGUMENTS:
  319. *
  320. * RETURNS:
  321. *
  322. */
  323. static void transJisToShiftOut(struct stJisToShift *pstJ,
  324. TCHAR *cReady,
  325. int *nReady,
  326. TCHAR cOne,
  327. TCHAR cTwo)
  328. {
  329. unsigned int rowOffset;
  330. unsigned int cellOffset;
  331. rowOffset = (cOne < 95) ? 112 : 176;
  332. cellOffset = (cOne % 2) ? ((cTwo > 95) ? 32 : 31) : 126;
  333. cOne = (TCHAR)(((cOne + 1) >> 1) + rowOffset);
  334. cTwo += (TCHAR)cellOffset;
  335. transPumpOutChar(cReady, nReady, cOne);
  336. transPumpOutChar(cReady, nReady, cTwo);
  337. }
  338. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  339. * FUNCTION:
  340. *
  341. * DESCRIPTION:
  342. *
  343. * ARGUMENTS:
  344. *
  345. * RETURNS:
  346. *
  347. */
  348. static void transEucToShiftOut(struct stJisToShift *pstJ,
  349. TCHAR *cReady,
  350. int *nReady,
  351. TCHAR cOne,
  352. TCHAR cTwo)
  353. {
  354. unsigned int rowOffset;
  355. unsigned int cellOffset;
  356. cOne &= 0x7F;
  357. cTwo &= 0x7F;
  358. rowOffset = (cOne < 95) ? 112 : 176;
  359. cellOffset = (cOne % 2) ? ((cTwo > 95) ? 32 : 31) : 126;
  360. cOne = (TCHAR)(((cOne + 1) >> 1) + rowOffset);
  361. cTwo += (TCHAR)cellOffset;
  362. transPumpOutChar(cReady, nReady, cOne);
  363. transPumpOutChar(cReady, nReady, cTwo);
  364. }
  365. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  366. * FUNCTION:
  367. *
  368. * DESCRIPTION:
  369. *
  370. * ARGUMENTS:
  371. * cOne -- the half width katakana character
  372. * cTwo -- the next character, maybe a MARU or NIGORI
  373. *
  374. * RETURNS:
  375. * TRUE means that both characters were processed, no further action needed.
  376. * FALSE means that the second character still needs to be processed.
  377. *
  378. */
  379. static int transHalfToFullKatakana(struct stShiftToJis *pstS,
  380. TCHAR *cReady,
  381. int *nReady,
  382. int fJisOut,
  383. TCHAR cOne,
  384. TCHAR cTwo)
  385. {
  386. int nRet = TRUE;
  387. int nigori = FALSE;
  388. int maru = FALSE;
  389. int tmp;
  390. /*
  391. * The data structure for mapping half width katakana characters to
  392. * full width characters in the Shift-JIS encoding scheme.
  393. */
  394. int mtable[][2] = {
  395. {129,66},{129,117},{129,118},{129,65},{129,69},{131,146},{131,64},
  396. {131,66},{131,68},{131,70},{131,72},{131,131},{131,133},{131,135},
  397. {131,98},{129,91},{131,65},{131,67},{131,69},{131,71},{131,73},
  398. {131,74},{131,76},{131,78},{131,80},{131,82},{131,84},{131,86},
  399. {131,88},{131,90},{131,92},{131,94},{131,96},{131,99},{131,101},
  400. {131,103},{131,105},{131,106},{131,107},{131,108},{131,109},
  401. {131,110},{131,113},{131,116},{131,119},{131,122},{131,125},
  402. {131,126},{131,128},{131,129},{131,130},{131,132},{131,134},
  403. {131,136},{131,137},{131,138},{131,139},{131,140},{131,141},
  404. {131,143},{131,147},{129,74},{129,75}
  405. };
  406. if (cTwo == 222) /* Is it a nigori mark ? */
  407. {
  408. if (ISNIGORI(cOne)) /* Can it be modified with a NIGORI ? */
  409. nigori = TRUE;
  410. else
  411. nRet = FALSE;
  412. }
  413. else if (cTwo == 223) /* Is it a maru mark ? */
  414. {
  415. if (ISMARU(cOne)) /* Can it be modified with a MARU ? */
  416. maru = TRUE;
  417. else
  418. nRet = FALSE;
  419. }
  420. else
  421. {
  422. /* Wasn't a nigori or a maru */
  423. nRet = FALSE;
  424. }
  425. tmp = cOne;
  426. cOne = (TCHAR)mtable[tmp - 161][0];
  427. cTwo = (TCHAR)mtable[tmp - 161][1];
  428. if (nigori)
  429. {
  430. /*
  431. * Transform a kana into a kana with nigori
  432. */
  433. if (((cTwo >= 74) && (cTwo <= 103)) ||
  434. ((cTwo >= 110) && (cTwo <= 122)))
  435. {
  436. cTwo += 1;
  437. }
  438. else if ((cOne == 131) && (cTwo == 69))
  439. {
  440. cTwo = (TCHAR)148;
  441. }
  442. }
  443. if (maru)
  444. {
  445. /*
  446. * Transform a kana into a kana with maru
  447. */
  448. if ((cTwo >= 110) && (cTwo <= 122))
  449. {
  450. cTwo += 2;
  451. }
  452. }
  453. if (fJisOut)
  454. {
  455. transShiftToJisOut(pstS,
  456. cReady,
  457. nReady,
  458. cOne,
  459. cTwo);
  460. }
  461. else
  462. {
  463. transShiftToEucOut(pstS,
  464. cReady,
  465. nReady,
  466. cOne,
  467. cTwo);
  468. }
  469. return nRet;
  470. }
  471. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  472. * FUNCTION:
  473. *
  474. * DESCRIPTION:
  475. *
  476. * ARGUMENTS:
  477. *
  478. * RETURNS:
  479. *
  480. */
  481. int transCharIn(VOID *pHdl,
  482. TCHAR cIn,
  483. int *nReady,
  484. int nSize,
  485. TCHAR *cReady)
  486. {
  487. int nOK;
  488. int nTemp;
  489. TCHAR cTest;
  490. pstICT pstI;
  491. struct stJisToShift *pstJ;
  492. *nReady = 0;
  493. pstI = (pstICT)pHdl;
  494. if (pstI)
  495. {
  496. switch(pstI->nInputMode)
  497. {
  498. case PASS_THRU_MODE:
  499. if (nSize > 0)
  500. {
  501. *cReady = cIn;
  502. *nReady = 1;
  503. }
  504. break;
  505. case JIS_TO_SHIFT_MODE:
  506. /*
  507. * This code is a modified version of the code found in the
  508. * book "Understanding Japanese Information Processing" by
  509. * Ken Lunde. See page 171, "Seven- to Eight-bit Conversion".
  510. */
  511. pstJ = (struct stJisToShift *)&pstI->uIn;
  512. if (cIn == TEXT('\x1B'))
  513. {
  514. /* The magical escape sequence */
  515. nTemp = pstJ->nInTwoByteMode;
  516. memset(pstJ, 0, sizeof(struct stJisToShift));
  517. pstJ->nInTwoByteMode = nTemp;
  518. pstJ->nSeenEscape = TRUE;
  519. }
  520. else if (pstJ->nSeenEscape)
  521. {
  522. /*
  523. * OK, this is the messy place. Just hang on and we will
  524. * get thru without too many injuries.
  525. *
  526. * This will need to be rewritten if the escape sequences
  527. * that we look for are changed.
  528. *
  529. * Currently we accept:
  530. * <esc> $ B to shift into 2 byte mode
  531. * <esc> ( J to shift out of 2 byte mode
  532. *
  533. * added codes as of 18-Jan-95:
  534. * <esc> $ @ to shift into 2 byte mode
  535. * <esc> ( H to shift out of 2 byte mode
  536. * <esc> ( B to shift out of 2 byte mode
  537. */
  538. pstJ->acBuffer[pstJ->nEscSeqCount] = cIn;
  539. pstJ->nEscSeqCount += 1;
  540. nOK = TRUE;
  541. switch(pstJ->nEscSeqCount)
  542. {
  543. case 1:
  544. cTest = pstJ->acBuffer[0];
  545. if ((cTest == TEXT('$')) || (cTest == TEXT('(')))
  546. break; /* OK */
  547. nOK = FALSE;
  548. break;
  549. case 2:
  550. cTest = pstJ->acBuffer[0];
  551. switch (cTest)
  552. {
  553. case TEXT('$'):
  554. cTest = pstJ->acBuffer[1];
  555. switch (cTest)
  556. {
  557. case TEXT('B'):
  558. case TEXT('@'):
  559. /*
  560. * Shift to two byte mode
  561. */
  562. if (!pstJ->nInTwoByteMode)
  563. {
  564. memset(pstJ, 0,
  565. sizeof(struct stJisToShift));
  566. pstJ->nInTwoByteMode = TRUE;
  567. }
  568. break;
  569. default:
  570. nOK = FALSE;
  571. break;
  572. }
  573. break;
  574. case TEXT('('):
  575. cTest = pstJ->acBuffer[1];
  576. switch (cTest)
  577. {
  578. case TEXT('J'):
  579. case TEXT('H'):
  580. case TEXT('B'):
  581. /*
  582. * Shift from two byte mode
  583. */
  584. if (pstJ->nInTwoByteMode)
  585. {
  586. memset(pstJ, 0,
  587. sizeof(struct stJisToShift));
  588. pstJ->nInTwoByteMode = FALSE;
  589. }
  590. break;
  591. default:
  592. nOK = FALSE;
  593. break;
  594. }
  595. break;
  596. default:
  597. nOK = FALSE;
  598. break;
  599. }
  600. break;
  601. default:
  602. nOK = FALSE;
  603. break;
  604. }
  605. if (!nOK)
  606. {
  607. pstJ->acBuffer[pstJ->nEscSeqCount] = TEXT('\0');
  608. /*
  609. * Dump out whatever it is we have seen
  610. */
  611. if (pstJ->nSeenEscape)
  612. {
  613. transPumpOutChar(cReady, nReady, TEXT('\x1B'));
  614. }
  615. transPumpOutString(cReady,
  616. nReady,
  617. pstJ->acBuffer);
  618. /*
  619. * For now, preserve the state of nInTwoByteMode
  620. */
  621. nTemp = pstJ->nInTwoByteMode;
  622. memset(pstJ, 0, sizeof(struct stJisToShift));
  623. pstJ->nInTwoByteMode = nTemp;
  624. }
  625. }
  626. else if (pstJ->nSeenFirstCharacter)
  627. {
  628. /*
  629. * Got two characters to convert and pump out
  630. */
  631. transJisToShiftOut(pstJ,
  632. cReady,
  633. nReady,
  634. pstJ->chPrev,
  635. cIn);
  636. pstJ->nSeenFirstCharacter = FALSE;
  637. pstJ->chPrev = TEXT('\0');
  638. }
  639. else if ((cIn == TEXT('\n')) || (cIn == TEXT('\r')))
  640. {
  641. /*
  642. * Switch out of two byte mode
  643. */
  644. pstJ->nInTwoByteMode = FALSE;
  645. pstJ->nSeenFirstCharacter = FALSE;
  646. pstJ->chPrev = TEXT('\0');
  647. transPumpOutChar(cReady, nReady, cIn);
  648. }
  649. else
  650. {
  651. if (pstJ->nInTwoByteMode)
  652. {
  653. pstJ->nSeenFirstCharacter = TRUE;
  654. pstJ->chPrev = cIn;
  655. }
  656. else
  657. {
  658. /*
  659. * Nothing going on, just pump out the character
  660. */
  661. transPumpOutChar(cReady, nReady, cIn);
  662. }
  663. }
  664. break;
  665. case EUC_TO_SHIFT_MODE:
  666. pstJ = (struct stJisToShift *)&pstI->uOut;
  667. if (pstJ->nSeenFirstCharacter)
  668. {
  669. if (ISEUC(cIn))
  670. {
  671. transEucToShiftOut(pstJ,
  672. cReady,
  673. nReady,
  674. pstJ->chPrev,
  675. cIn);
  676. }
  677. else
  678. {
  679. transPumpOutChar(cReady, nReady, pstJ->chPrev);
  680. transPumpOutChar(cReady, nReady, cIn);
  681. }
  682. pstJ->nSeenFirstCharacter = FALSE;
  683. pstJ->chPrev = 0;
  684. }
  685. else if (pstJ->nHalfWidthKanaSeen)
  686. {
  687. /*
  688. * Handle result of the previous case
  689. */
  690. transPumpOutChar(cReady, nReady, cIn);
  691. pstJ->nHalfWidthKanaSeen = FALSE;
  692. pstJ->chPrev = TEXT('\0');
  693. }
  694. else if (cIn == 0x8E)
  695. {
  696. /*
  697. * Set up to convert next character to half width katakana
  698. */
  699. pstJ->nHalfWidthKanaSeen = TRUE;
  700. pstJ->chPrev = cIn;
  701. }
  702. else if (ISEUC(cIn))
  703. {
  704. pstJ->nSeenFirstCharacter = TRUE;
  705. pstJ->chPrev = cIn;
  706. }
  707. else
  708. {
  709. transPumpOutChar(cReady, nReady, cIn);
  710. }
  711. break;
  712. case SHIFT_TO_JIS_MODE:
  713. default:
  714. break;
  715. }
  716. }
  717. return TRANS_OK;
  718. }
  719. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  720. * FUNCTION:
  721. *
  722. * DESCRIPTION:
  723. *
  724. * ARGUMENTS:
  725. *
  726. * RETURNS:
  727. *
  728. */
  729. int transCharOut(VOID *pHdl,
  730. TCHAR cOut,
  731. int *nReady,
  732. int nSize,
  733. TCHAR *cReady)
  734. {
  735. pstICT pstI;
  736. struct stShiftToJis *pstS;
  737. int nFinished;
  738. *nReady = 0;
  739. pstI = (pstICT)pHdl;
  740. restart:
  741. if (pstI)
  742. {
  743. switch(pstI->nOutputMode)
  744. {
  745. case PASS_THRU_MODE:
  746. if (nSize > 0)
  747. {
  748. *cReady = cOut;
  749. *nReady = 1;
  750. }
  751. break;
  752. case SHIFT_TO_JIS_MODE:
  753. /*
  754. * This code is a modified version of the code found in the
  755. * book "Understanding Japanese Information Processing" by
  756. * Ken Lunde. See page 170, "Eight- to Seven-bit Conversion".
  757. */
  758. pstS = (struct stShiftToJis *)&pstI->uOut;
  759. if ((cOut == TEXT('\r')) || (cOut == TEXT('\n')))
  760. {
  761. if (pstS->nInTwoByteMode)
  762. {
  763. /* Output the escape to one bye sequence */
  764. transPumpOutString(cReady,
  765. nReady,
  766. TEXT("\x1B(J"));
  767. pstS->nInTwoByteMode = FALSE;
  768. }
  769. /* Output the end of line character */
  770. transPumpOutChar(cReady, nReady, cOut);
  771. }
  772. else if (pstS->nLeadByteSeen)
  773. {
  774. /* Already seen a lead byte last time thru */
  775. transShiftToJisOut(pstS,
  776. cReady,
  777. nReady,
  778. pstS->chPrev,
  779. cOut);
  780. pstS->nLeadByteSeen = FALSE;
  781. pstS->chPrev = TEXT('\0');
  782. }
  783. else if (pstS->nHalfWidthKanaSeen)
  784. {
  785. nFinished = transHalfToFullKatakana(pstS,
  786. cReady,
  787. nReady,
  788. TRUE,
  789. pstS->chPrev,
  790. cOut);
  791. pstS->nHalfWidthKanaSeen = FALSE;
  792. pstS->chPrev = TEXT('\0');
  793. if (!nFinished)
  794. goto restart;
  795. }
  796. else if (SJIS1(cOut))
  797. {
  798. /* If the character is a DBCS lead byte */
  799. pstS->nLeadByteSeen = TRUE;
  800. pstS->chPrev = cOut;
  801. }
  802. else if (HANKATA(cOut))
  803. {
  804. /* If the character is a half width katakana character */
  805. pstS->nHalfWidthKanaSeen = TRUE;
  806. pstS->chPrev = cOut;
  807. }
  808. else
  809. {
  810. if (pstS->nInTwoByteMode)
  811. {
  812. /* Output the escape to one bye sequence */
  813. transPumpOutString(cReady,
  814. nReady,
  815. TEXT("\x1B(J"));
  816. pstS->nInTwoByteMode = FALSE;
  817. }
  818. /* Output the character */
  819. transPumpOutChar(cReady, nReady, cOut);
  820. }
  821. break;
  822. case SHIFT_TO_EUC_MODE:
  823. /*
  824. */
  825. pstS = (struct stShiftToJis *)&pstI->uOut;
  826. if (pstS->nLeadByteSeen)
  827. {
  828. /* Already seen a lead byte last time thru */
  829. transShiftToEucOut(pstS,
  830. cReady,
  831. nReady,
  832. pstS->chPrev,
  833. cOut);
  834. pstS->nLeadByteSeen = FALSE;
  835. pstS->chPrev = TEXT('\0');
  836. }
  837. else if (pstS->nHalfWidthKanaSeen)
  838. {
  839. nFinished = transHalfToFullKatakana(pstS,
  840. cReady,
  841. nReady,
  842. FALSE,
  843. pstS->chPrev,
  844. cOut);
  845. pstS->nHalfWidthKanaSeen = FALSE;
  846. pstS->chPrev = TEXT('\0');
  847. if (!nFinished)
  848. goto restart;
  849. }
  850. else if (SJIS1(cOut))
  851. {
  852. /* If the character is a DBCS lead byte */
  853. pstS->nLeadByteSeen = TRUE;
  854. pstS->chPrev = cOut;
  855. }
  856. else if (HANKATA(cOut))
  857. {
  858. /* If the character is a half width katakana character */
  859. transPumpOutChar(cReady, nReady, (TCHAR)0x8E);
  860. transPumpOutChar(cReady, nReady, cOut);
  861. }
  862. else
  863. {
  864. transPumpOutChar(cReady, nReady, cOut);
  865. }
  866. break;
  867. case JIS_TO_SHIFT_MODE:
  868. default:
  869. break;
  870. }
  871. }
  872. return TRANS_OK;
  873. }
  874. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  875. * DESCRIPTION:
  876. * Header stuff for the dialog procedure that follows.
  877. *
  878. * NOTE:
  879. * Most of this stuff was copied from TDLL\GENRCDLG.C along with the
  880. * framework code for the dialog procedure.
  881. */
  882. #if !defined(DlgParseCmd)
  883. #define DlgParseCmd(i,n,c,w,l) i=LOWORD(w);n=HIWORD(w);c=(HWND)l;
  884. #endif
  885. struct stSaveDlgStuff
  886. {
  887. /*
  888. * Put in whatever else you might need to access later
  889. */
  890. HSESSION hSession;
  891. };
  892. typedef struct stSaveDlgStuff SDS;
  893. // Dialog control defines...
  894. #define IDC_GR_TRANSLATION 101
  895. #define IDC_RB_SHIFT_JIS 102
  896. #define IDC_RB_STANDARD_JIS 103
  897. #define IDC_PB_HELP 8
  898. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  899. * FUNCTION: EncodeSelectDlg
  900. *
  901. * DESCRIPTION: Dialog manager stub
  902. *
  903. * ARGUMENTS: Standard Windows dialog manager
  904. *
  905. * RETURNS: Standard Windows dialog manager
  906. *
  907. */
  908. INT_PTR CALLBACK EncodeSelectDlg(HWND hDlg, UINT wMsg, WPARAM wPar, LPARAM lPar)
  909. {
  910. HWND hwndChild;
  911. INT nId;
  912. INT nNtfy;
  913. SDS *pS;
  914. static aHlpTable[] = {IDC_GR_TRANSLATION, IDH_HTRN_DIALOG,
  915. IDC_RB_SHIFT_JIS, IDH_HTRN_SHIFTJIS,
  916. IDC_RB_STANDARD_JIS, IDH_HTRN_JIS,
  917. IDCANCEL, IDH_CANCEL,
  918. IDOK, IDH_OK,
  919. 0, 0};
  920. #if defined(CHARACTER_TRANSLATION)
  921. HHTRANSLATE hTrans = NULL;
  922. pstICT pstI;
  923. #endif
  924. switch (wMsg)
  925. {
  926. case WM_INITDIALOG:
  927. pS = (SDS *)malloc(sizeof(SDS));
  928. if (pS == (SDS *)0)
  929. {
  930. /* TODO: decide if we need to display an error here */
  931. EndDialog(hDlg, FALSE);
  932. break;
  933. }
  934. SetWindowLongPtr(hDlg, DWLP_USER, (LONG_PTR)pS);
  935. // mscCenterWindowOnWindow(hDlg, GetParent(hDlg));
  936. pS->hSession = (HSESSION)lPar;
  937. #if defined(CHARACTER_TRANSLATION)
  938. hTrans = (HHTRANSLATE)sessQueryTranslateHdl(pS->hSession);
  939. if (hTrans)
  940. {
  941. pstI = (pstICT)hTrans->pDllHandle;
  942. if (pstI == NULL || (pstI && pstI->nInputMode == PASS_THRU_MODE))
  943. {
  944. CheckDlgButton(hDlg, IDC_RB_SHIFT_JIS, TRUE);
  945. }
  946. else
  947. {
  948. CheckDlgButton(hDlg, IDC_RB_STANDARD_JIS, TRUE);
  949. }
  950. }
  951. #endif
  952. break;
  953. case WM_DESTROY:
  954. break;
  955. case WM_CONTEXTMENU:
  956. if ( ((LPHELPINFO)lPar)->iCtrlId == IDOK || ((LPHELPINFO)lPar)->iCtrlId == IDCANCEL )
  957. {
  958. WinHelp(((LPHELPINFO)lPar)->hItemHandle,
  959. TEXT("windows.hlp"),
  960. HELP_CONTEXTMENU,
  961. (DWORD_PTR)(LPTSTR)aHlpTable);
  962. }
  963. else
  964. {
  965. LoadString(hInstanceDll, IDS_GNRL_HELPFILE, szHelpFileName,
  966. sizeof(szHelpFileName) / sizeof(TCHAR));
  967. WinHelp((HWND)wPar,
  968. szHelpFileName,
  969. HELP_CONTEXTMENU,
  970. (DWORD_PTR)(LPTSTR)aHlpTable);
  971. }
  972. break;
  973. case WM_HELP:
  974. if ( ((LPHELPINFO)lPar)->iCtrlId == IDOK || ((LPHELPINFO)lPar)->iCtrlId == IDCANCEL )
  975. {
  976. WinHelp(((LPHELPINFO)lPar)->hItemHandle,
  977. TEXT("windows.hlp"),
  978. HELP_WM_HELP,
  979. (DWORD_PTR)(LPTSTR)aHlpTable);
  980. }
  981. else
  982. {
  983. LoadString(hInstanceDll, IDS_GNRL_HELPFILE, szHelpFileName,
  984. sizeof(szHelpFileName) / sizeof(TCHAR));
  985. WinHelp(((LPHELPINFO)lPar)->hItemHandle,
  986. szHelpFileName,
  987. HELP_WM_HELP,
  988. (DWORD_PTR)(LPTSTR)aHlpTable);
  989. }
  990. break;
  991. case WM_COMMAND:
  992. /*
  993. * Did we plan to put a macro in here to do the parsing ?
  994. */
  995. DlgParseCmd(nId, nNtfy, hwndChild, wPar, lPar);
  996. switch (nId)
  997. {
  998. case IDOK:
  999. pS = (SDS *)GetWindowLongPtr(hDlg, DWLP_USER);
  1000. /*
  1001. * Do whatever saving is necessary
  1002. */
  1003. #if defined(CHARACTER_TRANSLATION)
  1004. hTrans = (HHTRANSLATE)sessQueryTranslateHdl(pS->hSession);
  1005. if (hTrans)
  1006. {
  1007. pstI = (pstICT)hTrans->pDllHandle;
  1008. if (pstI)
  1009. {
  1010. if (IsDlgButtonChecked(hDlg, IDC_RB_SHIFT_JIS) == BST_CHECKED)
  1011. {
  1012. pstI->nInputMode = PASS_THRU_MODE;
  1013. pstI->nOutputMode = PASS_THRU_MODE;
  1014. }
  1015. else
  1016. {
  1017. pstI->nInputMode = JIS_TO_SHIFT_MODE;
  1018. pstI->nOutputMode = SHIFT_TO_JIS_MODE;
  1019. }
  1020. }
  1021. }
  1022. #endif
  1023. /* Free the storeage */
  1024. free(pS);
  1025. pS = (SDS *)0;
  1026. EndDialog(hDlg, TRUE);
  1027. break;
  1028. case IDCANCEL:
  1029. pS = (SDS *)GetWindowLongPtr(hDlg, DWLP_USER);
  1030. /* Free the storeage */
  1031. free(pS);
  1032. pS = (SDS *)0;
  1033. EndDialog(hDlg, FALSE);
  1034. break;
  1035. default:
  1036. return FALSE;
  1037. }
  1038. break;
  1039. default:
  1040. return FALSE;
  1041. }
  1042. return TRUE;
  1043. }
  1044. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  1045. * FUNCTION: transQueryMode
  1046. *
  1047. * DESCRIPTION:
  1048. *
  1049. * ARGUMENTS:
  1050. *
  1051. * RETURNS:
  1052. */
  1053. int transQueryMode(VOID *pHdl)
  1054. {
  1055. pstICT pstI;
  1056. int nReturn;
  1057. pstI = (pstICT)pHdl;
  1058. if(pstI->nInputMode == PASS_THRU_MODE)
  1059. {
  1060. nReturn = 0;
  1061. }
  1062. else
  1063. {
  1064. nReturn = 1;
  1065. }
  1066. return(nReturn);
  1067. }
  1068. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  1069. * FUNCTION: transSetMode
  1070. *
  1071. * DESCRIPTION:
  1072. *
  1073. * ARGUMENTS:
  1074. *
  1075. * RETURNS:
  1076. */
  1077. int transSetMode(VOID *pHdl,
  1078. int nMode)
  1079. {
  1080. pstICT pstI;
  1081. pstI = (pstICT)pHdl;
  1082. if (nMode == 0)
  1083. {
  1084. pstI->nInputMode = PASS_THRU_MODE;
  1085. pstI->nOutputMode = PASS_THRU_MODE;
  1086. }
  1087. else
  1088. {
  1089. pstI->nInputMode = JIS_TO_SHIFT_MODE;
  1090. pstI->nOutputMode = SHIFT_TO_JIS_MODE;
  1091. }
  1092. return(1);
  1093. }