lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #! /usr/bin/perl -w |
| 2 | open F, "cat C-translit.h.in | gcc -E - |" || die "Cannot preprocess input file"; |
| 3 | |
| 4 | |
| 5 | sub cstrlen { |
| 6 | my($str) = @_; |
| 7 | my($len) = length($str); |
| 8 | my($cnt); |
| 9 | my($res) = 0; |
| 10 | |
| 11 | for ($cnt = 0; $cnt < $len; ++$cnt) { |
| 12 | if (substr($str, $cnt, 1) eq '\\') { |
| 13 | # Recognize the escape sequence. |
| 14 | if (substr($str, $cnt + 1, 1) eq 'x') { |
| 15 | my($inner); |
| 16 | for ($inner = $cnt + 2; $inner < $len && $inner < $cnt + 10; ++$inner) { |
| 17 | my($ch) = substr($str, $inner, 1); |
| 18 | next if (($ch ge '0' && $ch le '9') |
| 19 | || ($ch ge 'a' && $ch le 'f') |
| 20 | || ($ch ge 'A' && $ch le 'F')); |
| 21 | last; |
| 22 | } |
| 23 | $cnt = $inner; |
| 24 | ++$res; |
| 25 | } else { |
| 26 | die "invalid input" if ($cnt + 1 >= $len); |
| 27 | ++$res; |
| 28 | ++$cnt; |
| 29 | } |
| 30 | } else { |
| 31 | ++$res; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return $res; |
| 36 | } |
| 37 | |
| 38 | while (<F>) { |
| 39 | next if (/^#/); |
| 40 | next if (/^[ ]*$/); |
| 41 | chop; |
| 42 | |
| 43 | if (/"([^\"]*)"[ ]*"(.*)"/) { |
| 44 | my($from) = $1; |
| 45 | my($to) = $2; |
| 46 | my($fromlen) = cstrlen($from); |
| 47 | my($tolen) = cstrlen($to); |
| 48 | |
| 49 | push(@froms, $from); |
| 50 | push(@fromlens, $fromlen); |
| 51 | push(@tos, $to); |
| 52 | push(@tolens, $tolen); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | printf "#include <stdint.h>\n"; |
| 57 | |
| 58 | printf "#define NTRANSLIT %d\n", $#froms + 1; |
| 59 | |
| 60 | printf "static const uint32_t translit_from_idx[] =\n{\n "; |
| 61 | $col = 2; |
| 62 | $total = 0; |
| 63 | for ($cnt = 0; $cnt <= $#fromlens; ++$cnt) { |
| 64 | if ($cnt != 0) { |
| 65 | if ($col + 7 >= 79) { |
| 66 | printf(",\n "); |
| 67 | $col = 2; |
| 68 | } else { |
| 69 | printf(", "); |
| 70 | $col += 2; |
| 71 | } |
| 72 | } |
| 73 | printf("%4d", $total); |
| 74 | $total += $fromlens[$cnt] + 1; |
| 75 | $col += 4; |
| 76 | } |
| 77 | printf("\n};\n"); |
| 78 | |
| 79 | printf "static const wchar_t translit_from_tbl[] =\n "; |
| 80 | $col = 1; |
| 81 | for ($cnt = 0; $cnt <= $#froms; ++$cnt) { |
| 82 | if ($cnt != 0) { |
| 83 | if ($col + 6 >= 79) { |
| 84 | printf("\n "); |
| 85 | $col = 1; |
| 86 | } |
| 87 | printf(" L\"\\0\""); |
| 88 | $col += 6; |
| 89 | } |
| 90 | if ($col > 2 && $col + length($froms[$cnt]) + 4 >= 79) { |
| 91 | printf("\n "); |
| 92 | $col = 2; |
| 93 | } else { |
| 94 | printf(" "); |
| 95 | ++$col; |
| 96 | } |
| 97 | printf("L\"$froms[$cnt]\""); |
| 98 | $col += length($froms[$cnt]) + 3; |
| 99 | } |
| 100 | printf(";\n"); |
| 101 | |
| 102 | printf "static const uint32_t translit_to_idx[] =\n{\n "; |
| 103 | $col = 2; |
| 104 | $total = 0; |
| 105 | for ($cnt = 0; $cnt <= $#tolens; ++$cnt) { |
| 106 | if ($cnt != 0) { |
| 107 | if ($col + 7 >= 79) { |
| 108 | printf(",\n "); |
| 109 | $col = 2; |
| 110 | } else { |
| 111 | printf(", "); |
| 112 | $col += 2; |
| 113 | } |
| 114 | } |
| 115 | printf("%4d", $total); |
| 116 | $total += $tolens[$cnt] + 2; |
| 117 | $col += 4; |
| 118 | } |
| 119 | printf("\n};\n"); |
| 120 | |
| 121 | printf "static const wchar_t translit_to_tbl[] =\n "; |
| 122 | $col = 1; |
| 123 | for ($cnt = 0; $cnt <= $#tos; ++$cnt) { |
| 124 | if ($cnt != 0) { |
| 125 | if ($col + 6 >= 79) { |
| 126 | printf("\n "); |
| 127 | $col = 1; |
| 128 | } |
| 129 | printf(" L\"\\0\""); |
| 130 | $col += 6; |
| 131 | } |
| 132 | if ($col > 2 && $col + length($tos[$cnt]) + 6 >= 79) { |
| 133 | printf("\n "); |
| 134 | $col = 2; |
| 135 | } else { |
| 136 | printf(" "); |
| 137 | ++$col; |
| 138 | } |
| 139 | printf("%s", "L\"$tos[$cnt]\\0\""); |
| 140 | $col += length($tos[$cnt]) + 5; |
| 141 | } |
| 142 | printf(";\n"); |
| 143 | |
| 144 | exit 0; |