Logo Search packages:      
Sourcecode: fcitx version File versions  Download package

char* ConvertGBKSimple2Tradition ( char *  strHZ )

该函数装载data/gbks2t.tab的简体转繁体的码表, 然后按码表将GBK字符转换成GBK繁体字符。

WARNING: 该函数返回新分配内存字符串,请调用者 注意释放。

Definition at line 1787 of file tools.c.

{
    FILE           *fp;
    char           *ret;
    char            strPath[PATH_MAX];
    int             i, len;
    unsigned int    idx;

    if (strHZ == NULL)
      return NULL;

    if (!gGBKS2TTable) {
      len = 0;

      strcpy (strPath, PKGDATADIR "/data/");
      strcat (strPath, TABLE_GBKS2T);
      fp = fopen (strPath, "rb");
      if (!fp) {
          ret = (char *) malloc (sizeof (char) * (strlen (strHZ) + 1));
          strcpy (ret, strHZ);
          return ret;
      }

      fseek (fp, 0, SEEK_END);
      fgetpos (fp, (fpos_t *) & len);
      if (len > 0) {
          gGBKS2TTable = (char *) malloc (sizeof (char) * len);
          gGBKS2TTableSize = len;
          fseek (fp, 0, SEEK_SET);
          fread (gGBKS2TTable, sizeof (char), len, fp);
      }
      fclose (fp);
    }

    i = 0;
    len = strlen (strHZ);
    ret = (char *) malloc (sizeof (char) * (len + 1));
    for (; i < len; ++i) {
      if (i < (len - 1))
          if ((unsigned char) strHZ[i] >= (unsigned char) 0x81
            && (unsigned char) strHZ[i] <= (unsigned char) 0xfe &&
            (((unsigned char) strHZ[i + 1] >= (unsigned char) 0x40 && (unsigned char) strHZ[i + 1] <= (unsigned char) 0x7e) || ((unsigned char) strHZ[i + 1] > (unsigned char) 0x7f && (unsigned char) strHZ[i + 1] <= (unsigned char) 0xfe))) {
            idx = (((unsigned char) strHZ[i] - (unsigned char) 0x81)
                   * (unsigned char) 0xbe + ((unsigned char) strHZ[i + 1] - (unsigned char) 0x40)
                   - ((unsigned char) strHZ[i + 1] / (unsigned char) 0x80)) * 2;
            if (idx >= 0 && idx < gGBKS2TTableSize - 1) {
                //if ((unsigned char)gGBKS2TTable[idx] != (unsigned char)0xa1 && (unsigned char) gGBKS2TTable[idx + 1] != (unsigned char) 0x7f) {
                if ((unsigned char) gGBKS2TTable[idx + 1] != (unsigned char) 0x7f) {
                  ret[i] = gGBKS2TTable[idx];
                  ret[i + 1] = gGBKS2TTable[idx + 1];
                  i += 1;
                  continue;
                }
            }
          }
      ret[i] = strHZ[i];
    }
    ret[len] = '\0';

    return ret;
}

Generated by  Doxygen 1.6.0   Back to index