Human68k CUI emulator with sound.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sjisranges.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. $sjis = array();
  3. $unicode = array();
  4. $files = $argc < 2 ? array('sjis.txt') : array_slice($argv, 1);
  5. foreach($files as $file) {
  6. $f = fopen($file, "r");
  7. if(!$f) die("Could not open $sjisTxt");
  8. while(!feof($f)) {
  9. $line = fgets($f);
  10. if(preg_match('#^0x(?<sjis>[0-9a-f]{2,4})\s+(0x|U\+)(?<unicode>[0-9a-f]{4,})#i', $line, $match)) {
  11. $sjis[intval($match['sjis'], 16)] = intval($match['unicode'], 16);
  12. $unicode[intval($match['unicode'], 16)] = intval($match['sjis'], 16);
  13. }
  14. }
  15. fclose($f);
  16. }
  17. echo "#include <stdint.h>\n\n";
  18. generate_function($sjis, 'sjis', 'unicode');
  19. generate_function($unicode, 'unicode', 'sjis');
  20. function generate_function($data, $prefix, $to) {
  21. ksort($data);
  22. $ranges = array();
  23. // fill small gaps
  24. $prev = 0;
  25. foreach($data as $code => $unicode) {
  26. if($code - $prev > 1 && $code - $prev < 15) {
  27. for($i = $prev+1; $i < $code; $i++)
  28. $data[$i] = 0x55;
  29. }
  30. $prev = $code;
  31. }
  32. ksort($data);
  33. $prev = 0;
  34. $start = 0;
  35. $data[99999999] = null; // force end condition for last range
  36. $total = 0;
  37. foreach($data as $code => $unicode) {
  38. if($prev != $code-1) {
  39. if($code > 0 && $prev > 0) {
  40. $s = array_search(intval($start), array_keys($data));
  41. $e = array_search(intval($prev), array_keys($data));
  42. $l = $e - $s + 1;
  43. $ranges[] = array(
  44. 'start' => $start,
  45. 'end' => $prev,
  46. 's' => $s,
  47. 'e' => $e,
  48. 'data' => array_slice($data, $s, $l),
  49. 'table_start' => $total,
  50. 'table_end' => $total + $l,
  51. );
  52. $total += $l;
  53. }
  54. $start = $code;
  55. }
  56. $prev = $code;
  57. }
  58. $src_width = $prefix == 'unicode' ? 32 : 16;
  59. $dst_width = $prefix == 'unicode' ? 16 : 32;
  60. echo "static uint{$dst_width}_t {$prefix}_table[] = {\n";
  61. foreach($ranges as $range) {
  62. printf("\t /* 0x%04x - 0x%04x */", $range['start'], $range['end']);
  63. foreach($range['data'] as $k => $d) {
  64. if($k % 8 == 0)
  65. echo "\n\t";
  66. else echo ' ';
  67. printf('0x%04x,', $d);
  68. }
  69. echo "\n";
  70. }
  71. echo "};\n";
  72. echo "uint{$dst_width}_t {$prefix}_char_to_$to(uint{$src_width}_t $prefix) {\n";
  73. foreach($ranges as $k => $range) {
  74. printf("\tif($prefix >= 0x%04x && $prefix <= 0x%04x)", $range['start'], $range['end']);
  75. printf(" return {$prefix}_table[0x%04x + $prefix - 0x%04x];\n", $range['table_start'], $range['start']);
  76. }
  77. if($to == 'sjis')
  78. echo "\treturn 0x81a6; // ※\n}\n";
  79. else
  80. echo "\treturn 0x203b; // ※\n}\n";
  81. }