-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeEncoding
executable file
·137 lines (95 loc) · 3.02 KB
/
changeEncoding
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
#!/usr/bin/env perl -w
#
# changeEncoding: Switch between any of Perl's known character encodings.
#
# 2008-10-04: Written by Steven J. DeRose.
# 2010-09-12, 2012-10-10 sjd: Clean up. Use Encode.
#
# To do:
#
use strict;
use Getopt::Long;
use Encode;
our $VERSION_DATE = "2012-10-10";
my $from = "cp1252";
my $quiet = 0;
my $to = "utf8";
my $verbose = 0;
###############################################################################
#
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"from|iencoding=s" => \$from,
"h|help" => sub { system "perldoc $0"; exit; },
"help-encodings" => sub { showEncodings(); exit; },
"listEncodings" => sub {
warn "\nEncodings available:\n";
my $last = ""; my $buf = "";
for my $k (Encode->encodings(":all")) {
my $cur = substr($k,0,2);
if ($cur ne $last) {
warn "$buf\n";
$last = $cur; $buf = "";
}
$buf .= "$k ";
}
warn "$buf\n";
exit;
},
"q!" => \$quiet,
"to|oencoding=s" => \$to,
"v+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
);
($result) || die "Bad options.\n";
###############################################################################
###############################################################################
# Main
#
($ARGV[0]) || die "No input file specified.\n";
my $filename = shift;
open FH, "<:encoding($from)", "$filename" || die
"Can't open input file '$filename' with encoding '$from'.\n";
print "";
binmode(\*STDOUT, ":encoding($to)") || die
"Can't set encoding '$to' for STDOUT.\n";
my $recnum = 0;
while (my $rec = <FH>) {
$recnum++;
print $rec;
}
($quiet) || warn "Done, $recnum lines converted from $from to $to.\n";
exit;
###############################################################################
###############################################################################
###############################################################################
#
=pod
=head1 Usage
changeEncoding [options] file
Convert a file from one character encoding to another.
=head1 Options
=over
=item * B<--from> I<enc>
Encoding of input file.
=item * B<--listEncodings>
List all known encodings.
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<--to> I<encoding>
Encoding to convert to.
=item * B<--verbose> OR B<-v>
Add more messages, and check integrity frequently.
=item * B<--version>
Show version/license info and exit.
=back
=head1 Related commands/information
C<iconv> -- similar.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see http://creativecommons.org/licenses/by-sa/3.0/.
For the most recent version, see http://www.derose.net/steve/utilities/.
=cut