From 388814857c45c9076d88da6eb89c7180e9b7dbfd Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Sun, 29 Jul 2012 08:47:33 +0000 Subject: fileutil: introduce splitext() to get base and extension of filename Signed-off-by: Andy Shevchenko git-svn-id: file:///svnroot/renrot/trunk@618 fe2816f4-e837-0410-b10a-f608c9d244a1 --- lib/Image/RenRot/FileUtil.pm | 9 ++++++++- renrot | 30 ++++++++---------------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/Image/RenRot/FileUtil.pm b/lib/Image/RenRot/FileUtil.pm index 472fbf0..320aee7 100644 --- a/lib/Image/RenRot/FileUtil.pm +++ b/lib/Image/RenRot/FileUtil.pm @@ -15,7 +15,7 @@ use Image::RenRot::Logging; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); -@EXPORT = qw(makedir); +@EXPORT = qw(makedir splitext); ######################################################################################## # @@ -150,5 +150,12 @@ sub piper { return join("", @piped_arr); } +sub splitext { + my $filename = shift; + + return ($1, $2) if ($filename =~ m/(.*)\.([^\/\.]+)$/); + return ($filename, ""); +} + ######################################################################################## 1; # end diff --git a/renrot b/renrot index 8d4c599..ea0f97b 100755 --- a/renrot +++ b/renrot @@ -746,9 +746,6 @@ sub renameFile { $newFileName = $file; $filenameshash{$newFileName} = $unixTime; } else { - my $ext = ($file =~ m/(\.[^\.]+)$/) ? $1 : ""; - my $extLen = length($ext); - $newFileName = template2name ( $exifToolObj, $infoObj, @@ -759,11 +756,11 @@ sub renameFile { $angleSuffix ); - if ($filenameshash{$newFileName . $ext}) { - $newFileName .= "." . sprintf($counterSize, $fileCounter) . $ext; - } else { - $newFileName .= $ext; - } + my $ext = "." . (splitext($file))[1]; + + $newFileName .= "." . sprintf($counterSize, $fileCounter) if ($filenameshash{$newFileName . $ext}); + + $newFileName .= $ext; $filenameshash{$newFileName} = $unixTime; @@ -1633,15 +1630,6 @@ sub template2name { my $fileName = shift; my $counterSize = shift; my $angleSuffix = shift; - my ($base, $ext); # file name %n and extension %e - - if ($fileName =~ m/^(.*)\.([^\.]+)$/) { - $base = $1; - $ext = $2; - } else { - $base = $fileName; - $ext = ''; - } if (not defined $template) { fatalmsg ("Template isn't given!\n"), die_renrot(); @@ -1658,7 +1646,6 @@ sub template2name { my $WhiteBalance = ""; my $fileNameOriginal = ""; my $fileNameOriginalCounter = ""; # we can not use 0 as default value - my $fileNameOriginalExtensionLess = ""; if (defined $infoObj->{"FileNumber"}) { $FileNumber = $infoObj->{"FileNumber"}; @@ -1695,9 +1682,8 @@ sub template2name { if ($fileNameOriginal =~ m/^[[:alpha:]\-_]*(\d+)(\.[^\.]+)?$/) { $fileNameOriginalCounter = $1; } - if ($fileNameOriginal =~ m/^(.*)\.([^\.]+)$/) { - $fileNameOriginalExtensionLess = $1; - } + + my ($base, $ext) = splitext($fileName); # file name %n and extension %e my @templatearea = split (//, $template); my %templatehash = ( @@ -1716,7 +1702,7 @@ sub template2name { 'M' => $tm[5], 'm' => $tm[2], 'n' => $base, - 'O' => $fileNameOriginalExtensionLess, + 'O' => (splitext($fileNameOriginal))[0], 'o' => $fileNameOriginal, 'S' => $tm[6], 'W' => $WhiteBalance, -- cgit v1.2.1