File: process.pl

package info (click to toggle)
therion 6.3.4-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,544 kB
  • sloc: cpp: 195,273; tcl: 19,779; ansic: 8,434; perl: 1,895; makefile: 1,281; python: 255; asm: 219; sh: 104
file content (379 lines) | stat: -rwxr-xr-x 8,944 bytes parent folder | download
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl
## usage:
## ./process.pl - generate .h sources from texts.txt
## ./process.pl update - find new strings to translate and load all
##   texts_xy.txt into texts.txt
## ./process.pl export[-empty] xy - export texts_xy.txt with texts
##   for translation. If export-empty is used, only untranslated strings
##   are exported

@trans_sources = (
  "../thsymbolset.cxx",
  "../thexpmap.cxx",
  "../thlocale.cxx",
  "../thmapstat.cxx",
  "../thpdf.cxx",
);

sub read_lang_file {
  # nacita subor do hashu a vrati na neho referenciu
  my $fn = shift;
  my $lcode = shift;
  my $src = shift;
  open(INP,"$fn") || die("error: can't open $fn for input\n");
  if (defined($lcode)) {
    $lcode = check_language($lcode);
  } else {
    $lcode = "";
  }
  my %res;
  if (defined($src)) {
    %res = %{$src};
  } else {
    %res = ();
  }
  my $itm = "";
  my $lnum = 0;
  my $cmnt = "";
  while ($ln = <INP>) {
    $lnum++;
#    print "LINE: $ln";
    if ($ln =~ /^\s*(\#.*)/) {
      $cmnt .= "$1\n";
#      print "COMMENT\n";
      next;
    } elsif ($ln =~ /^\s*therion\:\s*(.*\S)\s*$/) {
      $itm = $1;
      $res{$itm}{therion} = $lnum;
      $res{$itm}{"therion-cmnt"} = $cmnt;
      $cmnt = "";
#      print "THERION: $itm\n";
    } elsif (($ln =~ /^\s*(\w+)\:\s*(.*\S)\s*$/) && (length($itm) > 0)) {
      ($lng, $trn) = ($1, $2);
      $clng = check_language($lng);
      if (length($lng) == 0) {
        warn("warning: invalid language \"$lng\" ($fn:$lnum)\n");
      }
      if ((length($lcode) == 0) || (substr($clng,0,2) eq $lcode)) {
        $res{$itm}{$clng} = $trn;
        $res{$itm}{"$clng-cmnt"} = $cmnt;
      }
      $cmnt = "";
#      print "LANGUAGE: $clng => $trn\n";
    } elsif ($ln !~ /^\s*$/) {
      warn("warning: error parsing $fn:$lnum\n");
    }
  }
  close(INP);
  return \%res;
}

sub check_language {
  my $lng = shift;
  if ($lng =~ /^([A-Za-z]{2})([\_\-]{1}([A-Za-z]{2})){0,1}$/) {
    if (length($3) > 0) {
      return (lc($1) . "_" . uc($3))
    } else {
      return (lc($1))
    }
  } else {
    return "";
  }
}

sub write_lang_file {
  my $fn = shift;
  my $href = shift;
  my %hr = %{$href};

  open(OUT,">$fn") || die("error: can't open $fn for output\n");

  my %sh = ();
  for $key (keys %hr) {
#    print "SORTING: $key -> $hr{$key}{therion}\n";
    $sh{$hr{$key}{therion}} = $key;
  }

  @nkeys = sort {$a <=> $b} (keys %sh);
  for $key (@nkeys) {
    print OUT $hr{$sh{$key}}{"therion-cmnt"};
    print OUT "therion: $sh{$key}\n";
    for $lkey (sort keys %{$hr{$sh{$key}}}) {
      if (($lkey !~ /^the/) && ($lkey !~ /\-cmnt/)) {
        print OUT $hr{$sh{$key}}{"$lkey-cmnt"};
        print OUT "$lkey: ". $hr{$sh{$key}}{$lkey} ."\n";
      }
    }
    print OUT "\n";
  }

  close(OUT);

}

sub write_sources {
  my $href = shift;
  my %hr = %{$href};

  # vytvorime si zoznam jazykov a ich alternativ
  my %lngs = ();
  my $key;
  my $lkey;
  foreach $key (keys %hr) {
    foreach $lkey (keys %{$hr{$key}}) {
      if (($lkey !~ /^the/) && ($lkey !~ /\-cmnt/)) {
        $lngs{$lkey} = $lkey;
        if (length($lkey) > 2) {
          $nlkey = substr($lkey,0,2);
          $lngs{$nlkey} = $nlkey;
        }
      }
    }
  }

  my @langs = (sort keys %lngs);
  my $i;
  my $lcode;
  $languages = "enum {\n  THLANG_SYSTEM = -2,\n  THLANG_UNKNOWN = -1,\n";
  $langcxxid = "static const thlang_pchar thlang_cxxids []  = {\n";
  $langparse = "static const thstok thtt_lang [] = {\n";
  for ($i = 0; $i <= $#langs; $i++) {
    $lcode = "THLANG_" . uc($langs[$i]);
    $langcxxid .= "  \"$lcode\",\n";
    $langparse .= "  {\"$langs[$i]\", $lcode},\n";
    $languages .= "  $lcode = $i,\n";
    $lngs{$langs[$i]} = $lcode;
  }
  $languages .= "};\n";
  $langcxxid .= "};\n";
  $langparse .= "  {NULL, THLANG_UNKNOWN},\n};\n";

  my $alternatives = "static const int thlang_alternatives [] = {\n";
  for ($i = 0; $i <= $#langs; $i++) {
    $lkey = $langs[$i];
    if ((length($lkey) > 2) && (defined($lngs{substr($lkey,0,2)}))) {
      $alternatives .= "  " . $lngs{substr($lkey,0,2)} . ",\n";
    } else {
      $alternatives .= "  THLANG_UNKNOWN,\n";
    }
  }
  $alternatives .= "};\n";

  @texts = sort keys %hr;

  $textparse = "static const thstok thtt_texts [" . ($#texts + 2) . "] = {\n";
  $texttable = "static thlang_pchar thlang_translations [" . ($#texts + 1) . "][" . ($#langs + 1) . "] = {\n";
  $i = 0;
  my $nlkey;
  foreach $key (@texts) {
    $textparse .= "  {\"$key\",$i},\n";
    $texttable .= "  {\n";
    # priradi ll_LL -> ll ak ll neni definovane
    foreach $lkey (@langs) {
      if (length($lkey) > 2) {
        $nlkey = substr($lkey,0,2);
        if (!defined($hr{$key}{$nlkey})) {
          $hr{$key}{$nlkey} = $hr{$key}{$lkey};
        }
      }
    }
    foreach $lkey (@langs) {
      if (defined($hr{$key}{$lkey})) {
        $texttable .= "    \"$hr{$key}{$lkey}\",\n";
      } else {
        $texttable .= "    NULL,\n";
      }
    }
    $texttable .= "  },\n";
    $i++;
  }
  $textparse .= "  {NULL, -1},\n};\n";
  $texttable .= "};\n";

  # exportujeme thlangdata.h
  open(OUT,">../thlangdata.h") || die("error: can't open thlangdata.h for output\n");
  print OUT <<ENDOUT;
/**
 * \@file thlangdata.h
 * Therion language translations module.
 *
 * THIS FILE IS GENERATED AUTOMATICALLY, DO NOT MODIFY IT !!!
 */
#pragma once

$languages


ENDOUT
  close(OUT);

  # exportujeme thlangdatafields.h
  open(OUT,">../thlangdatafields.h") || die("error: can't open thlangdatafields.h for output\n");
  print OUT <<ENDOUT;
/**
 * \@file thlangdatafields.h
 * Therion language translations module.
 *
 * THIS FILE IS GENERATED AUTOMATICALLY, DO NOT MODIFY IT !!!
 */

#include "thlangdata.h"
#include "thstok.h"
#include <cstddef>

typedef const char * thlang_pchar;


$langparse


$langcxxid


$alternatives


$textparse


$texttable


ENDOUT

  close(OUT);

}


sub update_todo_list {
  my $href = shift;
  my %hr = %{$href};

  # prejde vsetky kluce a priradi im vysoke cisla
  my $maxnm = -1;
  foreach $key (keys %hr) {
    if ($hr{$key}{therion} > $maxnm) {
      $maxnm = $hr{$key}{therion};
    }
  }
  $maxnm = -1 - $maxnm;

  foreach $key (keys %hr) {
    $hr{$key}{therion} += $maxnm;
  }

  my $i = 0;
  foreach $fn (@trans_sources) {
    print "updating definitions from $fn ...";
    open(INP,"$fn") || die("error: can't open $fn for input\n");
    my @lines = <INP>;
    close(INP);
    my $ln = join('',@lines);
    $ln =~ s/thT\(/\nthT\(/sg;
    @lines = split(/\n/,$ln);
    foreach $ln (@lines) {
      $i++;
      if ($ln =~ /^thT\(\"([^"]+)\"/) {
        $hr{$1}{therion} = $i;
      }
    }
    print " done\n";
  }

  #vypise warningy
  foreach $key (keys %hr) {
    if ($hr{$key}{therion} < 0) {
      warn("warning: expression \"$key\" doesn't need to be translated\n");
    }
  }

  return \%hr;
}


sub backup_file {
  my $fn = shift;

  open(INP,"$fn") || die("error: can't open $fn for input\n");
  my @lines = <INP>;
  close(INP);

  open(OUT,">$fn~") || die("error: can't open $fn~ for output\n");
  print OUT @lines;
  close(OUT);

}

sub export_language {
  (my $fn, my $lng, my $href, my $onlyempty) = (shift, shift, shift, shift);
  my %hr = %{$href};
  open(OUT,">$fn") || die("error: can't open $fn for output\n");

  my %sh = ();

  for $key (keys %hr) {
    $sh{$hr{$key}{therion}} = $key;
  }

  my @nkeys = sort {$a <=> $b} (keys %sh);
  for $key (@nkeys) {
    my $toexp = 1;
    if ($onlyempty) {
      for $lkey (sort keys %{$hr{$sh{$key}}}) {
        if ((substr($lkey,0,2) eq $lng) && ($lkey !~ /^the/) && ($lkey !~ /\-cmnt/)) {
          $toexp = 0;
        }
      }
    }
    if ($toexp) {
      print OUT $hr{$sh{$key}}{"therion-cmnt"};
      print OUT "therion: $sh{$key}\n";
      my $posc = "$lng:\n";
      for $lkey (sort keys %{$hr{$sh{$key}}}) {
        if ((substr($lkey,0,2) eq $lng) && ($lkey !~ /^the/) && ($lkey !~ /\-cmnt/)) {
          print OUT $hr{$sh{$key}}{"$lkey-cmnt"};
          print OUT "$lkey: ". $hr{$sh{$key}}{$lkey} ."\n";
          $posc = "";
        }
      }
      print OUT $posc;
      print OUT "\n";
    }
  }

  close(OUT);

}



$rf = read_lang_file("texts.txt");
# ak update
if ($ARGV[0] =~ /^update$/i) {
  opendir(CDR,".");
  @updfiles = grep /^texts\_[a-z]{2}\.txt$/, readdir(CDR);
  foreach $ufn (@updfiles) {
    my $lcode = substr($ufn,6,2);
    print "updating $lcode translations ...";
    $rf = read_lang_file($ufn,$lcode,$rf);
    backup_file("$ufn");
    unlink($ufn) || $quietdel || warn("warning: can't delete $ufn\n");
    print " done\n";
  }
  closedir(CDR);
  backup_file("texts.txt");
  $rf = update_todo_list($rf);
  write_lang_file("texts.txt",$rf);
  write_sources($rf);
} elsif ($ARGV[0] =~ /^export(\-empty)?$/i) {
  my $onlyempty = $1;
  my $lng = check_language($ARGV[1]);
  if (length($lng) != 2) {
    die("error: invalid language \"$ARGV[1]\"\n");
  }
  export_language("texts_$lng.txt",$lng,$rf,$onlyempty);
} else {
  write_sources($rf);
}