source: git/src/gettexttomsg.pl @ 3ee64fb

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 3ee64fb was c40038a, checked in by Olly Betts <olly@…>, 22 years ago

Vastly improved msg.pl script for extracting messages from source code.
Compared its output to message file and fixed up discrepancies.

git-svn-id: file:///home/survex-svn/survex/trunk@1872 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100755
File size: 3.0 KB
Line 
1#!/usr/bin/perl -w
2require 5.004;
3use strict;
4
5use integer;
6
7# Magic identifier (12 bytes)
8my $magic = "Svx\nMsg\r\n\xfe\xff\0";
9# Designed to be corrupted by ASCII ftp, top bit stripping (or
10# being used for parity).  Contains a zero byte so more likely
11# to be flagged as data (e.g. by perl's "-B" test).
12
13my $major = 0;
14my $minor = 8;
15
16# File format (multi-byte numbers in network order (bigendian)):
17# 12 bytes: Magic identifier
18# 1 byte:   File format major version number (0)
19# 1 byte:   File format minor version number (8)
20# 2 bytes:  Number of messages (N)
21# 4 bytes:  Offset from XXXX to end of file
22# XXXX:
23# N*:
24# <message> NUL
25
26my %ent = ();
27
28open ENT, "../lib/named-entities.txt" or die $!;
29while (<ENT>) {
30   my ($e, $v) = /^(\w+),(\d+)/;
31   $ent{$e} = $v;
32}
33close ENT;
34
35my %revmsgs = ();
36
37open MSG, "../lib/messages.txt" or die $!;
38while (<MSG>) {
39   next if /^\s*#/; # skip comments
40   
41   # en:  0 0.81 the message
42   # en-us: 0 0.81 " the message "
43   my ($langs, $msgno, $dummy, $msg) = /^([-\w,]+):\s*(\d+)\s+("?)(.*)\3/;
44
45   unless (defined $langs) {
46      chomp;
47      print STDERR "Warning: Bad line: \"$_\"\n";
48      next;
49   }
50
51   $langs =~ tr/-/_/;
52
53   if ($msg =~ /[\0-\x1f\x7f-\xff]/) {
54      print STDERR "Warning: literal character in message $msgno\n";
55   }
56
57   my $utf8 = string_to_utf8($msg);
58   for (split /,/, $langs) {
59      if (lc $_ eq 'en') {
60          $revmsgs{$utf8} = $msgno;
61      }
62   }
63}
64
65my $die = 0;
66
67while (<>) {
68    if (!/^\s*#/) {
69        while (/\\\n$/) {
70            $_ .= <>;
71        }
72        # very crude - doesn't know about comments, etc
73        s!\b_\("(.*?)"\)!replacement($1)!gse;
74    } elsif (/\s*#\s*define\s+_\(/) {
75        $_ = "#include \"message.h\"\n";
76    }
77    print;
78}
79
80if ($die) {
81    die "Not all messages found!\n";
82}
83
84sub replacement {
85    my $msg = shift;
86    $msg =~ s/\\\n//g;
87    $msg =~ s/\\n/\n/g;
88    $msg =~ s/\\t/\t/g;
89    my $msgno = "";
90    if (exists $revmsgs{$msg}) {
91        $msgno = $revmsgs{$msg};
92    } else {
93        if (!$die) {
94            print STDERR "Message(s) not found in message file:\n";
95            $die = 1;
96        }
97        print STDERR "'$msg'\n";
98    }
99    $msg =~ s/\n/&#10;/g;
100    $msg =~ s/\t/&#9;/g;
101    return "msg(/*$msg*/$msgno)";
102}
103
104sub string_to_utf8 {
105   my $s = shift;
106   $s =~ s/([\x80-\xff])/char_to_utf8(ord($1))/eg;
107   $s =~ s/\&(#\d+|#x[a-f0-9]+|[a-z0-9]+);?/decode_entity($1)/eig;
108   return $s;
109}
110
111sub decode_entity {
112   my $ent = shift;
113   return char_to_utf8($1) if $ent =~ /^#(\d+)$/;
114   return char_to_utf8(hex($1)) if $ent =~ /^#x([a-f0-9]+)$/;
115   return char_to_utf8($ent{$ent}) if exists $ent{$ent};
116   $ent = "\&$ent;";
117   print STDERR "Warning: entity \"$ent\" unknown\n";
118   return $ent;
119}
120
121sub char_to_utf8 {
122   my $unicode = shift;
123   # ASCII is easy, and the most common case
124   return chr($unicode) if $unicode < 0x80;
125
126   my $result = '';
127   my $n = 0x20;
128   while (1) {
129      $result = chr(0x80 | ($unicode & 0x3f)) . $result;
130      $unicode >>= 6;
131      last if $unicode < $n;
132      $n >>= 1;
133   }
134   $result = chr((0x100 - $n*2) | $unicode) . $result;
135   return $result;
136}
Note: See TracBrowser for help on using the repository browser.