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