| 1 |
#! /usr/bin/perl -w |
| 2 |
## ---------------------------------------------------------------------------- |
| 3 |
# ujguess |
| 4 |
# ----------------------------------------------------------------------------- |
| 5 |
# Mastering programmed by YAMASHINA Hio |
| 6 |
# |
| 7 |
# Copyright 2005 YAMASHINA Hio |
| 8 |
# ----------------------------------------------------------------------------- |
| 9 |
# $Id: ujguess 4697 2007-09-14 06:17:00Z pho $ |
| 10 |
# ----------------------------------------------------------------------------- |
| 11 |
package Unicode::Japanese::UJGuess; |
| 12 |
use strict; |
| 13 |
use Unicode::Japanese; |
| 14 |
our $VERSION = '0.02'; |
| 15 |
|
| 16 |
if( !caller ) |
| 17 |
{ |
| 18 |
__PACKAGE__->do_work(@ARGV); |
| 19 |
} |
| 20 |
|
| 21 |
# ----------------------------------------------------------------------------- |
| 22 |
# main. |
| 23 |
# |
| 24 |
sub do_work |
| 25 |
{ |
| 26 |
my $pkg = shift; |
| 27 |
|
| 28 |
my $string; |
| 29 |
my @files; |
| 30 |
my $no_filename; |
| 31 |
|
| 32 |
while(@_) |
| 33 |
{ |
| 34 |
my $key = shift; |
| 35 |
if( $key !~ /^-/ ) |
| 36 |
{ |
| 37 |
push(@files,$key); |
| 38 |
next; |
| 39 |
}elsif( $key eq '--' ) |
| 40 |
{ |
| 41 |
push(@files,@_); |
| 42 |
last; |
| 43 |
} |
| 44 |
if( $key eq '--no-filename' ) |
| 45 |
{ |
| 46 |
$no_filename = 1; |
| 47 |
next; |
| 48 |
}elsif( $key eq '--show-filename' ) |
| 49 |
{ |
| 50 |
$no_filename = 0; |
| 51 |
next; |
| 52 |
}elsif( $key eq '-s' ) |
| 53 |
{ |
| 54 |
my $value = shift; |
| 55 |
push(@files,[$key,$value]); |
| 56 |
next; |
| 57 |
}elsif( $key =~ /^(-h|--help)$/ ) |
| 58 |
{ |
| 59 |
print_usage(); |
| 60 |
return 1; |
| 61 |
}elsif( $key =~ /^(-V|--version)$/ ) |
| 62 |
{ |
| 63 |
print_version(); |
| 64 |
return 1; |
| 65 |
}else |
| 66 |
{ |
| 67 |
die "unkown argument [$key]"; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
!defined($no_filename) and $no_filename = @files<=1; |
| 72 |
|
| 73 |
Unicode::Japanese->new(); # load stub. |
| 74 |
local($/) = undef; |
| 75 |
if( !@files ) |
| 76 |
{ |
| 77 |
my $text = <STDIN>; |
| 78 |
$no_filename or print "-:"; |
| 79 |
print Unicode::Japanese->getcode($text)."\n"; |
| 80 |
} |
| 81 |
foreach my $file (@files) |
| 82 |
{ |
| 83 |
my $filename; |
| 84 |
my $text; |
| 85 |
if( ref($file) ) |
| 86 |
{ |
| 87 |
$filename = join(' ',@$file); |
| 88 |
$text = $file->[1]; |
| 89 |
}elsif( $file eq '-' ) |
| 90 |
{ |
| 91 |
$filename = '-'; |
| 92 |
$text = <STDIN>; |
| 93 |
}else |
| 94 |
{ |
| 95 |
$filename = $file; |
| 96 |
open(FILE,$file) or die "could not open file [$file] : $!"; |
| 97 |
$text = <FILE>; |
| 98 |
close(FILE); |
| 99 |
} |
| 100 |
$no_filename or print "$filename:"; |
| 101 |
print Unicode::Japanese->getcode($text)."\n"; |
| 102 |
} |
| 103 |
1; |
| 104 |
} |
| 105 |
|
| 106 |
# ----------------------------------------------------------------------------- |
| 107 |
# print_usage(); |
| 108 |
# |
| 109 |
sub print_usage |
| 110 |
{ |
| 111 |
print "usage: ujguess [options] [files...]\n"; |
| 112 |
print "options:\n"; |
| 113 |
print " --no-filename print only the name of character set\n"; |
| 114 |
print " --show-filename print both names of the file and character set\n"; |
| 115 |
print " -h|--help print this message\n"; |
| 116 |
print " -V|--version print the version of ujguess\n"; |
| 117 |
} |
| 118 |
|
| 119 |
# ----------------------------------------------------------------------------- |
| 120 |
# print_version(); |
| 121 |
# |
| 122 |
sub print_version |
| 123 |
{ |
| 124 |
print "ujguess $VERSION\n"; |
| 125 |
print "Unicode::Janaese $Unicode::Japanese::VERSION\n"; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
__END__ |
| 130 |
|
| 131 |
=head1 NAME |
| 132 |
|
| 133 |
ujguess -- Guess encoding of given files |
| 134 |
|
| 135 |
=head1 SYNOPSIS |
| 136 |
|
| 137 |
ujguess [files..] |
| 138 |
|
| 139 |
=head1 VERSION |
| 140 |
|
| 141 |
ujguess 0.02 |
| 142 |
|
| 143 |
=head1 DESCRIPTION |
| 144 |
|
| 145 |
B<ujguess> guesses encoding of given files. |
| 146 |
|
| 147 |
=over 4 |
| 148 |
|
| 149 |
=item --no-filename |
| 150 |
|
| 151 |
Don't prepend file name to each results. |
| 152 |
This is the default behavior if there is just one file to be processed. |
| 153 |
|
| 154 |
=item --show-filename |
| 155 |
|
| 156 |
Prepend file name to each results. |
| 157 |
This is the default behavior if there are two or more files to be processed. |
| 158 |
|
| 159 |
=item -h,--help |
| 160 |
|
| 161 |
Print a short help message. |
| 162 |
|
| 163 |
=item -V,--version |
| 164 |
|
| 165 |
Print the version of B<ujguess>. |
| 166 |
|
| 167 |
=back |
| 168 |
|
| 169 |
=head1 SEE ALSO |
| 170 |
|
| 171 |
L<Unicode::Japanese>, |
| 172 |
L<ujconv> |
| 173 |
|
| 174 |
=cut |
| 175 |
|
| 176 |
# ----------------------------------------------------------------------------- |
| 177 |
# End of File. |
| 178 |
# ----------------------------------------------------------------------------- |