home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / tojisjms.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  118 lines

  1. /***
  2. *tojisjms.c:  Converts JIS to JMS code, and JMS to JIS code.
  3. *
  4. *       Copyright (c) 1988-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Convert JIS code into Microsoft Kanji code, and vice versa.
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #include <cruntime.h>
  14. #include <mbdata.h>
  15. #include <mbstring.h>
  16. #include <mbctype.h>
  17.  
  18.  
  19. /***
  20. *unsigned int _mbcjistojms(c) - Converts JIS code to Microsoft Kanji Code.
  21. *
  22. *Purpose:
  23. *       Convert JIS code to Microsoft Kanji code.
  24. *
  25. *Entry:
  26. *       unsigned int c - JIS code to be converted. First byte is the upper
  27. *                          8 bits, and second is the lower 8 bits.
  28. *
  29. *Exit:
  30. *       Returns related Microsoft Kanji Code. First byte is the upper 8 bits
  31. *       and second byte is the lower 8 bits.
  32. *
  33. *Exceptions:
  34. *       If c is out of range, _mbcjistojms returns zero.
  35. *
  36. *******************************************************************************/
  37.  
  38. unsigned int __cdecl _mbcjistojms(
  39.     unsigned int c
  40.     )
  41. {
  42.         unsigned int h, l;
  43.  
  44.     if (__mbcodepage != _KANJI_CP)
  45.         return (c);
  46.  
  47.     h = (c >> 8) & 0xff;
  48.     l = c & 0xff;
  49.     if (h < 0x21 || h > 0x7e || l < 0x21 || l > 0x7e)
  50.         return 0;
  51.     if (h & 0x01) {    /* first byte is odd */
  52.         if (l <= 0x5f)
  53.             l += 0x1f;
  54.         else
  55.             l += 0x20;
  56.     }
  57.     else
  58.         l += 0x7e;
  59.  
  60.     h = ((h - 0x21) >> 1) + 0x81;
  61.     if (h > 0x9f)
  62.         h += 0x40;
  63.     return (h << 8) | l;
  64. }
  65.  
  66.  
  67. /***
  68. *unsigned int _mbcjmstojis(c) - Converts Microsoft Kanji code into JIS code.
  69. *
  70. *Purpose:
  71. *       To convert Microsoft Kanji code into JIS code.
  72. *
  73. *Entry:
  74. *       unsigned int c - Microsoft Kanji code to be converted. First byte is
  75. *                          the upper 8 bits, and the second is the lower 8 bits.
  76. *
  77. *Exit:
  78. *       Returns related JIS Code. First byte is the upper 8 bits and the second
  79. *       byte is the lower 8 bits. If c is out of range, return zero.
  80. *
  81. *Exceptions:
  82. *
  83. *******************************************************************************/
  84.  
  85. unsigned int __cdecl _mbcjmstojis(
  86.     unsigned int c
  87.     )
  88. {
  89.         unsigned int    h, l;
  90.  
  91.     if (__mbcodepage != _KANJI_CP)
  92.         return (c);
  93.  
  94.     h = (c >> 8) & 0xff;
  95.     l = c & 0xff;
  96.  
  97.     /* make sure input is valid shift-JIS */
  98.     if ((!(_ISLEADBYTE(h))) || (!(_ISTRAILBYTE(l))))
  99.         return 0;
  100.  
  101.     h -= (h >= 0xa0) ? 0xc1 : 0x81;
  102.     if(l >= 0x9f) {
  103.         c = (h << 9) + 0x2200;
  104.         c |= l - 0x7e;
  105.     } else {
  106.         c = (h << 9) + 0x2100;
  107.         c |= l - ((l <= 0x7e) ? 0x1f : 0x20);
  108.     }
  109.  
  110.     /* not all shift-JIS maps to JIS, so make sure output is valid */
  111.     if ( (c>0x7E7E) || (c<0x2121) || ((c&0xFF)>0x7E) || ((c&0xFF)<0x21) )
  112.         return 0;
  113.  
  114.     return c;
  115. }
  116.  
  117. #endif  /* _MBCS */
  118.