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