added mvBak() to POSIX

- Rename to a corresponding backup file
  If the backup file already exists, attempt with "01" .. "99" suffix.

- git ignore indexed backup files too
This commit is contained in:
Mark Olesen
2009-07-20 11:04:48 +02:00
parent 359f14ae91
commit 8ff026095d
6 changed files with 142 additions and 12 deletions

View File

@ -781,6 +781,45 @@ bool Foam::mv(const fileName& src, const fileName& dst)
}
//- Rename to a corresponding backup file
// If the backup file already exists, attempt with "01" .. "99" index
bool Foam::mvBak(const fileName& src, const std::string& ext)
{
if (POSIX::debug)
{
Info<< "mvBak : " << src << " to extension " << ext << endl;
}
if (exists(src, false))
{
const int maxIndex = 99;
char index[3];
for (int n = 0; n <= maxIndex; n++)
{
fileName dstName(src + "." + ext);
if (n)
{
sprintf(index, "%02d", n);
dstName += index;
}
// avoid overwriting existing files, except for the last
// possible index where we have no choice
if (!exists(dstName, false) || n == maxIndex)
{
return rename(src.c_str(), dstName.c_str()) == 0;
}
}
}
// fall-through: nothing to do
return false;
}
// Remove a file, returning true if successful otherwise false
bool Foam::rm(const fileName& file)
{