From 1ea10e6d7f8be44f6e1634759084ffec7b3a912f Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 17 Dec 2022 08:14:40 -0500 Subject: [PATCH] cwFile.h/cpp : backup() now takes an optional destination directory. --- cwFile.cpp | 7 +++++-- cwFile.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cwFile.cpp b/cwFile.cpp index 6ea28cf..b826d72 100644 --- a/cwFile.cpp +++ b/cwFile.cpp @@ -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; char* newName = nullptr; @@ -490,6 +490,9 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext ) goto errLabel; } + if( dst_dir == nullptr ) + dst_dir = pp->dirStr; + // iterate until a unique file name is found 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); // 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)); goto errLabel; diff --git a/cwFile.h b/cwFile.h index 4e29e20..28b0b5d 100644 --- a/cwFile.h +++ b/cwFile.h @@ -112,7 +112,7 @@ namespace cw // The integers chosen with zero and are incremented until an // 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. - 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. // Set *bufByteCntPtr to count of bytes read into the buffer.=