⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iso2022_jp3.h

📁 libiconv是一个很不错的字符集转换库。程序接口也很简单
💻 H
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 1999-2002 Free Software Foundation, Inc. * This file is part of the GNU LIBICONV Library. * * The GNU LIBICONV Library is free software; you can redistribute it * and/or modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * The GNU LIBICONV Library is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with the GNU LIBICONV Library; see the file COPYING.LIB. * If not, write to the Free Software Foundation, Inc., 59 Temple Place - * Suite 330, Boston, MA 02111-1307, USA. *//* * ISO-2022-JP-3 */#include "jisx0213.h"#define ESC 0x1b/* * The state is composed of one of the following values */#define STATE_ASCII             0  /* Esc ( B */#define STATE_JISX0201ROMAN     1  /* Esc ( J */#define STATE_JISX0201KATAKANA  2  /* Esc ( I */#define STATE_JISX0208          3  /* Esc $ @ or Esc $ B */#define STATE_JISX02131         4  /* Esc $ ( O */#define STATE_JISX02132         5  /* Esc $ ( P *//* * In the ISO-2022-JP-3 to UCS-4 direction, the state also holds the last * character to be output, shifted by 3 bits. */static intiso2022_jp3_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n){  ucs4_t last_wc = conv->istate >> 3;  if (last_wc) {    /* Output the buffered character. */    conv->istate &= 7;    *pwc = last_wc;    return 0; /* Don't advance the input pointer. */  } else {    state_t state = conv->istate;    int count = 0;    unsigned char c;    for (;;) {      c = *s;      if (c == ESC) {        if (n < count+3)          goto none;        if (s[1] == '(') {          if (s[2] == 'B') {            state = STATE_ASCII;            s += 3; count += 3;            if (n < count+1)              goto none;            continue;          }          if (s[2] == 'J') {            state = STATE_JISX0201ROMAN;            s += 3; count += 3;            if (n < count+1)              goto none;            continue;          }          if (s[2] == 'I') {            state = STATE_JISX0201KATAKANA;            s += 3; count += 3;            if (n < count+1)              goto none;            continue;          }          return RET_ILSEQ;        }        if (s[1] == '$') {          if (s[2] == '@' || s[2] == 'B') {            /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */            state = STATE_JISX0208;            s += 3; count += 3;            if (n < count+1)              goto none;            continue;          }          if (s[2] == '(') {            if (n < count+4)              goto none;            if (s[3] == 'O') {              state = STATE_JISX02131;              s += 4; count += 4;              if (n < count+1)                goto none;              continue;            }            if (s[3] == 'P') {              state = STATE_JISX02132;              s += 4; count += 4;              if (n < count+1)                goto none;              continue;            }          }          return RET_ILSEQ;        }        return RET_ILSEQ;      }      break;    }    switch (state) {      case STATE_ASCII:        if (c < 0x80) {          int ret = ascii_mbtowc(conv,pwc,s,1);          if (ret == RET_ILSEQ)            return RET_ILSEQ;          if (ret != 1) abort();          conv->istate = state;          return count+1;        } else          return RET_ILSEQ;      case STATE_JISX0201ROMAN:        if (c < 0x80) {          int ret = jisx0201_mbtowc(conv,pwc,s,1);          if (ret == RET_ILSEQ)            return RET_ILSEQ;          if (ret != 1) abort();          conv->istate = state;          return count+1;        } else          return RET_ILSEQ;      case STATE_JISX0201KATAKANA:        if (c < 0x80) {          unsigned char buf = c+0x80;          int ret = jisx0201_mbtowc(conv,pwc,&buf,1);          if (ret == RET_ILSEQ)            return RET_ILSEQ;          if (ret != 1) abort();          conv->istate = state;          return count+1;        } else          return RET_ILSEQ;      case STATE_JISX0208:        if (n < count+2)          goto none;        if (s[0] < 0x80 && s[1] < 0x80) {          int ret = jisx0208_mbtowc(conv,pwc,s,2);          if (ret == RET_ILSEQ)            return RET_ILSEQ;          if (ret != 2) abort();          conv->istate = state;          return count+2;        } else          return RET_ILSEQ;      case STATE_JISX02131:      case STATE_JISX02132:        if (n < count+2)          goto none;        if (s[0] < 0x80 && s[1] < 0x80) {          ucs4_t wc = jisx0213_to_ucs4(((state-STATE_JISX02131+1)<<8)+s[0],s[1]);          if (wc) {            if (wc < 0x80) {              /* It's a combining character. */              ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];              ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];              /* We cannot output two Unicode characters at once. So,                 output the first character and buffer the second one. */              *pwc = wc1;              conv->istate = (wc2 << 3) | state;            } else {              *pwc = wc;              conv->istate = state;            }            return count+2;          }        }        return RET_ILSEQ;      default: abort();    }  none:    conv->istate = state;    return RET_TOOFEW(count);  }}static intiso2022_jp3_flushwc (conv_t conv, ucs4_t *pwc){  ucs4_t last_wc = conv->istate >> 3;  if (last_wc) {    /* Output the buffered character. */    conv->istate &= 7;    *pwc = last_wc;    return 1;  } else    return 0;}/* * In the UCS-4 to ISO-2022-JP-3 direction, the state also holds the last two * bytes to be output, shifted by 3 bits, and the STATE_xxxxx value that was * effective before this buffered character, shifted by 19 bits. *//* Composition tables for each of the relevant combining characters.  */static const struct { unsigned short base; unsigned short composed; } iso2022_jp3_comp_table_data[] = {#define iso2022_jp3_comp_table02e5_idx 0#define iso2022_jp3_comp_table02e5_len 1  { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */#define iso2022_jp3_comp_table02e9_idx (iso2022_jp3_comp_table02e5_idx+iso2022_jp3_comp_table02e5_len)#define iso2022_jp3_comp_table02e9_len 1  { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */#define iso2022_jp3_comp_table0300_idx (iso2022_jp3_comp_table02e9_idx+iso2022_jp3_comp_table02e9_len)#define iso2022_jp3_comp_table0300_len 5  { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */  { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */  { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */  { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */  { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */#define iso2022_jp3_comp_table0301_idx (iso2022_jp3_comp_table0300_idx+iso2022_jp3_comp_table0300_len)#define iso2022_jp3_comp_table0301_len 4  { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */  { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */  { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */  { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */#define iso2022_jp3_comp_table309a_idx (iso2022_jp3_comp_table0301_idx+iso2022_jp3_comp_table0301_len)#define iso2022_jp3_comp_table309a_len 14  { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */  { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */  { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */  { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */  { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */  { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */  { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */  { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */  { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */  { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */  { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */  { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */  { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */  { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */};#define SPLIT_STATE \  unsigned short lasttwo = state >> 3; state_t prevstate = state >> 19; state &= 7#define COMBINE_STATE \  state |= (prevstate << 19) | (lasttwo << 3)#define COMBINE_STATE_NO_LASTTWO \  /* assume lasttwo == 0, then prevstate is ignored */static intiso2022_jp3_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n){  int count = 0;  unsigned char buf[2];  unsigned short jch;  int ret;  state_t state = conv->ostate;  SPLIT_STATE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -