| 1 |
#! /usr/bin/perl -w |
| 2 |
## ---------------------------------------------------------------------------- |
| 3 |
# ujconv |
| 4 |
# ----------------------------------------------------------------------------- |
| 5 |
# Mastering programmed by YAMASHINA Hio |
| 6 |
# |
| 7 |
# Copyright 2005 YAMASHINA Hio |
| 8 |
# ----------------------------------------------------------------------------- |
| 9 |
# $Id: ujconv 4697 2007-09-14 06:17:00Z pho $ |
| 10 |
# ----------------------------------------------------------------------------- |
| 11 |
package Unicode::Japanese::UJConv; |
| 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 $from = 'auto'; |
| 29 |
my $to = 'auto'; |
| 30 |
my $string; |
| 31 |
my @files; |
| 32 |
|
| 33 |
while(@_) |
| 34 |
{ |
| 35 |
my $key = shift; |
| 36 |
if( $key !~ /^-/ ) |
| 37 |
{ |
| 38 |
push(@files,$key); |
| 39 |
next; |
| 40 |
}elsif( $key eq '--' ) |
| 41 |
{ |
| 42 |
push(@files,@_); |
| 43 |
last; |
| 44 |
} |
| 45 |
if( $key eq '-f' ) |
| 46 |
{ |
| 47 |
$from = shift; |
| 48 |
next; |
| 49 |
}elsif( $key eq '-t' ) |
| 50 |
{ |
| 51 |
$to = shift; |
| 52 |
next; |
| 53 |
}elsif( $key eq '-s' ) |
| 54 |
{ |
| 55 |
my $value = shift; |
| 56 |
push(@files,[$key,$value]); |
| 57 |
next; |
| 58 |
}elsif( $key =~ /^(-h|--help)$/ ) |
| 59 |
{ |
| 60 |
print_usage(); |
| 61 |
return 1; |
| 62 |
}elsif( $key =~ /^(-V|--version)$/ ) |
| 63 |
{ |
| 64 |
print_version(); |
| 65 |
return 1; |
| 66 |
}elsif( $key =~ /^(-l|--list)$/ ) |
| 67 |
{ |
| 68 |
print_list(); |
| 69 |
return 1; |
| 70 |
}else |
| 71 |
{ |
| 72 |
die "unkown argument [$key]"; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if( $to eq 'auto' ) |
| 77 |
{ |
| 78 |
my $lang = $ENV{LANG}; |
| 79 |
if( $lang && $lang=~/\.(.*)/ ) |
| 80 |
{ |
| 81 |
my $code = $1; |
| 82 |
if( $code=~/^(ujis|jis|iso-2022-jp)$/i ) |
| 83 |
{ |
| 84 |
$to = 'jis'; |
| 85 |
}elsif( $code=~/^(ujis|eucJP)$/i ) |
| 86 |
{ |
| 87 |
$to = 'euc'; |
| 88 |
}elsif( $code=~/^(sjis|shift_?jis)$/i ) |
| 89 |
{ |
| 90 |
$to = 'sjis'; |
| 91 |
}elsif( $code=~/^(utf-?8)$/i ) |
| 92 |
{ |
| 93 |
$to = 'utf8'; |
| 94 |
} |
| 95 |
} |
| 96 |
if( $to eq 'auto' ) |
| 97 |
{ |
| 98 |
$to = $^O eq 'MSWin32' ? 'sjis' : 'euc'; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if( !@files ) |
| 103 |
{ |
| 104 |
print Unicode::Japanese->new($_,$from)->conv($to) while(<STDIN>); |
| 105 |
} |
| 106 |
foreach my $file (@files) |
| 107 |
{ |
| 108 |
my $text; |
| 109 |
if( ref($file) ) |
| 110 |
{ |
| 111 |
$text = $file->[1]; |
| 112 |
print Unicode::Japanese->new($text,$from)->conv($to); |
| 113 |
}elsif( $file eq '-' ) |
| 114 |
{ |
| 115 |
print Unicode::Japanese->new($_,$from)->conv($to) while(<STDIN>); |
| 116 |
}else |
| 117 |
{ |
| 118 |
open(FILE, '<:mmap', $file) or die "could not open file [$file] : $!"; |
| 119 |
print Unicode::Japanese->new($_,$from)->conv($to) while(<FILE>); |
| 120 |
close(FILE); |
| 121 |
} |
| 122 |
} |
| 123 |
1; |
| 124 |
} |
| 125 |
|
| 126 |
# ----------------------------------------------------------------------------- |
| 127 |
# print_usage(); |
| 128 |
# |
| 129 |
sub print_usage |
| 130 |
{ |
| 131 |
print "usage: ujconv [-f from_encode] [-t to_encode] [-s string] [files...]\n"; |
| 132 |
print "see \`perldoc ujconv' for details.\n"; |
| 133 |
} |
| 134 |
|
| 135 |
# ----------------------------------------------------------------------------- |
| 136 |
# print_version(); |
| 137 |
# |
| 138 |
sub print_version |
| 139 |
{ |
| 140 |
print "ujconv $VERSION\n"; |
| 141 |
print "Unicode::Janaese $Unicode::Japanese::VERSION\n"; |
| 142 |
} |
| 143 |
|
| 144 |
# ----------------------------------------------------------------------------- |
| 145 |
# print_list(); |
| 146 |
# |
| 147 |
sub print_list |
| 148 |
{ |
| 149 |
foreach my $enc (qw( |
| 150 |
utf8 |
| 151 |
ucs2 |
| 152 |
ucs4 |
| 153 |
utf16 |
| 154 |
jis |
| 155 |
euc |
| 156 |
euc-jp |
| 157 |
sjis |
| 158 |
cp932 |
| 159 |
sjis-imode |
| 160 |
sjis-doti |
| 161 |
sjis-jsky |
| 162 |
jis-jsky |
| 163 |
jis-au |
| 164 |
sjis-icon-au |
| 165 |
euc-icon-au |
| 166 |
jis-icon-au |
| 167 |
utf8-icon-au |
| 168 |
)) |
| 169 |
{ |
| 170 |
print "$enc\n"; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
__END__ |
| 175 |
|
| 176 |
=head1 NAME |
| 177 |
|
| 178 |
ujconv -- reinvented iconv(1) using Unicode::Japanese |
| 179 |
|
| 180 |
=head1 SYNOPSIS |
| 181 |
|
| 182 |
ujconv [-f from_encoding] [-t to_encoding] [-s string] [files...] |
| 183 |
ujconv -l |
| 184 |
ujconv -h |
| 185 |
ujconv -V |
| 186 |
|
| 187 |
=head1 VERSION |
| 188 |
|
| 189 |
ujconv 0.02 |
| 190 |
|
| 191 |
=head1 DESCRIPTION |
| 192 |
|
| 193 |
B<ujconv> is an iconv-like tool which is written in perl using |
| 194 |
Unicode::Japanese. |
| 195 |
|
| 196 |
B<ujconv> reads text from STDIN or files, convert them, and print them to |
| 197 |
STDOUT. |
| 198 |
|
| 199 |
Available options are as follows. Each options can be in short form (-f) or long |
| 200 |
form (--from): |
| 201 |
|
| 202 |
=over 4 |
| 203 |
|
| 204 |
=item -f,--from I<from_encoding> |
| 205 |
|
| 206 |
Convert characters from I<from_encoding>. Unlike B<iconv> this option can be |
| 207 |
omitted. In that case, the encoding of the input is guessed by B<ujconv>. |
| 208 |
|
| 209 |
=item -t,--to I<to_encoding> |
| 210 |
|
| 211 |
Convert characters to I<to_encoding>. |
| 212 |
|
| 213 |
=item -s,--string I<string> |
| 214 |
|
| 215 |
Input from the argument string instead of file or STDIN. |
| 216 |
|
| 217 |
=item -l,--list |
| 218 |
|
| 219 |
List all available encodings, one name per each lines. |
| 220 |
|
| 221 |
=item -h,--help |
| 222 |
|
| 223 |
Print a short help message. |
| 224 |
|
| 225 |
=item -V,--version |
| 226 |
|
| 227 |
Print the version of B<ujconv>. |
| 228 |
|
| 229 |
=back |
| 230 |
|
| 231 |
=head1 SEE ALSO |
| 232 |
|
| 233 |
L<Unicode::Japanese>, |
| 234 |
L<piconv(1)>, |
| 235 |
L<iconv(1)>, |
| 236 |
L<ujguess> |
| 237 |
|
| 238 |
=cut |
| 239 |
|
| 240 |
# ----------------------------------------------------------------------------- |
| 241 |
# End of File. |
| 242 |
# ----------------------------------------------------------------------------- |