euc_tw.h
Upload User: yingmei828
Upload Date: 2007-01-01
Package Size: 1646k
Code Size: 2k
Development Platform:

Unix_Linux

  1. /*
  2.  * EUC-TW
  3.  */
  4. static int
  5. euc_tw_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  6. {
  7.   unsigned char c = *s;
  8.   /* Code set 0 (ASCII) */
  9.   if (c < 0x80)
  10.     return ascii_mbtowc(conv,pwc,s,n);
  11.   /* Code set 1 (CNS 11643-1992 Plane 1) */
  12.   if (c >= 0xa1 && c < 0xff) {
  13.     if (n < 2)
  14.       return RET_TOOFEW(0);
  15.     {
  16.       unsigned char c2 = s[1];
  17.       if (c2 >= 0xa1 && c2 < 0xff) {
  18.         unsigned char buf[2];
  19.         buf[0] = c-0x80; buf[1] = c2-0x80;
  20.         return cns11643_1_mbtowc(conv,pwc,buf,2);
  21.       } else
  22.         return RET_ILSEQ;
  23.     }
  24.   }
  25.   /* Code set 2 (CNS 11643-1992 Planes 1-16) */
  26.   if (c == 0x8e) {
  27.     if (n < 4)
  28.       return RET_TOOFEW(0);
  29.     {
  30.       unsigned char c2 = s[1];
  31.       if (c2 >= 0xa1 && c2 <= 0xb0) {
  32.         unsigned char c3 = s[2];
  33.         unsigned char c4 = s[3];
  34.         if (c3 >= 0xa1 && c3 < 0xff && c4 >= 0xa1 && c4 < 0xff) {
  35.           unsigned char buf[2];
  36.           int ret;
  37.           buf[0] = c3-0x80; buf[1] = c4-0x80;
  38.           switch (c2-0xa0) {
  39.             case 1: ret = cns11643_1_mbtowc(conv,pwc,buf,2); break;
  40.             case 2: ret = cns11643_2_mbtowc(conv,pwc,buf,2); break;
  41.             case 3: ret = cns11643_3_mbtowc(conv,pwc,buf,2); break;
  42.             default: return RET_ILSEQ;
  43.           }
  44.           if (ret == RET_ILSEQ)
  45.             return RET_ILSEQ;
  46.           if (ret != 2) abort();
  47.           return 4;
  48.         }
  49.       }
  50.     }
  51.   }
  52.   return RET_ILSEQ;
  53. }
  54. static int
  55. euc_tw_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  56. {
  57.   unsigned char buf[3];
  58.   int ret;
  59.   /* Code set 0 (ASCII) */
  60.   ret = ascii_wctomb(conv,r,wc,n);
  61.   if (ret != RET_ILSEQ)
  62.     return ret;
  63.   ret = cns11643_wctomb(conv,buf,wc,3);
  64.   if (ret != RET_ILSEQ) {
  65.     if (ret != 3) abort();
  66.     /* Code set 1 (CNS 11643-1992 Plane 1) */
  67.     if (buf[0] == 0) {
  68.       if (n < 2)
  69.         return RET_TOOSMALL;
  70.       r[0] = buf[1]+0x80;
  71.       r[1] = buf[2]+0x80;
  72.       return 2;
  73.     }
  74.     /* Code set 2 (CNS 11643-1992 Planes 1-16) */
  75.     if (n < 4)
  76.       return RET_TOOSMALL;
  77.     r[0] = 0x8e;
  78.     r[1] = buf[0]+0xa1;
  79.     r[2] = buf[1]+0x80;
  80.     r[3] = buf[2]+0x80;
  81.     return 4;
  82.   }
  83.   return RET_ILSEQ;
  84. }