cwFile.h/cpp : backup() now takes an optional destination directory.

This commit is contained in:
kevin 2022-12-17 08:14:40 -05:00
parent 376684e3b5
commit 1ea10e6d7f
2 changed files with 6 additions and 3 deletions

View File

@ -463,7 +463,7 @@ cw::rc_t cw::file::copy(
} }
cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext ) cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext, const char* dst_dir )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
char* newName = nullptr; char* newName = nullptr;
@ -490,6 +490,9 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext )
goto errLabel; goto errLabel;
} }
if( dst_dir == nullptr )
dst_dir = pp->dirStr;
// iterate until a unique file name is found // iterate until a unique file name is found
for(n=0; 1; ++n) for(n=0; 1; ++n)
{ {
@ -499,7 +502,7 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext )
newName = mem::printf(newName,"%s_%i",pp->fnStr,n); newName = mem::printf(newName,"%s_%i",pp->fnStr,n);
// form the new file name into a complete path // form the new file name into a complete path
if((newFn = filesys::makeFn(pp->dirStr,newName,pp->extStr,nullptr)) == nullptr ) if((newFn = filesys::makeFn(dst_dir,newName,pp->extStr,nullptr)) == nullptr )
{ {
rc = cwLogError(kOpFailRC,"A backup file name could not be formed for the file '%s'.",cwStringNullGuard(newName)); rc = cwLogError(kOpFailRC,"A backup file name could not be formed for the file '%s'.",cwStringNullGuard(newName));
goto errLabel; goto errLabel;

View File

@ -112,7 +112,7 @@ namespace cw
// The integers chosen with zero and are incremented until an // The integers chosen with zero and are incremented until an
// unused file name is found in the same directory as 'fn'. // unused file name is found in the same directory as 'fn'.
// If the file identified by 'fn' is not found then the function returns quietly. // If the file identified by 'fn' is not found then the function returns quietly.
rc_t backup( const char* dir, const char* name, const char* ext ); rc_t backup( const char* dir, const char* name, const char* ext, const char* dst_dir=nullptr );
// Allocate and fill a zero terminated string from a file. // Allocate and fill a zero terminated string from a file.
// Set *bufByteCntPtr to count of bytes read into the buffer.= // Set *bufByteCntPtr to count of bytes read into the buffer.=