New weird syntax
[kugel-rb.git] / tools / binlang
blob7cfa56222729f307931e4ae124705cdadd3f45e6
1 #!/usr/bin/env perl
2 ############################################################################
3 # __________ __ ___.
4 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 # \/ \/ \/ \/ \/
9 # $Id$
11 # Copyright (C) 2002 by Daniel Stenberg <daniel@haxx.se>
13 # All files in this archive are subject to the GNU General Public License.
14 # See the file COPYING in the source tree root for full license agreement.
16 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 # KIND, either express or implied.
19 ############################################################################
21 if(!$ARGV[0] || !$ARGV[1] || !$ARGV[2]) {
22 print <<MOO
23 Usage: binlang <english file> <language file> <output file>
25 Generate a binary language file.
26 MOO
28 exit;
31 if($ARGV[0] eq "-v") {
32 shift @ARGV;
33 $debug=1;
36 my $english = $ARGV[0];
37 my $input = $ARGV[1];
38 my $output = $ARGV[2];
40 my $idnum=0;
42 open(ENG, "<$english") or die "Can't open $english";
43 open(LANG, "<$input") or die "Can't open $input";
44 open(OUTF, ">$output") or die "Can't open $output";
46 my $langversion = 2;
48 binmode OUTF;
50 printf OUTF ("\x1a%c", $langversion); # magic lang file header
53 # We scan the english file to get the correct order of the id numbers
55 my $idnum=0; # start with a true number
56 while(<ENG>) {
57 if($_ =~ / *\#/) {
58 # comment
59 next;
61 # get rid of DOS newlines
62 $_ =~ s/\r//g;
63 if($_ =~ /^ *([a-z]+): *(.*)/) {
64 ($var, $value) = ($1, $2);
65 $set{$var} = $value;
67 # "new" is always the last one, so now we have them all
68 if($var eq "new") {
69 $value = $set{'eng'};
71 if($value =~ s/^\"(.*)\"\s*$/$1/g) {
72 # Skip voice-only entries
73 if(!$value && $set{'voice'} ne "\"\"") {
74 $idnum{$set{'id'}} = '_done_';
75 next;
78 # Assign an ID number to this entry
79 $idnum{$set{'id'}}=$idnum;
80 $idnum++;
82 undef %set;
86 close(ENG);
88 while(<LANG>) {
89 if($_ =~ /^ *\#/) {
90 # comment
91 next;
93 # get rid of DOS newlines
94 $_ =~ s/\r//g;
95 if($_ =~ /^ *([a-z]+): *(.*)/) {
96 ($var, $value) = ($1, $2);
98 $set{$var} = $value;
100 # "new" is always the last one, so now we have them all
101 if($var eq "new") {
102 $idnum = $idnum{$set{'id'}};
104 # Skip already processed entries (like voice-only ones)
105 next if($idnum eq '_done_');
107 if(!$value) {
108 # if not set, get the english version
109 $value = $set{'eng'};
112 if($value =~ s/^\"(.*)\"\s*$/$1/g) {
113 if($idnum eq "") {
114 warn "Found no ".$set{'id'}." in english file!\n";
116 else {
117 $idnum{$set{'id'}} = '_done_';
119 printf OUTF ("%c%c%s\x00",
120 ($idnum>>8), ($idnum&0xff),
121 $value);
122 if($debug) {
123 printf("%02x => %s\n", $idnum, $value);
127 else {
128 warn "String for ".$set{'id'}." misses quotes\n";
131 undef %set;
137 close(LANG);
139 close(OUTF);
141 foreach $k (keys(%idnum))
143 if($idnum{$k} ne '_done_')
145 warn "Missing ID in $input: $k\n";