| 1 | #!/usr/local/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 24 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 24 | ▶ | use warnings FATAL => "utf8"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use open qw(:std :utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 24 | ▶ | use Getopt::Long qw[ GetOptions ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 24 | ▶ | use Pod::Usage qw[ pod2usage ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 24 | ▶ | use Carp qw[ confess ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 24 | ▶ | use Encode::Guess::Educated; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | ######################################################################## | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 24 | our $Errors = 0; | |||||||||||||||||||||||||||
| 19 | 24 | our $VERSION = "1.0 (2012-12-28)"; | |||||||||||||||||||||||||||
| 20 | 24 | our %Opt = (); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ######################################################################## | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 24 | main(); | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | exit($Errors == 0 ? 0 : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ######################################################################## | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 24 | ▶ | sub main { | ||||||||||||||||||||||||||
|
Subroutine
main: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 30 | 24 | set_traps(); | |||||||||||||||||||||||||||
| 31 | 24 | set_options(); | |||||||||||||||||||||||||||
| 32 | 0 | process_files(); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 24 | ▶ | sub set_traps { | ||||||||||||||||||||||||||
|
Subroutine
set_traps: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 24 | $| = 1; | |||||||||||||||||||||||||||
| 37 | 24 | ▶ | $SIG{__WARN__} = sub { confess "FATALIZED WARNING @_" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 24 | ▶ | $SIG{__DIE__} = sub { $^S || confess "UNCAUGHT FATAL @_" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 24 | ▶ | sub set_options { | ||||||||||||||||||||||||||
|
Subroutine
set_options: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 24 | Getopt::Long::Configure qw[ bundling auto_version ]; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 24 | ▶ | GetOptions(\%Opt, qw[ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | help|h|? | ||||||||||||||||||||||||||||
| 48 | man|m | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | debug|d | ||||||||||||||||||||||||||||
| 51 | dump-byte-table|B | ||||||||||||||||||||||||||||
| 52 | dump-training-data|D | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | training-datafile|t | ||||||||||||||||||||||||||||
| 55 | model|m | ||||||||||||||||||||||||||||
| 56 | list-models|L | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | short|s | ||||||||||||||||||||||||||||
| 59 | long|l | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | ]) || pod2usage(2); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ######################################################################## | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub process_files { | ||||||||||||||||||||||||||
|
Subroutine
process_files: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $obj = Encode::Guess::Educated->new(); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($Opt{"dump-bytes-table"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $obj->dump_byte_table(); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($Opt{"dump-training-data"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | $obj->dump_training_data(); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($Opt{debug}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | $obj->enable_debugging(1); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | for my $file (@ARGV) { | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | unless (-e $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | print STDERR "$0: can't stat $file: $!\n"; | |||||||||||||||||||||||||||
| 88 | 0 | $Errors++; | |||||||||||||||||||||||||||
| 89 | 0 | next; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | unless (-f _) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | print STDERR "$0: $file isn't a regular file\n"; | |||||||||||||||||||||||||||
| 94 | 0 | $Errors++; | |||||||||||||||||||||||||||
| 95 | 0 | next; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | unless (-s _) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | print STDERR "$0: $file is empty\n"; | |||||||||||||||||||||||||||
| 100 | 0 | $Errors++; | |||||||||||||||||||||||||||
| 101 | 0 | next; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my $found; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | eval { $found = $obj->guess_file_encoding($file) }; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | my $msg = $@; | |||||||||||||||||||||||||||
| 110 | # $msg =~ s/\s+at \S+ line \d.*$//s; | ||||||||||||||||||||||||||||
| 111 | 0 | $Errors++; | |||||||||||||||||||||||||||
| 112 | 0 | print STDERR "$0: $msg\n"; | |||||||||||||||||||||||||||
| 113 | 0 | next; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (! $found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | print STDERR "$0: Can't guess encoding of $file: ", $obj->get_errmsg(); | |||||||||||||||||||||||||||
| 118 | 0 | $Errors++; | |||||||||||||||||||||||||||
| 119 | 0 | next; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | my $enc = $obj->get_guessed_encoding(); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | print "$file: " if @ARGV > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | say $enc; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | next unless $Opt{long} || $Opt{short}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$main::Opt{"long"} or $main::Opt{"short"}
MC/DC0%0/2
$main::Opt{"long"} or $main::Opt{"short"}
$main::Opt{"long"}
$main::Opt{"short"}
Truth table0%0/3
A or B
A= $main::Opt{"long"}B= $main::Opt{"short"}
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $reason; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ($Opt{short}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 131 | 0 | $reason = $obj->get_short_report(); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | elsif ($Opt{long}) { | ||||||||||||||||||||||||||||
| 134 | 0 | $reason = $obj->get_long_report(); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 0 | die "not reached"; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | for ($reason) { | |||||||||||||||||||||||||||
| 141 | 0 | s/^/\t/gm; | |||||||||||||||||||||||||||
| 142 | 0 | s/\R*\z/\n/; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | say $reason; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||