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
|
#! /usr/bin/perl
use strict;
use warnings;
## Initialisations
use Encode;
use Getopt::Long;
use Pod::Usage;
$| = 1; # Autoflush on
sub from_to_file ($);
sub recode_dir ($);
Getopt::Long::Configure ("bundling", "ignore_case");
my ($verbose, $recursive, $all_files, $backup, $force, $from, $to) =
(0, 0, 0, 2, 0, "ISO-8859-1", "UTF-8");
GetOptions( "verbose|v" => \$verbose, "recursive|r" => \$recursive,
"backup|b!" => \$backup, "all-files|a" => \$all_files,
"force|F" => \$force, "from|f=s" => \$from, "to|t=s" => \$to )
or pod2usage(-exitval => 2, -verbose => 1);;
unless( @ARGV ) { pod2usage(-exitval => 2, -verbose => 1) }
if( $backup == 2 ) {
print "Do you want to backup your files ?(Y/n) ";
if( <STDIN> =~ /^no?\s*/i ){
$backup = 0;
}
}
## Main
foreach my $file ( @ARGV ){
if( -f $file ){
from_to_file( $file );
} elsif( -d $file ) {
recode_dir( $file );
}
}
## Fonctions
sub from_to_file ($) {
my $file = shift ;
my $file_dest = $file.".new";
my ($in, $out);
while( -e $file_dest ) { $file_dest .= ".new" }
open $in, "<:bytes", $file or die "$!\n";
open $out, ">:bytes", $file_dest or die "$!\n";
print "Encode $file from $from to $to.\n" if $verbose;
while(<$in>){
if ( $force ) {
unless( defined Encode::from_to( $_, $from, $to, Encode::FB_PERLQQ ) ) {
print "Error while encoding $file, are you sure that this file ",
"is encoded in $from ?\n";
}
} else {
eval { Encode::from_to( $_, $from, $to, 1 ) };
if( $@ ){
print "Error while encoding $file, are you sure that this file ",
"is encoded in $from ?\nEncoding of $file aborted !\n";
unlink $file_dest;
}
}
print {$out} $_;
}
# backup if asked for
if( $backup ){
my $file_bak = $file.".bak";
while( -e $file_bak ) { $file_bak .= ".bak" }
rename( $file, $file_bak );
}
close $in and close $out;
rename($file_dest, $file);
}
sub recode_dir ($) {
my $dir = shift ;
$dir .= "/" unless $dir =~ m</$>;
opendir RECDIR, $dir or die "$!\n";
my $file;
while( defined ( $file = readdir RECDIR ) ) {
unless( $file =~ /^\.{1,2}$/ ) {
if( -d $dir.$file and $recursive ) {
recode_dir( $dir.$file );
} elsif (-f $dir.$file and ($file !~ /^\./ or $all_files) ){
from_to_file( $dir.$file );
}
}
}
}
__END__
=head1 NAME
Recode.pl
=head1 SYNOPSIS
Recode.pl [options] [file|directory ...]
Options:
-v or --verbose Set the verbosity level of this program
-f or --from Encoding of the input files (default : ISO-8859-1)
-t or --to Target encoding (default : UTF-8)
-r or --recursive Process directories recursively
-a or --all-files Process hidden files too
-b or --backup Backup the files to *.bak
--nob or --nobackup To avoid the backup of the files
-F or --force The program try to convert all parts of a file
that seems to have the good encoding
B<This program> will read the given input file(s) and/or directory(ies)
and encode them with the encoding specified by --to. Be careful since
this program does B<not> check if the content of a file is really text !
=cut |
Partager