summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtools/genlang2252
1 files changed, 252 insertions, 0 deletions
diff --git a/tools/genlang2 b/tools/genlang2
new file mode 100755
index 0000000000..6db7d39a45
--- /dev/null
+++ b/tools/genlang2
@@ -0,0 +1,252 @@
1#!/usr/bin/perl -s
2
3if(!$ARGV[0]) {
4 print <<MOO
5Usage: genlang2 [-p=<prefix>][-t=<target>][-v] <language file>
6
7<prefix>.h and <prefix>.c will be created in the current directory. <prefix>
8is "lang" by default.
9
10Use -v for verbose (debug) output.
11
12MOO
13;
14 exit;
15}
16
17my $prefix = $p;
18if(!$prefix) {
19 $prefix="lang";
20}
21my $target = $t;
22if(!$target) {
23 print "Please specify a target!\n";
24 exit;
25}
26my $verbose=$v;
27
28my %id; # string to num hash
29my @idnum; # num to string array
30
31my %source; # id string to source phrase hash
32my %dest; # id string to dest phrase hash
33my %voice; # id string to voice phrase hash
34
35
36my $input = $ARGV[0];
37
38open(HFILE, ">$prefix.h");
39open(CFILE, ">$prefix.c");
40
41print HFILE <<MOO
42/* This file was automatically generated using genlang2 */
43/*
44 * The str() macro/functions is how to access strings that might be
45 * translated. Use it like str(MACRO) and expect a string to be
46 * returned!
47 */
48#define str(x) language_strings[x]
49
50/* this is the array for holding the string pointers.
51 It will be initialized at runtime. */
52extern unsigned char *language_strings[];
53/* this contains the concatenation of all strings, separated by \\0 chars */
54extern const unsigned char language_builtin[];
55
56/* The enum below contains all available strings */
57enum {
58MOO
59 ;
60
61print CFILE <<MOO
62/* This file was automaticly generated using genlang2, the strings come
63 from "$input" */
64
65#include "$prefix.h"
66
67unsigned char *language_strings[LANG_LAST_INDEX_IN_ARRAY];
68const unsigned char language_builtin[] =
69MOO
70 ;
71
72my @m;
73my $m="blank";
74
75sub match {
76 my ($string, $pattern)=@_;
77
78 $pattern =~ s/\*/.?*/g;
79 $pattern =~ s/\?/./g;
80
81 return ($string =~ $pattern);
82}
83
84sub blank {
85 # nothing to do
86}
87
88my %head;
89sub header {
90 my ($full, $n, $v)=@_;
91 $head{$n}=$v;
92}
93
94my %phrase;
95sub phrase {
96 my ($full, $n, $v)=@_;
97 $phrase{$n}=$v;
98}
99
100sub parsetarget {
101 my ($debug, $strref, $full, $n, $v)=@_;
102 my $string;
103 my @all= split(" *, *", $n);
104 my $test;
105 for $test (@all) {
106# print "TEST ($debug) $target for $test\n";
107 if(match($target, $test)) {
108 $string = $v;
109# print "MATCH: $test => $v\n";
110 }
111 }
112 if($string) {
113 $$strref = $string;
114 }
115 return $string;
116}
117
118my $src;
119sub source {
120 parsetarget("src", \$src, @_);
121}
122
123my $dest;
124sub dest {
125 parsetarget("dest", \$dest, @_);
126}
127
128my $voice;
129sub voice {
130 parsetarget("voice", \$voice, @_);
131}
132
133my $idcount; # counter for ID numbers
134
135open(LANG, "<$input");
136while(<LANG>) {
137 $line++;
138 if($_ =~ / *\#/) {
139 # comment
140 next;
141 }
142 # get rid of DOS newlines
143 $_ =~ s/\r//g;
144
145 # print "M: $m\n";
146
147 if(/ *<([^>]*)>/) {
148 my $part = $1;
149 #print "P: $part\n";
150 if($part =~ /^\//) {
151 if($part eq "/phrase") {
152 my $idstr = $phrase{'id'};
153
154 $id{$idstr} = $idcount;
155 $idnum[$idcount]=$idstr;
156
157 $source{$idstr}=$src;
158 $dest{$idstr}=$dest;
159 $voice{$idstr}=$voice;
160
161 if($verbose) {
162 print "id: $phrase{id}\n";
163 print "source: $src\n";
164 print "dest: $dest\n";
165 print "voice: $voice\n";
166 }
167
168 $idcount++;
169
170 undef $src;
171 undef $dest;
172 undef $voice;
173 undef %phrase;
174 }
175 # starts with a slash, this _ends_ this section
176 $m = pop @m; # get back old value
177 next;
178 }
179 push @m, $m; # store old value
180 $m = $1;
181 next;
182 }
183
184 if(/^ *([^:]+): *(.*)/) {
185 my ($name, $val)=($1, $2);
186 &$m($_, $name, $val);
187 }
188
189}
190close(LANG);
191
192# Output the ID names for the enum in the header file
193my $i;
194for $i (1 .. $idcount) {
195 my $name=$idnum[$i - 1]; # get the ID name
196
197 $name =~ s/\"//g; # cut off the quotes
198
199 printf HFILE (" %s,\n", $name);
200}
201
202# Output separation marker for last string ID and the upcoming voice IDs
203
204print HFILE <<MOO
205 LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */
206 /* --- below this follows voice-only strings --- */
207 VOICEONLY_DELIMITER = 0x8000,
208MOO
209 ;
210
211# TODO: add voice-only phrase IDs here
212
213# Output end of enum
214print HFILE <<MOO
215};
216/* end of generated enum list */
217MOO
218 ;
219
220# Output the target phrases for the source file
221for $i (1 .. $idcount) {
222 my $name=$idnum[$i - 1]; # get the ID
223 my $dest = $dest{$name}; # get the destination phrase
224
225 $dest =~ s:\"$:\\0\":; # insert a \0 before the second quote
226
227 printf CFILE (" %s\n", $dest);
228}
229
230# Output end of string chunk
231print CFILE <<MOO
232;
233/* end of generated string list */
234MOO
235 ;
236
237close(HFILE);
238close(CFILE);
239
240if($verbose) {
241 printf("%d ID strings scanned\n", $idcount);
242
243 print "* head *\n";
244 for(keys %head) {
245 printf "$_: %s\n", $head{$_};
246 }
247}
248
249#print "* phrase *\n";
250#for(keys %phrase) {
251# print "$_\n";
252#}