summaryrefslogtreecommitdiffstats
path: root/tools/genlang
blob: cde23f85fa744b07447b006a54a04c58f97eeab6 (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
#!/usr/bin/perl -s

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

When running this program. <prefix>.h and <prefix>.c will be created in the
"current directory". <prefix> is "lang" by default.
MOO
;
    exit;
}

my $prefix = $p;
if(!$prefix) {
    $prefix="lang";
}

my $input = $ARGV[0];

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

print HFILE <<MOO
/* This file was automatically generated using genlang */
/*
 * 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 genlang, the strings come
   from "$input" */
   
#include "$prefix.h"

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

open(LANG, "<$input");
while(<LANG>) {
    $line++;
    if($_ =~ / *\#/) {
        # comment
        next;
    }
    # get rid of DOS newlines
    $_ =~ s/\r//g;
    if($_ =~ / *([a-z]+): *(.*)/) {
        ($var, $value) = ($1, $2);
        # print "$var => $value\n";

        $set{$var} = $value;

        if( (($var eq "new") && $value && ($value !~ /^\"(.*)\"\W*$/)) ||
            (($var eq "voice") && $value && ($value !~ /^\"(.*)\"\W*$/)) ||
            (($var eq "eng") && ($value !~ /^\"(.*)\"\W*$/)) ) {
            print "$input:$line:missing quotes for ".$set{'id'}."\n";
            $errors++;
            next;
        }

        if($var eq "new") {
            # the last one for a single phrase

            if(!$value || ($value eq "\"\"") ) {
                # if not set, get the english version
                $value = $set{'eng'};
            }
#            print "VOICE: ".$set{'voice'}." VALUE: $value\n";
            # Note: if both entries are "", the string is deprecated,
            # but must be included to maintain compatibility
            if($set{'id'} =~ /^VOICE_/) {
                # voice-only
                push @vfile, $set{'id'};
            }
            else {
                push @hfile, $set{'id'};
                $value =~ s/^\"(.*)\"\W*$/\"$1\\0\"/;
                print CFILE "    $value\n";
            }

            undef %set;
        }

    }

}
close(LANG);

for(@hfile) {
    print HFILE "    $_,\n";
}

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
    ;

for(@vfile) {
    print HFILE "    $_,\n";
}

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

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

close(CFILE);
close(HFILE);

exit $errors;