// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2023 SASANO Takayoshi #include #include #include #include #define TABLE_SIZE 65536 static uint16_t ucs2euc[TABLE_SIZE]; static int input_table(char *filename) { #define jis2euc(x) ((x) | 0x8080) int ret = -1; FILE *fp; char buf[256], *p; long sjis __attribute__((unused)), jis, unicode; if ((fp = fopen(filename, "r")) == NULL) { printf("file open error\n"); goto fin0; } memset(ucs2euc, 0, sizeof(ucs2euc)); while (fgets(buf, sizeof(buf), fp) != NULL) { /* skip comment line */ if (buf[0] == '#') continue; /* get character code numbers */ jis = strtol(buf, &p, 0); if (*p == '\0') continue; unicode = strtol(p, &p, 0); /* skip invalid code */ if (unicode <= 0 || unicode >= TABLE_SIZE || jis <= 0 || jis >= TABLE_SIZE) continue; /* create unicode -> EUC table */ ucs2euc[unicode] = jis2euc(jis); } ret = 0; fclose(fp); fin0: return ret; } static int modify_table(char *filename, char *tag) { int ret = -1; FILE *fp; char buf[256], *p; long src, dst; if ((fp = fopen(filename, "r")) == NULL) { printf("file open error\n"); goto fin0; } while (fgets(buf, sizeof(buf), fp) != NULL) { /* skip comment line */ if (buf[0] == '#') continue; /* record should be started with "U+" */ if (strstr(buf, "U+") != buf) continue; src = strtol(&buf[2], NULL, 16); if ((p = strstr(buf, tag)) == NULL || (p = strstr(p, "U+")) == NULL) continue; dst = strtol(&p[2], NULL, 16); /* link to Simplified char from kTraditionalVariant */ if (ucs2euc[src] && !ucs2euc[dst]) ucs2euc[dst] = ucs2euc[src]; } ret = 0; fclose(fp); fin0: return ret; } static void dump_table(FILE *fp, uint16_t *table, int size) { int i; for (i = 0; i < size; i++) { if (!(i % 8)) fprintf(fp, "\t"); fprintf(fp, "0x%04x, ", table[i]); if (!((i + 1) % 8)) fprintf(fp, "\n"); } } static int output_result(char *filename) { FILE *fp; if ((fp = fopen(filename, "w")) == NULL) return -1; fprintf(fp, "// SPDX-License-Identifier: Unicode-TOU\n\n"); fprintf(fp, "#include \n\n"); fprintf(fp, "uint16_t ucs2euc[%d] = {\n", TABLE_SIZE); dump_table(fp, ucs2euc, TABLE_SIZE); fprintf(fp, "};\n\n"); fclose(fp); return 0; } int main(int argc, char *argv[]) { if (argc < 3) { printf("%s: usage [infile] [outfile] [modfile]\n", argv[0]); goto fin0; } if (input_table(argv[1])) goto fin0; if (argc >= 4) { if (modify_table(argv[3], "kTraditionalVariant") || modify_table(argv[3], "kZVariant")) goto fin0; } output_result(argv[2]); fin0: return 0; }