lib/Cz/Cstocs.pm

filter: Statementstmt 85.8% 243 / 283 Branchbran 66.4% 113 / 170 Conditioncond 78.7% 26 / 33 MC/DCmcdc 65.0% 13 / 20 Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 11 totaltotal 76.7% 410 / 534 CC 103 SCAR 34.9
CC
103
Cov
79%
new
64.9
fixup_tex_encoding
24.6
load_encoding
17.8
CRAP 32.9
« lib/Cz/Sort.pm ↑ Summary lib/Cz/Cstocs/Getopt.pm »
1  
2 =head1 NAME
3  
4 Cz::Cstocs - conversions of charset encodings for the Czech language
5  
6 =cut
7  
8 package Cz::Cstocs;
9  
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use vars qw( $VERSION $DEBUG $cstocsdir @ISA @EXPORT_OK %EXPORT $errstr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 @ISA = qw(Exporter);
16 4 @EXPORT_OK = ( '_stupidity_workaround' );
17 4 %EXPORT = ( '_stupidity_workaround' => 1 );
18  
19 0 ▶sub _stupidity_workaround {
Subroutine _stupidity_workaround: not called CC=1 SCAR=0.0
20 }
21  
22 8 ▶sub import {
Subroutine import: called CC=7 SCAR=19.8
23 8 my $class = shift;
24 8 my @data = @_;
25 8 ▶ if (@data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 1 7 8
26 1 my @avail = Cz::Cstocs->available_enc();
27 1 my $fn;
28 1 for $fn (@data) {
29 1 local $^W = 0;
30 1 ▶ next if grep { $_ eq $fn } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $fn;} @Cz::Cstocs::EXPORT_OK 0 1 1
31 1 my ($in, $out) = $fn =~ /^_?(.*?)_(?:to_)?(.*)$/;
32 1 ▶ next unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 1 1
33 1 my $fnref = new Cz::Cstocs $in, $out;
34 1 ▶ die "Definition of $fn failed: $errstr"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fnref 0 1 1
35 unless defined $fnref;;
36 1 ▶ eval "sub $fn { \$fnref->conv(\@_); }; ";
Subroutine _1250_il2: called CC=1 SCAR=0.0
37 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
38 0 die "Creating conversion function $fn failed: $@";
39 }
40 1 push @EXPORT_OK, $fn;
41 1 $EXPORT{$fn} = 1;
42 }
43 }
44 8 Cz::Cstocs->export_to_level(1, '_stupidity_workaround', @data);
45 }
46  
47 4 $VERSION = '3.43';
48  
49 # Debugging option
50 4 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Cz::Cstocs::DEBUG 4 0 4
51 38 ▶sub DEBUG () { $DEBUG; }
Subroutine DEBUG: called CC=1 SCAR=0.0
Pod undocumented
52  
53  
54 # Where to get the encoding files from
55 # Start with some default
56 4 my $defaultcstocsdir = '/packages/share/cstocs/lib';
57  
58 # Look at the environment variable
59 4 ▶if (defined $ENV{'CSTOCSDIR'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ENV{'CSTOCSDIR'}) { } 0 4 4
elsif (defined $INC{'Cz/Cstocs.pm'}) { } 4 0 4
60 0 $defaultcstocsdir = $ENV{'CSTOCSDIR'};
61 0 ▶ print STDERR "Using enc-dir $defaultcstocsdir from the CSTOCSDIR env-var\n"
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 0 0
62 if DEBUG;
63 }
64 # Or take the encoding files from the Perl tree
65 elsif (defined $INC{'Cz/Cstocs.pm'}) {
66 4 $defaultcstocsdir = $INC{'Cz/Cstocs.pm'};
67 4 $defaultcstocsdir =~ s!\.pm$!/enc!;
68 4 ▶ print STDERR "Using enc-dir $defaultcstocsdir from \@INC\n"
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 4 4
69 if DEBUG;
70 }
71  
72 # We have unless hare because you could have overriden $cstocsdir
73 4 ▶$cstocsdir = $defaultcstocsdir unless defined $cstocsdir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Cz::Cstocs::cstocsdir 4 0 4
74  
75  
76 # Hash that holds the accent file and a tag saying if the accent
77 # file has already been read
78 4 my %accent = ();
79 4 my $accent_read = 0;
80  
81 # Hash of alias covnersions
82 4 my %alias = ();
83 4 my $alias_read = 0;
84  
85 # Input and output hashes
86 4 my %input_hashes = ();
87 4 my %output_hashes = ();
88  
89 # Array of regexp parts
90 4 my %regexp_matches = ();
91  
92 # Table of conversion functions, so that we do not need to create them twice
93 4 my %functions = ();
94  
95 # List of diacritics
96 4 my @diacritics = qw( abovedot acute breve caron cedilla circumflex
97 diaeresis doubleacute ogonek ring );
98  
99  
100  
101 # ######################################################
102 # Now, the function -- loading encoding and accent files
103  
104 # Filling input and output_hashes tables for given encoding
105 25 ▶sub load_encoding {
Subroutine load_encoding: called CC=14 SCAR=28.8
Pod undocumented
106 25 my $enc = lc shift;
107  
108 25 ▶ return if defined $input_hashes{$enc}; # has already been loaded
Branch: 1 branch
BranchTrueFalseTotal
if defined $input_hashes{$enc} 14 11 25
109  
110 11 ▶ if ($enc eq 'mime') {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq "mime") 0 11 11
111 0 eval 'use MIME::Words ()';
112 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
113 0 die "Error loading encofing $enc: $@\n";
114 }
115 0 return;
116 }
117  
118 11 my $file = "$cstocsdir/$enc.enc";
119 11 ▶ open FILE, $file or die "Error reading $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die "Error reading ${file}: $!\n" 11 0 11
120 11 ▶ print STDERR "Parsing encoding file $file\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 11 11
121  
122 11 my ($input, $output) = ({}, {}); # just speedup thing
123 11 local $_;
124 11 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 1850 11 1861
125 1850 ▶ next if /^(#|\s*$)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(#|\s*$)/ 7 1843 1850
126 1843 my ($tag, $desc) = /^\s*(\S+)\s+(\S+)\s*$/;
127 1843 ▶ unless (defined $tag and defined $desc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag and defined $desc) 0 1843 1843
Condition33%1/3
defined $tag and defined $desc
!ll&&!rl&&r
001843
MC/DC0%0/2
defined $tag and defined $desc
defined $tag defined $desc
Truth table33%1/3
A and B
A= defined $tagB= defined $desc
ABresult
0X0
100
111
128 0 chomp;
129 0 warn "Syntax error in $file at line $: `$_'.\n";
130 0 next;
131 }
132 1843 ▶ if ($tag =~ /^\d+|0x\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^\d+|0x\d+$/) 1818 25 1843
133 $tag = pack 'C*', map {
134 1818 ▶ /^0/ ? oct($_) : $_
Branch: 1 branch
BranchTrueFalseTotal
/^0/ ? : 936 1264 2200
135 } split /,/, $tag;
136 }
137 1843 $input->{$tag} = $desc;
138 1843 ▶ $output->{$desc} = $tag unless defined $output->{$desc};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output->{$desc} 1832 11 1843
139 }
140 11 close FILE;
141  
142 11 $input_hashes{$enc} = $input;
143 11 $output_hashes{$enc} = $output;
144  
145 11 ▶ if ($enc eq "tex") {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq "tex") 1 10 11
146 1 fixup_tex_encoding();
147 }
148 }
149  
150 1 ▶sub fixup_tex_encoding {
Subroutine fixup_tex_encoding: called CC=24 SCAR=32.0
Pod undocumented
151 1 my $tag;
152
153 1 ▶ print STDERR "Doing tex fixup\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 1 1
154
155 1 my $input = $input_hashes{"tex"};
156 1 my $output = $output_hashes{"tex"};
157  
158 # we need this to fill the defaults
159 1 load_encoding('ascii');
160 1 my $asciiref = $output_hashes{'ascii'};
161 1 for $tag (keys %$asciiref) {
162 $output->{$tag} = $asciiref->{$tag}
163 95 ▶ unless defined $output->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output->{$tag} 95 0 95
164 }
165  
166 1 my %processed = ();
167  
168 1 my (@dialetters, @dianonletters, @nondialetters, @nondianonletters);
169 1 my (@inputs) = keys %$input;
170 1 for $tag (@inputs) {
171 24 my $value = $input->{$tag};
172  
173 24 my $az = 0;
174 24 ▶ $az = 1 if $tag =~ /[a-zA-Z]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /[a-zA-Z]$/ 18 6 24
175  
176 24 ▶ if ($az and $output->{$value} eq $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($az and $output->{$value} eq $tag) 16 8 24
Condition100%3/3
$az and $output->{$value} eq $tag
!ll&&!rl&&r
6216
MC/DC100%2/2
$az and $output->{$value} eq $tag
$az $output->{$value} eq $tag
Truth table100%3/3
A and B
A= $azB= $output->{$value} eq $tag
ABresult
0X0
100
111
177 16 $output->{$value} = $tag . '{}';
178 }
179 24 $input->{$tag . ' '} = $value;
180  
181 24 ▶ if (grep { $_ eq $value } @diacritics) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq $value;} @diacritics) { } 10 14 24
elsif ($az) { } 13 1 14
182 10 my $e;
183 10 ▶ if ($az) {
Branch: 1 branch
BranchTrueFalseTotal
if ($az) { } 5 5 10
184 5 push @dialetters, $tag;
185 5 for $e ('a'..'h', 'k'..'z', 'A'..'Z') {
186 250 $output->{$e.$value} = $tag.' '.$e
187 }
188 } else {
189 5 push @dianonletters, $tag;
190 5 for $e ('a'..'h', 'k'..'z', 'A'..'Z') {
191 250 $output->{$e.$value} = $tag.$e
192 }
193 5 for $e ('a'..'z', 'A'..'Z') {
194 260 $input->{$tag.$e} = $e.$value;
195 }
196 }
197 10 for $e ('i', 'j') {
198 20 $output->{$e.$value} = $tag.'\\'.$e.'{}'
199 }
200 10 for $e ('a'..'z', 'A'..'Z') {
201 520 $input->{$tag.' '.$e} = $e.$value;
202 }
203 10 for $e ('i', 'j') {
204 20 $input->{$tag.'\\'.$e} = $e.$value;
205 20 $input->{$tag.' \\'.$e} = $e.$value;
206 }
207 } elsif ($az) {
208 13 push @nondialetters, $tag;
209 } else {
210 1 push @nondianonletters, $tag;
211 }
212 }
213  
214 1 my $regexp = '';
215  
216 1 ▶ if (@dialetters) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dialetters) 1 0 1
217 $regexp .= join '', '(',
218 1 join('|', map { "\Q$_"; } @dialetters),
219 ")([ \\t]+[a-zA-Z]|[ \\t]*(\\\\[ij]([ \\t]+(\\{\\})?|[ \\t]*(\$|\\{\\}))|\\{([a-zA-Z]|\\\\[ij][ \\t]*(\\{\\})?)\\}))";
220 }
221 1 ▶ if (@dianonletters) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dianonletters) 1 0 1
222 1 ▶ $regexp .= '|' if $regexp ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $regexp ne "" 1 0 1
223 $regexp .= '(' . join '',
224 1 join('|', map { "\Q$_"; } @dianonletters),
225 ")[ \\t]*([a-zA-Z]|\\\\[ij]([ \\t]+(\\{\\})?|[ \\t]*(\$|\\{\\}))|\\{([a-zA-Z]|\\\\[ij][ \\t]*(\\{\\})?)\\})";
226 }
227 1 ▶ if (@nondialetters) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nondialetters) 1 0 1
228 1 ▶ $regexp .= '|' if $regexp ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $regexp ne "" 1 0 1
229 $regexp .= '(' . join '',
230 1 join('|', map { "\Q$_"; } @nondialetters),
231 ")([ \\t]+(\\{\\})?|[ \\t]*\$)"
232 }
233 1 ▶ if (@nondianonletters) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nondianonletters) 1 0 1
234 1 ▶ $regexp .= '|' if $regexp ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $regexp ne "" 1 0 1
235 $regexp .= '(' . join '',
236 1 join('|', map { "\Q$_"; } @nondianonletters),
237 ")[ \\t]*(\\{\\})?"
238 }
239  
240 1 $regexp_matches{'tex'} = $regexp;
241 1 1;
242 }
243  
244 # Loading accent file
245 11 ▶sub load_accent {
Subroutine load_accent: called CC=8 SCAR=22.8
Pod undocumented
246 11 ▶ return if $accent_read;
Branch: 1 branch
BranchTrueFalseTotal
if $accent_read 7 4 11
247 4 $accent_read = 1;
248
249 4 my $file = "$cstocsdir/accent";
250 4 ▶ open FILE, $file or die "Error reading accent file $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die "Error reading accent file ${file}: $!\n" 4 0 4
251 4 ▶ print STDERR "Parsing accent file $file\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 4 4
252  
253 4 local $_;
254 4 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 804 4 808
255 804 ▶ next if /^\s*(#|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(#|$)/ 0 804 804
256 804 my ($key, $val) = /^\s*(\S+)\s+(.+?)\s*$/;
257 804 ▶ unless (defined $key and defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and defined $val) 0 804 804
Condition33%1/3
defined $key and defined $val
!ll&&!rl&&r
00804
MC/DC0%0/2
defined $key and defined $val
defined $key defined $val
Truth table33%1/3
A and B
A= defined $keyB= defined $val
ABresult
0X0
100
111
258 0 chomp;
259 0 warn "Syntax error in $file at line $: `$_'.\n";
260 0 next;
261 }
262 804 $accent{$key} = $val;
263 }
264 4 close FILE;
265 }
266  
267 # Load the alias file, fill the global %alias hash;
268 28 ▶sub load_alias {
Subroutine load_alias: called CC=4 SCAR=13.9
Pod undocumented
269 28 ▶ return if $alias_read;
Branch: 1 branch
BranchTrueFalseTotal
if $alias_read 24 4 28
270 4 $alias_read = 1;
271 4 my $file = "$cstocsdir/alias";
272  
273 4 ▶ open FILE, $file or die "Error reading alias file $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or die "Error reading alias file ${file}: $!\n" 4 0 4
274 4 local $_;
275 4 ▶ while (<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 68 4 72
276 68 chomp;
277 68 my ($alias, $enc) = split;
278 68 $alias{$alias} = $enc;
279 }
280 4 close FILE;
281 }
282  
283 # Normalizes the encoding name -- expands aliases
284 28 ▶sub normalize_enc_name {
Subroutine normalize_enc_name: called CC=2 SCAR=6.9
Pod undocumented
285 28 load_alias();
286 28 my $enc = lc shift;
287 28 $enc =~ s/[^a-z0-9]//g;
288 28 ▶ ( defined $alias{$enc} ? $alias{$enc} : $enc );
Branch: 1 branch
BranchTrueFalseTotal
defined $alias{$enc} ? : 2 26 28
289 }
290  
291 # Recursively lookup the target
292 451 ▶sub lookup_accent {
Subroutine lookup_accent: called CC=4 SCAR=15.9
Pod undocumented
293 451 my ($outenc, $accent, $in) = @_;
294 451 my @target = split /\s+/, $in;
295 451 my $out = '';
296 451 for my $desc (@target) {
297 538 ▶ if (defined $outenc->{$desc}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $outenc->{$desc}) { } 538 0 538
elsif (defined $accent->{$desc}) { } 0 0 0
298 538 $out .= $outenc->{$desc};
299 } elsif (defined $accent->{$desc}) {
300 0 $out .= lookup_accent($outenc, $accent, $accent->{$desc});
301 } else {
302 0 die;
303 }
304 }
305 451 return $out;
306 }
307  
308 # Constructor -- takes two arguments, input and output encodings,
309 # a optionally hash of options. Returns reference to code that will
310 # do the conversion, or undef
311 14 ▶sub new {
Subroutine new: called CC=44 SCAR=41.7
Pod undocumented
312 14 my $class = shift;
313 14 my ($inputenc, $outputenc) = (shift, shift);
314  
315 14 local $/ = "\n";
316  
317 # check input values
318 14 ▶ unless (defined $inputenc and defined $outputenc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inputenc and defined $outputenc) 0 14 14
Condition33%1/3
defined $inputenc and defined $outputenc
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $inputenc and defined $outputenc
defined $inputenc defined $outputenc
Truth table33%1/3
A and B
A= defined $inputencB= defined $outputenc
ABresult
0X0
100
111
319 0 print STDERR "Both input and output encodings must be specified in call to ", __PACKAGE__, "::new\n";
320 0 return;
321 }
322  
323 # Default options
324 14 my $fillstring = ' ';
325 14 my $use_fillstring = 1;
326 14 my $use_accent = 1;
327 14 my $one_by_one = 0;
328  
329 # this is exception for TeX
330 14 ▶ $use_fillstring = 0 if $inputenc eq "tex";
Branch: 1 branch
BranchTrueFalseTotal
if $inputenc eq "tex" 2 12 14
331  
332 14 my %opts = @_;
333 14 my ($tag, $value);
334 14 ▶ while (($tag, $value) = each %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %opts) 3 14 17
335 3 ▶ print STDERR "Option: $tag = '$value'\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 3 3
336 3 ▶ $tag eq 'fillstring' and $fillstring = $value;
Branch: 1 branch
BranchTrueFalseTotal
$tag eq "fillstring" and $fillstring = $value 1 2 3
337 3 ▶ $tag eq 'use_accent' and
Branch: 2 branches
BranchTrueFalseTotal
$tag eq "use_accent" and $use_accent = defined $value ? $value : 0 1 2 3
defined $value ? : 1 0 1
338 $use_accent = (defined $value ? $value : 0);
339 3 ▶ $tag eq 'nofillstring' and
Branch: 3 branches
BranchTrueFalseTotal
$tag eq "nofillstring" and $use_fillstring = defined $value ? $value ? 0 : 1 : 0 0 3 3
defined $value ? : 0 0 0
$value ? : 0 0 0
340 $use_fillstring = (defined $value ?
341 ( $value ? 0 : 1) : 0);
342 3 ▶ $tag eq 'cstocsdir' and $cstocsdir = $value;
Branch: 1 branch
BranchTrueFalseTotal
$tag eq "cstocsdir" and $Cz::Cstocs::cstocsdir = $value 0 3 3
343 3 ▶ $tag eq 'one_by_one' and $one_by_one = $value;
Branch: 1 branch
BranchTrueFalseTotal
$tag eq "one_by_one" and $one_by_one = $value 1 2 3
344 }
345  
346 14 $inputenc = normalize_enc_name($inputenc);
347 14 $outputenc = normalize_enc_name($outputenc);
348  
349 # encode settings into the function name
350 14 ▶ if (defined $functions{"${inputenc}_${outputenc}_${fillstring}_${use_fillstring}_${use_accent}_${one_by_one}"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $functions{"${inputenc}_${outputenc}_${fillstring}_${use_fillstring}_${use_accent}_$one_by_one"}) 2 12 14
351 2 return $functions{"${inputenc}_${outputenc}_${fillstring}_${use_fillstring}_${use_accent}_${one_by_one}"};
352 }
353  
354 12 eval {
355 12 load_encoding($inputenc);
356 12 load_encoding($outputenc);
357 12 ▶ load_accent() if $use_accent;
Branch: 1 branch
BranchTrueFalseTotal
if $use_accent 11 1 12
358 };
359 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
360 0 $errstr = $@;
361 0 return;
362 }
363  
364 12 my $conv = {};
365  
366 12 my ($is_one_by_one, $has_space) = (1, 0);
367  
368 12 ▶ if ($outputenc ne 'mime') {
Branch: 1 branch
BranchTrueFalseTotal
if ($outputenc ne "mime") 12 0 12
369 12 my $key;
370 12 for $key (keys %{$input_hashes{$inputenc}}) {
371 3767 my $desc = $input_hashes{$inputenc}{$key};
372 3767 my $output = $output_hashes{$outputenc}{$desc};
373
374 3767 ▶ if (not defined $output and $use_accent) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $output and $use_accent) 1206 2561 3767
Condition100%3/3
not defined $output and $use_accent
!ll&&!rl&&r
18557061206
MC/DC100%2/2
not defined $output and $use_accent
not defined $output $use_accent
Truth table100%3/3
not(A) and B
A= not defined $outputB= $use_accent
ABresult
0X0
100
111
375 # Doesn't have friend in output encoding
376  
377  
378 1206 $output = eval {
379 lookup_accent($output_hashes{$outputenc},
380 1206 ▶ \%accent, $accent{$desc}) if defined $accent{$desc};
Branch: 1 branch
BranchTrueFalseTotal
if defined $accent{$desc} 451 755 1206
381 };
382 1206 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1206 1206
383 0 $errstr = "Error processing translitaration for $inputenc -> $outputenc for character $desc.\n";
384 0 return;
385 }
386  
387 1206 ▶ $output = undef if $one_by_one and defined $output
Branch: 1 branch
BranchTrueFalseTotal
if $one_by_one and defined $output and length $key < length $output 11 1195 1206
Condition83%5/6
$one_by_one and defined $output and length $key < length $output
!ll&&!rl&&r
11851011
$one_by_one and defined $output
!ll&&!rl&&r
1185021
MC/DC66%2/3
$one_by_one and defined $output and length $key < length $output
$one_by_one defined $output length $key < length $output
Truth table75%3/4
A and B and C
A= $one_by_oneB= defined $outputC= length $key < length $output
ABCresult
0XX0
10X0
1100
1111
388 and length $key < length $output;
389 }
390 3767 ▶ if (not defined $output and $use_fillstring) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $output and $use_fillstring) 11 3756 3767
Condition100%3/3
not defined $output and $use_fillstring
!ll&&!rl&&r
305070611
MC/DC100%2/2
not defined $output and $use_fillstring
not defined $output $use_fillstring
Truth table100%3/3
not(A) and B
A= not defined $outputB= $use_fillstring
ABresult
0X0
100
111
391 11 $output = $fillstring;
392 }
393
394 3767 ▶ next if (not defined $output
Branch: 1 branch
BranchTrueFalseTotal
if not defined $output or $inputenc ne "utf8" and $key eq $output 1629 2138 3767
Condition100%6/6
not defined $output or $inputenc ne "utf8" and $key eq $output
l!l&&r!l&&!r
7069232138
$inputenc ne "utf8" and $key eq $output
!ll&&!rl&&r
5501588923
MC/DC100%3/3
not defined $output or $inputenc ne "utf8" and $key eq $output
not defined $output $inputenc ne "utf8" $key eq $output
Truth table100%4/4
not(A) or B and C
A= not defined $outputB= $inputenc ne "utf8"C= $key eq $output
ABCresult
1XX1
0111
00X0
0100
395 or ($inputenc ne 'utf8' and $key eq $output));
396 2138 ▶ if (length $key != 1 or length $output != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (length $key != 1 or length $output != 1) 1611 527 2138
Condition100%3/3
length $key != 1 or length $output != 1
l!l&&r!l&&!r
1388223527
MC/DC100%2/2
length $key != 1 or length $output != 1
length $key != 1 length $output != 1
Truth table100%3/3
A or B
A= length $key != 1B= length $output != 1
ABresult
1X1
011
000
397 1611 { $is_one_by_one = 0; }
398 2138 $conv->{$key} = $output;
399 }
400 }
401  
402 12 my $fntext = ' sub { my @converted = map { my $e = $_; if (defined $e) {';
403  
404 12 ▶ if ($inputenc eq 'mime') {
Branch: 6 branches
BranchTrueFalseTotal
if ($inputenc eq 'mime') { } 0 12 12
elsif ($outputenc eq 'mime') { } 0 12 12
elsif (not keys %$conv) { } 1 11 12
elsif ($is_one_by_one) { } 1 10 11
elsif ($inputenc eq 'tex') { } 2 8 10
elsif ($inputenc eq 'utf8') { } 2 6 8
405 0 $fntext .= qq!
406 \$e =~ s/=\\s*=/==/g;
407 \$e = join '', map {
408 my \$conv;
409 if (defined \$_->[1]) {
410 (defined(\$conv = new Cz::Cstocs \$_->[1], '$outputenc', %{ \\%opts }))
411 ? \$conv->conv(\$_->[0])
412 : ()
413 } else {
414 \$_->[0]
415 }
416 } MIME::Words::decode_mimewords(\$e);
417 !;
418 } elsif ($outputenc eq 'mime') {
419 0 my %MIME_NAMES = (
420 il1 => 'ISO-8859-1',
421 il2 => 'ISO-8859-2',
422 utf8 => 'UTF-8',
423 1250 => 'Windows-1250',
424 1252 => 'Windows-1252',
425 );
426 0 my $charset = $MIME_NAMES{$inputenc};
427 0 ▶ if (not defined $charset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $charset) 0 0 0
428 0 die "Couldn't find MIME name for encoding $inputenc\n";
429 }
430 0 $fntext .= qq!
431 \$e = MIME::Words::encode_mimewords(\$e, Charset => '$charset');
432 \$e =~ s/\\?=( +)=\\?.*?\\?Q\\?/'_' x length \$1/egi;
433 !;
434 } elsif (not keys %$conv) {
435 # do nothing;
436 } elsif ($is_one_by_one) {
437 1 my $src = join "", keys %$conv;
438 1 $src = "\Q$src";
439 1 my $dst = join "", values %$conv;
440 1 $dst = "\Q$dst";
441 1 $fntext .= qq! \$e =~ tr/$src/$dst/; !;
442 } elsif ($inputenc eq 'tex') {
443 2 my $src = $regexp_matches{'tex'};
444 2 $fntext .= qq! \$e =~ s/$src/ my \$e = \$&; my \$orig = \$e; \$e =~ s#[{}]# #sog; \$e =~ s#[ \\t]+# #sog; \$e =~ s# \$##o; (defined \$conv->{\$e} ? \$conv->{\$e} : \$orig); /esog; !;
445 } elsif ($inputenc eq 'utf8') {
446 2 $fntext .= qq! \$e =~ s/[\\x21-\\x7f]|[\\xc0-\\xdf].|[\\xe0-\\xef]..|[\\xf0-\\xf7]...|[\\xf8-\\xfb]....|[\\xfc\\xfd]...../defined \$conv->{\$&} ? \$conv->{\$&} : (
447 $use_fillstring ? \$fillstring : '') /esog; !;
448 } else {
449 6 my $singles = join "", grep { length $_ == 1 } keys %$conv;
450 6 $singles = "[". "\Q$singles" . "]";
451
452 my $src = join "|",
453 0 ( map { my $e = "\Q$_"; $e; }
454 0 sort { length $b <=> length $a }
455 6 grep { length $_ != 1 } keys %$conv);
456 6 ▶ if ($singles ne "[]") {
Branch: 1 branch
BranchTrueFalseTotal
if ($singles ne "[]") 6 0 6
457 6 ▶ $src .= "|" unless $src eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $src eq "" 0 6 6
458 6 $src .= $singles;
459 }
460
461 6 $fntext .= qq! \$e =~ s/$src/\$conv->{\$&}/sog; !;
462 }
463  
464 12 $fntext .= ' $e; } else { undef; }} @_; if (wantarray) { return @converted; } else { return join "", map { defined $_ ? $_ : "" } @converted; } }';
465  
466 12 ▶ print STDERR "Conversion function for $inputenc to $outputenc:\n$fntext\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 12 12
467  
468 12 my $fn = eval $fntext;
469 0 ▶ do { chomp $@;
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
470 0 die "Fatal error in Cz::Cstocs: $@, line ", __LINE__, "\n";
471 } if $@;
472 12 bless $fn, $class;
473
474 12 $functions{"${inputenc}_${outputenc}_${fillstring}_${use_fillstring}_${use_accent}_${one_by_one}"} = $fn;
475 12 $fn;
476 }
477  
478 12 ▶sub conv {
Subroutine conv: called CC=1 SCAR=0.0
Pod undocumented
479 12 my $self = shift;
480 12 return &$self($_[0]);
481 }
482  
483 1 ▶sub available_enc {
Subroutine available_enc: called CC=2 SCAR=7.0
Pod undocumented
484 1 ▶ opendir DIR, $cstocsdir or warn "Error reading $cstocsdir\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $Cz::Cstocs::cstocsdir or warn "Error reading $Cz::Cstocs::cstocsdir\n" 1 0 1
485 1 my @list = sort map { s/\.enc$//; $_ } grep { /\.enc$/ } readdir DIR;
486 1 closedir DIR;
487 1 return @list;
488 }
489  
490 0 ▶sub diacritic_char {
Subroutine diacritic_char: not called CC=3 SCAR=24.8
Pod undocumented
491 0 my ($encoding, $char) = @_;
492 0 load_encoding($encoding);
493  
494 0 my @result = ();
495 0 my $dia;
496 0 for $dia (@diacritics) {
497 0 my $name = $char . $dia;
498 push @result, $output_hashes{$encoding}{$name}
499 0 ▶ if defined $output_hashes{$encoding}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $output_hashes{$encoding}{$name} 0 0 0
500 }
501 0 @result;
502 }
503  
504 4 1;
505  
506 =head1 SYNOPSIS
507  
508 use Cz::Cstocs;
509 my $il2_to_ascii = new Cz::Cstocs 'il2', 'ascii';
510 while (<>) {
511 print &$il2_to_ascii($_);
512 }
513  
514 use Cz::Cstocs 'il2_ascii';
515 while (<>) {
516 print il2_ascii($_);
517 }
518  
519 use Cz::Cstocs;
520 sub il2toascii;
521 # inform the parser that there is a function il2toascii
522 *il2toascii = new Cz::Cstocs 'il2', 'ascii';
523 # now define the function
524 print il2toascii $data;
525 # thanks to Jan Krynicky for poining this out
526  
527 =head1 DESCRIPTION
528  
529 This module helps in converting texts between various charset
530 encodings, used for Czech and Slovak languages. The instance of the
531 object B<Cz::Cstocs> is created using method B<new>. It takes at
532 least two parameters for input and output encoding and can be
533 afterwards used as a function reference to convert strings/lists.
534 Cz::Cstocs supports fairly free form of aliases, so iso8859-2,
535 ISO-8859-2, iso88592 and il2 are all aliases of the same encoding.
536 For backward compatibility, method I<conv> is supported as well,
537 so the example above could also read
538  
539 while (<>) {
540 print $il2_to_ascii->conv($_);
541 }
542  
543 You can also use typeglob syntax.
544  
545 The conversion function takes a list and returns list of converted
546 strings (in the list context) or one string consisting of concatenated
547 results (in the scalar context).
548  
549 You can modify the behaviour of the conversion function by specifying
550 hash of other options after the encoding names in call to B<new>.
551  
552 =over 4
553  
554 =item fillstring
555  
556 Gives alternate string that will replace characters from input
557 encoding that are not present in the output encoding. Default is
558 space.
559  
560 =item use_accent
561  
562 Defines whether the accent file should be used. Default is 1 (true).
563  
564 =item nofillstring
565  
566 When 1 (true), will keep characters that do not have friends in
567 accent nor output encoding, will no replace them with fillstring.
568 Default is 0 except for tex, because you probably rather want to keep
569 backslashed symbols than loose them.
570  
571 =item cstocsdir
572  
573 Alternate location for encoding and accent files. The default is the
574 F<Cz/Cstocs/enc> directory in Perl library tree. This location can
575 also be changed with the I<CSTOCSDIR> environment variable.
576  
577 =back
578  
579 There is an alternate way to define the conversion function: any
580 arguments after use Cz::Cstocs that have form encoding_encoding or
581 encoding_to_encoding are processed and the appropriate functions are
582 imported. So,
583  
584 use Cz::Cstocs qw(pc2_to_il2 il2_ascii);
585  
586 define two functions, that are loaded into caller's namespace and
587 can be used directly. In this case, you cannot specify additional
588 options, you only have default behaviour.
589  
590 =head1 ERROR HANDLING
591  
592 If you request an unknown encoding in the call to new Cz::Cstocs,
593 the conversion object is not defined and the variable
594 $Cz::Cstocs::errstr is set to the error message. When you specify
595 unknown encoding in the use call style (like C<use Cz::Cstocs
596 'il2_ascii';>), the die is called.
597  
598 =head1 AUTHOR
599  
600 Jan Pazdziora created the module version.
601  
602 Jan "Yenya" Kasprzak has done the original Un*x implementation.
603  
604 =head1 VERSION
605  
606 3.43
607  
608 =head1 SEE ALSO
609  
610 cstocs(1), perl(1), or Xcstocs at
611 http://www.lut.fi/~kurz/programs/xcstocs.tar.gz.
612  
613 =cut
614  
615  
« lib/Cz/Sort.pm ↑ Summary lib/Cz/Cstocs/Getopt.pm »