File: make_encmap

package info (click to toggle)
libxml-encoding-perl 2.09-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,352 kB
  • ctags: 46
  • sloc: xml: 14,006; perl: 496; ansic: 27; makefile: 2
file content (200 lines) | stat: -rwxr-xr-x 3,858 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
#!perl -w
#
# make_encmap
#
# Copyright 1998 Clark Cooper
# Changes in Version 2.00 onwards Copyright (C) 2009 Steve Hay
# All rights reserved.
#
# This program is free software; you may redistribute it and/or
# modify it under the same terms as Perl itself.
#

use 5.008001;

my $name = shift;
my $file = shift;

my $except_str = '$@\^`{}~';
my %Exceptions;

foreach (unpack('c*', $except_str)) {
  $Exceptions{$_} = 1;
}

die "Usage is:\n\tmake_encmap name file\n"
  unless (defined($name) and defined($file));

open(MAP, $file) or die "Couldn't open $file";

my @byte1;
my $minpos = 256;

while (<MAP>) {
  next if /^\#/;

  next unless /0x([\da-f]{2,4})\s+0x([\da-f]{4})\s*\#\s*(.*)\s*$/i;

  my ($from, $to, $name) = ($1, $2, $3);

  my $flen = length($from);
  die "Bad line at $., from must be either 2 or 4 digits:\n$_"
    if $flen == 3;

  my $toval = hex($to);
  my $f1 = substr($from, 0, 2);
  my $f1val = hex($f1);

  if ($flen == 2) {
    if ($f1val < 128) {
	next if $f1val == $toval;

	warn "The byte '0x$f1' mapped to 0x$to\n"
	  unless defined($Exceptions{$f1val});
      }
		      
    if (defined($byte1[$f1val])) {
      die "Multiple mappings for 0x$f1val: $to & " . sprintf("0x%x",
							     $byte1[$f1val])
	if ($byte1[$f1val] != $toval);
    }
    else {
      $byte1[$f1val] = $toval;
      $minpos = $f1val
	if $f1val < $minpos;
    }
  }
  else {
    my $b1 = $byte1[$f1val];

    if (defined($b1)) {
      die "The 1st byte of '$from' overlaps a single byte definition."
	unless ref($b1);
    }
    else {
      $b1 = $byte1[$f1val] = [];
      $minpos = $f1val
	if $f1val < $minpos;
    }

    my $f2 = substr($from, 2, 2);
    my $f2val = hex($f2);

    $b1->[$f2val] = $toval;
  }
}

close(MAP);

die "Minpos never set" unless $minpos < 256;

print "<encmap name='$name'>\n";

process_byte(2, $minpos, \@byte1);

print "</encmap>\n";

####
## End main
####

sub emit {
  my ($pre, $start, $lim, $val) = @_;

  my $len = $lim - $start;

  if ($len == 1) {
    printf("$pre<ch byte='x%02x' uni='x%04x'/>\n", $start, $val);
  }
  else {
    printf("$pre<range byte='x%02x' len='%d' uni='x%04x'/>\n",
	   $start, $len, $val);
  }
}  # End emit

sub process_byte {
  my ($lead, $minpos, $aref) = @_;

  my $rngstrt;
  my $rngval;
  my $i;

  my $prefix = ' ' x $lead;

  for ($i = $minpos; $i <= $#{$aref}; $i++) {
    my $v = $ {$aref}[$i];
  
    if (defined($v)) {
      if (ref($v)) {
	emit($prefix, $rngstrt, $i, $rngval)
	  if defined($rngstrt);

	$rngstrt = undef;
	printf "$prefix<prefix byte='x%02x'>\n", $i;
	process_byte($lead + 2, 0, $v);
	print "$prefix</prefix>\n";
      }
      else {
	next if (defined($rngstrt) and ($v - $rngval == $i - $rngstrt));

	emit($prefix, $rngstrt, $i, $rngval)
	  if defined($rngstrt);

	$rngstrt = $i;
	$rngval = $v;
      }
    }
    else {
      emit($prefix, $rngstrt, $i, $rngval)
	if defined($rngstrt);

      $rngstrt = undef;
    }
  }

  emit($prefix, $rngstrt, $i, $rngval)
    if defined($rngstrt);

}  # End process_byte

__END__

=head1 NAME

make_encmap - create an XML representation from an Unicode mapping file

=head1 SYNOPSIS

B<make_encmap> I<name> I<file>

=head1 DESCRIPTION

B<make_encmap> creates a XML encmap file with a given name from an Unicode
mapping file, received e.g. from F<ftp://ftp.unicode.org>. The result by default
is output to F<stdout>.

=head1 OPTIONS

There are no options you can use.

=head1 EXAMPLES

The following example shows the usage of B<make_encmap> for the ISO/IEC 8859-15
table.

B<
    wget ftp://ftp.unicode.org/Public/MAPPINGS/ISO8859/8859-15.TXT
    make_encmap 8859-15 8859-15.TXT E<gt> 8859-15.encmap
>

=head1 SEE ALSO

L<compile_encoding(1p)>,
L<XML::Encoding(3pm)>

=head1 AUTHORS

This manual page was written by Daniel Leidert E<lt>daniel.leidert@wgdd.deE<gt>
for the Debian project (but may be used by others).

=cut