cleanup a bit...

templating...
This commit is contained in:
Ward Wouts 2002-12-12 09:52:42 +00:00
parent dbe4afb325
commit 445c789564

View file

@ -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
&paranoia(\@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 (<TARGET>) {
@ -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,6 +134,7 @@ 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");
}
@ -153,18 +149,23 @@ sub run_checks {
&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
&paranoia(\@source, \@target);
}
return 0;
}
# returns 0 if all entries in @target_list are unique. 1 if not.
sub check_unique(@) {
my @uniqu;
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,7 +245,7 @@ sub safety_belt {
# Extra safety checks when one's useing a specified list of files
# Must be called like:
# paranoia(\@unsafe, \@source)
sub paranoia {
sub paranoia($$) {
my ($source, $target) = @_;
my $safe;
foreach $ctarget (@$target) {
@ -281,6 +282,9 @@ sub unlock {
# clean up lock & die
sub cdie {
#if (@pattern) {
#
#}
$message = shift;
&unlock;
die "$message";