1#! /usr/bin/perl -w
2open F, "cat C-translit.h.in | gcc -E - |" || die "Cannot preprocess input file";
3
4
5sub 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
38while (<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
56printf "#define NTRANSLIT %d\n", $#froms + 1;
57
58printf "static const uint32_t translit_from_idx[] =\n{\n  ";
59$col = 2;
60$total = 0;
61for ($cnt = 0; $cnt <= $#fromlens; ++$cnt) {
62  if ($cnt != 0) {
63    if ($col + 7 >= 79) {
64      printf(",\n  ");
65      $col = 2;
66    } else {
67      printf(", ");
68      $col += 2;
69    }
70  }
71  printf("%4d", $total);
72  $total += $fromlens[$cnt] + 1;
73  $col += 4;
74}
75printf("\n};\n");
76
77printf "static const wchar_t translit_from_tbl[] =\n ";
78$col = 1;
79for ($cnt = 0; $cnt <= $#froms; ++$cnt) {
80  if ($cnt != 0) {
81    if ($col + 6 >= 79) {
82      printf("\n ");
83      $col = 1;
84    }
85    printf(" L\"\\0\"");
86    $col += 6;
87  }
88  if ($col > 2 && $col + length($froms[$cnt]) + 4 >= 79) {
89    printf("\n  ");
90    $col = 2;
91  } else {
92    printf(" ");
93    ++$col;
94  }
95  printf("L\"$froms[$cnt]\"");
96  $col += length($froms[$cnt]) + 3;
97}
98printf(";\n");
99
100printf "static const uint32_t translit_to_idx[] =\n{\n  ";
101$col = 2;
102$total = 0;
103for ($cnt = 0; $cnt <= $#tolens; ++$cnt) {
104  if ($cnt != 0) {
105    if ($col + 7 >= 79) {
106      printf(",\n  ");
107      $col = 2;
108    } else {
109      printf(", ");
110      $col += 2;
111    }
112  }
113  printf("%4d", $total);
114  $total += $tolens[$cnt] + 2;
115  $col += 4;
116}
117printf("\n};\n");
118
119printf "static const wchar_t translit_to_tbl[] =\n ";
120$col = 1;
121for ($cnt = 0; $cnt <= $#tos; ++$cnt) {
122  if ($cnt != 0) {
123    if ($col + 6 >= 79) {
124      printf("\n ");
125      $col = 1;
126    }
127    printf(" L\"\\0\"");
128    $col += 6;
129  }
130  if ($col > 2 && $col + length($tos[$cnt]) + 6 >= 79) {
131    printf("\n  ");
132    $col = 2;
133  } else {
134    printf(" ");
135    ++$col;
136  }
137  printf("%s", "L\"$tos[$cnt]\\0\"");
138  $col += length($tos[$cnt]) + 5;
139}
140printf(";\n");
141
142exit 0;
143