codekingpro/portable-devtools
114k
1#!/usr/bin/perl2 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'3 if 0; # ^ Run only under a shell4#!./perl5use 5.008001;6BEGIN { pop @INC if $INC[-1] eq '.' }7use strict;8use warnings;9use Encode;10use Getopt::Std;11use Carp;12use Encode::Guess;13$Getopt::Std::STANDARD_HELP_VERSION = 1;14 15my %opt;16getopts( "huSs:", \%opt );17my @suspect_list;18list_valid_suspects() and exit if $opt{S};19@suspect_list = split /:,/, $opt{s} if $opt{s};20HELP_MESSAGE() if $opt{h};21HELP_MESSAGE() unless @ARGV;22do_guess($_) for @ARGV;23 24sub read_file {25 my $filename = shift;26 local $/;27 open my $fh, '<:raw', $filename or croak "$filename:$!";28 my $content = <$fh>;29 close $fh;30 return $content;31}32 33sub do_guess {34 my $filename = shift;35 my $data = read_file($filename);36 my $enc = guess_encoding( $data, @suspect_list );37 if ( !ref($enc) && $opt{u} ) {38 return 1;39 }40 print "$filename\t";41 if ( ref($enc) ) {42 print $enc->mime_name();43 }44 else {45 print "unknown";46 }47 print "\n";48 return 1;49}50 51sub list_valid_suspects {52 print join( "\n", Encode->encodings(":all") );53 print "\n";54 return 1;55}56 57sub HELP_MESSAGE {58 exec 'pod2usage', $0 or die "pod2usage: $!" 59}60__END__61=head1 NAME62 63encguess - guess character encodings of files64 65=head1 VERSION66 67$Id: encguess,v 0.4 2023/11/10 01:10:50 dankogai Exp $68 69=head1 SYNOPSIS70 71 encguess [switches] filename...72 73=head2 SWITCHES74 75=over 276 77=item -h78 79show this message and exit.80 81=item -s82 83specify a list of "suspect encoding types" to test, 84separated by either C<:> or C<,>85 86=item -S87 88output a list of all acceptable encoding types that can be used with89the -s param90 91=item -u92 93suppress display of unidentified types94 95=back96 97=head2 EXAMPLES:98 99=over 2100 101=item *102 103Guess encoding of a file named C<test.txt>, using only the default104suspect types.105 106 encguess test.txt107 108=item *109 110Guess the encoding type of a file named C<test.txt>, using the suspect111types C<euc-jp,shiftjis,7bit-jis>.112 113 encguess -s euc-jp,shiftjis,7bit-jis test.txt114 encguess -s euc-jp:shiftjis:7bit-jis test.txt115 116=item *117 118Guess the encoding type of several files, do not display results for119unidentified files.120 121 encguess -us euc-jp,shiftjis,7bit-jis test*.txt122 123=back124 125=head1 DESCRIPTION126 127The encoding identification is done by checking one encoding type at a128time until all but the right type are eliminated. The set of encoding129types to try is defined by the -s parameter and defaults to ascii,130utf8 and UTF-16/32 with BOM. This can be overridden by passing one or131more encoding types via the -s parameter. If you need to pass in132multiple suspect encoding types, use a quoted string with the a space133separating each value.134 135=head1 SEE ALSO136 137L<Encode::Guess>, L<Encode::Detect>138 139=head1 LICENSE AND COPYRIGHT140 141Copyright 2015 Michael LaGrasta and Dan Kogai.142 143This program is free software; you can redistribute it and/or modify it144under the terms of the Artistic License (2.0). You may obtain a145copy of the full license at:146 147L<http://www.perlfoundation.org/artistic_license_2_0>148 149=cut150 