Advertisement
uaa

UCS-2 <- -> sj3 wchar16_t table generator (under development)

uaa
Jan 27th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2024 SASANO Takayoshi <[email protected]>
  3. // cc create_ucs2w16.c -Wall -I/usr/local/include -L/usr/local/lib -liconv -o create_ucs2w16
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <iconv.h>
  8.  
  9. #define SS2 0x8e
  10. #define SS3 0x8f
  11.  
  12. #define TABLE_SIZE 65536
  13.  
  14. static unsigned short ucs2tow16[TABLE_SIZE];
  15. static unsigned short w16toucs2[TABLE_SIZE];
  16.  
  17. static int create_table(void)
  18. {
  19. iconv_t cd;
  20. int ucs, w16;
  21. unsigned char in[2], out[4];
  22. char *in_p, *out_p;
  23. ssize_t n;
  24. size_t insize, outsize;
  25.  
  26. memset(ucs2tow16, 0, sizeof(ucs2tow16));
  27. memset(w16toucs2, 0, sizeof(w16toucs2));
  28.  
  29. if ((cd = iconv_open("EUC-JP", "UCS-2BE")) < 0) {
  30. printf("iconv_open\n");
  31. return -1;
  32. }
  33.  
  34. for (ucs = 0; ucs < 0x10000; ucs++) {
  35. /* skip C0/C1 control, surrogate, private area */
  36. if ((ucs >= 0x0000 && ucs <= 0x001f) ||
  37. (ucs >= 0x0080 && ucs <= 0x009f) ||
  38. (ucs >= 0xd800 && ucs <= 0xdfff) ||
  39. (ucs >= 0xe000 && ucs <= 0xf8ff))
  40. continue;
  41.  
  42. in[0] = ucs >> 8;
  43. in[1] = ucs;
  44.  
  45. in_p = (char *)in;
  46. out_p = (char *)out;
  47. insize = sizeof(in);
  48. outsize = sizeof(out);
  49.  
  50. if ((n = iconv(cd, &in_p, &insize, &out_p, &outsize)) < 0)
  51. continue;
  52.  
  53. switch (out[0]) {
  54. case 0x00 ... 0x7f:
  55. w16 = out[0];
  56. break;
  57. case SS2:
  58. w16 = out[1];
  59. break;
  60. case SS3:
  61. w16 = (out[1] << 8) | (out[2] & 0x7f);
  62. break;
  63. default:
  64. w16 = (out[0] << 8) | out[1];
  65. break;
  66. }
  67.  
  68. w16toucs2[w16] = ucs;
  69. ucs2tow16[ucs] = w16;
  70. }
  71.  
  72. iconv_close(cd);
  73. return 0;
  74. }
  75.  
  76. static void dump_table(FILE *fp, uint16_t *table, int size)
  77. {
  78. int i;
  79.  
  80. for (i = 0; i < size; i++) {
  81. if (!(i % 8)) fprintf(fp, "\t");
  82. fprintf(fp, "0x%04x, ", table[i]);
  83. if (!((i + 1) % 8)) fprintf(fp, "\n");
  84. }
  85. }
  86.  
  87. static int output_result(char *filename)
  88. {
  89. FILE *fp;
  90.  
  91. if ((fp = fopen(filename, "w")) == NULL)
  92. return -1;
  93.  
  94. fprintf(fp, "// SPDX-License-Identifier: Unlicense\n\n");
  95.  
  96. fprintf(fp, "const unsigned short ucs2tow16[%d] = {\n", TABLE_SIZE);
  97. dump_table(fp, ucs2tow16, TABLE_SIZE);
  98. fprintf(fp, "};\n\n");
  99.  
  100. fprintf(fp, "const unsigned short w16toucs2[%d] = {\n", TABLE_SIZE);
  101. dump_table(fp, w16toucs2, TABLE_SIZE);
  102. fprintf(fp, "};\n\n");
  103.  
  104. fclose(fp);
  105.  
  106. return 0;
  107. }
  108.  
  109. int main(int argc, char *argv[])
  110. {
  111. if (argc < 2) {
  112. printf("%s: usage [outfile]\n", argv[0]);
  113. goto fin0;
  114. }
  115.  
  116. if (create_table())
  117. goto fin0;
  118.  
  119. output_result(argv[1]);
  120.  
  121. fin0:
  122. return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement