From 445c789564dff048e6dd0f769a16573bd74a00ab Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Thu, 12 Dec 2002 09:52:42 +0000 Subject: [PATCH] cleanup a bit... templating... --- mvwrap/mvwrap | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/mvwrap/mvwrap b/mvwrap/mvwrap index d5372bb..f088051 100755 --- a/mvwrap/mvwrap +++ b/mvwrap/mvwrap @@ -6,7 +6,7 @@ # Script to let an editor loose on a directory listing # Much easier than doing 6000 moves by hand # I think it's relatively safe now -# (C) 2001 Ward Wouts +# (C) 2001-2002 Ward Wouts # use IO::File; @@ -30,17 +30,12 @@ if (@pattern) { &run_checks; -if ($paranoia) { # extra checks for a specified list of files - ¶noia(\@source, \@target); -} - # if it's unsafe to move files directly, move the unsafe ones to a # temporary file first if (@unsafe = &check_safety(\@source, \@target)) { &safety_belt(\@unsafe, \@source); } - &move_files(\@source, \@target); &unlock; @@ -65,7 +60,7 @@ sub read_cur_dir { # call EDITOR or vi with filename # edit($filename); -sub edit { +sub edit($) { my $filename = shift; @editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename"); if ($opt_e) { @editor = ($opt_e, "$filename") } @@ -86,7 +81,7 @@ sub open_temp { return $target_name; } -sub read_temp { +sub read_temp($) { my $target_name = shift; open TARGET, $target_name or &cdie("Couldn't open tempfile: $target_name: $!"); foreach () { @@ -99,7 +94,7 @@ sub read_temp { # 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 { +sub move_files($$) { my ($from, $to) = @_; my ($i, $source, $target); for ( $i=0; $i < scalar(@$from); $i++ ) { @@ -139,32 +134,38 @@ sub pattern_edit { sub run_checks { my $line; + unless ( scalar(@source) == scalar(@target) ) { &cdie("Aborting. Source and target list don't have the same number of lines.\n"); } - + foreach $line (@target) { if ( $line =~ m/^$/ ) { &cdie("Aborting. You can't move to empty names.\n"); } } - + if ( &check_unique(@target) ) { &which_doubles(@source, @target); &cdie("Aborting. You're trying to move multiple files to the same name.\n"); } + + if ($paranoia) { # extra checks for a specified list of files + ¶noia(\@source, \@target); + } + return 0; } # returns 0 if all entries in @target_list are unique. 1 if not. -sub check_unique (@) { - my @uniqu; +sub check_unique(@) { my @target_list = @_; + my @uniqu; my %seen = (); @uniqu = grep { ! $seen{$_} ++ } @target_list; return (scalar(@uniqu) != scalar(@target)) } -sub which_doubles () { +sub which_doubles { my %selector = (); foreach $i (0 .. $#target) { if ( exists $selector{$target[$i]} ) { @@ -186,7 +187,7 @@ sub which_doubles () { # Must be called like: # check_safety(\@source, \@target) # Returns an array of unsafe line numbers -sub check_safety { +sub check_safety($$) { my ($from, $to) = @_; my ($i, $j, @changed, @danger, @unique, %seen); my ($a, $b); @@ -224,7 +225,7 @@ sub rand_file { # Moves files to a random filename and updates the source array # Must be called like: # safety_belt(\@unsafe, \@source) -sub safety_belt { +sub safety_belt($$) { my ($unsafe, $source) = @_; my($filenr, $filename, $rand); foreach $filenr (@$unsafe) { @@ -244,8 +245,8 @@ sub safety_belt { # Extra safety checks when one's useing a specified list of files # Must be called like: # paranoia(\@unsafe, \@source) -sub paranoia { - my ($source, $target) =@_; +sub paranoia($$) { + my ($source, $target) = @_; my $safe; foreach $ctarget (@$target) { $safe = 0; @@ -281,6 +282,9 @@ sub unlock { # clean up lock & die sub cdie { + #if (@pattern) { + # + #} $message = shift; &unlock; die "$message";