Don't throw away return code.
[kugel-rb.git] / tools / genlang
blob6032c848996459edb54628e957f833d2f258f7d8
1 #!/usr/bin/perl -s
3 if(!$ARGV[0]) {
4 print <<MOO
5 Usage: genlang [-p=<prefix>] <language file>
7 When running this program. <prefix>.h and <prefix>.c will be created in the
8 "current directory". <prefix> is "lang" by default.
9 MOO
11 exit;
14 my $prefix = $p;
15 if(!$prefix) {
16 $prefix="lang";
19 my $input = $ARGV[0];
21 open(HFILE, ">$prefix.h");
22 open(CFILE, ">$prefix.c");
24 print HFILE <<MOO
25 /* This file was automatically generated using genlang */
27 * The str() macro/functions is how to access strings that might be
28 * translated. Use it like str(MACRO) and expect a string to be
29 * returned!
31 #define str(x) language_strings[x]
33 /* this is the array with all the strings */
34 extern unsigned char *language_strings[];
36 /* The enum below contains all available strings */
37 enum {
38 MOO
41 print CFILE <<MOO
42 /* This file was automaticly generated using genlang, the strings come
43 from "$input" */
45 unsigned char *language_strings[]={
46 MOO
49 open(LANG, "<$input");
50 while(<LANG>) {
51 $line++;
52 if($_ =~ / *\#/) {
53 # comment
54 next;
56 # get rid of DOS newlines
57 $_ =~ s/\r//g;
58 if($_ =~ / *([a-z]+): *(.*)/) {
59 ($var, $value) = ($1, $2);
60 # print "$var => $value\n";
62 $set{$var} = $value;
64 if( (($var eq "new") && $value && ($value !~ /^\"(.*)\"\W*$/)) ||
65 (($var eq "voice") && $value && ($value !~ /^\"(.*)\"\W*$/)) ||
66 (($var eq "eng") && ($value !~ /^\"(.*)\"\W*$/)) ) {
67 print "$input:$line:missing quotes for ".$set{'id'}."\n";
68 $errors++;
69 next;
72 if($var eq "new") {
73 # the last one for a single phrase
75 if(!$value || ($value eq "\"\"") ) {
76 # if not set, get the english version
77 $value = $set{'eng'};
79 # print "VOICE: ".$set{'voice'}." VALUE: $value\n";
80 # Note: if both entries are "", the string is deprecated,
81 # but must be included to maintain compatibility
82 if(($value eq "\"\"") && $set{'voice'} ne "\"\"") {
83 # voice-only
84 push @vfile, $set{'id'};
86 else {
87 push @hfile, $set{'id'};
88 print CFILE " $value,\n";
91 undef %set;
97 close(LANG);
99 for(@hfile) {
100 print HFILE " $_,\n";
103 print HFILE " /* --- below this follows voice-only strings --- */\n",
104 " VOICEONLY_DELIMITER = 0x8000,\n";
106 for(@vfile) {
107 print HFILE " $_,\n";
110 print HFILE <<MOO
111 LANG_LAST_INDEX_IN_ARRAY /* this is not a string, this is a marker */
113 /* end of generated enum list */
117 print CFILE <<MOO
119 /* end of generated string list */
123 close(CFILE);
124 close(HFILE);
126 exit $errors;