some code cleanups

This commit is contained in:
Ward Wouts 2001-08-01 21:31:45 +00:00
parent 9770daf06f
commit 0b3629c4f5

View file

@ -15,19 +15,9 @@ use POSIX qw(tmpnam);
use Getopt::Long;
&cmdline;
#Read current dir
opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
while ( defined ($filename = readdir(DIRHANDLE)) ) {
push @dir, "$filename\n";
}
closedir(DIRHANDLE);
@source = sort @dir;
&read_cur_dir;
&lock;
if ($opt_f) { &read_pattern; }
if (@pattern) {
@target = @source;
&pattern_edit;
@ -37,19 +27,7 @@ if (@pattern) {
&read_temp($temp_file);
}
# run checks
unless ( scalar(@source) == scalar(@target) ) {
die "Aborting. Source and target list don't have the same number of lines.\n";
}
foreach $line (@target) {
if ( $line =~ m/^$/ ) { die "Aborting. You can't move to empy names.\n"; }
}
if ( &check_unique(@target) ) {
die "Aborting. You're trying to move multiple files to the same name.\n";
}
&run_checks;
# if it's unsafe to move files directly, move the unsafe ones to a
# temporary file first
@ -57,11 +35,24 @@ if (@unsafe = &check_safety(\@source, \@target)) {
&safety_belt(\@unsafe, \@source);
}
# final move
&move_files(\@source, \@target);
if ( -e ".mv_wrap" ) { unlink (".mv_wrap"); }
########################################################
# sub routines from here on...
#
#Read current dir
sub read_cur_dir {
my (@dir, $filename);
opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
while ( defined ($filename = readdir(DIRHANDLE)) ) {
push @dir, "$filename\n";
}
closedir(DIRHANDLE);
@source = sort @dir;
}
# call EDITOR or vi with filename
# edit($filename);
@ -93,8 +84,8 @@ sub read_temp {
close (TARGET);
}
# moves files from the names in one array to the names in another array
# must be called like:
# Moves files from the names in one array to the names in another array
# Must be called like:
# move_files(\@source, \@target)
sub move_files {
my ($from, $to) = @_;
@ -111,12 +102,14 @@ sub move_files {
}
}
# read pattern file
sub read_pattern {
open PAT, "< $opt_f" or die "Couldn't open pattern file: $?\n";
@pattern = <PAT>;
close PAT;
}
# use patterns to change filenames
sub pattern_edit {
my (@new_target, $pat);
foreach $pat (@pattern) {
@ -129,6 +122,21 @@ sub pattern_edit {
}
}
sub run_checks {
my $line;
unless ( scalar(@source) == scalar(@target) ) {
die "Aborting. Source and target list don't have the same number of lines.\n";
}
foreach $line (@target) {
if ( $line =~ m/^$/ ) { die "Aborting. You can't move to empy names.\n"; }
}
if ( &check_unique(@target) ) {
die "Aborting. You're trying to move multiple files to the same name.\n";
}
}
# returns 0 if all entries in @target_list are unique. 1 if not.
sub check_unique (@target_list){
my @uniqu;
@ -138,11 +146,11 @@ sub check_unique (@target_list){
return (scalar(@uniqu) != scalar(@target))
}
# compares one array of file names to another, making sure files
# Compares one array of file names to another, making sure files
# can be moved safely without overwriting each other
# must be called like:
# Must be called like:
# check_safety(\@source, \@target)
# returns an array of unsafe line numbers
# Returns an array of unsafe line numbers
sub check_safety {
my ($from, $to) = @_;
my ($i, $j, @danger);
@ -164,9 +172,8 @@ sub rand_file {
return $filename;
}
# moves files to a random filename and updates
# the source array
# must be called like:
# Moves files to a random filename and updates the source array
# Must be called like:
# safety_belt(\@unsafe, \@source)
sub safety_belt {
my ($unsafe, $source) = @_;
@ -198,11 +205,11 @@ sub unlock {
sub cmdline {
%optctl = ();
# getopts("e:hp:", \%option);
&GetOptions("e=s", "h", "p=s", \@pattern, "f=s");
&GetOptions(\%optctl, "e");
&help if $opt_h;
if ($opt_f) { &read_pattern; }
}
sub help {