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
|
#!/usr/bin/perl -w
use strict;
use Locale::Recode;
sub to_iso {
my($file) = $_[0];
my($file_dest) = $file.".new";
open FICHIER, "< $file" or die "$!\n";
open FICHIER_DEST, ">> $file_dest" or die "$!\n";
while(<FICHIER>){
my($cd) = Locale::Recode->new(from => 'UTF-8',
to=>'ISO-8859-1');
die $cd->getError if $cd->getError;
$cd->recode($_) or die $cd->getError;
print FICHIER_DEST;
}
close FICHIER and close FICHIER_DEST;
unlink $file and rename($file_dest, $file) and unlink $file_dest;
}
sub to_utf {
my($file) = $_[0];
my($file_dest) = $file.".new";
open FICHIER, "< $file" or die "$!\n";
open FICHIER_DEST, ">> $file_dest" or die "$!\n";
while(<FICHIER>){
my($cd) = Locale::Recode->new(from => 'ISO-8859-1',
to=>'UTF-8');
die $cd->getError if $cd->getError;
$cd->recode($_) or die $cd->getError;
print FICHIER_DEST;
}
close FICHIER and close FICHIER_DEST;
unlink $file and rename($file_dest, $file) and unlink $file_dest;
}
sub recode_file {
my($file) = $_[0];
my($file_dest) = $file.".new";
my($encode) = $_[1];
if($encode eq "1"){
&to_iso($file, $file_dest);
}
else {
&to_utf($file, $file_dest);
}
}
sub recode_repertory {
my($repertory) = $_[0];
my($pos) = rindex($repertory, '/');
if($pos != (length($repertory)-1)){
$repertory .= '/';
}
my($encode) = $_[1];
chdir $repertory;
my(@files) = `ls`;
my($file);
foreach $file (@files){
$file = $repertory.$file;
chomp $file;
if( -f $file){
&recode_file($file, $encode);
}
elsif ( -d $file){
&recode_repertory($file, $encode);
}
}
}
###################MAIN######################
if ($#ARGV > -1){
chomp $ARGV[0] and chomp $ARGV[1];
if ($ARGV[0] =~ /[A-Za-z]+/){
print "recode prend 2 arguments.\n";
print "Le premier argument doit être un nombre :\n";
print "1 : UTF-8 => ISO-8859-1\n";
print "2 : ISO-8859-1 => UTF-8\n";
print "Le deuxième argument doit être le chemin absolu d'un fichier ou d'un répertoire.\n";
exit 0;
}
if ($ARGV[0] > 2 or $ARGV[0] < 1){
print "Le premier argument doit être un nombre valide :\n";
print "1 : UTF-8 => ISO-8859-1\n";
print "2 : ISO-8859-1 => UTF-8\n";
exit 0;
}
if ( -f $ARGV[1]){
&recode_file($ARGV[1], $ARGV[0]);
}
elsif ( -d $ARGV[1]){
&recode_repertory($ARGV[1], $ARGV[0]);
}
}
elsif ($#ARGV > 1) {
print "recode prend 2 arguments.\n";
print "Le premier argument doit être un nombre :\n";
print "1 : UTF-8 => ISO-8859-1\n";
print "2 : ISO-8859-1 => UTF-8\n";
print "Le deuxième argument doit être le chemin absolu d'un fichier ou d'un répertoire.\n";
exit 0;
}
elsif ($#ARGV == 0) {
print "recode prend 2 arguments.\n";
print "Le premier argument doit être un nombre :\n";
print "1 : UTF-8 => ISO-8859-1\n";
print "2 : ISO-8859-1 => UTF-8\n";
print "Le deuxième argument doit être le chemin absolu d'un fichier ou d'un répertoire.\n";
exit 0;
}
else {
print "recode prend 2 arguments.\n";
print "Le premier argument doit être un nombre :\n";
print "1 : UTF-8 => ISO-8859-1\n";
print "2 : ISO-8859-1 => UTF-8\n";
print "Le deuxième argument doit être le chemin absolu d'un fichier ou d'un répertoire.\n";
exit 0;
} |
Partager