tools/translate_charmap.pl
author Alasdair Lumsden <alasdairrr@gmail.com>
Tue, 20 Sep 2011 18:51:35 +0000
branchoi_151a
changeset 570 869a4dca4ecf
parent 451 b819db8398b8
permissions -rwxr-xr-x
Adding tag oi_151a_release
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
346
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     1
#!/usr/bin/perl
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     2
#
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     3
# CDDL HEADER START
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     4
#
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     5
# The contents of this file are subject to the terms of the
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     6
# Common Development and Distribution License (the "License").  
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     7
# You may not use this file except in compliance with the License.
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     8
#
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
     9
# You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    10
# or http://www.opensolaris.org/os/licensing.
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    11
# See the License for the specific language governing permissions
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    12
# and limitations under the License.
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    13
#
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    14
# When distributing Covered Code, include this CDDL HEADER in each
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    15
# file and include the License file at src/OPENSOLARIS.LICENSE.
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    16
# If applicable, add the following below this CDDL HEADER, with the
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    17
# fields enclosed by brackets "[]" replaced with your own identifying
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    18
# information: Portions Copyright [yyyy] [name of copyright owner]
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    19
#
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    20
# CDDL HEADER END
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    21
#
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    22
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    23
# Use is subject to license terms.
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    24
#
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    25
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    26
# The script translate a file from one charmap to another
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    27
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    28
use strict;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    29
use warnings;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    30
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    31
die "Usage: translate_charmap.pl <charmap> <new charmap> <file to translate>\n" unless $#ARGV == 2;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    32
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    33
open F1, "<$ARGV[0]" or die $!;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    34
open F2, "<$ARGV[1]" or die $!;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    35
open F, "<$ARGV[2]" or die $!;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    36
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    37
my %cm;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    38
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    39
while (<F1>) { last if /^CHARMAP/ };
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    40
while (<F2>) { last if /^CHARMAP/ };
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    41
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    42
while (<F2>) {
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    43
	next if /^#/ or /^\s*$/;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    44
	last if /^END CHARMAP/;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    45
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    46
	chomp;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    47
	my ($name, $code) = /<(.*)>\s*(.*)\s*$/ or die $_;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    48
	$code = uc $code;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    49
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    50
	print "# rewrite $cm{$code} to $name\n" if defined $cm{$code} and length($cm{$code}) > length($name);
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    51
	$cm{$code} = $name unless defined $cm{$code} and length($cm{$code}) > length($name);
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    52
}
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    53
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    54
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    55
my %tr;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    56
while (<F1>) {
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    57
	next if /^#/ or /^\s*$/;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    58
	last if /^END CHARMAP/;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    59
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    60
	chomp;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    61
	my ($name, $code) = /<(.*)>\s*(.*)\s*$/ or die $_;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    62
	$code = uc $code;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    63
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    64
	print "# unknown code $code ($name)\n" unless defined $cm{$code};
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    65
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    66
	$tr{$name} = $cm{$code};
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    67
}
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    68
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    69
while (<F>) {
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    70
	if (/^#/) { print; next };
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    71
	
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    72
	my $line = $_;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    73
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    74
	for(my $i = 0;;) {
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    75
		my $n1 = index($line,"<",$i);
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    76
		
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    77
		if ($n1 == -1) {	
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    78
			print substr($line, $i);
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    79
			last;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    80
		}
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    81
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    82
		print substr($line, $i, $n1-$i+1);
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    83
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    84
		my $n2 = index($line, ">", $n1);
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    85
		die "'>' is missing" if $n2 == -1;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    86
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    87
		my $code = substr($line, $n1+1, $n2-$n1-1);
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    88
		
451
b819db8398b8 migrate hi_IN, ta_IN, te_IN to g11n-locale ws
jenda
parents: 346
diff changeset
    89
		if (defined $tr{$code}) {
b819db8398b8 migrate hi_IN, ta_IN, te_IN to g11n-locale ws
jenda
parents: 346
diff changeset
    90
			print $tr{$code};
b819db8398b8 migrate hi_IN, ta_IN, te_IN to g11n-locale ws
jenda
parents: 346
diff changeset
    91
		} else {
b819db8398b8 migrate hi_IN, ta_IN, te_IN to g11n-locale ws
jenda
parents: 346
diff changeset
    92
			warn "'$code' is unknown: '$line'" unless 
b819db8398b8 migrate hi_IN, ta_IN, te_IN to g11n-locale ws
jenda
parents: 346
diff changeset
    93
			print $code;
b819db8398b8 migrate hi_IN, ta_IN, te_IN to g11n-locale ws
jenda
parents: 346
diff changeset
    94
		}
b819db8398b8 migrate hi_IN, ta_IN, te_IN to g11n-locale ws
jenda
parents: 346
diff changeset
    95
346
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    96
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    97
		$i = $n2;
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    98
	}
2027df53c93b add tools/translate_charmap.pl (translate a file from one to another charmap)
jenda
parents:
diff changeset
    99
}