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

  1. /***
  2. *tombbmbc.c - convert 1-byte code to and from 2-byte code
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       _mbbtombc() - converts 1-byte code to corresponding 2-byte code
  8. *       _mbctombb() - converts 2-byte code to corresponding 1-byte code
  9. *
  10. *******************************************************************************/
  11.  
  12. #ifdef _MBCS
  13.  
  14. #include <cruntime.h>
  15. #include <mbdata.h>
  16. #include <mbstring.h>
  17. #include <mbctype.h>
  18.  
  19.  
  20. #define ASCLOW   0x20
  21. #define ASCHIGH  0x7e
  22.  
  23. #define SBLOW   0xA1
  24. #define SBHIGH  0xDF
  25.  
  26. #define MBLIMIT 0x8396
  27.  
  28. static unsigned short mbbtable[] = {
  29.         /*20*/  0x8140, 0x8149, 0x8168, 0x8194, 0x8190, 0x8193, 0x8195, 0x8166,
  30.                 0x8169, 0x816a, 0x8196, 0x817b, 0x8143, 0x817c, 0x8144, 0x815e,
  31.         /*30*/  0x824f, 0x8250, 0x8251, 0x8252, 0x8253, 0x8254, 0x8255, 0x8256,
  32.                 0x8257, 0x8258, 0x8146, 0x8147, 0x8183, 0x8181, 0x8184, 0x8148,
  33.         /*40*/  0x8197, 0x8260, 0x8261, 0x8262, 0x8263, 0x8264, 0x8265, 0x8266,
  34.                 0x8267, 0x8268, 0x8269, 0x826a, 0x826b, 0x826c, 0x826d, 0x826e,
  35.         /*50*/  0x826f, 0x8270, 0x8271, 0x8272, 0x8273, 0x8274, 0x8275, 0x8276,
  36.                 0x8277, 0x8278, 0x8279, 0x816d, 0x818f, 0x816e, 0x814f, 0x8151,
  37.         /*60*/  0x8165, 0x8281, 0x8282, 0x8283, 0x8284, 0x8285, 0x8286, 0x8287,
  38.                 0x8288, 0x8289, 0x828a, 0x828b, 0x828c, 0x828d, 0x828e, 0x828f,
  39.         /*70*/  0x8290, 0x8291, 0x8292, 0x8293, 0x8294, 0x8295, 0x8296, 0x8297,
  40.                 0x8298, 0x8299, 0x829a, 0x816f, 0x8162, 0x8170, 0x8150,
  41. };
  42.  
  43. static struct   {
  44.     unsigned char   asc;
  45.     char            synonym;
  46.     unsigned short  mbccode;
  47. }   mbctable[] = {
  48.     //     ASCII Code | Synonym | KANJI Code
  49. //Katakana Table
  50.         {    0xA7,         1,       0x8340      },  //  'a'
  51.         {    0xB1,         1,       0x8341      },  //  'A'
  52.         {    0xA8,         1,       0x8342      },  //  'i'
  53.         {    0xB2,         1,       0x8343      },  //  'I'
  54.         {    0xA9,         1,       0x8344      },  //  'u'
  55.         {    0xB3,         1,       0x8345      },  //  'U'
  56.         {    0xAA,         1,       0x8346      },  //  'e'
  57.         {    0xB4,         1,       0x8347      },  //  'E'
  58.         {    0xAB,         1,       0x8348      },  //  'o'
  59.         {    0xB5,         1,       0x8349      },  //  'O'
  60.  
  61.         {    0xB6,         2,       0x834A      },  //  'KA'
  62.         {    0xB7,         2,       0x834C      },  //  'KI'
  63.         {    0xB8,         2,       0x834E      },  //  'KU'
  64.         {    0xB9,         2,       0x8350      },  //  'KE'
  65.         {    0xBA,         2,       0x8352      },  //  'KO'
  66.  
  67.         {    0xBB,         2,       0x8354      },  //  'SA'
  68.         {    0xBC,         2,       0x8356      },  //  'SI'
  69.         {    0xBD,         2,       0x8358      },  //  'SU'
  70.         {    0xBE,         2,       0x835A      },  //  'SE'
  71.         {    0xBF,         2,       0x835C      },  //  'SO'
  72.  
  73.         {    0xC0,         2,       0x835E      },  //  'TA'
  74.         {    0xC1,         2,       0x8360      },  //  'CHI'
  75.         {    0xAF,         1,       0x8362      },  //  'tsu'
  76.         {    0xC2,         2,       0x8363      },  //  'TSU'
  77.         {    0xC3,         2,       0x8365      },  //  'TE''
  78.         {    0xC4,         2,       0x8367      },  //  'TO''
  79.  
  80.         {    0xC5,         1,       0x8369      },  //  'NA'
  81.         {    0xC6,         1,       0x836A      },  //  'NI'
  82.         {    0xC7,         1,       0x836B      },  //  'NU'
  83.         {    0xC8,         1,       0x836C      },  //  'NE'
  84.         {    0xC9,         1,       0x836D      },  //  'NO'
  85.  
  86.         {    0xCA,         3,       0x836E      },  //  'HA'
  87.         {    0xCB,         3,       0x8371      },  //  'HI'
  88.         {    0xCC,         3,       0x8374      },  //  'FU'
  89.         {    0xCD,         3,       0x8377      },  //  'HE'
  90.         {    0xCE,         3,       0x837A      },  //  'HO'
  91.  
  92.         {    0xCF,         1,       0x837D      },  //  'MA'
  93.         {    0xD0,         1,       0x837E      },  //  'MI'
  94.         {    0xD1,         1,       0x8380      },  //  'MU'
  95.         {    0xD2,         1,       0x8381      },  //  'ME'
  96.         {    0xD3,         1,       0x8382      },  //  'MO'
  97.  
  98.         {    0xAC,         1,       0x8383      },  //  'ya'
  99.         {    0xD4,         1,       0x8384      },  //  'YA'
  100.         {    0xAD,         1,       0x8385      },  //  'yu'
  101.         {    0xD5,         1,       0x8386      },  //  'YU'
  102.         {    0xAE,         1,       0x8387      },  //  'yo'
  103.         {    0xD6,         1,       0x8388      },  //  'YO'
  104.  
  105.         {    0xD7,         1,       0x8389      },  //  'RA'
  106.         {    0xD8,         1,       0x838A      },  //  'RI'
  107.         {    0xD9,         1,       0x838B      },  //  'RU'
  108.         {    0xDA,         1,       0x838C      },  //  'RE'
  109.         {    0xDB,         1,       0x838D      },  //  'RO'
  110.  
  111.         {    0xDC,         2,       0x838E      },  //  'WA'
  112.         {    0xB2,         1,       0x8390      },  //  'I'
  113.         {    0xB4,         1,       0x8391      },  //  'E'
  114.  
  115.         {    0xA6,         1,       0x8392      },  //  'WO'
  116.         {    0xDD,         1,       0x8393      },  //  'N'
  117.  
  118.         {    0xB3,         1,       0x8394      },  //  'U'
  119.         {    0xB6,         1,       0x8395      },  //  'KA'
  120.         {    0xB9,         1,       0x8396      },  //  'KE'
  121.  
  122. // Hiragana Table
  123.         {    0xA7,         1,       0x829F      },  //  'a'
  124.         {    0xB1,         1,       0x82A0      },  //  'A'
  125.         {    0xA8,         1,       0x82A1      },  //  'i'
  126.         {    0xB2,         1,       0x82A2      },  //  'I'
  127.         {    0xA9,         1,       0x82A3      },  //  'u'
  128.         {    0xB3,         1,       0x82A4      },  //  'U'
  129.         {    0xAA,         1,       0x82A5      },  //  'e'
  130.         {    0xB4,         1,       0x82A6      },  //  'E'
  131.         {    0xAB,         1,       0x82A7      },  //  'o'
  132.         {    0xB5,         1,       0x82A8      },  //  'O'
  133.  
  134.         {    0xB6,         2,       0x82A9      },  //  'KA'
  135.         {    0xB7,         2,       0x82AB      },  //  'KI'
  136.         {    0xB8,         2,       0x82AD      },  //  'KU'
  137.         {    0xB9,         2,       0x82AF      },  //  'KE'
  138.         {    0xBA,         2,       0x82B1      },  //  'KO'
  139.  
  140.         {    0xBB,         2,       0x82B3      },  //  'SA'
  141.         {    0xBC,         2,       0x82B5      },  //  'SI'
  142.         {    0xBD,         2,       0x82B7      },  //  'SU'
  143.         {    0xBE,         2,       0x82B9      },  //  'SE'
  144.         {    0xBF,         2,       0x82BB      },  //  'SO'
  145.  
  146.         {    0xC0,         2,       0x82BD      },  //  'TA'
  147.         {    0xC1,         2,       0x82BF      },  //  'CHI'
  148.         {    0xAF,         1,       0x82C1      },  //  'tsu'
  149.         {    0xC2,         2,       0x82C2      },  //  'TSU'
  150.         {    0xC3,         2,       0x82C4      },  //  'TE'
  151.         {    0xC4,         2,       0x82C6      },  //  'TO'
  152.  
  153.         {    0xC5,         1,       0x82C8      },  //  'NA'
  154.         {    0xC6,         1,       0x82C9      },  //  'NI'
  155.         {    0xC7,         1,       0x82CA      },  //  'NU'
  156.         {    0xC8,         1,       0x82CB      },  //  'NE'
  157.         {    0xC9,         1,       0x82CC      },  //  'NO'
  158.  
  159.         {    0xCA,         3,       0x82CD      },  //  'HA'
  160.         {    0xCB,         3,       0x82D0      },  //  'HI'
  161.         {    0xCC,         3,       0x82D3      },  //  'FU'
  162.         {    0xCD,         3,       0x82D6      },  //  'HE'
  163.         {    0xCE,         3,       0x82D9      },  //  'HO'
  164.  
  165.         {    0xCF,         1,       0x82DC      },  //  'MA'
  166.         {    0xD0,         1,       0x82DD      },  //  'MI'
  167.         {    0xD1,         1,       0x82DE      },  //  'MU'
  168.         {    0xD2,         1,       0x82DF      },  //  'ME'
  169.         {    0xD3,         1,       0x82E0      },  //  'MO'
  170.  
  171.         {    0xAC,         1,       0x82E1      },  //  'ya'
  172.         {    0xD4,         1,       0x82E2      },  //  'YA'
  173.         {    0xAD,         1,       0x82E3      },  //  'yu'
  174.         {    0xD5,         1,       0x82E4      },  //  'YU'
  175.         {    0xAE,         1,       0x82E5      },  //  'yo'
  176.         {    0xD6,         1,       0x82E6      },  //  'YO'
  177.  
  178.         {    0xD7,         1,       0x82E7      },  //  'RA'
  179.         {    0xD8,         1,       0x82E8      },  //  'RI'
  180.         {    0xD9,         1,       0x82E9      },  //  'RU'
  181.         {    0xDA,         1,       0x82EA      },  //  'RE'
  182.         {    0xDB,         1,       0x82EB      },  //  'RO'
  183.  
  184.         {    0xDC,         2,       0x82EC      },  //  'WA'
  185.         {    0xB2,         1,       0x82EE      },  //  'I'
  186.         {    0xB4,         1,       0x82EF      },  //  'E'
  187.  
  188.         {    0xA6,         1,       0x82F0      },  //  'WO'
  189.         {    0xDD,         1,       0x82F1      },  //  'N'
  190.  
  191.         {    0x20,         1,       0x8140      },  // ' '
  192. //      {    0xA0,         1,       0x8140      },  // ' '
  193.         {    0xA1,         1,       0x8142      },  //
  194.         {    0xA2,         1,       0x8175      },  //
  195.         {    0xA3,         1,       0x8176      },  //
  196.         {    0xA4,         1,       0x8141      },  //
  197.         {    0xA5,         1,       0x8145      },  //
  198.         {    0xB0,         1,       0x815b      },  //  '-'
  199.         {    0xDE,         1,       0x814a      },  //
  200.         {    0xDF,         1,       0x814b      },  //
  201.  
  202.         {    0,            0,       0           }   // == End of Table
  203.  
  204. };
  205.  
  206. /***
  207. *unsigned int _mbbtombc(c) - convert mbbvalue to mbcvalue.
  208. *
  209. *Purpose:
  210. *       Converts mbbvalue (1-byte) to corresponding mbcvalue code (2-byte).
  211. *
  212. *Entry:
  213. *       unsigned int c - mbbvalue character code to be converted.
  214. *
  215. *Exit:
  216. *       Returns corresponding mbbvalue (2-byte).
  217. *
  218. *Exceptions:
  219. *       Returns c if corresponding 2-byte code does not exist.
  220. *
  221. *******************************************************************************/
  222.  
  223. unsigned int __cdecl _mbbtombc(
  224.     unsigned int c
  225.     )
  226. {
  227.     int i;
  228.  
  229.     if (__mbcodepage != _KANJI_CP)
  230.         return (c);
  231.  
  232.     /* If c is in the ASCII range, then look up the corresponding value
  233.      * in the mbbtable. */
  234.  
  235.     if (c >= ASCLOW && c <= ASCHIGH)
  236.         return (mbbtable[c-ASCLOW]);
  237.  
  238.     /* Exception for KANJI */
  239.  
  240.     if (c == 0xdc)
  241.         return( 0x838f );
  242.  
  243.     /* If c is a Katakana character, lookup in mbctable. */
  244.  
  245.     if (c >= SBLOW && c <= SBHIGH)
  246.     {
  247.         for(i = 0; mbctable[i].asc != 0; i++)
  248.         {
  249.             if ( c == (unsigned int)mbctable[i].asc ) {
  250.                 c = (unsigned int)mbctable[i].mbccode ;
  251.                 break;
  252.                     }
  253.         }
  254.     }
  255.  
  256.         return(c);
  257. }
  258.  
  259.  
  260. /***
  261. *unsigned int _mbctombb(c) - convert mbcvalue to mbbvalue.
  262. *
  263. *Purpose:
  264. *       Converts mbcvalue (2-byte) to corresponding mbbvalue (1-byte).
  265. *
  266. *Entry:
  267. *       unsigned int c - mbcvalue character code to convert.
  268. *
  269. *Exit:
  270. *       Returns corresponding mbbvalue (1-byte).
  271. *
  272. *Exceptions:
  273. *       Returns c if corresponding 1-byte code does not exist.
  274. *
  275. *******************************************************************************/
  276.  
  277. unsigned int __cdecl _mbctombb(
  278.     unsigned int c
  279.     )
  280. {
  281.    int i;
  282.    int result;
  283.  
  284.     if (__mbcodepage != _KANJI_CP)
  285.         return (c);
  286.  
  287.    /* Check to see if c is in the ASCII range.  */
  288.  
  289.     for (i = 0; i <= ASCHIGH - ASCLOW; i++)
  290.     {
  291.         if (c == (unsigned int)mbbtable[i])
  292.             return((unsigned int)i + ASCLOW);
  293.     }
  294.  
  295.  
  296.    /*  If c is a valid MBCS value, search the mbctable for value. */
  297.  
  298.     if ( c <= MBLIMIT )
  299.     {
  300.         for (i = 0; mbctable[i].asc ; i++)
  301.         {
  302.             if ( !(result = (int)c - (int)mbctable[i].mbccode) )
  303.                 return( (unsigned int)mbctable[i].asc );
  304.             else if (((c & 0xff00) == (unsigned int)(mbctable[i].mbccode & 0xff00))
  305.             && (result > 0)
  306.             && ((result - mbctable[i].synonym) < 0))
  307.                 return( (unsigned int)mbctable[i].asc );
  308.         }
  309.     }
  310.  
  311.     return(c);
  312. }
  313.  
  314. #endif  /* _MBCS */
  315.