cwFileSys.h/cpp : Added formVersionedDirectory() and makeVersionedDirectory().

This commit is contained in:
kevin 2023-05-21 12:38:05 -04:00
parent d90b03d810
commit f5c1c5e435
2 changed files with 44 additions and 1 deletions

View File

@ -5,6 +5,7 @@
#include "cwCommonImpl.h" #include "cwCommonImpl.h"
#include "cwMem.h" #include "cwMem.h"
#include "cwString.h" #include "cwString.h"
#include "cwText.h"
#ifdef OS_LINUX #ifdef OS_LINUX
#include <libgen.h> // basename() dirname() #include <libgen.h> // basename() dirname()
@ -804,6 +805,45 @@ cw::filesys::dirEntry_t* cw::filesys::dirEntries( const char* dirStr, unsigned f
return r.rp; 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 ) cw::rc_t cw::filesys::makeDir( const char* dirStr )
{ {

View File

@ -85,7 +85,10 @@ namespace cw
dirEntry_t* dirEntries( const char* dirStr, unsigned includeFlags, unsigned* dirEntryCntRef ); 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 ); rc_t makeDir( const char* dirStr );
} }