aboutsummaryrefslogtreecommitdiff
path: root/locale/gen-translit.pl
blob: d44310b3165fe018fb63bd938daa19ee719f9dab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/perl -w
open F, "cat C-translit.h.in | gcc -E - |" || die "Cannot preprocess input file";


sub cstrlen {
  my($str) = @_;
  my($len) = length($str);
  my($cnt);
  my($res) = 0;

  for ($cnt = 0; $cnt < $len; ++$cnt) {
    if (substr($str, $cnt, 1) eq '\\') {
      # Recognize the escape sequence.
      if (substr($str, $cnt + 1, 1) eq 'x') {
	my($inner);
	for ($inner = $cnt + 2; $inner < $len && $inner < $cnt + 10; ++$inner) {
	  my($ch) = substr($str, $inner, 1);
	  next if (($ch ge '0' && $ch le '9')
		   || ($ch ge 'a' && $ch le 'f')
		   || ($ch ge 'A' && $ch le 'F'));
	  last;
	}
	$cnt = $inner;
	++$res;
      } else {
	die "invalid input" if ($cnt + 1 >= $len);
	++$res;
	++$cnt;
      }
    } else {
      ++$res;
    }
  }

  return $res;
}

while (<F>) {
  next if (/^#/);
  next if (/^[ 	]*$/);
  chop;

  if (/"([^\"]*)"[ 	]*"(.*)"/) {
    my($from) = $1;
    my($to) = $2;
    my($fromlen) = cstrlen($from);
    my($tolen) = cstrlen($to);

    push(@froms, $from);
    push(@fromlens, $fromlen);
    push(@tos, $to);
    push(@tolens, $tolen);
  }
}

printf "#define NTRANSLIT %d\n", $#froms + 1;

printf "static const uint32_t translit_from_idx[] =\n{\n  ";
$col = 2;
$total = 0;
for ($cnt = 0; $cnt <= $#fromlens; ++$cnt) {
  if ($cnt != 0) {
    if ($col + 7 >= 79) {
      printf(",\n  ");
      $col = 2;
    } else {
      printf(", ");
      $col += 2;
    }
  }
  printf("%4d", $total);
  $total += $fromlens[$cnt] + 1;
  $col += 4;
}
printf("\n};\n");

printf "static const wchar_t translit_from_tbl[] =\n ";
$col = 1;
for ($cnt = 0; $cnt <= $#froms; ++$cnt) {
  if ($cnt != 0) {
    if ($col + 6 >= 79) {
      printf("\n ");
      $col = 1;
    }
    printf(" L\"\\0\"");
    $col += 6;
  }
  if ($col > 2 && $col + length($froms[$cnt]) + 4 >= 79) {
    printf("\n  ");
    $col = 2;
  } else {
    printf(" ");
    ++$col;
  }
  printf("L\"$froms[$cnt]\"");
  $col += length($froms[$cnt]) + 3;
}
printf(";\n");

printf "static const uint32_t translit_to_idx[] =\n{\n  ";
$col = 2;
$total = 0;
for ($cnt = 0; $cnt <= $#tolens; ++$cnt) {
  if ($cnt != 0) {
    if ($col + 7 >= 79) {
      printf(",\n  ");
      $col = 2;
    } else {
      printf(", ");
      $col += 2;
    }
  }
  printf("%4d", $total);
  $total += $tolens[$cnt] + 2;
  $col += 4;
}
printf("\n};\n");

printf "static const wchar_t translit_to_tbl[] =\n ";
$col = 1;
for ($cnt = 0; $cnt <= $#tos; ++$cnt) {
  if ($cnt != 0) {
    if ($col + 6 >= 79) {
      printf("\n ");
      $col = 1;
    }
    printf(" L\"\\0\"");
    $col += 6;
  }
  if ($col > 2 && $col + length($tos[$cnt]) + 6 >= 79) {
    printf("\n  ");
    $col = 2;
  } else {
    printf(" ");
    ++$col;
  }
  printf("%s", "L\"$tos[$cnt]\\0\"");
  $col += length($tos[$cnt]) + 5;
}
printf(";\n");

exit 0;