Renaming files with the same timestamp: allow sequential numbering. Fixes bug #422

This commit is contained in:
Andreas Huggel 2005-05-22 04:20:32 +00:00
parent fe5a4c0022
commit 9b4cd05903
3 changed files with 57 additions and 10 deletions

View File

@ -660,16 +660,57 @@ namespace Action {
}
return 0;
}
bool go = true;
int seq = 1;
std::string s;
Params::FileExistsPolicy fileExistsPolicy
= Params::instance().fileExistsPolicy_;
while (go) {
if (Exiv2::fileExists(newPath)) {
switch (fileExistsPolicy) {
case Params::overwritePolicy:
go = false;
break;
case Params::renamePolicy:
newPath = Util::dirname(path)
+ EXV_SEPERATOR_STR + basename
+ "_" + Exiv2::toString(seq++)
+ Util::suffix(path);
break;
case Params::askPolicy:
std::cout << Params::instance().progname()
<< ": File `" << newPath
<< "' exists. [O]verwrite, [r]ename or [s]kip? ";
std::cin >> s;
switch (s[0]) {
case 'o':
case 'O':
go = false;
break;
case 'r':
case 'R':
fileExistsPolicy = Params::renamePolicy;
newPath = Util::dirname(path)
+ EXV_SEPERATOR_STR + basename
+ "_" + Exiv2::toString(seq++)
+ Util::suffix(path);
break;
default: // skip
return 0;
break;
}
}
}
else {
go = false;
}
}
if (Params::instance().verbose_) {
std::cout << "Renaming file to " << newPath << std::endl;
}
if (!Params::instance().force_ && Exiv2::fileExists(newPath)) {
std::cout << Params::instance().progname()
<< ": Overwrite `" << newPath << "'? ";
std::string s;
std::cin >> s;
if (s[0] != 'y' && s[0] != 'Y') return 0;
}
// Workaround for MinGW rename which does not overwrite existing files
remove(newPath.c_str());
if (::rename(path.c_str(), newPath.c_str()) == -1) {

View File

@ -192,6 +192,7 @@ void Params::help(std::ostream& os) const
<< " -V Show the program version and exit.\n"
<< " -v Be verbose during the program run.\n"
<< " -f Do not prompt before overwriting existing files (force).\n"
<< " -F Do not prompt before renaming existing files (Force).\n"
<< " -a time Time adjustment in the format [-]HH[:MM[:SS]]. This option\n"
<< " is only used with the `adjust' action.\n"
<< " -p mode Print mode for the `print' action. Possible modes are:\n"
@ -229,7 +230,8 @@ int Params::option(int opt, const std::string& optarg, int optopt)
case 'h': help_ = true; break;
case 'V': version_ = true; break;
case 'v': verbose_ = true; break;
case 'f': force_ = true; break;
case 'f': force_ = true; fileExistsPolicy_ = overwritePolicy; break;
case 'F': force_ = true; fileExistsPolicy_ = renamePolicy; break;
case 'r': rc = evalRename(optarg); break;
case 'a': rc = evalAdjust(optarg); break;
case 'p': rc = evalPrint(optarg); break;

View File

@ -124,12 +124,15 @@ public:
enum PrintMode { pmSummary, pmInterpreted, pmValues, pmHexdump, pmIptc,
pmComment };
//! Enumerates common targets, bitmap
enum commonTarget { ctExif = 1, ctIptc = 2, ctComment = 4, ctThumb = 8 };
enum CommonTarget { ctExif = 1, ctIptc = 2, ctComment = 4, ctThumb = 8 };
//! Enumerates the policies to handle existing files in rename action
enum FileExistsPolicy { overwritePolicy, renamePolicy, askPolicy };
bool help_; //!< Help option flag.
bool version_; //!< Version option flag.
bool verbose_; //!< Verbose (talkative) option flag.
bool force_; //!< Force overwrites flag.
FileExistsPolicy fileExistsPolicy_; //!< What to do if file to rename exists.
bool adjust_; //!< Adjustment flag.
PrintMode printMode_; //!< Print mode.
//! %Action (integer rather than TaskType to avoid dependency).
@ -149,11 +152,12 @@ private:
@brief Default constructor. Note that optstring_ is initialized here.
The c'tor is private to force instantiation through instance().
*/
Params() : optstring_(":hVvfa:r:p:d:e:i:m:M:l:"),
Params() : optstring_(":hVvfFa:r:p:d:e:i:m:M:l:"),
help_(false),
version_(false),
verbose_(false),
force_(false),
fileExistsPolicy_(askPolicy),
adjust_(false),
printMode_(pmSummary),
action_(0),