1 | #!/usr/bin/perl -w |
---|
2 | require 5.004; |
---|
3 | use strict; |
---|
4 | |
---|
5 | use integer; |
---|
6 | |
---|
7 | # Magic identifier (12 bytes) |
---|
8 | my $magic = "Svx\nMsg\r\n\xfe\xff\0"; |
---|
9 | # Designed to be corrupted by ASCII ftp, top bit stripping (or |
---|
10 | # being used for parity). Contains a zero byte so more likely |
---|
11 | # to be flagged as data (e.g. by perl's "-B" test). |
---|
12 | |
---|
13 | my $major = 0; |
---|
14 | my $minor = 8; |
---|
15 | |
---|
16 | # File format (multi-byte numbers in network order (bigendian)): |
---|
17 | # 12 bytes: Magic identifier |
---|
18 | # 1 byte: File format major version number (0) |
---|
19 | # 1 byte: File format minor version number (8) |
---|
20 | # 2 bytes: Number of messages (N) |
---|
21 | # 4 bytes: Offset from XXXX to end of file |
---|
22 | # XXXX: |
---|
23 | # N*: |
---|
24 | # <message> NUL |
---|
25 | |
---|
26 | my %ent = (); |
---|
27 | |
---|
28 | open ENT, "named-entities.txt" or die $!; |
---|
29 | while (<ENT>) { |
---|
30 | my ($e, $v) = /^(\w+),(\d+)/; |
---|
31 | $ent{$e} = $v; |
---|
32 | } |
---|
33 | close ENT; |
---|
34 | |
---|
35 | my %msgs = (); |
---|
36 | |
---|
37 | while (<>) { |
---|
38 | next if /^\s*#/; # skip comments |
---|
39 | |
---|
40 | # en: 0 0.81 the message |
---|
41 | # en-us: 0 0.81 " the message " |
---|
42 | my ($langs, $msgno, $dummy, $msg) = /^([-\w,]+):\s*(\d+)\s+("?)(.*)\3/; |
---|
43 | |
---|
44 | unless (defined $langs) { |
---|
45 | chomp; |
---|
46 | print STDERR "Warning: Bad line: \"$_\"\n"; |
---|
47 | next; |
---|
48 | } |
---|
49 | |
---|
50 | if ($msg =~ /[\0-\x1f\x7f-\xff]/) { |
---|
51 | print STDERR "Warning: literal character in message $msgno\n"; |
---|
52 | } |
---|
53 | |
---|
54 | my $utf8 = string_to_utf8($msg); |
---|
55 | for (split /,/, $langs) { |
---|
56 | ${$msgs{$_}}[$msgno] = $utf8; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | my $lang; |
---|
61 | my @langs = sort grep ! /-\*$/, keys %msgs; |
---|
62 | |
---|
63 | my $num_msgs = -1; |
---|
64 | foreach $lang (@langs) { |
---|
65 | my $aref = $msgs{$lang}; |
---|
66 | $num_msgs = scalar @$aref if scalar @$aref > $num_msgs; |
---|
67 | } |
---|
68 | |
---|
69 | foreach $lang (@langs) { |
---|
70 | open OUT, ">$lang.msg" or die $!; |
---|
71 | |
---|
72 | my $aref = $msgs{$lang}; |
---|
73 | |
---|
74 | my $parentaref; |
---|
75 | my $mainlang = $lang; |
---|
76 | $parentaref = $msgs{$mainlang} if $mainlang =~ s/-.*$//; |
---|
77 | |
---|
78 | print OUT $magic or die $!; |
---|
79 | print OUT pack("CCn", $major, $minor, $num_msgs) or die $!; |
---|
80 | |
---|
81 | my $buff = ''; |
---|
82 | |
---|
83 | my $n; |
---|
84 | for $n (0 .. $num_msgs - 1) { |
---|
85 | my $msg = $$aref[$n]; |
---|
86 | if (!defined $msg) { |
---|
87 | $msg = $$parentaref[$n] if defined $parentaref; |
---|
88 | if (!defined $msg) { |
---|
89 | $msg = ${$msgs{'en'}}[$n]; |
---|
90 | if (defined $msg && $msg ne '') { |
---|
91 | print STDERR "Warning: message $n not in language $lang\n"; |
---|
92 | } else { |
---|
93 | $msg = ''; |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | $buff .= $msg . "\0"; |
---|
98 | } |
---|
99 | |
---|
100 | print OUT pack('N',length($buff)), $buff or die $!; |
---|
101 | close OUT or die $!; |
---|
102 | } |
---|
103 | |
---|
104 | sub string_to_utf8 { |
---|
105 | my $s = shift; |
---|
106 | $s =~ s/[\x80-\xff]/char_to_utf8(ord$1)/eg; |
---|
107 | $s =~ s/\&(#\d+|#x[a-f0-9]+|[a-z0-9]+);?/decode_entity($1)/eig; |
---|
108 | return $s; |
---|
109 | } |
---|
110 | |
---|
111 | sub decode_entity { |
---|
112 | my $ent = shift; |
---|
113 | return char_to_utf8($1) if $ent =~ /^#(\d+)$/; |
---|
114 | return char_to_utf8(hex($1)) if $ent =~ /^#x([a-f0-9]+)$/; |
---|
115 | return char_to_utf8($ent{$ent}) if exists $ent{$ent}; |
---|
116 | $ent = "\&$ent;"; |
---|
117 | print STDERR "Warning: entity \"$ent\" unknown\n"; |
---|
118 | return $ent; |
---|
119 | } |
---|
120 | |
---|
121 | sub char_to_utf8 { |
---|
122 | my $unicode = shift; |
---|
123 | # ASCII is easy, and the most common case |
---|
124 | return chr($unicode) if $unicode < 0x80; |
---|
125 | |
---|
126 | my $result = ''; |
---|
127 | my $n = 0x20; |
---|
128 | while (1) { |
---|
129 | $result = chr(0x80 | ($unicode & 0x3f)) . $result; |
---|
130 | $unicode >>= 6; |
---|
131 | last if $unicode < $n; |
---|
132 | $n >>= 1; |
---|
133 | } |
---|
134 | $result = chr((0x100 - $n*2) | $unicode) . $result; |
---|
135 | return $result; |
---|
136 | } |
---|