diff --git a/cwFileSys.cpp b/cwFileSys.cpp index b4b035f..034c2af 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -241,6 +241,68 @@ char* cw::filesys::makeVersionedFn( const char* dir, const char* fn_prefix, cons return fnOut; } +char* cw::filesys::replaceDirectory( const char* fn0, const char* dir ) +{ + pathPart_t* pp = nullptr; + char* fn = nullptr; + + if((pp = pathParts( fn0 )) == nullptr ) + { + cwLogError(kOpFailRC,"File name parse failed."); + return nullptr; + } + + if((fn = makeFn( dir, pp->fnStr, pp->extStr)) == nullptr ) + { + cwLogError(kOpFailRC,"Unable to replace directory."); + } + + mem::release(pp); + return fn; + +} + +char* cw::filesys::replaceFilename( const char* fn0, const char* name ) +{ + pathPart_t* pp = nullptr; + char* fn = nullptr; + + if((pp = pathParts( fn0 )) == nullptr ) + { + cwLogError(kOpFailRC,"File name parse failed."); + return nullptr; + } + + if((fn = makeFn( pp->dirStr, name, pp->extStr)) == nullptr ) + { + cwLogError(kOpFailRC,"Unable to replace file name."); + } + + mem::release(pp); + return fn; +} + +char* cw::filesys::replaceExtension( const char* fn0, const char* ext ) +{ + pathPart_t* pp = nullptr; + char* fn = nullptr; + + if((pp = pathParts( fn0 )) == nullptr ) + { + cwLogError(kOpFailRC,"File name parse failed."); + return nullptr; + } + + if((fn = makeFn( pp->dirStr, pp->fnStr, ext)) == nullptr ) + { + cwLogError(kOpFailRC,"Unable to replace file extension."); + } + + mem::release(pp); + return fn; +} + + char* cw::filesys::expandPath( const char* dir ) { diff --git a/cwFileSys.h b/cwFileSys.h index af9a6ff..30be347 100644 --- a/cwFileSys.h +++ b/cwFileSys.h @@ -27,6 +27,12 @@ namespace cw char* vMakeVersionedFn(const char* dir, const char* fn_prefix, const char* ext, va_list vl ); char* makeVersionedFn( const char* dir, const char* fn_prefix, const char* ext, ... ); + // Replace the directory/name/extension part of a complete path. + // The returned string must be release by a call to mem::release() or mem::free(). + char* replaceDirectory( const char* fn, const char* dir ); + char* replaceFilename( const char* fn, const char* name ); + char* replaceExtension( const char* fn, const char* ext ); + // The returned string must be released by a call to mem::release() or mem::free(). char* expandPath( const char* dir ); @@ -44,7 +50,7 @@ namespace cw // Given a file name decompose it into a directory string, file name string and file extension string. // The returned record and the strings it points to are contained in a single block of - // memory which must be released by a call to memRelease() or memFree() + // memory which must be released by a call to mem::release() or mem::free() pathPart_t* pathParts( const char* pathNameStr ); // Flags used by dirEntries 'includeFlags' parameter.