From f5c1c5e435028ffe558db310a5418663fb1905aa Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 21 May 2023 12:38:05 -0400 Subject: [PATCH] cwFileSys.h/cpp : Added formVersionedDirectory() and makeVersionedDirectory(). --- cwFileSys.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ cwFileSys.h | 5 ++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/cwFileSys.cpp b/cwFileSys.cpp index 38203ea..9325207 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -5,6 +5,7 @@ #include "cwCommonImpl.h" #include "cwMem.h" #include "cwString.h" +#include "cwText.h" #ifdef OS_LINUX #include // basename() dirname() @@ -804,6 +805,45 @@ cw::filesys::dirEntry_t* cw::filesys::dirEntries( const char* dirStr, unsigned f return r.rp; } +char* cw::filesys::formVersionedDirectory(const char* recordDir, const char* recordFolder) +{ + char* dir = nullptr; + + for(unsigned version_numb=0; true; ++version_numb) + { + unsigned n = textLength(recordFolder) + 32; + char folder[n+1]; + + snprintf(folder,n,"%s_%i",recordFolder,version_numb); + + if((dir = filesys::makeFn(recordDir,folder, NULL, NULL)) == nullptr ) + { + cwLogError(kOpFailRC,"Unable to form a versioned directory from:'%s'",cwStringNullGuard(recordDir)); + return nullptr; + } + + if( !filesys::isDir(dir) ) + break; + + mem::release(dir); + } + + return dir; +} + +char* cw::filesys::makeVersionedDirectory(const char* recordDir, const char* recordFolder ) +{ + rc_t rc = kOkRC; + char* dir; + if((dir = formVersionedDirectory(recordDir,recordFolder)) == nullptr ) + return nullptr; + + if((rc = makeDir(dir)) != kOkRC ) + return nullptr; + + return dir; +} + cw::rc_t cw::filesys::makeDir( const char* dirStr ) { diff --git a/cwFileSys.h b/cwFileSys.h index 411e9a4..ae2924c 100644 --- a/cwFileSys.h +++ b/cwFileSys.h @@ -85,7 +85,10 @@ namespace cw dirEntry_t* dirEntries( const char* dirStr, unsigned includeFlags, unsigned* dirEntryCntRef ); - + // Release the returned string memory via mem::release() + char* formVersionedDirectory(const char* recordDir, const char* recordFolder); + char* makeVersionedDirectory(const char* recordDir, const char* recordFolder ); + rc_t makeDir( const char* dirStr ); }