summaryrefslogtreecommitdiff
path: root/tools/genlang2
blob: 6db7d39a4528e88f026d506820bbdc7f3d2bf0d8 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/perl -s

if(!$ARGV[0]) {
    print <<MOO
Usage: genlang2 [-p=<prefix>][-t=<target>][-v] <language file>

<prefix>.h and <prefix>.c will be created in the current directory. <prefix>
is "lang" by default.

Use -v for verbose (debug) output.

MOO
;
    exit;
}

my $prefix = $p;
if(!$prefix) {
    $prefix="lang";
}
my $target = $t;
if(!$target) {
    print "Please specify a target!\n";
    exit;
}
my $verbose=$v;

my %id; # string to num hash
my @idnum; # num to string array

my %source; # id string to source phrase hash
my %dest; # id string to dest phrase hash
my %voice; # id string to voice phrase hash


my $input = $ARGV[0];

open(HFILE, ">$prefix.h");
open(CFILE, ">$prefix.c");

print HFILE <<MOO
/* This file was automatically generated using genlang2 */
/*
 * The str() macro/functions is how to access strings that might be
 * translated. Use it like str(MACRO) and expect a string to be
 * returned!
 */
#define str(x) language_strings[x]

/* this is the array for holding the string pointers.
   It will be initialized at runtime. */
extern unsigned char *language_strings[];
/* this contains the concatenation of all strings, separated by \\0 chars */
extern const unsigned char language_builtin[];

/* The enum below contains all available strings */
enum {
MOO
    ;

print CFILE <<MOO
/* This file was automaticly generated using genlang2, the strings come
   from "$input" */
   
#include "$prefix.h"

unsigned char *language_strings[LANG_LAST_INDEX_IN_ARRAY];
const unsigned char language_builtin[] =
MOO
    ;

my @m;
my $m="blank";

sub match {
    my ($string, $pattern)=@_;

    $pattern =~ s/\*/.?*/g;
    $pattern =~ s/\?/./g;

    return ($string =~ $pattern);
}

sub blank {
    # nothing to do
}

my %head;
sub header {
    my ($full, $n, $v)=@_;
    $head{$n}=$v;
}

my %phrase;
sub phrase {
    my ($full, $n, $v)=@_;
    $phrase{$n}=$v;
}

sub parsetarget {
    my ($debug, $strref, $full, $n, $v)=@_;
    my $string;
    my @all= split(" *, *", $n);
    my $test;
    for $test (@all) {
#        print "TEST ($debug) $target for $test\n";
        if(match($target, $test)) {
            $string = $v;
#            print "MATCH: $test => $v\n";
        }
    }
    if($string) {
        $$strref = $string;
    }
    return $string;
}

my $src;
sub source {
    parsetarget("src", \$src, @_);
}

my $dest;
sub dest {
    parsetarget("dest", \$dest, @_);
}

my $voice;
sub voice {
    parsetarget("voice", \$voice, @_);
}

my $idcount; # counter for ID numbers

open(LANG, "<$input");
while(<LANG>) {
    $line++;
    if($_ =~ / *\#/) {
        # comment
        next;
    }
    # get rid of DOS newlines
    $_ =~ s/\r//g;

   # print "M: $m\n";

    if(/ *<([^>]*)>/) {
        my $part = $1;
        #print "P: $part\n";
        if($part =~ /^\//) {
            if($part eq "/phrase") {
                my $idstr = $phrase{'id'};

                $id{$idstr} = $idcount;
                $idnum[$idcount]=$idstr;

                $source{$idstr}=$src;
                $dest{$idstr}=$dest;
                $voice{$idstr}=$voice;

                if($verbose) {
                    print "id: $phrase{id}\n";
                    print "source: $src\n";
                    print "dest: $dest\n";
                    print "voice: $voice\n";
                }

                $idcount++;

                undef $src;
                undef $dest;
                undef $voice;
                undef %phrase;
            }
            # starts with a slash, this _ends_ this section
            $m = pop @m; # get back old value
            next;
        }
        push @m, $m; # store old value
        $m = $1;
        next;
    }

    if(/^ *([^:]+): *(.*)/) {
        my ($name, $val)=($1, $2);
        &$m($_, $name, $val);
    }

}
close(LANG);

# Output the ID names for the enum in the header file
my $i;
for $i (1 .. $idcount) {
    my $name=$idnum[$i - 1]; # get the ID name

    $name =~ s/\"//g; # cut off the quotes

    printf HFILE ("    %s,\n", $name);
}

# Output separation marker for last string ID and the upcoming voice IDs

print HFILE <<MOO
    LANG_LAST_INDEX_IN_ARRAY, /* this is not a string, this is a marker */
    /* --- below this follows voice-only strings --- */
    VOICEONLY_DELIMITER = 0x8000,
MOO
    ;

# TODO: add voice-only phrase IDs here

# Output end of enum
print HFILE <<MOO
};
/* end of generated enum list */
MOO
    ;

# Output the target phrases for the source file
for $i (1 .. $idcount) {
    my $name=$idnum[$i - 1]; # get the ID
    my $dest = $dest{$name}; # get the destination phrase

    $dest =~ s:\"$:\\0\":; # insert a \0 before the second quote

    printf CFILE ("    %s\n", $dest);
}

# Output end of string chunk
print CFILE <<MOO
;
/* end of generated string list */
MOO
    ;

close(HFILE);
close(CFILE);

if($verbose) {
    printf("%d ID strings scanned\n", $idcount);

    print "* head *\n";
    for(keys %head) {
        printf "$_: %s\n", $head{$_};
    }
}

#print "* phrase *\n";
#for(keys %phrase) {
#    print "$_\n";
#}