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